Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use defer instead of os.Exit(m.Run()) #493

Open
wants to merge 1 commit into
base: v3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 7 additions & 6 deletions README.md
Expand Up @@ -100,14 +100,15 @@ func TestMain(m *testing.M) {
log.Fatalf("Could not connect to database: %s", err)
}

code := m.Run()
// as of go1.15 testing.M returns the exit code of m.Run(), so it is safe to use defer here
defer func() {
if err := pool.Purge(resource); err != nil {
log.Fatalf("Could not purge resource: %s", err)
}

// You can't defer this because os.Exit doesn't care for defer
if err := pool.Purge(resource); err != nil {
log.Fatalf("Could not purge resource: %s", err)
}
}()

os.Exit(code)
m.Run()
}

func TestSomething(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion docker/auth.go
@@ -1,4 +1,4 @@
// Copyright © 2023 Ory Corp
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0

// Copyright 2015 go-dockerclient authors. All rights reserved.
Expand Down
2 changes: 1 addition & 1 deletion docker/change.go
@@ -1,4 +1,4 @@
// Copyright © 2023 Ory Corp
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0

// Copyright 2014 go-dockerclient authors. All rights reserved.
Expand Down
2 changes: 1 addition & 1 deletion docker/client.go
@@ -1,4 +1,4 @@
// Copyright © 2023 Ory Corp
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0

// Copyright 2013 go-dockerclient authors. All rights reserved.
Expand Down
2 changes: 1 addition & 1 deletion docker/client_unix.go
@@ -1,4 +1,4 @@
// Copyright © 2023 Ory Corp
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0

// Copyright 2016 go-dockerclient authors. All rights reserved.
Expand Down
2 changes: 1 addition & 1 deletion docker/client_windows.go
@@ -1,4 +1,4 @@
// Copyright © 2023 Ory Corp
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0

// Copyright 2016 go-dockerclient authors. All rights reserved.
Expand Down
2 changes: 1 addition & 1 deletion docker/container.go
@@ -1,4 +1,4 @@
// Copyright © 2023 Ory Corp
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0

// Copyright 2013 go-dockerclient authors. All rights reserved.
Expand Down
2 changes: 1 addition & 1 deletion docker/distribution.go
@@ -1,4 +1,4 @@
// Copyright © 2023 Ory Corp
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0

// Copyright 2017 go-dockerclient authors. All rights reserved.
Expand Down
2 changes: 1 addition & 1 deletion docker/env.go
@@ -1,4 +1,4 @@
// Copyright © 2023 Ory Corp
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0

// Copyright 2014 Docker authors. All rights reserved.
Expand Down
2 changes: 1 addition & 1 deletion docker/event.go
@@ -1,4 +1,4 @@
// Copyright © 2023 Ory Corp
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0

// Copyright 2014 go-dockerclient authors. All rights reserved.
Expand Down
2 changes: 1 addition & 1 deletion docker/exec.go
@@ -1,4 +1,4 @@
// Copyright © 2023 Ory Corp
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0

// Copyright 2014 go-dockerclient authors. All rights reserved.
Expand Down
2 changes: 1 addition & 1 deletion docker/image.go
@@ -1,4 +1,4 @@
// Copyright © 2023 Ory Corp
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0

// Copyright 2013 go-dockerclient authors. All rights reserved.
Expand Down
4 changes: 2 additions & 2 deletions docker/misc.go
@@ -1,4 +1,4 @@
// Copyright © 2023 Ory Corp
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0

// Copyright 2013 go-dockerclient authors. All rights reserved.
Expand Down Expand Up @@ -85,7 +85,7 @@ type DockerInfo struct {
InitBinary string
DefaultRuntime string
LiveRestoreEnabled bool
//Swarm swarm.Info
// Swarm swarm.Info
}

// PluginsInfo is a struct with the plugins registered with the docker daemon
Expand Down
2 changes: 1 addition & 1 deletion docker/network.go
@@ -1,4 +1,4 @@
// Copyright © 2023 Ory Corp
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0

// Copyright 2015 go-dockerclient authors. All rights reserved.
Expand Down
2 changes: 1 addition & 1 deletion docker/opts/env.go
@@ -1,4 +1,4 @@
// Copyright © 2023 Ory Corp
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0

package opts
Expand Down
2 changes: 1 addition & 1 deletion docker/opts/hosts.go
@@ -1,4 +1,4 @@
// Copyright © 2023 Ory Corp
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0

package opts
Expand Down
2 changes: 1 addition & 1 deletion docker/opts/hosts_unix.go
@@ -1,4 +1,4 @@
// Copyright © 2023 Ory Corp
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0

//go:build !windows
Expand Down
2 changes: 1 addition & 1 deletion docker/opts/hosts_windows.go
@@ -1,4 +1,4 @@
// Copyright © 2023 Ory Corp
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0

package opts
Expand Down
2 changes: 1 addition & 1 deletion docker/opts/ip.go
@@ -1,4 +1,4 @@
// Copyright © 2023 Ory Corp
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0

package opts
Expand Down
2 changes: 1 addition & 1 deletion docker/opts/opts.go
@@ -1,4 +1,4 @@
// Copyright © 2023 Ory Corp
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0

package opts
Expand Down
2 changes: 1 addition & 1 deletion docker/opts/opts_unix.go
@@ -1,4 +1,4 @@
// Copyright © 2023 Ory Corp
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0

//go:build !windows
Expand Down
2 changes: 1 addition & 1 deletion docker/opts/opts_windows.go
@@ -1,4 +1,4 @@
// Copyright © 2023 Ory Corp
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0

package opts
Expand Down
2 changes: 1 addition & 1 deletion docker/opts/quotedstring.go
@@ -1,4 +1,4 @@
// Copyright © 2023 Ory Corp
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0

package opts
Expand Down
2 changes: 1 addition & 1 deletion docker/opts/runtime.go
@@ -1,4 +1,4 @@
// Copyright © 2023 Ory Corp
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0

package opts
Expand Down
2 changes: 1 addition & 1 deletion docker/opts/ulimit.go
@@ -1,4 +1,4 @@
// Copyright © 2023 Ory Corp
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0

package opts
Expand Down
15 changes: 8 additions & 7 deletions docker/pkg/archive/archive.go
@@ -1,4 +1,4 @@
// Copyright © 2023 Ory Corp
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0

package archive // import "github.com/ory/dockertest/v3/docker/pkg/archive"
Expand All @@ -21,12 +21,13 @@ import (
"strings"
"syscall"

"github.com/sirupsen/logrus"

"github.com/ory/dockertest/v3/docker/pkg/fileutils"
"github.com/ory/dockertest/v3/docker/pkg/idtools"
"github.com/ory/dockertest/v3/docker/pkg/ioutils"
"github.com/ory/dockertest/v3/docker/pkg/pools"
"github.com/ory/dockertest/v3/docker/pkg/system"
"github.com/sirupsen/logrus"
)

var unpigzPath string
Expand Down Expand Up @@ -499,13 +500,13 @@ func (ta *tarAppender) addTarFile(path, name string) error {
}
}

