-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsetup_test.go
More file actions
150 lines (129 loc) · 3.92 KB
/
setup_test.go
File metadata and controls
150 lines (129 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"bytes"
"encoding/json"
"os"
"path/filepath"
"strings"
"testing"
"github.com/docker/cli/cli/config/configfile"
)
// setupTestEnvironment sets up a temporary directory for Docker config
// and ensures it's used for the duration of the test.
func setupTestEnvironment(t *testing.T) string {
t.Helper()
tempDir := t.TempDir()
t.Setenv("DOCKER_CONFIG", tempDir)
return tempDir
}
func TestRunSetupCommand_Errors(t *testing.T) {
setupTestEnvironment(t)
testCases := []struct {
name string
args []string
errContains string
}{
{"no args", []string{}, "missing argument"},
{"default with extra args", []string{"default", "extra"}, `"default" command does not accept additional arguments`},
{"show with extra args", []string{"show", "extra"}, `"show" command does not accept additional arguments`},
{"invalid registry", []string{"invalid/registry"}, "invalid registry"},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
out := new(bytes.Buffer)
err := RunSetupCommand(tc.args, out)
if err == nil {
t.Fatalf("Expected an error but got none")
}
if !strings.Contains(err.Error(), tc.errContains) {
t.Errorf("Expected error to contain %q, but got %q", tc.errContains, err.Error())
}
})
}
}
func TestRunSetupCommand_Show(t *testing.T) {
tempDir := setupTestEnvironment(t)
configPath := filepath.Join(tempDir, "config.json")
out := new(bytes.Buffer)
// Run command
err := RunSetupCommand([]string{"show"}, out)
if err != nil {
t.Fatalf("Expected no error but got %v", err)
}
actual := out.String()
expected := "default: false\nregistries: []\n"
if actual != expected {
t.Errorf("Expected output %q, but got %q", expected, actual)
}
// Test with a configured file
config := &configfile.ConfigFile{
CredentialsStore: "env",
CredentialHelpers: map[string]string{
"docker.io": "env",
},
}
configData, err := json.MarshalIndent(config, "", "\t")
if err != nil {
t.Fatalf("Unexpected error marshaling config: %v", err)
}
err = os.WriteFile(configPath, configData, 0600)
if err != nil {
t.Fatalf("Unexpected error writing config file: %v", err)
}
out.Reset()
err = RunSetupCommand([]string{"show"}, out)
if err != nil {
t.Fatalf("Expected no error but got %v", err)
}
actual = out.String()
expected = "default: true\nregistries:\n - docker.io\n"
if actual != expected {
t.Errorf("Expected output %q, but got %q", expected, actual)
}
}
func TestRunSetupCommand_Default(t *testing.T) {
tempDir := setupTestEnvironment(t)
configPath := filepath.Join(tempDir, "config.json")
out := new(bytes.Buffer)
// Run setup default
err := RunSetupCommand([]string{"default"}, out)
if err != nil {
t.Fatalf("RunSetupCommand() failed: %v", err)
}
// Verify config
configData, err := os.ReadFile(configPath)
if err != nil {
t.Fatalf("Failed to read config file: %v", err)
}
var config configfile.ConfigFile
err = json.Unmarshal(configData, &config)
if err != nil {
t.Fatalf("Failed to unmarshal config file: %v", err)
}
if config.CredentialsStore != "env" { //nolint:goconst // Redundant constant
t.Errorf("Expected credsStore to be 'env', got %q", config.CredentialsStore)
}
}
func TestRunSetupCommand_Registry(t *testing.T) {
tempDir := setupTestEnvironment(t)
configPath := filepath.Join(tempDir, "config.json")
out := new(bytes.Buffer)
// Run setup for a registry
err := RunSetupCommand([]string{"docker.io"}, out)
if err != nil {
t.Fatalf("RunSetupCommand() failed: %v", err)
}
// Verify config
configData, err := os.ReadFile(configPath)
if err != nil {
t.Fatalf("Failed to read config file: %v", err)
}
var config configfile.ConfigFile
err = json.Unmarshal(configData, &config)
if err != nil {
t.Fatalf("Failed to unmarshal config file: %v", err)
}
if helper, ok := config.CredentialHelpers["docker.io"]; !ok || helper != "env" {
t.Errorf("Expected credHelper for 'docker.io' to be 'env', got %q", helper)
}
}