@@ -41,33 +41,76 @@ func withCliConfig(configFile *configfile.ConfigFile) func(*test.FakeCli) {
4141 }
4242}
4343
44- func TestCreateNoName (t * testing.T ) {
44+ func TestCreateInvalids (t * testing.T ) {
4545 cli , cleanup := makeFakeCli (t )
4646 defer cleanup ()
47- err := runCreate (cli , & createOptions {})
48- assert .ErrorContains (t , err , `context name cannot be empty` )
49- }
50-
51- func TestCreateExitingContext (t * testing.T ) {
52- cli , cleanup := makeFakeCli (t )
53- defer cleanup ()
54- assert .NilError (t , cli .ContextStore ().CreateOrUpdateContext (store.ContextMetadata {Name : "test" }))
55-
56- err := runCreate (cli , & createOptions {
57- name : "test" ,
58- })
59- assert .ErrorContains (t , err , `context "test" already exists` )
60- }
61-
62- func TestCreateInvalidOrchestrator (t * testing.T ) {
63- cli , cleanup := makeFakeCli (t )
64- defer cleanup ()
65-
66- err := runCreate (cli , & createOptions {
67- name : "test" ,
68- defaultStackOrchestrator : "invalid" ,
69- })
70- assert .ErrorContains (t , err , `specified orchestrator "invalid" is invalid, please use either kubernetes, swarm or all` )
47+ assert .NilError (t , cli .ContextStore ().CreateOrUpdateContext (store.ContextMetadata {Name : "existing-context" }))
48+ tests := []struct {
49+ options createOptions
50+ expecterErr string
51+ }{
52+ {
53+ expecterErr : `context name cannot be empty` ,
54+ },
55+ {
56+ options : createOptions {
57+ name : " " ,
58+ },
59+ expecterErr : `context name " " is invalid` ,
60+ },
61+ {
62+ options : createOptions {
63+ name : "existing-context" ,
64+ },
65+ expecterErr : `context "existing-context" already exists` ,
66+ },
67+ {
68+ options : createOptions {
69+ name : "invalid-docker-host" ,
70+ docker : map [string ]string {
71+ keyHost : "some///invalid/host" ,
72+ },
73+ },
74+ expecterErr : `unable to parse docker host` ,
75+ },
76+ {
77+ options : createOptions {
78+ name : "invalid-orchestrator" ,
79+ defaultStackOrchestrator : "invalid" ,
80+ },
81+ expecterErr : `specified orchestrator "invalid" is invalid, please use either kubernetes, swarm or all` ,
82+ },
83+ {
84+ options : createOptions {
85+ name : "orchestrator-swarm-no-endpoint" ,
86+ defaultStackOrchestrator : "swarm" ,
87+ },
88+ expecterErr : `docker endpoint configuration is required` ,
89+ },
90+ {
91+ options : createOptions {
92+ name : "orchestrator-kubernetes-no-endpoint" ,
93+ defaultStackOrchestrator : "kubernetes" ,
94+ docker : map [string ]string {},
95+ },
96+ expecterErr : `cannot specify orchestrator "kubernetes" without configuring a Kubernetes endpoint` ,
97+ },
98+ {
99+ options : createOptions {
100+ name : "orchestrator-all-no-endpoint" ,
101+ defaultStackOrchestrator : "all" ,
102+ docker : map [string ]string {},
103+ },
104+ expecterErr : `cannot specify orchestrator "all" without configuring a Kubernetes endpoint` ,
105+ },
106+ }
107+ for _ , tc := range tests {
108+ tc := tc
109+ t .Run (tc .options .name , func (t * testing.T ) {
110+ err := runCreate (cli , & tc .options )
111+ assert .ErrorContains (t , err , tc .expecterErr )
112+ })
113+ }
71114}
72115
73116func TestCreateOrchestratorSwarm (t * testing.T ) {
@@ -81,7 +124,7 @@ func TestCreateOrchestratorSwarm(t *testing.T) {
81124 })
82125 assert .NilError (t , err )
83126 assert .Equal (t , "test\n " , cli .OutBuffer ().String ())
84- assert .Equal (t , "Context \" test\" has been created \n " , cli .ErrBuffer ().String ())
127+ assert .Equal (t , "Successfully created context \" test\" \n " , cli .ErrBuffer ().String ())
85128}
86129
87130func TestCreateOrchestratorEmpty (t * testing.T ) {
@@ -95,31 +138,8 @@ func TestCreateOrchestratorEmpty(t *testing.T) {
95138 assert .NilError (t , err )
96139}
97140
98- func TestCreateOrchestratorKubernetesNoEndpoint (t * testing.T ) {
99- cli , cleanup := makeFakeCli (t )
100- defer cleanup ()
101-
102- err := runCreate (cli , & createOptions {
103- name : "test" ,
104- defaultStackOrchestrator : "kubernetes" ,
105- docker : map [string ]string {},
106- })
107- assert .ErrorContains (t , err , `cannot specify orchestrator "kubernetes" without configuring a Kubernetes endpoint` )
108- }
109-
110- func TestCreateOrchestratorAllNoKubernetesEndpoint (t * testing.T ) {
111- cli , cleanup := makeFakeCli (t )
112- defer cleanup ()
113-
114- err := runCreate (cli , & createOptions {
115- name : "test" ,
116- defaultStackOrchestrator : "all" ,
117- docker : map [string ]string {},
118- })
119- assert .ErrorContains (t , err , `cannot specify orchestrator "all" without configuring a Kubernetes endpoint` )
120- }
121-
122141func validateTestKubeEndpoint (t * testing.T , s store.Store , name string ) {
142+ t .Helper ()
123143 ctxMetadata , err := s .GetContextMetadata (name )
124144 assert .NilError (t , err )
125145 kubeMeta := ctxMetadata .Endpoints [kubernetes .KubernetesEndpoint ].(kubernetes.EndpointMeta )
@@ -132,6 +152,7 @@ func validateTestKubeEndpoint(t *testing.T, s store.Store, name string) {
132152}
133153
134154func createTestContextWithKube (t * testing.T , cli command.Cli ) {
155+ t .Helper ()
135156 revert := env .Patch (t , "KUBECONFIG" , "./testdata/test-kubeconfig" )
136157 defer revert ()
137158
@@ -152,15 +173,3 @@ func TestCreateOrchestratorAllKubernetesEndpointFromCurrent(t *testing.T) {
152173 createTestContextWithKube (t , cli )
153174 validateTestKubeEndpoint (t , cli .ContextStore (), "test" )
154175}
155-
156- func TestCreateInvalidDockerHost (t * testing.T ) {
157- cli , cleanup := makeFakeCli (t )
158- defer cleanup ()
159- err := runCreate (cli , & createOptions {
160- name : "test" ,
161- docker : map [string ]string {
162- keyHost : "some///invalid/host" ,
163- },
164- })
165- assert .ErrorContains (t , err , "unable to parse docker host" )
166- }
0 commit comments