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

Commit 56dd8b8

Browse files
committed
Test the Vagrant driver
1 parent 2081b15 commit 56dd8b8

File tree

3 files changed

+55
-10
lines changed

3 files changed

+55
-10
lines changed

driver/vagrant/driver.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ const (
2727

2828
// Driver is a driver based on Vagrant.
2929
type Driver struct {
30-
machine string
31-
sshClient *openssh.Client
32-
rsyncClient *rsync.Client
30+
Machine string
31+
SSHClient *openssh.Client
32+
RsyncClient *rsync.Client
3333
}
3434

3535
func init() {
@@ -75,15 +75,15 @@ func NewDriver(machine string) (*Driver, error) {
7575

7676
// RunCommand runs the specified command on the Vagrant machine.
7777
func (drv Driver) RunCommand(args []string) error {
78-
return drv.sshClient.RunCommand(args)
78+
return drv.SSHClient.RunCommand(args)
7979
}
8080

8181
// Upload copies files to the Vagrant machine.
8282
func (drv Driver) Upload(dst string, src ...string) error {
83-
return drv.rsyncClient.Copy(dst, src...)
83+
return drv.RsyncClient.Copy(dst, src...)
8484
}
8585

8686
// String returns the driver's name.
8787
func (drv Driver) String() string {
88-
return fmt.Sprintf("Vagrant driver (machine: %s)", drv.machine)
88+
return fmt.Sprintf("Vagrant driver (machine: %s)", drv.Machine)
8989
}

driver/vagrant/driver_test.go

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,44 @@
1-
package vagrant
1+
package vagrant_test
22

33
import (
4+
"os"
45
"testing"
56

67
"github.com/mlafeldt/chef-runner/driver"
8+
. "github.com/mlafeldt/chef-runner/driver/vagrant"
9+
"github.com/mlafeldt/chef-runner/log"
10+
"github.com/mlafeldt/chef-runner/util"
711
"github.com/stretchr/testify/assert"
812
)
913

14+
func init() {
15+
// Be quiet during testing
16+
log.SetLevel(log.LevelWarn)
17+
}
18+
1019
func TestDriverInterface(t *testing.T) {
1120
assert.Implements(t, (*driver.Driver)(nil), new(Driver))
1221
}
1322

23+
func TestNewDriver(t *testing.T) {
24+
util.InDir("../../testdata", func() {
25+
oldPath := os.Getenv("PATH")
26+
os.Setenv("PATH", "./bin:/usr/bin:/bin")
27+
defer os.Setenv("PATH", oldPath)
28+
29+
drv, err := NewDriver("some-machine")
30+
if assert.NoError(t, err) {
31+
defer os.RemoveAll(".chef-runner")
32+
assert.Equal(t, "default", drv.SSHClient.Host)
33+
assert.Equal(t, ".chef-runner/vagrant/machines/some-machine/ssh_config",
34+
drv.SSHClient.ConfigFile)
35+
assert.Equal(t, "default", drv.RsyncClient.RemoteHost)
36+
}
37+
})
38+
}
39+
1440
func TestString(t *testing.T) {
1541
expect := "Vagrant driver (machine: some-machine)"
16-
actual := Driver{machine: "some-machine"}.String()
42+
actual := Driver{Machine: "some-machine"}.String()
1743
assert.Equal(t, expect, actual)
1844
}
19-
20-
// TODO: test driver machinery

testdata/bin/vagrant

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/bin/sh
2+
# Vagrant stub for testing
3+
4+
if test "$1 $2" = "ssh-config some-machine"; then
5+
cat <<EOF
6+
Host default
7+
HostName 127.0.0.1
8+
User vagrant
9+
Port 2200
10+
UserKnownHostsFile /dev/null
11+
StrictHostKeyChecking no
12+
PasswordAuthentication no
13+
IdentityFile /Users/mlafeldt/.vagrant.d/insecure_private_key
14+
IdentitiesOnly yes
15+
LogLevel FATAL
16+
EOF
17+
exit 0
18+
fi
19+
20+
echo >&2 "invalid test args"
21+
exit 1

0 commit comments

Comments
 (0)