|
| 1 | +// Copyright 2026 Redpanda Data, Inc. |
| 2 | +// |
| 3 | +// Use of this software is governed by the Business Source License |
| 4 | +// included in the file licenses/BSL.md |
| 5 | +// |
| 6 | +// As of the Change Date specified in that file, in accordance with |
| 7 | +// the Business Source License, use of this software will be governed |
| 8 | +// by the Apache License, Version 2.0 |
| 9 | + |
| 10 | +package steps |
| 11 | + |
| 12 | +import ( |
| 13 | + "context" |
| 14 | + "fmt" |
| 15 | + "slices" |
| 16 | + "strings" |
| 17 | + "time" |
| 18 | + |
| 19 | + "github.com/cucumber/godog" |
| 20 | + "github.com/stretchr/testify/require" |
| 21 | + corev1 "k8s.io/api/core/v1" |
| 22 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 23 | + |
| 24 | + framework "github.com/redpanda-data/redpanda-operator/harpoon" |
| 25 | + redpandav1alpha2 "github.com/redpanda-data/redpanda-operator/operator/api/redpanda/v1alpha2" |
| 26 | +) |
| 27 | + |
| 28 | +func checkClusterHasSyncError(ctx context.Context, t framework.TestingT, clusterName string, errdoc *godog.DocString) { |
| 29 | + errorString := strings.TrimSpace(errdoc.Content) |
| 30 | + |
| 31 | + var cluster redpandav1alpha2.Redpanda |
| 32 | + |
| 33 | + key := t.ResourceKey(clusterName) |
| 34 | + |
| 35 | + t.Logf("Checking cluster %q has sync error", clusterName) |
| 36 | + require.Eventually(t, func() bool { |
| 37 | + require.NoError(t, t.Get(ctx, key, &cluster)) |
| 38 | + hasCondition := t.HasCondition(metav1.Condition{ |
| 39 | + Type: "ResourcesSynced", |
| 40 | + Status: metav1.ConditionFalse, |
| 41 | + Reason: "Error", |
| 42 | + }, cluster.Status.Conditions) |
| 43 | + |
| 44 | + t.Logf(`Checking cluster resource conditions contains an errored "ResourcesSynced"? %v`, hasCondition) |
| 45 | + if !hasCondition { |
| 46 | + return false |
| 47 | + } |
| 48 | + |
| 49 | + for _, condition := range cluster.Status.Conditions { |
| 50 | + if condition.Type == "ResourcesSynced" && condition.Status == metav1.ConditionFalse && condition.Reason == "Error" { |
| 51 | + t.Logf("Found error message: %q", condition.Message) |
| 52 | + return strings.Contains(condition.Message, errorString) |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + return false |
| 57 | + }, 5*time.Minute, 5*time.Second, "%s", delayLog(func() string { |
| 58 | + return fmt.Sprintf(`Cluster %q never contained an error on the condition reason "ResourcesSynced" with a matching error string: %q, final Conditions: %+v`, key.String(), errorString, cluster.Status.Conditions) |
| 59 | + })) |
| 60 | +} |
| 61 | + |
| 62 | +func checkResourceNoFieldManagers(ctx context.Context, t framework.TestingT, clusterName string, list *godog.DocString) { |
| 63 | + fieldManagerCheck(ctx, t, false, clusterName, list.Content) |
| 64 | +} |
| 65 | + |
| 66 | +func checkResourceFieldManagers(ctx context.Context, t framework.TestingT, clusterName string, list *godog.DocString) { |
| 67 | + fieldManagerCheck(ctx, t, true, clusterName, list.Content) |
| 68 | +} |
| 69 | + |
| 70 | +func fieldManagerCheck(ctx context.Context, t framework.TestingT, presence bool, clusterName, managerList string) { |
| 71 | + managers := []string{} |
| 72 | + for _, line := range strings.Split(strings.TrimSpace(managerList), "\n") { |
| 73 | + if manager := strings.TrimSpace(line); manager != "" { |
| 74 | + managers = append(managers, manager) |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + var cluster corev1.Service |
| 79 | + |
| 80 | + key := t.ResourceKey(clusterName) |
| 81 | + |
| 82 | + t.Logf("Checking resource %q field manager", clusterName) |
| 83 | + require.Eventually(t, func() bool { |
| 84 | + require.NoError(t, t.Get(ctx, key, &cluster)) |
| 85 | + |
| 86 | + fieldManagers := cluster.GetManagedFields() |
| 87 | + for _, manager := range managers { |
| 88 | + if !slices.ContainsFunc(fieldManagers, func(entry metav1.ManagedFieldsEntry) bool { |
| 89 | + return entry.Manager == manager |
| 90 | + }) { |
| 91 | + t.Logf(`Resource %q does not contain the field manager %q`, key.String(), manager) |
| 92 | + // negation because if we are checking for presence, then we want to return that the condition |
| 93 | + // is not met |
| 94 | + return !presence |
| 95 | + } |
| 96 | + t.Logf(`Found field manager %q in resource %q`, manager, key.String()) |
| 97 | + } |
| 98 | + return presence |
| 99 | + }, 5*time.Minute, 5*time.Second, "%s", delayLog(func() string { |
| 100 | + finalManagers := []string{} |
| 101 | + for _, entry := range cluster.GetManagedFields() { |
| 102 | + finalManagers = append(finalManagers, entry.Manager) |
| 103 | + } |
| 104 | + if presence { |
| 105 | + return fmt.Sprintf(`Resource %q never contained all of the field managers: %+v, was: %+v`, key.String(), managers, finalManagers) |
| 106 | + } |
| 107 | + return fmt.Sprintf(`Resource %q never lacked all of the field managers: %+v, was: %+v`, key.String(), managers, finalManagers) |
| 108 | + })) |
| 109 | +} |
0 commit comments