def _send_package_data(self, host, data): # UpdateScan is a weird class, we have to instantiate and assign a host # to run the function we're testing. self.update_scan = UpdateScan() self.update_scan.host = host self.update_scan.started_at = IMLDateTime.utcnow() self.update_scan.update_packages({'agent': data})
def _acquire_token(self, url, username, password, credit_count, duration=None, preferred_profile=None): """ Localised use of the REST API to acquire a server registration token. """ session = self._get_authenticated_session(url, username, password) # Acquire a server profile response = session.get("%sapi/server_profile/" % url) if not preferred_profile: profile = response.json()['objects'][0] else: try: profile = [ p for p in response.json()['objects'] if p['name'] == preferred_profile ][0] except IndexError: raise RuntimeError("No such profile: %s" % preferred_profile) args = {'credits': credit_count, 'profile': profile['resource_uri']} if duration is not None: args['expiry'] = (IMLDateTime.utcnow() + duration).isoformat() response = session.post("%sapi/registration_token/" % url, data=json.dumps(args)) assert response.ok return response.json()['secret']
def test_update_properties(self): update_scan = UpdateScan() update_scan.host = synthetic_host('test1') update_scan.started_at = IMLDateTime.utcnow() self.assertEqual(update_scan.host.properties, '{}') update_scan.update_properties(None) update_scan.update_properties({'key': 'value'})
def setUp(self): if not ManagedHost.objects.filter(fqdn = self.CLIENT_NAME).count(): if not ServerProfile.objects.filter(name = 'TestAgentRpcProfile').count(): server_profile = ServerProfile.objects.create( name = 'TestAgentRpcProfile', ui_name = 'Profile created to TestAgentRpc can work', managed = True, worker = False, rsyslog = True, ntp = True, corosync = True, corosync2 = False) else: server_profile = ServerProfile.objects.get(name = 'TestAgentRpcProfile') self.host = ManagedHost.objects.create( fqdn = self.CLIENT_NAME, nodename = self.CLIENT_NAME, address = self.CLIENT_NAME, state = 'lnet_down', state_modified_at = IMLDateTime.utcnow(), server_profile = server_profile ) LNetConfiguration.objects.create(host = self.host, state = 'lnet_down') ClientCertificate.objects.create(host = self.host, serial = self.CLIENT_CERT_SERIAL) else: self.host = ManagedHost.objects.get(fqdn = self.CLIENT_NAME) super(TestAgentRpc, self).setUp()
def test_HYD648(self): """Test that datetimes in the API have a timezone""" synthetic_host('myserver') response = self.api_client.get("/api/host/") self.assertHttpOK(response) host = self.deserialize(response)['objects'][0] t = IMLDateTime.parse(host['state_modified_at']) self.assertNotEqual(t.tzinfo, None)
def fake_log_message(message): return LogMessage.objects.create( datetime=IMLDateTime.utcnow(), message=message, severity=0, facility=0, tag="", message_class=LogMessage.get_message_class(message))
def test_fetch_not_dismissed_alerts_since_last_sample(self): data = { "begin__gte": str(self.sample_date), "dismissed": 'false', "severity__in": ['WARNING', 'ERROR'] } response = self.api_client.get("/api/alert/", data=data) self.assertHttpOK(response) objects = self.deserialize(response)['objects'] self.assertEqual(len(objects), 2, self.dump_objects(objects)) for ev in objects: self.assertEqual(ev['dismissed'], False) self.assertTrue(ev['severity'] in ['WARNING', 'ERROR']) self.assertTrue(IMLDateTime.parse(ev['begin']) >= self.sample_date)
def test_creation(self): """ During a POST, only expiry should be settable """ # Empty is OK response = self.api_client.post( self.RESOURCE_PATH, data={'profile': self.profile['resource_uri']}) self.assertHttpCreated(response) expiry_value = IMLDateTime.utcnow() expiry_value += datetime.timedelta(seconds=120) expiry_value = expiry_value.replace(microsecond=0) creation_allowed_values = { 'expiry': expiry_value.isoformat(), 'credits': 129 } for attr, test_val in creation_allowed_values.items(): response = self.api_client.post(self.RESOURCE_PATH, data={ 'profile': self.profile['resource_uri'], attr: test_val }) self.assertHttpCreated(response) created_obj = self.deserialize(response) self.assertEqual(created_obj[attr], test_val) # Anything else is not OK creation_forbidden_values = { 'secret': "X" * SECRET_LENGTH * 2, 'cancelled': True, 'id': 0 } for attribute, test_val in creation_forbidden_values.items(): response = self.api_client.post(self.RESOURCE_PATH, data={ 'profile': self.profile['resource_uri'], attribute: test_val }) self.assertHttpBadRequest(response)
def _wait_for_server_boot_time(self, fqdn, old_boot_time=None): running_time = 0 while running_time < TEST_TIMEOUT: hosts = self.get_list("/api/host/") for host in hosts: if host['fqdn'] == fqdn: if host['boot_time'] is not None: boot_time = IMLDateTime.parse(host['boot_time']) if old_boot_time: if boot_time > old_boot_time: return boot_time else: return boot_time running_time += 1 time.sleep(1) self.assertLess(running_time, TEST_TIMEOUT, "Timed out waiting for host boot_time to be set.")
def test_readonly_attributes(self): """Test that attributes which should be readonly reject PATCHes""" response = self.api_client.post( self.RESOURCE_PATH, data={'profile': self.profile['resource_uri']}) self.assertHttpCreated(response) original_object = self.deserialize(response) token_uri = original_object['resource_uri'] readonly_test_values = { 'secret': "X" * SECRET_LENGTH * 2, 'expiry': IMLDateTime.utcnow(), 'credits': 666 } for attribute, test_val in readonly_test_values.items(): response = self.api_client.patch(token_uri, data={attribute: test_val}) self.assertHttpBadRequest(response) # Check it hasn't changed self.assertDictEqual( self.deserialize(self.api_client.get(token_uri)), original_object)
def _call(cls, host, cmd, args): cls.calls.append((cmd, args)) cls.host_calls[host].append((cmd, args)) if not cls.succeed: cls._fail(host.fqdn) if (cmd, args) in cls.fail_commands: cls._fail(host.fqdn) mock_server = cls.mock_servers[host.address] log.info("invoke_agent %s %s %s" % (host, cmd, args)) # This isn't really accurate because lnet is scanned asynchonously, but it is as close as we can get today # Fixme: Also I know think this is writing to the wrong thing and should be changing the mock_server entries. # to lnet_up, I guess the mock_server needs an lnet state really, rather than relying on nids present. if cmd == "load_lnet": synthetic_lnet_configuration(host, mock_server['nids']) return elif cmd == "device_plugin": # Only returns nid info today. return create_synthetic_device_info(host, mock_server, args['plugin']) elif cmd == 'format_target': inode_size = None if 'mkfsoptions' in args: inode_arg = re.search("-I (\d+)", args['mkfsoptions']) if inode_arg: inode_size = int(inode_arg.group(1).__str__()) if inode_size is None: # A 'foo' value inode_size = 777 return {'uuid': uuid.uuid1().__str__(), 'inode_count': 666, 'inode_size': inode_size, 'filesystem_type': 'ext4'} elif cmd == 'stop_target': ha_label = args['ha_label'] target = ManagedTarget.objects.get(ha_label = ha_label) return agent_result_ok elif cmd == 'start_target': ha_label = args['ha_label'] target = ManagedTarget.objects.get(ha_label = ha_label) return agent_result(target.primary_host.nodename) elif cmd == 'register_target': # Assume mount paths are "/mnt/testfs-OST0001" style mount_point = args['mount_point'] label = re.search("/mnt/([^\s]+)", mount_point).group(1) return {'label': label} elif cmd == 'detect_scan': return mock_server['detect-scan'] elif cmd == 'install_packages': return agent_result([]) elif cmd == 'register_server': api_client = TestApiClient() old_is_authenticated = CsrfAuthentication.is_authenticated try: CsrfAuthentication.is_authenticated = mock.Mock(return_value = True) api_client.client.login(username = '******', password = '******') fqdn = cls.mock_servers[host]['fqdn'] response = api_client.post(args['url'] + "register/%s/" % args['secret'], data = { 'address': host, 'fqdn': fqdn, 'nodename': cls.mock_servers[host]['nodename'], 'capabilities': ['manage_targets'], 'version': cls.version, 'csr': helper.generate_csr(fqdn) }) assert response.status_code == 201 registration_data = Serializer().deserialize(response.content, format = response['Content-Type']) print "MockAgent.invoke returning %s" % registration_data return registration_data finally: CsrfAuthentication.is_authenticated = old_is_authenticated elif cmd == 'kernel_status': return { 'running': 'fake_kernel-0.1', 'required': 'fake_kernel-0.1', 'available': ['fake_kernel-0.1'] } elif cmd == 'reboot_server': now = IMLDateTime.utcnow() log.info("rebooting %s; updating boot_time to %s" % (host, now)) job_scheduler_notify.notify(host, now, {'boot_time': now}) elif 'socket.gethostbyname(socket.gethostname())' in cmd: if not mock_server['tests']['hostname_valid']: return '127.0.0.1' else: return mock_server['address'] elif 'print os.uname()[1]' in cmd: return '%s\n%s' % (mock_server['nodename'], mock_server['fqdn']) elif 'socket.getfqdn()' in cmd: return mock_server['fqdn'] elif 'ping' in cmd: result = ((0 if mock_server['tests']['reverse_resolve'] else 2) + (0 if mock_server['tests']['reverse_ping'] else 1)) return result elif 'python-fedora-django' in cmd: return 0 if mock_server['tests']['yum_valid_repos'] else 1 elif 'ElectricFence' in cmd: return 0 if mock_server['tests']['yum_can_update'] else 1 elif 'curl -k https' in cmd: return json.dumps({'host_id': host.id, 'command_id': 0}) elif cmd in ['configure_pacemaker', 'unconfigure_pacemaker', 'configure_rsyslog', 'unconfigure_rsyslog', 'configure_target_store', 'unconfigure_target_store', 'deregister_server', 'restart_agent', 'shutdown_server', 'host_corosync_config', 'check_block_device', 'set_conf_param', 'purge_configuration']: return None elif cmd in ['configure_target_ha', 'unconfigure_target_ha', 'start_lnet', 'stop_lnet', 'unload_lnet', 'unconfigure_lnet', 'configure_corosync', 'unconfigure_corosync', 'start_corosync', 'stop_corosync', 'start_pacemaker', 'stop_pacemaker', 'configure_ntp', 'unconfigure_ntp', 'import_target', 'export_target', 'import_target', 'export_target' 'set_profile', 'update_profile', 'failover_target', 'failback_target', 'configure_network', 'open_firewall', 'close_firewall']: return agent_result_ok elif cmd == 'get_corosync_autoconfig': return agent_result({'interfaces': {'eth0': {'dedicated': False, 'ipaddr': '192.168.0.1', 'prefix': 24}, 'eth1': {'dedicated': True, 'ipaddr': '10.10.0.01', 'prefix': 24}}, 'mcast_port': '666'}) else: assert False, "The %s command is not in the known list for MockAgentRpc. Please add it then when people modify it a simple text search will let them know to change it here as well." % cmd