|
| 1 | +package rh_operators |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/operators/v1alpha1" |
| 9 | + "github.com/operator-framework/operator-lifecycle-manager/pkg/api/client" |
| 10 | + "github.com/operator-framework/operator-lifecycle-manager/pkg/api/client/clientset/versioned" |
| 11 | + "github.com/operator-framework/operator-lifecycle-manager/pkg/lib/operatorclient" |
| 12 | + pclient "github.com/operator-framework/operator-lifecycle-manager/pkg/package-server/client" |
| 13 | + psVersioned "github.com/operator-framework/operator-lifecycle-manager/pkg/package-server/client/clientset/versioned" |
| 14 | + |
| 15 | + "github.com/sirupsen/logrus" |
| 16 | + "github.com/stretchr/testify/assert" |
| 17 | + "github.com/stretchr/testify/require" |
| 18 | +) |
| 19 | + |
| 20 | +var ( |
| 21 | + kubeconfig = os.Getenv("KUBECONFIG") |
| 22 | + testlog = logrus.New() |
| 23 | +) |
| 24 | + |
| 25 | +// TestRHOperators installs all Red Hat Operators available on the cluster and see that they install successfully. |
| 26 | +func TestRHOperators(t *testing.T) { |
| 27 | + c := operatorclient.NewClientFromConfig(kubeconfig, testlog) |
| 28 | + |
| 29 | + crc, err := pclient.NewClient(kubeconfig) |
| 30 | + require.NoError(t, err, "error creating package-server client") |
| 31 | + |
| 32 | + vc, err := client.NewClient(kubeconfig) |
| 33 | + require.NoError(t, err, "error creating cr client") |
| 34 | + |
| 35 | + // Serial Test operators with installModes other than typeOwnNamespace to avoid OperatorGroup intersection. |
| 36 | + operatorList, err := ListRHOperatorsWithoutInstallModes(crc, v1alpha1.InstallModeTypeOwnNamespace) |
| 37 | + require.NoError(t, err, "failed to list Red Hat Operators") |
| 38 | + |
| 39 | + for _, operator := range operatorList { |
| 40 | + operatorName := operator |
| 41 | + t.Run(fmt.Sprintf("Testing for operator %s", operator), func(t *testing.T) { |
| 42 | + testInstallOperators(t, c, crc, vc, operatorName) |
| 43 | + }) |
| 44 | + } |
| 45 | + |
| 46 | + CleanupAll(t, c, vc) |
| 47 | + |
| 48 | + operatorList, err = ListRHOperatorsByInstallModes(crc, v1alpha1.InstallModeTypeOwnNamespace) |
| 49 | + require.NoError(t, err, "failed to list Red Hat Operators with OwnNamespace InstallMode") |
| 50 | + |
| 51 | + for _, operator := range operatorList { |
| 52 | + operatorName := operator |
| 53 | + t.Run(fmt.Sprintf("Testing for operator %s", operator), func(t *testing.T) { |
| 54 | + t.Parallel() |
| 55 | + testInstallOperators(t, c, crc, vc, operatorName) |
| 56 | + }) |
| 57 | + } |
| 58 | + |
| 59 | + CleanupAll(t, c, vc) |
| 60 | +} |
| 61 | + |
| 62 | +// testInstallOperators installs operators and wait till its CSV is in succeeded phase then deletes created resources |
| 63 | +// and wait till the created Namespace is terminated. |
| 64 | +func testInstallOperators(t *testing.T, c operatorclient.ClientInterface, crc psVersioned.Interface, |
| 65 | + vc versioned.Interface, operatorName string) { |
| 66 | + t.Logf("Installing %s operator", operatorName) |
| 67 | + |
| 68 | + o, err := NewOperator(c, crc, vc, operatorName) |
| 69 | + if err != nil { |
| 70 | + assert.Failf(t, err.Error(), "failed to load operator %s from packagemanifests", operatorName) |
| 71 | + return |
| 72 | + } |
| 73 | + |
| 74 | + err = o.Subscribe() |
| 75 | + assert.NoError(t, err, "error creating subscription for operator %s", operatorName) |
| 76 | + |
| 77 | + err = o.Unsubscribe(false) |
| 78 | + assert.NoError(t, err, "error cleaning up subscription for operator %s", operatorName) |
| 79 | + if err != nil { |
| 80 | + CleanupOperatorCSVs(t, o) |
| 81 | + CleanupOperatorNamespace(t, o) |
| 82 | + } |
| 83 | + |
| 84 | + err = o.WaitToDeleteNamespace(o.namespace) |
| 85 | + if err != nil { |
| 86 | + t.Logf("error cleaning up Namespace: %s, %v", o.namespace, err) |
| 87 | + } |
| 88 | +} |
0 commit comments