Skip to content
This repository has been archived by the owner on Oct 15, 2019. It is now read-only.

Commit

Permalink
Separate unit test and integration tests
Browse files Browse the repository at this point in the history
Skip tests that require environement sutup (e.g. k8s cluster)
when running tests in short mode. Add an additional 'integarion'
target to Makefile for running those tests.

Signed-off-by: Pablo Chacin <pchacin@suse.com>
  • Loading branch information
Pablo Chacin committed Nov 21, 2018
1 parent f359756 commit 063bb9b
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 183 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ check: $(GOLINT)

.PHONY: test
test:
@$(GO) test -short -v $(SOURCES_DIRS_GO) -coverprofile cover.out

.PHONY: integration
integration:
@$(GO) test -v $(SOURCES_DIRS_GO) -coverprofile cover.out

.PHONY: check
Expand Down
39 changes: 1 addition & 38 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,63 +1,26 @@
module github.com/kubic-project/registries-operator

require (
cloud.google.com/go v0.30.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/docker/distribution v2.6.2+incompatible // indirect
github.com/ghodss/yaml v1.0.0 // indirect
github.com/go-logr/logr v0.1.0 // indirect
github.com/go-logr/zapr v0.1.0 // indirect
github.com/gogo/protobuf v1.1.1 // indirect
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b
github.com/golang/groupcache v0.0.0-20180924190550-6f2cf27854a4 // indirect
github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c // indirect
github.com/google/gofuzz v0.0.0-20170612174753-24818f796faf // indirect
github.com/google/uuid v1.0.0 // indirect
github.com/googleapis/gnostic v0.2.0 // indirect
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7 // indirect
github.com/hashicorp/golang-lru v0.5.0 // indirect
github.com/imdario/mergo v0.3.6 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/json-iterator/go v1.1.5 // indirect
github.com/kubernetes/kubernetes v1.12.1
github.com/kubic-project/dex-operator v0.0.0-20181120155903-6899f85378b2
github.com/kubic-project/kubic-init v0.0.0-20181029092444-8266cfbf30df
github.com/mattbaird/jsonpatch v0.0.0-20171005235357-81af80346b1a // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 // indirect
github.com/onsi/gomega v1.4.2
github.com/pborman/uuid v0.0.0-20180906182336-adf5a7427709 // indirect
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
github.com/pkg/errors v0.8.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/renstrom/dedent v1.0.0
github.com/spf13/cobra v0.0.3
github.com/spf13/pflag v1.0.3
github.com/stevvooe/resumable v0.0.0-20180830230917-22b14a53ba50 // indirect
github.com/stretchr/testify v1.2.2 // indirect
github.com/ugorji/go v1.1.1 // indirect
github.com/yuroyoro/swalker v0.0.0-20160622113523-0a5950e9162f // indirect
go.uber.org/atomic v1.3.2 // indirect
go.uber.org/multierr v1.1.0 // indirect
go.uber.org/zap v1.9.1 // indirect
golang.org/x/crypto v0.0.0-20180222182404-49796115aa4b // indirect
golang.org/x/net v0.0.0-20181005035420-146acd28ed58
golang.org/x/oauth2 v0.0.0-20181003184128-c57b0facaced // indirect
golang.org/x/sys v0.0.0-20181005133103-4497e2df6f9e // indirect
golang.org/x/time v0.0.0-20180412165947-fbb02b2291d2 // indirect
golang.org/x/tools v0.0.0-20181101071927-45ff765b4815 // indirect
google.golang.org/appengine v1.2.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
k8s.io/api v0.0.0-20180712090710-2d6f90ab1293
k8s.io/apiextensions-apiserver v0.0.0-20180808065829-408db4a50408 // indirect
k8s.io/apimachinery v0.0.0-20180621070125-103fd098999d
k8s.io/apiserver v0.0.0-20180808060109-1844acd6a035
k8s.io/client-go v0.0.0-20180806134042-1f13a808da65
k8s.io/code-generator v0.0.0-20181101090831-772c233fdd26 // indirect
k8s.io/gengo v0.0.0-20181019081622-7338e4bfd691 // indirect
k8s.io/klog v0.1.0
k8s.io/kube-openapi v0.0.0-20180928202339-9dfdf9be683f // indirect
k8s.io/kubernetes v1.11.3
k8s.io/utils v0.0.0-20180918230422-cd34563cd63c // indirect
sigs.k8s.io/controller-runtime v0.1.4
sigs.k8s.io/testing_frameworks v0.0.0-20180709092217-5818a3a284a1 // indirect
)
124 changes: 0 additions & 124 deletions go.sum

This file was deleted.

