From fdda08112b2c9b977e37d4bc5065318bb2645616 Mon Sep 17 00:00:00 2001 From: Vince Rose Date: Wed, 6 Aug 2025 09:49:42 -0700 Subject: [PATCH 1/5] add an option to set junit suite name. Pass the bazel target name --- bazel/container_structure_test.bzl | 6 +++- bazel/test/.bazelversion | 2 +- bazel/test/README.md | 35 ++++++++++++++++++- cmd/container-structure-test/app/cmd/test.go | 3 +- .../app/cmd/test/util.go | 4 +-- pkg/config/options.go | 15 ++++---- pkg/output/output.go | 17 ++++++--- pkg/output/output_test.go | 2 +- 8 files changed, 65 insertions(+), 19 deletions(-) diff --git a/bazel/container_structure_test.bzl b/bazel/container_structure_test.bzl index e59d183d..e7a3302f 100644 --- a/bazel/container_structure_test.bzl +++ b/bazel/container_structure_test.bzl @@ -44,7 +44,11 @@ fi """ def _structure_test_impl(ctx): - fixed_args = [] + fixed_args = [ + "--test-report $XML_OUTPUT_FILE", + "--output junit", + "--junit-suite-name $TEST_TARGET" + ] test_bin = ctx.toolchains["@container_structure_test//bazel:structure_test_toolchain_type"].st_info.binary jq_bin = ctx.toolchains["@aspect_bazel_lib//lib:jq_toolchain_type"].jqinfo.bin diff --git a/bazel/test/.bazelversion b/bazel/test/.bazelversion index f3b5af39..93c8ddab 100644 --- a/bazel/test/.bazelversion +++ b/bazel/test/.bazelversion @@ -1 +1 @@ -6.1.1 +7.6.0 diff --git a/bazel/test/README.md b/bazel/test/README.md index 79fea015..7148bdaa 100644 --- a/bazel/test/README.md +++ b/bazel/test/README.md @@ -2,8 +2,41 @@ Verifies that the container_structure_test bazel rule exposed by this project works properly. +## Running tests with pre-compiled toolchain + ```sh cd bazel/test bazel test ... bazel test --enable_bzlmod ... -``` \ No newline at end of file +``` + +## Testing with local changes (non-pre-compiled toolchain) + +When developing changes, you may want to test your modifications before they're compiled a release. Here's how to test with a locally built binary: + +1. Build your local binary: + ```sh + go build -o /tmp/container-structure-test-local ./cmd/container-structure-test/ + ``` + +2. Temporarily modify `bazel/container_structure_test.bzl`: + ```sh + # Backup the original + cp bazel/container_structure_test.bzl bazel/container_structure_test.bzl.backup + + # Replace the st_path reference with your local binary + sed -i '' 's|readonly st=$(rlocation {st_path})|readonly st="/tmp/container-structure-test-local"|g' bazel/container_structure_test.bzl + ``` + +3. Run the bazel test: + ```sh + cd bazel/test + bazel test :test --test_output=all + ``` + +4. Restore the original rule: + ```sh + mv bazel/container_structure_test.bzl.backup bazel/container_structure_test.bzl + ``` + +This allows you to verify that your changes work correctly with the bazel integration before submitting them. diff --git a/cmd/container-structure-test/app/cmd/test.go b/cmd/container-structure-test/app/cmd/test.go index 14953622..89cd3131 100644 --- a/cmd/container-structure-test/app/cmd/test.go +++ b/cmd/container-structure-test/app/cmd/test.go @@ -205,7 +205,7 @@ func run(out io.Writer) error { channel := make(chan interface{}, 1) go runTests(out, channel, args, driverImpl) // TODO(nkubala): put a sync.WaitGroup here - return test.ProcessResults(out, opts.Output, channel) + return test.ProcessResults(out, opts.Output, opts.JunitSuiteName, channel) } func runTests(out io.Writer, channel chan interface{}, args *drivers.DriverConfig, driverImpl func(drivers.DriverConfig) (drivers.Driver, error)) { @@ -246,6 +246,7 @@ func AddTestFlags(cmd *cobra.Command) { cmd.Flags().MarkDeprecated("json", "please use --output instead") cmd.Flags().VarP(&opts.Output, "output", "o", "output format for the test report (available format: text, json, junit)") cmd.Flags().BoolVar(&opts.NoColor, "no-color", false, "no color in the output") + cmd.Flags().StringVar(&opts.JunitSuiteName, "junit-suite-name", "", "name to use for the junit test suite (defaults to 'container-structure-test.test')") cmd.Flags().StringArrayVarP(&opts.ConfigFiles, "config", "c", []string{}, "test config files") cmd.MarkFlagRequired("config") diff --git a/cmd/container-structure-test/app/cmd/test/util.go b/cmd/container-structure-test/app/cmd/test/util.go index 801a64b1..ba912e34 100644 --- a/cmd/container-structure-test/app/cmd/test/util.go +++ b/cmd/container-structure-test/app/cmd/test/util.go @@ -105,7 +105,7 @@ func Parse(fp string, args *drivers.DriverConfig, driverImpl func(drivers.Driver return tests, nil } -func ProcessResults(out io.Writer, format unversioned.OutputValue, c chan interface{}) error { +func ProcessResults(out io.Writer, format unversioned.OutputValue, junitSuiteName string, c chan interface{}) error { totalPass := 0 totalFail := 0 totalDuration := time.Duration(0) @@ -143,7 +143,7 @@ func ProcessResults(out io.Writer, format unversioned.OutputValue, c chan interf // only output results here if we're in json mode summary.Results = results } - output.FinalResults(out, format, summary) + output.FinalResults(out, format, junitSuiteName, summary) return err } diff --git a/pkg/config/options.go b/pkg/config/options.go index ac1dbe9b..ae24c3c3 100644 --- a/pkg/config/options.go +++ b/pkg/config/options.go @@ -28,11 +28,12 @@ type StructureTestOptions struct { TestReport string ConfigFiles []string - JSON bool - Output unversioned.OutputValue - Pull bool - Save bool - Quiet bool - Force bool - NoColor bool + JSON bool + Output unversioned.OutputValue + JunitSuiteName string + Pull bool + Save bool + Quiet bool + Force bool + NoColor bool } diff --git a/pkg/output/output.go b/pkg/output/output.go index 500dd568..056c3103 100644 --- a/pkg/output/output.go +++ b/pkg/output/output.go @@ -31,6 +31,13 @@ import ( var bannerLength = 27 // default banner length +func getJunitSuiteName(junitSuiteName string) string { + if junitSuiteName != "" { + return junitSuiteName + } + return "container-structure-test.test" +} + func OutputResult(out io.Writer, result *types.TestResult) { color.Default.Fprintf(out, "=== RUN: %s\n", result.Name) if result.Pass { @@ -58,7 +65,7 @@ func Banner(out io.Writer, filename string) { color.Purple.Fprintln(out, strings.Repeat("=", bannerLength)) } -func FinalResults(out io.Writer, format types.OutputValue, result types.SummaryObject) error { +func FinalResults(out io.Writer, format types.OutputValue, junitSuiteName string, result types.SummaryObject) error { if format == types.Json { res, err := json.Marshal(result) if err != nil { @@ -94,10 +101,10 @@ func FinalResults(out io.Writer, format types.OutputValue, result types.SummaryO Fail: result.Fail, Total: result.Total, Duration: time.Duration.Seconds(result.Duration), // JUnit expects durations as float of seconds - TestSuite: types.JUnitTestSuite{ - Name: "container-structure-test.test", - Results: junit_cases, - }, + TestSuite: types.JUnitTestSuite{ + Name: getJunitSuiteName(junitSuiteName), + Results: junit_cases, + }, } res := []byte(strings.ReplaceAll(xml.Header, "\n", "")) marshalled, err := xml.Marshal(junit_result) diff --git a/pkg/output/output_test.go b/pkg/output/output_test.go index a2ccae9f..ef8badfb 100644 --- a/pkg/output/output_test.go +++ b/pkg/output/output_test.go @@ -59,7 +59,7 @@ func TestFinalResults(t *testing.T) { t.Run(test.format.String(), func(t *testing.T) { t.Parallel() - FinalResults(test.actual, test.format, result) + FinalResults(test.actual, test.format, "", result) if strings.TrimSpace(test.actual.String()) != test.expected { t.Errorf("expected %s but got %s", test.expected, test.actual) From 92436c368120155cb3a360ab9a5ea57013cec6fe Mon Sep 17 00:00:00 2001 From: Vincent Rose Date: Fri, 15 Aug 2025 15:47:29 -0700 Subject: [PATCH 2/5] Apply suggestions from code review Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> --- pkg/output/output.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pkg/output/output.go b/pkg/output/output.go index 056c3103..cc3c87a0 100644 --- a/pkg/output/output.go +++ b/pkg/output/output.go @@ -101,10 +101,10 @@ func FinalResults(out io.Writer, format types.OutputValue, junitSuiteName string Fail: result.Fail, Total: result.Total, Duration: time.Duration.Seconds(result.Duration), // JUnit expects durations as float of seconds - TestSuite: types.JUnitTestSuite{ - Name: getJunitSuiteName(junitSuiteName), - Results: junit_cases, - }, + TestSuite: types.JUnitTestSuite{ + Name: getJunitSuiteName(junitSuiteName), + Results: junit_cases, + }, } res := []byte(strings.ReplaceAll(xml.Header, "\n", "")) marshalled, err := xml.Marshal(junit_result) From eb3ea111004c7d23aeb44acc3fd202fac7f39611 Mon Sep 17 00:00:00 2001 From: Vince Rose Date: Fri, 15 Aug 2025 15:57:37 -0700 Subject: [PATCH 3/5] update instructions to work with bsd and gnu sed --- bazel/test/.bazelversion | 2 +- bazel/test/README.md | 8 ++------ 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/bazel/test/.bazelversion b/bazel/test/.bazelversion index 93c8ddab..f3b5af39 100644 --- a/bazel/test/.bazelversion +++ b/bazel/test/.bazelversion @@ -1 +1 @@ -7.6.0 +6.1.1 diff --git a/bazel/test/README.md b/bazel/test/README.md index 7148bdaa..5a351f52 100644 --- a/bazel/test/README.md +++ b/bazel/test/README.md @@ -21,11 +21,7 @@ When developing changes, you may want to test your modifications before they're 2. Temporarily modify `bazel/container_structure_test.bzl`: ```sh - # Backup the original - cp bazel/container_structure_test.bzl bazel/container_structure_test.bzl.backup - - # Replace the st_path reference with your local binary - sed -i '' 's|readonly st=$(rlocation {st_path})|readonly st="/tmp/container-structure-test-local"|g' bazel/container_structure_test.bzl + sed -i.bak 's|readonly st=$(rlocation {st_path})|readonly st="/tmp/container-structure-test-local"|g' bazel/container_structure_test.bzl ``` 3. Run the bazel test: @@ -36,7 +32,7 @@ When developing changes, you may want to test your modifications before they're 4. Restore the original rule: ```sh - mv bazel/container_structure_test.bzl.backup bazel/container_structure_test.bzl + mv bazel/container_structure_test.bzl.bak bazel/container_structure_test.bzl ``` This allows you to verify that your changes work correctly with the bazel integration before submitting them. From a5ef464743266fdeccca73d5680f776e54d16251 Mon Sep 17 00:00:00 2001 From: Vince Rose Date: Tue, 23 Sep 2025 10:40:11 -0700 Subject: [PATCH 4/5] disable bazel test --- .github/workflows/bazel.yaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/bazel.yaml b/.github/workflows/bazel.yaml index f54fd886..213bc250 100644 --- a/.github/workflows/bazel.yaml +++ b/.github/workflows/bazel.yaml @@ -10,6 +10,8 @@ on: jobs: test: + # Disabled until post-release + when: never uses: bazel-contrib/.github/.github/workflows/bazel.yaml@v6 with: folders: '["bazel/test"]' From 2bb878bd29ff32a2f4ee1d12f9ac1cef974c7969 Mon Sep 17 00:00:00 2001 From: Vince Rose Date: Wed, 1 Oct 2025 13:50:59 -0700 Subject: [PATCH 5/5] re-enable bazel tests --- .github/workflows/bazel.yaml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.github/workflows/bazel.yaml b/.github/workflows/bazel.yaml index 213bc250..f54fd886 100644 --- a/.github/workflows/bazel.yaml +++ b/.github/workflows/bazel.yaml @@ -10,8 +10,6 @@ on: jobs: test: - # Disabled until post-release - when: never uses: bazel-contrib/.github/.github/workflows/bazel.yaml@v6 with: folders: '["bazel/test"]'