Skip to content

Commit

Permalink
test(unit): add test convention
Browse files Browse the repository at this point in the history
- adjust `test` script file.
- give examples for using fake clientset in unit tests.
  • Loading branch information
Frank Mai authored and gitlawr committed Aug 20, 2020
1 parent 5e43d89 commit c3c0e6a
Show file tree
Hide file tree
Showing 29 changed files with 5,521 additions and 56 deletions.
3 changes: 3 additions & 0 deletions .golangci.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
"skip-files": [
"/zz_generated_"
],
"skip-dirs": [
"generated"
],
"deadline": "5m"
}
}
45 changes: 28 additions & 17 deletions Dockerfile.dapper
Original file line number Diff line number Diff line change
@@ -1,29 +1,40 @@
FROM golang:1.13.4-alpine3.10
FROM golang:1.13.15-buster
RUN apt-get update -qq && apt-get install -y --no-install-recommends \
xz-utils \
unzip \
netcat \
&& rm -rf /var/lib/apt/lists/*

ARG DAPPER_HOST_ARCH
ENV ARCH $DAPPER_HOST_ARCH

RUN apk -U add bash git gcc musl-dev docker vim less file curl wget ca-certificates
RUN go get -d golang.org/x/lint/golint && \
git -C /go/src/golang.org/x/lint/golint checkout -b current 06c8688daad7faa9da5a0c2f163a3d14aac986ca && \
go install golang.org/x/lint/golint && \
rm -rf /go/src /go/pkg
RUN mkdir -p /go/src/golang.org/x && \
cd /go/src/golang.org/x && git clone https://github.com/golang/tools && \
git -C /go/src/golang.org/x/tools checkout -b current aa82965741a9fecd12b026fbb3d3c6ed3231b8f8 && \
go install golang.org/x/tools/cmd/goimports
RUN rm -rf /go/src /go/pkg
RUN if [ "${ARCH}" == "amd64" ]; then \
curl -sL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s v1.15.0; \
# -- for make rules
## install docker client
RUN apt-get update -qq && apt-get install -y --no-install-recommends \
apt-transport-https \
ca-certificates \
curl \
gnupg \
&& rm -rf /var/lib/apt/lists/*; \
\
curl -fsSL https://download.docker.com/linux/debian/gpg | apt-key add - >/dev/null; \
echo "deb [arch=$(dpkg --print-architecture)] https://download.docker.com/linux/debian buster stable" > /etc/apt/sources.list.d/docker.list; \
\
apt-get update -qq && apt-get install -y --no-install-recommends \
docker-ce-cli=5:19.03.* \
&& rm -rf /var/lib/apt/lists/*
## install golangci
RUN if [ "$(go env GOARCH)" = "amd64" ]; then \
curl -sL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b "$(go env GOPATH)/bin" v1.27.0; \
fi
# -- for make rules

# -- for dapper
ENV GO111MODULE off
ENV DAPPER_ENV REPO TAG DRONE_TAG CROSS
ENV DAPPER_SOURCE /go/src/github.com/rancher/harvester/
ENV DAPPER_OUTPUT ./bin ./dist
ENV DAPPER_DOCKER_SOCKET true
ENV HOME ${DAPPER_SOURCE}
WORKDIR ${DAPPER_SOURCE}
# -- for dapper

WORKDIR ${DAPPER_SOURCE}
ENTRYPOINT ["./scripts/entry"]
CMD ["ci"]
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ require (
github.com/rancher/wrangler v0.6.2-0.20200622171942-7224e49a2407
github.com/rancher/wrangler-api v0.6.1-0.20200515193802-dcf70881b087
github.com/sirupsen/logrus v1.5.0
github.com/stretchr/testify v1.5.1
github.com/urfave/cli v1.22.2
golang.org/x/crypto v0.0.0-20200414173820-0848c9571904
k8s.io/api v0.18.0
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ github.com/blang/semver v3.1.0+incompatible/go.mod h1:kRBLl5iJ+tD4TcOOxsy/0fnweb
github.com/blang/semver v3.5.0+incompatible/go.mod h1:kRBLl5iJ+tD4TcOOxsy/0fnwebNt5EWlYSAyrTnjyyk=
github.com/blang/semver v3.5.1+incompatible h1:cQNTCjp13qL8KC3Nbxr/y2Bqb63oX6wdnnjpJbkM4JQ=
github.com/blang/semver v3.5.1+incompatible/go.mod h1:kRBLl5iJ+tD4TcOOxsy/0fnwebNt5EWlYSAyrTnjyyk=
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 h1:DDGfHa7BWjL4YnC6+E63dPcxHo2sUxDIu8g3QgEJdRY=
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869/go.mod h1:Ekp36dRnpXw/yCqJaO+ZrUyxD+3VXMFFr56k5XYrpB4=
github.com/bradfitz/gomemcache v0.0.0-20190913173617-a41fca850d0b/go.mod h1:H0wQNHz2YrLsuXOZozoeDmnHXkNCRmMW0gwFWDfEZDA=
github.com/brancz/gojsontoyaml v0.0.0-20190425155809-e8bd32d46b3d/go.mod h1:IyUJYN1gvWjtLF5ZuygmxbnsAyP3aJS6cHzIuZY50B0=
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/global/crds.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func createCRDs(ctx context.Context, _ *config.Scaled, server *server.Server) er
)...).
BatchCreateCRDs(ctx, crd.NamespacedTypes(
getCRDName(v1alpha1.SchemeGroupVersion, "VirtualMachineImage"),
getCRDName(v1alpha1.SchemeGroupVersion, "keyPair"),
getCRDName(v1alpha1.SchemeGroupVersion, "KeyPair"),
getCRDName(v1alpha1.SchemeGroupVersion, "VirtualMachineTemplate"),
getCRDName(v1alpha1.SchemeGroupVersion, "VirtualMachineTemplateVersion"),
)...).
Expand Down
12 changes: 0 additions & 12 deletions pkg/controller/global/image/register.go

This file was deleted.

5 changes: 2 additions & 3 deletions pkg/controller/global/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@ package global
import (
"context"

"github.com/rancher/steve/pkg/server"

"github.com/rancher/harvester/pkg/config"
"github.com/rancher/harvester/pkg/controller/global/image"
"github.com/rancher/harvester/pkg/controller/global/settings"
"github.com/rancher/steve/pkg/server"
)

type registerFunc func(context.Context, *config.Scaled, *server.Server) error

var registerFuncs = []registerFunc{
createCRDs,
settings.Register,
image.Register,
}

func Setup(ctx context.Context, server *server.Server) error {
Expand Down
12 changes: 6 additions & 6 deletions pkg/controller/master/keypair/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,22 @@ package keypair
import (
"fmt"

"golang.org/x/crypto/ssh"

apisv1alpha1 "github.com/rancher/harvester/pkg/apis/harvester.cattle.io/v1alpha1"
"github.com/rancher/harvester/pkg/generated/controllers/harvester.cattle.io/v1alpha1"
"golang.org/x/crypto/ssh"
)

// Handler computes key pairs' fingerprints
type Handler struct {
keyPairs v1alpha1.KeyPairController
keyPairCache v1alpha1.KeyPairCache
keyPairClient v1alpha1.KeyPairClient
}

func (h *Handler) OnKeyPairChanged(key string, keyPair *apisv1alpha1.KeyPair) (*apisv1alpha1.KeyPair, error) {
if keyPair == nil || keyPair.DeletionTimestamp != nil {
return nil, nil
return keyPair, nil
}

if keyPair.Spec.PublicKey == "" || keyPair.Status.FingerPrint != "" {
return keyPair, nil
}
Expand All @@ -33,6 +34,5 @@ func (h *Handler) OnKeyPairChanged(key string, keyPair *apisv1alpha1.KeyPair) (*
toUpdate.Status.FingerPrint = fingerPrint
apisv1alpha1.KeyPairValidated.True(toUpdate)
}
_, err = h.keyPairs.Update(toUpdate)
return keyPair, err
return h.keyPairClient.Update(toUpdate)
}
Loading

0 comments on commit c3c0e6a

Please sign in to comment.