def test_onejob(self): # Our self.host is initially lnet_up self.assertEqual( LNetConfiguration.objects.get(pk=self.lnet_configuration.pk).state, "lnet_up") # This tests a state transition which is done by a single job command_id = JobSchedulerClient.command_run_jobs( [{ "class_name": "UpdateDevicesJob", "args": { "hosts": [api.get_resource_uri(self.host)] } }], "Test single job action", ) self.drain_progress() self.assertEqual(Command.objects.get(pk=command_id).complete, True) self.assertEqual(Command.objects.get(pk=command_id).jobs.count(), 1) # Test that if I try to run the same again I get None command = Command.set_state([(freshen(self.lnet_configuration), "lnet_up")]) self.assertEqual(command, None)
def dehydrate_attributes(self, bundle): # a list of dicts, one for each attribute. Excludes hidden attributes. result = {} resource = bundle.obj.to_resource() attr_props = resource.get_all_attribute_properties() for name, props in attr_props: # Exclude password hashes if isinstance(props, attributes.Password): continue val = getattr(resource, name) if isinstance(val, BaseStorageResource): if val._handle: from chroma_api.urls import api raw = api.get_resource_uri( StorageResourceRecord.objects.get(pk=val._handle)) else: raw = None else: raw = val result[name] = { 'raw': raw, 'markup': props.to_markup(val), 'label': props.get_label(name), 'class': props.__class__.__name__ } return result
def dehydrate_locked_item_uri(self, bundle): from chroma_api.urls import api locked_item = bundle.obj.locked_item if hasattr(locked_item, 'content_type'): locked_item = locked_item.downcast() return api.get_resource_uri(locked_item)
def test_host_lists(self): """Test that commands which take a list of hosts as an argument are get the host URIs converted to host IDs (for use with HostListMixin)""" from chroma_api.urls import api hosts = [] for i in range(0, 2): address = "myserver_%d" % i host = ManagedHost.objects.create(address=address, fqdn=address, nodename=address) hosts.append(host) with mock.patch( "chroma_core.services.job_scheduler.job_scheduler_client.JobSchedulerClient.command_run_jobs", mock.Mock(return_value=Command.objects.create().id), ): response = self.api_client.post( "/api/command/", data={ "message": "Test command", "jobs": [ {"class_name": "UpdateNidsJob", "args": {"hosts": [api.get_resource_uri(h) for h in hosts]}} ], }, ) self.assertEqual(response.status_code, 201) host_ids = "[%s]" % ", ".join([str(h.id) for h in hosts]) JobSchedulerClient.command_run_jobs.assert_called_once_with( [{"class_name": "UpdateNidsJob", "args": {"host_ids": host_ids}}], "Test command" )
def dehydrate_network_interfaces(self, bundle): from chroma_api.urls import api return [ api.get_resource_uri(network_interface) for network_interface in bundle.obj.network_interfaces ]
def substitute(obj, match, group=1): resource_uri = api.get_resource_uri(obj) substitutions.append({ "start": match.start(group), "end": match.end(group), "label": obj.get_label(), "resource_uri": resource_uri, })
def substitute(obj, match, group=1): resource_uri = api.get_resource_uri(obj) substitutions.append({ 'start': match.start(group), 'end': match.end(group), 'label': obj.get_label(), 'resource_uri': resource_uri })
def setUp(self): super(TestCreateHostAPI, self).setUp() # Body for a POST to either host or test_host self.input_data = { "address": "myaddress", "auth_type": "existing_keys_choice", "server_profile": api.get_resource_uri(ServerProfile.objects.get()), "root_password": "******", "private_key": sample_private_key, "private_key_passphrase": "secret_key_pw", }
def test_mgs_nid_change(self): mgs = synthetic_host("mgs") mds = synthetic_host("mds") oss = synthetic_host("oss") from chroma_core.models import ( ManagedMgs, ManagedMdt, ManagedOst, ManagedFilesystem, ManagedTarget, ManagedTargetMount, ) self.mgt, mgt_tms = ManagedMgs.create_for_volume( synthetic_volume_full(mgs).id, name="MGS") self.fs = ManagedFilesystem.objects.create(mgs=self.mgt, name="testfs") self.mdt, mdt_tms = ManagedMdt.create_for_volume( synthetic_volume_full(mds).id, filesystem=self.fs) self.ost, ost_tms = ManagedOst.create_for_volume( synthetic_volume_full(oss).id, filesystem=self.fs) ObjectCache.add(ManagedFilesystem, self.fs) for target in [self.mgt, self.ost, self.mdt]: ObjectCache.add(ManagedTarget, target.managedtarget_ptr) for tm in chain(mgt_tms, mdt_tms, ost_tms): ObjectCache.add(ManagedTargetMount, tm) self.fs = self.set_and_assert_state(self.fs, "available") self.mock_servers["mgs"]["nids"] = [Nid.Nid("192.168.0.99", "tcp", 0)] self.assertNidsCorrect(mgs) JobSchedulerClient.command_run_jobs([{ "class_name": "UpdateNidsJob", "args": { "hosts": [api.get_resource_uri(mgs)] } }], "Test update nids") self.drain_progress() # The -3 looks past the start/stop that happens after writeconf self.assertEqual(MockAgentRpc.host_calls[mgs][-3][0], "writeconf_target") self.assertEqual(MockAgentRpc.host_calls[mds][-3][0], "writeconf_target") self.assertEqual(MockAgentRpc.host_calls[oss][-3][0], "writeconf_target") self.assertState(self.fs, "stopped")
def assertNidsCorrect(self, host): JobSchedulerClient.command_run_jobs([{ "class_name": "UpdateDevicesJob", "args": { "hosts": [api.get_resource_uri(host)] } }], "Test update of nids") self.drain_progress() mock_nids = set([ str(Nid.nid_tuple_to_string(Nid.Nid(n[0], n[1], n[2]))) for n in self.mock_servers[host.address]["nids"] ]) recorded_nids = set([ str(n.nid_string) for n in Nid.objects.filter(lnet_configuration__host=host) ]) self.assertSetEqual(mock_nids, recorded_nids)
def dehydrate_affected(self, bundle): from chroma_api.urls import api alert = bundle.obj affected_objects = [] def affect_target(target): affected_objects.append(target) if target.filesystem_member: affected_objects.append(target.filesystem) elif target.target_type == "mgs": for fs in target.managedfilesystem_set.all(): affected_objects.append(fs) affected_objects.extend(alert.affected_objects) alert.affected_targets(affect_target) affected_objects.append(alert.alert_item) return [api.get_resource_uri(ao) for ao in set(affected_objects)]
def dehydrate_alert_item(self, bundle): return api.get_resource_uri(bundle.obj.alert_item)
def dehydrate_alert_item(self, bundle): from chroma_api.urls import api return api.get_resource_uri(bundle.obj.alert_item)
def dehydrate_network_interfaces(self, bundle): return [ api.get_resource_uri(network_interface) for network_interface in bundle.obj.network_interfaces ]
def dehydrate_failover_servers(self, bundle): from chroma_api.urls import api return [ api.get_resource_uri(host) for host in bundle.obj.failover_hosts ]