1package validation
2
3import (
4 "strings"
5 "testing"
6
7 "github.com/stretchr/testify/assert"
8 "github.com/stretchr/testify/require"
9)
10
11func TestValidateImageDigest(t *testing.T) {
12 validLower := "sha256:" + strings.Repeat("a", 64)
13 require.NoError(t, ValidateImageDigest(validLower))
14
15 for _, digest := range []string{
16 "sha256:" + strings.Repeat("A", 64),
17 "",
18 "sha256:" + strings.Repeat("a", 63),
19 "sha256:" + strings.Repeat("a", 65),
20 "sha256:" + strings.Repeat("g", 64),
21 "sha512:" + strings.Repeat("a", 64),
22 } {
23 assert.Error(t, ValidateImageDigest(digest), digest)
24 }
25}
26
27func TestParseImageReference(t *testing.T) {
28 tests := []struct {
29 name string
30 imageRef string
31 wantName string
32 wantRef string
33 }{
34 {
35 name: "simple image with latest tag",
36 imageRef: "myapp:latest",
37 wantName: "myapp",
38 wantRef: "latest",
39 },
40 {
41 name: "simple image with custom tag",
42 imageRef: "myapp:v1.0.0",
43 wantName: "myapp",
44 wantRef: "v1.0.0",
45 },
46 {
47 name: "simple image with digest",
48 imageRef: "myapp@sha256:abc123def456",
49 wantName: "myapp",
50 wantRef: "sha256:abc123def456",
51 },
52 {
53 name: "registry with image and tag",
54 imageRef: "registry.example.com/myapp:latest",
55 wantName: "registry.example.com/myapp",
56 wantRef: "latest",
57 },
58 {
59 name: "registry with port and image and tag",
60 imageRef: "registry.example.com:5000/myapp:v1.0",
61 wantName: "registry.example.com:5000/myapp",
62 wantRef: "v1.0",
63 },
64 {
65 name: "registry with port, image, and digest",
66 imageRef: "registry.example.com:5000/myapp@sha256:abc123",
67 wantName: "registry.example.com:5000/myapp",
68 wantRef: "sha256:abc123",
69 },
70 {
71 name: "simple image without tag (defaults to latest)",
72 imageRef: "myapp",
73 wantName: "myapp",
74 wantRef: "latest",
75 },
76 {
77 name: "registry with image without tag",
78 imageRef: "registry.example.com/myapp",
79 wantName: "registry.example.com/myapp",
80 wantRef: "latest",
81 },
82 {
83 name: "nested path image with tag",
84 imageRef: "registry.example.com/path/to/myapp:latest",
85 wantName: "registry.example.com/path/to/myapp",
86 wantRef: "latest",
87 },
88 {
89 name: "localhost registry with port",
90 imageRef: "localhost:5000/myapp:latest",
91 wantName: "localhost:5000/myapp",
92 wantRef: "latest",
93 },
94 {
95 name: "IP address registry with port",
96 imageRef: "192.168.1.100:5000/myapp:latest",
97 wantName: "192.168.1.100:5000/myapp",
98 wantRef: "latest",
99 },
100 }
101
102 for _, tt := range tests {
103 t.Run(tt.name, func(t *testing.T) {
104 gotName, gotRef := ParseImageReference(tt.imageRef)
105 assert.Equal(t, tt.wantName, gotName, "name mismatch")
106 assert.Equal(t, tt.wantRef, gotRef, "reference mismatch")
107 })
108 }
109}