@@ -3,13 +3,14 @@ package extent
33import (
44 "reflect"
55 "testing"
6+ "time"
67
78 "github.com/AlekSi/pointer"
89 "github.com/davecgh/go-spew/spew"
910)
1011
1112func TestUnmarshalYAML (t * testing.T ) {
12- t .Run ("unmarhsal the required_context field" , func (tt * testing.T ) {
13+ t .Run ("Unmarhsal the required_context field" , func (tt * testing.T ) {
1314 s := `
1415envs:
1516 - name: dev
3738 }
3839 })
3940
40- t .Run ("unmarshal auto_merge: false " , func (tt * testing.T ) {
41+ t .Run ("Unmarshal ' auto_merge: false' " , func (tt * testing.T ) {
4142 s := `
4243envs:
4344 - name: dev
6465 }
6566 })
6667
67- t .Run ("unmarshal auto_merge: true" , func (tt * testing.T ) {
68+ t .Run ("Unmarshal ' auto_merge: true' " , func (tt * testing.T ) {
6869 s := `
6970envs:
7071 - name: dev
9293}
9394
9495func TestConfig_Eval (t * testing.T ) {
95- t .Run ("Evaluate the configuration ." , func (t * testing.T ) {
96+ t .Run ("Umarshal the task with the variable template ." , func (t * testing.T ) {
9697 s := `
9798envs:
9899 - name: dev
@@ -122,7 +123,7 @@ envs:
122123 }
123124 })
124125
125- t .Run ("Evaluate the configuration with the regexp." , func (t * testing.T ) {
126+ t .Run ("Unmarshal the deployable_ref field with a regexp." , func (t * testing.T ) {
126127 s := `
127128envs:
128129 - name: dev
@@ -153,6 +154,38 @@ envs:
153154 t .Errorf ("Config = %v expected %v" , spew .Sdump (c ), spew .Sdump (e ))
154155 }
155156 })
157+
158+ t .Run ("Unmarshal the freeze_windows field" , func (t * testing.T ) {
159+ s := `
160+ envs:
161+ - name: dev
162+ freeze_windows:
163+ - start: "55 23 * * *"
164+ duration: "10m"`
165+
166+ c := & Config {}
167+ if err := UnmarshalYAML ([]byte (s ), c ); err != nil {
168+ t .Fatalf ("Failed to parse the configuration file: %v" , err )
169+ }
170+
171+ e := & Config {
172+ Envs : []* Env {
173+ {
174+ Name : "dev" ,
175+ FreezeWindows : []FreezeWindow {
176+ {
177+ Start : "55 23 * * *" ,
178+ Duration : "10m" ,
179+ },
180+ },
181+ },
182+ },
183+ source : []byte (s ),
184+ }
185+ if ! reflect .DeepEqual (c , e ) {
186+ t .Errorf ("Config = %v expected %v" , spew .Sdump (c ), spew .Sdump (e ))
187+ }
188+ })
156189}
157190
158191func TestEnv_IsProductionEnvironment (t * testing.T ) {
@@ -224,3 +257,77 @@ func TestEnv_IsDeployableRef(t *testing.T) {
224257 }
225258 })
226259}
260+
261+ func TestEnv_IsFreezed (t * testing.T ) {
262+ t .Run ("Return true when the time is in the window" , func (t * testing.T ) {
263+ runs := []struct {
264+ t time.Time
265+ e * Env
266+ want bool
267+ }{
268+ {
269+ t : time .Date (2012 , 12 , 1 , 23 , 55 , 10 , 0 , time .UTC ),
270+ e : & Env {
271+ FreezeWindows : []FreezeWindow {
272+ {
273+ Start : "55 23 * Dec *" ,
274+ Duration : "10m" ,
275+ },
276+ },
277+ },
278+ want : true ,
279+ },
280+ {
281+ t : time .Date (2012 , 1 , 1 , 0 , 3 , 0 , 0 , time .UTC ),
282+ e : & Env {
283+ FreezeWindows : []FreezeWindow {
284+ {
285+ Start : "55 23 * Dec *" ,
286+ Duration : "10m" ,
287+ },
288+ },
289+ },
290+ want : true ,
291+ },
292+ }
293+ e := & Env {
294+ FreezeWindows : []FreezeWindow {
295+ {
296+ Start : "55 23 * Dec *" ,
297+ Duration : "10m" ,
298+ },
299+ },
300+ }
301+
302+ for _ , r := range runs {
303+ freezed , err := e .IsFreezed (r .t )
304+ if err != nil {
305+ t .Fatalf ("IsFreezed returns an error: %s" , err )
306+ }
307+
308+ if freezed != r .want {
309+ t .Fatalf ("IsFreezed = %v, wanted %v" , freezed , r .want )
310+ }
311+ }
312+ })
313+
314+ t .Run ("Return false when the time is out of the window" , func (t * testing.T ) {
315+ e := & Env {
316+ FreezeWindows : []FreezeWindow {
317+ {
318+ Start : "55 23 * Dec *" ,
319+ Duration : "10m" ,
320+ },
321+ },
322+ }
323+
324+ freezed , err := e .IsFreezed (time .Date (2012 , 1 , 1 , 0 , 10 , 0 , 0 , time .UTC ))
325+ if err != nil {
326+ t .Fatalf ("IsFreezed returns an error: %s" , err )
327+ }
328+
329+ if freezed != false {
330+ t .Fatalf ("IsFreezed = %v, wanted %v" , freezed , false )
331+ }
332+ })
333+ }
0 commit comments