Skip to content

Commit d70d361

Browse files
authored
Merge pull request #447 from databacker/simplify-integration-tests
Simplify integration tests
2 parents 76a1b04 + 68ac091 commit d70d361

File tree

8 files changed

+54
-33
lines changed

8 files changed

+54
-33
lines changed

.github/workflows/ci.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,3 @@ jobs:
8484
platforms: linux/amd64,linux/arm64
8585
tags: |
8686
${{env.IMAGE_NAME}}:${{github.sha}}
87-
- name: test
88-
run: make test

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ push: build
3535
docker push $(TARGET)
3636

3737
integration_test:
38-
go test -v ./test --tags=integration
38+
TEST_INTEGRATION=true go test -v ./test
3939

4040
integration_test_debug:
41-
dlv --wd=./test test ./test --build-flags="-tags=integration"
41+
TEST_INTEGRATION=true dlv --wd=./test test ./test
4242

4343
vet:
4444
go vet --tags=integration ./...

test/backup_log_containers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//go:build integration && !logs
1+
//go:build !logs
22

33
package test
44

test/backup_nolog_containers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//go:build integration && logs
1+
//go:build logs
22

33
package test
44

test/backup_teardown_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
//go:build integration
2-
31
package test
42

53
import "fmt"

test/backup_test.go

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
//go:build integration
2-
31
package test
42

