|
15 | 15 | package upgrade |
16 | 16 |
|
17 | 17 | import ( |
| 18 | + "context" |
| 19 | + "path/filepath" |
| 20 | + "slices" |
| 21 | + "strings" |
18 | 22 | "testing" |
| 23 | + "time" |
| 24 | + |
| 25 | + "github.com/crossplane-contrib/xp-testing/pkg/resources" |
| 26 | + "k8s.io/klog/v2" |
| 27 | + "sigs.k8s.io/e2e-framework/klient/wait" |
| 28 | + "sigs.k8s.io/e2e-framework/pkg/envconf" |
19 | 29 | ) |
20 | 30 |
|
21 | 31 | func TestUpgradeProvider(t *testing.T) { |
22 | 32 | fromTag, toTag := loadTags() |
23 | 33 |
|
| 34 | + serviceInstanceDir := filepath.Join(resourceDirectoryRoot, "serviceInstance") |
| 35 | + serviceCredentialBindingDir := filepath.Join(resourceDirectoryRoot, "serviceCredentialBinding") |
| 36 | + dependentDirs := []string{serviceCredentialBindingDir} |
| 37 | + |
| 38 | + requiredDirs := removeFromDirs(resourceDirectories, dependentDirs) |
| 39 | + |
24 | 40 | upgradeTest := NewCustomUpgradeTest("baseline-upgrade-test"). |
25 | 41 | FromVersion(fromTag). |
26 | 42 | ToVersion(toTag). |
27 | | - WithResourceDirectories(resourceDirectories) |
28 | | - |
| 43 | + WithResourceDirectories(resourceDirectories). |
| 44 | + SkipDefaultResourceVerification(). |
| 45 | + WithCustomPreUpgradeAssessment( |
| 46 | + "Check all required resources are healthy before upgrade", |
| 47 | + func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context { |
| 48 | + return verifyResources(ctx, t, cfg, requiredDirs, verifyTimeout) |
| 49 | + }, |
| 50 | + ). |
| 51 | + WithCustomPreUpgradeAssessment( |
| 52 | + "Check service instance and dependent resources are healthy before upgrade", |
| 53 | + func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context { |
| 54 | + return verifyServiceInstanceWithDependents(ctx, t, cfg, serviceInstanceDir, dependentDirs, verifyTimeout) |
| 55 | + }, |
| 56 | + ). |
| 57 | + WithCustomPostUpgradeAssessment( |
| 58 | + "Check all required resources are healthy after upgrade", |
| 59 | + func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context { |
| 60 | + return verifyResources(ctx, t, cfg, requiredDirs, verifyTimeout) |
| 61 | + }, |
| 62 | + ). |
| 63 | + WithCustomPostUpgradeAssessment( |
| 64 | + "Check service instance and dependent resources are healthy after upgrade", |
| 65 | + func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context { |
| 66 | + return verifyServiceInstanceWithDependents(ctx, t, cfg, serviceInstanceDir, dependentDirs, verifyTimeout) |
| 67 | + }, |
| 68 | + ) |
29 | 69 | testenv.Test(t, upgradeTest.Feature()) |
30 | 70 | } |
31 | 71 |
|
| 72 | +// verifyResources waits for resources in dirs to be ready |
| 73 | +func verifyResources(ctx context.Context, t *testing.T, cfg *envconf.Config, dirs []string, timeout time.Duration) context.Context { |
| 74 | + for _, dir := range dirs { |
| 75 | + klog.V(4).Infof("verify resources of directory %s", dir) |
| 76 | + if err := resources.WaitForResourcesToBeSynced(ctx, cfg, dir, nil, wait.WithTimeout(timeout)); err != nil { |
| 77 | + t.Errorf("verify resources of directory %s failed: %v", dir, err) |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + return ctx |
| 82 | +} |
| 83 | + |
| 84 | +// verifyServiceInstanceWithDependents verifies the service instance directory first and |
| 85 | +// if successful dependent directories |
| 86 | +func verifyServiceInstanceWithDependents(ctx context.Context, t *testing.T, cfg *envconf.Config, serviceInstanceDir string, dependentDirs []string, timeout time.Duration) context.Context { |
| 87 | + klog.V(4).Infof("verify service instance") |
| 88 | + if err := resources.WaitForResourcesToBeSynced(ctx, cfg, serviceInstanceDir, nil, wait.WithTimeout(timeout)); err != nil { |
| 89 | + t.Errorf("verify service instance failed: %v — skipping verification of: %s", err, strings.Join(dependentDirs, ", ")) |
| 90 | + return ctx |
| 91 | + } |
| 92 | + return verifyResources(ctx, t, cfg, dependentDirs, timeout) |
| 93 | +} |
| 94 | + |
| 95 | +// removeFromDirs is a helper function to remove directories from the list of directories to verify, |
| 96 | +// allowing dependent resources to be verified separately |
| 97 | +func removeFromDirs(dirs []string, remove []string) []string { |
| 98 | + result := slices.Clone(dirs) |
| 99 | + return slices.DeleteFunc(result, func(d string) bool { |
| 100 | + return slices.Contains(remove, d) |
| 101 | + }) |
| 102 | +} |
| 103 | + |
32 | 104 | // loadTags is a helper function to load FROM and TO tags for tests |
33 | 105 | // This allows custom tests to reuse the same version configuration |
34 | 106 | func loadTags() (string, string) { |
|
0 commit comments