Skip to content

Commit 73b408c

Browse files
committed
greatly improved unittest code coverage
1 parent db29164 commit 73b408c

27 files changed

+591
-25
lines changed

base_command.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ func (b *BaseCommand) init(handler outputHandler, subcommand string, args ...str
5050
}
5151

5252
// setup the command
53-
arguments := append([]string{subcommand, "--machine-readable"}, args...)
53+
arguments := b.client.buildArguments(subcommand)
54+
arguments = append(arguments, args...)
5455
if b.AdditionalArgs != nil && len(b.AdditionalArgs) > 0 {
5556
arguments = append(arguments, b.AdditionalArgs...)
5657
}

base_command_test.go

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package vagrant
2+
3+
import (
4+
"io"
5+
"testing"
6+
)
7+
8+
// Use this method in tests to cleanup a BaseCommand that was init()'d but not
9+
// executed. init() creates a goroutine that needs to be cleaned up.
10+
func (b *BaseCommand) cleanup() {
11+
if b.cmd == nil {
12+
return
13+
}
14+
15+
// closing Stdout will cause the goroutine to exit
16+
b.cmd.Stdout.(io.Closer).Close()
17+
18+
// wait for the goroutine to exit - should be immediate
19+
b.readers.Wait()
20+
21+
// cleanup
22+
b.cmd = nil
23+
b.readers = nil
24+
}
25+
26+
func TestBaseCommand_init(t *testing.T) {
27+
client := newMockVagrantClient()
28+
handler := newMockOutputHandler()
29+
cmd := newBaseCommand(client)
30+
cmd.Env = []string{"ENV1=value1"}
31+
cmd.init(handler, "test", "arg1")
32+
defer cmd.cleanup()
33+
34+
if cmd.cmd.Path != client.executable {
35+
t.Errorf("Expected cmd path to be %v; got %v", client.executable, cmd.cmd.Path)
36+
}
37+
38+
argsLength := len(cmd.cmd.Args)
39+
if argsLength < 6 {
40+
t.Errorf("Expected len(args) to be at least 6; got %v", argsLength)
41+
} else {
42+
if cmd.cmd.Args[0] != client.executable {
43+
t.Errorf("Expected args[0] to be %v; got %v", client.executable, cmd.cmd.Args[0])
44+
}
45+
if cmd.cmd.Args[argsLength-3] != "test" {
46+
t.Errorf("Expected args[1] to be 'test'; got %v", cmd.cmd.Args[argsLength-3])
47+
}
48+
if cmd.cmd.Args[argsLength-2] != "--machine-readable" {
49+
t.Errorf("Expected args[1] to be '--machine-readable'; got %v", cmd.cmd.Args[argsLength-2])
50+
}
51+
if cmd.cmd.Args[argsLength-1] != "arg1" {
52+
t.Errorf("Expected args[2] to be 'arg1'; got %v", cmd.cmd.Args[argsLength-1])
53+
}
54+
}
55+
56+
if cmd.cmd.Env == nil {
57+
t.Errorf("Expected cmd.Env to be set")
58+
} else if cmd.cmd.Env[0] != "ENV1=value1" {
59+
t.Errorf("Expected env[0] to be 'ENV1=value1'; got %v", cmd.cmd.Env[0])
60+
}
61+
62+
if cmd.cmd.Dir != client.VagrantfileDir {
63+
t.Errorf("Expected Dir to be %v; got %v", client.VagrantfileDir, cmd.cmd.Dir)
64+
}
65+
}
66+
67+
func TestBaseCommand_Run(t *testing.T) {
68+
client := newMockVagrantClient()
69+
handler := newMockOutputHandler()
70+
cmd := newBaseCommand(client)
71+
cmd.init(handler, "test", "arg1")
72+
73+
if err := cmd.Run(); err != nil {
74+
t.Fatalf("Error: %v", err)
75+
}
76+
77+
if handler.subcommand != "test" {
78+
t.Errorf("Expected subcommand 'test'; got %v", handler.subcommand)
79+
}
80+
if !handler.machineReadable {
81+
t.Errorf("Expected machine-readable")
82+
}
83+
84+
if len(handler.args) != 1 {
85+
t.Fatalf("Expected 1 arg; got %v", len(handler.args))
86+
}
87+
if handler.args[0] != "arg1" {
88+
t.Errorf("Expected arg 1 to be 'arg1'; got %v", handler.args[0])
89+
}
90+
}

