Skip to content

Commit

Permalink
Merge pull request #278 from gmfrasca/add-unit-tests
Browse files Browse the repository at this point in the history
Add Unit Tests
  • Loading branch information
openshift-merge-robot committed Aug 25, 2023
2 parents 2ece499 + b54afb5 commit d7ff069
Show file tree
Hide file tree
Showing 15 changed files with 1,320 additions and 6 deletions.
10 changes: 9 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,15 @@ vet: ## Run go vet against code.

.PHONY: test
test: manifests generate fmt vet envtest ## Run tests.
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test ./... -coverprofile cover.out
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test ./... --tags=test_all -coverprofile cover.out

.PHONY: unittest
unittest: manifests generate fmt vet envtest ## Run tests.
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test ./... -v --tags=test_unit -coverprofile cover.out

.PHONY: functest
functest: manifests generate fmt vet envtest ## Run tests.
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test ./... --tags=test_functional -coverprofile cover.out

##@ Build

Expand Down
115 changes: 115 additions & 0 deletions controllers/apiserver_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
//go:build test_all || test_unit

/*
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 controllers

import (
"testing"

dspav1alpha1 "github.com/opendatahub-io/data-science-pipelines-operator/api/v1alpha1"
"github.com/stretchr/testify/assert"
appsv1 "k8s.io/api/apps/v1"
)

func TestDeployAPIServer(t *testing.T) {
testNamespace := "testnamespace"
testDSPAName := "testdspa"
expectedAPIServerName := "ds-pipeline-testdspa"

// Construct DSPASpec with deployed APIServer
dspa := &dspav1alpha1.DataSciencePipelinesApplication{
Spec: dspav1alpha1.DSPASpec{
APIServer: &dspav1alpha1.APIServer{
Deploy: true,
},
Database: &dspav1alpha1.Database{
DisableHealthCheck: false,
MariaDB: &dspav1alpha1.MariaDB{
Deploy: true,
},
},
ObjectStorage: &dspav1alpha1.ObjectStorage{
DisableHealthCheck: false,
Minio: &dspav1alpha1.Minio{
Deploy: false,
Image: "someimage",
},
},
},
}

// Enrich DSPA with name+namespace
dspa.Name = testDSPAName
dspa.Namespace = testNamespace

// Create Context, Fake Controller and Params
ctx, params, reconciler := CreateNewTestObjects()
err := params.ExtractParams(ctx, dspa, reconciler.Client, reconciler.Log)
assert.Nil(t, err)

// Assert APIServer Deployment doesn't yet exist
deployment := &appsv1.Deployment{}
created, err := reconciler.IsResourceCreated(ctx, deployment, expectedAPIServerName, testNamespace)
assert.False(t, created)
assert.Nil(t, err)

// Run test reconciliation
err = reconciler.ReconcileAPIServer(ctx, dspa, params)
assert.Nil(t, err)

// Assert APIServer Deployment now exists
deployment = &appsv1.Deployment{}
created, err = reconciler.IsResourceCreated(ctx, deployment, expectedAPIServerName, testNamespace)
assert.True(t, created)
assert.Nil(t, err)
}

func TestDontDeployAPIServer(t *testing.T) {
testNamespace := "testnamespace"
testDSPAName := "testdspa"
expectedAPIServerName := "ds-pipeline-testdspa"

// Construct DSPASpec with non-deployed APIServer
dspa := &dspav1alpha1.DataSciencePipelinesApplication{
Spec: dspav1alpha1.DSPASpec{
APIServer: &dspav1alpha1.APIServer{
Deploy: false,
},
},
}

// Enrich DSPA with name+namespace
dspa.Namespace = testNamespace
dspa.Name = testDSPAName

// Create Context, Fake Controller and Params
ctx, params, reconciler := CreateNewTestObjects()

// Ensure APIServer Deployment doesn't yet exist
created, err := reconciler.IsResourceCreated(ctx, dspa, testDSPAName, testNamespace)
assert.False(t, created)
assert.Nil(t, err)

// Run test reconciliation
err = reconciler.ReconcileAPIServer(ctx, dspa, params)
assert.Nil(t, err)

// Ensure APIServer Deployment still doesn't exist
created, err = reconciler.IsResourceCreated(ctx, dspa, expectedAPIServerName, testNamespace)
assert.False(t, created)
assert.Nil(t, err)
}
87 changes: 87 additions & 0 deletions controllers/common_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
//go:build test_all || test_unit

/*
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 controllers

import (
"testing"

dspav1alpha1 "github.com/opendatahub-io/data-science-pipelines-operator/api/v1alpha1"
"github.com/stretchr/testify/assert"
networkingv1 "k8s.io/api/networking/v1"
)

func TestDeployCommonPolicies(t *testing.T) {
testNamespace := "testnamespace"
testDSPAName := "testdspa"
expectedNetworkPolicyName := "ds-pipelines-testdspa"
expectedEnvoyNetworkPolicyName := "ds-pipelines-envoy-testdspa"

// Construct Basic DSPA Spec
dspa := &dspav1alpha1.DataSciencePipelinesApplication{
Spec: dspav1alpha1.DSPASpec{
Database: &dspav1alpha1.Database{
DisableHealthCheck: false,
MariaDB: &dspav1alpha1.MariaDB{
Deploy: true,
},
},
ObjectStorage: &dspav1alpha1.ObjectStorage{
DisableHealthCheck: false,
Minio: &dspav1alpha1.Minio{
Deploy: false,
Image: "someimage",
},
},
},
}

// Enrich DSPA with name+namespace
dspa.Name = testDSPAName
dspa.Namespace = testNamespace

// Create Context, Fake Controller and Params
ctx, params, reconciler := CreateNewTestObjects()
err := params.ExtractParams(ctx, dspa, reconciler.Client, reconciler.Log)
assert.Nil(t, err)

// Assert Common NetworkPolicies don't yet exist
np := &networkingv1.NetworkPolicy{}
created, err := reconciler.IsResourceCreated(ctx, np, expectedNetworkPolicyName, testNamespace)
assert.False(t, created)
assert.Nil(t, err)

np = &networkingv1.NetworkPolicy{}
created, err = reconciler.IsResourceCreated(ctx, np, expectedEnvoyNetworkPolicyName, testNamespace)
assert.False(t, created)
assert.Nil(t, err)

// Run test reconciliation
err = reconciler.ReconcileCommon(dspa, params)
assert.Nil(t, err)

// Assert Common NetworkPolicies now exist
np = &networkingv1.NetworkPolicy{}
created, err = reconciler.IsResourceCreated(ctx, np, expectedNetworkPolicyName, testNamespace)
assert.True(t, created)
assert.Nil(t, err)

np = &networkingv1.NetworkPolicy{}
created, err = reconciler.IsResourceCreated(ctx, np, expectedEnvoyNetworkPolicyName, testNamespace)
assert.True(t, created)
assert.Nil(t, err)
}
126 changes: 126 additions & 0 deletions controllers/database_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
//go:build test_all || test_unit

/*
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 controllers

import (
"testing"

dspav1alpha1 "github.com/opendatahub-io/data-science-pipelines-operator/api/v1alpha1"
"github.com/stretchr/testify/assert"
appsv1 "k8s.io/api/apps/v1"
)

func TestDeployDatabase(t *testing.T) {
testNamespace := "testnamespace"
testDSPAName := "testdspa"
expectedDatabaseName := "mariadb-testdspa"

// Construct DSPA Spec with deployed MariaDB Database
dspa := &dspav1alpha1.DataSciencePipelinesApplication{
Spec: dspav1alpha1.DSPASpec{
Database: &dspav1alpha1.Database{
DisableHealthCheck: false,
MariaDB: &dspav1alpha1.MariaDB{
Deploy: true,
},
},
ObjectStorage: &dspav1alpha1.ObjectStorage{
DisableHealthCheck: false,
Minio: &dspav1alpha1.Minio{
Deploy: false,
Image: "someimage",
},
},
},
}

// Enrich DSPA with name+namespace
dspa.Name = testDSPAName
dspa.Namespace = testNamespace

// Create Context, Fake Controller and Params
ctx, params, reconciler := CreateNewTestObjects()
err := params.ExtractParams(ctx, dspa, reconciler.Client, reconciler.Log)
assert.Nil(t, err)

// Assert Database Deployment doesn't yet exist
deployment := &appsv1.Deployment{}
created, err := reconciler.IsResourceCreated(ctx, deployment, expectedDatabaseName, testNamespace)
assert.False(t, created)
assert.Nil(t, err)

// Run test reconciliation
err = reconciler.ReconcileDatabase(ctx, dspa, params)
assert.Nil(t, err)

// Assert Database Deployment now exists
deployment = &appsv1.Deployment{}
created, err = reconciler.IsResourceCreated(ctx, deployment, expectedDatabaseName, testNamespace)
assert.True(t, created)
assert.Nil(t, err)
}

func TestDontDeployDatabase(t *testing.T) {
testNamespace := "testnamespace"
testDSPAName := "testdspa"
expectedDatabaseName := "mariadb-testdspa"

// Construct DSPA Spec with non-deployed MariaDB Database
dspa := &dspav1alpha1.DataSciencePipelinesApplication{
Spec: dspav1alpha1.DSPASpec{
Database: &dspav1alpha1.Database{
DisableHealthCheck: false,
MariaDB: &dspav1alpha1.MariaDB{
Deploy: false,
},
},
ObjectStorage: &dspav1alpha1.ObjectStorage{
DisableHealthCheck: false,
Minio: &dspav1alpha1.Minio{
Deploy: false,
Image: "someimage",
},
},
},
}

// Enrich DSPA with name+namespace
dspa.Name = testDSPAName
dspa.Namespace = testNamespace

// Create Context, Fake Controller and Params
ctx, params, reconciler := CreateNewTestObjects()
err := params.ExtractParams(ctx, dspa, reconciler.Client, reconciler.Log)
assert.Nil(t, err)

// Assert Database Deployment doesn't yet exist
deployment := &appsv1.Deployment{}
created, err := reconciler.IsResourceCreated(ctx, deployment, expectedDatabaseName, testNamespace)
assert.False(t, created)
assert.Nil(t, err)

// Run test reconciliation
err = reconciler.ReconcileDatabase(ctx, dspa, params)
assert.Nil(t, err)

// Assert Database Deployment still doesn't exist
deployment = &appsv1.Deployment{}
created, err = reconciler.IsResourceCreated(ctx, deployment, expectedDatabaseName, testNamespace)
assert.False(t, created)
assert.Nil(t, err)
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:build test_all || test_functional
// +build test_all test_functional

/*
Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -17,6 +20,7 @@ package controllers

import (
"fmt"

mfc "github.com/manifestival/controller-runtime-client"
mf "github.com/manifestival/manifestival"
. "github.com/onsi/ginkgo/v2"
Expand Down
Loading

0 comments on commit d7ff069

Please sign in to comment.