github.1git.de/docker/cli@v26.1.3+incompatible/cli/command/context/create_test.go (about) 1 // FIXME(thaJeztah): remove once we are a module; the go:build directive prevents go from downgrading language version to go1.16: 2 //go:build go1.19 3 4 package context 5 6 import ( 7 "fmt" 8 "testing" 9 10 "github.com/docker/cli/cli/command" 11 "github.com/docker/cli/cli/config/configfile" 12 "github.com/docker/cli/cli/context/docker" 13 "github.com/docker/cli/cli/context/store" 14 "github.com/docker/cli/internal/test" 15 "gotest.tools/v3/assert" 16 ) 17 18 func makeFakeCli(t *testing.T, opts ...func(*test.FakeCli)) *test.FakeCli { 19 t.Helper() 20 dir := t.TempDir() 21 storeConfig := store.NewConfig( 22 func() any { return &command.DockerContext{} }, 23 store.EndpointTypeGetter(docker.DockerEndpoint, func() any { return &docker.EndpointMeta{} }), 24 ) 25 contextStore := &command.ContextStoreWithDefault{ 26 Store: store.New(dir, storeConfig), 27 Resolver: func() (*command.DefaultContext, error) { 28 return &command.DefaultContext{ 29 Meta: store.Metadata{ 30 Endpoints: map[string]any{ 31 docker.DockerEndpoint: docker.EndpointMeta{ 32 Host: "unix:///var/run/docker.sock", 33 }, 34 }, 35 Metadata: command.DockerContext{ 36 Description: "", 37 }, 38 Name: command.DefaultContextName, 39 }, 40 TLS: store.ContextTLSData{}, 41 }, nil 42 }, 43 } 44 result := test.NewFakeCli(nil, opts...) 45 for _, o := range opts { 46 o(result) 47 } 48 result.SetContextStore(contextStore) 49 return result 50 } 51 52 func withCliConfig(configFile *configfile.ConfigFile) func(*test.FakeCli) { 53 return func(m *test.FakeCli) { 54 m.SetConfigFile(configFile) 55 } 56 } 57 58 func TestCreate(t *testing.T) { 59 cli := makeFakeCli(t) 60 assert.NilError(t, cli.ContextStore().CreateOrUpdate(store.Metadata{Name: "existing-context"})) 61 tests := []struct { 62 options CreateOptions 63 expecterErr string 64 }{ 65 { 66 expecterErr: `context name cannot be empty`, 67 }, 68 { 69 options: CreateOptions{ 70 Name: "default", 71 }, 72 expecterErr: `"default" is a reserved context name`, 73 }, 74 { 75 options: CreateOptions{ 76 Name: " ", 77 }, 78 expecterErr: `context name " " is invalid`, 79 }, 80 { 81 options: CreateOptions{ 82 Name: "existing-context", 83 }, 84 expecterErr: `context "existing-context" already exists`, 85 }, 86 { 87 options: CreateOptions{ 88 Name: "invalid-docker-host", 89 Docker: map[string]string{ 90 keyHost: "some///invalid/host", 91 }, 92 }, 93 expecterErr: `unable to parse docker host`, 94 }, 95 } 96 for _, tc := range tests { 97 tc := tc 98 t.Run(tc.options.Name, func(t *testing.T) { 99 err := RunCreate(cli, &tc.options) 100 if tc.expecterErr == "" { 101 assert.NilError(t, err) 102 } else { 103 assert.ErrorContains(t, err, tc.expecterErr) 104 } 105 }) 106 } 107 } 108 109 func assertContextCreateLogging(t *testing.T, cli *test.FakeCli, n string) { 110 t.Helper() 111 assert.Equal(t, n+"\n", cli.OutBuffer().String()) 112 assert.Equal(t, fmt.Sprintf("Successfully created context %q\n", n), cli.ErrBuffer().String()) 113 } 114 115 func TestCreateOrchestratorEmpty(t *testing.T) { 116 cli := makeFakeCli(t) 117 118 err := RunCreate(cli, &CreateOptions{ 119 Name: "test", 120 Docker: map[string]string{}, 121 }) 122 assert.NilError(t, err) 123 assertContextCreateLogging(t, cli, "test") 124 } 125 126 func TestCreateFromContext(t *testing.T) { 127 cases := []struct { 128 name string 129 description string 130 expectedDescription string 131 docker map[string]string 132 }{ 133 { 134 name: "no-override", 135 expectedDescription: "original description", 136 }, 137 { 138 name: "override-description", 139 description: "new description", 140 expectedDescription: "new description", 141 }, 142 } 143 144 cli := makeFakeCli(t) 145 cli.ResetOutputBuffers() 146 assert.NilError(t, RunCreate(cli, &CreateOptions{ 147 Name: "original", 148 Description: "original description", 149 Docker: map[string]string{ 150 keyHost: "tcp://42.42.42.42:2375", 151 }, 152 })) 153 assertContextCreateLogging(t, cli, "original") 154 155 cli.ResetOutputBuffers() 156 assert.NilError(t, RunCreate(cli, &CreateOptions{ 157 Name: "dummy", 158 Description: "dummy description", 159 Docker: map[string]string{ 160 keyHost: "tcp://24.24.24.24:2375", 161 }, 162 })) 163 assertContextCreateLogging(t, cli, "dummy") 164 165 cli.SetCurrentContext("dummy") 166 167 for _, c := range cases { 168 c := c 169 t.Run(c.name, func(t *testing.T) { 170 cli.ResetOutputBuffers() 171 err := RunCreate(cli, &CreateOptions{ 172 From: "original", 173 Name: c.name, 174 Description: c.description, 175 Docker: c.docker, 176 }) 177 assert.NilError(t, err) 178 assertContextCreateLogging(t, cli, c.name) 179 newContext, err := cli.ContextStore().GetMetadata(c.name) 180 assert.NilError(t, err) 181 newContextTyped, err := command.GetDockerContext(newContext) 182 assert.NilError(t, err) 183 dockerEndpoint, err := docker.EndpointFromContext(newContext) 184 assert.NilError(t, err) 185 assert.Equal(t, newContextTyped.Description, c.expectedDescription) 186 assert.Equal(t, dockerEndpoint.Host, "tcp://42.42.42.42:2375") 187 }) 188 } 189 } 190 191 func TestCreateFromCurrent(t *testing.T) { 192 cases := []struct { 193 name string 194 description string 195 orchestrator string 196 expectedDescription string 197 }{ 198 { 199 name: "no-override", 200 expectedDescription: "original description", 201 }, 202 { 203 name: "override-description", 204 description: "new description", 205 expectedDescription: "new description", 206 }, 207 } 208 209 cli := makeFakeCli(t) 210 cli.ResetOutputBuffers() 211 assert.NilError(t, RunCreate(cli, &CreateOptions{ 212 Name: "original", 213 Description: "original description", 214 Docker: map[string]string{ 215 keyHost: "tcp://42.42.42.42:2375", 216 }, 217 })) 218 assertContextCreateLogging(t, cli, "original") 219 220 cli.SetCurrentContext("original") 221 222 for _, c := range cases { 223 c := c 224 t.Run(c.name, func(t *testing.T) { 225 cli.ResetOutputBuffers() 226 err := RunCreate(cli, &CreateOptions{ 227 Name: c.name, 228 Description: c.description, 229 }) 230 assert.NilError(t, err) 231 assertContextCreateLogging(t, cli, c.name) 232 newContext, err := cli.ContextStore().GetMetadata(c.name) 233 assert.NilError(t, err) 234 newContextTyped, err := command.GetDockerContext(newContext) 235 assert.NilError(t, err) 236 dockerEndpoint, err := docker.EndpointFromContext(newContext) 237 assert.NilError(t, err) 238 assert.Equal(t, newContextTyped.Description, c.expectedDescription) 239 assert.Equal(t, dockerEndpoint.Host, "tcp://42.42.42.42:2375") 240 }) 241 } 242 }