4 changes: 4 additions & 0 deletions pkg/apis/kubic/v1beta1/registry_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,17 @@ package v1beta1
import (
"testing"

"github.com/kubic-project/registries-operator/pkg/test"
"github.com/onsi/gomega"
"golang.org/x/net/context"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
)

func TestStorageRegistry(t *testing.T) {

test.SkipUnlessIntegrationTesting(t)

key := types.NamespacedName{
Name: "foo",
Namespace: "default",
Expand Down
36 changes: 23 additions & 13 deletions pkg/apis/kubic/v1beta1/v1beta1_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"path/filepath"
"testing"

"github.com/kubic-project/registries-operator/pkg/test"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand All @@ -33,24 +34,33 @@ var cfg *rest.Config
var c client.Client

func TestMain(m *testing.M) {
t := &envtest.Environment{
CRDDirectoryPaths: []string{filepath.Join("..", "..", "..", "..", "configs", "crds")},
}

err := SchemeBuilder.AddToScheme(scheme.Scheme)
if err != nil {
log.Fatal(err)
}
var t *envtest.Environment

if cfg, err = t.Start(); err != nil {
log.Fatal(err)
}
if test.ShouldRunIntegrationSetupAndTeardown(m) {
t = &envtest.Environment{
CRDDirectoryPaths: []string{filepath.Join("..", "..", "..", "..", "configs", "crds")},
}

err := SchemeBuilder.AddToScheme(scheme.Scheme)
if err != nil {
log.Fatal(err)
}

if c, err = client.New(cfg, client.Options{Scheme: scheme.Scheme}); err != nil {
log.Fatal(err)
if cfg, err = t.Start(); err != nil {
log.Fatal(err)
}

if c, err = client.New(cfg, client.Options{Scheme: scheme.Scheme}); err != nil {
log.Fatal(err)
}
}

code := m.Run()
t.Stop()

if test.ShouldRunIntegrationSetupAndTeardown(m) {
t.Stop()
}

os.Exit(code)
}
26 changes: 18 additions & 8 deletions pkg/controller/registry/registry_controller_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"testing"

"github.com/kubic-project/registries-operator/pkg/apis"
"github.com/kubic-project/registries-operator/pkg/test"
"github.com/onsi/gomega"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
Expand All @@ -35,18 +36,27 @@ import (
var cfg *rest.Config

func TestMain(m *testing.M) {
t := &envtest.Environment{
CRDDirectoryPaths: []string{filepath.Join("..", "..", "..", "config", "crds")},
}
apis.AddToScheme(scheme.Scheme)

var err error
if cfg, err = t.Start(); err != nil {
log.Fatal(err)
var t *envtest.Environment

if test.ShouldRunIntegrationSetupAndTeardown(m) {
t = &envtest.Environment{
CRDDirectoryPaths: []string{filepath.Join("..", "..", "..", "config", "crds")},
}
apis.AddToScheme(scheme.Scheme)

var err error
if cfg, err = t.Start(); err != nil {
log.Fatal(err)
}
}

code := m.Run()
t.Stop()

if test.ShouldRunIntegrationSetupAndTeardown(m) {
t.Stop()
}

os.Exit(code)
}

Expand Down
4 changes: 4 additions & 0 deletions pkg/controller/registry/registry_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/reconcile"

kubicv1beta1 "github.com/kubic-project/registries-operator/pkg/apis/kubic/v1beta1"
"github.com/kubic-project/registries-operator/pkg/test"
)

var c client.Client
Expand All @@ -42,6 +43,9 @@ var depKey = types.NamespacedName{Name: "foo-deployment", Namespace: "default"}
const timeout = time.Second * 5

func TestReconcile(t *testing.T) {

test.SkipUnlessIntegrationTesting(t)

g := gomega.NewGomegaWithT(t)
instance := &kubicv1beta1.Registry{ObjectMeta: metav1.ObjectMeta{Name: "foo", Namespace: "default"}}

Expand Down
37 changes: 37 additions & 0 deletions pkg/test/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2018 SUSE LINUX GmbH, Nuernberg, Germany..
*
* 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.
*
*/
package test

import (
"flag"
"testing"
)

// SkipUnlessIntegrationTesting should skip this specific test if we are not running in integration
// testing mode
func SkipUnlessIntegrationTesting(t *testing.T) {
if testing.Short() {
t.SkipNow()
}
}

// ShouldRunIntegrationSetupAndTeardown should skip this specific test if we are not running in
// integration testing mode
func ShouldRunIntegrationSetupAndTeardown(m *testing.M) bool {
flag.Parse()
return !testing.Short()
}

0 comments on commit 063bb9b

Please sign in to comment.