Skip to content
This repository was archived by the owner on Dec 17, 2021. It is now read-only.

Commit 32a684d

Browse files
committed
commands/url: Add
1 parent c4f3857 commit 32a684d

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

commands/url.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package commands
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/docker/machine/libmachine"
7+
)
8+
9+
func cmdURL(c CommandLine, api libmachine.API) error {
10+
if len(c.Args()) > 1 {
11+
return ErrExpectedOneMachine
12+
}
13+
14+
target, err := targetHost(c, api)
15+
if err != nil {
16+
return err
17+
}
18+
19+
host, err := api.Load(target)
20+
if err != nil {
21+
return err
22+
}
23+
24+
url, err := host.URL()
25+
if err != nil {
26+
return err
27+
}
28+
29+
fmt.Println(url)
30+
31+
return nil
32+
}

commands/url_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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/host"
9+
"github.com/docker/machine/libmachine/libmachinetest"
10+
"github.com/docker/machine/libmachine/state"
11+
"github.com/stretchr/testify/assert"
12+
)
13+
14+
func TestCmdURLMissingMachineName(t *testing.T) {
15+
commandLine := &commandstest.FakeCommandLine{}
16+
api := &libmachinetest.FakeAPI{}
17+
18+
err := cmdURL(commandLine, api)
19+
20+
assert.Equal(t, ErrNoDefault, err)
21+
}
22+
23+
func TestCmdURLTooManyNames(t *testing.T) {
24+
commandLine := &commandstest.FakeCommandLine{
25+
CliArgs: []string{"machineToRemove1", "machineToRemove2"},
26+
}
27+
api := &libmachinetest.FakeAPI{}
28+
29+
err := cmdURL(commandLine, api)
30+
31+
assert.EqualError(t, err, "Error: Expected one machine name as an argument")
32+
}
33+
34+
func TestCmdURL(t *testing.T) {
35+
commandLine := &commandstest.FakeCommandLine{
36+
CliArgs: []string{"machine"},
37+
}
38+
api := &libmachinetest.FakeAPI{
39+
Hosts: []*host.Host{
40+
{
41+
Name: "machine",
42+
Driver: &fakedriver.Driver{
43+
MockState: state.Running,
44+
MockIP: "120.0.0.1",
45+
},
46+
},
47+
},
48+
}
49+
50+
stdoutGetter := commandstest.NewStdoutGetter()
51+
defer stdoutGetter.Stop()
52+
53+
err := cmdURL(commandLine, api)
54+
55+
assert.NoError(t, err)
56+
assert.Equal(t, "tcp://120.0.0.1:2376\n", stdoutGetter.Output())
57+
}

0 commit comments

Comments
 (0)