|
| 1 | +package e2e |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/operator-framework/operator-marketplace/pkg/apis" |
| 10 | + operator "github.com/operator-framework/operator-marketplace/pkg/apis/marketplace/v1alpha1" |
| 11 | + |
| 12 | + olm "github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/operators/v1alpha1" |
| 13 | + |
| 14 | + "github.com/operator-framework/operator-sdk/pkg/test" |
| 15 | + |
| 16 | + corev1 "k8s.io/api/core/v1" |
| 17 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 18 | +) |
| 19 | + |
| 20 | +const ( |
| 21 | + cleanupRetryInterval = time.Second * 1 |
| 22 | + cleanupTimeout = time.Second * 5 |
| 23 | +) |
| 24 | + |
| 25 | +// Test marketplace is the root function that triggers the set of e2e tests |
| 26 | +func TestMarketplace(t *testing.T) { |
| 27 | + // Add marketplace types to test framework scheme |
| 28 | + operatorsource := &operator.OperatorSource{ |
| 29 | + TypeMeta: metav1.TypeMeta{ |
| 30 | + Kind: operator.OperatorSourceKind, |
| 31 | + APIVersion: fmt.Sprintf("%s/%s", |
| 32 | + operator.SchemeGroupVersion.Group, operator.SchemeGroupVersion.Version), |
| 33 | + }, |
| 34 | + } |
| 35 | + catalogsourceconfig := &operator.CatalogSourceConfig{ |
| 36 | + TypeMeta: metav1.TypeMeta{ |
| 37 | + Kind: operator.CatalogSourceConfigKind, |
| 38 | + APIVersion: fmt.Sprintf("%s/%s", |
| 39 | + operator.SchemeGroupVersion.Group, operator.SchemeGroupVersion.Version), |
| 40 | + }, |
| 41 | + } |
| 42 | + err := test.AddToFrameworkScheme(apis.AddToScheme, operatorsource) |
| 43 | + if err != nil { |
| 44 | + t.Fatalf("failed to add operatorsource custom resource scheme to framework: %v", err) |
| 45 | + } |
| 46 | + err = test.AddToFrameworkScheme(apis.AddToScheme, catalogsourceconfig) |
| 47 | + if err != nil { |
| 48 | + t.Fatalf("failed to add catalogsourceconfig custom resource scheme to framework: %v", err) |
| 49 | + } |
| 50 | + // Add (olm) catalog sources to framework scheme |
| 51 | + catalogsource := &olm.CatalogSource{ |
| 52 | + TypeMeta: metav1.TypeMeta{ |
| 53 | + Kind: olm.CatalogSourceKind, |
| 54 | + APIVersion: olm.CatalogSourceCRDAPIVersion, |
| 55 | + }, |
| 56 | + } |
| 57 | + err = test.AddToFrameworkScheme(olm.AddToScheme, catalogsource) |
| 58 | + if err != nil { |
| 59 | + t.Fatalf("failed to add catalogsource custom resource scheme to framework: %v", err) |
| 60 | + } |
| 61 | + // run subtests |
| 62 | + t.Run("marketplace-group", func(t *testing.T) { |
| 63 | + t.Run("Cluster", MarketplaceCluster) |
| 64 | + }) |
| 65 | +} |
| 66 | + |
| 67 | +// This method initializes the environment and triggers the test |
| 68 | +func MarketplaceCluster(t *testing.T) { |
| 69 | + ctx := test.NewTestCtx(t) |
| 70 | + defer ctx.Cleanup() |
| 71 | + err := ctx.InitializeClusterResources(&test.CleanupOptions{TestContext: ctx, Timeout: cleanupTimeout, RetryInterval: cleanupRetryInterval}) |
| 72 | + if err != nil { |
| 73 | + t.Fatalf("failed to initialize cluster resources: %v", err) |
| 74 | + } |
| 75 | + t.Log("Initialized cluster resources") |
| 76 | + // get global framework variables |
| 77 | + f := test.Global |
| 78 | + |
| 79 | + if err = defaultCreateTest(t, f, ctx); err != nil { |
| 80 | + t.Fatal(err) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +// This function runs a basic happy case end to end workflow for marketplace |
| 85 | +// First create an operatorsource which points to external app registry on quay |
| 86 | +// Check that the catalogsourceconfig was created |
| 87 | +// Then check the configmap and catalogsource were created from the catalogsourceconfig |
| 88 | +func defaultCreateTest(t *testing.T, f *test.Framework, ctx *test.TestCtx) error { |
| 89 | + namespace, err := ctx.GetNamespace() |
| 90 | + if err != nil { |
| 91 | + return fmt.Errorf("could not get namespace: %v", err) |
| 92 | + } |
| 93 | + |
| 94 | + testOperatorSource := &operator.OperatorSource{ |
| 95 | + TypeMeta: metav1.TypeMeta{ |
| 96 | + Kind: operator.OperatorSourceKind, |
| 97 | + }, |
| 98 | + ObjectMeta: metav1.ObjectMeta{ |
| 99 | + Name: "global-operators", |
| 100 | + Namespace: namespace, |
| 101 | + }, |
| 102 | + Spec: operator.OperatorSourceSpec{ |
| 103 | + Type: "appregistry", |
| 104 | + Endpoint: "https://quay.io/cnr", |
| 105 | + RegistryNamespace: "marketplace_e2e", |
| 106 | + }, |
| 107 | + } |
| 108 | + |
| 109 | + catalogSourceConfigName := "opsrc-global-operators" |
| 110 | + configMapName := "csc-cm-opsrc-global-operators" |
| 111 | + catalogSourceName := "csc-cs-opsrc-global-operators" |
| 112 | + |
| 113 | + // Create the operatorsource to download the manifests. |
| 114 | + err = f.Client.Create( |
| 115 | + context.TODO(), |
| 116 | + testOperatorSource, |
| 117 | + &test.CleanupOptions{ |
| 118 | + TestContext: ctx, |
| 119 | + Timeout: cleanupTimeout, |
| 120 | + RetryInterval: cleanupRetryInterval, |
| 121 | + }) |
| 122 | + if err != nil { |
| 123 | + return err |
| 124 | + } |
| 125 | + |
| 126 | + // Check that we created the catalogsourceconfig. |
| 127 | + resultCatalogSourceConfig := &operator.CatalogSourceConfig{} |
| 128 | + err = WaitForResult(t, f, resultCatalogSourceConfig, namespace, catalogSourceConfigName) |
| 129 | + if err != nil { |
| 130 | + return err |
| 131 | + } |
| 132 | + |
| 133 | + // Check for the config map created from the catalogsourceconfig. |
| 134 | + resultConfigMap := &corev1.ConfigMap{} |
| 135 | + err = WaitForResult(t, f, resultConfigMap, namespace, configMapName) |
| 136 | + if err != nil { |
| 137 | + return err |
| 138 | + } |
| 139 | + |
| 140 | + // Then check for the catalog source. |
| 141 | + resultCatalogSource := &olm.CatalogSource{} |
| 142 | + err = WaitForResult(t, f, resultCatalogSource, namespace, catalogSourceName) |
| 143 | + if err != nil { |
| 144 | + return err |
| 145 | + } |
| 146 | + |
| 147 | + // Assert that the catalogsource spec properly references the configmap. |
| 148 | + if resultCatalogSource.Spec.ConfigMap != resultConfigMap.Name { |
| 149 | + t.Errorf( |
| 150 | + "The created catalogsource %s was not properly associated with the created configmap %s", |
| 151 | + resultCatalogSource.Name, |
| 152 | + resultConfigMap.Name, |
| 153 | + ) |
| 154 | + } |
| 155 | + |
| 156 | + return nil |
| 157 | +} |
0 commit comments