Skip to content

Commit

Permalink
Merge pull request operator-framework#140 from ecordell/unit-tests
Browse files Browse the repository at this point in the history
Unit test coverage reporting for gitlab
  • Loading branch information
ecordell committed Nov 3, 2017
2 parents 7a020d4 + 94ee59b commit f703814
Show file tree
Hide file tree
Showing 65 changed files with 425 additions and 243 deletions.
4 changes: 2 additions & 2 deletions .gitlab-ci.jsonnet
Expand Up @@ -77,8 +77,8 @@ local jobs = {
},

'unit-tests': unittest_stage {
coverage: @"/^TOTAL.*\s+(\d+\%)\s*$/",
script: ["make vendor", "make test"],
coverage: @"/\d\d\.\d.$/",
script: ["make vendor", "make test-cover"],
},

// End2End tests
Expand Down
4 changes: 2 additions & 2 deletions .gitlab-ci.yml
Expand Up @@ -183,11 +183,11 @@ unit-tests:
- mkdir -p $GOPATH/src/github.com/coreos-inc/alm
- cp -a $CI_PROJECT_DIR/* $GOPATH/src/github.com/coreos-inc/alm
- cd $GOPATH/src/github.com/coreos-inc/alm
coverage: /^TOTAL.*\s+(\d+\%)\s*$/
coverage: /\d\d\.\d.$/
image: quay.io/coreos/alm-ci:${CI_COMMIT_REF_SLUG}
script:
- make vendor
- make test
- make test-cover
stage: tests
tags:
- kubernetes
Expand Down
2 changes: 1 addition & 1 deletion Documentation/design/resources/schema_test.go
Expand Up @@ -26,7 +26,7 @@ import (
"k8s.io/apimachinery/pkg/runtime"
utilfeature "k8s.io/apiserver/pkg/util/feature"

"github.com/coreos-inc/alm/apis/clusterserviceversion/v1alpha1"
"github.com/coreos-inc/alm/pkg/apis/clusterserviceversion/v1alpha1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/validation/field"
)
Expand Down
8 changes: 3 additions & 5 deletions Documentation/install/alm_resources/alm_deployment.yaml
Expand Up @@ -35,14 +35,12 @@ spec:
path: /healthz
port: 8080
env:
- name: MY_POD_NAMESPACE
- name: OPERATOR_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: MY_POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: OPERATOR_NAME
value: alm-operator
imagePullSecrets:
- name: coreos-pull-secret

44 changes: 29 additions & 15 deletions Makefile
Expand Up @@ -5,23 +5,33 @@ CATALOG_PKG := github.com/coreos-inc/alm/cmd/catalog
CATALOG_EXECUTABLE := ./bin/catalog
IMAGE_REPO := quay.io/coreos/alm
IMAGE_TAG ?= "dev"
TOP_LEVEL := $(dir $(shell glide novendor))
PACKAGES := $(shell find $(TOP_LEVEL) -type d -not -path '*/\.*')

.PHONY: test run clean vendor vendor-update coverage

COVERUTIL := $(GOPATH)/bin/gocoverutil

$(COVERUTIL):
go get -u github.com/AlekSi/gocoverutil

all: test build

test:
go vet `glide novendor`
go test -v -race `glide novendor`
go vet ./pkg/...
go test -v ./Documentation/...
go test -v -race ./pkg/...

test-cover: $(COVERUTIL)
go vet ./pkg/...
go test -v ./Documentation/...
$(COVERUTIL) -coverprofile=cover.out test -v -race -covermode=atomic ./pkg/...
go tool cover -func=cover.out

cover: $(COVERUTIL)
$(COVERUTIL) -coverprofile=cover.out test -covermode=count ./pkg/...
go tool cover -func=cover.out

coverage:
echo "mode: count" > coverage-all.out
$(foreach pkg,$(PACKAGES),\
go test -coverprofile=coverage.out -covermode=count $(pkg);\
tail -n +2 coverage.out >> coverage-all.out;)
go tool cover -html=coverage-all.out
coverage-html: cover
go tool cover -html=cover.out

build:
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -i -o $(ALM_EXECUTABLE) $(ALM_PKG)
Expand Down Expand Up @@ -65,10 +75,14 @@ $(MOCKGEN):
go get github.com/golang/mock/mockgen

