Skip to content

Commit

Permalink
assert that last unpacked > progressing last transitioned
Browse files Browse the repository at this point in the history
Signed-off-by: Per Goncalves da Silva <pegoncal@redhat.com>
  • Loading branch information
Per Goncalves da Silva committed Jan 15, 2025
1 parent 171af61 commit 5d621fd
Showing 1 changed file with 74 additions and 61 deletions.
135 changes: 74 additions & 61 deletions test/upgrade-e2e/post_upgrade_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package upgradee2e

import (
"bufio"
"context"
"fmt"
"io"
Expand Down Expand Up @@ -37,52 +36,40 @@ func TestClusterExtensionAfterOLMUpgrade(t *testing.T) {
ctx := context.Background()
defer getArtifactsOutput(t)

managerLabelSelector := labels.Set{"control-plane": "operator-controller-controller-manager"}
now := time.Now()

t.Log("Checking that the controller-manager deployment is updated")
require.EventuallyWithT(t, func(ct *assert.CollectT) {
var managerDeployments appsv1.DeploymentList
assert.NoError(ct, c.List(ctx, &managerDeployments, client.MatchingLabelsSelector{Selector: managerLabelSelector.AsSelector()}))
assert.Len(ct, managerDeployments.Items, 1)
managerDeployment := managerDeployments.Items[0]
// wait for catalogd deployment to finish
t.Log("Wait for catalogd deployment to be ready")
waitForDeployment(t, ctx, "catalogd-controller-manager")

assert.True(ct,
managerDeployment.Status.UpdatedReplicas == *managerDeployment.Spec.Replicas &&
managerDeployment.Status.Replicas == *managerDeployment.Spec.Replicas &&
managerDeployment.Status.AvailableReplicas == *managerDeployment.Spec.Replicas &&
managerDeployment.Status.ReadyReplicas == *managerDeployment.Spec.Replicas,
)
}, time.Minute, time.Second)
// wait for operator-controller deployment to finish
t.Log("Wait for operator-controller deployment to be ready")
waitForDeployment(t, ctx, "operator-controller-controller-manager")

var managerPods corev1.PodList
t.Log("Waiting for only one controller-manager Pod to remain")
require.EventuallyWithT(t, func(ct *assert.CollectT) {
assert.NoError(ct, c.List(ctx, &managerPods, client.MatchingLabelsSelector{Selector: managerLabelSelector.AsSelector()}))
assert.Len(ct, managerPods.Items, 1)
}, time.Minute, time.Second)

t.Log("Reading logs to make sure that ClusterExtension was reconciled by operator-controller before we update it")
// Make sure that after we upgrade OLM itself we can still reconcile old objects without any changes
logCtx, cancel := context.WithTimeout(ctx, time.Minute)
defer cancel()
substrings := []string{
"reconcile ending",
fmt.Sprintf(`ClusterExtension=%q`, testClusterExtensionName),
}
found, err := watchPodLogsForSubstring(logCtx, &managerPods.Items[0], "manager", substrings...)
require.NoError(t, err)
require.True(t, found)

t.Log("Checking that the ClusterCatalog is serving")
t.Log("Checking that the ClusterCatalog is unpacked")
require.EventuallyWithT(t, func(ct *assert.CollectT) {
var clusterCatalog catalogd.ClusterCatalog
assert.NoError(ct, c.Get(ctx, types.NamespacedName{Name: testClusterCatalogName}, &clusterCatalog))

// check serving condition
cond := apimeta.FindStatusCondition(clusterCatalog.Status.Conditions, catalogd.TypeServing)
if !assert.NotNil(ct, cond) {
return
}
assert.Equal(ct, metav1.ConditionTrue, cond.Status)
assert.Equal(ct, catalogd.ReasonAvailable, cond.Reason)

// check progressing condition
cond = apimeta.FindStatusCondition(clusterCatalog.Status.Conditions, catalogd.TypeProgressing)
if !assert.NotNil(ct, cond) {
return
}
assert.Equal(ct, metav1.ConditionTrue, cond.Status)
assert.Equal(ct, catalogd.ReasonSucceeded, cond.Reason)

// check that the catalog was recently unpacked (after progressing is over)
t.Logf("last unpacked: %s - progressing last transitioned: %s", clusterCatalog.Status.LastUnpacked.String(), cond.LastTransitionTime.String())
assert.True(ct, clusterCatalog.Status.LastUnpacked.After(now))
}, time.Minute, time.Second)

t.Log("Checking that the ClusterExtension is installed")
Expand Down Expand Up @@ -122,37 +109,63 @@ func TestClusterExtensionAfterOLMUpgrade(t *testing.T) {
}, time.Minute, time.Second)
}

