Skip to content

Commit

Permalink
Merge branch 'main' into v1
Browse files Browse the repository at this point in the history
* main:
  ci: add generate for mocks (#2774)
  fix: docker config error handling when config file does not exist (#2772)
  docs: refine heading badges in README (#2770)
  • Loading branch information
mdelapenya committed Sep 10, 2024
2 parents 66eb1c1 + b4f8294 commit 018c0d5
Show file tree
Hide file tree
Showing 49 changed files with 277 additions and 133 deletions.
14 changes: 13 additions & 1 deletion .github/workflows/ci-test-go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,25 @@ jobs:
# takes precedence over all other caching options.
skip-cache: true

- name: generate
if: ${{ inputs.platform == 'ubuntu-latest' }}
working-directory: ./${{ inputs.project-directory }}
shell: bash
run: |
make generate
git --no-pager diff && [[ 0 -eq $(git status --porcelain | wc -l) ]]
- name: modVerify
working-directory: ./${{ inputs.project-directory }}
run: go mod verify

- name: modTidy
if: ${{ inputs.platform == 'ubuntu-latest' }}
working-directory: ./${{ inputs.project-directory }}
run: make tidy
shell: bash
run: |
make tidy
git --no-pager diff && [[ 0 -eq $(git status --porcelain | wc -l) ]]
- name: ensure compilation
working-directory: ./${{ inputs.project-directory }}
Expand Down
19 changes: 3 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,14 @@
# Testcontainers

[![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://github.com/codespaces/new?hide_repo_select=true&ref=main&repo=141451032&machine=standardLinux32gb&devcontainer_path=.devcontainer%2Fdevcontainer.json&location=EastUs)

**Builds**

[![Main pipeline](https://github.com/testcontainers/testcontainers-go/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/testcontainers/testcontainers-go/actions/workflows/ci.yml)

**Documentation**

[![GoDoc Reference](https://pkg.go.dev/badge/github.com/testcontainers/testcontainers-go.svg)](https://pkg.go.dev/github.com/testcontainers/testcontainers-go)

**Social**

[![Slack](https://img.shields.io/badge/Slack-4A154B?logo=slack)](https://testcontainers.slack.com/)

**Code quality**

[![Go Report Card](https://goreportcard.com/badge/github.com/testcontainers/testcontainers-go)](https://goreportcard.com/report/github.com/testcontainers/testcontainers-go)
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=testcontainers_testcontainers-go&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=testcontainers_testcontainers-go)
[![License](https://img.shields.io/badge/license-MIT-blue)](https://github.com/testcontainers/testcontainers-go/blob/main/LICENSE)

**License**
[![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://github.com/codespaces/new?hide_repo_select=true&ref=main&repo=141451032&machine=standardLinux32gb&devcontainer_path=.devcontainer%2Fdevcontainer.json&location=EastUs)

[![License](https://img.shields.io/badge/license-MIT-blue)](https://github.com/testcontainers/testcontainers-go/blob/main/LICENSE)
[![Join our Slack](https://img.shields.io/badge/Slack-4A154B?logo=slack)](https://testcontainers.slack.com/)

_Testcontainers for Go_ is a Go package that makes it simple to create and clean up container-based dependencies for
automated integration/smoke tests. The clean, easy-to-use API enables developers to programmatically define containers
Expand Down
49 changes: 20 additions & 29 deletions auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"net/url"
"os"
"sync"
Expand Down Expand Up @@ -111,24 +110,12 @@ func (c *credentialsCache) Get(hostname, configKey string) (string, string, erro
return user, password, nil
}

// configFileKey returns a key to use for caching credentials based on
// configKey returns a key to use for caching credentials based on
// the contents of the currently active config.
func configFileKey() (string, error) {
configPath, err := dockercfg.ConfigPath()
if err != nil {
return "", err
}

f, err := os.Open(configPath)
if err != nil {
return "", fmt.Errorf("open config file: %w", err)
}

defer f.Close()

func configKey(cfg *dockercfg.Config) (string, error) {
h := md5.New()
if _, err := io.Copy(h, f); err != nil {
return "", fmt.Errorf("copying config file: %w", err)
if err := json.NewEncoder(h).Encode(cfg); err != nil {
return "", fmt.Errorf("encode config: %w", err)
}

return hex.EncodeToString(h.Sum(nil)), nil
Expand All @@ -139,10 +126,14 @@ func configFileKey() (string, error) {
func GetDockerConfigs() (map[string]registry.AuthConfig, error) {
cfg, err := getDockerConfig()
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return map[string]registry.AuthConfig{}, nil
}

return nil, err
}

configKey, err := configFileKey()
key, err := configKey(cfg)
if err != nil {
return nil, err
}
Expand All @@ -169,7 +160,7 @@ func GetDockerConfigs() (map[string]registry.AuthConfig, error) {
switch {
case ac.Username == "" && ac.Password == "":
// Look up credentials from the credential store.
u, p, err := creds.Get(k, configKey)
u, p, err := creds.Get(k, key)
if err != nil {
results <- authConfigResult{err: err}
return
Expand All @@ -192,7 +183,7 @@ func GetDockerConfigs() (map[string]registry.AuthConfig, error) {
go func(k string) {
defer wg.Done()

u, p, err := creds.Get(k, configKey)
u, p, err := creds.Get(k, key)
if err != nil {
results <- authConfigResult{err: err}
return
Expand Down Expand Up @@ -234,22 +225,22 @@ func GetDockerConfigs() (map[string]registry.AuthConfig, error) {
// 1. the DOCKER_AUTH_CONFIG environment variable, unmarshalling it into a dockercfg.Config
// 2. the DOCKER_CONFIG environment variable, as the path to the config file
// 3. else it will load the default config file, which is ~/.docker/config.json
func getDockerConfig() (dockercfg.Config, error) {
dockerAuthConfig := os.Getenv("DOCKER_AUTH_CONFIG")
if dockerAuthConfig != "" {
cfg := dockercfg.Config{}
err := json.Unmarshal([]byte(dockerAuthConfig), &cfg)
if err == nil {
return cfg, nil
func getDockerConfig() (*dockercfg.Config, error) {
if env := os.Getenv("DOCKER_AUTH_CONFIG"); env != "" {
var cfg dockercfg.Config
if err := json.Unmarshal([]byte(env), &cfg); err != nil {
return nil, fmt.Errorf("unmarshal DOCKER_AUTH_CONFIG: %w", err)
}

return &cfg, nil
}

cfg, err := dockercfg.LoadDefaultConfig()
if err != nil {
return cfg, err
return nil, fmt.Errorf("load default config: %w", err)
}

return cfg, nil
return &cfg, nil
}

func getRegistryAuth(reg string, cfgs map[string]registry.AuthConfig) (registry.AuthConfig, bool) {
Expand Down
Loading

0 comments on commit 018c0d5

Please sign in to comment.