forked from drone/drone-kaniko
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kaniko_test.go
150 lines (146 loc) · 3.42 KB
/
kaniko_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
package kaniko
import (
"testing"
"github.com/google/go-cmp/cmp"
)
func TestBuild_labelsForTag(t *testing.T) {
tests := []struct {
name string
tag string
expandTags []string
}{
{
name: "semver",
tag: "v1.2.3",
expandTags: []string{"1", "1.2", "1.2.3"},
},
{
name: "no_patch",
tag: "v1.2",
expandTags: []string{"1", "1.2", "1.2.0"},
},
{
name: "only_major",
tag: "v1",
expandTags: []string{"1", "1.0", "1.0.0"},
},
{
name: "full_with_build",
tag: "v1.2.3+build-info",
expandTags: []string{"1+build-info", "1.2+build-info", "1.2.3+build-info"},
},
{
name: "build_with_underscores",
tag: "v1.2.3+linux_amd64",
expandTags: []string{"1+linux-amd64", "1.2+linux-amd64", "1.2.3+linux-amd64"},
},
{
name: "prerelease",
tag: "v1.2.3-rc1",
expandTags: []string{"1.2.3-rc1"},
},
{
name: "prerelease_with_build",
tag: "v1.2.3-rc1+bld",
expandTags: []string{"1.2.3-rc1+bld"},
},
{
name: "invalid_build",
tag: "v1+bld", // can only include build detail with all three elements
expandTags: []string{"v1+bld"},
},
{
name: "accidental_non_semver",
tag: "1.2.3",
expandTags: []string{"1", "1.2", "1.2.3"},
},
{
name: "non_semver",
tag: "latest",
expandTags: []string{"latest"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tags := Build{ExpandTag: true}.labelsForTag(tt.tag)
if got, want := tags, tt.expandTags; !cmp.Equal(got, want) {
t.Errorf("tagsFor(%q) = %q, want %q", tt.tag, got, want)
}
})
}
}
func TestBuild_AutoTags(t *testing.T) {
tests := []struct {
name string
repoBranch string
commitRef string
autoTagSuffix string
expectedTags []string
}{
{
name: "commit push",
repoBranch: "master",
commitRef: "refs/heads/master",
autoTagSuffix: "",
expectedTags: []string{"latest"},
},
{
name: "tag push",
repoBranch: "master",
commitRef: "refs/tags/v1.0.0",
autoTagSuffix: "",
expectedTags: []string{
"1",
"1.0",
"1.0.0",
},
},
{
name: "beta tag push",
repoBranch: "master",
commitRef: "refs/tags/v1.0.0-beta.1",
autoTagSuffix: "",
expectedTags: []string{
"1.0.0-beta.1",
},
},
{
name: "tag push with suffix",
repoBranch: "master",
commitRef: "refs/tags/v1.0.0",
autoTagSuffix: "linux-amd64",
expectedTags: []string{
"1-linux-amd64",
"1.0-linux-amd64",
"1.0.0-linux-amd64",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
b := Build{DroneCommitRef: tt.commitRef, DroneRepoBranch: tt.repoBranch, AutoTag: true}
if tt.autoTagSuffix != "" {
b.AutoTagSuffix = tt.autoTagSuffix
}
tags, err := b.AutoTags()
if err != nil {
t.Errorf("Unexpected err %q", err)
}
if got, want := tags, tt.expectedTags; !cmp.Equal(got, want) {
t.Errorf("auto detected tags = %q, wanted = %q", got, want)
}
})
}
t.Run("auto-tag cannot be enabled with user provided tags", func(t *testing.T) {
b := Build{
DroneCommitRef: "refs/tags/v1.0.0",
DroneRepoBranch: "master",
AutoTag: true,
Tags: []string{"v1"},
}
_, err := b.AutoTags()
if err == nil {
t.Errorf("Expect error for invalid flags")
}
})
}