@@ -2,7 +2,6 @@ package main
2
2
3
3
import (
4
4
"context"
5
- "errors"
6
5
"fmt"
7
6
"io/fs"
8
7
stdlog "log"
@@ -180,7 +179,7 @@ func (o AddFileOp) Apply() error {
180
179
}
181
180
182
181
func (o AddFileOp ) String () string {
183
- return fmt .Sprintf ("AddFileOp (%s, %d)" , Join (o .dir , o .name ), len (o .content ))
182
+ return fmt .Sprintf ("AddFile (%s, %d)" , Join (o .dir , o .name ), len (o .content ))
184
183
}
185
184
186
185
type UpdateFileOp struct {
@@ -198,7 +197,7 @@ func newUpdateFileOp(base string, project int64) Operation {
198
197
return SkipOp {}
199
198
}
200
199
201
- return AddFileOp {
200
+ return UpdateFileOp {
202
201
base : base ,
203
202
dir : dir ,
204
203
name : files [rand .Intn (len (files ))],
@@ -211,7 +210,7 @@ func (o UpdateFileOp) Apply() error {
211
210
}
212
211
213
212
func (o UpdateFileOp ) String () string {
214
- return fmt .Sprintf ("UpdateFileOp (%s, %d)" , Join (o .dir , o .name ), len (o .content ))
213
+ return fmt .Sprintf ("UpdateFile (%s, %d)" , Join (o .dir , o .name ), len (o .content ))
215
214
}
216
215
217
216
type AddDirOp struct {
@@ -277,7 +276,36 @@ func (o RemoveFileOp) Apply() error {
277
276
}
278
277
279
278
func (o RemoveFileOp ) String () string {
280
- return fmt .Sprintf ("RemoveFileOp(%s)" , Join (o .dir , o .name ))
279
+ return fmt .Sprintf ("RemoveFile(%s)" , Join (o .dir , o .name ))
280
+ }
281
+
282
+ type RemoveDirOp struct {
283
+ base string
284
+ dir string
285
+ name string
286
+ }
287
+
288
+ func newRemoveDirOp (base string , project int64 ) Operation {
289
+ dir := fmt .Sprint (project )
290
+ dirs := objectFilter (walkDir (Join (base , dir )), typeDirectory )
291
+
292
+ if len (dirs ) == 0 {
293
+ return SkipOp {}
294
+ }
295
+
296
+ return RemoveDirOp {
297
+ base : base ,
298
+ dir : dir ,
299
+ name : dirs [rand .Intn (len (dirs ))],
300
+ }
301
+ }
302
+
303
+ func (o RemoveDirOp ) Apply () error {
304
+ return os .RemoveAll (Join (o .base , o .dir , o .name ))
305
+ }
306
+
307
+ func (o RemoveDirOp ) String () string {
308
+ return fmt .Sprintf ("RemoveDir(%s)" , Join (o .dir , o .name ))
281
309
}
282
310
283
311
type AddSymlinkOp struct {
@@ -320,12 +348,12 @@ func (o AddSymlinkOp) Apply() error {
320
348
}
321
349
322
350
func (o AddSymlinkOp ) String () string {
323
- return fmt .Sprintf ("AddSymlinkOp (%s, %s)" , Join (o .dir , o .target ), Join (o .dir , o .name ))
351
+ return fmt .Sprintf ("AddSymlink (%s, %s)" , Join (o .dir , o .target ), Join (o .dir , o .name ))
324
352
}
325
353
326
354
type OpConstructor func (dir string , project int64 ) Operation
327
355
328
- var opConstructors = []OpConstructor {newAddFileOp , newUpdateFileOp , newAddDirOp , newRemoveFileOp , newAddSymlinkOp }
356
+ var opConstructors = []OpConstructor {newAddFileOp , newUpdateFileOp , newAddDirOp , newRemoveFileOp , newRemoveDirOp , newAddSymlinkOp }
329
357
330
358
func randomOperation (baseDir string , project int64 ) Operation {
331
359
var operation Operation = SkipOp {}
@@ -340,71 +368,97 @@ func randomOperation(baseDir string, project int64) Operation {
340
368
return operation
341
369
}
342
370
343
- func createDirs (projects int ) (string , string , string , string , error ) {
371
+ type Directories struct {
372
+ base string
373
+ reset string
374
+ previous string
375
+ step string
376
+ }
377
+
378
+ func createDirectories (projects int ) (* Directories , error ) {
344
379
var dirs []string
345
380
346
- for _ , name := range []string {"base" , "continue " , "reset " , "step" } {
381
+ for _ , name := range []string {"base" , "reset " , "previous " , "step" } {
347
382
dir , err := os .MkdirTemp ("" , fmt .Sprintf ("dl-ft-%s-" , name ))
348
383
if err != nil {
349
- return "" , "" , "" , "" , fmt .Errorf ("cannot create tmp dir: %w" , err )
384
+ return nil , fmt .Errorf ("cannot create tmp dir: %w" , err )
350
385
}
351
386
352
387
for projectIdx := 1 ; projectIdx <= projects ; projectIdx ++ {
353
388
err = os .MkdirAll (filepath .Join (dir , fmt .Sprint (projectIdx )), 0755 )
354
389
if err != nil {
355
- return "" , "" , "" , "" , fmt .Errorf ("cannot create project dir: %w" , err )
390
+ return nil , fmt .Errorf ("cannot create project dir: %w" , err )
356
391
}
357
392
}
358
393
dirs = append (dirs , dir )
359
394
}
360
395
361
- return dirs [0 ], dirs [1 ], dirs [2 ], dirs [3 ], nil
396
+ return & Directories {
397
+ base : dirs [0 ],
398
+ reset : dirs [1 ],
399
+ previous : dirs [2 ],
400
+ step : dirs [3 ],
401
+ }, nil
402
+ }
403
+
404
+ func (d * Directories ) Log (ctx context.Context ) {
405
+ logger .Info (ctx , "base" , zap .String ("path" , d .base ))
406
+ logger .Info (ctx , "reset" , zap .String ("path" , d .reset ))
407
+ logger .Info (ctx , "prev" , zap .String ("path" , d .previous ))
408
+ logger .Info (ctx , "step" , zap .String ("path" , d .step ))
362
409
}
363
410
364
- func runIteration (ctx context.Context , client * dlc.Client , project int64 , operation Operation , baseDir , continueDir , resetDir , stepDir string ) error {
411
+ func (d * Directories ) RemoveAll () {
412
+ os .RemoveAll (d .base )
413
+ os .RemoveAll (d .reset )
414
+ os .RemoveAll (d .previous )
415
+ os .RemoveAll (d .step )
416
+ }
417
+
418
+ func runIteration (ctx context.Context , client * dlc.Client , project int64 , operation Operation , dirs * Directories ) (int64 , error ) {
365
419
err := operation .Apply ()
366
420
if err != nil {
367
- return fmt .Errorf ("failed to apply operation %s: %w" , operation .String (), err )
421
+ return - 1 , fmt .Errorf ("failed to apply operation %s: %w" , operation .String (), err )
368
422
}
369
423
370
- version , _ , err := client .Update (ctx , project , projectDir (baseDir , project ))
424
+ version , _ , err := client .Update (ctx , project , projectDir (dirs . base , project ))
371
425
if err != nil {
372
- return fmt .Errorf ("failed to update project %d: %w" , project , err )
426
+ return - 1 , fmt .Errorf ("failed to update project %d: %w" , project , err )
373
427
}
374
428
375
- _ , _ , err = client .Rebuild (ctx , project , "" , nil , projectDir (continueDir , project ), "" )
429
+ os .RemoveAll (projectDir (dirs .reset , project ))
430
+ err = os .MkdirAll (projectDir (dirs .reset , project ), 0755 )
376
431
if err != nil {
377
- return fmt .Errorf ("failed to rebuild continue project %d : %w" , project , err )
432
+ return - 1 , fmt .Errorf ("failed to create reset dir %s : %w" , projectDir ( dirs . reset , project ) , err )
378
433
}
379
434
380
- os .RemoveAll (projectDir (resetDir , project ))
381
- err = os .MkdirAll (projectDir (resetDir , project ), 0755 )
435
+ _ , _ , err = client .Rebuild (ctx , project , "" , nil , projectDir (dirs .reset , project ), "" )
382
436
if err != nil {
383
- return fmt .Errorf ("failed to create reset dir %s : %w" , projectDir ( resetDir , project ) , err )
437
+ return - 1 , fmt .Errorf ("failed to rebuild from reset, project %d : %w" , project , err )
384
438
}
385
439
386
- _ , _ , err = client .Rebuild (ctx , project , "" , nil , projectDir (resetDir , project ), "" )
440
+ _ , _ , err = client .Rebuild (ctx , project , "" , nil , projectDir (dirs . previous , project ), "" )
387
441
if err != nil {
388
- return fmt .Errorf ("failed to rebuild reset project %d: %w" , project , err )
442
+ return - 1 , fmt .Errorf ("failed to rebuild from previous version, project %d: %w" , project , err )
389
443
}
390
444
391
- os .RemoveAll (projectDir (stepDir , project ))
392
- err = os .MkdirAll (projectDir (stepDir , project ), 0755 )
445
+ os .RemoveAll (projectDir (dirs . step , project ))
446
+ err = os .MkdirAll (projectDir (dirs . step , project ), 0755 )
393
447
if err != nil {
394
- return fmt .Errorf ("failed to create step dir %s: %w" , projectDir (stepDir , project ), err )
448
+ return - 1 , fmt .Errorf ("failed to create step dir %s: %w" , projectDir (dirs . step , project ), err )
395
449
}
396
450
397
451
stepVersion := int64 (rand .Intn (int (version )))
398
- _ , _ , err = client .Rebuild (ctx , project , "" , & stepVersion , projectDir (stepDir , project ), "" )
452
+ _ , _ , err = client .Rebuild (ctx , project , "" , & stepVersion , projectDir (dirs . step , project ), "" )
399
453
if err != nil {
400
- return fmt .Errorf ("failed to rebuild step project %d: %w" , project , err )
454
+ return - 1 , fmt .Errorf ("failed to rebuild step to version %v, project %d: %w" , stepVersion , project , err )
401
455
}
402
- _ , _ , err = client .Rebuild (ctx , project , "" , & version , projectDir (stepDir , project ), "" )
456
+ _ , _ , err = client .Rebuild (ctx , project , "" , & version , projectDir (dirs . step , project ), "" )
403
457
if err != nil {
404
- return fmt .Errorf ("failed to rebuild step project %d: %w" , project , err )
458
+ return - 1 , fmt .Errorf ("failed to rebuild step from version %v, project %d: %w" , stepVersion , project , err )
405
459
}
406
460
407
- return nil
461
+ return stepVersion , nil
408
462
}
409
463
410
464
type MatchError struct {
@@ -507,35 +561,35 @@ func logOpLog(ctx context.Context, opLog []Operation) {
507
561
}
508
562
}
509
563
510
- func verifyDirs (ctx context.Context , projects int , baseDir , continueDir , resetDir , stepDir string ) error {
564
+ func verifyDirs (ctx context.Context , projects int , dirs * Directories , stepVersion int64 ) error {
511
565
for projectIdx := 1 ; projectIdx <= projects ; projectIdx ++ {
512
566
project := int64 (projectIdx )
513
567
514
- matchErrors , err := compareDirs (project , projectDir (baseDir , project ), projectDir (resetDir , project ))
568
+ matchErrors , err := compareDirs (project , projectDir (dirs . base , project ), projectDir (dirs . reset , project ))
515
569
if err != nil {
516
570
return fmt .Errorf ("failed to compare base & reset dirs: %w" , err )
517
571
}
518
572
if len (matchErrors ) > 0 {
519
573
logMatchErrors (ctx , matchErrors )
520
- return errors . New ("reset directory match error" )
574
+ return fmt . Errorf ("reset directory match error, project %d" , project )
521
575
}
522
576
523
- matchErrors , err = compareDirs (project , projectDir (baseDir , project ), projectDir (continueDir , project ))
577
+ matchErrors , err = compareDirs (project , projectDir (dirs . base , project ), projectDir (dirs . previous , project ))
524
578
if err != nil {
525
- return fmt .Errorf ("failed to compare base & continue dirs: %w" , err )
579
+ return fmt .Errorf ("failed to compare base & previous dirs: %w" , err )
526
580
}
527
581
if len (matchErrors ) > 0 {
528
582
logMatchErrors (ctx , matchErrors )
529
- return errors . New ( "continue directory match error" )
583
+ return fmt . Errorf ( "previous version directory match error, project %d" , project )
530
584
}
531
585
532
- matchErrors , err = compareDirs (project , projectDir (baseDir , project ), projectDir (stepDir , project ))
586
+ matchErrors , err = compareDirs (project , projectDir (dirs . base , project ), projectDir (dirs . step , project ))
533
587
if err != nil {
534
588
return fmt .Errorf ("failed to compare base & step dirs: %w" , err )
535
589
}
536
590
if len (matchErrors ) > 0 {
537
591
logMatchErrors (ctx , matchErrors )
538
- return errors . New ("step directory match error" )
592
+ return fmt . Errorf ("step from verion %d directory match error, project %d" , stepVersion , project )
539
593
}
540
594
}
541
595
return nil
@@ -552,29 +606,27 @@ func fuzzTest(ctx context.Context, client *dlc.Client, projects, iterations int)
552
606
}
553
607
}
554
608
555
- baseDir , continueDir , resetDir , stepDir , err := createDirs (projects )
609
+ dirs , err := createDirectories (projects )
556
610
if err != nil {
557
611
return err
558
612
}
559
- defer os .RemoveAll (baseDir )
560
- defer os .RemoveAll (continueDir )
561
- defer os .RemoveAll (resetDir )
562
- defer os .RemoveAll (stepDir )
613
+ // defer dirs.RemoveAll()
614
+ dirs .Log (ctx )
563
615
564
616
var opLog []Operation
565
617
566
618
for iterIdx := 0 ; iterIdx < iterations ; iterIdx ++ {
567
619
project := int64 (rand .Intn (projects ) + 1 )
568
620
569
- operation := randomOperation (baseDir , project )
621
+ operation := randomOperation (dirs . base , project )
570
622
opLog = append (opLog , operation )
571
623
572
- err := runIteration (ctx , client , project , operation , baseDir , continueDir , resetDir , stepDir )
624
+ stepVersion , err := runIteration (ctx , client , project , operation , dirs )
573
625
if err != nil {
574
626
return fmt .Errorf ("failed to run iteration %d: %w" , iterIdx , err )
575
627
}
576
628
577
- err = verifyDirs (ctx , projects , baseDir , continueDir , resetDir , stepDir )
629
+ err = verifyDirs (ctx , projects , dirs , stepVersion )
578
630
if err != nil {
579
631
logOpLog (ctx , opLog )
580
632
return err
0 commit comments