Skip to content

Commit

Permalink
Add Unit Test for Common sub-component reconciler
Browse files Browse the repository at this point in the history
  • Loading branch information
gmfrasca committed Aug 24, 2023
1 parent 0a0910e commit c374de0
Showing 1 changed file with 87 additions and 0 deletions.
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)
}

0 comments on commit c374de0

Please sign in to comment.