Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow tests resources to override repo from provider #296

Merged
merged 3 commits into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions docs/resources/tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ description: |-
- `drivers` (Attributes) The resource specific driver configuration. This is merged with the provider scoped drivers configuration. (see [below for nested schema](#nestedatt--drivers))
- `labels` (Map of String) Metadata to attach to the tests resource. Used for filtering and grouping.
- `name` (String) The name of the test. If one is not provided, a random name will be generated.
- `repo` (String) The target repository the provider will use for pushing/pulling dynamically built images, overriding provider config.
- `skipped` (Boolean) Whether or not the tests were skipped. This is set to true if the tests were skipped, and false otherwise.
- `tests` (Attributes List) An ordered list of test suites to run (see [below for nested schema](#nestedatt--tests))
- `timeout` (String) The maximum amount of time to wait for all tests to complete. This includes the time it takes to start and destroy the driver.
Expand Down
39 changes: 27 additions & 12 deletions internal/provider/tests_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,16 @@ type TestsResource struct {
}

type TestsResourceModel struct {
Id types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
Driver DriverResourceModel `tfsdk:"driver"`
Drivers *TestsDriversResourceModel `tfsdk:"drivers"`
Images TestsImageResource `tfsdk:"images"`
Tests []TestResourceModel `tfsdk:"tests"`
Timeout types.String `tfsdk:"timeout"`
Labels map[string]string `tfsdk:"labels"`
Skipped types.Bool `tfsdk:"skipped"`
Id types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
Driver DriverResourceModel `tfsdk:"driver"`
Drivers *TestsDriversResourceModel `tfsdk:"drivers"`
Images TestsImageResource `tfsdk:"images"`
Tests []TestResourceModel `tfsdk:"tests"`
Timeout types.String `tfsdk:"timeout"`
Labels map[string]string `tfsdk:"labels"`
Skipped types.Bool `tfsdk:"skipped"`
RepoOverride types.String `tfsdk:"repo"`
}

type TestsImageResource map[string]string
Expand Down Expand Up @@ -120,6 +121,10 @@ func (t *TestsResource) Schema(ctx context.Context, req resource.SchemaRequest,
Description: "The driver to use for the test suite. Only one driver can be used at a time.",
Required: true,
},
"repo": schema.StringAttribute{
Optional: true,
Description: "The target repository the provider will use for pushing/pulling dynamically built images, overriding provider config.",
},
"drivers": DriverResourceSchema(ctx),
"images": schema.MapAttribute{
ElementType: types.StringType,
Expand Down Expand Up @@ -284,7 +289,17 @@ func (t *TestsResource) do(ctx context.Context, data *TestsResourceModel) (ds di
return []diag.Diagnostic{diag.NewErrorDiagnostic("invalid entrypoint image provided", "")}
}

trepo, err := name.NewRepository(fmt.Sprintf("%s/%s", t.repo.String(), "imagetest"))
repo := t.repo
if data.RepoOverride.ValueString() != "" {
l.InfoContextf(ctx, "using repository override %q", data.RepoOverride.String())
var err error
repo, err = name.NewRepository(data.RepoOverride.ValueString())
if err != nil {
return []diag.Diagnostic{diag.NewErrorDiagnostic("failed to parse repo override", err.Error())}
}
}

trepo, err := name.NewRepository(fmt.Sprintf("%s/%s", repo.String(), "imagetest"))
if err != nil {
return []diag.Diagnostic{diag.NewErrorDiagnostic("failed to create target repository", err.Error())}
}
Expand Down Expand Up @@ -369,8 +384,8 @@ func (t *TestsResource) do(ctx context.Context, data *TestsResourceModel) (ds di
}
envs["IMAGES"] = string(imgsResolvedData)
envs["IMAGETEST_DRIVER"] = string(data.Driver)
envs["IMAGETEST_REGISTRY"] = t.repo.RegistryStr()
envs["IMAGETEST_REPO"] = t.repo.String()
envs["IMAGETEST_REGISTRY"] = trepo.RegistryStr()
envs["IMAGETEST_REPO"] = trepo.String()

if os.Getenv("IMAGETEST_SKIP_TEARDOWN_ON_FAILURE") != "" || os.Getenv("IMAGETEST_SKIP_TEARDOWN") != "" {
envs["IMAGETEST_PAUSE_ON_ERROR"] = "true"
Expand Down