LEFT | RIGHT |
1 package main_test | 1 package main_test |
2 | 2 |
3 import ( | 3 import ( |
4 "flag" | 4 "flag" |
5 "fmt" | 5 "fmt" |
6 "io/ioutil" | 6 "io/ioutil" |
7 . "launchpad.net/gocheck" | 7 . "launchpad.net/gocheck" |
8 main "launchpad.net/juju/go/cmd/juju" | 8 main "launchpad.net/juju/go/cmd/juju" |
9 "os" | 9 "os" |
10 "os/exec" | 10 "os/exec" |
(...skipping 16 matching lines...) Expand all Loading... |
27 func TestRunMain(t *testing.T) { | 27 func TestRunMain(t *testing.T) { |
28 if *flagRunMain { | 28 if *flagRunMain { |
29 main.Main(flag.Args()) | 29 main.Main(flag.Args()) |
30 } | 30 } |
31 } | 31 } |
32 | 32 |
33 func badrun(c *C, exit int, cmd ...string) []string { | 33 func badrun(c *C, exit int, cmd ...string) []string { |
34 args := append([]string{"-test.run", "TestRunMain", "-run-main", "--", "
juju"}, cmd...) | 34 args := append([]string{"-test.run", "TestRunMain", "-run-main", "--", "
juju"}, cmd...) |
35 ps := exec.Command(os.Args[0], args...) | 35 ps := exec.Command(os.Args[0], args...) |
36 output, err := ps.CombinedOutput() | 36 output, err := ps.CombinedOutput() |
37 » c.Assert(err, ErrorMatches, fmt.Sprintf("exit status %d", exit)) | 37 » if exit != 0 { |
| 38 » » c.Assert(err, ErrorMatches, fmt.Sprintf("exit status %d", exit)) |
| 39 » } |
38 return strings.Split(string(output), "\n") | 40 return strings.Split(string(output), "\n") |
| 41 } |
| 42 |
| 43 func assertError(c *C, lines []string, err string) { |
| 44 c.Assert(lines[len(lines)-2], Equals, err) |
| 45 c.Assert(lines[len(lines)-1], Equals, "") |
39 } | 46 } |
40 | 47 |
41 func (s *MainSuite) TestActualRunNoCommand(c *C) { | 48 func (s *MainSuite) TestActualRunNoCommand(c *C) { |
42 // Check error when command not specified | 49 // Check error when command not specified |
43 lines := badrun(c, 2) | 50 lines := badrun(c, 2) |
44 » c.Assert(lines[0], Equals, "no command specified") | 51 » c.Assert(lines[0], Equals, "usage: juju [options] <command> ...") |
45 » c.Assert(lines[1], Equals, "usage: juju <command> [options] ...") | 52 » assertError(c, lines, "error: no command specified") |
46 } | 53 } |
47 | 54 |
48 func (s *MainSuite) TestActualRunBadCommand(c *C) { | 55 func (s *MainSuite) TestActualRunBadCommand(c *C) { |
49 // Check error when command unknown | 56 // Check error when command unknown |
50 lines := badrun(c, 2, "discombobulate") | 57 lines := badrun(c, 2, "discombobulate") |
51 » c.Assert(lines[0], Equals, "unrecognised command: juju discombobulate") | 58 » c.Assert(lines[0], Equals, "usage: juju [options] <command> ...") |
52 » c.Assert(lines[1], Equals, "usage: juju <command> [options] ...") | 59 » assertError(c, lines, "error: unrecognised command: juju discombobulate"
) |
53 } | 60 } |
54 | 61 |
55 func (s *MainSuite) TestActualRunBadJujuArg(c *C) { | 62 func (s *MainSuite) TestActualRunBadJujuArg(c *C) { |
56 // Check error when unknown option specified before command | 63 // Check error when unknown option specified before command |
57 lines := badrun(c, 2, "--cheese", "bootstrap") | 64 lines := badrun(c, 2, "--cheese", "bootstrap") |
58 » c.Assert(lines[0], Equals, "flag provided but not defined: --cheese") | 65 » c.Assert(lines[0], Equals, "usage: juju [options] <command> ...") |
59 » c.Assert(lines[1], Equals, "usage: juju <command> [options] ...") | 66 » assertError(c, lines, "error: flag provided but not defined: --cheese") |
60 } | 67 } |
61 | 68 |
62 func (s *MainSuite) TestActualRunBadBootstrapArg(c *C) { | 69 func (s *MainSuite) TestActualRunBadBootstrapArg(c *C) { |
63 // Check error when unknown option specified after command | 70 // Check error when unknown option specified after command |
64 lines := badrun(c, 2, "bootstrap", "--cheese") | 71 lines := badrun(c, 2, "bootstrap", "--cheese") |
65 » c.Assert(lines[0], Equals, "flag provided but not defined: --cheese") | 72 » c.Assert(lines[0], Equals, "usage: juju bootstrap [options]") |
66 » c.Assert(lines[1], Equals, "usage: juju bootstrap [options]") | 73 » assertError(c, lines, "error: flag provided but not defined: --cheese") |
67 } | 74 } |
68 | 75 |
69 func (s *MainSuite) TestActualRunSubcmdArgWorksNotInterspersed(c *C) { | 76 func (s *MainSuite) TestActualRunSubcmdArgWorksNotInterspersed(c *C) { |
70 // Check error when otherwise-valid option specified before command | 77 // Check error when otherwise-valid option specified before command |
71 lines := badrun(c, 2, "--environment", "erewhon", "bootstrap") | 78 lines := badrun(c, 2, "--environment", "erewhon", "bootstrap") |
72 » c.Assert(lines[0], Equals, "flag provided but not defined: --environment
") | 79 » c.Assert(lines[0], Equals, "usage: juju [options] <command> ...") |
73 » c.Assert(lines[1], Equals, "usage: juju <command> [options] ...") | 80 » assertError(c, lines, "error: flag provided but not defined: --environme
nt") |
74 } | 81 } |
75 | 82 |
76 var brokenConfig = ` | 83 var brokenConfig = ` |
77 environments: | 84 environments: |
78 one: | 85 one: |
79 type: dummy | 86 type: dummy |
80 zookeeper: false | 87 zookeeper: false |
81 broken: true | 88 broken: true |
82 ` | 89 ` |
83 | 90 |
(...skipping 13 matching lines...) Expand all Loading... |
97 msg := "broken environment" | 104 msg := "broken environment" |
98 return msg, func() { os.Setenv("HOME", home) } | 105 return msg, func() { os.Setenv("HOME", home) } |
99 } | 106 } |
100 | 107 |
101 func (s *MainSuite) TestActualRunJujuArgsBeforeCommand(c *C) { | 108 func (s *MainSuite) TestActualRunJujuArgsBeforeCommand(c *C) { |
102 // Check global args work when specified before command | 109 // Check global args work when specified before command |
103 msg, unbreak := breakJuju(c) | 110 msg, unbreak := breakJuju(c) |
104 defer unbreak() | 111 defer unbreak() |
105 logpath := filepath.Join(c.MkDir(), "log") | 112 logpath := filepath.Join(c.MkDir(), "log") |
106 lines := badrun(c, 1, "--log-file", logpath, "--verbose", "--debug", "bo
otstrap") | 113 lines := badrun(c, 1, "--log-file", logpath, "--verbose", "--debug", "bo
otstrap") |
107 » c.Assert(lines[0], Equals, msg) | 114 » assertError(c, lines, "error: "+msg) |
108 content, err := ioutil.ReadFile(logpath) | 115 content, err := ioutil.ReadFile(logpath) |
109 c.Assert(err, IsNil) | 116 c.Assert(err, IsNil) |
110 fullmsg := fmt.Sprintf(`.* JUJU:DEBUG juju bootstrap command failed: %s\
n`, msg) | 117 fullmsg := fmt.Sprintf(`.* JUJU:DEBUG juju bootstrap command failed: %s\
n`, msg) |
111 c.Assert(string(content), Matches, fullmsg) | 118 c.Assert(string(content), Matches, fullmsg) |
112 } | 119 } |
113 | 120 |
114 func (s *MainSuite) TestActualRunJujuArgsAfterCommand(c *C) { | 121 func (s *MainSuite) TestActualRunJujuArgsAfterCommand(c *C) { |
115 // Check global args work when specified after command | 122 // Check global args work when specified after command |
116 msg, unbreak := breakJuju(c) | 123 msg, unbreak := breakJuju(c) |
117 defer unbreak() | 124 defer unbreak() |
118 logpath := filepath.Join(c.MkDir(), "log") | 125 logpath := filepath.Join(c.MkDir(), "log") |
119 lines := badrun(c, 1, "bootstrap", "--log-file", logpath, "--verbose", "
--debug") | 126 lines := badrun(c, 1, "bootstrap", "--log-file", logpath, "--verbose", "
--debug") |
120 » c.Assert(lines[0], Equals, msg) | 127 » assertError(c, lines, "error: "+msg) |
121 content, err := ioutil.ReadFile(logpath) | 128 content, err := ioutil.ReadFile(logpath) |
122 c.Assert(err, IsNil) | 129 c.Assert(err, IsNil) |
123 fullmsg := fmt.Sprintf(`.* JUJU:DEBUG juju bootstrap command failed: %s\
n`, msg) | 130 fullmsg := fmt.Sprintf(`.* JUJU:DEBUG juju bootstrap command failed: %s\
n`, msg) |
124 c.Assert(string(content), Matches, fullmsg) | 131 c.Assert(string(content), Matches, fullmsg) |
125 } | 132 } |
126 | 133 |
127 var commandNames = []string{ | 134 var commandNames = []string{ |
128 "bootstrap", | 135 "bootstrap", |
129 "destroy-environment", | 136 "destroy-environment", |
130 } | 137 } |
131 | 138 |
132 func (s *MainSuite) TestHelp(c *C) { | 139 func (s *MainSuite) TestHelp(c *C) { |
133 // Check that we have correctly registered all the commands | 140 // Check that we have correctly registered all the commands |
134 // by checking the help output. | 141 // by checking the help output. |
135 | 142 |
136 » lines := badrun(c, 2, "-help") | 143 » lines := badrun(c, 0, "-help") |
137 » c.Assert(lines[0], Matches, "flag: help requested.*") | 144 » c.Assert(lines[0], Matches, `usage: juju .*`) |
138 | 145 |
139 for ; len(lines) > 0; lines = lines[1:] { | 146 for ; len(lines) > 0; lines = lines[1:] { |
140 if lines[0] == "commands:" { | 147 if lines[0] == "commands:" { |
141 break | 148 break |
142 } | 149 } |
143 } | 150 } |
144 c.Assert(lines, Not(HasLen), 0) | 151 c.Assert(lines, Not(HasLen), 0) |
145 | 152 |
146 var names []string | 153 var names []string |
147 for lines = lines[1:]; len(lines) > 0; lines = lines[1:] { | 154 for lines = lines[1:]; len(lines) > 0; lines = lines[1:] { |
148 f := strings.Fields(lines[0]) | 155 f := strings.Fields(lines[0]) |
149 if len(f) == 0 { | 156 if len(f) == 0 { |
150 continue | 157 continue |
151 } | 158 } |
152 c.Assert(f, Not(HasLen), 0) | 159 c.Assert(f, Not(HasLen), 0) |
153 names = append(names, f[0]) | 160 names = append(names, f[0]) |
154 } | 161 } |
155 sort.Strings(names) | 162 sort.Strings(names) |
156 c.Assert(names, DeepEquals, commandNames) | 163 c.Assert(names, DeepEquals, commandNames) |
157 } | 164 } |
LEFT | RIGHT |