def test_collect_with_unassigned_machines(self): yield self.build_topology() # get a service's units and unassign one of them wordpress = yield self.service_state_manager.get_service_state( "wordpress") units = yield wordpress.get_all_unit_states() # There is only a single wordpress machine in the topology. unit = units[0] machine_id = yield unit.get_assigned_machine_id() yield unit.unassign_from_machine() yield unit.set_public_address(None) # test that the machine is in state information w/o assignment state = yield status.collect(None, self.provider, self.client, None) # verify that the unassigned machine appears in the state self.assertEqual(state["machines"][machine_id], {"dns-name": "steamcloud-1.com", "instance-id": 0, "instance-state": "unknown", "state": "not-started"}) # verify that we have a record of the unassigned service; # but note that unassigning this machine without removing the # service unit and relation units now produces other dangling # records in the topology self.assertEqual( state["services"]["wordpress"]["units"], {"wordpress/0": {"machine": None, "public-address": None, "relations": { "cache": {"state": "up"}, "db": {"state": "up"}, "proxy": {"state": "up"}}, "state": "started"}})
def test_collect_with_removed_unit(self): yield self.build_topology() # get a service's units and unassign one of them wordpress = yield self.service_state_manager.get_service_state( "wordpress") units = yield wordpress.get_all_unit_states() # There is only a single wordpress machine in the topology. unit = units[0] machine_id = yield unit.get_assigned_machine_id() yield wordpress.remove_unit_state(unit) # test that wordpress has no assigned service units state = yield status.collect(None, self.provider, self.client, None) self.assertEqual( state["services"]["wordpress"], {"charm": "local:series/wordpress-3", "relations": {"cache": "memcache", "db": "mysql", "proxy": "varnish"}, "units": {}}) # but its machine is still available as reported by status seen_machines = set() for service, service_data in state["services"].iteritems(): for unit, unit_data in service_data["units"].iteritems(): seen_machines.add(unit_data["machine"]) self.assertIn(machine_id, state["machines"]) self.assertNotIn(machine_id, seen_machines)
def test_collect_with_removed_unit(self): yield self.build_topology() # get a service's units and unassign one of them wordpress = yield self.service_state_manager.get_service_state( "wordpress") units = yield wordpress.get_all_unit_states() # There is only a single wordpress machine in the topology. unit = units[0] machine_id = yield unit.get_assigned_machine_id() yield wordpress.remove_unit_state(unit) # test that wordpress has no assigned service units state = yield status.collect(None, self.provider, self.client, None) self.assertEqual( state["services"]["wordpress"], { "charm": "local:series/wordpress-3", "relations": { "cache": "memcache", "db": "mysql", "proxy": "varnish" }, "units": {} }) # but its machine is still available as reported by status seen_machines = set() for service, service_data in state["services"].iteritems(): for unit, unit_data in service_data["units"].iteritems(): seen_machines.add(unit_data["machine"]) self.assertIn(machine_id, state["machines"]) self.assertNotIn(machine_id, seen_machines)
def test_provider_pending_machine_state(self): # verify that we get some error reporting if the provider # doesn't have proper machine info yield self.build_topology() # add a new machine to the topology (but not the provider) # and status it m8 = yield self.machine_state_manager.add_machine_state() wordpress = yield self.service_state_manager.get_service_state( "wordpress") wpu = yield wordpress.add_unit_state() yield wpu.assign_to_machine(m8) # test that we identify we don't have machine state state = yield status.collect(None, self.provider, self.client, logging.getLogger()) self.assertEqual(state["machines"][7]["instance-id"], "pending")
def test_collect_with_unassigned_machines(self): yield self.build_topology() # get a service's units and unassign one of them wordpress = yield self.service_state_manager.get_service_state( "wordpress") units = yield wordpress.get_all_unit_states() # There is only a single wordpress machine in the topology. unit = units[0] machine_id = yield unit.get_assigned_machine_id() yield unit.unassign_from_machine() yield unit.set_public_address(None) # test that the machine is in state information w/o assignment state = yield status.collect(None, self.provider, self.client, None) # verify that the unassigned machine appears in the state self.assertEqual( state["machines"][machine_id], { "dns-name": "steamcloud-1.com", "instance-id": 0, "instance-state": "unknown", "state": "not-started" }) # verify that we have a record of the unassigned service; # but note that unassigning this machine without removing the # service unit and relation units now produces other dangling # records in the topology self.assertEqual( state["services"]["wordpress"]["units"], { "wordpress/0": { "machine": None, "public-address": None, "relations": { "cache": { "state": "up" }, "db": { "state": "up" }, "proxy": { "state": "up" } }, "state": "started" } })
def test_provider_pending_machine_state(self): # verify that we get some error reporting if the provider # doesn't have proper machine info yield self.build_topology() # add a new machine to the topology (but not the provider) # and status it m8 = yield self.machine_state_manager.add_machine_state() wordpress = yield self.service_state_manager.get_service_state( "wordpress") wpu = yield wordpress.add_unit_state() yield wpu.assign_to_machine(m8) # test that we identify we don't have machine state state = yield status.collect( None, self.provider, self.client, logging.getLogger()) self.assertEqual(state["machines"][7]["instance-id"], "pending")
def test_peer_relation(self): """Verify status works with peer relations. """ m1 = yield self.machine_state_manager.add_machine_state() m2 = yield self.machine_state_manager.add_machine_state() yield self.provider.start_machine({"machine-id": 0, "dns-name": "steamcloud-1.com"}) yield self.provider.start_machine({"machine-id": 1, "dns-name": "steamcloud-2.com"}) yield m1.set_instance_id(0) yield m2.set_instance_id(1) riak = yield self.add_service_from_charm("riak") riak_u1 = yield self.add_unit(riak, m1) riak_u2 = yield self.add_unit(riak, m2, with_agent=lambda _: False) yield self.set_unit_state(riak_u1, "started") yield self.set_unit_state(riak_u2, "started") _, (peer_rel,) = yield self.relation_state_manager.add_relation_state( RelationEndpoint("riak", "peer", "ring", "peer")) yield ZookeeperWorkflowState( self.client, (yield peer_rel.add_unit_state(riak_u1))).set_state("up") yield peer_rel.add_unit_state(riak_u2) state = yield status.collect( ["riak"], self.provider, self.client, None) self.assertEqual( state["services"]["riak"], {"charm": "local:series/riak-7", "relations": {"ring": "riak"}, "units": {"riak/0": {"machine": 0, "public-address": "riak-0.example.com", "relations": {"ring": {"state": "up"}}, "state": "started"}, "riak/1": {"machine": 1, "public-address": "riak-1.example.com", "relations": {"ring": {"state": None}}, "state": "down"}}})
def test_collect_filtering(self): yield self.build_topology() # collect by service name state = yield status.collect( ["wordpress"], self.provider, self.client, None) # Validate that only the expected service is present # in the state self.assertEqual(state["machines"].keys(), [0]) self.assertEqual(state["services"].keys(), ["wordpress"]) # collect by unit name state = yield status.collect(["*/0"], self.provider, self.client, None) self.assertEqual(set(state["machines"].keys()), set([0, 1, 3, 5])) self.assertEqual(set(state["services"].keys()), set(["memcache", "varnish", "mysql", "wordpress"])) # collect by unit name state = yield status.collect(["*/1"], self.provider, self.client, None) self.assertEqual(set(state["machines"].keys()), set([2, 4, 6])) # verify that only the proper units and services are present self.assertEqual( state["services"], {"memcache": { "charm": "local:series/dummy-1", "relations": {"cache": "wordpress"}, "units": { "memcache/1": { "machine": 6, "state": "installed", "public-address": "memcache-1.example.com", "relations": {"cache": {"state": "down"}}}}}, "mysql": { "exposed": True, "charm": "local:series/mysql-1", "relations": {"db": "wordpress"}, "units": { "mysql/1": { "machine": 2, "public-address": "mysql-1.example.com", "open-ports": [], "state": "stopped", "relations": {"db": {"state": "departed"}}}}}, "varnish": { "exposed": True, "charm": "local:series/varnish-1", "relations": {"proxy": "wordpress"}, "units": { "varnish/1": { "machine": 4, "public-address": "varnish-1.example.com", "open-ports": ["80/tcp"], "state": "started", "relations": {"proxy": {"state": "up"}}}}}}) # filter a missing service state = yield status.collect( ["cluehammer"], self.provider, self.client, None) self.assertEqual(set(state["machines"].keys()), set([])) self.assertEqual(set(state["services"].keys()), set([])) # filter a missing unit state = yield status.collect(["*/7"], self.provider, self.client, None) self.assertEqual(set(state["machines"].keys()), set([])) self.assertEqual(set(state["services"].keys()), set([]))
def test_collect(self): yield self.build_topology(skip_unit_agents=("varnish/1",)) agent = MachineAgent() options = TwistedOptionNamespace() options["juju_directory"] = self.makeDir() options["zookeeper_servers"] = get_test_zookeeper_address() options["machine_id"] = "0" agent.configure(options) agent.set_watch_enabled(False) agent.client = self.client yield agent.start() # collect everything state = yield status.collect(None, self.provider, self.client, None) services = state["services"] self.assertIn("wordpress", services) self.assertIn("varnish", services) self.assertIn("mysql", services) # and verify the specifics of a single service self.assertTrue("mysql" in services) units = list(services["mysql"]["units"]) self.assertEqual(len(units), 2) self.assertEqual(state["machines"][0], {"instance-id": 0, "instance-state": "unknown", "dns-name": "steamcloud-1.com", "state": "running"}) self.assertEqual(services["mysql"]["relations"], {"db": "wordpress"}) self.assertEqual(services["wordpress"]["relations"], {"cache": "memcache", "db": "mysql", "proxy": "varnish"}) self.assertEqual( services["varnish"], {"units": {"varnish/1": { "machine": 4, "state": "down", "open-ports": ["80/tcp"], "public-address": "varnish-1.example.com", "relations": {"proxy": {"state": "up"}}}, "varnish/0": { "machine": 3, "state": "started", "public-address": "varnish-0.example.com", "open-ports": ["80/tcp"], "relations": {"proxy": {"state": "up"}}}}, "exposed": True, "charm": "local:series/varnish-1", "relations": {"proxy": "wordpress"}}) self.assertEqual( services["wordpress"], {"charm": "local:series/wordpress-3", "relations": { "cache": "memcache", "db": "mysql", "proxy": "varnish"}, "units": { "wordpress/0": { "machine": 0, "public-address": "wordpress-0.example.com", "relations": { "cache": {"state": "up"}, "db": {"state": "up"}, "proxy": {"state": "up"}}, "state": "started"}}}) self.assertEqual( services["memcache"], {"charm": "local:series/dummy-1", "relations": {"cache": "wordpress"}, "units": { "memcache/0": { "machine": 5, "public-address": "memcache-0.example.com", "relations": { "cache": {"state": "up"}}, "state": "pending"}, "memcache/1": { "machine": 6, "public-address": "memcache-1.example.com", "relations": { "cache": {"state": "down"}}, "state": "installed"}}})
def test_collect_filtering(self): yield self.build_topology() # collect by service name state = yield status.collect(["wordpress"], self.provider, self.client, None) # Validate that only the expected service is present # in the state self.assertEqual(state["machines"].keys(), [0]) self.assertEqual(state["services"].keys(), ["wordpress"]) # collect by unit name state = yield status.collect(["*/0"], self.provider, self.client, None) self.assertEqual(set(state["machines"].keys()), set([0, 1, 3, 5])) self.assertEqual(set(state["services"].keys()), set(["memcache", "varnish", "mysql", "wordpress"])) # collect by unit name state = yield status.collect(["*/1"], self.provider, self.client, None) self.assertEqual(set(state["machines"].keys()), set([2, 4, 6])) # verify that only the proper units and services are present self.assertEqual( state["services"], { "memcache": { "charm": "local:series/dummy-1", "relations": { "cache": "wordpress" }, "units": { "memcache/1": { "machine": 6, "state": "installed", "public-address": "memcache-1.example.com", "relations": { "cache": { "state": "down" } } } } }, "mysql": { "exposed": True, "charm": "local:series/mysql-1", "relations": { "db": "wordpress" }, "units": { "mysql/1": { "machine": 2, "public-address": "mysql-1.example.com", "open-ports": [], "state": "stopped", "relations": { "db": { "state": "departed" } } } } }, "varnish": { "exposed": True, "charm": "local:series/varnish-1", "relations": { "proxy": "wordpress" }, "units": { "varnish/1": { "machine": 4, "public-address": "varnish-1.example.com", "open-ports": ["80/tcp"], "state": "started", "relations": { "proxy": { "state": "up" } } } } } }) # filter a missing service state = yield status.collect(["cluehammer"], self.provider, self.client, None) self.assertEqual(set(state["machines"].keys()), set([])) self.assertEqual(set(state["services"].keys()), set([])) # filter a missing unit state = yield status.collect(["*/7"], self.provider, self.client, None) self.assertEqual(set(state["machines"].keys()), set([])) self.assertEqual(set(state["services"].keys()), set([]))
def test_collect(self): yield self.build_topology(skip_unit_agents=("varnish/1", )) agent = MachineAgent() options = TwistedOptionNamespace() options["juju_directory"] = self.makeDir() options["zookeeper_servers"] = get_test_zookeeper_address() options["machine_id"] = "0" agent.configure(options) agent.set_watch_enabled(False) agent.client = self.client yield agent.start() # collect everything state = yield status.collect(None, self.provider, self.client, None) services = state["services"] self.assertIn("wordpress", services) self.assertIn("varnish", services) self.assertIn("mysql", services) # and verify the specifics of a single service self.assertTrue("mysql" in services) units = list(services["mysql"]["units"]) self.assertEqual(len(units), 2) self.assertEqual( state["machines"][0], { "instance-id": 0, "instance-state": "unknown", "dns-name": "steamcloud-1.com", "state": "running" }) self.assertEqual(services["mysql"]["relations"], {"db": "wordpress"}) self.assertEqual(services["wordpress"]["relations"], { "cache": "memcache", "db": "mysql", "proxy": "varnish" }) self.assertEqual( services["varnish"], { "units": { "varnish/1": { "machine": 4, "state": "down", "open-ports": ["80/tcp"], "public-address": "varnish-1.example.com", "relations": { "proxy": { "state": "up" } } }, "varnish/0": { "machine": 3, "state": "started", "public-address": "varnish-0.example.com", "open-ports": ["80/tcp"], "relations": { "proxy": { "state": "up" } } } }, "exposed": True, "charm": "local:series/varnish-1", "relations": { "proxy": "wordpress" } }) self.assertEqual( services["wordpress"], { "charm": "local:series/wordpress-3", "relations": { "cache": "memcache", "db": "mysql", "proxy": "varnish" }, "units": { "wordpress/0": { "machine": 0, "public-address": "wordpress-0.example.com", "relations": { "cache": { "state": "up" }, "db": { "state": "up" }, "proxy": { "state": "up" } }, "state": "started" } } }) self.assertEqual( services["memcache"], { "charm": "local:series/dummy-1", "relations": { "cache": "wordpress" }, "units": { "memcache/0": { "machine": 5, "public-address": "memcache-0.example.com", "relations": { "cache": { "state": "up" } }, "state": "pending" }, "memcache/1": { "machine": 6, "public-address": "memcache-1.example.com", "relations": { "cache": { "state": "down" } }, "state": "installed" } } })
def test_peer_relation(self): """Verify status works with peer relations. """ m1 = yield self.machine_state_manager.add_machine_state() m2 = yield self.machine_state_manager.add_machine_state() yield self.provider.start_machine({ "machine-id": 0, "dns-name": "steamcloud-1.com" }) yield self.provider.start_machine({ "machine-id": 1, "dns-name": "steamcloud-2.com" }) yield m1.set_instance_id(0) yield m2.set_instance_id(1) riak = yield self.add_service_from_charm("riak") riak_u1 = yield self.add_unit(riak, m1) riak_u2 = yield self.add_unit(riak, m2, with_agent=lambda _: False) yield self.set_unit_state(riak_u1, "started") yield self.set_unit_state(riak_u2, "started") _, (peer_rel, ) = yield self.relation_state_manager.add_relation_state( RelationEndpoint("riak", "peer", "ring", "peer")) yield ZookeeperWorkflowState( self.client, (yield peer_rel.add_unit_state(riak_u1))).set_state("up") yield peer_rel.add_unit_state(riak_u2) state = yield status.collect(["riak"], self.provider, self.client, None) self.assertEqual( state["services"]["riak"], { "charm": "local:series/riak-7", "relations": { "ring": "riak" }, "units": { "riak/0": { "machine": 0, "public-address": "riak-0.example.com", "relations": { "ring": { "state": "up" } }, "state": "started" }, "riak/1": { "machine": 1, "public-address": "riak-1.example.com", "relations": { "ring": { "state": None } }, "state": "down" } } })