|
| 1 | +package commands |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/docker/machine/commands/commandstest" |
| 7 | + "github.com/docker/machine/drivers/fakedriver" |
| 8 | + "github.com/docker/machine/libmachine" |
| 9 | + "github.com/docker/machine/libmachine/host" |
| 10 | + "github.com/docker/machine/libmachine/libmachinetest" |
| 11 | + "github.com/docker/machine/libmachine/state" |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | +) |
| 14 | + |
| 15 | +func TestCmdIPMissingMachineName(t *testing.T) { |
| 16 | + commandLine := &commandstest.FakeCommandLine{} |
| 17 | + api := &libmachinetest.FakeAPI{} |
| 18 | + |
| 19 | + err := cmdURL(commandLine, api) |
| 20 | + |
| 21 | + assert.Equal(t, err, ErrNoDefault) |
| 22 | +} |
| 23 | + |
| 24 | +func TestCmdIP(t *testing.T) { |
| 25 | + testCases := []struct { |
| 26 | + commandLine CommandLine |
| 27 | + api libmachine.API |
| 28 | + expectedErr error |
| 29 | + expectedOut string |
| 30 | + }{ |
| 31 | + { |
| 32 | + commandLine: &commandstest.FakeCommandLine{ |
| 33 | + CliArgs: []string{"machine"}, |
| 34 | + }, |
| 35 | + api: &libmachinetest.FakeAPI{ |
| 36 | + Hosts: []*host.Host{ |
| 37 | + { |
| 38 | + Name: "machine", |
| 39 | + Driver: &fakedriver.Driver{ |
| 40 | + MockState: state.Running, |
| 41 | + MockIP: "1.2.3.4", |
| 42 | + }, |
| 43 | + }, |
| 44 | + }, |
| 45 | + }, |
| 46 | + expectedErr: nil, |
| 47 | + expectedOut: "1.2.3.4\n", |
| 48 | + }, |
| 49 | + { |
| 50 | + commandLine: &commandstest.FakeCommandLine{ |
| 51 | + CliArgs: []string{}, |
| 52 | + }, |
| 53 | + api: &libmachinetest.FakeAPI{ |
| 54 | + Hosts: []*host.Host{ |
| 55 | + { |
| 56 | + Name: "default", |
| 57 | + Driver: &fakedriver.Driver{ |
| 58 | + MockState: state.Running, |
| 59 | + MockIP: "1.2.3.4", |
| 60 | + }, |
| 61 | + }, |
| 62 | + }, |
| 63 | + }, |
| 64 | + expectedErr: nil, |
| 65 | + expectedOut: "1.2.3.4\n", |
| 66 | + }, |
| 67 | + } |
| 68 | + |
| 69 | + for _, tc := range testCases { |
| 70 | + stdoutGetter := commandstest.NewStdoutGetter() |
| 71 | + |
| 72 | + err := cmdIP(tc.commandLine, tc.api) |
| 73 | + |
| 74 | + assert.Equal(t, tc.expectedErr, err) |
| 75 | + assert.Equal(t, tc.expectedOut, stdoutGetter.Output()) |
| 76 | + |
| 77 | + stdoutGetter.Stop() |
| 78 | + } |
| 79 | +} |
0 commit comments