Skip to content

Commit

Permalink
Merge pull request opendatahub-io#628 from hbelmiro/RHOAIENG-4261
Browse files Browse the repository at this point in the history
Replaced Ginkgo and Gomega with Testify in functional tests
  • Loading branch information
HumairAK authored May 6, 2024
2 parents 575c774 + e8a56f6 commit 93da360
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 132 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)
}
})
})
}
})
}
83 changes: 38 additions & 45 deletions controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,33 @@ package controllers

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

"github.com/go-logr/logr"
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 @@ -61,19 +58,19 @@ var (

const (
WorkingNamespace = "default"
DSPCRName = "testdsp"
timeout = time.Second * 6
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,19 +90,17 @@ 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

// Initialize logger
opts := zap.Options{
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 +111,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 +120,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 93da360

Please sign in to comment.