//check whether the file is overlayfs whiteout
//if yes, skip re-mapping container ID mappings.
// check whether the file is overlayfs whiteout
// if yes, skip re-mapping container ID mappings.
isOverlayWhiteout := fi.Mode()&os.ModeCharDevice != 0 && hdr.Devmajor == 0 && hdr.Devminor == 0

//handle re-mapping container ID mappings back to host ID mappings before
//writing tar headers/files. We skip whiteout files because they were written
//by the kernel and already have proper ownership relative to the host
// handle re-mapping container ID mappings back to host ID mappings before
// writing tar headers/files. We skip whiteout files because they were written
// by the kernel and already have proper ownership relative to the host
if !isOverlayWhiteout &&
!strings.HasPrefix(filepath.Base(hdr.Name), WhiteoutPrefix) &&
!ta.IDMappings.Empty() {
Expand Down
5 changes: 3 additions & 2 deletions docker/pkg/archive/archive_linux.go
@@ -1,4 +1,4 @@
// Copyright © 2023 Ory Corp
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0

package archive // import "github.com/ory/dockertest/v3/docker/pkg/archive"
Expand All @@ -9,8 +9,9 @@ import (
"path/filepath"
"strings"

"github.com/ory/dockertest/v3/docker/pkg/system"
"golang.org/x/sys/unix"

"github.com/ory/dockertest/v3/docker/pkg/system"
)

func getWhiteoutConverter(format WhiteoutFormat) tarWhiteoutConverter {
Expand Down
2 changes: 1 addition & 1 deletion docker/pkg/archive/archive_other.go
@@ -1,4 +1,4 @@
// Copyright © 2023 Ory Corp
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0

//go:build !linux
Expand Down
5 changes: 3 additions & 2 deletions docker/pkg/archive/archive_unix.go
@@ -1,4 +1,4 @@
// Copyright © 2023 Ory Corp
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0

//go:build !windows
Expand All @@ -15,9 +15,10 @@ import (
"path/filepath"
"syscall"

"golang.org/x/sys/unix"

"github.com/ory/dockertest/v3/docker/pkg/idtools"
"github.com/ory/dockertest/v3/docker/pkg/system"
"golang.org/x/sys/unix"
)

// fixVolumePathPrefix does platform specific processing to ensure that if
Expand Down
4 changes: 2 additions & 2 deletions docker/pkg/archive/archive_windows.go
@@ -1,4 +1,4 @@
// Copyright © 2023 Ory Corp
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0

package archive // import "github.com/ory/dockertest/v3/docker/pkg/archive"
Expand Down Expand Up @@ -44,7 +44,7 @@ func CanonicalTarNameForPath(p string) (string, error) {
// chmodTarEntry is used to adjust the file permissions used in tar header based
// on the platform the archival is done.
func chmodTarEntry(perm os.FileMode) os.FileMode {
//perm &= 0755 // this 0-ed out tar flags (like link, regular file, directory marker etc.)
// perm &= 0755 // this 0-ed out tar flags (like link, regular file, directory marker etc.)
permPart := perm & os.ModePerm
noPermPart := perm &^ os.ModePerm
// Add the x bit: make everything +x from windows
Expand Down
5 changes: 3 additions & 2 deletions docker/pkg/archive/changes.go
@@ -1,4 +1,4 @@
// Copyright © 2023 Ory Corp
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0

package archive // import "github.com/ory/dockertest/v3/docker/pkg/archive"
Expand All @@ -16,10 +16,11 @@ import (
"syscall"
"time"

"github.com/sirupsen/logrus"

"github.com/ory/dockertest/v3/docker/pkg/idtools"
"github.com/ory/dockertest/v3/docker/pkg/pools"
"github.com/ory/dockertest/v3/docker/pkg/system"
"github.com/sirupsen/logrus"
)

// ChangeType represents the change type.
Expand Down
5 changes: 3 additions & 2 deletions docker/pkg/archive/changes_linux.go
@@ -1,4 +1,4 @@
// Copyright © 2023 Ory Corp
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0

package archive // import "github.com/ory/dockertest/v3/docker/pkg/archive"
Expand All @@ -12,8 +12,9 @@ import (
"syscall"
"unsafe"

"github.com/ory/dockertest/v3/docker/pkg/system"
"golang.org/x/sys/unix"

"github.com/ory/dockertest/v3/docker/pkg/system"
)

// walker is used to implement collectFileInfoForChanges on linux. Where this
Expand Down
2 changes: 1 addition & 1 deletion docker/pkg/archive/changes_other.go
@@ -1,4 +1,4 @@
// Copyright © 2023 Ory Corp
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0

//go:build !linux
Expand Down
5 changes: 3 additions & 2 deletions docker/pkg/archive/changes_unix.go
@@ -1,4 +1,4 @@
// Copyright © 2023 Ory Corp
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0

//go:build !windows
Expand All @@ -10,8 +10,9 @@ import (
"os"
"syscall"

"github.com/ory/dockertest/v3/docker/pkg/system"
"golang.org/x/sys/unix"

"github.com/ory/dockertest/v3/docker/pkg/system"
)

func statDifferent(oldStat *system.StatT, newStat *system.StatT) bool {
Expand Down
2 changes: 1 addition & 1 deletion docker/pkg/archive/changes_windows.go
@@ -1,4 +1,4 @@
// Copyright © 2023 Ory Corp
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0

package archive // import "github.com/ory/dockertest/v3/docker/pkg/archive"
Expand Down
5 changes: 3 additions & 2 deletions docker/pkg/archive/copy.go
@@ -1,4 +1,4 @@
// Copyright © 2023 Ory Corp
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0

package archive // import "github.com/ory/dockertest/v3/docker/pkg/archive"
Expand All @@ -12,8 +12,9 @@ import (
"path/filepath"
"strings"

"github.com/ory/dockertest/v3/docker/pkg/system"
"github.com/sirupsen/logrus"

"github.com/ory/dockertest/v3/docker/pkg/system"
)

// Errors used or returned by this file.
Expand Down
2 changes: 1 addition & 1 deletion docker/pkg/archive/copy_unix.go
@@ -1,4 +1,4 @@
// Copyright © 2023 Ory Corp
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0

//go:build !windows
Expand Down
2 changes: 1 addition & 1 deletion docker/pkg/archive/copy_windows.go
@@ -1,4 +1,4 @@
// Copyright © 2023 Ory Corp
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0

package archive // import "github.com/ory/dockertest/v3/docker/pkg/archive"
Expand Down
5 changes: 3 additions & 2 deletions docker/pkg/archive/diff.go
@@ -1,4 +1,4 @@
// Copyright © 2023 Ory Corp
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0

package archive // import "github.com/ory/dockertest/v3/docker/pkg/archive"
Expand All @@ -13,10 +13,11 @@ import (
"runtime"
"strings"

"github.com/sirupsen/logrus"

"github.com/ory/dockertest/v3/docker/pkg/idtools"
"github.com/ory/dockertest/v3/docker/pkg/pools"
"github.com/ory/dockertest/v3/docker/pkg/system"
"github.com/sirupsen/logrus"
)

// UnpackLayer unpack `layer` to a `dest`. The stream `layer` can be
Expand Down