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

fix: allow testing an index with a single image #78

Merged
merged 3 commits into from
Oct 3, 2023
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
3 changes: 2 additions & 1 deletion internal/provider/exec_test_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,9 @@ func (d *ExecTestDataSource) Read(ctx context.Context, req datasource.ReadReques
resp.Diagnostics.AddError("Invalid ref", fmt.Sprintf("Unable to parse ref %s, got error: %s", data.Digest.ValueString(), err))
return
}

// Check we can get the image before running the test.
if _, err := remote.Image(ref, d.popts.withContext(ctx)...); err != nil {
if _, err := remote.Get(ref, d.popts.withContext(ctx)...); err != nil {
resp.Diagnostics.AddError("Unable to fetch image", fmt.Sprintf("Unable to fetch image for ref %s, got error: %s", data.Digest.ValueString(), err))
return
}
Expand Down
38 changes: 36 additions & 2 deletions internal/provider/structure_test_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/chainguard-dev/terraform-provider-oci/pkg/structure"
"github.com/chainguard-dev/terraform-provider-oci/pkg/validators"
"github.com/google/go-containerregistry/pkg/name"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/datasource"
Expand Down Expand Up @@ -127,8 +128,8 @@ func (d *StructureTestDataSource) Read(ctx context.Context, req datasource.ReadR
resp.Diagnostics.AddError("Invalid ref", fmt.Sprintf("Unable to parse ref %s, got error: %s", data.Digest.ValueString(), err))
return
}
// TODO: This should accept a platform, or fail if the ref points to an index.
img, err := remote.Image(ref, d.popts.withContext(ctx)...)

desc, err := remote.Get(ref, d.popts.withContext(ctx)...)
if err != nil {
resp.Diagnostics.AddError("Unable to fetch image", fmt.Sprintf("Unable to fetch image for ref %s, got error: %s", data.Digest.ValueString(), err))
return
Expand All @@ -150,6 +151,39 @@ func (d *StructureTestDataSource) Read(ctx context.Context, req datasource.ReadR
}
}

var img v1.Image
switch {
case desc.MediaType.IsImage():
img, err = desc.Image()
if err != nil {
resp.Diagnostics.AddError("Unable to fetch image", fmt.Sprintf("Unable to fetch image for ref %s, got error: %s", data.Digest.ValueString(), err))
return
}
case desc.MediaType.IsIndex():
index, err := desc.ImageIndex()
if err != nil {
resp.Diagnostics.AddError("Unable to read image index", fmt.Sprintf("Unable to read image index for ref %s, got error: %s", data.Digest.ValueString(), err))
return
}

indexManifest, err := index.IndexManifest()
if err != nil {
resp.Diagnostics.AddError("Unable to read image index manifest", fmt.Sprintf("Unable to read image index manifest for ref %s, got error: %s", data.Digest.ValueString(), err))
return
}

if len(indexManifest.Manifests) == 0 {
resp.Diagnostics.AddError("Unable to read image from index manifest", fmt.Sprintf("Unable to read image from index manifest for ref %s: index is empty", data.Digest.ValueString()))
}

firstDescriptor := indexManifest.Manifests[0]
img, err = index.Image(firstDescriptor.Digest)
if err != nil {
resp.Diagnostics.AddError("Unable to load image", fmt.Sprintf("Unable to load image for ref %s, got error: %s", data.Digest.ValueString(), err))
return
}
}

if err := conds.Check(img); err != nil {
data.TestedRef = basetypes.NewStringValue("")
data.Id = basetypes.NewStringValue("")
Expand Down