Skip to content

Commit 416e08f

Browse files
committed
restructure test setup
Signed-off-by: Avi Deitcher <[email protected]>
1 parent 5e49a3d commit 416e08f

File tree

1 file changed

+31
-69
lines changed

1 file changed

+31
-69
lines changed

test/backup_test.go

Lines changed: 31 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ func (d *dockerContext) makeSMB(smbImage string) error {
338338
return nil
339339
}
340340

341-
func (d *dockerContext) createBackupFile(mysqlCID, mysqlUser, mysqlPass, outfile, compactOutfile, multiLineInsertOutfile string) error {
341+
func (d *dockerContext) createBackupFiles(mysqlCID, mysqlUser, mysqlPass string, outfilesToCommands map[string][]string) error {
342342
ctx := context.Background()
343343

344344
// Create and populate the table
@@ -425,75 +425,32 @@ DELIMITER ;
425425
}
426426

427427
// Dump the database multiple times, each with different options
428-
// - compact
429-
mysqlDumpCompactCmd := []string{"mysqldump", "-hlocalhost", "--protocol=tcp", "--complete-insert", fmt.Sprintf("-u%s", mysqlUser), fmt.Sprintf("-p%s", mysqlPass), "--compact", "--databases", "--triggers", "--routines", "tester"}
430-
attachResp, exitCode, err = d.execInContainer(ctx, mysqlCID, mysqlDumpCompactCmd)
431-
if err != nil {
432-
return fmt.Errorf("failed to attach to exec: %w", err)
433-
}
434-
defer attachResp.Close()
435-
if exitCode != 0 {
436-
return fmt.Errorf("failed to dump database: %w", err)
437-
}
438-
439-
fCompact, err := os.Create(compactOutfile)
440-
if err != nil {
441-
return err
442-
}
443-
defer func() {
444-
_ = fCompact.Close()
445-
}()
446-
447-
_, _ = stdcopy.StdCopy(fCompact, &bufe, attachResp.Reader)
448-
449-
bufo.Reset()
450-
bufe.Reset()
451-
452-
// - regular
453-
mysqlDumpCmd := []string{"mysqldump", "-hlocalhost", "--protocol=tcp", "--complete-insert", fmt.Sprintf("-u%s", mysqlUser), fmt.Sprintf("-p%s", mysqlPass), "--databases", "--triggers", "--routines", "tester"}
454-
attachResp, exitCode, err = d.execInContainer(ctx, mysqlCID, mysqlDumpCmd)
455-
if err != nil {
456-
return fmt.Errorf("failed to attach to exec: %w", err)
457-
}
458-
defer attachResp.Close()
459-
if exitCode != 0 {
460-
return fmt.Errorf("failed to dump database: %w", err)
461-
}
462-
463-
f, err := os.Create(outfile)
464-
if err != nil {
465-
return err
466-
}
467-
defer func() {
468-
_ = f.Close()
469-
}()
428+
for outfile, cmdOpts := range outfilesToCommands {
429+
mysqlDumpCmd := []string{"mysqldump", "-hlocalhost", "--protocol=tcp", "--complete-insert", fmt.Sprintf("-u%s", mysqlUser), fmt.Sprintf("-p%s", mysqlPass), "--databases", "--triggers", "--routines"}
430+
mysqlDumpCmd = append(mysqlDumpCmd, cmdOpts...)
431+
mysqlDumpCmd = append(mysqlDumpCmd, "tester")
432+
attachResp, exitCode, err = d.execInContainer(ctx, mysqlCID, mysqlDumpCmd)
433+
if err != nil {
434+
return fmt.Errorf("failed to attach to exec: %w", err)
435+
}
436+
defer attachResp.Close()
437+
if exitCode != 0 {
438+
return fmt.Errorf("failed to dump database: %w", err)
439+
}
470440

471-
_, _ = stdcopy.StdCopy(f, &bufe, attachResp.Reader)
472-
bufo.Reset()
473-
bufe.Reset()
441+
f, err := os.Create(outfile)
442+
if err != nil {
443+
return err
444+
}
445+
defer func() {
446+
_ = f.Close()
447+
}()
474448

475-
// - skip-extended
476-
mysqlDumpSkipExtendedCmd := []string{"mysqldump", "-hlocalhost", "--protocol=tcp", "--complete-insert", fmt.Sprintf("-u%s", mysqlUser), fmt.Sprintf("-p%s", mysqlPass), "--skip-extended-insert", "--databases", "--triggers", "--routines", "tester"}
477-
attachResp, exitCode, err = d.execInContainer(ctx, mysqlCID, mysqlDumpSkipExtendedCmd)
478-
if err != nil {
479-
return fmt.Errorf("failed to attach to exec: %w", err)
480-
}
481-
defer attachResp.Close()
482-
if exitCode != 0 {
483-
return fmt.Errorf("failed to dump database: %w", err)
484-
}
449+
_, _ = stdcopy.StdCopy(f, &bufe, attachResp.Reader)
485450

486-
fSkipExtended, err := os.Create(multiLineInsertOutfile)
487-
if err != nil {
488-
return err
451+
bufo.Reset()
452+
bufe.Reset()
489453
}
490-
defer func() {
491-
_ = fSkipExtended.Close()
492-
}()
493-
494-
_, _ = stdcopy.StdCopy(fSkipExtended, &bufe, attachResp.Reader)
495-
bufo.Reset()
496-
bufe.Reset()
497454

498455
return err
499456
}
@@ -550,7 +507,7 @@ func (d *dockerContext) rmContainers(cids ...string) error {
550507
// - check that the backup now is there in the right format
551508
// - clear the target
552509

553-
func setup(dc *dockerContext, base, backupFile, compactBackupFile, skipExtendedInsertBackupFile string) (mysql, smb containerPort, s3url string, s3backend gofakes3.Backend, err error) {
510+
func setup(dc *dockerContext, base string, outfilesToCmdOpts map[string][]string) (mysql, smb containerPort, s3url string, s3backend gofakes3.Backend, err error) {
554511
if err := dc.makeSMB(smbImage); err != nil {
555512
return mysql, smb, s3url, s3backend, fmt.Errorf("failed to build smb image: %v", err)
556513
}
@@ -583,7 +540,7 @@ func setup(dc *dockerContext, base, backupFile, compactBackupFile, skipExtendedI
583540

584541
// create the backup file
585542
log.Debugf("Creating backup files")
586-
if err := dc.createBackupFile(mysql.id, mysqlUser, mysqlPass, backupFile, compactBackupFile, skipExtendedInsertBackupFile); err != nil {
543+
if err := dc.createBackupFiles(mysql.id, mysqlUser, mysqlPass, outfilesToCmdOpts); err != nil {
587544
return mysql, smb, s3url, s3backend, fmt.Errorf("failed to create backup file: %v", err)
588545
}
589546
return
@@ -1027,7 +984,12 @@ func TestIntegration(t *testing.T) {
1027984
backupFile := filepath.Join(base, "backup.sql")
1028985
compactBackupFile := filepath.Join(base, "backup-compact.sql")
1029986
skipExtendedInsertBackupFile := filepath.Join(base, "backup-skip-extended.sql")
1030-
if mysql, smb, s3, s3backend, err = setup(dc, base, backupFile, compactBackupFile, skipExtendedInsertBackupFile); err != nil {
987+
outfilesToCmdOpts := map[string][]string{
988+
backupFile: nil,
989+
compactBackupFile: {"--compact"},
990+
skipExtendedInsertBackupFile: {"--skip-extended-insert"},
991+
}
992+
if mysql, smb, s3, s3backend, err = setup(dc, base, outfilesToCmdOpts); err != nil {
1031993
t.Fatalf("failed to setup test: %v", err)
1032994
}
1033995
backupData, err := os.ReadFile(backupFile)

0 commit comments

Comments
 (0)