@@ -3,6 +3,9 @@ package commands
33import (
44 "testing"
55
6+ "flag"
7+ "github.com/docker/machine/commands/commandstest"
8+ "github.com/docker/machine/libmachine/mcnflag"
69 "github.com/stretchr/testify/assert"
710)
811
@@ -20,3 +23,97 @@ func TestValidateSwarmDiscoveryAcceptsValidFormat(t *testing.T) {
2023 err := validateSwarmDiscovery ("token://deadbeefcafe" )
2124 assert .NoError (t , err )
2225}
26+
27+ type fakeFlagGetter struct {
28+ flag.Value
29+ value interface {}
30+ }
31+
32+ func (ff fakeFlagGetter ) Get () interface {} {
33+ return ff .value
34+ }
35+
36+ var nilStringSlice []string
37+
38+ var getDriverOptsFlags = []mcnflag.Flag {
39+ mcnflag.BoolFlag {
40+ Name : "bool" ,
41+ },
42+ mcnflag.IntFlag {
43+ Name : "int" ,
44+ },
45+ mcnflag.IntFlag {
46+ Name : "int_defaulted" ,
47+ Value : 42 ,
48+ },
49+ mcnflag.StringFlag {
50+ Name : "string" ,
51+ },
52+ mcnflag.StringFlag {
53+ Name : "string_defaulted" ,
54+ Value : "bob" ,
55+ },
56+ mcnflag.StringSliceFlag {
57+ Name : "stringslice" ,
58+ },
59+ mcnflag.StringSliceFlag {
60+ Name : "stringslice_defaulted" ,
61+ Value : []string {"joe" },
62+ },
63+ }
64+
65+ var getDriverOptsTests = []struct {
66+ data map [string ]interface {}
67+ expected map [string ]interface {}
68+ }{
69+ {
70+ expected : map [string ]interface {}{
71+ "bool" : false ,
72+ "int" : 0 ,
73+ "int_defaulted" : 42 ,
74+ "string" : "" ,
75+ "string_defaulted" : "bob" ,
76+ "stringslice" : nilStringSlice ,
77+ "stringslice_defaulted" : []string {"joe" },
78+ },
79+ },
80+ {
81+ data : map [string ]interface {}{
82+ "bool" : fakeFlagGetter {value : true },
83+ "int" : fakeFlagGetter {value : 42 },
84+ "int_defaulted" : fakeFlagGetter {value : 37 },
85+ "string" : fakeFlagGetter {value : "jake" },
86+ "string_defaulted" : fakeFlagGetter {value : "george" },
87+ // NB: StringSlices are not flag.Getters.
88+ "stringslice" : []string {"ford" },
89+ "stringslice_defaulted" : []string {"zaphod" , "arthur" },
90+ },
91+ expected : map [string ]interface {}{
92+ "bool" : true ,
93+ "int" : 42 ,
94+ "int_defaulted" : 37 ,
95+ "string" : "jake" ,
96+ "string_defaulted" : "george" ,
97+ "stringslice" : []string {"ford" },
98+ "stringslice_defaulted" : []string {"zaphod" , "arthur" },
99+ },
100+ },
101+ }
102+
103+ func TestGetDriverOpts (t * testing.T ) {
104+ for _ , tt := range getDriverOptsTests {
105+ commandLine := & commandstest.FakeCommandLine {
106+ LocalFlags : & commandstest.FakeFlagger {
107+ Data : tt .data ,
108+ },
109+ }
110+ driverOpts := getDriverOpts (commandLine , getDriverOptsFlags )
111+ assert .Equal (t , tt .expected ["bool" ], driverOpts .Bool ("bool" ))
112+ assert .Equal (t , tt .expected ["int" ], driverOpts .Int ("int" ))
113+ assert .Equal (t , tt .expected ["int_defaulted" ], driverOpts .Int ("int_defaulted" ))
114+ assert .Equal (t , tt .expected ["string" ], driverOpts .String ("string" ))
115+ assert .Equal (t , tt .expected ["string_defaulted" ], driverOpts .String ("string_defaulted" ))
116+ assert .Equal (t , tt .expected ["stringslice" ], driverOpts .StringSlice ("stringslice" ))
117+ assert .Equal (t , tt .expected ["stringslice_defaulted" ], driverOpts .StringSlice ("stringslice_defaulted" ))
118+ }
119+ }
0 commit comments