From c45e08df2eba55570c65ddcfe4523640808a0f60 Mon Sep 17 00:00:00 2001 From: Alexander Chuzhoy Date: Mon, 22 Apr 2024 15:35:51 -0400 Subject: [PATCH] lifecycle: add unit test for IBU. Signed-off-by: Alexander Chuzhoy --- pkg/clients/clients.go | 4 + pkg/lca/ibu_test.go | 693 +++++++++++++++++++++++++++++++++++++++++ pkg/lca/ica_test.go | 1 - 3 files changed, 697 insertions(+), 1 deletion(-) create mode 100644 pkg/lca/ibu_test.go delete mode 100644 pkg/lca/ica_test.go diff --git a/pkg/clients/clients.go b/pkg/clients/clients.go index c1427aacf..9255e84c7 100644 --- a/pkg/clients/clients.go +++ b/pkg/clients/clients.go @@ -429,6 +429,10 @@ func GetTestClients(tcp TestClientParams) *Settings { genericClientObjects = append(genericClientObjects, v) case *policiesv1beta1.PolicySet: genericClientObjects = append(genericClientObjects, v) + case *lcav1alpha1.ImageBasedUpgrade: + genericClientObjects = append(genericClientObjects, v) + case *lcasgv1alpha1.SeedGenerator: + genericClientObjects = append(genericClientObjects, v) // Velero Client Objects case *velerov1.Backup: veleroClientObjects = append(veleroClientObjects, v) diff --git a/pkg/lca/ibu_test.go b/pkg/lca/ibu_test.go new file mode 100644 index 000000000..b82d8c67c --- /dev/null +++ b/pkg/lca/ibu_test.go @@ -0,0 +1,693 @@ +package lca + +import ( + "fmt" + "testing" + + lcav1alpha1 "github.com/openshift-kni/lifecycle-agent/api/v1alpha1" + "github.com/stretchr/testify/assert" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime" + "k8s.io/apimachinery/pkg/runtime/schema" + + "github.com/openshift-kni/eco-goinfra/pkg/clients" +) + +var ( + ibulGvk = schema.GroupVersionKind{ + Group: "lca.openshift.io", + Version: "v1alpha1", + Kind: "ImageBasedUpgrade", + } +) + +func TestImageBasedUpgradeWithOptions(t *testing.T) { + testSettings := buildTestClientWithDummyObject() + testBuilder, _ := PullImageBasedUpgrade(testSettings) + testBuilder = testBuilder.WithOptions( + func(builder *ImageBasedUpgradeBuilder) (*ImageBasedUpgradeBuilder, error) { + return builder, nil + }) + + assert.Equal(t, "", testBuilder.errorMsg) + + testBuilder = testBuilder.WithOptions( + func(builder *ImageBasedUpgradeBuilder) (*ImageBasedUpgradeBuilder, error) { + return builder, fmt.Errorf("error") + }) + assert.Equal(t, "error", testBuilder.errorMsg) +} + +func TestImageBasedUpgradePull(t *testing.T) { + testCases := []struct { + expectedError error + addToRuntimeObjects bool + expectedErrorText string + }{ + { + expectedError: nil, + addToRuntimeObjects: true, + }, + } + + for _, testCase := range testCases { + var ( + runtimeObjects []runtime.Object + testSettings *clients.Settings + ) + + testIBU := generateImageBasedUpgrade() + + if testCase.addToRuntimeObjects { + runtimeObjects = append(runtimeObjects, testIBU) + } + + testSettings = clients.GetTestClients(clients.TestClientParams{ + K8sMockObjects: runtimeObjects, + }) + + // Test the PullImageBasedUpgrade function + builderResult, err := PullImageBasedUpgrade(testSettings) + + // Check the error + assert.Equal(t, err, testCase.expectedError) + + if testCase.expectedError == nil { + assert.NotNil(t, builderResult) + } + } +} + +func TestImageBasedUpgradeUpdate(t *testing.T) { + testCases := []struct { + expectedError error + addToRuntimeObjects bool + expectedErrorText string + }{ + { + expectedError: nil, + addToRuntimeObjects: true, + }, + } + + for _, testCase := range testCases { + var ( + runtimeObjects []runtime.Object + testSettings *clients.Settings + ) + + testIBU := generateImageBasedUpgrade() + + if testCase.addToRuntimeObjects { + runtimeObjects = append(runtimeObjects, testIBU) + } + + testSettings = clients.GetTestClients(clients.TestClientParams{ + K8sMockObjects: runtimeObjects, + }) + + ibuBuilder, err := PullImageBasedUpgrade(testSettings) + assert.Nil(t, err) + + // Test the PullImageBasedUpgrade function + builderResult, err := ibuBuilder.WithSeedImage("quay.io/no-image").Update() + + // Check the error + assert.Equal(t, err, testCase.expectedError) + + if testCase.expectedError == nil { + assert.NotNil(t, builderResult) + } + } +} + +func TestImageBasedUpgradeDelete(t *testing.T) { + testCases := []struct { + expectedError error + addToRuntimeObjects bool + expectedErrorText string + }{ + { + expectedError: nil, + addToRuntimeObjects: true, + }, + } + + for _, testCase := range testCases { + var ( + runtimeObjects []runtime.Object + testSettings *clients.Settings + ) + + testIBU := generateImageBasedUpgrade() + + if testCase.addToRuntimeObjects { + runtimeObjects = append(runtimeObjects, testIBU) + } + + testSettings = clients.GetTestClients(clients.TestClientParams{ + K8sMockObjects: runtimeObjects, + }) + + ibuBuilder, err := PullImageBasedUpgrade(testSettings) + assert.Nil(t, err) + + // Test the Delete function + builderResult, err := ibuBuilder.Delete() + + // Check the error + assert.Equal(t, err, testCase.expectedError) + + if testCase.expectedError == nil { + assert.Nil(t, builderResult.Object) + } + } +} + +func TestImageBasedUpgradeGet(t *testing.T) { + testCases := []struct { + expectedError error + addToRuntimeObjects bool + expectedErrorText string + }{ + { + expectedError: nil, + addToRuntimeObjects: true, + }, + } + + for _, testCase := range testCases { + var ( + runtimeObjects []runtime.Object + testSettings *clients.Settings + ) + + testIBU := generateImageBasedUpgrade() + + if testCase.addToRuntimeObjects { + runtimeObjects = append(runtimeObjects, testIBU) + } + + testSettings = clients.GetTestClients(clients.TestClientParams{ + K8sMockObjects: runtimeObjects, + }) + + ibuBuilder, err := PullImageBasedUpgrade(testSettings) + assert.Nil(t, err) + + // Test the Get function + builderResult, err := ibuBuilder.Get() + + // Check the error + assert.Equal(t, err, testCase.expectedError) + + if testCase.expectedError == nil { + assert.NotNil(t, builderResult) + } + } +} + +func TestImageBasedUpgradeExists(t *testing.T) { + testCases := []struct { + expectedError error + addToRuntimeObjects bool + expectedErrorText string + }{ + { + expectedError: nil, + addToRuntimeObjects: true, + }, + } + + for _, testCase := range testCases { + var ( + runtimeObjects []runtime.Object + testSettings *clients.Settings + ) + + testIBU := generateImageBasedUpgrade() + + if testCase.addToRuntimeObjects { + runtimeObjects = append(runtimeObjects, testIBU) + } + + testSettings = clients.GetTestClients(clients.TestClientParams{ + K8sMockObjects: runtimeObjects, + }) + + ibuBuilder, err := PullImageBasedUpgrade(testSettings) + assert.Nil(t, err) + + // Test the Exists function + builderResult := ibuBuilder.Exists() + + // Check the error + assert.Equal(t, err, testCase.expectedError) + + if testCase.expectedError == nil { + assert.NotNil(t, builderResult) + } + } +} + +func TestImageBasedUpgradeWithSeedImage(t *testing.T) { + testCases := []struct { + expectedError error + addToRuntimeObjects bool + expectedErrorText string + seedImage string + }{ + { + expectedError: nil, + addToRuntimeObjects: true, + seedImage: "quay.io/foo", + }, + { + expectedError: nil, + addToRuntimeObjects: true, + seedImage: "", + }, + } + + for _, testCase := range testCases { + var ( + runtimeObjects []runtime.Object + testSettings *clients.Settings + ) + + testIBU := generateImageBasedUpgrade() + + if testCase.addToRuntimeObjects { + runtimeObjects = append(runtimeObjects, testIBU) + } + + testSettings = clients.GetTestClients(clients.TestClientParams{ + K8sMockObjects: runtimeObjects, + }) + + ibuBuilder, err := PullImageBasedUpgrade(testSettings) + assert.Nil(t, err) + + // Test the WithSeedImage function + builderResult := ibuBuilder.WithSeedImage(testCase.seedImage) + + // Check the error + assert.Equal(t, err, testCase.expectedError) + + if testCase.expectedError == nil { + assert.NotNil(t, builderResult) + } + } +} + +func TestImageBasedUpgradeWithAdditionalImages(t *testing.T) { + testCases := []struct { + expectedError error + addToRuntimeObjects bool + expectedErrorText string + additionalImagesConfigMapName string + additionalImagesConfigMapNamespace string + }{ + { + expectedError: nil, + addToRuntimeObjects: true, + additionalImagesConfigMapName: "cmName", + additionalImagesConfigMapNamespace: "nsName", + }, + { + expectedError: nil, + addToRuntimeObjects: true, + additionalImagesConfigMapName: "", + additionalImagesConfigMapNamespace: "", + }, + } + + for _, testCase := range testCases { + var ( + runtimeObjects []runtime.Object + testSettings *clients.Settings + ) + + testIBU := generateImageBasedUpgrade() + + if testCase.addToRuntimeObjects { + runtimeObjects = append(runtimeObjects, testIBU) + } + + testSettings = clients.GetTestClients(clients.TestClientParams{ + K8sMockObjects: runtimeObjects, + }) + + ibuBuilder, err := PullImageBasedUpgrade(testSettings) + assert.Nil(t, err) + + // Test the WithAdditionalImages function + builderResult := ibuBuilder.WithAdditionalImages( + testCase.additionalImagesConfigMapName, testCase.additionalImagesConfigMapNamespace) + + // Check the error + assert.Equal(t, err, testCase.expectedError) + + if testCase.expectedError == nil { + assert.NotNil(t, builderResult) + } + } +} + +func TestImageBasedUpgradeWithExtraManifests(t *testing.T) { + testCases := []struct { + expectedError error + addToRuntimeObjects bool + expectedErrorText string + extraManifestsConfigMapName string + extraManifestsConfigMapNamespace string + }{ + { + expectedError: nil, + addToRuntimeObjects: true, + extraManifestsConfigMapName: "cmName", + extraManifestsConfigMapNamespace: "nsName", + }, + { + expectedError: nil, + addToRuntimeObjects: true, + extraManifestsConfigMapName: "", + extraManifestsConfigMapNamespace: "", + }, + } + + for _, testCase := range testCases { + var ( + runtimeObjects []runtime.Object + testSettings *clients.Settings + ) + + testIBU := generateImageBasedUpgrade() + + if testCase.addToRuntimeObjects { + runtimeObjects = append(runtimeObjects, testIBU) + } + + testSettings = clients.GetTestClients(clients.TestClientParams{ + K8sMockObjects: runtimeObjects, + }) + + ibuBuilder, err := PullImageBasedUpgrade(testSettings) + assert.Nil(t, err) + + // Test the WithExtraManifests function + builderResult := ibuBuilder.WithExtraManifests( + testCase.extraManifestsConfigMapName, testCase.extraManifestsConfigMapNamespace) + + // Check the error + assert.Equal(t, err, testCase.expectedError) + + if testCase.expectedError == nil { + assert.NotNil(t, builderResult) + } + } +} + +func TestImageBasedUpgradeWithOadpContent(t *testing.T) { + testCases := []struct { + expectedError error + addToRuntimeObjects bool + expectedErrorText string + oadpContentConfigMapName string + oadpContentConfigMapNamespace string + }{ + { + expectedError: nil, + addToRuntimeObjects: true, + oadpContentConfigMapName: "cmName", + oadpContentConfigMapNamespace: "nsName", + }, + { + expectedError: nil, + addToRuntimeObjects: true, + oadpContentConfigMapName: "", + oadpContentConfigMapNamespace: "", + }, + } + + for _, testCase := range testCases { + var ( + runtimeObjects []runtime.Object + testSettings *clients.Settings + ) + + testIBU := generateImageBasedUpgrade() + + if testCase.addToRuntimeObjects { + runtimeObjects = append(runtimeObjects, testIBU) + } + + testSettings = clients.GetTestClients(clients.TestClientParams{ + K8sMockObjects: runtimeObjects, + }) + + ibuBuilder, err := PullImageBasedUpgrade(testSettings) + assert.Nil(t, err) + + // Test the WithExtraManifests function + builderResult := ibuBuilder.WithExtraManifests( + testCase.oadpContentConfigMapName, testCase.oadpContentConfigMapNamespace) + + // Check the error + assert.Equal(t, err, testCase.expectedError) + + if testCase.expectedError == nil { + assert.NotNil(t, builderResult) + } + } +} + +func TestImageBasedUpgradeWithSeedImageVersion(t *testing.T) { + testCases := []struct { + expectedError error + addToRuntimeObjects bool + expectedErrorText string + seedImageVersion string + }{ + { + expectedError: nil, + addToRuntimeObjects: true, + seedImageVersion: "seedversion", + }, + { + expectedError: nil, + addToRuntimeObjects: true, + seedImageVersion: "", + }, + } + + for _, testCase := range testCases { + var ( + runtimeObjects []runtime.Object + testSettings *clients.Settings + ) + + testIBU := generateImageBasedUpgrade() + + if testCase.addToRuntimeObjects { + runtimeObjects = append(runtimeObjects, testIBU) + } + + testSettings = clients.GetTestClients(clients.TestClientParams{ + K8sMockObjects: runtimeObjects, + }) + + ibuBuilder, err := PullImageBasedUpgrade(testSettings) + assert.Nil(t, err) + + // Test the WithExtraManifests function + builderResult := ibuBuilder.WithSeedImageVersion( + testCase.seedImageVersion) + + // Check the error + assert.Equal(t, err, testCase.expectedError) + + if testCase.expectedError == nil { + assert.NotNil(t, builderResult) + } + } +} + +func TestImageBasedUpgradeWithSeedImagePullSecretRef(t *testing.T) { + testCases := []struct { + expectedError error + addToRuntimeObjects bool + expectedErrorText string + pullSecretName string + }{ + { + expectedError: nil, + addToRuntimeObjects: true, + pullSecretName: "pull-secret", + }, + { + expectedError: nil, + addToRuntimeObjects: true, + pullSecretName: "", + }, + } + + for _, testCase := range testCases { + var ( + runtimeObjects []runtime.Object + testSettings *clients.Settings + ) + + testIBU := generateImageBasedUpgrade() + + if testCase.addToRuntimeObjects { + runtimeObjects = append(runtimeObjects, testIBU) + } + + testSettings = clients.GetTestClients(clients.TestClientParams{ + K8sMockObjects: runtimeObjects, + }) + + ibuBuilder, err := PullImageBasedUpgrade(testSettings) + assert.Nil(t, err) + + // Test the WithExtraManifests function + builderResult := ibuBuilder.WithSeedImagePullSecretRef( + testCase.pullSecretName) + + // Check the error + assert.Equal(t, err, testCase.expectedError) + + if testCase.expectedError == nil { + assert.NotNil(t, builderResult) + } + } +} + +func TestImageBasedUpgradeWaitUntilStageComplete(t *testing.T) { + testCases := []struct { + expectedError error + addToRuntimeObjects bool + expectedErrorText string + stage string + }{ + { + expectedError: fmt.Errorf("wrong stage selected for imagebasedupgrade"), + addToRuntimeObjects: true, + stage: "wrong", + }, + { + expectedError: fmt.Errorf("wrong stage selected for imagebasedupgrade"), + addToRuntimeObjects: true, + stage: "", + }, + } + + for _, testCase := range testCases { + var ( + runtimeObjects []runtime.Object + testSettings *clients.Settings + ) + + testIBU := generateImageBasedUpgrade() + + if testCase.addToRuntimeObjects { + runtimeObjects = append(runtimeObjects, testIBU) + } + + testSettings = clients.GetTestClients(clients.TestClientParams{ + K8sMockObjects: runtimeObjects, + }) + + ibuBuilder, err := PullImageBasedUpgrade(testSettings) + assert.Nil(t, err) + + // Test the WithExtraManifests function + builderResult, err := ibuBuilder.WaitUntilStageComplete( + testCase.stage) + + // Check the error + assert.Equal(t, err, testCase.expectedError) + + if testCase.expectedError == nil { + assert.NotNil(t, builderResult) + } + } +} + +func TestImageBasedUpgradeWithStage(t *testing.T) { + testCases := []struct { + expectedError error + addToRuntimeObjects bool + expectedErrorText string + stage string + }{ + { + expectedError: nil, + addToRuntimeObjects: true, + stage: "Idle", + }, + { + expectedError: nil, + addToRuntimeObjects: true, + stage: "Wrong", + }, + } + + for _, testCase := range testCases { + var ( + runtimeObjects []runtime.Object + testSettings *clients.Settings + ) + + testIBU := generateImageBasedUpgrade() + + if testCase.addToRuntimeObjects { + runtimeObjects = append(runtimeObjects, testIBU) + } + + testSettings = clients.GetTestClients(clients.TestClientParams{ + K8sMockObjects: runtimeObjects, + }) + + ibuBuilder, err := PullImageBasedUpgrade(testSettings) + assert.Nil(t, err) + + // Test the WithExtraManifests function + builderResult := ibuBuilder.WithStage( + testCase.stage) + + // Check the error + assert.Equal(t, err, testCase.expectedError) + + if testCase.expectedError == nil { + assert.NotNil(t, builderResult) + } + } +} + +func generateImageBasedUpgrade() *lcav1alpha1.ImageBasedUpgrade { + return &lcav1alpha1.ImageBasedUpgrade{ + ObjectMeta: metav1.ObjectMeta{ + Name: "upgrade", + }, + } +} + +func buildTestClientWithDummyObject() *clients.Settings { + return clients.GetTestClients(clients.TestClientParams{ + K8sMockObjects: buildDummyIBU(), + GVK: []schema.GroupVersionKind{ibulGvk}, + }) +} + +func buildDummyIBU() []runtime.Object { + return append([]runtime.Object{}, &lcav1alpha1.ImageBasedUpgrade{ + ObjectMeta: metav1.ObjectMeta{ + Name: "upgrade", + }, + Spec: lcav1alpha1.ImageBasedUpgradeSpec{ + Stage: "Idle", + }, + }) +} diff --git a/pkg/lca/ica_test.go b/pkg/lca/ica_test.go deleted file mode 100644 index 253e96e96..000000000 --- a/pkg/lca/ica_test.go +++ /dev/null @@ -1 +0,0 @@ -package lca