-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprobe_test.go
157 lines (153 loc) · 3.22 KB
/
probe_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main
import "testing"
func TestImageExistsInRegistry(t *testing.T) {
type args struct {
imageid string
}
tests := []struct {
name string
args args
want bool
wantErr bool
}{
{
"When using the docker registry and a valid image then it should return true",
args{
imageid: "alpine:latest",
},
true,
false,
},
{
"When using the github registry and a valid image then it should return true",
args{
imageid: "ghcr.io/riskident/jelease:v0.6.3",
},
true,
false,
},
{
"When using the docker registry and an invalid image then it should return false",
args{
imageid: "alpine:latest-invalid",
},
false,
true,
},
{
"When a valid image is used from a private registry then it should return true",
args{
imageid: "ghcr.io/riskident/image-availability-exporter/busybox:latest",
},
true,
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := imageExistsInAnotherRegistry(tt.args.imageid)
if (err != nil) != tt.wantErr {
t.Errorf("ImageExistsInRegistry() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("ImageExistsInRegistry() got = %v, want %v", got, tt.want)
}
})
}
}
func TestImageExistsInDockerhub(t *testing.T) {
type args struct {
image string
}
tests := []struct {
name string
args args
want bool
wantErr bool
}{
{
"When using a valid image then it should return true",
args{
image: "alpine:latest",
},
true,
false,
},
{
"When using an invalid image then it should return false",
args{
image: "alpine:latest-invalid",
},
false,
false,
},
{
"When the name of the image does not contain a tag then the tag is latest",
args{
image: "alpine",
},
true,
false,
},
{
"When the image has a full name then the registry part is deleted",
args{
image: "docker.io/grafana/grafana:11.1.0",
},
true,
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := imageExistsInDockerhub(tt.args.image)
if (err != nil) != tt.wantErr {
t.Errorf("imageExistsInDockerhub() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("imageExistsInDockerhub() got = %v, want %v", got, tt.want)
}
})
}
}
func TestGetRegistryFromImageName(t *testing.T) {
type args struct {
image string
}
tests := []struct {
name string
args args
want string
}{
{
"When the image is from the docker registry then it should return docker.io",
args{
image: "alpine:latest",
},
"docker.io",
},
{
"When the image is from the github registry then it should return ghcr.io",
args{
image: "ghcr.io/riskident/jelease:v0.6.3",
},
"ghcr.io",
},
{
"When the image is from a private registry then it should return the registry",
args{
image: "docker-riskident.2rioffice.com/platform/aow-reminder:1.1.0",
},
"docker-riskident.2rioffice.com",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := getRegistryFromImageName(tt.args.image); got != tt.want {
t.Errorf("getRegistryFromImageName() = %v, want %v", got, tt.want)
}
})
}
}