generate-mock-client: $(MOCKGEN)
mockgen -package=client -source=client/clusterserviceversion_client.go > client/zz_generated.mock_clusterserviceversion_client.go
mockgen -package=client -source=client/installplan_client.go > client/zz_generated.mock_installplan_client.go
mockgen -package=client -source=client/alphacatalogentry_client.go > client/zz_generated.mock_alphacatalogentry_client.go
mockgen -package=client -source=client/deployment_install_client.go > client/zz_generated.mock_deployment_install_client.go
mockgen -package=install -source=install/resolver.go > install/zz_generated.mock_resolver.go
mockgen -source=client/clusterserviceversion_client.go -package=alm > operators/alm/zz_generated.mock_clusterserviceversion_client_test.go
mockgen -package=client -source=client/installplan_client.go > client/zz_generated.mock_installplan_client_test.go
mockgen -source=client/alphacatalogentry_client.go -package=catalog > catalog/zz_generated.mock_alphacatalogentry_client_test.go
mockgen -source=client/deployment_install_client.go -package=install > install/zz_generated.mock_deployment_install_client_test.go
# can't use "source" mockgen, see: https://github.com/golang/mock/issues/11#issuecomment-294380653
mockgen -package=alm -imports client=github.com/coreos-inc/alm/pkg/vendor/github.com/coreos-inc/operator-client/pkg/client,v1=github.com/coreos-inc/alm/pkg/vendor/k8s.io/apimachinery/pkg/apis/meta/v1 github.com/coreos-inc/alm/pkg/install StrategyResolverInterface > operators/alm/zz_generated.mock_resolver_test.go
# mockgen doesn't handle vendored dependencies well: https://github.com/golang/mock/issues/30
sed -i '' s,github.com/coreos-inc/alm/pkg/vendor/,, operators/alm/zz_generated.mock_resolver_test.go
goimports -w operators/alm/zz_generated.mock_resolver_test.go

make gen-all: gen-ci codegen generate-mock-client
18 changes: 10 additions & 8 deletions cmd/alm/main.go
@@ -1,3 +1,5 @@
//+build !test

package main

import (
Expand All @@ -11,13 +13,13 @@ import (

"fmt"

"github.com/coreos-inc/alm/annotater"
"github.com/coreos-inc/alm/operators/alm"
"github.com/coreos-inc/alm/pkg/annotater"
"github.com/coreos-inc/alm/pkg/operators/alm"
)

const (
EnvOperatorPodName = "MY_POD_NAME"
EnvOperatorPodNamespace = "MY_POD_NAMESPACE"
EnvOperatorName = "OPERATOR_NAME"
EnvOperatorNamespace = "OPERATOR_NAMESPACE"
ALMManagedAnnotationKey = "alm-manager"
)

Expand All @@ -29,13 +31,13 @@ func main() {
watchedNamespaces := flag.String("watchedNamespaces", "", "comma separated list of namespaces that alm operator will watch")
flag.Parse()

namespace := os.Getenv(EnvOperatorPodNamespace)
namespace := os.Getenv(EnvOperatorNamespace)
if len(namespace) == 0 {
log.Fatalf("must set env %s", EnvOperatorPodNamespace)
log.Fatalf("must set env %s", EnvOperatorNamespace)
}
name := os.Getenv(EnvOperatorPodName)
name := os.Getenv(EnvOperatorName)
if len(name) == 0 {
log.Fatalf("must set env %s", EnvOperatorPodName)
log.Fatalf("must set env %s", EnvOperatorName)
}

// Serve a health check.
Expand Down
8 changes: 5 additions & 3 deletions cmd/catalog/main.go
@@ -1,3 +1,5 @@
//+build !test

package main

import (
Expand All @@ -9,9 +11,9 @@ import (

log "github.com/sirupsen/logrus"

source "github.com/coreos-inc/alm/catalog"
"github.com/coreos-inc/alm/client"
"github.com/coreos-inc/alm/operators/catalog"
source "github.com/coreos-inc/alm/pkg/catalog"
"github.com/coreos-inc/alm/pkg/client"
"github.com/coreos-inc/alm/pkg/operators/catalog"
)

const (
Expand Down
8 changes: 3 additions & 5 deletions deploy/alm-app/templates/alm_deployment.yaml
Expand Up @@ -38,14 +38,12 @@ spec:
path: /healthz
port: {{ .Values.alm.service.internalPort }}
env:
- name: MY_POD_NAMESPACE
- name: OPERATOR_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: MY_POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: OPERATOR_NAME
value: {{ template "fullname" . }}-alm
resources:
{{ toYaml .Values.alm.resources | indent 12 }}
{{- if .Values.alm.nodeSelector }}
Expand Down
4 changes: 2 additions & 2 deletions hack/k8s/codegen/run_in_docker.sh
Expand Up @@ -14,8 +14,8 @@ api_groups=("clusterserviceversion:v1alpha1" "installplan:v1alpha1"
for group in ${api_groups[@]}; do
echo -n "[$group] "
codegen::generate-groups deepcopy \
github.com/coreos-inc/alm/generated \
github.com/coreos-inc/alm/apis \
github.com/coreos-inc/alm/pkg/generated \
github.com/coreos-inc/alm/pkg/apis \
$group \
--go-header-file "./hack/k8s/codegen/boilerplate.go.txt"
done
126 changes: 0 additions & 126 deletions install/zz_generated.mock_resolver.go

This file was deleted.

2 changes: 1 addition & 1 deletion annotater/annotater.go → pkg/annotater/annotater.go
Expand Up @@ -81,7 +81,7 @@ func (a *Annotator) annotateNamespace(namespace *v1.Namespace, annotations map[s

for key, value := range annotations {
if existing, ok := namespace.Annotations[key]; ok && existing != value {
return fmt.Errorf("attempted to annotate namespace %s, but already annotated by %s:%s", namespace.Name, key, existing)
return fmt.Errorf("attempted to annotate namespace %s with %s:%s, but already annotated by %s:%s", namespace.Name, key, value, key, existing)
}
namespace.Annotations[key] = value
}
Expand Down

0 comments on commit f703814

Please sign in to comment.