@@ -3,6 +3,8 @@ package cmd
33import (
44 "strings"
55 "testing"
6+
7+ "github.com/spf13/viper"
68)
79
810func TestRootCmdUsesRunE (t * testing.T ) {
@@ -32,3 +34,71 @@ func TestExecuteReturnsErrorForMissingHosts(t *testing.T) {
3234 t .Fatalf ("unexpected error: %v" , err )
3335 }
3436}
37+
38+ func TestExecuteReturnsErrorForInvalidHostSpec (t * testing.T ) {
39+ prevHostGroup := hostGroup
40+ prevHostsFile := hostsFile
41+ t .Cleanup (func () {
42+ hostGroup = prevHostGroup
43+ hostsFile = prevHostsFile
44+ RootCmd .SetArgs (nil )
45+ })
46+
47+ hostGroup = ""
48+ hostsFile = ""
49+ RootCmd .SetArgs ([]string {":" })
50+
51+ err := Execute ()
52+ if err == nil {
53+ t .Fatalf ("expected error" )
54+ }
55+ if ! strings .Contains (err .Error (), "invalid host" ) {
56+ t .Fatalf ("unexpected error: %v" , err )
57+ }
58+ }
59+
60+ func TestExecuteReturnsErrorWhenHostsFileCannotBeRead (t * testing.T ) {
61+ prevHostGroup := hostGroup
62+ prevHostsFile := hostsFile
63+ t .Cleanup (func () {
64+ hostGroup = prevHostGroup
65+ hostsFile = prevHostsFile
66+ RootCmd .SetArgs (nil )
67+ })
68+
69+ hostGroup = ""
70+ hostsFile = "/path/that/does/not/exist"
71+ RootCmd .SetArgs ([]string {"host1" })
72+
73+ err := Execute ()
74+ if err == nil {
75+ t .Fatalf ("expected error" )
76+ }
77+ if ! strings .Contains (err .Error (), "unable to read hostsFile" ) {
78+ t .Fatalf ("unexpected error: %v" , err )
79+ }
80+ }
81+
82+ func TestExecuteReturnsErrorForInvalidGroupSpec (t * testing.T ) {
83+ prevHostGroup := hostGroup
84+ prevHostsFile := hostsFile
85+ t .Cleanup (func () {
86+ hostGroup = prevHostGroup
87+ hostsFile = prevHostsFile
88+ viper .Set ("groups.bad" , nil )
89+ RootCmd .SetArgs (nil )
90+ })
91+
92+ viper .Set ("groups.bad" , map [string ]interface {}{"user" : "deploy" })
93+ hostGroup = "bad"
94+ hostsFile = ""
95+ RootCmd .SetArgs ([]string {"host1" })
96+
97+ err := Execute ()
98+ if err == nil {
99+ t .Fatalf ("expected error" )
100+ }
101+ if ! strings .Contains (err .Error (), "missing hosts" ) {
102+ t .Fatalf ("unexpected error: %v" , err )
103+ }
104+ }
0 commit comments