From d6b06a878769662b17a23747d5d7aad538819f7b Mon Sep 17 00:00:00 2001 From: Ian Howell Date: Wed, 26 Jun 2019 11:41:22 -0500 Subject: [PATCH] Zuul: Switch from travis to zuul This PS adds the skeleton for a set of zuul checks and gates for airshipctl and removes the travis config. This PS also removes some dead code from the util package. This change is required to get unit tests passing. Change-Id: Ifb1be49cb1bb82c62a0085b6da9b8ff1b261a95b --- .travis.yml | 15 -------- Dockerfile | 21 +++++++++-- Makefile | 31 +++++++++++++--- README.md | 2 - pkg/util/util.go | 41 --------------------- pkg/util/util_test.go | 40 -------------------- playbooks/airship-airshipctl-common.yaml | 14 +++++++ playbooks/airship-airshipctl-lint-test.yaml | 27 ++++++++++++++ test/utilities.go | 3 -- zuul.d/jobs.yaml | 17 +++++++++ zuul.d/nodesets.yaml | 17 +++++++++ zuul.d/projects.yaml | 19 ++++++++++ 12 files changed, 137 insertions(+), 110 deletions(-) delete mode 100644 .travis.yml delete mode 100644 pkg/util/util.go delete mode 100644 pkg/util/util_test.go create mode 100644 playbooks/airship-airshipctl-common.yaml create mode 100644 playbooks/airship-airshipctl-lint-test.yaml create mode 100644 zuul.d/jobs.yaml create mode 100644 zuul.d/nodesets.yaml create mode 100644 zuul.d/projects.yaml diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index f498a3d10..000000000 --- a/.travis.yml +++ /dev/null @@ -1,15 +0,0 @@ -language: go - -go: - - "1.12" - -env: -- GO111MODULE=on - -script: make test - -git: - depth: 1 - -notifications: - email: false diff --git a/Dockerfile b/Dockerfile index 15cb199ad..dc285b904 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,20 @@ -FROM alpine +ARG GO_IMAGE=docker.io/golang:1.12.6-stretch +ARG RELEASE_IMAGE=scratch +FROM ${GO_IMAGE} as builder -COPY /bin/airshipctl /bin/airshipctl +SHELL [ "/bin/bash", "-cex" ] +ADD . /usr/src/airshipctl +WORKDIR /usr/src/airshipctl +ENV GO111MODULE=on -CMD airshipctl help +RUN make get-modules + +ARG MAKE_TARGET=build +RUN make ${MAKE_TARGET} && \ + if [[ "${MAKE_TARGET}" == 'lint' ]]; then \ + mkdir -p /usr/src/airshipctl/bin; \ + touch /usr/src/airshipctl/bin/airshipctl; \ + fi + +FROM ${RELEASE_IMAGE} as release +COPY --from=builder /usr/src/airshipctl/bin/airshipctl /usr/local/bin/airshipctl diff --git a/Makefile b/Makefile index daa0eb345..fd05d37b9 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ SHELL := /bin/bash -GO_FLAGS := -ldflags '-extldflags "-static"' +GO_FLAGS := -ldflags '-extldflags "-static"' -tags=netgo BINDIR := bin EXECUTABLE_CLI := airshipctl @@ -11,13 +11,20 @@ SCRIPTS_DIR := scripts LINTER_CMD := "github.com/golangci/golangci-lint/cmd/golangci-lint" run ADDTL_LINTERS := goconst,gofmt,unparam +# docker +DOCKER_MAKE_TARGET := build + # go options PKG := ./... TESTS := . +.PHONY: get-modules +get-modules: + @go mod download + .PHONY: build -build: - @CGO_ENABLED=0 go build -o $(BINDIR)/$(EXECUTABLE_CLI) $(GO_FLAGS) +build: get-modules + @GO111MODULE=on CGO_ENABLED=0 go build -o $(BINDIR)/$(EXECUTABLE_CLI) $(GO_FLAGS) .PHONY: test test: build @@ -28,15 +35,27 @@ test: unit-tests .PHONY: unit-tests unit-tests: build @echo "Performing unit test step..." - @go test -run $(TESTS) $(PKG) $(TESTFLAGS) + @GO111MODULE=on go test -run $(TESTS) $(PKG) $(TESTFLAGS) @echo "All unit tests passed" .PHONY: lint lint: @echo "Performing linting step..." - @go run ${LINTER_CMD} --enable ${ADDTL_LINTERS} + @GO111MODULE=on go run ${LINTER_CMD} --enable ${ADDTL_LINTERS} @echo "Linting completed successfully" +.PHONY: docker-image +docker-image: + @docker build . --build-arg MAKE_TARGET=$(DOCKER_MAKE_TARGET) + +.PHONY: docker-image-unit-tests +docker-image-unit-tests: DOCKER_MAKE_TARGET = unit-tests +docker-image-unit-tests: docker-image + +.PHONY: docker-image-lint +docker-image-lint: DOCKER_MAKE_TARGET = lint +docker-image-lint: docker-image + .PHONY: clean clean: @rm -fr $(BINDIR) @@ -49,4 +68,4 @@ docs: update-golden: TESTFLAGS += -update -v update-golden: PKG = github.com/ian-howell/airshipctl/cmd/... update-golden: - @go test $(PKG) $(TESTFLAGS) + @GO111MODULE=on go test $(PKG) $(TESTFLAGS) diff --git a/README.md b/README.md index 17ca6cac7..6caff8711 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,3 @@ -[![Build Status](https://travis-ci.com/ian-howell/airshipctl.svg?branch=master)](https://travis-ci.com/ian-howell/airshipctl) - # airshipctl ### Custom Plugins Tutorial diff --git a/pkg/util/util.go b/pkg/util/util.go deleted file mode 100644 index 6704edea3..000000000 --- a/pkg/util/util.go +++ /dev/null @@ -1,41 +0,0 @@ -package util - -import ( - "os" - "time" -) - -// IsReadable returns nil if the file at path is readable. An error is returned otherwise. -func IsReadable(path string) error { - f, err := os.Open(path) - if err != nil { - return err.(*os.PathError).Err - } - return f.Close() -} - -// Clock is mostly useful for unit testing, allowing for mocking out of time -var clock *time.Time - -// Now returns time.Now() if the Clock is nil. If the Clock is not nil, it is -// returned instead. This is useful for testing -func Now() time.Time { - if clock == nil { - return time.Now() - } - return *clock -} - -// InitClock creates a clock for unit testing -func InitClock() { - mockClock := time.Date(2000, time.January, 0, 0, 0, 0, 0, time.UTC) - clock = &mockClock -} - -// Clock gives access to the mocked clock -func Clock() time.Time { - if clock == nil { - InitClock() - } - return *clock -} diff --git a/pkg/util/util_test.go b/pkg/util/util_test.go deleted file mode 100644 index dab0217e9..000000000 --- a/pkg/util/util_test.go +++ /dev/null @@ -1,40 +0,0 @@ -package util_test - -import ( - "io/ioutil" - "os" - "testing" - - "github.com/ian-howell/airshipctl/pkg/util" -) - -func TestIsReadable(t *testing.T) { - file, err := ioutil.TempFile(os.TempDir(), "airshipctl") - if err != nil { - t.Fatalf("Unexpected error: %s", err.Error()) - } - defer os.Remove(file.Name()) - - if err := util.IsReadable(file.Name()); err != nil { - t.Errorf("Unexpected error: %s", err.Error()) - } - - expected := "permission denied" - err = file.Chmod(0000) - if err != nil { - t.Fatalf("Unexpected error: %s", err.Error()) - } - if err := util.IsReadable(file.Name()); err == nil { - t.Errorf("Expected '%s' error", expected) - } else if err.Error() != "permission denied" { - t.Errorf("Expected '%s' error, got '%s'", expected, err.Error()) - } - - expected = "no such file or directory" - os.Remove(file.Name()) - if err := util.IsReadable(file.Name()); err == nil { - t.Errorf("Expected '%s' error", expected) - } else if err.Error() != expected { - t.Errorf("Expected '%s' error, got '%s'", expected, err.Error()) - } -} diff --git a/playbooks/airship-airshipctl-common.yaml b/playbooks/airship-airshipctl-common.yaml new file mode 100644 index 000000000..d1dffe950 --- /dev/null +++ b/playbooks/airship-airshipctl-common.yaml @@ -0,0 +1,14 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +- hosts: all + roles: + - install-docker diff --git a/playbooks/airship-airshipctl-lint-test.yaml b/playbooks/airship-airshipctl-lint-test.yaml new file mode 100644 index 000000000..cf467c0ea --- /dev/null +++ b/playbooks/airship-airshipctl-lint-test.yaml @@ -0,0 +1,27 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +- hosts: primary + tasks: + + - name: Run Linter + block: + - name: "make docker-image-lint" + make: + chdir: "{{ zuul.project.src_dir }}" + target: docker-image-lint + + - name: Run Unit Tests + block: + - name: "make docker-image-unit-tests" + make: + chdir: "{{ zuul.project.src_dir }}" + target: docker-image-unit-tests diff --git a/test/utilities.go b/test/utilities.go index e2254cb0d..7cb978f9b 100644 --- a/test/utilities.go +++ b/test/utilities.go @@ -11,8 +11,6 @@ import ( "github.com/spf13/cobra" "k8s.io/apimachinery/pkg/runtime" - - "github.com/ian-howell/airshipctl/pkg/util" ) // UpdateGolden writes out the golden files with the latest values, rather than failing the test. @@ -35,7 +33,6 @@ type CmdTest struct { // output from its golden file, or generates golden files if the -update flag // is passed func RunTest(t *testing.T, test *CmdTest, cmd *cobra.Command) { - util.InitClock() actual := &bytes.Buffer{} cmd.SetOutput(actual) args := strings.Fields(test.CmdLine) diff --git a/zuul.d/jobs.yaml b/zuul.d/jobs.yaml new file mode 100644 index 000000000..773dda002 --- /dev/null +++ b/zuul.d/jobs.yaml @@ -0,0 +1,17 @@ +--- +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +- job: + name: airship-airshipctl-lint-test + pre-run: playbooks/airship-airshipctl-common.yaml + run: playbooks/airship-airshipctl-lint-test.yaml + nodeset: airship-airshipctl-single-node diff --git a/zuul.d/nodesets.yaml b/zuul.d/nodesets.yaml new file mode 100644 index 000000000..906ee1230 --- /dev/null +++ b/zuul.d/nodesets.yaml @@ -0,0 +1,17 @@ +--- +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +- nodeset: + name: airship-airshipctl-single-node + nodes: + - name: primary + label: ubuntu-bionic diff --git a/zuul.d/projects.yaml b/zuul.d/projects.yaml new file mode 100644 index 000000000..aa883cfd7 --- /dev/null +++ b/zuul.d/projects.yaml @@ -0,0 +1,19 @@ +--- +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +- project: + check: + jobs: + - airship-airshipctl-lint-test + gate: + jobs: + - airship-airshipctl-lint-test