diff --git a/.gitignore b/.gitignore index 39bfe06..979c7cd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,55 +1,10 @@ -# Temporary Build Files -build/_output -build/_test -# Created by https://www.gitignore.io/api/go,vim,emacs,visualstudiocode -### Emacs ### -# -*- mode: gitignore; -*- -*~ -\#*\# -/.emacs.desktop -/.emacs.desktop.lock -*.elc -auto-save-list -tramp -.\#* -# Org-mode -.org-id-locations -*_archive -# flymake-mode -*_flymake.* -# eshell files -/eshell/history -/eshell/lastdir -# elpa packages -/elpa/ -# reftex files -*.rel -# AUCTeX auto folder -/auto/ -# cask packages -.cask/ -dist/ -# Flycheck -flycheck_*.el -# server auth directory -/server/ -# projectiles files -.projectile -projectile-bookmarks.eld -# directory configuration -.dir-locals.el -# saveplace -places -# url cache -url/cache/ -# cedet -ede-projects.el -# smex -smex-items -# company-statistics -company-statistics-cache.el -# anaconda-mode -anaconda-mode/ +### IntelliJ ### +.idea/ +*.iws +*.iml +/out/ +.idea_modules/ + ### Go ### # Binaries for programs and plugins *.exe @@ -57,26 +12,30 @@ anaconda-mode/ *.dll *.so *.dylib -# Test binary, build with 'go test -c' + +# Test binary, built with `go test -c` *.test + # Output of the go coverage tool, specifically when used with LiteIDE *.out -### Vim ### -# swap -.sw[a-p] -.*.sw[a-p] -# session -Session.vim -# temporary -.netrwhist -# auto-generated tag files -tags + +### Go Patch ### +/vendor/ +/Godeps/ + ### VisualStudioCode ### .vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json + +### VisualStudioCode Patch ### +# Ignore all local history of files .history -# End of https://www.gitignore.io/api/go,vim,emacs,visualstudiocode -### IDEA ### -.idea/* -### MISC ### -bin/* -vendor/* \ No newline at end of file + +# devconsole-operator binaries +bin/ +tmp/ + +# Generated assets diff --git a/Dockerfile.builder b/Dockerfile.builder new file mode 100644 index 0000000..05c72a3 --- /dev/null +++ b/Dockerfile.builder @@ -0,0 +1,37 @@ +FROM centos:7 +LABEL maintainer "Devtools " +LABEL author "Devtools " +ENV LANG=en_US.utf8 +ARG USE_GO_VERSION_FROM_WEBSITE=0 + +# Some packages might seem weird but they are required by the RVM installer. +RUN yum install epel-release -y \ + && yum --enablerepo=centosplus --enablerepo=epel install -y \ + findutils \ + git \ + $(test "$USE_GO_VERSION_FROM_WEBSITE" != 1 && echo "golang") \ + make \ + procps-ng \ + tar \ + wget \ + which \ + && yum clean all + +RUN if [[ "$USE_GO_VERSION_FROM_WEBSITE" = 1 ]]; then cd /tmp \ + && wget https://dl.google.com/go/go1.10.linux-amd64.tar.gz \ + && echo "b5a64335f1490277b585832d1f6c7f8c6c11206cba5cd3f771dcb87b98ad1a33 go1.10.linux-amd64.tar.gz" > checksum \ + && sha256sum -c checksum \ + && tar -C /usr/local -xzf go1.10.linux-amd64.tar.gz \ + && rm -f go1.10.linux-amd64.tar.gz; \ + fi +ENV PATH=$PATH:/usr/local/go/bin + +# Get dep for Go package management and make sure the directory has full rwz permissions for non-root users +ENV GOPATH /tmp/go +RUN mkdir -p $GOPATH/bin && chmod a+rwx $GOPATH +RUN cd $GOPATH/bin \ + curl -L -s https://github.com/golang/dep/releases/download/v0.5.0/dep-linux-amd64 -o dep \ + echo "287b08291e14f1fae8ba44374b26a2b12eb941af3497ed0ca649253e21ba2f83 dep" > dep-linux-amd64.sha256 \ + sha256sum -c dep-linux-amd64.sha256 + +ENTRYPOINT ["/bin/bash"] diff --git a/Dockerfile.deploy b/Dockerfile.deploy new file mode 100644 index 0000000..a34ec95 --- /dev/null +++ b/Dockerfile.deploy @@ -0,0 +1,18 @@ +FROM centos:7 +LABEL maintainer "Devtools " +LABEL author "Devtools " +ENV LANG=en_US.utf8 +ENV INSTALL_PREFIX=/usr/local/devconsole-operator + +# Create a non-root user and a group with the same name: "devconsole-operator" +ENV F8_USER_NAME=devconsole-operator +RUN useradd --no-create-home -s /bin/bash ${F8_USER_NAME} + +COPY bin/devconsole-operator ${INSTALL_PREFIX}/bin/devconsole-operator + +# From here onwards, any RUN, CMD, or ENTRYPOINT will be run under the following user +USER ${F8_USER_NAME} + +WORKDIR ${INSTALL_PREFIX} + +ENTRYPOINT [ "bin/devconsole-operator" ] diff --git a/Dockerfile.deploy.rhel b/Dockerfile.deploy.rhel new file mode 100644 index 0000000..7dd5f4e --- /dev/null +++ b/Dockerfile.deploy.rhel @@ -0,0 +1,19 @@ +FROM quay.io/openshiftio/rhel-base-golang:latest + +LABEL maintainer "Devtools " +LABEL author "Devtools " +ENV LANG=en_US.utf8 +ENV F8_INSTALL_PREFIX=/usr/local/devconsole-operator + +# Create a non-root user and a group with the same name: "devconsole-operator" +ENV F8_USER_NAME=devconsole-operator +RUN useradd --no-create-home -s /bin/bash ${F8_USER_NAME} + +COPY bin/devconsole-operator ${F8_INSTALL_PREFIX}/bin/devconsole-operator + +# From here onwards, any RUN, CMD, or ENTRYPOINT will be run under the following user +USER ${F8_USER_NAME} + +WORKDIR ${F8_INSTALL_PREFIX} + +ENTRYPOINT [ "bin/devconsole-operator" ] diff --git a/Dockerfile.dev b/Dockerfile.dev new file mode 100644 index 0000000..1d9db20 --- /dev/null +++ b/Dockerfile.dev @@ -0,0 +1,7 @@ +# TODO: change this to a sane base image +FROM quay.io/openshiftio/toolchain-operator:faab15e + +ENV F8_INSTALL_PREFIX=/usr/local/devconsole-operator + +COPY bin/devconsole-operator ${F8_INSTALL_PREFIX}/bin/devconsole-operator +RUN echo chmod +x ${F8_INSTALL_PREFIX}/bin/devconsole-operator \ No newline at end of file diff --git a/Gopkg.lock b/Gopkg.lock index b3fa14d..55e3346 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -9,6 +9,14 @@ revision = "28a4bc8c44b3acbcc482cff0cdf7de29a4688b61" version = "v0.35.1" +[[projects]] + digest = "1:75d2b55b13298745ec068057251d05d65bbae0a668201fe45ad6986551a55601" + name = "github.com/BurntSushi/toml" + packages = ["."] + pruneopts = "NT" + revision = "3012a1dbe2e4bd1391d42b32f0577cb7bbc7f005" + version = "v0.3.1" + [[projects]] digest = "1:d8ebbd207f3d3266d4423ce4860c9f3794956306ded6c7ba312ecc69cdfbf04c" name = "github.com/PuerkitoBio/purell" @@ -100,6 +108,14 @@ revision = "1d29f06aebd59ccdf11ae04aa0334ded96e2d909" version = "v0.18.0" +[[projects]] + digest = "1:9059915429f7f3a5f18cfa6b7cab9a28721d7ac6db4079a62044aa229eb7f2a8" + name = "github.com/gobuffalo/envy" + packages = ["."] + pruneopts = "NT" + revision = "fa0dfdc10b5366ce365b7d9d1755a03e4e797bc5" + version = "v1.6.15" + [[projects]] digest = "1:932970e69f16e127aa0653b8263ae588cd127fa53273e19ba44332902c9826f2" name = "github.com/gogo/protobuf" @@ -207,6 +223,14 @@ revision = "7c29201646fa3de8506f701213473dd407f19646" version = "v0.3.7" +[[projects]] + digest = "1:f5b9328966ccea0970b1d15075698eff0ddb3e75889560aad2e9f76b289b536a" + name = "github.com/joho/godotenv" + packages = ["."] + pruneopts = "NT" + revision = "23d116af351c84513e1946b527c88823e476be13" + version = "v1.3.0" + [[projects]] digest = "1:1d39c063244ad17c4b18e8da1551163b6ffb52bd1640a49a8ec5c3b7bf4dbd5d" name = "github.com/json-iterator/go" @@ -215,6 +239,14 @@ revision = "1624edc4454b8682399def8740d46db5e4362ba4" version = "v1.1.5" +[[projects]] + digest = "1:4059c14e87a2de3a434430340521b5feece186c1469eff0834c29a63870de3ed" + name = "github.com/konsorten/go-windows-terminal-sequences" + packages = ["."] + pruneopts = "NT" + revision = "5c8c8bd35d3832f5d134ae1e1e375b69a4d25242" + version = "v1.0.1" + [[projects]] branch = "master" digest = "1:7d9fcac7f1228470c4ea0ee31cdfb662a758c44df691e39b3e76c11d3e12ba8f" @@ -227,6 +259,14 @@ pruneopts = "NT" revision = "60711f1a8329503b04e1c88535f419d0bb440bff" +[[projects]] + digest = "1:56dbf15e091bf7926cb33a57cb6bdfc658fc6d3498d2f76f10a97ce7856f1fde" + name = "github.com/markbates/inflect" + packages = ["."] + pruneopts = "NT" + revision = "24b83195037b3bc61fcda2d28b7b0518bce293b6" + version = "v1.0.4" + [[projects]] branch = "master" digest = "1:0e9bfc47ab9941ecc3344e580baca5deb4091177e84dd9773b48b38ec26b93d5" @@ -255,9 +295,14 @@ digest = "1:b186c2fcf87d05c5129ad0d89ea062853b0bdf6d6bca1ed4a22157962f2ad8dd" name = "github.com/operator-framework/operator-sdk" packages = [ + "internal/util/fileutil", + "internal/util/k8sutil", "pkg/k8sutil", "pkg/leader", "pkg/ready", + "pkg/scaffold", + "pkg/scaffold/input", + "pkg/test", "version", ] pruneopts = "NT" @@ -288,6 +333,45 @@ revision = "5f041e8faa004a95c88a202771f4cc3e991971e6" version = "v2.0.1" +[[projects]] + digest = "1:0028cb19b2e4c3112225cd871870f2d9cf49b9b4276531f03438a88e94be86fe" + name = "github.com/pmezard/go-difflib" + packages = ["difflib"] + pruneopts = "NT" + revision = "792786c7400a136282c1664665ae0a8db921c6c2" + version = "v1.0.0" + +[[projects]] + digest = "1:fcef1ce61da6f8f6f115154fb0e0e5b159fe11656839ba1e6061372711c013ee" + name = "github.com/rogpeppe/go-internal" + packages = [ + "modfile", + "module", + "semver", + ] + pruneopts = "NT" + revision = "1cf9852c553c5b7da2d5a4a091129a7822fed0c9" + version = "v1.2.2" + +[[projects]] + digest = "1:1f84287a4ca2c8f729d8155ba4c45915f5854ebbd214e406070779753da68422" + name = "github.com/sirupsen/logrus" + packages = ["."] + pruneopts = "NT" + revision = "e1e72e9de974bd926e5c56f83753fba2df402ce5" + version = "v1.3.0" + +[[projects]] + digest = "1:1bc08ec221c4fb25e6f2c019b23fe989fb44573c696983d8e403a3b76cc378e1" + name = "github.com/spf13/afero" + packages = [ + ".", + "mem", + ] + pruneopts = "NT" + revision = "f4711e4db9e9a1d3887343acb72b2bbfc2f686f5" + version = "v1.2.1" + [[projects]] digest = "1:9d8420bbf131d1618bde6530af37c3799340d3762cc47210c1d9532a4c3a2779" name = "github.com/spf13/pflag" @@ -296,6 +380,17 @@ revision = "298182f68c66c05229eb03ac171abe6e309ee79a" version = "v1.0.3" +[[projects]] + digest = "1:8e8fc2cb42cfa0a18fda0c9c4034092faa7aac773da5efdde8828eb4b96c001d" + name = "github.com/stretchr/testify" + packages = [ + "assert", + "require", + ] + pruneopts = "NT" + revision = "ffdc059bfe9ce6a4e144ba849dbedead332c6053" + version = "v1.3.0" + [[projects]] digest = "1:22f696cee54865fb8e9ff91df7b633f6b8f22037a8015253c6b6a71ca82219c7" name = "go.uber.org/atomic" @@ -501,6 +596,17 @@ pruneopts = "NT" revision = "2d6f90ab1293a1fb871cf149423ebb72aa7423aa" +[[projects]] + digest = "1:117a407949aaaad9f7fbe3da8d6c2055f2c223ac0cbebd39f47ff71899622a91" + name = "k8s.io/apiextensions-apiserver" + packages = [ + "pkg/apis/apiextensions", + "pkg/apis/apiextensions/v1beta1", + "pkg/client/clientset/clientset/scheme", + ] + pruneopts = "NT" + revision = "408db4a50408e2149acbd657bceb2480c13cb0a4" + [[projects]] digest = "1:b07bf863262aae765494d60f0d524483f211b29f9bb27d445a79c13af8676bf2" name = "k8s.io/apimachinery" @@ -556,6 +662,7 @@ name = "k8s.io/client-go" packages = [ "discovery", + "discovery/cached", "dynamic", "kubernetes", "kubernetes/scheme", @@ -738,7 +845,9 @@ "github.com/operator-framework/operator-sdk/pkg/k8sutil", "github.com/operator-framework/operator-sdk/pkg/leader", "github.com/operator-framework/operator-sdk/pkg/ready", + "github.com/operator-framework/operator-sdk/pkg/test", "github.com/operator-framework/operator-sdk/version", + "github.com/stretchr/testify/require", "k8s.io/api/core/v1", "k8s.io/apimachinery/pkg/api/errors", "k8s.io/apimachinery/pkg/apis/meta/v1", diff --git a/Gopkg.toml b/Gopkg.toml index cbc6a98..18679e3 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -52,3 +52,11 @@ required = [ [[prune.project]] name = "k8s.io/code-generator" non-go = false + +[[constraint]] + name = "github.com/stretchr/testify" + version = "1.3.0" + +[[constraint]] + branch = "master" + name = "github.com/wadey/gocovmerge" diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..88b6652 --- /dev/null +++ b/Makefile @@ -0,0 +1,272 @@ +PROJECT_NAME=devopsconsole-operator +PACKAGE_NAME:=github.com/redhat-developer/$(PROJECT_NAME) +CUR_DIR=$(shell pwd) +TMP_PATH=$(CUR_DIR)/tmp +INSTALL_PREFIX=$(CUR_DIR)/bin +VENDOR_DIR=vendor +INCLUDE_DIR=make +SOURCE_DIR ?= . +SOURCES := $(shell find $(SOURCE_DIR) -path $(SOURCE_DIR)/vendor -prune -o -name '*.go' -print) + +BINARY_SERVER_BIN=$(INSTALL_PREFIX)/devconsole-operator +GO_BIN_NAME=go +GIT_BIN_NAME=git +DEP_BIN_NAME=dep +DOCKER_BIN_NAME=docker +UNAME_S=$(shell uname -s) +GOCOV_BIN=$(VENDOR_DIR)/github.com/axw/gocov/gocov/gocov +GOCOVMERGE_BIN=$(VENDOR_DIR)/github.com/wadey/gocovmerge/gocovmerge +GOCYCLO_DIR=$(VENDOR_DIR)/github.com/fzipp/gocyclo +GOCYCLO_BIN=$(GOCYCLO_DIR)/gocyclo +GOLANGCI_LINT_BIN_NAME:=golangci-lint + +# declares variable that are OS-sensitive +include ./$(INCLUDE_DIR)/test.mk +include ./$(INCLUDE_DIR)/Makefile.dev + +DOCKER_BIN := $(shell command -v $(DOCKER_BIN_NAME) 2> /dev/null) +include ./$(INCLUDE_DIR)/docker.mk + +# This is a fix for a non-existing user in passwd file when running in a docker +# container and trying to clone repos of dependencies +GIT_COMMITTER_NAME ?= "user" +GIT_COMMITTER_EMAIL ?= "user@example.com" +export GIT_COMMITTER_NAME +export GIT_COMMITTER_EMAIL + +COMMIT=$(shell git rev-parse HEAD 2>/dev/null) +GITUNTRACKEDCHANGES := $(shell git status --porcelain --untracked-files=no) +ifneq ($(GITUNTRACKEDCHANGES),) + COMMIT := $(COMMIT)-dirty +endif +BUILD_TIME=`date -u '+%Y-%m-%dT%H:%M:%SZ'` + +.DEFAULT_GOAL := help + +# Call this function with $(call log-info,"Your message") +define log-info = + @echo "INFO: $(1)" +endef + +# ------------------------------------------------------------------- +# Docker build +# ------------------------------------------------------------------- +BUILD_DIR = bin +REGISTRY_URI = quay.io +REGISTRY_NS = ${PROJECT_NAME} +REGISTRY_IMAGE = ${PROJECT_NAME} + +ifeq ($(TARGET),rhel) + REGISTRY_URL := ${REGISTRY_URI}/openshiftio/rhel-${REGISTRY_NS}-${REGISTRY_IMAGE} + DOCKERFILE := Dockerfile.rhel +else + REGISTRY_URL := ${REGISTRY_URI}/openshiftio/${REGISTRY_NS}-${REGISTRY_IMAGE} + DOCKERFILE := Dockerfile +endif + +$(BUILD_DIR): + mkdir $(BUILD_DIR) + +.PHONY: build-linux $(BUILD_DIR) +build-linux: makefiles prebuild-check deps ## Builds the Linux binary for the container image into bin/ folder + CGO_ENABLED=0 GOARCH=amd64 GOOS=linux go build -v $(LDFLAGS) -o $(BUILD_DIR)/$(PROJECT_NAME) + +image: clean-artifacts build-linux + docker build -t $(REGISTRY_URL) \ + --build-arg BUILD_DIR=$(BUILD_DIR)\ + --build-arg PROJECT_NAME=$(PROJECT_NAME)\ + -f $(VENDOR_DIR)/github.com/fabric8-services/fabric8-common/makefile/$(DOCKERFILE) . + +# ------------------------------------------------------------------- +# help! +# ------------------------------------------------------------------- + +.PHONY: help +# Based on https://gist.github.com/rcmachado/af3db315e31383502660 +## Display this help text +help:/ + $(info Available targets) + $(info -----------------) + @awk '/^[a-zA-Z\-\_0-9]+:/ { \ + helpMessage = match(lastLine, /^## (.*)/); \ + helpCommand = substr($$1, 0, index($$1, ":")-1); \ + if (helpMessage) { \ + helpMessage = substr(lastLine, RSTART + 3, RLENGTH); \ + gsub(/##/, "\n ", helpMessage); \ + printf "%-35s - %s\n", helpCommand, helpMessage; \ + lastLine = "" \ + } \ + } \ + { hasComment = match(lastLine, /^## (.*)/); \ + if(hasComment) { \ + lastLine=lastLine$$0; \ + } \ + else { \ + lastLine = $$0 \ + } \ + }' $(MAKEFILE_LIST) +# ------------------------------------------------------------------- +# required tools +# ------------------------------------------------------------------- + +# Find all required tools: +GIT_BIN := $(shell command -v $(GIT_BIN_NAME) 2> /dev/null) +TMP_BIN_DIR := $(TMP_PATH)/bin +DEP_BIN := $(TMP_BIN_DIR)/$(DEP_BIN_NAME) +DEP_VERSION=v0.5.0 +GOLANGCI_LINT_VERSION=1.12 +GOLANGCI_LINT_BIN=$(TMP_BIN_DIR)/$(GOLANGCI_LINT_BIN_NAME) +GO_BIN := $(shell command -v $(GO_BIN_NAME) 2> /dev/null) + +$(INSTALL_PREFIX): + mkdir -p $(INSTALL_PREFIX) +$(TMP_PATH): + mkdir -p $(TMP_PATH) + +.PHONY: prebuild-check +prebuild-check: $(TMP_PATH) $(INSTALL_PREFIX) +# Check that all tools where found +ifndef GIT_BIN + $(error The "$(GIT_BIN_NAME)" executable could not be found in your PATH) +endif +ifndef DEP_BIN + $(error The "$(DEP_BIN_NAME)" executable could not be found in your PATH) +endif +ifndef GO_BIN + $(error The "$(GO_BIN_NAME)" executable could not be found in your PATH) +endif + +# ------------------------------------------------------------------- +# deps +# ------------------------------------------------------------------- + +.PHONY: deps +## Download build dependencies +deps: $(DEP_BIN) $(VENDOR_DIR) + +# install dep in a the tmp/bin dir of the repo +$(DEP_BIN): + @echo "Installing 'dep' $(DEP_VERSION) at '$(TMP_BIN_DIR)'..." + mkdir -p $(TMP_BIN_DIR) +ifeq ($(UNAME_S),Darwin) + @curl -L -s https://github.com/golang/dep/releases/download/$(DEP_VERSION)/dep-darwin-amd64 -o $(DEP_BIN) + @cd $(TMP_BIN_DIR) && \ + curl -L -s https://github.com/golang/dep/releases/download/$(DEP_VERSION)/dep-darwin-amd64.sha256 -o $(TMP_BIN_DIR)/dep-darwin-amd64.sha256 && \ + echo "1a7bdb0d6c31ecba8b3fd213a1170adf707657123e89dff234871af9e0498be2 dep" > dep-darwin-amd64.sha256 && \ + shasum -a 256 --check dep-darwin-amd64.sha256 +else + @curl -L -s https://github.com/golang/dep/releases/download/$(DEP_VERSION)/dep-linux-amd64 -o $(DEP_BIN) + @cd $(TMP_BIN_DIR) && \ + echo "287b08291e14f1fae8ba44374b26a2b12eb941af3497ed0ca649253e21ba2f83 dep" > dep-linux-amd64.sha256 && \ + sha256sum -c dep-linux-amd64.sha256 +endif + @chmod +x $(DEP_BIN) + +$(VENDOR_DIR): Gopkg.toml + @echo "checking dependencies with $(DEP_BIN_NAME)" + @$(DEP_BIN) ensure -v + +# ------------------------------------------------------------------- +# Code format/check +# ------------------------------------------------------------------- +GOFORMAT_FILES := $(shell find . -name '*.go' | grep -vEf $(INCLUDE_DIR)/gofmt_exclude) + +.PHONY: check-go-format +## Exits with an error if there are files that do not match formatting defined by gofmt +check-go-format: prebuild-check deps + @gofmt -s -l ${GOFORMAT_FILES} 2>&1 \ + | tee /tmp/gofmt-errors \ + | read \ + && echo "ERROR: These files differ from gofmt's style (run 'make format-go-code' to fix this):" \ + && cat /tmp/gofmt-errors \ + && exit 1 \ + || true + +.PHONY: analyze-go-code +## Run golangci analysis over the code +analyze-go-code: deps + $(info >>--- RESULTS: GOLANGCI CODE ANALYSIS ---<<) + @go get -u github.com/golangci/golangci-lint/cmd/golangci-lint + @golangci-lint run + +.PHONY: format-go-code +## Formats any go file that does not match formatting defined by gofmt +format-go-code: prebuild-check + @gofmt -s -l -w ${GOFORMAT_FILES} + +# ------------------------------------------------------------------- +# Code format/check with golangci-lint +# ------------------------------------------------------------------- +.PHONY: check-go-code +## Checks the code with golangci-lint +check-go-code: $(GOLANGCI_LINT_BIN) + @echo "checking code..." + $(GOLANGCI_LINT_BIN) run + +# install golangci-lint in the 'tmp/bin' dir of the repo +$(GOLANGCI_LINT_BIN): + @echo "Installing 'golang-ci-lint' $(GOLANGCI_LINT_VERSION) at '$(TMP_BIN_DIR)' ..." + mkdir -p $(TMP_BIN_DIR) +ifeq ($(UNAME_S),Darwin) + @curl -L -s https://github.com/golangci/golangci-lint/releases/download/v$(GOLANGCI_LINT_VERSION)/golangci-lint-$(GOLANGCI_LINT_VERSION)-darwin-amd64.tar.gz -o $(GOLANGCI_LINT_BIN)-$(GOLANGCI_LINT_VERSION)-darwin-amd64.tar.gz && \ + cd $(TMP_BIN_DIR) && curl -L -s https://github.com/golangci/golangci-lint/releases/download/v$(GOLANGCI_LINT_VERSION)/golangci-lint-$(GOLANGCI_LINT_VERSION)-checksums.txt -o golangci-lint-$(GOLANGCI_LINT_VERSION)-checksums.txt && \ + grep "darwin-amd64" golangci-lint-$(GOLANGCI_LINT_VERSION)-checksums.txt > golangci-lint-$(GOLANGCI_LINT_VERSION)-checksum-darwin-amd64.txt && \ + shasum -a 256 --check golangci-lint-$(GOLANGCI_LINT_VERSION)-checksum-darwin-amd64.txt && \ + tar xvf $(GOLANGCI_LINT_BIN)-$(GOLANGCI_LINT_VERSION)-darwin-amd64.tar.gz -C $(TMP_BIN_DIR) && \ + mv $(TMP_BIN_DIR)/golangci-lint-$(GOLANGCI_LINT_VERSION)-darwin-amd64/golangci-lint $(GOLANGCI_LINT_BIN) +else + @curl -L -s https://github.com/golangci/golangci-lint/releases/download/v$(GOLANGCI_LINT_VERSION)/golangci-lint-$(GOLANGCI_LINT_VERSION)-linux-amd64.tar.gz -o $(GOLANGCI_LINT_BIN)-$(GOLANGCI_LINT_VERSION)-linux-amd64.tar.gz && \ + cd $(TMP_BIN_DIR) && curl -L -s https://github.com/golangci/golangci-lint/releases/download/v$(GOLANGCI_LINT_VERSION)/golangci-lint-$(GOLANGCI_LINT_VERSION)-checksums.txt -o golangci-lint-$(GOLANGCI_LINT_VERSION)-checksums.txt && \ + grep "linux-amd64" golangci-lint-$(GOLANGCI_LINT_VERSION)-checksums.txt > golangci-lint-$(GOLANGCI_LINT_VERSION)-checksum-linux-amd64.txt && \ + sha256sum -c golangci-lint-$(GOLANGCI_LINT_VERSION)-checksum-linux-amd64.txt && \ + tar xvf $(GOLANGCI_LINT_BIN)-$(GOLANGCI_LINT_VERSION)-linux-amd64.tar.gz -C $(TMP_BIN_DIR) && \ + mv $(TMP_BIN_DIR)/golangci-lint-$(GOLANGCI_LINT_VERSION)-linux-amd64/golangci-lint $(GOLANGCI_LINT_BIN) +endif + @chmod +x $(GOLANGCI_LINT_BIN) + + +# ------------------------------------------------------------------- +# clean +# ------------------------------------------------------------------- + +# For the global "clean" target all targets in this variable will be executed +CLEAN_TARGETS = + +CLEAN_TARGETS += clean-artifacts +.PHONY: clean-artifacts +# Removes the ./bin directory. +clean-artifacts: + -rm -rf $(INSTALL_PREFIX) + +CLEAN_TARGETS += clean-object-files +.PHONY: clean-object-files +# Runs go clean to remove any executables or other object files. +clean-object-files: + go clean ./... + +CLEAN_TARGETS += clean-vendor +.PHONY: clean-vendor +# Removes the ./vendor directory. +clean-vendor: + -rm -rf $(VENDOR_DIR) + +CLEAN_TARGETS += clean-tmp +.PHONY: clean-tmp +# Removes the ./vendor directory. +clean-tmp: + -rm -rf $(TMP_PATH) + +# Keep this "clean" target here after all `clean-*` sub tasks +.PHONY: clean +## Cleans the project, removes all generated code/bins and vendor packages +clean: $(CLEAN_TARGETS) + +# ------------------------------------------------------------------- +# build the binary executable (to ship in prod) +# ------------------------------------------------------------------- + +.PHONY: build +## Build the operator +build: prebuild-check deps + @echo "building $(BINARY_SERVER_BIN)..." + go build -v -o $(BINARY_SERVER_BIN) cmd/manager/main.go diff --git a/cmd/manager/main.go b/cmd/manager/main.go index 0ddb594..f43d103 100644 --- a/cmd/manager/main.go +++ b/cmd/manager/main.go @@ -53,7 +53,11 @@ func main() { } // Become the leader before proceeding - leader.Become(context.TODO(), "devopsconsole-operator-lock") + err = leader.Become(context.TODO(), "devopsconsole-operator-lock") + if err != nil { + log.Error(err, "") + os.Exit(1) + } r := ready.NewFileReady() err = r.Set() @@ -61,7 +65,13 @@ func main() { log.Error(err, "") os.Exit(1) } - defer r.Unset() + defer func() { + err := r.Unset() + if err != nil { + log.Error(err, "") + os.Exit(1) + } + }() // Create a new Cmd to provide shared dependencies and start components mgr, err := manager.New(cfg, manager.Options{Namespace: namespace}) diff --git a/deploy/test/global-manifests.yaml b/deploy/test/global-manifests.yaml new file mode 100644 index 0000000..27e4e03 --- /dev/null +++ b/deploy/test/global-manifests.yaml @@ -0,0 +1,38 @@ +apiVersion: apiextensions.k8s.io/v1beta1 +kind: CustomResourceDefinition +metadata: + name: envdeployments.devconsole.openshift.io +spec: + group: devconsole.openshift.io + names: + kind: DevConsole + listKind: DevConsoleList + plural: envdeployments + singular: envdeployment + scope: Namespaced + version: v1alpha1 +--- +kind: ClusterRole +apiVersion: rbac.authorization.k8s.io/v1 +metadata: + name: devconsole +rules: +- apiGroups: + - codeready.io + resources: + - devconsole/finalizers + verbs: + - update +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRoleBinding +metadata: + name: devconsole +roleRef: + apiGroup: rbac.authorization.k8s.io + kind: ClusterRole + name: devconsole +subjects: +- kind: ServiceAccount + name: devconsole + namespace: devconsole-e2e-test \ No newline at end of file diff --git a/deploy/test/namespace-manifests.yaml b/deploy/test/namespace-manifests.yaml new file mode 100644 index 0000000..221e13d --- /dev/null +++ b/deploy/test/namespace-manifests.yaml @@ -0,0 +1,99 @@ +apiVersion: v1 +kind: ServiceAccount +metadata: + name: devconsole + +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: Role +metadata: + creationTimestamp: null + name: devconsole +rules: +- apiGroups: + - "" + resources: + - pods + - services + - endpoints + - persistentvolumeclaims + - events + - configmaps + - secrets + verbs: + - '*' +- apiGroups: + - apps + resources: + - deployments + - daemonsets + - replicasets + - statefulsets + verbs: + - '*' +- apiGroups: + - monitoring.coreos.com + resources: + - servicemonitors + verbs: + - get + - create +- apiGroups: + - devconsole.openshift.io + resources: + - '*' + - envdeployments + verbs: + - '*' +- apiGroups: + - clusterregistry.k8s.io + resources: + - '*' + verbs: + - '*' + +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + name: devconsole +spec: + replicas: 1 + selector: + matchLabels: + name: devconsole + template: + metadata: + labels: + name: devconsole + spec: + serviceAccountName: devconsole + containers: + - name: devconsole + # Replace this with the built image name + image: REPLACE_IMAGE + ports: + - containerPort: 60000 + name: metrics + command: + - devconsole + imagePullPolicy: Always + readinessProbe: + exec: + command: + - stat + - /tmp/operator-sdk-ready + initialDelaySeconds: 4 + periodSeconds: 10 + failureThreshold: 1 + env: + - name: WATCH_NAMESPACE + valueFrom: + fieldRef: + fieldPath: metadata.namespace + - name: POD_NAME + valueFrom: + fieldRef: + fieldPath: metadata.name + - name: OPERATOR_NAME + value: "devconsole" diff --git a/make/Makefile.dev b/make/Makefile.dev new file mode 100644 index 0000000..51e9731 --- /dev/null +++ b/make/Makefile.dev @@ -0,0 +1,87 @@ +DOCKER_REPO?=quay.io/openshiftio +IMAGE_NAME?=devconsole-operator +SHORT_COMMIT=$(shell git rev-parse --short HEAD) +ifneq ($(GITUNTRACKEDCHANGES),) +SHORT_COMMIT := $(SHORT_COMMIT)-dirty +endif + +TIMESTAMP:=$(shell date +%s) +TAG?=$(SHORT_COMMIT)-$(TIMESTAMP) + +DEPLOY_DIR:=deploy + +.PHONY: create-resources +create-resources: + @echo "Logging using system:admin..." + @oc login -u system:admin + @echo "Creating sub resources..." + @echo "Creating CRDs..." + @oc create -f $(DEPLOY_DIR)/crds/devconsole_v1alpha1_envdeployment_crd.yaml + @echo "Creating Namespace" + @oc create -f $(DEPLOY_DIR)/namespace.yaml + @echo "oc project codeready-devconsole" + @oc project codeready-devconsole + @echo "Creating Service Account" + @oc create -f $(DEPLOY_DIR)/service_account.yaml + @echo "Creating Role" + @oc create -f $(DEPLOY_DIR)/role.yaml + @echo "Creating RoleBinding" + @oc create -f $(DEPLOY_DIR)/role_binding.yaml + @echo "Creating Cluster Role" + @oc create -f $(DEPLOY_DIR)/cluster_role.yaml + @echo "Creating ClusterRoleBinding" + @oc create -f $(DEPLOY_DIR)/cluster_role_binding.yaml + +.PHONY: create-cr +create-cr: + @echo "Creating Custom Resource..." + @oc create -f $(DEPLOY_DIR)/crds/devconsole_v1alpha1_envdeployment_crd.yaml + +.PHONY: build-image +build-image: + docker build -t $(DOCKER_REPO)/$(IMAGE_NAME):$(TAG) -f Dockerfile.dev . + docker tag $(DOCKER_REPO)/$(IMAGE_NAME):$(TAG) $(DOCKER_REPO)/$(IMAGE_NAME):test + +.PHONY: deploy-operator-only +deploy-operator-only: + @echo "Creating Deployment for Operator" + @cat minishift/operator.yaml | sed s/\:dev/:$(TAG)/ | oc create -f - + +.PHONY: clean-all +clean-all: clean-operator clean-resources + +.PHONY: clean-operator +clean-operator: + @echo "Deleting Deployment for Operator" + @cat minishift/operator.yaml | sed s/\:dev/:$(TAG)/ | oc delete -f - || true + +.PHONY: clean-resources +clean-resources: + @echo "Deleting sub resources..." + @echo "Deleting RoleBinding" + @oc delete -f $(DEPLOY_DIR)/role_binding.yaml || true + @echo "Deleting ClusterRoleBinding" + @oc delete -f $(DEPLOY_DIR)/cluster_role_binding.yaml || true + @echo "Deleting Role" + @oc delete -f $(DEPLOY_DIR)/role.yaml || true + @echo "Deleting ClusterRole" + @oc delete -f $(DEPLOY_DIR)/cluster_role.yaml || true + @echo "Deleting Service Account" + @oc delete -f $(DEPLOY_DIR)/service_account.yaml || true + @echo "Deleting Custom Resources..." + @oc delete -f $(DEPLOY_DIR)/crds/devconsole_v1alpha1_envdeployment_crd.yaml|| true + @echo "Deleting Namespace" + @oc delete -f $(DEPLOY_DIR)/namespace.yaml || true + @echo "Deleting Custom Resource Definitions..." + @oc delete -f $(DEPLOY_DIR)/crds/devconsole_v1alpha1_envdeployment_crd.yaml || true + +.PHONY: deploy-operator +deploy-operator: build build-image deploy-operator-only + +.PHONY: minishift-start +minishift-start: + minishift start --cpus 4 --memory 8GB + -eval `minishift docker-env` && oc login -u system:admin + +.PHONY: deploy-all +deploy-all: clean-resources create-resources deps prebuild-check deploy-operator create-cr diff --git a/make/docker.mk b/make/docker.mk new file mode 100644 index 0000000..84e5322 --- /dev/null +++ b/make/docker.mk @@ -0,0 +1,108 @@ +DOCKER_IMAGE_CORE := $(PROJECT_NAME) +DOCKER_IMAGE_DEPLOY := $(PROJECT_NAME)-deploy + +ifeq ($(TARGET),rhel) + DOCKERFILE_DEPLOY := Dockerfile.deploy.rhel +else + DOCKERFILE_DEPLOY := Dockerfile.deploy +endif + +# If running in Jenkins we don't allow for interactively running the container +ifneq ($(BUILD_TAG),) + DOCKER_RUN_INTERACTIVE_SWITCH := +else + DOCKER_RUN_INTERACTIVE_SWITCH := -i +endif + +# The workspace environment is set by Jenkins and defaults to /tmp if not set +WORKSPACE ?= /tmp +DOCKER_BUILD_DIR := $(WORKSPACE)/$(PROJECT_NAME)-build + +# The BUILD_TAG environment variable will be set by jenkins +# to reflect jenkins-${JOB_NAME}-${BUILD_NUMBER} +BUILD_TAG ?= $(PROJECT_NAME)-local-build +DOCKER_CONTAINER_NAME := $(BUILD_TAG) + +# Where is the GOPATH inside the build container? +GOPATH_IN_CONTAINER=/tmp/go +PACKAGE_PATH=$(GOPATH_IN_CONTAINER)/src/$(PACKAGE_NAME) + +.PHONY: docker-image-builder +## Builds the docker image used to build the software +docker-image-builder: + @echo "Building docker image $(DOCKER_IMAGE_CORE)" + docker build --build-arg USE_GO_VERSION_FROM_WEBSITE=$(USE_GO_VERSION_FROM_WEBSITE) -t $(DOCKER_IMAGE_CORE) -f $(CUR_DIR)/Dockerfile.builder $(CUR_DIR) + +.PHONY: docker-image-deploy +## Creates a runnable image using the artifacts from the bin directory +docker-image-deploy: + docker build -t $(DOCKER_IMAGE_DEPLOY) -f $(CUR_DIR)/$(DOCKERFILE_DEPLOY) $(CUR_DIR) + +.PHONY: docker-build-dir +# Creates the docker build directory. +docker-build-dir: + @echo "Creating build directory $(BUILD_DIR)" + mkdir -p $(DOCKER_BUILD_DIR) + +CLEAN_TARGETS += clean-docker-build-container +.PHONY: clean-docker-build-container +# Removes any existing container used to build the software (if any). +clean-docker-build-container: + @echo "Removing container named \"$(DOCKER_CONTAINER_NAME)\" (if any)" +ifneq ($(strip $(shell docker ps -qa --filter "name=$(DOCKER_CONTAINER_NAME)" 2>/dev/null)),) + @docker rm -f $(DOCKER_CONTAINER_NAME) +else + @echo "No container named \"$(DOCKER_CONTAINER_NAME)\" to remove" +endif + +CLEAN_TARGETS += clean-docker-build-dir +.PHONY: clean-docker-build-dir +# Removes the docker build directory. +clean-docker-build-dir: + @echo "Cleaning build directory $(BUILD_DIR)" + -rm -rf $(DOCKER_BUILD_DIR) + +.PHONY: docker-start +## Starts the docker build container in the background (detached mode). +## After calling this command you can invoke all the make targets from the +## normal Makefile (e.g. deps, build) inside the build container +## by prefixing them with "docker-". For example to execute "make deps" +## inside the build container, just run "make docker-deps". +## To remove the container when no longer needed, call "make docker-rm". +docker-start: docker-build-dir docker-image-builder +ifneq ($(strip $(shell docker ps -qa --filter "name=$(DOCKER_CONTAINER_NAME)" 2>/dev/null)),) + @echo "Docker container \"$(DOCKER_CONTAINER_NAME)\" already exists. To recreate, run \"make docker-rm\"." +else + docker run \ + --detach=true \ + -t \ + $(DOCKER_RUN_INTERACTIVE_SWITCH) \ + --name="$(DOCKER_CONTAINER_NAME)" \ + -e ADMIN_POSTGRES_PORT=5432 \ + -v $(CUR_DIR):$(PACKAGE_PATH):Z \ + -u $(shell id -u $(USER)):$(shell id -g $(USER)) \ + -e GOPATH=$(GOPATH_IN_CONTAINER) \ + -w $(PACKAGE_PATH) \ + $(DOCKER_IMAGE_CORE) + @echo "Docker container \"$(DOCKER_CONTAINER_NAME)\" created. Continue with \"make docker-deps\"." +endif + +.PHONY: docker-rm +# Removes the docker build container, if any (see "make docker-start"). +docker-rm: +ifneq ($(strip $(shell docker ps -qa --filter "name=$(DOCKER_CONTAINER_NAME)" 2>/dev/null)),) + docker rm -f "$(DOCKER_CONTAINER_NAME)" +else + @echo "No container named \"$(DOCKER_CONTAINER_NAME)\" to remove." +endif + +# This is a wildcard target to let you call any make target from the normal makefile +# but it will run inside the docker container. This target will only get executed if +# there's no specialized form available. For example if you call "make docker-start" +# not this target gets executed but the "docker-start" target. +docker-%: + $(eval makecommand:=$(subst docker-,,$@)) +ifeq ($(strip $(shell docker ps -qa --filter "name=$(DOCKER_CONTAINER_NAME)" 2>/dev/null)),) + $(error No container name "$(DOCKER_CONTAINER_NAME)" exists to run the command "make $(makecommand)") +endif + docker exec -t $(DOCKER_RUN_INTERACTIVE_SWITCH) "$(DOCKER_CONTAINER_NAME)" bash -ec 'make $(makecommand)' diff --git a/make/gofmt_exclude b/make/gofmt_exclude new file mode 100644 index 0000000..6d03c3e --- /dev/null +++ b/make/gofmt_exclude @@ -0,0 +1 @@ +vendor/.* diff --git a/make/test.mk b/make/test.mk new file mode 100644 index 0000000..bd1fc5c --- /dev/null +++ b/make/test.mk @@ -0,0 +1,347 @@ +#=============================================================================== +# Testing has become a rather big and interconnected topic and that's why it +# has arrived in it's own file. +# +# We have to types of tests available: +# +# 1. unit tests +# +# The unit tests can be executed fairly simply be running `go test` +# +# Usage +# ----- +# If you want to run the unit tests, type +# +# $ make test-unit +# +# To run all tests, type +# +# $ make test-all +# +# To output unit-test coverage profile information for each function, type +# +# $ make coverage-unit +# +# To generate unit-test HTML representation of coverage profile (opens a browser), type +# +# $ make coverage-unit-html +# +# To output all coverage profile information for each function, type +# +# $ make coverage-all +# +# Artifacts and coverage modes +# ---------------------------- +# Each package generates coverage outputs under tmp/coverage/$(PACKAGE) where +# $(PACKAGE) resolves to the Go package. Here's an example of a coverage file +# for the package "github.com/fabric8-services/fabric8-auth/models" with coverage mode +# "set" generated by the unit tests: +# +# tmp/coverage/github.com/fabric8-services/fabric8-auth/models/coverage.unit.mode-set +# +# For unit-tests all results are combined into this file: +# +# tmp/coverage.unit.mode-$(COVERAGE_MODE) +# +# +# The overall coverage gets combined into this file: +# +# tmp/coverage.mode-$(COVERAGE_MODE) +# +# The $(COVERAGE_MODE) in each filename indicates what coverage mode was used. +# +# These are possible coverage modes (see https://blog.golang.org/cover): +# +# set: did each statement run? (default) +# count: how many times did each statement run? +# atomic: like count, but counts precisely in parallel programs +# +# To choose another coverage mode, simply prefix the invovation of `make`: +# +# $ COVERAGE_MODE=count make test-unit +#=============================================================================== + +# mode can be: set, count, or atomic +COVERAGE_MODE ?= set + +# By default no go test calls will use the -v switch when running tests. +# But if you want you can enable that by setting GO_TEST_VERBOSITY_FLAG=-v +GO_TEST_VERBOSITY_FLAG ?= + + +# By default reduce the amount of log output from tests +ADMIN_LOG_LEVEL ?= error + +# Output directory for coverage information +COV_DIR = $(TMP_PATH)/coverage + +# Files that combine package coverages for unit tests separately +COV_PATH_UNIT = $(TMP_PATH)/coverage.unit.mode-$(COVERAGE_MODE) + +# File that stores overall coverge for all packages and unit-tests +COV_PATH_OVERALL = $(TMP_PATH)/coverage.mode-$(COVERAGE_MODE) + +# This pattern excludes some folders from the coverage calculation (see grep -v) +ALL_PKGS_EXCLUDE_PATTERN = 'vendor\|app\|tool\/cli\|design\|client\|test' + +# This pattern excludes some folders from the go code analysis +GOANALYSIS_PKGS_EXCLUDE_PATTERN="vendor|app|client|tool/cli" +GOANALYSIS_DIRS=$(shell go list -f {{.Dir}} ./... | grep -v -E $(GOANALYSIS_PKGS_EXCLUDE_PATTERN)) + +#------------------------------------------------------------------------------- +# Normal test targets +# +# These test targets are the ones that will be invoked from the outside. If +# they are called and the artifacts already exist, then the artifacts will +# first be cleaned and recreated. This ensures that the tests are always +# executed. +#------------------------------------------------------------------------------- + +.PHONY: test-all +## Runs the unit tests and e2e tests +test-all: prebuild-check test-unit test-e2e + +# Runs the unit tests and produces coverage files for each package. +.PHONY: test-unit-with-coverage +test-unit-with-coverage: prebuild-check clean-coverage-unit $(COV_PATH_UNIT) + +.PHONY: test-unit +## Runs the unit tests without coverage +test-unit: prebuild-check $(SOURCES) + $(call log-info,"Running test: $@") + $(eval TEST_PACKAGES:=$(shell go list ./... | grep -v $(ALL_PKGS_EXCLUDE_PATTERN))) + ADMIN_LOG_LEVEL=$(ADMIN_LOG_LEVEL) go test -vet off $(GO_TEST_VERBOSITY_FLAG) $(TEST_PACKAGES) + +.PHONY: test-e2e +## Runs the e2e tests without coverage +test-e2e: build build-image e2e-setup + $(call log-info,"Running E2E test: $@") + go test ./test/e2e/... -root=$(PWD) -kubeconfig=$(HOME)/.kube/config -globalMan deploy/test/global-manifests.yaml -namespacedMan deploy/test/namespace-manifests.yaml -v -parallel=1 -singleNamespace + +.PHONY: e2e-setup +e2e-setup: e2e-cleanup + oc new-project devconsole-e2e-test || true + +.PHONY: e2e-cleanup +e2e-cleanup: + oc login -u system:admin + oc delete project devconsole-e2e-test || true + +#------------------------------------------------------------------------------- +# Inspect coverage of unit tests, integration tests in either pure +# console mode or in a browser (*-html). +# +# If the test coverage files to be evaluated already exist, then no new +# tests are executed. If they don't exist, we first run the tests. +#------------------------------------------------------------------------------- + +# Prints the total coverage of a given package. +# The total coverage is printed as the last argument in the +# output of "go tool cover". If the requested test name (first argument) +# Is *, then unit, integration tests will be combined +define print-package-coverage +$(eval TEST_NAME:=$(1)) +$(eval PACKAGE_NAME:=$(2)) +$(eval COV_FILE:="$(COV_DIR)/$(PACKAGE_NAME)/coverage.$(TEST_NAME).mode-$(COVERAGE_MODE)") + @if [ "$(TEST_NAME)" == "*" ]; then \ + UNIT_FILE=`echo $(COV_FILE) | sed 's/*/unit/'`; \ + INTEGRATON_FILE=`echo $(COV_FILE) | sed 's/*/integration/'`; \ + COV_FILE=`echo $(COV_FILE) | sed 's/*/combined/'`; \ + if [ ! -e $${UNIT_FILE} ]; then \ + COV_FILE=$${INTEGRATION_FILE}; \ + else \ + if [ ! -e $${INTEGRATION_FILE} ]; then \ + COV_FILE=$${UNIT_FILE}; \ + else \ + $(GOCOVMERGE_BIN) $${UNIT_FILE} $${INTEGRATION_FILE} > $${COV_FILE}; \ + fi; \ + fi; \ +else \ + COV_FILE=$(COV_FILE); \ +fi; \ +if [ -e "$${COV_FILE}" ]; then \ + VAL=`go tool cover -func=$${COV_FILE} \ + | grep '^total:' \ + | grep '\S\+$$' -o \ + | sed 's/%//'`; \ + printf "%-80s %#5.2f%%\n" "$(PACKAGE_NAME)" "$${VAL}"; \ +else \ + printf "%-80s %6s\n" "$(PACKAGE_NAME)" "n/a"; \ +fi +endef + +# Iterates over every package and prints its test coverage +# for a given test name ("unit"). +define package-coverage +$(eval TEST_NAME:=$(1)) +@printf "\n\nPackage coverage:\n" +$(eval TEST_PACKAGES:=$(shell go list ./... | grep -v $(ALL_PKGS_EXCLUDE_PATTERN))) +$(foreach package, $(TEST_PACKAGES), $(call print-package-coverage,$(TEST_NAME),$(package))) +endef + +$(COV_PATH_OVERALL): $(GOCOVMERGE_BIN) + @$(GOCOVMERGE_BIN) $(COV_PATH_UNIT) > $(COV_PATH_OVERALL) + +# Console coverage output: + +# First parameter: file to do in-place replacement with. +define cleanup-coverage-file +@sed -i '/.*\/sqlbindata\.go.*/d' $(1) +@sed -i '/.*\/confbindata\.go.*/d' $(1) +endef + +# Output coverage profile information for each function (only based on unit-tests). +# Re-runs unit-tests if coverage information is outdated. +.PHONY: coverage-unit +## Runs the unit tests with producing coverage files for each package +coverage-unit: prebuild-check $(COV_PATH_UNIT) + $(call cleanup-coverage-file,$(COV_PATH_UNIT)) + @go tool cover -func=$(COV_PATH_UNIT) + $(call package-coverage,unit) + + +.PHONY: coverage-all +# Output coverage profile information for each function. +# Re-runs unit- and integration-tests if coverage information is outdated. +coverage-all: prebuild-check clean-coverage-overall $(COV_PATH_OVERALL) + $(call cleanup-coverage-file,$(COV_PATH_OVERALL)) + @go tool cover -func=$(COV_PATH_OVERALL) + $(call package-coverage,*) + +# HTML coverage output: + +# Generate HTML representation (and show in browser) of coverage profile (based on unit tests). +# Re-runs unit tests if coverage information is outdated. +.PHONY: coverage-unit-html +## Runs the unit tests with producing coverage html for each package +coverage-unit-html: prebuild-check $(COV_PATH_UNIT) + $(call cleanup-coverage-file,$(COV_PATH_UNIT)) + @go tool cover -html=$(COV_PATH_UNIT) + +.PHONY: coverage-all-html +# Output coverage profile information for each function. +# Re-runs unit if coverage information is outdated. +coverage-all-html: prebuild-check clean-coverage-overall $(COV_PATH_OVERALL) + $(call cleanup-coverage-file,$(COV_PATH_OVERALL)) + @go tool cover -html=$(COV_PATH_OVERALL) + +# Experimental: + +.PHONY: gocov-unit-annotate +# (EXPERIMENTAL) Show actual code and how it is covered with unit tests. +# This target only runs the tests if the coverage file does exist. +gocov-unit-annotate: prebuild-check $(GOCOV_BIN) $(COV_PATH_UNIT) + $(call cleanup-coverage-file,$(COV_PATH_UNIT)) + @$(GOCOV_BIN) convert $(COV_PATH_UNIT) | $(GOCOV_BIN) annotate - + +.PHONY: .gocov-unit-report +.gocov-unit-report: prebuild-check $(GOCOV_BIN) $(COV_PATH_UNIT) + $(call cleanup-coverage-file,$(COV_PATH_UNIT)) + @$(GOCOV_BIN) convert $(COV_PATH_UNIT) | $(GOCOV_BIN) report + + +#------------------------------------------------------------------------------- +# Test artifacts are coverage files for unit and integration tests. +#------------------------------------------------------------------------------- + +# The test-package function executes tests for a package and saves the collected +# coverage output to a directory. After storing the coverage information it is +# also appended to a file of choice (without the "mode"-line) +# +# Parameters: +# 1. Test name (e.g. "unit" or "integration") +# 2. package name "github.com/fabric8-services/fabric8-auth/model" +# 3. File in which to combine the output +# 4. Path to file in which to store names of packages that failed testing +# 5. Environment variable (in the form VAR=VALUE) to be specified for running +# the test. For multiple environment variables, pass "VAR1=VAL1 VAR2=VAL2". +define test-package +$(eval TEST_NAME := $(1)) +$(eval PACKAGE_NAME := $(2)) +$(eval COMBINED_OUT_FILE := $(3)) +$(eval ERRORS_FILE := $(4)) +$(eval ENV_VAR := $(5)) +$(eval ALL_PKGS_COMMA_SEPARATED := $(6)) +@mkdir -p $(COV_DIR)/$(PACKAGE_NAME); +$(eval COV_OUT_FILE := $(COV_DIR)/$(PACKAGE_NAME)/coverage.$(TEST_NAME).mode-$(COVERAGE_MODE)) +@$(ENV_VAR) ADMIN_LOG_LEVEL=$(ADMIN_LOG_LEVEL) \ + go test -vet off $(PACKAGE_NAME) \ + $(GO_TEST_VERBOSITY_FLAG) \ + -coverprofile $(COV_OUT_FILE) \ + -coverpkg $(ALL_PKGS_COMMA_SEPARATED) \ + -covermode=$(COVERAGE_MODE) \ + -timeout 10m \ + $(EXTRA_TEST_PARAMS) \ + || echo $(PACKAGE_NAME) >> $(ERRORS_FILE) + +@if [ -e "$(COV_OUT_FILE)" ]; then \ + if [ ! -e "$(COMBINED_OUT_FILE)" ]; then \ + cp $(COV_OUT_FILE) $(COMBINED_OUT_FILE); \ + else \ + cp $(COMBINED_OUT_FILE) $(COMBINED_OUT_FILE).tmp; \ + $(GOCOVMERGE_BIN) $(COV_OUT_FILE) $(COMBINED_OUT_FILE).tmp > $(COMBINED_OUT_FILE); \ + fi \ +fi +endef + +# Exits the makefile with an error if the file (first parameter) exists. +# Before exiting, the contents of the passed file is printed. +define check-test-results +$(eval ERRORS_FILE := $(1)) +@if [ -e "$(ERRORS_FILE)" ]; then \ +echo ""; \ +echo "ERROR: The following packages did not pass the tests:"; \ +echo "-----------------------------------------------------"; \ +cat $(ERRORS_FILE); \ +echo "-----------------------------------------------------"; \ +echo ""; \ +exit 1; \ +fi +endef + +# NOTE: We don't have prebuild-check as a dependency here because it would cause +# the recipe to be always executed. +$(COV_PATH_UNIT): $(SOURCES) $(GOCOVMERGE_BIN) + $(eval TEST_NAME := unit) + $(eval ERRORS_FILE := $(TMP_PATH)/errors.$(TEST_NAME)) + $(call log-info,"Running test: $(TEST_NAME)") + @mkdir -p $(COV_DIR) + @echo "mode: $(COVERAGE_MODE)" > $(COV_PATH_UNIT) + @-rm -f $(ERRORS_FILE) + $(eval TEST_PACKAGES:=$(shell go list ./... | grep -v $(ALL_PKGS_EXCLUDE_PATTERN))) + $(eval ALL_PKGS_COMMA_SEPARATED:=$(shell echo $(TEST_PACKAGES) | tr ' ' ,)) + $(foreach package, $(TEST_PACKAGES), $(call test-package,$(TEST_NAME),$(package),$(COV_PATH_UNIT),$(ERRORS_FILE),,$(ALL_PKGS_COMMA_SEPARATED))) + $(call check-test-results,$(ERRORS_FILE)) + +#------------------------------------------------------------------------------- +# Additional tools to build +#------------------------------------------------------------------------------- + +$(GOCOV_BIN): prebuild-check + @cd $(VENDOR_DIR)/github.com/axw/gocov/gocov/ && go build + +$(GOCOVMERGE_BIN): prebuild-check + @cd $(VENDOR_DIR)/github.com/wadey/gocovmerge && go build + + +#------------------------------------------------------------------------------- +# Clean targets +#------------------------------------------------------------------------------- + +CLEAN_TARGETS += clean-coverage +.PHONY: clean-coverage +# Removes all coverage files +clean-coverage: clean-coverage-unit clean-coverage-overall + -@rm -rf $(COV_DIR) + +CLEAN_TARGETS += clean-coverage-overall +.PHONY: clean-coverage-overall +# Removes overall coverage file +clean-coverage-overall: + -@rm -f $(COV_PATH_OVERALL) + +CLEAN_TARGETS += clean-coverage-unit +.PHONY: clean-coverage-unit +# Removes unit test coverage file +clean-coverage-unit: + -@rm -f $(COV_PATH_UNIT) diff --git a/pkg/controller/envdeployment/envdeployment_controller_test.go b/pkg/controller/envdeployment/envdeployment_controller_test.go new file mode 100644 index 0000000..b7b9943 --- /dev/null +++ b/pkg/controller/envdeployment/envdeployment_controller_test.go @@ -0,0 +1,16 @@ +package envdeployment + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestExample(t *testing.T) { + + require.True(t, true) + + t.Run("subtest example", func(t *testing.T) { + require.True(t, true) + }) +} diff --git a/test/README.md b/test/README.md new file mode 100644 index 0000000..6f48e4e --- /dev/null +++ b/test/README.md @@ -0,0 +1,25 @@ +## E2E Tests + +### How to Run + +#### TL;DR + +``` +make minishift-start +eval $(minishift docker-env) +make test-e2e +``` + +Make sure that you have minishift running. you can check it using `minishift status`. If not then start it using `make minishift-start` target. + +After successfully starting minishift, configure your shell to use docker daemon from minishift using `eval $(minishift docker-env)`. + +Now it's time to run E2E tests for `devopsconsole-operator` which will create it's required resources from `deploy/test/` on OpenShift use following command: + +``` +make test-e2e +``` + +This make target is building new docker image `$(DOCKER_REPO)/$(IMAGE_NAME):test`(e.g. `quay.io/openshiftio/devopsconsole-operator:test`) which is used in the operator's deployment manifests in e2e tests. + +Also remember that it uses the `system:admin` account for creating all required resources from `deploy/test` directory. \ No newline at end of file diff --git a/test/e2e/example_test.go b/test/e2e/example_test.go new file mode 100644 index 0000000..e43815e --- /dev/null +++ b/test/e2e/example_test.go @@ -0,0 +1,26 @@ +package e2e + +import ( + "testing" + + //framework "github.com/operator-framework/operator-sdk/pkg/test" + //"github.com/operator-framework/operator-sdk/pkg/test/e2eutil" + //"github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + //"golang.org/x/net/context" + //metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +func TestExample(t *testing.T) { + + require.True(t, true) + + // get global framework variables + //f := framework.Global + + // this is a unit test stub, add tests here + + t.Run("subtest example", func(t *testing.T) { + require.True(t, true) + }) +} diff --git a/test/e2e/main_test.go b/test/e2e/main_test.go new file mode 100644 index 0000000..35d5c41 --- /dev/null +++ b/test/e2e/main_test.go @@ -0,0 +1,11 @@ +package e2e + +import ( + "testing" + + framework "github.com/operator-framework/operator-sdk/pkg/test" +) + +func TestMain(m *testing.M) { + framework.MainEntry(m) +}