diff --git a/.github/workflows/bashate_and_shellcheck.yaml b/.github/workflows/bashate_and_shellcheck.yaml deleted file mode 100644 index 5fcec19..0000000 --- a/.github/workflows/bashate_and_shellcheck.yaml +++ /dev/null @@ -1,29 +0,0 @@ -name: Lint Bash Scripts -on: - push: - branches: - - '*' - -jobs: - lint: - name: Run Bashate and ShellCheck - runs-on: ubuntu-latest - - steps: - - name: Checkout code - uses: actions/checkout@v2 - - - name: Build tool - run: | - make build # Creates the ./bin directory required by shellcheck afterwards - shell: bash - - - name: Run ShellCheck - run: | - make shellcheck # Execute the shellcheck target in the Makefile - shell: bash - - - name: Run Bashate - run: | - make bashate # Execute the bashate target in the Makefile - shell: bash diff --git a/.github/workflows/pull_request_workflow.yml b/.github/workflows/pull_request_workflow.yml new file mode 100644 index 0000000..c912046 --- /dev/null +++ b/.github/workflows/pull_request_workflow.yml @@ -0,0 +1,45 @@ +name: CI + +on: + push: + branches: [ "main" ] + pull_request: + branches: [ "main" ] + + +jobs: + + golang_lint: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-go@v4 + with: + go-version: "stable" + - run: make deps-update + - run: make fmt + - run: make vet + - run: make lint +# - run: make vendor-diff + - run: make golangci-lint + + golang_unittests: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-go@v4 + with: + go-version: "stable" + - run: make generate + - run: make unit-test + + bash_lint: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + - run: make build # Creates the ./bin directory required by shellcheck afterward + - run: make shellcheck + - run: make bashate diff --git a/.github/workflows/vulncheck_periodic.yaml b/.github/workflows/vulncheck_periodic.yml similarity index 100% rename from .github/workflows/vulncheck_periodic.yaml rename to .github/workflows/vulncheck_periodic.yml diff --git a/Dockerfile b/Dockerfile index 2f10505..5771769 100644 --- a/Dockerfile +++ b/Dockerfile @@ -10,7 +10,7 @@ WORKDIR /workspace COPY go.mod go.sum ./ COPY vendor/ vendor/ -# Copy the go source +# Copy the Go source installation_configuration_files COPY main.go main.go COPY cmd/ cmd/ COPY internal/ internal/ @@ -24,7 +24,7 @@ RUN curl -sL https://github.com/kubernetes-sigs/cri-tools/releases/download/$CRI ########### Runtime ########## -FROM registry.ci.openshift.org/ocp/4.13:tools +FROM registry.access.redhat.com/ubi9/ubi:latest WORKDIR / @@ -32,4 +32,8 @@ COPY --from=builder /workspace/ibu-imager . COPY --from=builder /workspace/crictl /usr/bin/ COPY installation_configuration_files/ installation_configuration_files/ +RUN yum -y install jq && \ + yum clean all && \ + rm -rf /var/cache/yum + ENTRYPOINT ["./ibu-imager"] diff --git a/Makefile b/Makefile index a79c609..5a38051 100644 --- a/Makefile +++ b/Makefile @@ -15,6 +15,11 @@ IMAGE_TAG_BASE ?= quay.io/lochoa/ibu-imager # Image URL to use all building/pushing image targets IMG ?= $(IMAGE_TAG_BASE):$(VERSION) +# Unittests variables +TEST_FORMAT ?= standard-verbose +GOTEST_FLAGS = --format=$(TEST_FORMAT) +GINKGO_FLAGS = -ginkgo.focus="$(FOCUS)" -ginkgo.v -ginkgo.skip="$(SKIP)" + default: help .PHONY: fmt @@ -27,6 +32,11 @@ vet: ## Run go vet against code. @echo "Running go vet" go vet ./... +.PHONY: lint +lint: ## Run golint against code. + @echo "Running golint" + hack/lint.sh + .PHONY: deps-update deps-update: ## Run go mod tidy and vendor against code. go mod tidy && go mod vendor @@ -41,16 +51,33 @@ bashate: ## Run bashate @echo "Running bashate" hack/bashate.sh -lint: vendor-diff - golangci-lint run -v +.PHONY: golangci-lint +golangci-lint: ## Run golangci-lint against code. + @echo "Running golangci-lint" + hack/golangci-lint.sh vendor-diff: go mod vendor && git diff --exit-code vendor +.PHONY: generate generate: + @echo "Running mockgen" + bash -c 'pushd "$(GOPATH)" && \ + go install github.com/golang/mock/mockgen@latest && \ + popd' go generate $(shell go list ./...) $(MAKE) fmt +_test: $(REPORTS) + gotestsum $(GOTEST_FLAGS) $(TEST) $(GINKGO_FLAGS) -timeout $(TIMEOUT) + +unit-test: ## Run unittests + @echo "Running unittests" + bash -c 'pushd "$(GOPATH)" && \ + go install gotest.tools/gotestsum@latest && \ + popd' + $(MAKE) _test TEST_SCENARIO=unit TIMEOUT=30m TEST="$(or $(TEST),$(shell go list ./...))" + ##@ Build build: deps-update fmt vet ## Build manager binary. diff --git a/README.md b/README.md index bd0d2dc..2ed6b8f 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # IBU Imager -[![Lint Bash Scripts](https://github.com/leo8a/ibu-imager/actions/workflows/bashate_and_shellcheck.yaml/badge.svg)](https://github.com/leo8a/ibu-imager/actions/workflows/bashate_and_shellcheck.yaml) +[![CI](https://github.com/leo8a/ibu-imager/actions/workflows/pull_request_workflow.yml/badge.svg)](https://github.com/leo8a/ibu-imager/actions/workflows/pull_request_workflow.yml) This application will assist users to easily create an OCI seed image for the Image-Based Upgrade (IBU) workflow, using a simple CLI. diff --git a/dev.Dockerfile b/dev.Dockerfile deleted file mode 100644 index 6b9e24a..0000000 --- a/dev.Dockerfile +++ /dev/null @@ -1,40 +0,0 @@ -########## Builder ########## -FROM registry.hub.docker.com/library/golang:1.19 AS builder - -ENV CRIO_VERSION="v1.28.0" - -# Set workring directory -WORKDIR /workspace - -# Copy the Go Modules installation_configuration_files -COPY go.mod go.sum ./ -COPY vendor/ vendor/ - -# Copy the go source -COPY main.go main.go -COPY cmd/ cmd/ -COPY internal/ internal/ - -# Build the binary -RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -mod=vendor -a -o ibu-imager main.go - -# Download crio CLI -RUN curl -sL https://github.com/kubernetes-sigs/cri-tools/releases/download/$CRIO_VERSION/crictl-$CRIO_VERSION-linux-amd64.tar.gz \ - | tar xvzf - -C . && chmod +x ./crictl - - -########### Runtime ########## -FROM registry.access.redhat.com/ubi9/ubi:latest - -WORKDIR / - -COPY --from=builder /workspace/ibu-imager . -COPY --from=builder /workspace/crictl /usr/bin/ -COPY installation_configuration_files/ installation_configuration_files/ - - -RUN yum -y install jq && \ - yum clean all && \ - rm -rf /var/cache/yum - -ENTRYPOINT ["./ibu-imager"] diff --git a/hack/clean.sh b/hack/clean.sh index 5b59ab2..47317b8 100755 --- a/hack/clean.sh +++ b/hack/clean.sh @@ -1,15 +1,13 @@ #!/bin/bash sudo rm -rf /var/tmp/container_list.done \ - /var/tmp/backup && \ + /var/tmp/backup && \ sudo rm -f /usr/local/bin/prepare-installation-configuration.sh \ - /usr/local/bin/installation-configuration.sh && \ + /usr/local/bin/installation-configuration.sh && \ sudo systemctl disable installation-configuration.service && \ sudo systemctl disable prepare-installation-configuration.service && \ rm -f /etc/systemd/system/installation-configuration.service \ - /etc/systemd/system/prepare-installation-configuration.service && \ + /etc/systemd/system/prepare-installation-configuration.service && \ sudo podman rmi quay.io/alosadag/ibu-seed-sno0:oneimage --force && \ sudo systemctl enable --now kubelet && \ -sudo systemctl enable --now crio - - + sudo systemctl enable --now crio diff --git a/hack/golangci-lint.sh b/hack/golangci-lint.sh index bea95b4..1ca849a 100755 --- a/hack/golangci-lint.sh +++ b/hack/golangci-lint.sh @@ -1,20 +1,18 @@ #!/bin/bash -which golangci-lint -if [ $? -ne 0 ]; then - echo "Downloading golangci-lint tool" - curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -d -b $(go env GOPATH)/bin v1.51.1 +export GOCACHE=/tmp/ +export GOLANGCI_LINT_CACHE=/tmp/.cache + - if [ $? -ne 0 ]; then - echo "Install from script failed. Trying go install" - go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.51.1 - if [ $? -ne 0 ]; then +if ! which golangci-lint &> /dev/null; then + echo "Downloading golangci-lint tool..." + if ! curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -d -b "$(go env GOPATH)/bin" v1.51.1; then + echo "Install from script failed. Trying 'go install'..." + if ! go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.51.1; then echo "Install of golangci-lint failed" exit 1 fi fi fi -export GOCACHE=/tmp/ -export GOLANGCI_LINT_CACHE=/tmp/.cache golangci-lint run --verbose --print-resources-usage --modules-download-mode=vendor --timeout=5m0s diff --git a/hack/lint.sh b/hack/lint.sh new file mode 100755 index 0000000..58b5c54 --- /dev/null +++ b/hack/lint.sh @@ -0,0 +1,20 @@ +#!/bin/bash + +RETVAL=0 +GENERATED_FILES="zz_generated.*.go" + + +cd $GOPATH || fatal "Failed to enter $GOPATH directory" +if ! which golint &> /dev/null; then + echo "Downloading golint tool..." + go install golang.org/x/lint/golint@latest +fi + +for file in $(find . -path ./vendor -prune -o -type f -name '*.go' -print | grep -E -v "$GENERATED_FILES"); do + golint -set_exit_status "$file" + if [[ $? -ne 0 ]]; then + RETVAL=1 + fi +done + +exit $RETVAL