func watchPodLogsForSubstring(ctx context.Context, pod *corev1.Pod, container string, substrings ...string) (bool, error) {
podLogOpts := corev1.PodLogOptions{
Follow: true,
Container: container,
}

req := kclientset.CoreV1().Pods(pod.Namespace).GetLogs(pod.Name, &podLogOpts)
podLogs, err := req.Stream(ctx)
if err != nil {
return false, err
}
defer podLogs.Close()
func waitForDeployment(t *testing.T, ctx context.Context, controlPlaneLabel string) {
deploymentLabelSelector := labels.Set{"control-plane": controlPlaneLabel}.AsSelector()

scanner := bufio.NewScanner(podLogs)
for scanner.Scan() {
line := scanner.Text()
t.Log("Checking that the deployment is updated")
require.EventuallyWithT(t, func(ct *assert.CollectT) {
var managerDeployments appsv1.DeploymentList
assert.NoError(ct, c.List(ctx, &managerDeployments, client.MatchingLabelsSelector{Selector: deploymentLabelSelector}))
assert.Len(ct, managerDeployments.Items, 1)
managerDeployment := managerDeployments.Items[0]

foundCount := 0
for _, substring := range substrings {
if strings.Contains(line, substring) {
foundCount++
}
}
if foundCount == len(substrings) {
return true, nil
}
}
assert.True(ct,
managerDeployment.Status.UpdatedReplicas == *managerDeployment.Spec.Replicas &&
managerDeployment.Status.Replicas == *managerDeployment.Spec.Replicas &&
managerDeployment.Status.AvailableReplicas == *managerDeployment.Spec.Replicas &&
managerDeployment.Status.ReadyReplicas == *managerDeployment.Spec.Replicas,
)
}, time.Minute, time.Second)

return false, scanner.Err()
var managerPods corev1.PodList
t.Log("Waiting for only one Pod to remain")
require.EventuallyWithT(t, func(ct *assert.CollectT) {
assert.NoError(ct, c.List(ctx, &managerPods, client.MatchingLabelsSelector{Selector: deploymentLabelSelector}))
assert.Len(ct, managerPods.Items, 1)
}, time.Minute, time.Second)
}

//func watchPodLogsForSubstring(ctx context.Context, pod *corev1.Pod, container string, substrings ...string) (bool, error) {
// podLogOpts := corev1.PodLogOptions{
// Follow: true,
// Container: container,
// }
//
// req := kclientset.CoreV1().Pods(pod.Namespace).GetLogs(pod.Name, &podLogOpts)
// podLogs, err := req.Stream(ctx)
// if err != nil {
// return false, err
// }
// defer podLogs.Close()
//
// scanner := bufio.NewScanner(podLogs)
// for scanner.Scan() {
// line := scanner.Text()
//
// foundCount := 0
// for _, substring := range substrings {
// if strings.Contains(line, substring) {
// foundCount++
// }
// }
// if foundCount == len(substrings) {
// return true, nil
// }
// }
//
// return false, scanner.Err()
//}

// getArtifactsOutput gets all the artifacts from the test run and saves them to the artifact path.
// Currently it saves:
// - clusterextensions
Expand Down

0 comments on commit 5d621fd

Please sign in to comment.