OLD | NEW |
1 package main | 1 package main |
2 | 2 |
3 import ( | 3 import ( |
4 "encoding/base64" | 4 "encoding/base64" |
5 . "launchpad.net/gocheck" | 5 . "launchpad.net/gocheck" |
6 "launchpad.net/goyaml" | 6 "launchpad.net/goyaml" |
7 "launchpad.net/juju-core/cmd" | 7 "launchpad.net/juju-core/cmd" |
8 "launchpad.net/juju-core/juju/testing" | 8 "launchpad.net/juju-core/juju/testing" |
9 "launchpad.net/juju-core/state" | 9 "launchpad.net/juju-core/state" |
10 ) | 10 ) |
(...skipping 28 matching lines...) Expand all Loading... |
39 c.Assert(err, ErrorMatches, "--instance-id option must be set") | 39 c.Assert(err, ErrorMatches, "--instance-id option must be set") |
40 } | 40 } |
41 | 41 |
42 func (s *BootstrapSuite) TestParseNoEnvConfig(c *C) { | 42 func (s *BootstrapSuite) TestParseNoEnvConfig(c *C) { |
43 _, err := initBootstrapCommand([]string{"--instance-id", "x"}) | 43 _, err := initBootstrapCommand([]string{"--instance-id", "x"}) |
44 c.Assert(err, ErrorMatches, "--env-config option must be set") | 44 c.Assert(err, ErrorMatches, "--env-config option must be set") |
45 | 45 |
46 } | 46 } |
47 | 47 |
48 func (s *BootstrapSuite) TestSetMachineId(c *C) { | 48 func (s *BootstrapSuite) TestSetMachineId(c *C) { |
49 » args := []string{"--state-servers"} | 49 » args := []string{ |
50 » args = append(args, s.StateInfo(c).Addrs...) | 50 » » "--state-servers", s.StateInfo(c).Addrs[0], |
51 » args = append(args, "--instance-id", "over9000") | 51 » » "--instance-id", "over9000", |
52 » args = append(args, "--env-config", b64yaml{ | 52 » » "--env-config", b64yaml{ |
53 » » "name": "dummyenv", | 53 » » » "name": "dummyenv", |
54 » » "type": "dummy", | 54 » » » "type": "dummy", |
55 » » "state-server": "false", | 55 » » » "state-server": false, |
56 » » "authorized-keys": "i-am-a-key", | 56 » » » "authorized-keys": "i-am-a-key", |
57 » }.encode()) | 57 » » }.encode(), |
| 58 » } |
58 cmd, err := initBootstrapCommand(args) | 59 cmd, err := initBootstrapCommand(args) |
59 c.Assert(err, IsNil) | 60 c.Assert(err, IsNil) |
60 err = cmd.Run(nil) | 61 err = cmd.Run(nil) |
61 c.Assert(err, IsNil) | 62 c.Assert(err, IsNil) |
62 | 63 |
63 machines, err := s.State.AllMachines() | 64 machines, err := s.State.AllMachines() |
64 c.Assert(err, IsNil) | 65 c.Assert(err, IsNil) |
65 c.Assert(len(machines), Equals, 1) | 66 c.Assert(len(machines), Equals, 1) |
66 | 67 |
67 instid, err := machines[0].InstanceId() | 68 instid, err := machines[0].InstanceId() |
68 c.Assert(err, IsNil) | 69 c.Assert(err, IsNil) |
69 c.Assert(instid, Equals, "over9000") | 70 c.Assert(instid, Equals, "over9000") |
70 } | 71 } |
71 | 72 |
72 func (s *BootstrapSuite) TestMachinerWorkers(c *C) { | 73 func (s *BootstrapSuite) TestMachinerWorkers(c *C) { |
73 » args := []string{"--state-servers"} | 74 » args := []string{ |
74 » args = append(args, s.StateInfo(c).Addrs...) | 75 » » "--state-servers", s.StateInfo(c).Addrs[0], |
75 » args = append(args, "--instance-id", "over9000") | 76 » » "--instance-id", "over9000", |
76 » args = append(args, "--env-config", b64yaml{ | 77 » » "--env-config", b64yaml{ |
77 » » "name": "dummyenv", | 78 » » » "name": "dummyenv", |
78 » » "type": "dummy", | 79 » » » "type": "dummy", |
79 » » "state-server": "false", | 80 » » » "state-server": false, |
80 » » "authorized-keys": "i-am-a-key", | 81 » » » "authorized-keys": "i-am-a-key", |
81 » }.encode()) | 82 » » }.encode(), |
| 83 » } |
82 cmd, err := initBootstrapCommand(args) | 84 cmd, err := initBootstrapCommand(args) |
83 c.Assert(err, IsNil) | 85 c.Assert(err, IsNil) |
84 err = cmd.Run(nil) | 86 err = cmd.Run(nil) |
85 c.Assert(err, IsNil) | 87 c.Assert(err, IsNil) |
86 | 88 |
87 m, err := s.State.Machine(0) | 89 m, err := s.State.Machine(0) |
88 c.Assert(err, IsNil) | 90 c.Assert(err, IsNil) |
89 c.Assert(m.Workers(), DeepEquals, []state.WorkerKind{state.MachinerWorke
r, state.ProvisionerWorker, state.FirewallerWorker}) | 91 c.Assert(m.Workers(), DeepEquals, []state.WorkerKind{state.MachinerWorke
r, state.ProvisionerWorker, state.FirewallerWorker}) |
90 } | 92 } |
91 | 93 |
| 94 func testOpenState(c *C, info *state.Info, expectErr error) { |
| 95 st, err := state.Open(info) |
| 96 if st != nil { |
| 97 st.Close() |
| 98 } |
| 99 if expectErr != nil { |
| 100 c.Assert(err, Equals, expectErr) |
| 101 } else { |
| 102 c.Assert(err, IsNil) |
| 103 } |
| 104 } |
| 105 |
| 106 func (s *BootstrapSuite) TestInitialPassword(c *C) { |
| 107 // First get a state that's properly logged in, so |
| 108 // that we can reset the password if something goes wrong. |
| 109 err := s.State.SetAdminPassword("arble") |
| 110 c.Assert(err, IsNil) |
| 111 defer func() { |
| 112 if err := s.State.SetAdminPassword(""); err != nil { |
| 113 c.Errorf("cannot reset admin password: %v", err) |
| 114 } |
| 115 }() |
| 116 info := s.StateInfo(c) |
| 117 info.Password = "arble" |
| 118 st, err := state.Open(info) |
| 119 c.Assert(err, IsNil) |
| 120 defer st.Close() |
| 121 // Revert to previous passwordless state. |
| 122 err = st.SetAdminPassword("") |
| 123 c.Assert(err, IsNil) |
| 124 |
| 125 args := []string{ |
| 126 "--state-servers", s.StateInfo(c).Addrs[0], |
| 127 "--instance-id", "over9000", |
| 128 "--env-config", b64yaml{ |
| 129 "name": "dummyenv", |
| 130 "type": "dummy", |
| 131 "state-server": false, |
| 132 "authorized-keys": "i-am-a-key", |
| 133 }.encode(), |
| 134 "--initial-password", "foo", |
| 135 } |
| 136 cmd, err := initBootstrapCommand(args) |
| 137 c.Assert(err, IsNil) |
| 138 err = cmd.Run(nil) |
| 139 c.Assert(err, IsNil) |
| 140 |
| 141 // Check that we cannot now connect to the state |
| 142 // without a password. |
| 143 info = s.StateInfo(c) |
| 144 testOpenState(c, info, state.ErrUnauthorized) |
| 145 |
| 146 info.Password = "foo" |
| 147 testOpenState(c, info, nil) |
| 148 |
| 149 info.EntityName = "machine-0" |
| 150 testOpenState(c, info, nil) |
| 151 } |
| 152 |
92 var base64ConfigTests = []struct { | 153 var base64ConfigTests = []struct { |
93 input []string | 154 input []string |
94 err string | 155 err string |
95 expected map[string]interface{} | 156 expected map[string]interface{} |
96 }{ | 157 }{ |
97 { | 158 { |
98 // no value supplied | 159 // no value supplied |
99 nil, | 160 nil, |
100 "--env-config option must be set", | 161 "--env-config option must be set", |
101 nil, | 162 nil, |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 | 197 |
137 type b64yaml map[string]interface{} | 198 type b64yaml map[string]interface{} |
138 | 199 |
139 func (m b64yaml) encode() string { | 200 func (m b64yaml) encode() string { |
140 data, err := goyaml.Marshal(m) | 201 data, err := goyaml.Marshal(m) |
141 if err != nil { | 202 if err != nil { |
142 panic(err) | 203 panic(err) |
143 } | 204 } |
144 return base64.StdEncoding.EncodeToString(data) | 205 return base64.StdEncoding.EncodeToString(data) |
145 } | 206 } |
OLD | NEW |