Skip to content

Commit

Permalink
test: fix TestGetDockerConfig (#1413)
Browse files Browse the repository at this point in the history
Do not assume non-empty auths section in local config file but test that
getDockerConfig returns default config when DOCKER_CONFIG env variable
is not set.

Also remove t.Cleanup calls that restore env variables because t.Setenv
restores them itself.
  • Loading branch information
AlexanderYastrebov authored Aug 2, 2023
1 parent f4532f3 commit 9b14c53
Showing 1 changed file with 5 additions and 36 deletions.
41 changes: 5 additions & 36 deletions docker_auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,33 +22,23 @@ var testDockerConfigDirPath = filepath.Join("testdata", ".docker")

var indexDockerIO = testcontainersdocker.IndexDockerIO

var originalDockerAuthConfig string

func init() {
originalDockerAuthConfig = os.Getenv("DOCKER_AUTH_CONFIG")
}

func TestGetDockerConfig(t *testing.T) {
const expectedErrorMessage = "Expected to find %s in auth configs"

// Verify that the default docker config file exists before any test in this suite runs.
// Then, we can safely run the tests that rely on it.
cfg, err := dockercfg.LoadDefaultConfig()
defaultCfg, err := dockercfg.LoadDefaultConfig()
require.Nil(t, err)
require.NotNil(t, cfg)
require.NotNil(t, defaultCfg)

t.Run("without DOCKER_CONFIG env var retrieves default", func(t *testing.T) {
t.Setenv("DOCKER_CONFIG", "")

cfg, err := getDockerConfig()
require.Nil(t, err)
require.NotNil(t, cfg)

assert.Greater(t, len(cfg.AuthConfigs), 0)

authCfgs := cfg.AuthConfigs

if _, ok := authCfgs[indexDockerIO]; !ok {
t.Errorf(expectedErrorMessage, indexDockerIO)
}
assert.Equal(t, defaultCfg, cfg)
})

t.Run("with DOCKER_CONFIG env var pointing to a non-existing file raises error", func(t *testing.T) {
Expand Down Expand Up @@ -82,10 +72,6 @@ func TestGetDockerConfig(t *testing.T) {
})

t.Run("DOCKER_AUTH_CONFIG env var takes precedence", func(t *testing.T) {
t.Cleanup(func() {
os.Setenv("DOCKER_AUTH_CONFIG", originalDockerAuthConfig)
})

t.Setenv("DOCKER_AUTH_CONFIG", `{
"auths": {
"`+exampleAuth+`": {}
Expand All @@ -111,10 +97,6 @@ func TestGetDockerConfig(t *testing.T) {
})

t.Run("retrieve auth with DOCKER_AUTH_CONFIG env var", func(t *testing.T) {
t.Cleanup(func() {
os.Setenv("DOCKER_AUTH_CONFIG", originalDockerAuthConfig)
})

base64 := "Z29waGVyOnNlY3JldA==" // gopher:secret

t.Setenv("DOCKER_AUTH_CONFIG", `{
Expand Down Expand Up @@ -170,10 +152,6 @@ func removeImageFromLocalCache(t *testing.T, image string) {
}

func TestBuildContainerFromDockerfileWithDockerAuthConfig(t *testing.T) {
t.Cleanup(func() {
os.Setenv("DOCKER_AUTH_CONFIG", originalDockerAuthConfig)
})

// using the same credentials as in the Docker Registry
base64 := "dGVzdHVzZXI6dGVzdHBhc3N3b3Jk" // testuser:testpassword
t.Setenv("DOCKER_AUTH_CONFIG", `{
Expand Down Expand Up @@ -203,10 +181,6 @@ func TestBuildContainerFromDockerfileWithDockerAuthConfig(t *testing.T) {
}

func TestBuildContainerFromDockerfileShouldFailWithWrongDockerAuthConfig(t *testing.T) {
t.Cleanup(func() {
os.Setenv("DOCKER_AUTH_CONFIG", originalDockerAuthConfig)
})

// using different credentials than in the Docker Registry
base64 := "Zm9vOmJhcg==" // foo:bar
t.Setenv("DOCKER_AUTH_CONFIG", `{
Expand Down Expand Up @@ -236,11 +210,6 @@ func TestBuildContainerFromDockerfileShouldFailWithWrongDockerAuthConfig(t *test
}

func TestCreateContainerFromPrivateRegistry(t *testing.T) {
t.Cleanup(func() {
os.Setenv("DOCKER_AUTH_CONFIG", originalDockerAuthConfig)
})
os.Unsetenv("DOCKER_AUTH_CONFIG")

// using the same credentials as in the Docker Registry
base64 := "dGVzdHVzZXI6dGVzdHBhc3N3b3Jk" // testuser:testpassword
t.Setenv("DOCKER_AUTH_CONFIG", `{
Expand Down

0 comments on commit 9b14c53

Please sign in to comment.