@@ -12,57 +12,77 @@ import (
12
12
"go.uber.org/mock/gomock"
13
13
)
14
14
15
- const (
16
- scheme = "xyz"
17
- bucket = "b"
18
- object = "o"
19
- )
20
-
21
- func TestSync (t * testing.T ) {
22
- ctrl := gomock .NewController (t )
23
- mockCron := synctesting .NewMockCron (ctrl )
24
- mockCron .EXPECT ().AddFunc (gomock .Any (), gomock .Any ()).DoAndReturn (func (spec string , cmd func ()) error {
25
- return nil
26
- })
27
- mockCron .EXPECT ().Start ().Times (1 )
28
-
29
- blobSync := & Sync {
30
- Bucket : scheme + "://" + bucket ,
31
- Object : object ,
32
- Cron : mockCron ,
33
- Logger : logger .NewLogger (nil , false ),
15
+ func TestBlobSync (t * testing.T ) {
16
+ tests := map [string ]struct {
17
+ scheme string
18
+ bucket string
19
+ object string
20
+ content string
21
+ convertedContent string
22
+ }{
23
+ "json file type" : {
24
+ scheme : "xyz" ,
25
+ bucket : "b" ,
26
+ object : "flags.json" ,
27
+ content : "{\" flags\" :{}}" ,
28
+ convertedContent : "{\" flags\" :{}}" ,
29
+ },
30
+ "yaml file type" : {
31
+ scheme : "xyz" ,
32
+ bucket : "b" ,
33
+ object : "flags.yaml" ,
34
+ content : "flags: []" ,
35
+ convertedContent : "{\" flags\" :[]}" ,
36
+ },
34
37
}
35
- blobMock := NewMockBlob (scheme , func () * Sync {
36
- return blobSync
37
- })
38
- blobSync .BlobURLMux = blobMock .URLMux ()
39
-
40
- ctx := context .Background ()
41
- dataSyncChan := make (chan sync.DataSync , 1 )
42
-
43
- config := "my-config"
44
- blobMock .AddObject (object , config )
45
38
46
- go func () {
47
- err := blobSync .Sync (ctx , dataSyncChan )
48
- if err != nil {
49
- log .Fatalf ("Error start sync: %s" , err .Error ())
50
- return
51
- }
52
- }()
53
-
54
- data := <- dataSyncChan // initial sync
55
- if data .FlagData != config {
56
- t .Errorf ("expected content: %s, but received content: %s" , config , data .FlagData )
39
+ for name , tt := range tests {
40
+ t .Run (name , func (t * testing.T ) {
41
+ ctrl := gomock .NewController (t )
42
+ mockCron := synctesting .NewMockCron (ctrl )
43
+ mockCron .EXPECT ().AddFunc (gomock .Any (), gomock .Any ()).DoAndReturn (func (spec string , cmd func ()) error {
44
+ return nil
45
+ })
46
+ mockCron .EXPECT ().Start ().Times (1 )
47
+
48
+ blobSync := & Sync {
49
+ Bucket : tt .scheme + "://" + tt .bucket ,
50
+ Object : tt .object ,
51
+ Cron : mockCron ,
52
+ Logger : logger .NewLogger (nil , false ),
53
+ }
54
+ blobMock := NewMockBlob (tt .scheme , func () * Sync {
55
+ return blobSync
56
+ })
57
+ blobSync .BlobURLMux = blobMock .URLMux ()
58
+
59
+ ctx := context .Background ()
60
+ dataSyncChan := make (chan sync.DataSync , 1 )
61
+
62
+ blobMock .AddObject (tt .object , tt .content )
63
+
64
+ go func () {
65
+ err := blobSync .Sync (ctx , dataSyncChan )
66
+ if err != nil {
67
+ log .Fatalf ("Error start sync: %s" , err .Error ())
68
+ return
69
+ }
70
+ }()
71
+
72
+ data := <- dataSyncChan // initial sync
73
+ if data .FlagData != tt .convertedContent {
74
+ t .Errorf ("expected content: %s, but received content: %s" , tt .convertedContent , data .FlagData )
75
+ }
76
+ tickWithConfigChange (t , mockCron , dataSyncChan , blobMock , tt .object , tt .convertedContent )
77
+ tickWithoutConfigChange (t , mockCron , dataSyncChan )
78
+ tickWithConfigChange (t , mockCron , dataSyncChan , blobMock , tt .object , tt .convertedContent )
79
+ tickWithoutConfigChange (t , mockCron , dataSyncChan )
80
+ tickWithoutConfigChange (t , mockCron , dataSyncChan )
81
+ })
57
82
}
58
- tickWithConfigChange (t , mockCron , dataSyncChan , blobMock , "new config" )
59
- tickWithoutConfigChange (t , mockCron , dataSyncChan )
60
- tickWithConfigChange (t , mockCron , dataSyncChan , blobMock , "new config 2" )
61
- tickWithoutConfigChange (t , mockCron , dataSyncChan )
62
- tickWithoutConfigChange (t , mockCron , dataSyncChan )
63
83
}
64
84
65
- func tickWithConfigChange (t * testing.T , mockCron * synctesting.MockCron , dataSyncChan chan sync.DataSync , blobMock * MockBlob , newConfig string ) {
85
+ func tickWithConfigChange (t * testing.T , mockCron * synctesting.MockCron , dataSyncChan chan sync.DataSync , blobMock * MockBlob , object string , newConfig string ) {
66
86
time .Sleep (1 * time .Millisecond ) // sleep so the new file has different modification date
67
87
blobMock .AddObject (object , newConfig )
68
88
mockCron .Tick ()
@@ -73,7 +93,7 @@ func tickWithConfigChange(t *testing.T, mockCron *synctesting.MockCron, dataSync
73
93
t .Errorf ("expected content: %s, but received content: %s" , newConfig , data .FlagData )
74
94
}
75
95
} else {
76
- t .Errorf ("data channel unexpecdly closed" )
96
+ t .Errorf ("data channel unexpectedly closed" )
77
97
}
78
98
default :
79
99
t .Errorf ("data channel has no expected update" )
@@ -87,13 +107,18 @@ func tickWithoutConfigChange(t *testing.T, mockCron *synctesting.MockCron, dataS
87
107
if ok {
88
108
t .Errorf ("unexpected update: %s" , data .FlagData )
89
109
} else {
90
- t .Errorf ("data channel unexpecdly closed" )
110
+ t .Errorf ("data channel unexpectedly closed" )
91
111
}
92
112
default :
93
113
}
94
114
}
95
115
96
116
func TestReSync (t * testing.T ) {
117
+ const (
118
+ scheme = "xyz"
119
+ bucket = "b"
120
+ object = "flags.json"
121
+ )
97
122
ctrl := gomock .NewController (t )
98
123
mockCron := synctesting .NewMockCron (ctrl )
99
124
0 commit comments