LEFT | RIGHT |
| 1 from yaml import dump |
| 2 |
1 from twisted.internet.defer import inlineCallbacks | 3 from twisted.internet.defer import inlineCallbacks |
2 from yaml import dump | |
3 | 4 |
4 from juju.control import main | 5 from juju.control import main |
| 6 from juju.state.environment import EnvironmentStateManager |
5 | 7 |
6 from .common import MachineControlToolTest | 8 from .common import MachineControlToolTest |
7 | 9 |
8 | 10 |
9 class ControlAddUnitTest(MachineControlToolTest): | 11 class ControlAddUnitTest(MachineControlToolTest): |
10 | 12 |
11 @inlineCallbacks | 13 @inlineCallbacks |
12 def setUp(self): | 14 def setUp(self): |
13 yield super(ControlAddUnitTest, self).setUp() | 15 yield super(ControlAddUnitTest, self).setUp() |
14 config = { | |
15 "environments": {"firstenv": {"type": "dummy"}}} | |
16 | |
17 self.write_config(dump(config)) | |
18 self.config.load() | |
19 self.service_state1 = yield self.add_service_from_charm("mysql") | 16 self.service_state1 = yield self.add_service_from_charm("mysql") |
20 self.service_unit1 = yield self.service_state1.add_unit_state() | 17 self.service_unit1 = yield self.service_state1.add_unit_state() |
21 self.machine_state1 = yield self.machine_state_manager.\ | 18 self.machine_state1 = yield self.add_machine_state() |
22 add_machine_state() | |
23 yield self.service_unit1.assign_to_machine(self.machine_state1) | 19 yield self.service_unit1.assign_to_machine(self.machine_state1) |
24 | 20 |
25 self.output = self.capture_logging() | 21 self.output = self.capture_logging() |
26 self.stderr = self.capture_stream("stderr") | 22 self.stderr = self.capture_stream("stderr") |
27 | 23 |
28 @inlineCallbacks | 24 @inlineCallbacks |
29 def test_add_unit(self): | 25 def test_add_unit(self): |
30 """ | 26 """ |
31 'juju add-unit <service_name>' will add a new service | 27 'juju add-unit <service_name>' will add a new service |
32 unit of the given service. | 28 unit of the given service. |
33 """ | 29 """ |
34 unit_names = yield self.service_state1.get_unit_names() | 30 unit_names = yield self.service_state1.get_unit_names() |
35 self.assertEqual(len(unit_names), 1) | 31 self.assertEqual(len(unit_names), 1) |
36 | 32 |
37 finished = self.setup_cli_reactor() | 33 finished = self.setup_cli_reactor() |
38 self.setup_exit(0) | 34 self.setup_exit(0) |
39 self.mocker.replay() | 35 self.mocker.replay() |
40 main(["add-unit", "mysql"]) | 36 # trash environment to check syncing |
41 yield finished | 37 yield self.client.delete("/environment") |
| 38 main(["add-unit", "mysql"]) |
| 39 yield finished |
| 40 |
| 41 # verify the env state was synced |
| 42 esm = EnvironmentStateManager(self.client) |
| 43 yield esm.get_config() |
42 | 44 |
43 # verify the unit and its machine assignment. | 45 # verify the unit and its machine assignment. |
44 unit_names = yield self.service_state1.get_unit_names() | 46 unit_names = yield self.service_state1.get_unit_names() |
45 self.assertEqual(len(unit_names), 2) | 47 self.assertEqual(len(unit_names), 2) |
46 | 48 |
47 topology = yield self.get_topology() | 49 topology = yield self.get_topology() |
48 unit = yield self.service_state1.get_unit_state("mysql/1") | 50 unit = yield self.service_state1.get_unit_state("mysql/1") |
49 machine_id = topology.get_service_unit_machine( | 51 machine_id = topology.get_service_unit_machine( |
50 self.service_state1.internal_id, unit.internal_id) | 52 self.service_state1.internal_id, unit.internal_id) |
51 self.assertNotEqual(machine_id, None) | 53 self.assertNotEqual(machine_id, None) |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 | 98 |
97 @inlineCallbacks | 99 @inlineCallbacks |
98 def test_add_unit_subordinate_service(self): | 100 def test_add_unit_subordinate_service(self): |
99 yield self.add_service_from_charm("logging") | 101 yield self.add_service_from_charm("logging") |
100 finished = self.setup_cli_reactor() | 102 finished = self.setup_cli_reactor() |
101 self.setup_exit(0) | 103 self.setup_exit(0) |
102 self.mocker.replay() | 104 self.mocker.replay() |
103 main(["add-unit", "logging"]) | 105 main(["add-unit", "logging"]) |
104 yield finished | 106 yield finished |
105 self.assertIn( | 107 self.assertIn( |
106 "Subordinate services acquire units with their principal service.", | 108 "Subordinate services acquire units from " |
| 109 "their principal service.", |
107 self.stderr.getvalue()) | 110 self.stderr.getvalue()) |
108 | 111 |
109 @inlineCallbacks | 112 @inlineCallbacks |
110 def test_add_unit_reuses_machines(self): | 113 def test_add_unit_reuses_machines(self): |
111 """Verify that if machines are not in use, add-unit uses them.""" | 114 """Verify that if machines are not in use, add-unit uses them.""" |
112 # add machine to wordpress, then destroy and reallocate later | 115 # add machine to wordpress, then destroy and reallocate later |
113 # in this test to mysql as mysql/1's machine | 116 # in this test to mysql as mysql/1's machine |
114 wordpress_service_state = yield self.add_service_from_charm( | 117 wordpress_service_state = yield self.add_service_from_charm( |
115 "wordpress") | 118 "wordpress") |
116 wordpress_unit_state = yield wordpress_service_state.add_unit_state() | 119 wordpress_unit_state = yield wordpress_service_state.add_unit_state() |
117 wordpress_machine_state = \ | 120 wordpress_machine_state = yield self.add_machine_state() |
118 yield self.machine_state_manager.add_machine_state() | |
119 yield wordpress_unit_state.assign_to_machine(wordpress_machine_state) | 121 yield wordpress_unit_state.assign_to_machine(wordpress_machine_state) |
120 yield wordpress_unit_state.unassign_from_machine() | 122 yield wordpress_unit_state.unassign_from_machine() |
121 | 123 |
122 finished = self.setup_cli_reactor() | 124 finished = self.setup_cli_reactor() |
123 self.setup_exit(0) | 125 self.setup_exit(0) |
124 self.mocker.replay() | 126 self.mocker.replay() |
125 main(["add-unit", "mysql"]) | 127 main(["add-unit", "mysql"]) |
126 yield finished | 128 yield finished |
127 self.assertIn( | 129 self.assertIn( |
128 "Unit 'mysql/1' added to service 'mysql'", | 130 "Unit 'mysql/1' added to service 'mysql'", |
129 self.output.getvalue()) | 131 self.output.getvalue()) |
130 yield self.assert_machine_assignments("wordpress", [None]) | 132 yield self.assert_machine_assignments("wordpress", [None]) |
131 yield self.assert_machine_assignments("mysql", [1, 2]) | 133 yield self.assert_machine_assignments("mysql", [1, 2]) |
132 | 134 |
133 @inlineCallbacks | 135 @inlineCallbacks |
134 def test_policy_from_environment(self): | 136 def test_policy_from_environment(self): |
135 config = { | 137 config = { |
136 "environments": {"firstenv": { | 138 "environments": {"firstenv": { |
137 "placement": "local", | 139 "placement": "local", |
138 "type": "dummy"}}} | 140 "type": "dummy"}}} |
139 | 141 yield self.push_config("firstenv", config) |
140 self.write_config(dump(config)) | |
141 self.config.load() | |
142 | 142 |
143 ms0 = yield self.machine_state_manager.get_machine_state(0) | 143 ms0 = yield self.machine_state_manager.get_machine_state(0) |
144 yield self.service_unit1.unassign_from_machine() | 144 yield self.service_unit1.unassign_from_machine() |
145 yield self.service_unit1.assign_to_machine(ms0) | 145 yield self.service_unit1.assign_to_machine(ms0) |
146 | 146 |
147 unit_names = yield self.service_state1.get_unit_names() | 147 unit_names = yield self.service_state1.get_unit_names() |
148 self.assertEqual(len(unit_names), 1) | 148 self.assertEqual(len(unit_names), 1) |
149 | 149 |
150 finished = self.setup_cli_reactor() | 150 finished = self.setup_cli_reactor() |
151 self.setup_exit(0) | 151 self.setup_exit(0) |
152 self.mocker.replay() | 152 self.mocker.replay() |
153 main(["add-unit", "mysql"]) | 153 main(["add-unit", "mysql"]) |
154 yield finished | 154 yield finished |
155 | 155 |
156 # Verify the local policy was used | 156 # Verify the local policy was used |
157 topology = yield self.get_topology() | 157 topology = yield self.get_topology() |
158 unit = yield self.service_state1.get_unit_state("mysql/1") | 158 unit = yield self.service_state1.get_unit_state("mysql/1") |
159 machine_id = topology.get_service_unit_machine( | 159 machine_id = topology.get_service_unit_machine( |
160 self.service_state1.internal_id, unit.internal_id) | 160 self.service_state1.internal_id, unit.internal_id) |
161 self.assertNotEqual(machine_id, None) | 161 self.assertNotEqual(machine_id, None) |
162 self.assertIn( | 162 self.assertIn( |
163 "Unit 'mysql/1' added to service 'mysql'", | 163 "Unit 'mysql/1' added to service 'mysql'", |
164 self.output.getvalue()) | 164 self.output.getvalue()) |
165 # adding a second unit still assigns to machine 0 with local policy | 165 # adding a second unit still assigns to machine 0 with local policy |
166 yield self.assert_machine_assignments("mysql", [0, 0]) | 166 yield self.assert_machine_assignments("mysql", [0, 0]) |
| 167 |
| 168 @inlineCallbacks |
| 169 def test_legacy_option_in_legacy_env(self): |
| 170 yield self.client.delete("/constraints") |
| 171 |
| 172 finished = self.setup_cli_reactor() |
| 173 self.setup_exit(0) |
| 174 self.mocker.replay() |
| 175 main(["add-unit", "mysql"]) |
| 176 yield finished |
| 177 |
| 178 unit_names = yield self.service_state1.get_unit_names() |
| 179 self.assertEqual(len(unit_names), 2) |
| 180 |
| 181 @inlineCallbacks |
| 182 def test_legacy_option_in_fresh_env(self): |
| 183 local_config = { |
| 184 "environments": {"firstenv": { |
| 185 "some-legacy-key": "blah", |
| 186 "type": "dummy"}}} |
| 187 self.write_config(dump(local_config)) |
| 188 self.config.load() |
| 189 |
| 190 finished = self.setup_cli_reactor() |
| 191 self.setup_exit(0) |
| 192 self.mocker.replay() |
| 193 main(["add-unit", "mysql"]) |
| 194 yield finished |
| 195 |
| 196 output = self.output.getvalue() |
| 197 self.assertIn( |
| 198 "Your environments.yaml contains deprecated keys", output) |
| 199 unit_names = yield self.service_state1.get_unit_names() |
| 200 self.assertEqual(len(unit_names), 1) |
LEFT | RIGHT |