Skip to content

Commit

Permalink
Replaced Ginkgo and Gomega with Testify in functional tests
Browse files Browse the repository at this point in the history
Signed-off-by: hbelmiro <helber.belmiro@gmail.com>
  • Loading branch information
hbelmiro committed Apr 17, 2024
1 parent ce249da commit bb5acb1
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 125 deletions.
59 changes: 30 additions & 29 deletions controllers/dspipeline_controller_func_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,67 +20,68 @@ package controllers

import (
"fmt"

mfc "github.com/manifestival/controller-runtime-client"
mf "github.com/manifestival/manifestival"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
util "github.com/opendatahub-io/data-science-pipelines-operator/controllers/testutil"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"testing"
)

var _ = Describe("The DS Pipeline Controller", Ordered, func() {
uc := util.UtilContext{}
BeforeAll(func() {
client := mfc.NewClient(k8sClient)
opts := mf.UseClient(client)
uc = util.UtilContext{
Ctx: ctx,
Ns: WorkingNamespace,
Opts: opts,
Client: k8sClient,
}
})
var uc = util.UtilContext{}

func setup() {
client := mfc.NewClient(k8sClient)
opts := mf.UseClient(client)
uc = util.UtilContext{
Ctx: ctx,
Ns: WorkingNamespace,
Opts: opts,
Client: k8sClient,
}
}

func (s *ControllerSuite) TestDSPipelineController() {
setup()

testcases := util.GenerateDeclarativeTestCases()
testcases := util.GenerateDeclarativeTestCases(s.T())

for caseCount, tc := range testcases {
// We assign local copies of all looping variables, as they are mutating
// we want the correct variables captured in each `It` closure, we do this
// by creating local variables
// https://onsi.github.io/ginkgo/#dynamically-generating-specs
testcase := tc
description := testcase.Description
Context(description, func() {
paths := testcase.Deploy
It(fmt.Sprintf("[case %x] Should successfully deploy the Custom Resource (and additional resources)", caseCount), func() {
paths := testcase.Deploy
s.T().Run(fmt.Sprintf("Case %d: %s", caseCount, description), func(t *testing.T) {
t.Run(fmt.Sprintf("[case %x] Should successfully deploy the Custom Resource (and additional resources)", caseCount), func(t *testing.T) {
viper.New()
viper.SetConfigFile(testcase.Config)
err := viper.ReadInConfig()
Expect(err).ToNot(HaveOccurred(), "Failed to read config file")
assert.NoError(t, err)
for _, path := range paths {
util.DeployResource(uc, path)
util.DeployResource(uc, path, t)
}
})

It(fmt.Sprintf("[case %x] Should create expected resources", caseCount), func() {
t.Run(fmt.Sprintf("[case %x] Should create expected resources", caseCount), func(t *testing.T) {
for _, resourcesCreated := range testcase.Expected.Created {
util.CompareResources(uc, resourcesCreated)
util.CompareResources(uc, resourcesCreated, t)
}
})

It(fmt.Sprintf("[case %x] Should expect NOT to create some resources", caseCount), func() {
t.Run(fmt.Sprintf("[case %x] Should expect NOT to create some resources", caseCount), func(t *testing.T) {
for _, resourcesNotCreated := range testcase.Expected.NotCreated {
util.ResourceDoesNotExists(uc, resourcesNotCreated)
util.ResourceDoesNotExists(uc, resourcesNotCreated, t)
}
})

It(fmt.Sprintf("[case %x] Should successfully delete the Custom Resource (and additional resources)", testcase), func() {
t.Run(fmt.Sprintf("[case %x] Should successfully delete the Custom Resource (and additional resources)", caseCount), func(t *testing.T) {
for _, path := range testcase.Deploy {
p := path
util.DeleteResource(uc, p)
util.DeleteResource(uc, p, t)
}
})
})
}
})
}
79 changes: 39 additions & 40 deletions controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,34 @@ package controllers

import (
"context"
"path/filepath"
"testing"
"time"

"github.com/go-logr/logr"
"github.com/onsi/gomega/format"
dspav1alpha1 "github.com/opendatahub-io/data-science-pipelines-operator/api/v1alpha1"
buildv1 "github.com/openshift/api/build/v1"
imagev1 "github.com/openshift/api/image/v1"
routev1 "github.com/openshift/api/route/v1"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"go.uber.org/zap/zapcore"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
ctrl "sigs.k8s.io/controller-runtime"

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

"github.com/go-logr/logr"
"k8s.io/client-go/kubernetes/scheme"
clientgoscheme "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"
"os"
"path/filepath"
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"testing"
"time"

dspav1alpha1 "github.com/opendatahub-io/data-science-pipelines-operator/api/v1alpha1"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/envtest"
//+kubebuilder:scaffold:imports
)

// These tests use Ginkgo (BDD-style Go testing framework). Refer to
// http://onsi.github.io/ginkgo/ to learn more about Ginkgo.
// These tests use Testify (A toolkit with common assertions and mocks). Refer to
// https://github.com/stretchr/testify to learn more about Testify.

var (
cfg *rest.Config
Expand All @@ -66,14 +64,17 @@ const (
interval = time.Millisecond * 2
)

func TestAPIs(t *testing.T) {
RegisterFailHandler(Fail)
type ControllerSuite struct {
suite.Suite
}

RunSpecs(t, "Controller Suite")
func TestAPIs(t *testing.T) {
testingSuite := new(ControllerSuite)
suite.Run(t, testingSuite)
}

var _ = BeforeEach(func() {
By("Overriding the Database and Object Store live connection functions with trivial stubs")
func (s *ControllerSuite) SetupTest() {
logf.Log.Info("Overriding the Database and Object Store live connection functions with trivial stubs")
ConnectAndQueryDatabase = func(
host string,
log logr.Logger,
Expand All @@ -93,9 +94,9 @@ var _ = BeforeEach(func() {
objStoreConnectionTimeout time.Duration) (bool, error) {
return true, nil
}
})
}

var _ = BeforeSuite(func() {
func (s *ControllerSuite) SetupSuite() {
ctx, cancel = context.WithCancel(context.TODO())

format.MaxLength = 0
Expand All @@ -105,7 +106,7 @@ var _ = BeforeSuite(func() {
Development: true,
TimeEncoder: zapcore.TimeEncoderOfLayout(time.RFC3339),
}
logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseFlagOptions(&opts)))
logf.SetLogger(zap.New(zap.WriteTo(os.Stdout), zap.UseFlagOptions(&opts)))

// Register API objects
utilruntime.Must(clientgoscheme.AddToScheme(scheme.Scheme))
Expand All @@ -116,7 +117,7 @@ var _ = BeforeSuite(func() {

//+kubebuilder:scaffold:scheme

By("bootstrapping test environment")
logf.Log.Info("bootstrapping test environment")
testEnv = &envtest.Environment{
CRDDirectoryPaths: []string{filepath.Join("..", "config", "crd", "bases"), filepath.Join("..", "config", "crd", "external")},
ErrorIfCRDPathMissing: true,
Expand All @@ -125,45 +126,43 @@ var _ = BeforeSuite(func() {
var err error
// cfg is defined in this file globally.
cfg, err = testEnv.Start()
Expect(err).NotTo(HaveOccurred())
Expect(cfg).NotTo(BeNil())
assert.NoError(s.T(), err)
assert.NotNil(s.T(), cfg)

// Initialize Kubernetes client
k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
Expect(err).NotTo(HaveOccurred())
Expect(k8sClient).NotTo(BeNil())
assert.NoError(s.T(), err)
assert.NotNil(s.T(), k8sClient)

// Setup controller manager
mgr, err := ctrl.NewManager(cfg, ctrl.Options{
Scheme: scheme.Scheme,
LeaderElection: false,
MetricsBindAddress: "0",
})
Expect(err).NotTo(HaveOccurred())
assert.NoError(s.T(), err)

err = (&DSPAReconciler{
Client: k8sClient,
Log: ctrl.Log.WithName("controllers").WithName("ds-pipelines-controller"),
Scheme: scheme.Scheme,
TemplatesPath: "../config/internal/",
}).SetupWithManager(mgr)
Expect(err).ToNot(HaveOccurred())
assert.NoError(s.T(), err)

// Start the manager
go func() {
defer GinkgoRecover()
err = mgr.Start(ctx)
Expect(err).ToNot(HaveOccurred(), "Failed to run manager")
assert.NoError(s.T(), err, "Failed to run manager")
}()
}

})

var _ = AfterSuite(func() {
func (s *ControllerSuite) TearDownSuite() {
// Give some time to allow workers to gracefully shutdown
time.Sleep(5 * time.Second)
cancel()
By("tearing down the test environment")
logf.Log.Info("tearing down the test environment")
time.Sleep(1 * time.Second)
err := testEnv.Stop()
Expect(err).NotTo(HaveOccurred())
})
assert.NoError(s.T(), err)
}
Loading

0 comments on commit bb5acb1

Please sign in to comment.