@@ -4,34 +4,37 @@ import (
44 "errors"
55 "fmt"
66 "io"
7+ "os"
8+ "path/filepath"
79 "testing"
810
911 "github.com/docker/cli/internal/test"
1012 "github.com/docker/docker/api/types/swarm"
1113 "gotest.tools/v3/assert"
14+ is "gotest.tools/v3/assert/cmp"
1215 "gotest.tools/v3/golden"
1316)
1417
1518func TestSwarmInitErrorOnAPIFailure (t * testing.T ) {
1619 testCases := []struct {
1720 name string
1821 flags map [string ]string
19- swarmInitFunc func () (string , error )
22+ swarmInitFunc func (swarm. InitRequest ) (string , error )
2023 swarmInspectFunc func () (swarm.Swarm , error )
2124 swarmGetUnlockKeyFunc func () (swarm.UnlockKeyResponse , error )
2225 nodeInspectFunc func () (swarm.Node , []byte , error )
2326 expectedError string
2427 }{
2528 {
2629 name : "init-failed" ,
27- swarmInitFunc : func () (string , error ) {
30+ swarmInitFunc : func (swarm. InitRequest ) (string , error ) {
2831 return "" , errors .New ("error initializing the swarm" )
2932 },
3033 expectedError : "error initializing the swarm" ,
3134 },
3235 {
3336 name : "init-failed-with-ip-choice" ,
34- swarmInitFunc : func () (string , error ) {
37+ swarmInitFunc : func (swarm. InitRequest ) (string , error ) {
3538 return "" , errors .New ("could not choose an IP address to advertise" )
3639 },
3740 expectedError : "could not choose an IP address to advertise - specify one with --advertise-addr" ,
@@ -85,14 +88,14 @@ func TestSwarmInit(t *testing.T) {
8588 testCases := []struct {
8689 name string
8790 flags map [string ]string
88- swarmInitFunc func () (string , error )
91+ swarmInitFunc func (req swarm. InitRequest ) (string , error )
8992 swarmInspectFunc func () (swarm.Swarm , error )
9093 swarmGetUnlockKeyFunc func () (swarm.UnlockKeyResponse , error )
9194 nodeInspectFunc func () (swarm.Node , []byte , error )
9295 }{
9396 {
9497 name : "init" ,
95- swarmInitFunc : func () (string , error ) {
98+ swarmInitFunc : func (swarm. InitRequest ) (string , error ) {
9699 return "nodeID" , nil
97100 },
98101 },
@@ -101,7 +104,7 @@ func TestSwarmInit(t *testing.T) {
101104 flags : map [string ]string {
102105 flagAutolock : "true" ,
103106 },
104- swarmInitFunc : func () (string , error ) {
107+ swarmInitFunc : func (swarm. InitRequest ) (string , error ) {
105108 return "nodeID" , nil
106109 },
107110 swarmGetUnlockKeyFunc : func () (swarm.UnlockKeyResponse , error ) {
@@ -131,3 +134,28 @@ func TestSwarmInit(t *testing.T) {
131134 })
132135 }
133136}
137+
138+ func TestSwarmInitWithExternalCA (t * testing.T ) {
139+ cli := test .NewFakeCli (& fakeClient {
140+ swarmInitFunc : func (req swarm.InitRequest ) (string , error ) {
141+ if assert .Check (t , is .Len (req .Spec .CAConfig .ExternalCAs , 1 )) {
142+ assert .Equal (t , req .Spec .CAConfig .ExternalCAs [0 ].CACert , cert )
143+ assert .Equal (t , req .Spec .CAConfig .ExternalCAs [0 ].Protocol , swarm .ExternalCAProtocolCFSSL )
144+ assert .Equal (t , req .Spec .CAConfig .ExternalCAs [0 ].URL , "https://example.com" )
145+ }
146+ return "nodeID" , nil
147+ },
148+ })
149+
150+ tempDir := t .TempDir ()
151+ certFile := filepath .Join (tempDir , "cert.pem" )
152+ err := os .WriteFile (certFile , []byte (cert ), 0o644 )
153+ assert .NilError (t , err )
154+
155+ cmd := newInitCommand (cli )
156+ cmd .SetArgs ([]string {})
157+ cmd .SetOut (io .Discard )
158+ cmd .SetErr (io .Discard )
159+ assert .NilError (t , cmd .Flags ().Set (flagExternalCA , "protocol=cfssl,url=https://example.com,cacert=" + certFile ))
160+ assert .NilError (t , cmd .Execute ())
161+ }
0 commit comments