-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
111 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
container-engine-lib/lib/backend_impls/docker/docker_manager/docker_auth_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package docker_manager | ||
|
||
import ( | ||
"encoding/base64" | ||
"fmt" | ||
"io/ioutil" | ||
Check failure on line 6 in container-engine-lib/lib/backend_impls/docker/docker_manager/docker_auth_test.go GitHub Actions / golang-lint (container-engine-lib)
|
||
"os" | ||
"testing" | ||
|
||
"github.com/docker/docker/api/types/registry" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
// WriteStaticConfig writes a static Docker config.json file to a temporary directory | ||
func WriteStaticConfig(t *testing.T, configContent string) string { | ||
tmpDir, err := os.MkdirTemp("", "docker-config") | ||
if err != nil { | ||
t.Fatalf("Failed to create temp directory: %v", err) | ||
} | ||
|
||
configPath := tmpDir + "/config.json" | ||
err = ioutil.WriteFile(configPath, []byte(configContent), 0600) | ||
if err != nil { | ||
t.Fatalf("Failed to write config.json: %v", err) | ||
} | ||
|
||
// Set the DOCKER_CONFIG environment variable to the temp directory | ||
os.Setenv("DOCKER_CONFIG", tmpDir) | ||
return tmpDir | ||
} | ||
|
||
func TestGetAuthConfigForRepoPlain(t *testing.T) { | ||
cfg := ` | ||
{ | ||
"auths": { | ||
"https://index.docker.io/v1/": { | ||
"auth": "dXNlcjpwYXNzd29yZA==" | ||
} | ||
} | ||
} | ||
` | ||
tmpDir := WriteStaticConfig(t, cfg) | ||
defer os.RemoveAll(tmpDir) | ||
|
||
expectedAuth := registry.AuthConfig{ | ||
Username: "user", | ||
Password: "password", | ||
} | ||
encodedAuth := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", expectedAuth.Username, expectedAuth.Password))) | ||
|
||
// Test 1: Retrieve auth config for Docker Hub using docker.io domain | ||
authConfig, err := GetAuthFromDockerConfig("docker.io/my-repo/my-image:latest") | ||
assert.NoError(t, err) | ||
assert.Equal(t, encodedAuth, authConfig.Auth, "Auth for Docker Hub should match") | ||
|
||
// Test 2: Retrieve auth config for Docker Hub using no domain | ||
authConfig, err = GetAuthFromDockerConfig("my-repo/my-image:latest") | ||
assert.NoError(t, err) | ||
assert.Equal(t, encodedAuth, authConfig.Auth, "Auth for Docker Hub should match when using no host prefix") | ||
|
||
// Test 3: Retrieve auth config for Docker Hub using full domain and https:// prefix | ||
authConfig, err = GetAuthFromDockerConfig("https://registry-1.docker.io/my-repo/my-image:latest") | ||
assert.NoError(t, err) | ||
assert.Equal(t, encodedAuth, authConfig.Auth, "Auth for Docker Hub should match when using no host prefix") | ||
|
||
} | ||
|
||
func TestGetAuthConfigForRepoOSX(t *testing.T) { | ||
t.Skip("Skipping test that requires macOS keychain") | ||
|
||
cfg := `{ | ||
"auths": { | ||
"https://index.docker.io/v1/": {} | ||
}, | ||
"credsStore": "osxkeychain" | ||
}` | ||
tmpDir := WriteStaticConfig(t, cfg) | ||
defer os.RemoveAll(tmpDir) | ||
|
||
authConfig, err := GetAuthFromDockerConfig("my-repo/my-image:latest") | ||
assert.NoError(t, err) | ||
assert.NotNil(t, authConfig, "Auth config should not be nil") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters