Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion bazel/container_structure_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
31 changes: 30 additions & 1 deletion bazel/test/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,37 @@

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 ...
```
```

## 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
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:
```sh
cd bazel/test
bazel test :test --test_output=all
```

4. Restore the original rule:
```sh
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.
3 changes: 2 additions & 1 deletion cmd/container-structure-test/app/cmd/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions cmd/container-structure-test/app/cmd/test/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down
15 changes: 8 additions & 7 deletions pkg/config/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
11 changes: 9 additions & 2 deletions pkg/output/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -95,7 +102,7 @@ func FinalResults(out io.Writer, format types.OutputValue, result types.SummaryO
Total: result.Total,
Duration: time.Duration.Seconds(result.Duration), // JUnit expects durations as float of seconds
TestSuite: types.JUnitTestSuite{
Name: "container-structure-test.test",
Name: getJunitSuiteName(junitSuiteName),
Results: junit_cases,
},
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/output/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading