github.com/AbhinandanKurakure/podman/v3@v3.4.10/pkg/auth/auth_test.go (about)

     1  package auth
     2  
     3  import (
     4  	"io/ioutil"
     5  	"testing"
     6  
     7  	"github.com/containers/image/v5/types"
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestAuthConfigsToAuthFile(t *testing.T) {
    12  	for _, tc := range []struct {
    13  		name             string
    14  		server           string
    15  		shouldErr        bool
    16  		expectedContains string
    17  	}{
    18  		{
    19  			name:             "empty auth configs",
    20  			server:           "",
    21  			shouldErr:        false,
    22  			expectedContains: "{}",
    23  		},
    24  		{
    25  			name:             "registry with prefix",
    26  			server:           "my-registry.local/username",
    27  			shouldErr:        false,
    28  			expectedContains: `"my-registry.local/username":`,
    29  		},
    30  		{
    31  			name:             "normalize https:// prefix",
    32  			server:           "http://my-registry.local/username",
    33  			shouldErr:        false,
    34  			expectedContains: `"my-registry.local/username":`,
    35  		},
    36  		{
    37  			name:             "normalize docker registry with https prefix",
    38  			server:           "http://index.docker.io/v1/",
    39  			shouldErr:        false,
    40  			expectedContains: `"index.docker.io":`,
    41  		},
    42  		{
    43  			name:             "normalize docker registry without https prefix",
    44  			server:           "docker.io/v2/",
    45  			shouldErr:        false,
    46  			expectedContains: `"docker.io":`,
    47  		},
    48  	} {
    49  		configs := map[string]types.DockerAuthConfig{}
    50  		if tc.server != "" {
    51  			configs[tc.server] = types.DockerAuthConfig{}
    52  		}
    53  
    54  		filePath, err := authConfigsToAuthFile(configs)
    55  
    56  		if tc.shouldErr {
    57  			assert.NotNil(t, err)
    58  			assert.Empty(t, filePath)
    59  		} else {
    60  			assert.Nil(t, err)
    61  			content, err := ioutil.ReadFile(filePath)
    62  			assert.Nil(t, err)
    63  			assert.Contains(t, string(content), tc.expectedContains)
    64  		}
    65  	}
    66  }