@@ -18,10 +18,134 @@ package tag
18
18
19
19
import (
20
20
"testing"
21
+ "time"
21
22
23
+ "github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
22
24
"github.com/GoogleContainerTools/skaffold/testutil"
23
25
)
24
26
27
+ func TestTagTemplate_GenerateTag (t * testing.T ) {
28
+ aLocalTimeStamp := time .Date (2015 , 03 , 07 , 11 , 06 , 39 , 123456789 , time .Local )
29
+
30
+ dateTimeExample := & dateTimeTagger {
31
+ Format : "2006-01-02" ,
32
+ TimeZone : "UTC" ,
33
+ timeFn : func () time.Time { return aLocalTimeStamp },
34
+ }
35
+
36
+ envTemplateExample , _ := NewEnvTemplateTagger ("{{.FOO}}" )
37
+ invalidEnvTemplate , _ := NewEnvTemplateTagger ("{{.BAR}}" )
38
+ env := []string {"FOO=BAR" }
39
+
40
+ tagTemplateExample , _ := NewTemplateTagger ("" , nil )
41
+
42
+ tests := []struct {
43
+ description string
44
+ template string
45
+ customMap map [string ]Tagger
46
+ expected string
47
+ shouldErr bool
48
+ }{
49
+ {
50
+ description : "empty template" ,
51
+ },
52
+ {
53
+ description : "only text" ,
54
+ template : "foo-bar" ,
55
+ expected : "foo-bar" ,
56
+ },
57
+ {
58
+ description : "only component (dateTime) in template, providing more components than necessary" ,
59
+ template : "{{.FOO}}" ,
60
+ customMap : map [string ]Tagger {"FOO" : dateTimeExample , "BAR" : envTemplateExample },
61
+ expected : "2015-03-07" ,
62
+ },
63
+ {
64
+ description : "envTemplate and sha256 as components" ,
65
+ template : "foo-{{.FOO}}-{{.BAR}}" ,
66
+ customMap : map [string ]Tagger {"FOO" : envTemplateExample , "BAR" : & ChecksumTagger {}},
67
+ expected : "foo-BAR-latest" ,
68
+ },
69
+ {
70
+ description : "using tagTemplate as a component" ,
71
+ template : "{{.FOO}}" ,
72
+ customMap : map [string ]Tagger {"FOO" : tagTemplateExample },
73
+ shouldErr : true ,
74
+ },
75
+ {
76
+ description : "faulty component, envTemplate has undefined references" ,
77
+ template : "{{.FOO}}" ,
78
+ customMap : map [string ]Tagger {"FOO" : invalidEnvTemplate },
79
+ shouldErr : true ,
80
+ },
81
+ {
82
+ description : "missing required components" ,
83
+ template : "{{.FOO}}" ,
84
+ shouldErr : true ,
85
+ },
86
+ {
87
+ description : "default component name SHA" ,
88
+ template : "{{.SHA}}" ,
89
+ expected : "latest" ,
90
+ },
91
+ {
92
+ description : "override default components" ,
93
+ template : "{{.GIT}}-{{.DATE}}-{{.SHA}}" ,
94
+ customMap : map [string ]Tagger {"GIT" : dateTimeExample , "DATE" : envTemplateExample , "SHA" : dateTimeExample },
95
+ expected : "2015-03-07-BAR-2015-03-07" ,
96
+ },
97
+ }
98
+ for _ , test := range tests {
99
+ testutil .Run (t , test .description , func (t * testutil.T ) {
100
+ t .Override (& util .OSEnviron , func () []string { return env })
101
+
102
+ c , err := NewTemplateTagger (test .template , test .customMap )
103
+
104
+ t .CheckNoError (err )
105
+
106
+ tag , err := c .GenerateTag ("." , "test" )
107
+
108
+ t .CheckErrorAndDeepEqual (test .shouldErr , err , test .expected , tag )
109
+ })
110
+ }
111
+ }
112
+
113
+ func TestTagTemplate_NewTemplateTagger (t * testing.T ) {
114
+ tests := []struct {
115
+ description string
116
+ template string
117
+ customMap map [string ]Tagger
118
+ shouldErr bool
119
+ }{
120
+ {
121
+ description : "valid template with nil map" ,
122
+ template : "{{.FOO}}" ,
123
+ },
124
+ {
125
+ description : "valid template with atleast one mapping" ,
126
+ template : "{{.FOO}}" ,
127
+ customMap : map [string ]Tagger {"FOO" : & ChecksumTagger {}},
128
+ },
129
+ {
130
+ description : "invalid template with nil mapping" ,
131
+ template : "{{.FOO" ,
132
+ shouldErr : true ,
133
+ },
134
+ {
135
+ description : "invalid template with atleast one mapping" ,
136
+ template : "{{.FOO" ,
137
+ customMap : map [string ]Tagger {"FOO" : & ChecksumTagger {}},
138
+ shouldErr : true ,
139
+ },
140
+ }
141
+ for _ , test := range tests {
142
+ testutil .Run (t , test .description , func (t * testutil.T ) {
143
+ _ , err := NewTemplateTagger (test .template , test .customMap )
144
+ t .CheckError (test .shouldErr , err )
145
+ })
146
+ }
147
+ }
148
+
25
149
func TestTagTemplate_ExecuteTagTemplate (t * testing.T ) {
26
150
tests := []struct {
27
151
description string
0 commit comments