Skip to content

Commit

Permalink
fix: allow testing an index with a single image (#78)
Browse files Browse the repository at this point in the history
* Fix the situation where an index with a single image errors out
because GGCR expects a `linux/amd64` image to be present in the index.

---------

Signed-off-by: Mauren Berti <[email protected]>
  • Loading branch information
stormqueen1990 authored Oct 3, 2023
1 parent 251fe37 commit 1028779
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
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

0 comments on commit 1028779

Please sign in to comment.