|
| 1 | +package docker |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/docker/cli/cli/config/configfile" |
| 9 | + "github.com/opencontainers/go-digest" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | +) |
| 12 | + |
| 13 | +func Test_loadConfig(t *testing.T) { |
| 14 | + root := "test-fixtures" |
| 15 | + |
| 16 | + prepareConfig := func(t *testing.T, context, fname string) *configfile.ConfigFile { |
| 17 | + t.Helper() |
| 18 | + cfg := configfile.New(fname) |
| 19 | + cfg.CurrentContext = context |
| 20 | + |
| 21 | + return cfg |
| 22 | + } |
| 23 | + |
| 24 | + tests := []struct { |
| 25 | + name string |
| 26 | + filename string |
| 27 | + want *configfile.ConfigFile |
| 28 | + err error |
| 29 | + wantErr bool |
| 30 | + }{ |
| 31 | + { |
| 32 | + name: "config file not found", |
| 33 | + filename: "some-nonexisting-file", |
| 34 | + wantErr: true, |
| 35 | + }, |
| 36 | + { |
| 37 | + name: "config file parsed normally", |
| 38 | + filename: filepath.Join(root, "config0.json"), |
| 39 | + wantErr: false, |
| 40 | + want: prepareConfig(t, "colima", filepath.Join(root, "config0.json")), |
| 41 | + }, |
| 42 | + { |
| 43 | + name: "config file cannot be parsed", |
| 44 | + filename: filepath.Join(root, "config1.json"), |
| 45 | + wantErr: true, |
| 46 | + }, |
| 47 | + } |
| 48 | + for _, tt := range tests { |
| 49 | + t.Run(tt.name, func(t *testing.T) { |
| 50 | + got, err := loadConfig(tt.filename) |
| 51 | + if tt.wantErr { |
| 52 | + assert.NotNil(t, err) |
| 53 | + } else { |
| 54 | + assert.Nil(t, err) |
| 55 | + } |
| 56 | + |
| 57 | + assert.Equal(t, tt.want, got) |
| 58 | + }) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func Test_resolveContextName(t *testing.T) { |
| 63 | + type args struct { |
| 64 | + contextOverride string |
| 65 | + config *configfile.ConfigFile |
| 66 | + } |
| 67 | + tests := []struct { |
| 68 | + name string |
| 69 | + args args |
| 70 | + want string |
| 71 | + }{ |
| 72 | + { |
| 73 | + name: "respect contextOverride", |
| 74 | + args: args{ |
| 75 | + contextOverride: "contextFromEnvironment", |
| 76 | + }, |
| 77 | + want: "contextFromEnvironment", |
| 78 | + }, |
| 79 | + { |
| 80 | + name: "returns default context name if config is nil", |
| 81 | + args: args{}, |
| 82 | + want: "default", |
| 83 | + }, |
| 84 | + { |
| 85 | + name: "returns context from the config", |
| 86 | + args: args{ |
| 87 | + config: &configfile.ConfigFile{ |
| 88 | + CurrentContext: "colima", |
| 89 | + }, |
| 90 | + }, |
| 91 | + want: "colima", |
| 92 | + }, |
| 93 | + } |
| 94 | + for _, tt := range tests { |
| 95 | + t.Run(tt.name, func(t *testing.T) { |
| 96 | + got := resolveContextName(tt.args.contextOverride, tt.args.config) |
| 97 | + |
| 98 | + assert.Equal(t, tt.want, got) |
| 99 | + }) |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +func Test_endpointFromContext(t *testing.T) { |
| 104 | + root := "test-fixtures" |
| 105 | + |
| 106 | + tmpCtxDir := func(t *testing.T) string { |
| 107 | + t.Helper() |
| 108 | + d, err := os.MkdirTemp("", "tests") |
| 109 | + if err != nil { |
| 110 | + t.Fatalf("cant setup test: %v", err) |
| 111 | + } |
| 112 | + |
| 113 | + ctxDir := filepath.Join(d, "contexts") |
| 114 | + err = os.MkdirAll(ctxDir, 0o755) |
| 115 | + if err != nil { |
| 116 | + t.Fatalf("cant setup test: %v", err) |
| 117 | + } |
| 118 | + |
| 119 | + return ctxDir |
| 120 | + } |
| 121 | + |
| 122 | + readFixture := func(t *testing.T, name string) []byte { |
| 123 | + t.Helper() |
| 124 | + data, err := os.ReadFile(filepath.Join(root, name)) |
| 125 | + if err != nil { |
| 126 | + t.Fatalf("cant setup test: %v", err) |
| 127 | + } |
| 128 | + |
| 129 | + return data |
| 130 | + } |
| 131 | + |
| 132 | + // meta files stored under ~/.docker with names like |
| 133 | + // ~/.docker/contexts/meta/f24fd3749c1368328e2b149bec149cb6795619f244c5b584e844961215dadd16/meta.json |
| 134 | + writeTestMeta := func(t *testing.T, fixture, dir, contextName string) { |
| 135 | + t.Helper() |
| 136 | + data := readFixture(t, fixture) |
| 137 | + dd := digest.FromString(contextName) |
| 138 | + |
| 139 | + base := filepath.Join(dir, "meta", dd.Encoded()) |
| 140 | + |
| 141 | + err := os.MkdirAll(base, 0o755) |
| 142 | + if err != nil { |
| 143 | + t.Fatalf("cant setup test: %v", err) |
| 144 | + } |
| 145 | + |
| 146 | + outFname := filepath.Join(base, "meta.json") |
| 147 | + |
| 148 | + err = os.WriteFile(outFname, data, 0o600) |
| 149 | + if err != nil { |
| 150 | + t.Fatalf("cant setup test: %v", err) |
| 151 | + } |
| 152 | + |
| 153 | + t.Logf("fixture %s written to: %s", fixture, outFname) |
| 154 | + } |
| 155 | + |
| 156 | + tests := []struct { |
| 157 | + name string |
| 158 | + ctxName string |
| 159 | + want string |
| 160 | + fixture string |
| 161 | + wantErr bool |
| 162 | + }{ |
| 163 | + { |
| 164 | + name: "reads docker host from the meta data", |
| 165 | + want: "unix:///some_weird_location/.colima/docker.sock", |
| 166 | + ctxName: "colima", |
| 167 | + fixture: "meta0.json", |
| 168 | + }, |
| 169 | + { |
| 170 | + name: "cant read docker host from the meta data", |
| 171 | + want: "", |
| 172 | + ctxName: "colima", |
| 173 | + fixture: "", |
| 174 | + wantErr: true, |
| 175 | + }, |
| 176 | + { |
| 177 | + name: "invalid endpoint name", |
| 178 | + want: "", |
| 179 | + ctxName: "colima", |
| 180 | + fixture: "meta1.json", |
| 181 | + wantErr: true, |
| 182 | + }, |
| 183 | + { |
| 184 | + name: "no host defined", |
| 185 | + want: "", |
| 186 | + ctxName: "colima", |
| 187 | + fixture: "meta2.json", |
| 188 | + wantErr: true, |
| 189 | + }, |
| 190 | + } |
| 191 | + for _, tt := range tests { |
| 192 | + t.Run(tt.name, func(t *testing.T) { |
| 193 | + dir := tmpCtxDir(t) |
| 194 | + if tt.fixture != "" { |
| 195 | + writeTestMeta(t, tt.fixture, dir, tt.ctxName) |
| 196 | + } |
| 197 | + |
| 198 | + got, err := endpointFromContext(dir, tt.ctxName) |
| 199 | + if tt.wantErr { |
| 200 | + assert.NotNil(t, err) |
| 201 | + } else { |
| 202 | + assert.Nil(t, err) |
| 203 | + } |
| 204 | + |
| 205 | + assert.Equal(t, tt.want, got) |
| 206 | + }) |
| 207 | + } |
| 208 | +} |
0 commit comments