diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml new file mode 100644 index 0000000..4c1ecbc --- /dev/null +++ b/.github/workflows/test.yaml @@ -0,0 +1,73 @@ +name: test + +on: + workflow_dispatch: + pull_request: + merge_group: + push: + branches: + - main + +permissions: + contents: read + +jobs: + unit: + name: migration CLI unit tests + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v7 + with: + persist-credentials: false + - uses: actions/setup-go@v7 + with: + go-version-file: go.mod + - name: Run unit tests + run: make test + + e2e-smoke: + name: migration CLI E2E smoke + runs-on: ubuntu-latest + timeout-minutes: 45 + env: + E2E_CLUSTER_NAME: olm-0to1-smoke + E2E_FIXTURE_CLUSTER_NAME: olm-0to1-smoke + E2E_FIXTURE_KUBECONFIG: ${{ github.workspace }}/library-olm/.kubeconfig/olm-0to1-smoke + E2E_KUBECONFIG: ${{ github.workspace }}/library-olm/.kubeconfig/olm-0to1-smoke + steps: + - uses: actions/checkout@v7 + with: + persist-credentials: false + # The fixture setup and snapshots are pinned to the library-olm module + # revision used by the standalone CLIs. The test builds this repo's binaries. + - uses: actions/checkout@v7 + with: + repository: operator-framework/library-olm + ref: 4c37ce218c2eecf9eb5231e038340ce63012ab99 + path: library-olm + persist-credentials: false + - uses: actions/setup-go@v7 + with: + go-version-file: go.mod + - name: Set up controller-free fixture cluster + run: make -C library-olm migration/e2e-fixture-setup + - name: Replay one OLMv0 operator fixture + run: make -C library-olm migration/e2e-install-fixture-v0 E2E_OPERATOR=ecr-secret-operator + - name: Exercise both standalone CLIs + run: make e2e-smoke + - name: Capture cluster diagnostics + if: failure() + run: | + mkdir -p artifacts + kubectl --kubeconfig "$E2E_KUBECONFIG" get subscriptions,clusterserviceversions,catalogsources -A -o wide > artifacts/olmv0.txt 2>&1 || true + kubectl --kubeconfig "$E2E_KUBECONFIG" get clustercatalogs,clusterextensions,clusterobjectsets -o wide > artifacts/olmv1.txt 2>&1 || true + - name: Tear down fixture cluster + if: always() + run: make -C library-olm migration/e2e-teardown + - name: Upload diagnostics + if: failure() + uses: actions/upload-artifact@v7 + with: + name: migration-cli-e2e-smoke-diagnostics + path: artifacts/ + if-no-files-found: ignore diff --git a/.github/workflows/unit-test.yaml b/.github/workflows/unit-test.yaml deleted file mode 100644 index 806f7a8..0000000 --- a/.github/workflows/unit-test.yaml +++ /dev/null @@ -1,26 +0,0 @@ -name: unit-test - -on: - workflow_dispatch: - pull_request: - merge_group: - push: - branches: - - main - -permissions: - contents: read - -jobs: - test: - name: migration CLI unit tests - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v7 - with: - persist-credentials: false - - uses: actions/setup-go@v7 - with: - go-version-file: go.mod - - name: Run unit tests - run: make test diff --git a/Makefile b/Makefile index 4c37492..98edee6 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -.PHONY: build test +.PHONY: build test e2e-smoke build: mkdir -p bin @@ -7,3 +7,7 @@ build: test: go test ./cmd/... + +# Run against a dedicated Kind cluster prepared with library-olm's fixture setup. +e2e-smoke: build + go test -tags=e2e -count=1 -timeout=20m ./test/e2e -run '^TestMigrationCLISmoke$$' diff --git a/README.md b/README.md index f966f82..402b672 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,23 @@ make build This creates `bin/migrate-catalogs-v0-to-v1` and `bin/migrate-operators-v0-to-v1`. Run `make test` to test both command packages. +## E2E smoke test + +`make e2e-smoke` builds both binaries and tests them against a dedicated Kind +cluster with the `ecr-secret-operator` OLMv0 fixture. The test follows the +catalog dry-run and migration, operator check and dry-run, conversion, and +acknowledged rollback. It verifies JSONL results, the resulting OLMv1 objects, +and preservation of the operator Deployment. It refuses a cluster whose +current context is not `kind-$E2E_CLUSTER_NAME` or that has the OLMv0 controller. + +CI provisions the cluster with the committed fixtures from the pinned +`library-olm` revision in `go.mod`; the binaries under test are built from this +repository. To run locally, prepare that fixture cluster using +`library-olm`'s `migration/e2e-fixture-setup` and +`migration/e2e-install-fixture-v0 E2E_OPERATOR=ecr-secret-operator` targets, +then set `E2E_CLUSTER_NAME` and `E2E_KUBECONFIG` and run `make e2e-smoke`. +Use `library-olm`'s `migration/e2e-teardown` target to delete the cluster. + ## Use Create OLMv1 ClusterCatalogs before migrating operators: diff --git a/test/e2e/cli_smoke_test.go b/test/e2e/cli_smoke_test.go new file mode 100644 index 0000000..4ebc7dc --- /dev/null +++ b/test/e2e/cli_smoke_test.go @@ -0,0 +1,182 @@ +//go:build e2e + +package e2e + +import ( + "encoding/json" + "os" + "os/exec" + "path/filepath" + "strings" + "testing" +) + +const ( + fixtureNamespace = "migration-e2e-ecr-secret" + fixtureSubscription = "ecr-secret-operator" + fixtureCatalog = "operatorhubio-catalog" +) + +type cliRecord struct { + Type string `json:"type"` + Target string `json:"target"` + Status string `json:"status"` + Data json.RawMessage `json:"data"` +} + +// TestMigrationCLISmoke follows the catalog, check, preview, convert, and +// rollback path from library-olm's fixture E2E suite using this repo's binaries. +func TestMigrationCLISmoke(t *testing.T) { + kubeconfig := os.Getenv("E2E_KUBECONFIG") + clusterName := os.Getenv("E2E_CLUSTER_NAME") + if kubeconfig == "" || clusterName == "" { + t.Fatal("E2E_KUBECONFIG and E2E_CLUSTER_NAME are required") + } + if _, err := os.Stat(kubeconfig); err != nil { + t.Fatalf("E2E_KUBECONFIG: %v", err) + } + kubectl := func(args ...string) string { + t.Helper() + return run(t, "kubectl", append([]string{"--kubeconfig", kubeconfig}, args...)...) + } + if context := strings.TrimSpace(kubectl("config", "current-context")); context != "kind-"+clusterName { + t.Fatalf("refusing to mutate context %q; want dedicated kind cluster %q", context, "kind-"+clusterName) + } + if controller := strings.TrimSpace(kubectl("get", "deployment/olm-operator", "-n", "olm", "--ignore-not-found", "-o", "name")); controller != "" { + t.Fatalf("fixture cluster unexpectedly has an OLMv0 controller: %s", controller) + } + if existing := strings.TrimSpace(kubectl("get", "clustercatalog/"+fixtureCatalog, "--ignore-not-found", "-o", "name")); existing != "" { + t.Fatalf("fixture catalog already exists: %s", existing) + } + if existing := strings.TrimSpace(kubectl("get", "clusterextension/"+fixtureSubscription, "--ignore-not-found", "-o", "name")); existing != "" { + t.Fatalf("fixture operator is already migrated: %s", existing) + } + kubectl("get", "subscription/"+fixtureSubscription, "-n", fixtureNamespace) + kubectl("get", "catalogsource/"+fixtureCatalog, "-n", "olm") + deployment := "deployment/" + fixtureSubscription + "-controller-manager" + workloadUID := strings.TrimSpace(kubectl("get", deployment, "-n", fixtureNamespace, "-o", "jsonpath={.metadata.uid}")) + if workloadUID == "" { + t.Fatal("fixture operator Deployment has no UID") + } + + catalogCLI := binary(t, "migrate-catalogs-v0-to-v1") + operatorCLI := binary(t, "migrate-operators-v0-to-v1") + catalogTarget := "olm/" + fixtureCatalog + operatorTarget := fixtureNamespace + "/" + fixtureSubscription + + preview := run(t, catalogCLI, "--kubeconfig", kubeconfig, "--dry-run", "--output=jsonl") + assertCatalogOutcome(t, requireRecord(t, preview, "result", catalogTarget), "dry-run") + if existing := strings.TrimSpace(kubectl("get", "clustercatalog/"+fixtureCatalog, "--ignore-not-found", "-o", "name")); existing != "" { + t.Fatal("catalog dry-run created a ClusterCatalog") + } + + created := run(t, catalogCLI, "--kubeconfig", kubeconfig, "--output=jsonl") + assertCatalogOutcome(t, requireRecord(t, created, "result", catalogTarget), "created") + kubectl("wait", "--for=jsonpath={.status.conditions[?(@.type=='Serving')].status}=True", "clustercatalog/"+fixtureCatalog, "--timeout=10m") + + check := run(t, operatorCLI, "check", fixtureSubscription, "-n", fixtureNamespace, "--kubeconfig", kubeconfig, "--output=jsonl") + var checkData struct { + Status string `json:"status"` + } + decodeData(t, requireRecord(t, check, "check", operatorTarget), &checkData) + if checkData.Status != "Eligible" { + t.Fatalf("operator check status = %q, want Eligible:\n%s", checkData.Status, check) + } + + operatorPreview := run(t, operatorCLI, "convert", fixtureSubscription, "-n", fixtureNamespace, "--dry-run", "--kubeconfig", kubeconfig, "--output=jsonl") + var previewData struct { + ClusterObjectSet string `json:"cluster_object_set"` + ClusterObjectDeployment string `json:"cluster_object_deployment"` + KindCounts map[string]int `json:"kind_counts"` + } + decodeData(t, requireRecord(t, operatorPreview, "dry-run", operatorTarget), &previewData) + if previewData.ClusterObjectSet == "" && previewData.ClusterObjectDeployment == "" { + t.Fatalf("operator dry-run omitted its resource manager:\n%s", operatorPreview) + } + if len(previewData.KindCounts) == 0 { + t.Fatalf("operator dry-run omitted collected resource kinds:\n%s", operatorPreview) + } + if existing := strings.TrimSpace(kubectl("get", "clusterextension/"+fixtureSubscription, "--ignore-not-found", "-o", "name")); existing != "" { + t.Fatal("operator dry-run created a ClusterExtension") + } + kubectl("get", "subscription/"+fixtureSubscription, "-n", fixtureNamespace) + + converted := run(t, operatorCLI, "convert", fixtureSubscription, "-n", fixtureNamespace, "--kubeconfig", kubeconfig, "--output=jsonl") + if record := requireRecord(t, converted, "result", operatorTarget); record.Status != "completed" { + t.Fatalf("conversion status = %q, want completed:\n%s", record.Status, converted) + } + kubectl("wait", "--for=jsonpath={.status.conditions[?(@.type=='Installed')].status}=True", "clusterextension/"+fixtureSubscription, "--timeout=10m") + if remaining := strings.TrimSpace(kubectl("get", "subscription/"+fixtureSubscription, "-n", fixtureNamespace, "--ignore-not-found", "-o", "name")); remaining != "" { + t.Fatalf("conversion retained Subscription: %s", remaining) + } + if uid := strings.TrimSpace(kubectl("get", deployment, "-n", fixtureNamespace, "-o", "jsonpath={.metadata.uid}")); uid != workloadUID { + t.Fatalf("conversion changed workload UID from %s to %s", workloadUID, uid) + } + + rolledBack := run(t, operatorCLI, "rollback", fixtureSubscription, "--acknowledge-installed", "--kubeconfig", kubeconfig, "--output=jsonl") + if record := requireRecord(t, rolledBack, "result", fixtureSubscription); record.Status != "completed" { + t.Fatalf("rollback status = %q, want completed:\n%s", record.Status, rolledBack) + } + kubectl("get", "subscription/"+fixtureSubscription, "-n", fixtureNamespace) + if remaining := strings.TrimSpace(kubectl("get", "clusterextension/"+fixtureSubscription, "--ignore-not-found", "-o", "name")); remaining != "" { + t.Fatalf("rollback retained ClusterExtension: %s", remaining) + } + if uid := strings.TrimSpace(kubectl("get", deployment, "-n", fixtureNamespace, "-o", "jsonpath={.metadata.uid}")); uid != workloadUID { + t.Fatalf("rollback changed workload UID from %s to %s", workloadUID, uid) + } +} + +func binary(t *testing.T, name string) string { + t.Helper() + path, err := filepath.Abs(filepath.Join("..", "..", "bin", name)) + if err != nil { + t.Fatal(err) + } + if _, err := os.Stat(path); err != nil { + t.Fatalf("migration binary %s: %v", path, err) + } + return path +} + +func run(t *testing.T, name string, args ...string) string { + t.Helper() + command := exec.CommandContext(t.Context(), name, args...) + output, err := command.CombinedOutput() + if err != nil { + t.Fatalf("%s %s: %v\n%s", name, strings.Join(args, " "), err, output) + } + return string(output) +} + +func requireRecord(t *testing.T, stream, recordType, target string) cliRecord { + t.Helper() + for _, line := range strings.Split(strings.TrimSpace(stream), "\n") { + var record cliRecord + if err := json.Unmarshal([]byte(line), &record); err != nil { + t.Fatalf("invalid JSONL record: %v\n%s", err, stream) + } + if record.Type == recordType && record.Target == target { + return record + } + } + t.Fatalf("missing %s record for %s:\n%s", recordType, target, stream) + return cliRecord{} +} + +func decodeData(t *testing.T, record cliRecord, value any) { + t.Helper() + if err := json.Unmarshal(record.Data, value); err != nil { + t.Fatalf("decode %s record for %s: %v", record.Type, record.Target, err) + } +} + +func assertCatalogOutcome(t *testing.T, record cliRecord, want string) { + t.Helper() + var data struct { + Outcome string `json:"outcome"` + } + decodeData(t, record, &data) + if record.Status != "completed" || data.Outcome != want { + t.Fatalf("catalog result = %s/%s, want completed/%s", record.Status, data.Outcome, want) + } +}