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