From c88100cba7225fedd980fa2bedfa86a5e803196e Mon Sep 17 00:00:00 2001 From: chandankumar4 Date: Thu, 30 May 2024 18:01:30 +0530 Subject: [PATCH] Add lint and unit tests in github action Signed-off-by: chandankumar4 --- .github/workflows/ci.yaml | 67 +++++++++++++++++++++++++++++++++++++-- .gitignore | 8 +++++ Makefile | 18 ++--------- 3 files changed, 76 insertions(+), 17 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 1a188b1e..e0cd1dc8 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -1,5 +1,5 @@ -name: test +name: CI on: push: branches: @@ -7,6 +7,7 @@ on: pull_request: branches: - "main" + jobs: codegen: name: Codegen @@ -14,25 +15,87 @@ jobs: timeout-minutes: 5 steps: - name: Checkout code - uses: actions/checkout@v3 + uses: actions/checkout@v4 + - name: Restore go build cache uses: actions/cache@v3 with: path: ~/.cache/go-build key: ${{ runner.os }}-go-build-v1-${{ hashFiles('**/go.mod') }} + - name: Setup Golang uses: actions/setup-go@v4.0.0 with: go-version: '1.21' + - name: Add bins to PATH run: | echo /home/runner/go/bin >> $GITHUB_PATH echo /usr/local/bin >> $GITHUB_PATH + - name: Get dependencies run: go mod download + - name: Make codegen run: | echo 'GOPATH=/home/runner/go' >> $GITHUB_ENV make -B codegen + - name: Ensure nothing changed run: git diff --exit-code + + lint: + name: Lint + runs-on: ubuntu-latest + timeout-minutes: 10 + env: + GOPATH: /home/runner/go + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Setup Golang + uses: actions/setup-go@v5 + with: + go-version: '1.21' + + - name: golangci-lint + uses: golangci/golangci-lint-action@v6 + with: + version: v1.58 + args: --timeout=10m + + unit-tests: + name: Unit Tests + runs-on: ubuntu-latest + timeout-minutes: 15 + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Setup Golang + uses: actions/setup-go@v4.0.0 + with: + go-version: '1.21' + id: go + + - name: Install MockGen + run: go install github.com/golang/mock/mockgen@v1.6.0 + + - name: Restore Go build cache + uses: actions/cache@v3 + with: + path: ~/.cache/go-build + key: ${{ runner.os }}-go-build-v1-${{ github.run_id }} + + - name: Get dependencies + run: go mod download + + - name: Test + run: make test + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 + + - name: Verify docker image + run: make image \ No newline at end of file diff --git a/.gitignore b/.gitignore index 97b80188..7546539c 100644 --- a/.gitignore +++ b/.gitignore @@ -18,6 +18,14 @@ bin/* # Dependency directories (remove the comment below to include it) # vendor/ +# editor and IDE paraphernalia +.idea +.vscode +*.swp +*.swo +*~ +**/.DS_Store + # Go workspace file go.work diff --git a/Makefile b/Makefile index 3a7061b5..81f65a5e 100644 --- a/Makefile +++ b/Makefile @@ -8,12 +8,9 @@ IMG ?= numaplane-controller VERSION ?= latest # BASE_VERSION will be used during release process to bump up versions BASE_VERSION := latest -# Default cluster name where numaplane get deployed, update it as needed. -CLUSTER_NAME ?= staging-usw2-k8s IMAGE_NAMESPACE ?= quay.io/numaproj IMAGE_FULL_PATH ?= $(IMAGE_NAMESPACE)/$(IMG):$(VERSION) - BUILD_DATE=$(shell date -u +'%Y-%m-%dT%H:%M:%SZ') GIT_COMMIT=$(shell git rev-parse HEAD) GIT_BRANCH=$(shell git rev-parse --symbolic-full-name --verify --quiet --abbrev-ref HEAD) @@ -116,7 +113,7 @@ test: manifests generate fmt vet envtest ## Run tests. KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test -race -v ./... -coverprofile cover.out GOLANGCI_LINT = $(shell pwd)/bin/golangci-lint -GOLANGCI_LINT_VERSION ?= v1.54.2 +GOLANGCI_LINT_VERSION ?= v1.58.0 golangci-lint: @[ -f $(GOLANGCI_LINT) ] || { \ set -e ;\ @@ -141,13 +138,8 @@ build: manifests generate fmt vet ## Build manager binary. run: manifests generate fmt vet ## Run a controller from your host. go run -gcflags=${GCFLAGS} ./cmd/main.go -.PHONY: run-agent -run-agent: generate fmt vet ## Run agent from your host. - go run -gcflags=${GCFLAGS} cmd/agent/main.go - - clean: - -rm bin/manager -f + -rm -f bin/manager # If you wish to build the manager image targeting other platforms you can use the --platform flag. # (i.e. docker build --platform linux/arm64). However, you must enable docker buildKit for it. @@ -182,14 +174,10 @@ docker-buildx: ## Build and push docker image for the manager for cross-platform ##@ Deployment -ifndef ignore-not-found - ignore-not-found = false -endif - .PHONY: start start: image $(KUBECTL) apply -f tests/manifests/numaplane-ns.yaml - $(KUBECTL) kustomize tests/manifests | sed 's/CLUSTER_NAME_VALUE/$(CLUSTER_NAME)/g' | sed 's@quay.io/numaproj/@$(IMAGE_NAMESPACE)/@' | sed 's/$(IMG):$(BASE_VERSION)/$(IMG):$(VERSION)/' | $(KUBECTL) apply -f - + $(KUBECTL) kustomize tests/manifests | sed 's@quay.io/numaproj/@$(IMAGE_NAMESPACE)/@' | sed 's/$(IMG):$(BASE_VERSION)/$(IMG):$(VERSION)/' | $(KUBECTL) apply -f - ##@ Build Dependencies