-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from buildpacks/image-index
More cleanup
- Loading branch information
Showing
10 changed files
with
242 additions
and
332 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package layout | ||
|
||
import ( | ||
"fmt" | ||
|
||
v1 "github.com/google/go-containerregistry/pkg/v1" | ||
|
||
"github.com/buildpacks/imgutil" | ||
) | ||
|
||
// NewIndex will return an OCI ImageIndex saved on disk using OCI media Types. It can be modified and saved to a registry | ||
func NewIndex(repoName string, ops ...imgutil.IndexOption) (*imgutil.CNBIndex, error) { | ||
options := &imgutil.IndexOptions{} | ||
for _, op := range ops { | ||
if err := op(options); err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
var err error | ||
|
||
if options.BaseIndex == nil && options.BaseIndexRepoName != "" { // options.BaseIndex supersedes options.BaseIndexRepoName | ||
options.BaseIndex, err = newV1Index( | ||
options.BaseIndexRepoName, | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
return imgutil.NewCNBIndex(repoName, *options) | ||
} | ||
|
||
// newV1Index creates a layout image index from the given path. | ||
func newV1Index(path string) (v1.ImageIndex, error) { | ||
if !imageExists(path) { | ||
return nil, nil | ||
} | ||
layoutPath, err := FromPath(path) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to load layout from path: %w", err) | ||
} | ||
return layoutPath.ImageIndex() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package layout_test | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/google/go-containerregistry/pkg/v1/types" | ||
"github.com/sclevine/spec" | ||
"github.com/sclevine/spec/report" | ||
|
||
"github.com/buildpacks/imgutil" | ||
"github.com/buildpacks/imgutil/layout" | ||
h "github.com/buildpacks/imgutil/testhelpers" | ||
) | ||
|
||
func TestLayoutIndex(t *testing.T) { | ||
spec.Run(t, "LayoutNewIndex", testNewIndex, spec.Parallel(), spec.Report(report.Terminal{})) | ||
} | ||
|
||
func testNewIndex(t *testing.T, when spec.G, it spec.S) { | ||
var ( | ||
idx imgutil.ImageIndex | ||
tempDir string | ||
repoName string | ||
err error | ||
) | ||
|
||
it.Before(func() { | ||
// creates the directory to save all the OCI images on disk | ||
tempDir, err = os.MkdirTemp("", "image-indexes") | ||
h.AssertNil(t, err) | ||
|
||
// global directory and paths | ||
testDataDir = filepath.Join("testdata", "layout") | ||
_ = idx | ||
}) | ||
|
||
it.After(func() { | ||
err := os.RemoveAll(tempDir) | ||
h.AssertNil(t, err) | ||
}) | ||
|
||
when("#NewIndex", func() { | ||
it.Before(func() { | ||
repoName = "some/index" | ||
}) | ||
|
||
when("index doesn't exists on disk", func() { | ||
it("creates empty image index", func() { | ||
idx, err = layout.NewIndex( | ||
repoName, | ||
imgutil.WithXDGRuntimePath(tempDir), | ||
) | ||
h.AssertNil(t, err) | ||
}) | ||
|
||
it("ignores FromBaseIndex if it doesn't exist", func() { | ||
idx, err = layout.NewIndex( | ||
repoName, | ||
imgutil.WithXDGRuntimePath(tempDir), | ||
imgutil.FromBaseIndex("non-existent/index"), | ||
) | ||
h.AssertNil(t, err) | ||
}) | ||
|
||
it("creates empty image index with Docker media-types", func() { | ||
idx, err = layout.NewIndex( | ||
repoName, | ||
imgutil.WithXDGRuntimePath(tempDir), | ||
imgutil.WithMediaType(types.DockerManifestList), | ||
) | ||
h.AssertNil(t, err) | ||
}) | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.