diff --git a/Earthfile b/Earthfile index 374524b..053c114 100644 --- a/Earthfile +++ b/Earthfile @@ -225,10 +225,10 @@ go-test: COPY --dir +envtest-setup/envtest /usr/local/kubebuilder/bin # a bit dirty but preload the cache with the images we use in IT (found in functions.yaml) WITH DOCKER \ - --pull xpkg.upbound.io/crossplane-contrib/function-go-templating:v0.11.0 \ - --pull xpkg.upbound.io/crossplane-contrib/function-auto-ready:v0.4.2 \ - --pull xpkg.upbound.io/crossplane-contrib/function-environment-configs:v0.4.0 \ - --pull xpkg.upbound.io/crossplane-contrib/function-extra-resources:v0.1.0 + --pull xpkg.crossplane.io/crossplane-contrib/function-go-templating:v0.11.0 \ + --pull xpkg.crossplane.io/crossplane-contrib/function-auto-ready:v0.4.2 \ + --pull xpkg.crossplane.io/crossplane-contrib/function-environment-configs:v0.4.0 \ + --pull xpkg.crossplane.io/crossplane-contrib/function-extra-resources:v0.2.0 # this is silly, but we put these files into the default KUBEBUILDER_ASSETS location, because if we set # KUBEBUILDER_ASSETS on `go test` to the artifact path, which is perhaps more intuitive, the syntax highlighting # in intellij breaks due to the word BUILD in all caps. @@ -340,7 +340,7 @@ ci-artifacts: BUILD +multiplatform-build \ --CROSSPLANE_REPO=index.docker.io/crossplane-contrib/crossplane-diff \ --CROSSPLANE_REPO=ghcr.io/crossplane-contrib/crossplane-diff \ - --CROSSPLANE_REPO=xpkg.upbound.io/crossplane-contrib/crossplane-diff + --CROSSPLANE_REPO=xpkg.crossplane.io/crossplane-contrib/crossplane-diff # ci-codeql-setup sets up CodeQL for the ci-codeql target. ci-codeql-setup: diff --git a/cmd/diff/main_test.go b/cmd/diff/main_test.go new file mode 100644 index 0000000..d2b5229 --- /dev/null +++ b/cmd/diff/main_test.go @@ -0,0 +1,46 @@ +package main + +import ( + "context" + "os" + "os/exec" + "strings" + "testing" + "time" +) + +// TestMain runs before all tests and cleans up after all tests complete. +func TestMain(m *testing.M) { + // Run all tests + exitCode := m.Run() + + // Clean up orphaned function containers after tests complete + cleanupFunctionContainers() + + // Exit with the test suite's exit code + os.Exit(exitCode) +} + +// cleanupFunctionContainers removes the named function containers used by integration tests. +func cleanupFunctionContainers() { + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) + defer cancel() + + // Use exec.CommandContext to run docker command + cmd := exec.CommandContext(ctx, "docker", "ps", "-a", "-q", "--filter", "name=-it$") + output, err := cmd.Output() + if err != nil { + // Docker might not be available or no containers found - that's okay + return + } + + containerIDs := strings.Fields(string(output)) + if len(containerIDs) == 0 { + return + } + + // Remove the containers + args := append([]string{"rm", "-f"}, containerIDs...) + cleanupCmd := exec.CommandContext(ctx, "docker", args...) + _ = cleanupCmd.Run() // Ignore errors during cleanup +} diff --git a/cmd/diff/testdata/comp/resources/functions.yaml b/cmd/diff/testdata/comp/resources/functions.yaml index 3c32822..7f7fdef 100644 --- a/cmd/diff/testdata/comp/resources/functions.yaml +++ b/cmd/diff/testdata/comp/resources/functions.yaml @@ -1,29 +1,49 @@ --- # TODO: when updating these, ensure you also update the list of pulled images in Earthfile/go-test +# +# Named Docker containers for performance optimization in integration tests. +# The 'runtime-docker-name' annotation enables container reuse across multiple test cases. +# The 'runtime-docker-cleanup: Orphan' annotation leaves containers running after render completes, +# allowing subsequent tests to reuse them instead of creating new ones. +# +# IMPORTANT: These containers will persist after test runs. Clean them up manually with: +# docker rm -f $(docker ps -a -q --filter "name=function-*-it") apiVersion: pkg.crossplane.io/v1beta1 kind: Function metadata: name: function-go-templating + annotations: + render.crossplane.io/runtime-docker-name: "function-go-templating-v0.11.0-it" + render.crossplane.io/runtime-docker-cleanup: "Orphan" spec: - package: xpkg.upbound.io/crossplane-contrib/function-go-templating:v0.11.0 + package: xpkg.crossplane.io/crossplane-contrib/function-go-templating:v0.11.0 --- apiVersion: pkg.crossplane.io/v1beta1 kind: Function metadata: name: function-auto-ready + annotations: + render.crossplane.io/runtime-docker-name: "function-auto-ready-v0.4.2-it" + render.crossplane.io/runtime-docker-cleanup: "Orphan" spec: - package: xpkg.upbound.io/crossplane-contrib/function-auto-ready:v0.4.2 + package: xpkg.crossplane.io/crossplane-contrib/function-auto-ready:v0.4.2 --- apiVersion: pkg.crossplane.io/v1beta1 kind: Function metadata: name: function-environment-configs + annotations: + render.crossplane.io/runtime-docker-name: "function-environment-configs-v0.4.0-it" + render.crossplane.io/runtime-docker-cleanup: "Orphan" spec: - package: xpkg.upbound.io/crossplane-contrib/function-environment-configs:v0.4.0 + package: xpkg.crossplane.io/crossplane-contrib/function-environment-configs:v0.4.0 --- apiVersion: pkg.crossplane.io/v1beta1 kind: Function metadata: name: function-extra-resources + annotations: + render.crossplane.io/runtime-docker-name: "function-extra-resources-v0.2.0-it" + render.crossplane.io/runtime-docker-cleanup: "Orphan" spec: - package: xpkg.upbound.io/crossplane-contrib/function-extra-resources:v0.1.0 + package: xpkg.crossplane.io/crossplane-contrib/function-extra-resources:v0.2.0 diff --git a/cmd/diff/testdata/diff/resources/functions.yaml b/cmd/diff/testdata/diff/resources/functions.yaml index 3c32822..7f7fdef 100644 --- a/cmd/diff/testdata/diff/resources/functions.yaml +++ b/cmd/diff/testdata/diff/resources/functions.yaml @@ -1,29 +1,49 @@ --- # TODO: when updating these, ensure you also update the list of pulled images in Earthfile/go-test +# +# Named Docker containers for performance optimization in integration tests. +# The 'runtime-docker-name' annotation enables container reuse across multiple test cases. +# The 'runtime-docker-cleanup: Orphan' annotation leaves containers running after render completes, +# allowing subsequent tests to reuse them instead of creating new ones. +# +# IMPORTANT: These containers will persist after test runs. Clean them up manually with: +# docker rm -f $(docker ps -a -q --filter "name=function-*-it") apiVersion: pkg.crossplane.io/v1beta1 kind: Function metadata: name: function-go-templating + annotations: + render.crossplane.io/runtime-docker-name: "function-go-templating-v0.11.0-it" + render.crossplane.io/runtime-docker-cleanup: "Orphan" spec: - package: xpkg.upbound.io/crossplane-contrib/function-go-templating:v0.11.0 + package: xpkg.crossplane.io/crossplane-contrib/function-go-templating:v0.11.0 --- apiVersion: pkg.crossplane.io/v1beta1 kind: Function metadata: name: function-auto-ready + annotations: + render.crossplane.io/runtime-docker-name: "function-auto-ready-v0.4.2-it" + render.crossplane.io/runtime-docker-cleanup: "Orphan" spec: - package: xpkg.upbound.io/crossplane-contrib/function-auto-ready:v0.4.2 + package: xpkg.crossplane.io/crossplane-contrib/function-auto-ready:v0.4.2 --- apiVersion: pkg.crossplane.io/v1beta1 kind: Function metadata: name: function-environment-configs + annotations: + render.crossplane.io/runtime-docker-name: "function-environment-configs-v0.4.0-it" + render.crossplane.io/runtime-docker-cleanup: "Orphan" spec: - package: xpkg.upbound.io/crossplane-contrib/function-environment-configs:v0.4.0 + package: xpkg.crossplane.io/crossplane-contrib/function-environment-configs:v0.4.0 --- apiVersion: pkg.crossplane.io/v1beta1 kind: Function metadata: name: function-extra-resources + annotations: + render.crossplane.io/runtime-docker-name: "function-extra-resources-v0.2.0-it" + render.crossplane.io/runtime-docker-cleanup: "Orphan" spec: - package: xpkg.upbound.io/crossplane-contrib/function-extra-resources:v0.1.0 + package: xpkg.crossplane.io/crossplane-contrib/function-extra-resources:v0.2.0