Skip to content

Commit

Permalink
test trustedCABundle ManagementState conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
Sara4994 committed Dec 6, 2024
1 parent a0ac2d2 commit fcc2720
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 32 deletions.
59 changes: 28 additions & 31 deletions tests/e2e/creation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
"fmt"
"log"
"reflect"
"strings"
"testing"
"time"

"github.com/go-logr/logr"
operatorv1 "github.com/openshift/api/operator/v1"
"github.com/stretchr/testify/require"
autoscalingv1 "k8s.io/api/autoscaling/v1"
Expand All @@ -32,7 +32,6 @@ import (
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/cluster"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/feature/serverless"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/metadata/labels"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/trustedcabundle"
)

func creationTestSuite(t *testing.T) {
Expand Down Expand Up @@ -452,43 +451,41 @@ func (tc *testContext) testDefaultCertsAvailable() error {
}

func (tc *testContext) testTrustedCABundle() error {
managementStateChangeTrustedCA := false
CAConfigMapName := "odh-trusted-ca-bundle"
CADataFieldName := "odh-ca-bundle.crt"

err := trustedcabundle.ConfigureTrustedCABundle(tc.ctx, tc.customClient, logr.Logger{}, tc.testDSCI, managementStateChangeTrustedCA)
if tc.testDSCI.Spec.TrustedCABundle.ManagementState == operatorv1.Managed {
foundConfigMap := &corev1.ConfigMap{}
err := tc.customClient.Get(tc.ctx, client.ObjectKey{
Name: CAConfigMapName,
Namespace: tc.testDSCI.Spec.ApplicationsNamespace,
}, foundConfigMap)

if err != nil {
return fmt.Errorf("Error while configuring trusted-ca-bundle: %w", err)
}
istrustedCABundleUpdated, err := trustedcabundle.IsTrustedCABundleUpdated(tc.ctx, tc.customClient, tc.testDSCI)

if istrustedCABundleUpdated == true {
return fmt.Errorf("odh-trusted-ca-bundle in config map does not match with DSCI's TrustedCABundle.CustomCABundle, needs update: %w", err)
}

err = trustedcabundle.AddCABundleCMInAllNamespaces(tc.ctx, tc.customClient, logr.Logger{}, tc.testDSCI)
if err != nil {
return fmt.Errorf("Config map not found, %w", err)
}

if err != nil {
return fmt.Errorf("failed adding configmap %s to all namespaces: %w", CAConfigMapName, err)
}
checkNewline := strings.HasSuffix(foundConfigMap.Data[CADataFieldName], "\n")

if err := trustedcabundle.RemoveCABundleCMInAllNamespaces(tc.ctx, tc.customClient); err != nil {
return fmt.Errorf("error deleting configmap %s from all namespaces %w", CAConfigMapName, err)
}

foundConfigMap := &corev1.ConfigMap{}
err = tc.customClient.Get(tc.ctx, client.ObjectKey{
Name: CAConfigMapName,
Namespace: tc.testDSCI.Spec.ApplicationsNamespace,
}, foundConfigMap)
if checkNewline == false {
fmt.Print("Newline not found at the end of configmap")
}

if err != nil {
return errors.New("Config map not found")
}
if strings.TrimSpace(foundConfigMap.Data[CADataFieldName]) != tc.testDSCI.Spec.TrustedCABundle.CustomCABundle {
return fmt.Errorf("odh-trusted-ca-bundle in config map does not match with DSCI's TrustedCABundle.CustomCABundle, needs update: %w", err)
}
} else {
foundConfigMap := &corev1.ConfigMap{}
err := tc.customClient.Get(tc.ctx, client.ObjectKey{
Name: CAConfigMapName,
Namespace: tc.testDSCI.Spec.ApplicationsNamespace,
}, foundConfigMap)

if foundConfigMap.Data[CADataFieldName] != tc.testDSCI.Spec.TrustedCABundle.CustomCABundle {
return fmt.Errorf("odh-trusted-ca-bundle in config map does not match with DSCI's TrustedCABundle.CustomCABundle, needs update: %w", err)
if k8serr.IsNotFound(err) {
fmt.Printf("Config map not found in the namespace")
} else {
return fmt.Errorf("failed to validate trusted CA bundle %w", err)
}
}
return nil
}
Expand Down
6 changes: 5 additions & 1 deletion tests/e2e/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,11 @@ func setupDSCICR(name string) *dsciv1.DSCInitialization {
},
TrustedCABundle: &dsciv1.TrustedCABundleSpec{
ManagementState: "Managed",
CustomCABundle: "-----Begin....",
CustomCABundle: `-----BEGIN CERTIFICATE-----
MIIFVjCCAz6gAwIBAgIUQ+NxE9izWRRdt86M/TX9b7wFjUUwDQYJKoZIhvcNAQEL
...
IrrVQJLuM7IjWcmOvFjai57QGfIvWcaMY1q6n6MLsLOaXLoRuBLpDLvPbmyAhykU
------END ------`,
},
ServiceMesh: &infrav1.ServiceMeshSpec{
ControlPlane: infrav1.ControlPlaneSpec{
Expand Down

0 comments on commit fcc2720

Please sign in to comment.