command_destroy.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ func (cmd *DestroyCommand) init() error {
4444

4545
// Run the command
4646
func (cmd *DestroyCommand) Run() error {
47-
if err := cmd.init(); err != nil {
47+
if err := cmd.Start(); err != nil {
4848
return err
4949
}
50-
return cmd.BaseCommand.Run()
50+
return cmd.Wait()
5151
}
5252

5353
// Start the command. You must call Wait() to complete execution.

command_destroy_test.go

+21
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@ import (
44
"testing"
55
)
66

7+
func init() {
8+
successfulOutput["destroy"] = `
9+
1534347289,default,metadata,provider,virtualbox
10+
1534347290,default,action,destroy,start
11+
1534347290,default,ui,info,==> default: Forcing shutdown of VM...
12+
1534347292,default,ui,info,==> default: Destroying VM and associated drives...
13+
1534347292,default,action,destroy,end
14+
`
15+
}
16+
717
func TestDestroyCommand_buildArguments(t *testing.T) {
818
client := newMockVagrantClient()
919

@@ -27,3 +37,14 @@ func TestDestroyCommand_buildArguments(t *testing.T) {
2737
assertArguments(t, args, "--force", "--parallel")
2838
})
2939
}
40+
41+
func TestDestroyCommand_Run(t *testing.T) {
42+
client := newMockVagrantClient()
43+
cmd := client.Destroy()
44+
if err := cmd.Run(); err != nil {
45+
t.Fatalf("Command failed to run: %v", err)
46+
}
47+
if cmd.Error != nil {
48+
t.Fatalf("Command returned error: %v", cmd.Error)
49+
}
50+
}

command_halt.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ func (cmd *HaltCommand) init() error {
3535

3636
// Run the command
3737
func (cmd *HaltCommand) Run() error {
38-
if err := cmd.init(); err != nil {
38+
if err := cmd.Start(); err != nil {
3939
return err
4040
}
41-
return cmd.BaseCommand.Run()
41+
return cmd.Wait()
4242
}
4343

4444
// Start the command. You must call Wait() to complete execution.

command_halt_test.go

+20
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@ import (
44
"testing"
55
)
66

7+
func init() {
8+
successfulOutput["halt"] = `
9+
1534863784,default,metadata,provider,virtualbox
10+
1534863784,default,action,halt,start
11+
1534863784,default,ui,info,==> default: VM not created. Moving on...
12+
1534863784,default,action,halt,end
13+
`
14+
}
15+
716
func TestHaltCommand_buildArguments(t *testing.T) {
817
client := newMockVagrantClient()
918

@@ -20,3 +29,14 @@ func TestHaltCommand_buildArguments(t *testing.T) {
2029
assertArguments(t, args, "--force")
2130
})
2231
}
32+
33+
func TestHaltCommand_Run(t *testing.T) {
34+
client := newMockVagrantClient()
35+
cmd := client.Halt()
36+
if err := cmd.Run(); err != nil {
37+
t.Fatalf("Command failed to run: %v", err)
38+
}
39+
if cmd.Error != nil {
40+
t.Fatalf("Command returned error: %v", cmd.Error)
41+
}
42+
}

command_port.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ func (cmd *PortCommand) init() error {
2222

2323
// Run the command
2424
func (cmd *PortCommand) Run() error {
25-
if err := cmd.init(); err != nil {
25+
if err := cmd.Start(); err != nil {
2626
return err
2727
}
28-
return cmd.BaseCommand.Run()
28+
return cmd.Wait()
2929
}
3030

3131
// Start the command. You must call Wait() to complete execution.

command_port_test.go

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package vagrant
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func init() {
8+
successfulOutput["port"] = `
9+
1534865103,default,metadata,provider,virtualbox
10+
1534865103,,ui,info,The forwarded ports for the machine are listed below. Please note that\nthese values may differ from values configured in the Vagrantfile if the\nprovider supports automatic port collision detection and resolution.
11+
1534865103,,ui,info,
12+
1534865103,,ui,info, 22 (guest) => 2222 (host)
13+
1534865103,default,forwarded_port,22,2222
14+
`
15+
}
16+
17+
func TestPortCommand_Run(t *testing.T) {
18+
client := newMockVagrantClient()
19+
cmd := client.Port()
20+
if err := cmd.Run(); err != nil {
21+
t.Fatalf("Command failed to run: %v", err)
22+
}
23+
if cmd.Error != nil {
24+
t.Fatalf("Command return error: %v", cmd.Error)
25+
}
26+
27+
if len(cmd.ForwardedPorts) != 1 {
28+
t.Fatalf("Expected forwarded ports for 1 VM; got %v", len(cmd.ForwardedPorts))
29+
}
30+
31+
forwardedPorts, ok := cmd.ForwardedPorts["default"]
32+
if !ok {
33+
t.Fatal("Expected forwarded ports for 'default', but there were none.")
34+
}
35+
36+
if len(forwardedPorts) != 1 {
37+
t.Fatalf("Expected 1 forwarded port; got %v", len(forwardedPorts))
38+
}
39+
if forwardedPorts[0].Guest != 22 || forwardedPorts[0].Host != 2222 {
40+
t.Errorf("Expected guest port 22 -> host 2222; got %v", forwardedPorts[0])
41+
}
42+
}

command_provision.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ func (cmd *ProvisionCommand) init() error {
2424

2525
// Run the command
2626
func (cmd *ProvisionCommand) Run() error {
27-
if err := cmd.init(); err != nil {
27+
if err := cmd.Start(); err != nil {
2828
return err
2929
}
30-
return cmd.BaseCommand.Run()
30+
return cmd.Wait()
3131
}
3232

3333
// Start the command. You must call Wait() to complete execution.

command_provision_test.go

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package vagrant
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func init() {
8+
successfulOutput["provision"] = `
9+
1534897890,default,metadata,provider,virtualbox
10+
1534897890,default,action,provision,start
11+
1534897890,default,action,provision,end
12+
`
13+
}
14+
15+
func TestProvisionCommand_Run(t *testing.T) {
16+
client := newMockVagrantClient()
17+
cmd := client.Provision()
18+
if err := cmd.Run(); err != nil {
19+
t.Fatalf("Command failed to run: %v", err)
20+
}
21+
if cmd.Error != nil {
22+
t.Fatalf("Command returned error: %v", cmd.Error)
23+
}
24+
}

command_reload.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ func (cmd *ReloadCommand) init() error {
2424

2525
// Run the command
2626
func (cmd *ReloadCommand) Run() error {
27-
if err := cmd.init(); err != nil {
27+
if err := cmd.Start(); err != nil {
2828
return err
2929
}
30-
return cmd.BaseCommand.Run()
30+
return cmd.Wait()
3131
}
3232

3333
// Start the command. You must call Wait() to complete execution.

command_reload_test.go

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package vagrant
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func init() {
8+
successfulOutput["reload"] = `
9+
1534898387,default,metadata,provider,virtualbox
10+
1534898387,default,action,reload,start
11+
1534898388,default,ui,output,==> default: Attempting graceful shutdown of VM...
12+
1534898390,default,ui,output,==> default: Checking if box 'coreos-stable' is up to date...
13+
1534898391,default,ui,info,==> default: Clearing any previously set forwarded ports...
14+
1534898392,default,ui,info,==> default: Clearing any previously set network interfaces...
15+
1534898392,default,ui,output,==> default: Preparing network interfaces based on configuration...
16+
1534898392,default,ui,detail, default: Adapter 1: nat
17+
1534898392,default,ui,output,==> default: Forwarding ports...
18+
1534898392,default,ui,detail, default: 22 (guest) => 2222 (host) (adapter 1)
19+
1534898392,default,ui,info,==> default: Running 'pre-boot' VM customizations...
20+
1534898392,default,ui,info,==> default: Booting VM...
21+
1534898392,default,ui,output,==> default: Waiting for machine to boot. This may take a few minutes...
22+
1534898393,default,ui,detail, default: SSH address: 127.0.0.1:2222
23+
1534898393,default,ui,detail, default: SSH username: core
24+
1534898393,default,ui,detail, default: SSH auth method: private key
25+
1534898408,default,ui,output,==> default: Machine booted and ready!
26+
1534898409,default,ui,info,==> default: Machine already provisioned. Run 'vagrant provision' or use the '--provision'\n==> default: flag to force provisioning. Provisioners marked to run always will still run.
27+
1534898409,default,action,reload,end
28+
`
29+
}
30+
31+
func TestReloadCommand_Run(t *testing.T) {
32+
client := newMockVagrantClient()
33+
cmd := client.Reload()
34+
if err := cmd.Run(); err != nil {
35+
t.Fatalf("Command failed to run: %v", err)
36+
}
37+
if cmd.Error != nil {
38+
t.Fatalf("Command returned error: %v", cmd.Error)
39+
}
40+
}

command_resume.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ func (cmd *ResumeCommand) init() error {
2424

2525
// Run the command
2626
func (cmd *ResumeCommand) Run() error {
27-
if err := cmd.init(); err != nil {
27+
if err := cmd.Start(); err != nil {
2828
return err
2929
}
30-
return cmd.BaseCommand.Run()
30+
return cmd.Wait()
3131
}
3232

3333
// Start the command. You must call Wait() to complete execution.

command_resume_test.go

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package vagrant
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func init() {
8+
successfulOutput["resume"] = `
9+
1534947403,default,metadata,provider,virtualbox
10+
1534947403,default,action,resume,start
11+
1534947403,default,ui,info,==> default: Resuming suspended VM...
12+
1534947403,default,ui,info,==> default: Booting VM...
13+
1534947405,default,ui,output,==> default: Waiting for machine to boot. This may take a few minutes...
14+
1534947406,default,ui,detail, default: SSH address: 127.0.0.1:2222
15+
1534947406,default,ui,detail, default: SSH username: core
16+
1534947406,default,ui,detail, default: SSH auth method: private key
17+
1534947413,default,ui,output,==> default: Machine booted and ready!
18+
1534947413,default,ui,info,==> default: Machine already provisioned. Run 'vagrant provision' or use the '--provision'\n==> default: flag to force provisioning. Provisioners marked to run always will still run.
19+
1534947413,default,action,resume,end
20+
`
21+
}
22+
23+
func TestResumeCommand_Run(t *testing.T) {
24+
client := newMockVagrantClient()
25+
cmd := client.Resume()
26+
if err := cmd.Run(); err != nil {
27+
t.Fatalf("Command failed to run: %v", err)
28+
}
29+
if cmd.Error != nil {
30+
t.Fatalf("Command returned error: %v", cmd.Error)
31+
}
32+
}

command_sshconfig.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ func (cmd *SSHConfigCommand) init() error {
3535

3636
// Run the command
3737
func (cmd *SSHConfigCommand) Run() error {
38-
if err := cmd.init(); err != nil {
38+
if err := cmd.Start(); err != nil {
3939
return err
4040
}
41-
return cmd.BaseCommand.Run()
41+
return cmd.Wait()
4242
}
4343

4444
// Start the command. You must call Wait() to complete execution.

0 commit comments

Comments
 (0)