-
Notifications
You must be signed in to change notification settings - Fork 618
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add optional digest suffix to template locator
The suffix is "@digest" which may include an optional algorithm (defaults to "sha256"). The encoded digest must be at least 7 characters long. Examples: - template://my@sha256:60a87371451eabcd211c929759db61746a7c6a1c068f59d868db6aa8dca637bd - template://my@sha256:60a87371451 - template://my@60a8737 Signed-off-by: Jan Dubois <[email protected]>
- Loading branch information
Showing
2 changed files
with
121 additions
and
15 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
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,62 @@ | ||
package limatmpl | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/opencontainers/go-digest" | ||
"gotest.tools/v3/assert" | ||
) | ||
|
||
type digestTest struct { | ||
locator string | ||
fileRef string | ||
digest string | ||
error string | ||
} | ||
|
||
func TestDigestSuffix(t *testing.T) { | ||
tests := []digestTest{ | ||
// may looks like a digest, but is (intentionally) allowed as part of a template name | ||
{"tmpl.sh", "tmpl.sh", "", ""}, | ||
{"tmpl.sh@v1", "tmpl.sh@v1", "", ""}, | ||
{"[email protected]", "[email protected]", "", ""}, | ||
// this is an error though because it looks like a (too short) digest | ||
{"tmpl.sh@1", "", "", "fewer than"}, | ||
|
||
// can always append a file extension to use a digest string as part of a template name | ||
{"[email protected]", "[email protected]", "", ""}, | ||
{"template://my@1234567", "template://my", "sha256:1234567", ""}, | ||
{"template://[email protected]", "template://[email protected]", "", ""}, | ||
{"my@sha256:1234567", "my", "sha256:1234567", ""}, | ||
{"my@sha256:1234567.yaml", "my@sha256:1234567.yaml", "", ""}, | ||
|
||
// digest inside the middle of a URL is always ignored | ||
{"https://example.com/templates@sha256:1234567/my.yaml", "https://example.com/templates@sha256:1234567/my.yaml", "", ""}, | ||
|
||
// locators with digests | ||
{"tmpl.sh@1234567", "tmpl.sh", "sha256:1234567", ""}, | ||
{"tmpl.sh@sha256:1234567", "tmpl.sh", "sha256:1234567", ""}, | ||
|
||
// invalid locators | ||
{"tmpl.sh@invalid:1234567", "", "", "unavailable digest"}, | ||
{"tmpl.sh@abcdef", "tmpl.sh", "", "fewer than"}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.locator, func(t *testing.T) { | ||
tmpl := Template{Locator: test.locator} | ||
err := tmpl.splitOffDigest() | ||
if test.error != "" { | ||
assert.ErrorContains(t, err, test.error, test.locator) | ||
} else { | ||
assert.NilError(t, err) | ||
assert.Equal(t, tmpl.Locator, test.fileRef) | ||
if test.digest == "" { | ||
assert.Equal(t, tmpl.digest, "") | ||
} else { | ||
actual := digest.NewDigestFromEncoded(tmpl.algorithm, tmpl.digest) | ||
assert.Equal(t, actual.String(), test.digest) | ||
} | ||
} | ||
}) | ||
} | ||
} |