Skip to content

Commit

Permalink
gocheck test for repo-controller (#1996)
Browse files Browse the repository at this point in the history
  • Loading branch information
pavannd1 committed Apr 7, 2023
1 parent a8650ff commit 94cd184
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 43 deletions.
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ require (
github.com/lib/pq v1.10.7
github.com/luci/go-render v0.0.0-20160219211803-9a04cc21af0f
github.com/mitchellh/mapstructure v1.5.0
github.com/onsi/ginkgo/v2 v2.6.0
github.com/onsi/gomega v1.24.1

//pinned openshift to release-4.5 branch
github.com/openshift/api v0.0.0-20200526144822-34f54f12813a
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -419,11 +419,9 @@ github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+W
github.com/onsi/ginkgo v1.11.0 h1:JAKSXpt1YjtLA7YpPiqO9ss6sNXEsPfSGdwN0UHqzrw=
github.com/onsi/ginkgo v1.11.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo/v2 v2.6.0 h1:9t9b9vRUbFq3C4qKFCGkVuq/fIHji802N1nrtkh1mNc=
github.com/onsi/ginkgo/v2 v2.6.0/go.mod h1:63DOGlLAH8+REH8jUGdL3YpCpu7JODesutUjdENfUAc=
github.com/onsi/gomega v0.0.0-20170829124025-dcabb60a477c/go.mod h1:C1qb7wdrVGGVU+Z6iS04AVkA3Q65CEZX59MT0QO5uiA=
github.com/onsi/gomega v1.7.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/onsi/gomega v1.24.1 h1:KORJXNNTzJXzu4ScJWssJfJMnJ+2QJqhoQSRwNlze9E=
github.com/onsi/gomega v1.24.1/go.mod h1:3AOiACssS3/MajrniINInwbfOOtfZvplPzuRSmvt1jM=
github.com/openshift/api v0.0.0-20200521101457-60c476765272/go.mod h1:TkhafijfTiRi1Q3120/ZSE4oIWKQ4DGRh3byPywv4Mw=
github.com/openshift/api v0.0.0-20200526144822-34f54f12813a h1:riE/kCXnb051RWT/z+DytxKEZ3+JromVDl79rXAKyFY=
github.com/openshift/api v0.0.0-20200526144822-34f54f12813a/go.mod h1:l6TGeqJ92DrZBuWMNKcot1iZUHfbYSJyBWHGgg6Dn6s=
Expand Down
72 changes: 33 additions & 39 deletions pkg/controllers/repositoryserver/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,62 +17,56 @@ package repositoryserver
import (
"path/filepath"
"testing"
"time"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

. "gopkg.in/check.v1"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/envtest"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"

crkanisteriov1alpha1 "github.com/kanisterio/kanister/pkg/apis/cr/v1alpha1"
//+kubebuilder:scaffold:imports
)

// These tests use Ginkgo (BDD-style Go testing framework). Refer to
// http://onsi.github.io/ginkgo/ to learn more about Ginkgo.

var cfg *rest.Config
var k8sClient client.Client
var testEnv *envtest.Environment
// Hook up gocheck into the "go test" runner.
func Test(t *testing.T) { TestingT(t) }

func TestAPIs(t *testing.T) {
RegisterFailHandler(Fail)

RunSpecs(t, "Controller Suite")
type ControllerSuite struct {
testEnv *envtest.Environment
}

var _ = BeforeSuite(func() {
logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)))
var _ = Suite(&ControllerSuite{})

By("bootstrapping test environment")
testEnv = &envtest.Environment{
CRDDirectoryPaths: []string{filepath.Join("..", "pkg", "customresource")},
func (s *ControllerSuite) SetUpSuite(c *C) {
c.Log("Bootstrapping test environment with Kanister CRDs")
useExistingCluster := true
s.testEnv = &envtest.Environment{
CRDDirectoryPaths: []string{filepath.Join("..", "..", "customresource")},
ErrorIfCRDPathMissing: true,
UseExistingCluster: &useExistingCluster,
}

var err error
// cfg is defined in this file globally.
cfg, err = testEnv.Start()
Expect(err).NotTo(HaveOccurred())
Expect(cfg).NotTo(BeNil())
cfg, err := s.testEnv.Start()
c.Assert(err, IsNil)
c.Assert(cfg, NotNil)

err = crkanisteriov1alpha1.AddToScheme(scheme.Scheme)
Expect(err).NotTo(HaveOccurred())

//+kubebuilder:scaffold:scheme
c.Assert(err, IsNil)

k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
Expect(err).NotTo(HaveOccurred())
Expect(k8sClient).NotTo(BeNil())
k8sClient, err := client.New(cfg, client.Options{Scheme: scheme.Scheme})
c.Assert(err, IsNil)
c.Assert(k8sClient, NotNil)
}

})
func (s *ControllerSuite) TearDownSuite(c *C) {
if s.testEnv != nil {
c.Log("Tearing down the test environment")
err := s.testEnv.Stop()
c.Assert(err, IsNil)
}
}

var _ = AfterSuite(func() {
By("tearing down the test environment")
err := testEnv.Stop()
Expect(err).NotTo(HaveOccurred())
})
func (s *ControllerSuite) TestWatch(c *C) {
// We give it a few seconds complete it's scan. This isn't required for the
// test, but is a more realistic startup scenario.
time.Sleep(5 * time.Second)
}

0 comments on commit 94cd184

Please sign in to comment.