Skip to content

Commit

Permalink
Fix resolving local manfiests by digest and fixup tests
Browse files Browse the repository at this point in the history
  • Loading branch information
amisevsk committed Jul 9, 2024
1 parent c3b6b0f commit 84c842b
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 29 deletions.
16 changes: 15 additions & 1 deletion pkg/lib/repo/local/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ import (
"sort"

"kitops/pkg/lib/constants"
"kitops/pkg/lib/repo/util"

"github.com/opencontainers/go-digest"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"oras.land/oras-go/v2/errdef"
)
Expand Down Expand Up @@ -124,7 +126,19 @@ func (li *localIndex) delete(target ocispec.Descriptor) error {
}

func (li *localIndex) resolve(reference string) (ocispec.Descriptor, error) {
return li.modelTags.get(reference)
if reference == "" {
return ocispec.DescriptorEmptyJSON, errdef.ErrMissingReference
}
if util.ReferenceIsDigest(reference) {
for _, desc := range li.Manifests {
if desc.Digest == digest.Digest(reference) {
return desc, nil
}
}
return ocispec.DescriptorEmptyJSON, errdef.ErrNotFound
} else {
return li.modelTags.get(reference)
}
}

func (li *localIndex) tag(desc ocispec.Descriptor, reference string) error {
Expand Down
7 changes: 7 additions & 0 deletions pkg/lib/repo/util/reference.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,13 @@ func ParseReference(refString string) (reference *registry.Reference, extraTags
return reference, extraTags, nil
}

// ReferenceIsDigest returns if the reference is a digest. If false, reference should
// be treated as a tag
func ReferenceIsDigest(ref string) bool {
err := digest.Digest(ref).Validate()
return err == nil
}

// DefaultReference returns a reference that can be used when no reference is supplied. It uses
// the default registry and repository
func DefaultReference() *registry.Reference {
Expand Down
6 changes: 3 additions & 3 deletions testing/modelkit-refs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,13 @@ func TestModelKitReferences(t *testing.T) {
// Pack the current dir, unpack it into the next dir. If we expect this to fail, assert that
// output contains expected text
if modelkit.PackErrRegexp != nil {
packOutput := runCommand(t, expectError, "pack", curDir, "-t", modelkit.Tag, "-v")
packOutput := runCommand(t, expectError, "pack", curDir, "-t", modelkit.Tag)
assertContainsLineRegexp(t, packOutput, *modelkit.PackErrRegexp, true)
continue
}
runCommand(t, expectNoError, "pack", curDir, "-t", modelkit.Tag, "-v")
runCommand(t, expectNoError, "pack", curDir, "-t", modelkit.Tag)
runCommand(t, expectNoError, "list")
runCommand(t, expectNoError, "unpack", modelkit.Tag, "-d", nextDir, "-v")
runCommand(t, expectNoError, "unpack", modelkit.Tag, "-d", nextDir)

// Verify unpacked contents
checkFilesExist(t, nextDir, allFiles)
Expand Down
8 changes: 4 additions & 4 deletions testing/pack-unpack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ func TestPackUnpack(t *testing.T) {
// Create files for test case
setupFiles(t, modelKitPath, append(tt.Files, tt.IgnoredFiles...))

runCommand(t, expectNoError, "pack", modelKitPath, "-t", modelKitTag, "-v")
runCommand(t, expectNoError, "pack", modelKitPath, "-t", modelKitTag)
runCommand(t, expectNoError, "list")
runCommand(t, expectNoError, "unpack", modelKitTag, "-d", unpackPath, "-v")
runCommand(t, expectNoError, "unpack", modelKitTag, "-d", unpackPath)

checkFilesExist(t, unpackPath, tt.Files)
checkFilesDoNotExist(t, unpackPath, append(tt.IgnoredFiles, ".kitignore"))
Expand Down Expand Up @@ -97,7 +97,7 @@ datasets:
}
setupFiles(t, modelKitPath, []string{"test-file.txt", "test-dir/test-subfile.txt"})

packOut := runCommand(t, expectNoError, "pack", modelKitPath, "-t", "test:repack1", "-v")
packOut := runCommand(t, expectNoError, "pack", modelKitPath, "-t", "test:repack1")
digestOne := digestFromPack(t, packOut)

// Change timestamps on file to simulate an unpacked modelkit at a future time
Expand All @@ -112,7 +112,7 @@ datasets:
t.Fatal(err)
}

packOut = runCommand(t, expectNoError, "pack", modelKitPath, "-t", "test:repack2", "-v")
packOut = runCommand(t, expectNoError, "pack", modelKitPath, "-t", "test:repack2")
digestTwo := digestFromPack(t, packOut)

assert.Equal(t, digestOne, digestTwo, "Digests should be the same")
Expand Down
40 changes: 20 additions & 20 deletions testing/remove_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func TestRemoveSingleModelkitTag(t *testing.T) {
}

// Pack model kit and tag it
packOut := runCommand(t, expectNoError, "pack", modelKitPath, "-t", "test:delete_testing", "-v")
packOut := runCommand(t, expectNoError, "pack", modelKitPath, "-t", "test:delete_testing")
digest := digestFromPack(t, packOut)
modelRegexp := fmt.Sprintf(`^test\s+delete_testing.*%s$`, digest)

Expand All @@ -60,7 +60,7 @@ func TestRemoveSingleModelkitTag(t *testing.T) {
assertContainsLineRegexp(t, listOut, modelRegexp, true)

// Remove modelkit and verify it's no longer in 'kit list'
runCommand(t, expectNoError, "remove", "test:delete_testing", "-v")
runCommand(t, expectNoError, "remove", "test:delete_testing")
listOut = runCommand(t, expectNoError, "list")
assertContainsLineRegexp(t, listOut, modelRegexp, false)
}
Expand All @@ -79,7 +79,7 @@ func TestRemoveSingleModelkitDigest(t *testing.T) {
}

// Pack model kit and tag it
packOut := runCommand(t, expectNoError, "pack", modelKitPath, "-t", "test:delete_testing", "-v")
packOut := runCommand(t, expectNoError, "pack", modelKitPath, "-t", "test:delete_testing")
digest := digestFromPack(t, packOut)
modelRegexp := fmt.Sprintf(`^test\s+delete_testing.*%s$`, digest)

Expand All @@ -89,7 +89,7 @@ func TestRemoveSingleModelkitDigest(t *testing.T) {

// Remove modelkit and verify it's no longer in 'kit list'
ref := fmt.Sprintf("test@%s", digest)
runCommand(t, expectNoError, "remove", ref, "-v")
runCommand(t, expectNoError, "remove", ref)
listOut = runCommand(t, expectNoError, "list")
assertContainsLineRegexp(t, listOut, modelRegexp, false)
}
Expand All @@ -108,7 +108,7 @@ func TestRemoveSingleModelkitNoTag(t *testing.T) {
}

// Pack model kit and tag it
packOut := runCommand(t, expectNoError, "pack", modelKitPath, "-v")
packOut := runCommand(t, expectNoError, "pack", modelKitPath)
digest := digestFromPack(t, packOut)
modelRegexp := fmt.Sprintf(`^.*%s$`, digest)

Expand All @@ -117,7 +117,7 @@ func TestRemoveSingleModelkitNoTag(t *testing.T) {
assertContainsLineRegexp(t, listOut, modelRegexp, true)

// Remove modelkit and verify it's no longer in 'kit list'
runCommand(t, expectNoError, "remove", digest, "-v")
runCommand(t, expectNoError, "remove", digest)
listOut = runCommand(t, expectNoError, "list")
assertContainsLineRegexp(t, listOut, modelRegexp, false)
}
Expand All @@ -136,11 +136,11 @@ func TestRemoveModelkitUntagsWhenMultiple(t *testing.T) {
}

// Pack model kit and tag it
packOut := runCommand(t, expectNoError, "pack", modelKitPath, "-t", "test:test_tag_1", "-v")
packOut := runCommand(t, expectNoError, "pack", modelKitPath, "-t", "test:test_tag_1")
digest := digestFromPack(t, packOut)
firstModelRegexp := fmt.Sprintf(`^test\s+test_tag_1.*%s$`, digest)

runCommand(t, expectNoError, "tag", "test:test_tag_1", "test:test_tag_2", "-v")
runCommand(t, expectNoError, "tag", "test:test_tag_1", "test:test_tag_2")
secondModelRegexp := fmt.Sprintf(`^test\s+test_tag_2.*%s$`, digest)

// Ensure modelkit exists in output of 'kit list'
Expand All @@ -149,7 +149,7 @@ func TestRemoveModelkitUntagsWhenMultiple(t *testing.T) {
assertContainsLineRegexp(t, listOut, secondModelRegexp, true)

// Remove modelkit and verify it's no longer in 'kit list'
runCommand(t, expectNoError, "remove", "test:test_tag_1", "-v")
runCommand(t, expectNoError, "remove", "test:test_tag_1")
listOut = runCommand(t, expectNoError, "list")
assertContainsLineRegexp(t, listOut, firstModelRegexp, false)
assertContainsLineRegexp(t, listOut, secondModelRegexp, true)
Expand All @@ -169,11 +169,11 @@ func TestRemoveModelkitUntagsAllWhenDigest(t *testing.T) {
}

// Pack model kit and tag it
packOut := runCommand(t, expectNoError, "pack", modelKitPath, "-t", "test:test_tag_1", "-v")
packOut := runCommand(t, expectNoError, "pack", modelKitPath, "-t", "test:test_tag_1")
digest := digestFromPack(t, packOut)
firstModelRegexp := fmt.Sprintf(`^test\s+test_tag_1.*%s$`, digest)

runCommand(t, expectNoError, "tag", "test:test_tag_1", "test:test_tag_2", "-v")
runCommand(t, expectNoError, "tag", "test:test_tag_1", "test:test_tag_2")
secondModelRegexp := fmt.Sprintf(`^test\s+test_tag_2.*%s$`, digest)

// Ensure modelkit exists in output of 'kit list'
Expand All @@ -183,7 +183,7 @@ func TestRemoveModelkitUntagsAllWhenDigest(t *testing.T) {

// Remove modelkit and verify it's no longer in 'kit list'
ref := fmt.Sprintf("test@%s", digest)
runCommand(t, expectNoError, "remove", ref, "-v")
runCommand(t, expectNoError, "remove", ref)
listOut = runCommand(t, expectNoError, "list")
assertContainsLineRegexp(t, listOut, firstModelRegexp, false)
assertContainsLineRegexp(t, listOut, secondModelRegexp, false)
Expand All @@ -203,18 +203,18 @@ func TestRemoveModelkitUntagged(t *testing.T) {
}

// Pack model kit and tag it
packOut := runCommand(t, expectNoError, "pack", modelKitPath, "-t", "test:testing-tag", "-v")
packOut := runCommand(t, expectNoError, "pack", modelKitPath, "-t", "test:testing-tag")
digestOne := digestFromPack(t, packOut)
regexpOne := fmt.Sprintf("^test.*%s$", digestOne)

// Create files to pack with a different digests
setupFiles(t, modelKitPath, []string{"testfile-1"})
packOut = runCommand(t, expectNoError, "pack", modelKitPath, "-t", "test:testing-tag", "-v")
packOut = runCommand(t, expectNoError, "pack", modelKitPath, "-t", "test:testing-tag")
digestTwo := digestFromPack(t, packOut)
regexpTwo := fmt.Sprintf("^test.*%s$", digestTwo)

setupFiles(t, modelKitPath, []string{"testfile-2"})
packOut = runCommand(t, expectNoError, "pack", modelKitPath, "-t", "test:testing-tag", "-v")
packOut = runCommand(t, expectNoError, "pack", modelKitPath, "-t", "test:testing-tag")
digestThree := digestFromPack(t, packOut)
regexpThree := fmt.Sprintf(`^test\s+testing-tag.*%s$`, digestThree)

Expand All @@ -225,7 +225,7 @@ func TestRemoveModelkitUntagged(t *testing.T) {
assertContainsLineRegexp(t, listOut, regexpThree, true)

// Remove modelkit and verify it's no longer in 'kit list'
runCommand(t, expectNoError, "remove", "--all", "-v")
runCommand(t, expectNoError, "remove", "--all")
listOut = runCommand(t, expectNoError, "list")
assertContainsLineRegexp(t, listOut, regexpOne, false)
assertContainsLineRegexp(t, listOut, regexpTwo, false)
Expand All @@ -246,18 +246,18 @@ func TestRemoveModelkitAll(t *testing.T) {
}

// Pack model kit and tag it
packOut := runCommand(t, expectNoError, "pack", modelKitPath, "-t", "test:testing-tag", "-v")
packOut := runCommand(t, expectNoError, "pack", modelKitPath, "-t", "test:testing-tag")
digestOne := digestFromPack(t, packOut)
regexpOne := fmt.Sprintf("^test.*%s$", digestOne)

// Create files to pack with a different digests
setupFiles(t, modelKitPath, []string{"testfile-1"})
packOut = runCommand(t, expectNoError, "pack", modelKitPath, "-t", "test:testing-tag", "-v")
packOut = runCommand(t, expectNoError, "pack", modelKitPath, "-t", "test:testing-tag")
digestTwo := digestFromPack(t, packOut)
regexpTwo := fmt.Sprintf("^test.*%s$", digestTwo)

setupFiles(t, modelKitPath, []string{"testfile-2"})
packOut = runCommand(t, expectNoError, "pack", modelKitPath, "-t", "test:testing-tag", "-v")
packOut = runCommand(t, expectNoError, "pack", modelKitPath, "-t", "test:testing-tag")
digestThree := digestFromPack(t, packOut)
regexpThree := fmt.Sprintf(`^test\s+testing-tag.*%s$`, digestThree)

Expand All @@ -268,7 +268,7 @@ func TestRemoveModelkitAll(t *testing.T) {
assertContainsLineRegexp(t, listOut, regexpThree, true)

// Remove modelkit and verify it's no longer in 'kit list'
runCommand(t, expectNoError, "remove", "--all", "--force", "-v")
runCommand(t, expectNoError, "remove", "--all", "--force")
listOut = runCommand(t, expectNoError, "list")
assertContainsLineRegexp(t, listOut, regexpOne, false)
assertContainsLineRegexp(t, listOut, regexpTwo, false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,4 @@ modelkits:
manifestVersion: 1.0.0
model:
path: test-ref:model-11
packErrRegexp: Reached maximum number of model references
packErrRegexp: reached maximum number of model references
1 change: 1 addition & 0 deletions testing/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func testPreflight(t *testing.T) func(t *testing.T) {
// calls `os.Exit`, this command will terminate without generating any logs.
// Returns the stdout and stderr output of the command.
func runCommand(t *testing.T, e shouldExpectError, args ...string) string {
args = append(args, "-vvv")
t.Logf("Running command: kit %s", strings.Join(args, " "))
runCmd := cmd.RunCommand()
runCmd.SetArgs(args)
Expand Down

0 comments on commit 84c842b

Please sign in to comment.