Skip to content

Commit

Permalink
Merge pull request #616 from HumairAK/add_params_unittest
Browse files Browse the repository at this point in the history
Add params unittest cases for cabundle
  • Loading branch information
HumairAK authored Mar 25, 2024
2 parents b80afc2 + 0613258 commit 7228357
Show file tree
Hide file tree
Showing 8 changed files with 280 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ repos:
exclude: README.md
- id: check-merge-conflict
- id: end-of-file-fixer
exclude: controllers/testdata/tls/ca-bundle.crt
exclude: controllers/testdata/tls
- id: check-added-large-files
- id: check-case-conflict
- id: check-json
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ $(CONTROLLER_GEN): $(LOCALBIN)
.PHONY: envtest
envtest: $(ENVTEST) ## Download envtest-setup locally if necessary.
$(ENVTEST): $(LOCALBIN)
test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest
test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@release-0.16

.PHONY: bundle
bundle: manifests kustomize ## Generate bundle manifests and metadata, then validate generated files.
Expand Down
6 changes: 3 additions & 3 deletions controllers/dspipeline_fake_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ func NewFakeController() *DSPAReconciler {
// Create Scheme
FakeScheme := scheme.Scheme
utilruntime.Must(clientgoscheme.AddToScheme(FakeScheme))
utilruntime.Must(buildv1.AddToScheme(FakeScheme))
utilruntime.Must(imagev1.AddToScheme(FakeScheme))
utilruntime.Must(routev1.AddToScheme(FakeScheme))
utilruntime.Must(buildv1.Install(FakeScheme))
utilruntime.Must(imagev1.Install(FakeScheme))
utilruntime.Must(routev1.Install(FakeScheme))
utilruntime.Must(dspav1alpha1.AddToScheme(FakeScheme))
FakeBuilder.WithScheme(FakeScheme)

Expand Down
9 changes: 6 additions & 3 deletions controllers/dspipeline_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ func (p *DSPAParams) SetupObjectParams(ctx context.Context, dsp *dspa.DataScienc

}

func (p *DSPAParams) SetupMLMD(ctx context.Context, dsp *dspa.DataSciencePipelinesApplication, client client.Client, log logr.Logger) error {
func (p *DSPAParams) SetupMLMD(dsp *dspa.DataSciencePipelinesApplication, log logr.Logger) error {
if p.UsingV2Pipelines(dsp) {
if p.MLMD == nil {
log.Info("MLMD not specified, but is a required component for V2 Pipelines. Including MLMD with default specs.")
Expand Down Expand Up @@ -666,7 +666,10 @@ func (p *DSPAParams) ExtractParams(ctx context.Context, dsp *dspa.DataSciencePip
if sysCertsErr != nil {
return sysCertsErr
}
p.APICustomPemCerts = append(p.APICustomPemCerts, certs)

if len(certs) != 0 {
p.APICustomPemCerts = append(p.APICustomPemCerts, certs)
}
}

p.CustomCABundle = &dspa.CABundle{
Expand Down Expand Up @@ -758,7 +761,7 @@ func (p *DSPAParams) ExtractParams(ctx context.Context, dsp *dspa.DataSciencePip
setStringDefault(argoExecImageFromConfig, &p.WorkflowController.ArgoExecImage)
}

err := p.SetupMLMD(ctx, dsp, client, log)
err := p.SetupMLMD(dsp, log)
if err != nil {
return err
}
Expand Down
228 changes: 228 additions & 0 deletions controllers/dspipeline_params_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
//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 (
dspav1alpha1 "github.com/opendatahub-io/data-science-pipelines-operator/api/v1alpha1"
"github.com/opendatahub-io/data-science-pipelines-operator/controllers/testutil"
"github.com/stretchr/testify/assert"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"testing"
)

type Client struct {
Clientset kubernetes.Interface
}

func TestExtractParams_WithEmptyDSPA(t *testing.T) {
dspa := testutil.CreateEmptyDSPA()
ctx, params, reconciler := CreateNewTestObjects()
err := params.ExtractParams(ctx, dspa, reconciler.Client, reconciler.Log)
assert.Nil(t, err)
}

func TestExtractParams_CABundle(t *testing.T) {

ctx, _, client := CreateNewTestObjects()

tt := []struct {
msg string
dsp *dspav1alpha1.DataSciencePipelinesApplication
CustomCABundleRootMountPath string
CustomSSLCertDir *string
PiplinesCABundleMountPath string
SSLCertFileEnv string
APICustomPemCerts [][]byte
CustomCABundle *dspav1alpha1.CABundle
ConfigMapPreReq []*v1.ConfigMap
errorMsg string
}{
{
msg: "no bundle provided",
dsp: testutil.CreateEmptyDSPA(),
CustomCABundleRootMountPath: "/dsp-custom-certs",
CustomSSLCertDir: nil,
PiplinesCABundleMountPath: "/dsp-custom-certs/dsp-ca.crt",
APICustomPemCerts: nil,
CustomCABundle: nil,
},
{
msg: "user bundle provided, but no configmap",
dsp: testutil.CreateDSPAWithAPIServerCABundle("testcakey", "testcaname"),
CustomCABundleRootMountPath: "/dsp-custom-certs",
CustomSSLCertDir: nil,
PiplinesCABundleMountPath: "/dsp-custom-certs/dsp-ca.crt",
APICustomPemCerts: nil,
CustomCABundle: nil,
ConfigMapPreReq: []*v1.ConfigMap{},
errorMsg: "configmaps \"testcaname\" not found",
},
{
msg: "user bundle provided",
dsp: testutil.CreateDSPAWithAPIServerCABundle("testcakey", "testcaname"),
CustomCABundleRootMountPath: "/dsp-custom-certs",
CustomSSLCertDir: strPtr("/dsp-custom-certs:/etc/ssl/certs:/etc/pki/tls/certs"),
PiplinesCABundleMountPath: "/dsp-custom-certs/dsp-ca.crt",
APICustomPemCerts: [][]byte{[]byte("bundle-contents")},
CustomCABundle: &dspav1alpha1.CABundle{ConfigMapKey: "dsp-ca.crt", ConfigMapName: "dsp-trusted-ca-testdspa"},
ConfigMapPreReq: []*v1.ConfigMap{
{
ObjectMeta: metav1.ObjectMeta{Name: "testcaname", Namespace: "testnamespace"},
Data: map[string]string{"testcakey": "bundle-contents"},
},
},
},
{
msg: "odh-trusted-ca bundle provided",
dsp: testutil.CreateEmptyDSPA(),
CustomCABundleRootMountPath: "/dsp-custom-certs",
CustomSSLCertDir: strPtr("/dsp-custom-certs:/etc/ssl/certs:/etc/pki/tls/certs"),
PiplinesCABundleMountPath: "/dsp-custom-certs/dsp-ca.crt",
APICustomPemCerts: [][]byte{[]byte("odh-bundle-contents")},
CustomCABundle: &dspav1alpha1.CABundle{ConfigMapKey: "dsp-ca.crt", ConfigMapName: "dsp-trusted-ca-testdspa"},
ConfigMapPreReq: []*v1.ConfigMap{
{
ObjectMeta: metav1.ObjectMeta{Name: "odh-trusted-ca-bundle", Namespace: "testnamespace"},
Data: map[string]string{"testcakey": "odh-bundle-contents"},
},
},
},
{
msg: "some empty values in odh-trusted-ca bundle provided",
dsp: testutil.CreateEmptyDSPA(),
CustomCABundleRootMountPath: "/dsp-custom-certs",
CustomSSLCertDir: strPtr("/dsp-custom-certs:/etc/ssl/certs:/etc/pki/tls/certs"),
PiplinesCABundleMountPath: "/dsp-custom-certs/dsp-ca.crt",
APICustomPemCerts: [][]byte{[]byte("odh-bundle-contents-2")},
CustomCABundle: &dspav1alpha1.CABundle{ConfigMapKey: "dsp-ca.crt", ConfigMapName: "dsp-trusted-ca-testdspa"},
ConfigMapPreReq: []*v1.ConfigMap{
{
ObjectMeta: metav1.ObjectMeta{Name: "odh-trusted-ca-bundle", Namespace: "testnamespace"},
Data: map[string]string{"ca-bundle.crt": "", "odh-ca-bundle.crt": "odh-bundle-contents-2"},
},
},
},
{
msg: "some empty values in odh-trusted-ca bundle provided",
dsp: testutil.CreateEmptyDSPA(),
CustomCABundleRootMountPath: "/dsp-custom-certs",
CustomSSLCertDir: nil,
PiplinesCABundleMountPath: "/dsp-custom-certs/dsp-ca.crt",
APICustomPemCerts: nil,
CustomCABundle: nil,
ConfigMapPreReq: []*v1.ConfigMap{
{
ObjectMeta: metav1.ObjectMeta{Name: "odh-trusted-ca-bundle", Namespace: "testnamespace"},
Data: map[string]string{"ca-bundle.crt": "", "odh-ca-bundle.crt": ""},
},
},
},
{
msg: "both user and odh-trusted-ca bundle provided",
dsp: testutil.CreateDSPAWithAPIServerCABundle("testcakey", "testcaname"),
CustomCABundleRootMountPath: "/dsp-custom-certs",
CustomSSLCertDir: strPtr("/dsp-custom-certs:/etc/ssl/certs:/etc/pki/tls/certs"),
PiplinesCABundleMountPath: "/dsp-custom-certs/dsp-ca.crt",
APICustomPemCerts: [][]byte{[]byte("odh-bundle-contents"), []byte("bundle-contents")},
CustomCABundle: &dspav1alpha1.CABundle{ConfigMapKey: "dsp-ca.crt", ConfigMapName: "dsp-trusted-ca-testdspa"},
ConfigMapPreReq: []*v1.ConfigMap{
{
ObjectMeta: metav1.ObjectMeta{Name: "odh-trusted-ca-bundle", Namespace: "testnamespace"},
Data: map[string]string{"testcakey": "odh-bundle-contents"},
},
{
ObjectMeta: metav1.ObjectMeta{Name: "testcaname", Namespace: "testnamespace"},
Data: map[string]string{"testcakey": "bundle-contents"},
},
},
},
{
msg: "both user and odh-trusted-ca bundle provided with non empty SSL_CERT_FILE",
dsp: testutil.CreateDSPAWithAPIServerCABundle("testcakey", "testcaname"),
CustomCABundleRootMountPath: "/dsp-custom-certs",
CustomSSLCertDir: strPtr("/dsp-custom-certs:/etc/ssl/certs:/etc/pki/tls/certs"),
PiplinesCABundleMountPath: "/dsp-custom-certs/dsp-ca.crt",
APICustomPemCerts: [][]byte{[]byte("odh-bundle-contents"), []byte("bundle-contents"), []byte("dummycontent")},
CustomCABundle: &dspav1alpha1.CABundle{ConfigMapKey: "dsp-ca.crt", ConfigMapName: "dsp-trusted-ca-testdspa"},
ConfigMapPreReq: []*v1.ConfigMap{
{
ObjectMeta: metav1.ObjectMeta{Name: "odh-trusted-ca-bundle", Namespace: "testnamespace"},
Data: map[string]string{"testcakey": "odh-bundle-contents"},
},
{
ObjectMeta: metav1.ObjectMeta{Name: "testcaname", Namespace: "testnamespace"},
Data: map[string]string{"testcakey": "bundle-contents"},
},
},
SSLCertFileEnv: "testdata/tls/dummy-ca-bundle.crt",
},
}

for _, test := range tt {
t.Run(test.msg, func(t *testing.T) {
if test.SSLCertFileEnv == "" {
t.Setenv("SSL_CERT_FILE", "testdata/tls/empty-ca-bundle.crt")
} else {
t.Setenv("SSL_CERT_FILE", test.SSLCertFileEnv)
}

if test.ConfigMapPreReq != nil && len(test.ConfigMapPreReq) > 0 {
for _, cfg := range test.ConfigMapPreReq {
err := client.Create(ctx, cfg)
assert.Nil(t, err)
}
}

actualParams := &DSPAParams{}
extractError := actualParams.ExtractParams(ctx, test.dsp, client.Client, client.Log)
if test.errorMsg != "" {
assert.Contains(t, extractError.Error(), test.errorMsg)
} else {
assert.Nil(t, extractError)
}

actualCustomCABundleRootMountPath := actualParams.CustomCABundleRootMountPath
assert.Equal(t, actualCustomCABundleRootMountPath, test.CustomCABundleRootMountPath)

actualCustomSSLCertDir := actualParams.CustomSSLCertDir
assert.Equal(t, actualCustomSSLCertDir, test.CustomSSLCertDir)

actualPipelinesCABundleMountPath := actualParams.PiplinesCABundleMountPath
assert.Equal(t, actualPipelinesCABundleMountPath, test.PiplinesCABundleMountPath)

actualAPICustomPemCerts := actualParams.APICustomPemCerts
assert.Equal(t, actualAPICustomPemCerts, test.APICustomPemCerts)

actualCustomCABundle := actualParams.CustomCABundle
assert.Equal(t, actualCustomCABundle, test.CustomCABundle)

if test.ConfigMapPreReq != nil && len(test.ConfigMapPreReq) > 0 {
for _, cfg := range test.ConfigMapPreReq {
err := client.Delete(ctx, cfg)
assert.Nil(t, err)
}
}
})
}
}

func strPtr(v string) *string {
return &v
}
1 change: 1 addition & 0 deletions controllers/testdata/tls/dummy-ca-bundle.crt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dummycontent
Empty file.
41 changes: 40 additions & 1 deletion controllers/testutil/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package testutil
import (
"context"
"fmt"

dspav1alpha1 "github.com/opendatahub-io/data-science-pipelines-operator/api/v1alpha1"
"os"
"time"

Expand Down Expand Up @@ -210,3 +210,42 @@ func GenerateDeclarativeTestCases() []Case {

return testcases
}

func CreateEmptyDSPA() *dspav1alpha1.DataSciencePipelinesApplication {
dspa := &dspav1alpha1.DataSciencePipelinesApplication{
Spec: dspav1alpha1.DSPASpec{
APIServer: &dspav1alpha1.APIServer{Deploy: false},
MLMD: &dspav1alpha1.MLMD{Deploy: false},
PersistenceAgent: &dspav1alpha1.PersistenceAgent{Deploy: false},
ScheduledWorkflow: &dspav1alpha1.ScheduledWorkflow{Deploy: false},
MlPipelineUI: &dspav1alpha1.MlPipelineUI{
Deploy: false,
Image: "testimage-MlPipelineUI:test",
},
WorkflowController: &dspav1alpha1.WorkflowController{Deploy: false},
Database: &dspav1alpha1.Database{DisableHealthCheck: false, MariaDB: &dspav1alpha1.MariaDB{Deploy: false}},
ObjectStorage: &dspav1alpha1.ObjectStorage{
DisableHealthCheck: false,
Minio: &dspav1alpha1.Minio{
Deploy: false,
Image: "testimage-Minio:test",
},
},
},
}
dspa.Name = "testdspa"
dspa.Namespace = "testnamespace"
return dspa
}

func CreateDSPAWithAPIServerCABundle(key string, cfgmapName string) *dspav1alpha1.DataSciencePipelinesApplication {
dspa := CreateEmptyDSPA()
dspa.Spec.APIServer = &dspav1alpha1.APIServer{
Deploy: true,
CABundle: &dspav1alpha1.CABundle{
ConfigMapKey: key,
ConfigMapName: cfgmapName,
},
}
return dspa
}

0 comments on commit 7228357

Please sign in to comment.