@@ -55,9 +55,11 @@ type FakeClient struct {
55
55
}
56
56
57
57
type FakePublish struct {
58
- items []gw.ItemInput
59
- committed int
60
- id string
58
+ items []gw.ItemInput
59
+ committed int
60
+ commitmodes []string
61
+ frozen bool
62
+ id string
61
63
}
62
64
63
65
type BrokenPublish struct {
@@ -111,7 +113,7 @@ func (c *FakeClient) WhoAmI(context.Context) (map[string]interface{}, error) {
111
113
}
112
114
113
115
func (p * FakePublish ) AddItems (ctx context.Context , items []gw.ItemInput ) error {
114
- if p .committed != 0 {
116
+ if p .frozen {
115
117
return fmt .Errorf ("attempted to modify committed publish" )
116
118
}
117
119
p .items = append (p .items , items ... )
@@ -122,12 +124,16 @@ func (p *BrokenPublish) AddItems(_ context.Context, _ []gw.ItemInput) error {
122
124
return fmt .Errorf ("invalid publish" )
123
125
}
124
126
125
- func (p * BrokenPublish ) Commit (_ context.Context ) error {
127
+ func (p * BrokenPublish ) Commit (_ context.Context , _ string ) error {
126
128
return fmt .Errorf ("invalid publish" )
127
129
}
128
130
129
- func (p * FakePublish ) Commit (ctx context.Context ) error {
131
+ func (p * FakePublish ) Commit (ctx context.Context , mode string ) error {
132
+ if mode == "" || mode == "phase2" {
133
+ p .frozen = true
134
+ }
130
135
p .committed ++
136
+ p .commitmodes = append (p .commitmodes , mode )
131
137
return nil
132
138
}
133
139
@@ -148,72 +154,102 @@ func TestMainTypicalSync(t *testing.T) {
148
154
SetConfig (t , CONFIG )
149
155
ctrl := MockController (t )
150
156
151
- mockGw := gw .NewMockInterface (ctrl )
152
- ext .gw = mockGw
153
-
154
- client := FakeClient {blobs : make (map [string ]string )}
155
- mockGw .EXPECT ().NewClient (gomock .Any (), EnvMatcher {"best-env" }).Return (& client , nil )
156
-
157
- srcPath := path .Clean (wd + "/../../test/data/srctrees/just-files" )
158
-
159
- args := []string {
160
- "rsync" ,
161
- srcPath + "/" ,
162
- "exodus:/some/target" ,
163
- }
164
-
165
- got := Main (args )
166
-
167
- // It should complete successfully.
168
- if got != 0 {
169
- t .Error ("returned incorrect exit code" , got )
170
- }
171
-
172
- // Check paths of some blobs we expected to deal with.
173
- binPath := client .blobs ["c66f610d98b2c9fe0175a3e99ba64d7fc7de45046515ff325be56329a9347dd6" ]
174
- helloPath := client .blobs ["5891b5b522d5df086d0ff0b110fbd9d21bb4fc7163af34d08286a2e846f6be03" ]
175
-
176
- // It should have uploaded the binary from here
177
- if binPath != srcPath + "/subdir/some-binary" {
178
- t .Error ("binary uploaded from unexpected path" , binPath )
179
- }
180
-
181
- // For the hello file, since there were two copies, it's undefined which one of them
182
- // was used for the upload - but should be one of them.
183
- if helloPath != srcPath + "/hello-copy-one" && helloPath != srcPath + "/hello-copy-two" {
184
- t .Error ("hello uploaded from unexpected path" , helloPath )
185
- }
186
-
187
- // It should have created one publish.
188
- if len (client .publishes ) != 1 {
189
- t .Error ("expected to create 1 publish, instead created" , len (client .publishes ))
190
- }
191
-
192
- p := client .publishes [0 ]
193
-
194
- // Build up a URI => Key mapping of what was published
195
- itemMap := make (map [string ]string )
196
- for _ , item := range p .items {
197
- if _ , ok := itemMap [item .WebURI ]; ok {
198
- t .Error ("tried to publish this URI more than once:" , item .WebURI )
199
- }
200
- itemMap [item .WebURI ] = item .ObjectKey
201
- }
202
-
203
- // It should have been exactly this
204
- expectedItems := map [string ]string {
205
- "/some/target/subdir/some-binary" : "c66f610d98b2c9fe0175a3e99ba64d7fc7de45046515ff325be56329a9347dd6" ,
206
- "/some/target/hello-copy-one" : "5891b5b522d5df086d0ff0b110fbd9d21bb4fc7163af34d08286a2e846f6be03" ,
207
- "/some/target/hello-copy-two" : "5891b5b522d5df086d0ff0b110fbd9d21bb4fc7163af34d08286a2e846f6be03" ,
208
- }
209
-
210
- if ! reflect .DeepEqual (itemMap , expectedItems ) {
211
- t .Error ("did not publish expected items, published:" , itemMap )
212
- }
213
-
214
- // It should have committed the publish (once)
215
- if p .committed != 1 {
216
- t .Error ("expected to commit publish (once), instead p.committed ==" , p .committed )
157
+ tests := []struct {
158
+ name string
159
+ commitArg string
160
+ expectedCommits int
161
+ expectedCommitMode string
162
+ }{
163
+ {"typical" , "" , 1 , "" },
164
+
165
+ {"explicit autocommit" , "--exodus-commit=auto" , 1 , "" },
166
+
167
+ {"no commit" , "--exodus-commit=none" , 0 , "" },
168
+
169
+ {"commit specified mode" , "--exodus-commit=xyz" , 1 , "xyz" },
170
+ }
171
+
172
+ for _ , tt := range tests {
173
+ t .Run (tt .name , func (t * testing.T ) {
174
+ mockGw := gw .NewMockInterface (ctrl )
175
+ ext .gw = mockGw
176
+
177
+ client := FakeClient {blobs : make (map [string ]string )}
178
+ mockGw .EXPECT ().NewClient (gomock .Any (), EnvMatcher {"best-env" }).Return (& client , nil )
179
+
180
+ srcPath := path .Clean (wd + "/../../test/data/srctrees/just-files" )
181
+
182
+ args := []string {
183
+ "rsync" ,
184
+ srcPath + "/" ,
185
+ }
186
+
187
+ if tt .commitArg != "" {
188
+ args = append (args , tt .commitArg )
189
+ }
190
+ args = append (args , "exodus:/some/target" )
191
+
192
+ got := Main (args )
193
+
194
+ // It should complete successfully.
195
+ if got != 0 {
196
+ t .Error ("returned incorrect exit code" , got )
197
+ }
198
+
199
+ // Check paths of some blobs we expected to deal with.
200
+ binPath := client .blobs ["c66f610d98b2c9fe0175a3e99ba64d7fc7de45046515ff325be56329a9347dd6" ]
201
+ helloPath := client .blobs ["5891b5b522d5df086d0ff0b110fbd9d21bb4fc7163af34d08286a2e846f6be03" ]
202
+
203
+ // It should have uploaded the binary from here
204
+ if binPath != srcPath + "/subdir/some-binary" {
205
+ t .Error ("binary uploaded from unexpected path" , binPath )
206
+ }
207
+
208
+ // For the hello file, since there were two copies, it's undefined which one of them
209
+ // was used for the upload - but should be one of them.
210
+ if helloPath != srcPath + "/hello-copy-one" && helloPath != srcPath + "/hello-copy-two" {
211
+ t .Error ("hello uploaded from unexpected path" , helloPath )
212
+ }
213
+
214
+ // It should have created one publish.
215
+ if len (client .publishes ) != 1 {
216
+ t .Error ("expected to create 1 publish, instead created" , len (client .publishes ))
217
+ }
218
+
219
+ p := client .publishes [0 ]
220
+
221
+ // Build up a URI => Key mapping of what was published
222
+ itemMap := make (map [string ]string )
223
+ for _ , item := range p .items {
224
+ if _ , ok := itemMap [item .WebURI ]; ok {
225
+ t .Error ("tried to publish this URI more than once:" , item .WebURI )
226
+ }
227
+ itemMap [item .WebURI ] = item .ObjectKey
228
+ }
229
+
230
+ // It should have been exactly this
231
+ expectedItems := map [string ]string {
232
+ "/some/target/subdir/some-binary" : "c66f610d98b2c9fe0175a3e99ba64d7fc7de45046515ff325be56329a9347dd6" ,
233
+ "/some/target/hello-copy-one" : "5891b5b522d5df086d0ff0b110fbd9d21bb4fc7163af34d08286a2e846f6be03" ,
234
+ "/some/target/hello-copy-two" : "5891b5b522d5df086d0ff0b110fbd9d21bb4fc7163af34d08286a2e846f6be03" ,
235
+ }
236
+
237
+ if ! reflect .DeepEqual (itemMap , expectedItems ) {
238
+ t .Error ("did not publish expected items, published:" , itemMap )
239
+ }
240
+
241
+ // It should have committed (or not) as expected
242
+ if p .committed != tt .expectedCommits {
243
+ t .Error ("expected " , tt .expectedCommits , " commits, got " , p .committed )
244
+ }
245
+
246
+ // If a commit happened at all, it should have used the mode we expect.
247
+ if tt .expectedCommits != 0 {
248
+ if p .commitmodes [0 ] != tt .expectedCommitMode {
249
+ t .Error ("expected " , tt .expectedCommitMode , " commit mode, got " , p .commitmodes [0 ])
250
+ }
251
+ }
252
+ })
217
253
}
218
254
}
219
255
0 commit comments