|
| 1 | +package upgradee2e |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "context" |
| 6 | + "fmt" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | + appsv1 "k8s.io/api/apps/v1" |
| 14 | + corev1 "k8s.io/api/core/v1" |
| 15 | + apimeta "k8s.io/apimachinery/pkg/api/meta" |
| 16 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 17 | + "k8s.io/apimachinery/pkg/labels" |
| 18 | + "k8s.io/apimachinery/pkg/types" |
| 19 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 20 | + |
| 21 | + catalogdv1alpha1 "github.com/operator-framework/catalogd/api/core/v1alpha1" |
| 22 | + |
| 23 | + ocv1alpha1 "github.com/operator-framework/operator-controller/api/v1alpha1" |
| 24 | +) |
| 25 | + |
| 26 | +func TestClusterExtensionAfterOLMUpgrade(t *testing.T) { |
| 27 | + t.Log("Starting checks after OLM upgrade") |
| 28 | + ctx := context.Background() |
| 29 | + |
| 30 | + managerLabelSelector := labels.Set{"control-plane": "controller-manager"} |
| 31 | + |
| 32 | + t.Log("Checking that the controller-manager deployment is updated") |
| 33 | + require.EventuallyWithT(t, func(ct *assert.CollectT) { |
| 34 | + var managerDeployments appsv1.DeploymentList |
| 35 | + assert.NoError(ct, c.List(ctx, &managerDeployments, client.MatchingLabelsSelector{Selector: managerLabelSelector.AsSelector()})) |
| 36 | + assert.Len(ct, managerDeployments.Items, 1) |
| 37 | + managerDeployment := managerDeployments.Items[0] |
| 38 | + |
| 39 | + assert.True(ct, |
| 40 | + managerDeployment.Status.UpdatedReplicas == *managerDeployment.Spec.Replicas && |
| 41 | + managerDeployment.Status.Replicas == *managerDeployment.Spec.Replicas && |
| 42 | + managerDeployment.Status.AvailableReplicas == *managerDeployment.Spec.Replicas && |
| 43 | + managerDeployment.Status.ReadyReplicas == *managerDeployment.Spec.Replicas, |
| 44 | + ) |
| 45 | + }, time.Minute, time.Second) |
| 46 | + |
| 47 | + var managerPods corev1.PodList |
| 48 | + t.Log("Waiting for only one controller-manager Pod to remain") |
| 49 | + require.EventuallyWithT(t, func(ct *assert.CollectT) { |
| 50 | + assert.NoError(ct, c.List(ctx, &managerPods, client.MatchingLabelsSelector{Selector: managerLabelSelector.AsSelector()})) |
| 51 | + assert.Len(ct, managerPods.Items, 1) |
| 52 | + }, time.Minute, time.Second) |
| 53 | + |
| 54 | + t.Log("Reading logs to make sure that ClusterExtension was reconciled by operator-controller before we update it") |
| 55 | + // Make sure that after we upgrade OLM itself we can still reconcile old objects without any changes |
| 56 | + logCtx, cancel := context.WithTimeout(ctx, time.Minute) |
| 57 | + defer cancel() |
| 58 | + substring := fmt.Sprintf(`"ClusterExtension": {"name":"%s"}`, testClusterExtensionName) |
| 59 | + found, err := watchPodLogsForSubstring(logCtx, &managerPods.Items[0], "manager", substring) |
| 60 | + require.NoError(t, err) |
| 61 | + require.True(t, found) |
| 62 | + |
| 63 | + t.Log("Checking that the ClusterCatalog is unpacked") |
| 64 | + require.EventuallyWithT(t, func(ct *assert.CollectT) { |
| 65 | + var clusterCatalog catalogdv1alpha1.ClusterCatalog |
| 66 | + assert.NoError(ct, c.Get(ctx, types.NamespacedName{Name: testClusterCatalogName}, &clusterCatalog)) |
| 67 | + cond := apimeta.FindStatusCondition(clusterCatalog.Status.Conditions, catalogdv1alpha1.TypeUnpacked) |
| 68 | + if !assert.NotNil(ct, cond) { |
| 69 | + return |
| 70 | + } |
| 71 | + assert.Equal(ct, metav1.ConditionTrue, cond.Status) |
| 72 | + assert.Equal(ct, catalogdv1alpha1.ReasonUnpackSuccessful, cond.Reason) |
| 73 | + }, time.Minute, time.Second) |
| 74 | + |
| 75 | + t.Log("Checking that the ClusterExtension is installed") |
| 76 | + var clusterExtension ocv1alpha1.ClusterExtension |
| 77 | + require.EventuallyWithT(t, func(ct *assert.CollectT) { |
| 78 | + assert.NoError(ct, c.Get(ctx, types.NamespacedName{Name: testClusterExtensionName}, &clusterExtension)) |
| 79 | + cond := apimeta.FindStatusCondition(clusterExtension.Status.Conditions, ocv1alpha1.TypeInstalled) |
| 80 | + if !assert.NotNil(ct, cond) { |
| 81 | + return |
| 82 | + } |
| 83 | + assert.Equal(ct, metav1.ConditionTrue, cond.Status) |
| 84 | + assert.Equal(ct, ocv1alpha1.ReasonSuccess, cond.Reason) |
| 85 | + assert.Contains(ct, cond.Message, "Instantiated bundle") |
| 86 | + assert.NotEmpty(ct, clusterExtension.Status.InstalledBundle) |
| 87 | + assert.NotEmpty(ct, clusterExtension.Status.InstalledBundle.Version) |
| 88 | + }, time.Minute, time.Second) |
| 89 | + |
| 90 | + previousVersion := clusterExtension.Status.InstalledBundle.Version |
| 91 | + |
| 92 | + t.Log("Updating the ClusterExtension to change version") |
| 93 | + // Make sure that after we upgrade OLM itself we can still reconcile old objects if we change them |
| 94 | + clusterExtension.Spec.Version = "1.0.1" |
| 95 | + require.NoError(t, c.Update(ctx, &clusterExtension)) |
| 96 | + |
| 97 | + t.Log("Checking that the ClusterExtension installs successfully") |
| 98 | + require.EventuallyWithT(t, func(ct *assert.CollectT) { |
| 99 | + assert.NoError(ct, c.Get(ctx, types.NamespacedName{Name: testClusterExtensionName}, &clusterExtension)) |
| 100 | + cond := apimeta.FindStatusCondition(clusterExtension.Status.Conditions, ocv1alpha1.TypeInstalled) |
| 101 | + if !assert.NotNil(ct, cond) { |
| 102 | + return |
| 103 | + } |
| 104 | + assert.Equal(ct, ocv1alpha1.ReasonSuccess, cond.Reason) |
| 105 | + assert.Contains(ct, cond.Message, "Instantiated bundle") |
| 106 | + assert.Equal(ct, &ocv1alpha1.BundleMetadata{Name: "prometheus-operator.1.0.1", Version: "1.0.1"}, clusterExtension.Status.ResolvedBundle) |
| 107 | + assert.Equal(ct, &ocv1alpha1.BundleMetadata{Name: "prometheus-operator.1.0.1", Version: "1.0.1"}, clusterExtension.Status.InstalledBundle) |
| 108 | + assert.NotEqual(ct, previousVersion, clusterExtension.Status.InstalledBundle.Version) |
| 109 | + }, time.Minute, time.Second) |
| 110 | +} |
| 111 | + |
| 112 | +func watchPodLogsForSubstring(ctx context.Context, pod *corev1.Pod, container, substring string) (bool, error) { |
| 113 | + podLogOpts := corev1.PodLogOptions{ |
| 114 | + Follow: true, |
| 115 | + Container: container, |
| 116 | + } |
| 117 | + |
| 118 | + req := kclientset.CoreV1().Pods(pod.Namespace).GetLogs(pod.Name, &podLogOpts) |
| 119 | + podLogs, err := req.Stream(ctx) |
| 120 | + if err != nil { |
| 121 | + return false, err |
| 122 | + } |
| 123 | + defer podLogs.Close() |
| 124 | + |
| 125 | + scanner := bufio.NewScanner(podLogs) |
| 126 | + for scanner.Scan() { |
| 127 | + line := scanner.Text() |
| 128 | + |
| 129 | + if strings.Contains(line, substring) { |
| 130 | + return true, nil |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + return false, scanner.Err() |
| 135 | +} |
0 commit comments