53
import (
@@ -33,6 +31,7 @@ import (
3331

3432
"github.com/docker/docker/api/types"
3533
"github.com/docker/docker/api/types/container"
34+
imagetypes "github.com/docker/docker/api/types/image"
3635
"github.com/docker/docker/client"
3736
"github.com/docker/docker/pkg/stdcopy"
3837
"github.com/docker/go-connections/nat"
@@ -84,11 +83,11 @@ type backupTarget struct {
8483
}
8584

8685
type testOptions struct {
87-
compact bool
86+
compact bool //nolint:unused // used in the future
8887
targets []string
8988
dc *dockerContext
9089
base string
91-
prePost bool
90+
prePost bool //nolint:unused // used in the future
9291
backupData []byte
9392
mysql containerPort
9493
smb containerPort
@@ -335,8 +334,8 @@ func (d *dockerContext) makeSMB(smbImage string) error {
335334
if err != nil {
336335
return fmt.Errorf("failed to build smb image: %w", err)
337336
}
338-
io.Copy(os.Stdout, resp.Body)
339-
resp.Body.Close()
337+
_, _ = io.Copy(os.Stdout, resp.Body)
338+
_ = resp.Body.Close()
340339

341340
return nil
342341
}
@@ -442,7 +441,9 @@ DELIMITER ;
442441
if err != nil {
443442
return err
444443
}
445-
defer fCompact.Close()
444+
defer func() {
445+
_ = fCompact.Close()
446+
}()
446447

447448
_, _ = stdcopy.StdCopy(fCompact, &bufe, attachResp.Reader)
448449

@@ -463,12 +464,15 @@ DELIMITER ;
463464
if err != nil {
464465
return err
465466
}
466-
defer f.Close()
467+
defer func() {
468+
_ = f.Close()
469+
}()
467470

468471
_, _ = stdcopy.StdCopy(f, &bufe, attachResp.Reader)
469472
return err
470473
}
471474

475+
//nolint:unused // useful in the future
472476
func (d *dockerContext) logContainers(cids ...string) error {
473477
ctx := context.Background()
474478
for _, cid := range cids {
@@ -480,7 +484,9 @@ func (d *dockerContext) logContainers(cids ...string) error {
480484
if err != nil {
481485
return fmt.Errorf("failed to get logs for container %s: %w", cid, err)
482486
}
483-
defer logs.Close()
487+
defer func() {
488+
_ = logs.Close()
489+
}()
484490

485491
if _, err := io.Copy(os.Stdout, logs); err != nil {
486492
return fmt.Errorf("failed to stream logs for container %s: %w", cid, err)
@@ -560,12 +566,12 @@ log_queries_not_using_indexes = 1
560566
return mysql, smb, s3url, s3backend, fmt.Errorf("failed to create mysql log directory: %v", err)
561567
}
562568
// ensure we have mysql image
563-
resp, err := dc.cli.ImagePull(context.Background(), mysqlImage, types.ImagePullOptions{})
569+
resp, err := dc.cli.ImagePull(context.Background(), mysqlImage, imagetypes.PullOptions{})
564570
if err != nil {
565571
return mysql, smb, s3url, s3backend, fmt.Errorf("failed to pull mysql image: %v", err)
566572
}
567-
io.Copy(os.Stdout, resp)
568-
resp.Close()
573+
_, _ = io.Copy(os.Stdout, resp)
574+
_ = resp.Close()
569575
mysqlCID, mysqlPort, err := dc.startContainer(mysqlImage, "mysql", "3306/tcp", []string{fmt.Sprintf("%s:/etc/mysql/conf.d/log.conf:ro", confFile), fmt.Sprintf("%s:/var/log/mysql", logDir)}, nil, []string{
570576
fmt.Sprintf("MYSQL_ROOT_PASSWORD=%s", mysqlRootPass),
571577
"MYSQL_DATABASE=tester",
@@ -594,8 +600,7 @@ func backupTargetsToStorage(targets []backupTarget, base, s3 string) ([]storage.
594600
var targetVals []storage.Storage
595601
// all targets should have the same sequence, with varying subsequence, so take any one
596602
for _, tgt := range targets {
597-
tg := tgt.String()
598-
tg = tgt.WithPrefix(base)
603+
tg := tgt.WithPrefix(base)
599604
localPath := tgt.LocalPath()
600605
if err := os.MkdirAll(localPath, 0o755); err != nil {
601606
return nil, fmt.Errorf("failed to create local path %s: %v", localPath, err)
@@ -753,13 +758,13 @@ func checkDumpTest(t *testing.T, base string, expected []byte, s3backend gofakes
753758
if _, err := os.Stat(postBackupOutFile); err != nil {
754759
t.Errorf("%s script didn't run, output file doesn't exist", msg)
755760
}
756-
os.RemoveAll(postBackupOutFile)
761+
_ = os.RemoveAll(postBackupOutFile)
757762

758763
msg = fmt.Sprintf("%s %s pre-backup", id, target.String())
759764
if _, err := os.Stat(preBackupOutFile); err != nil {
760765
t.Errorf("%s script didn't run, output file doesn't exist", msg)
761766
}
762-
os.RemoveAll(preBackupOutFile)
767+
_ = os.RemoveAll(preBackupOutFile)
763768
}
764769
p := target.Path()
765770
if p == "" {
@@ -805,8 +810,6 @@ func checkDumpTest(t *testing.T, base string, expected []byte, s3backend gofakes
805810
// to each format
806811
assert.Equal(t, expectedFiltered, string(b), "%s tar contents do not match actual dump", id)
807812
}
808-
809-
return
810813
}
811814

812815
// gunzipUntarScanFilter is a helper function to extract the actual data from a backup
@@ -817,7 +820,9 @@ func gunzipUntarScanFilter(r io.Reader) (b []byte, err error) {
817820
if err != nil {
818821
return nil, err
819822
}
820-
defer gr.Close()
823+
defer func() {
824+
_ = gr.Close()
825+
}()
821826
tr := tar.NewReader(gr)
822827
if _, err := tr.Next(); err != nil {
823828
return nil, err
@@ -886,12 +891,12 @@ func populatePrePost(base string, targets []backupTarget) (err error) {
886891
}
887892

888893
func startDatabase(dc *dockerContext, baseDir, image, name string) (containerPort, error) {
889-
resp, err := dc.cli.ImagePull(context.Background(), image, types.ImagePullOptions{})
894+
resp, err := dc.cli.ImagePull(context.Background(), image, imagetypes.PullOptions{})
890895
if err != nil {
891896
return containerPort{}, fmt.Errorf("failed to pull mysql image: %v", err)
892897
}
893-
io.Copy(os.Stdout, resp)
894-
resp.Close()
898+
_, _ = io.Copy(os.Stdout, resp)
899+
_ = resp.Close()
895900

896901
// start the mysql container; configure it for lots of debug logging, in case we need it
897902
mysqlConf := `
@@ -931,6 +936,7 @@ log_queries_not_using_indexes = 1
931936
}
932937

933938
func TestIntegration(t *testing.T) {
939+
CheckSkipIntegration(t, "integration")
934940
syscall.Umask(0)
935941
dc, err := getDockerContext()
936942
if err != nil {

test/package_noteardown_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
//go:build integration && keepcontainers
1+
//go:build keepcontainers
22

33
package test
44

5-
import "fmt"
6-
75
func teardown(dc *dockerContext, cids ...string) error {
86
return nil
97
}

test/util_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package test
2+
3+
import (
4+
"os"
5+
"testing"
6+
)
7+
8+
const (
9+
integrationTestEnvVar = "TEST_INTEGRATION"
10+
)
11+
12+
func IsIntegration() bool {
13+
val, isIntegration := os.LookupEnv(integrationTestEnvVar)
14+
return isIntegration && val != "false" && val != ""
15+
}
16+
17+
func CheckSkipIntegration(t *testing.T, name string) {
18+
if !IsIntegration() {
19+
t.Skipf("Skipping integration test %s, set %s to run", integrationTestEnvVar, name)
20+
}
21+
}

0 commit comments

Comments
 (0)