|
1 | 1 | package render_test
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "cmp" |
4 | 5 | "errors"
|
5 | 6 | "fmt"
|
| 7 | + "github.com/operator-framework/operator-controller/internal/operator-controller/rukpak/bundle/source" |
| 8 | + "github.com/operator-framework/operator-controller/internal/operator-controller/rukpak/render/registryv1" |
| 9 | + "os" |
| 10 | + "path/filepath" |
6 | 11 | "reflect"
|
| 12 | + "sigs.k8s.io/yaml" |
| 13 | + "slices" |
| 14 | + "strings" |
7 | 15 | "testing"
|
8 | 16 |
|
9 | 17 | "github.com/stretchr/testify/require"
|
@@ -267,3 +275,84 @@ func Test_BundleValidatorCallsAllValidationFnsInOrder(t *testing.T) {
|
267 | 275 | require.NoError(t, val.Validate(nil))
|
268 | 276 | require.Equal(t, "hi", actual)
|
269 | 277 | }
|
| 278 | + |
| 279 | +// This test ensures consistent rendering behavior for registry+v1 bundles: |
| 280 | +// - Uses the bundles defined in the testdata directory |
| 281 | +// - Renders manifests using the registryv1 renderer |
| 282 | +// - Sorts rendered objects by Kind, Namespace, and Name to ensure deterministic output |
| 283 | +// - Writes the results to a temporary directory for inspection |
| 284 | +// - Ensures the rendering completes without error |
| 285 | +// - Automatically cleans up the generated files after the test completes |
| 286 | +// |
| 287 | +// This was introduced to prevent flaky diffs caused by non-deterministic manifest ordering. |
| 288 | +// Related issue: https://github.com/operator-framework/operator-controller/pull/1895 |
| 289 | +func Test_RenderRegistryV1Bundle_GenerateAndAutoCleanup(t *testing.T) { |
| 290 | + bundleRoot := "testdata/bundles" |
| 291 | + |
| 292 | + testCases := []struct { |
| 293 | + name string |
| 294 | + installNamespace string |
| 295 | + watchNamespace string |
| 296 | + bundle string |
| 297 | + testCaseName string |
| 298 | + }{ |
| 299 | + { |
| 300 | + name: "AllNamespaces", |
| 301 | + installNamespace: "argocd-system", |
| 302 | + watchNamespace: "", |
| 303 | + bundle: "argocd-operator.v0.6.0", |
| 304 | + testCaseName: "all-namespaces", |
| 305 | + }, |
| 306 | + { |
| 307 | + name: "SingleNamespaces", |
| 308 | + installNamespace: "argocd-system", |
| 309 | + watchNamespace: "argocd-watch", |
| 310 | + bundle: "argocd-operator.v0.6.0", |
| 311 | + testCaseName: "single-namespace", |
| 312 | + }, |
| 313 | + { |
| 314 | + name: "OwnNamespaces", |
| 315 | + installNamespace: "argocd-system", |
| 316 | + watchNamespace: "argocd-system", |
| 317 | + bundle: "argocd-operator.v0.6.0", |
| 318 | + testCaseName: "own-namespace", |
| 319 | + }, |
| 320 | + } |
| 321 | + |
| 322 | + for _, tc := range testCases { |
| 323 | + t.Run(tc.name, func(t *testing.T) { |
| 324 | + bundlePath := filepath.Join(bundleRoot, tc.bundle) |
| 325 | + outputDir := filepath.Join(t.TempDir(), tc.bundle, tc.testCaseName) |
| 326 | + |
| 327 | + fs := os.DirFS(bundlePath) |
| 328 | + bundle, err := source.FromFS(fs).GetBundle() |
| 329 | + require.NoError(t, err) |
| 330 | + |
| 331 | + rendered, err := registryv1.Renderer.Render(bundle, tc.installNamespace, |
| 332 | + render.WithTargetNamespaces(tc.watchNamespace)) |
| 333 | + require.NoError(t, err) |
| 334 | + |
| 335 | + sorted := slices.SortedFunc(slices.Values(rendered), orderByKindNamespaceName) |
| 336 | + |
| 337 | + require.NoError(t, os.MkdirAll(outputDir, os.ModePerm)) |
| 338 | + |
| 339 | + for idx, obj := range sorted { |
| 340 | + kind := obj.GetObjectKind().GroupVersionKind().Kind |
| 341 | + filename := fmt.Sprintf("%02d_%s_%s.yaml", idx, strings.ToLower(kind), obj.GetName()) |
| 342 | + filePath := filepath.Join(outputDir, filename) |
| 343 | + |
| 344 | + data, err := yaml.Marshal(obj) |
| 345 | + require.NoError(t, err) |
| 346 | + require.NoError(t, os.WriteFile(filePath, data, 0600)) |
| 347 | + } |
| 348 | + }) |
| 349 | + } |
| 350 | +} |
| 351 | + |
| 352 | +func orderByKindNamespaceName(a client.Object, b client.Object) int { |
| 353 | + return cmp.Or( |
| 354 | + cmp.Compare(a.GetObjectKind().GroupVersionKind().Kind, b.GetObjectKind().GroupVersionKind().Kind), |
| 355 | + cmp.Compare(a.GetNamespace(), b.GetNamespace()), |
| 356 | + cmp.Compare(a.GetName(), b.GetName()), |
| 357 | + ) |
| 358 | +} |
0 commit comments