class CommandsApiTest(unittest.TestCase): @classmethod def setUpClass(self): self.client = TestClient(CommandsApi) @classmethod def tearDownClass(self): self.client.disconnect() def setUp(self): self.assertTrue(self.client.disco_hosts) def test_execute_command_success(self): """ Execute a command. """ self.execute('ls -al', assertion=True) def test_execute_command_failure(self): """ Execute a command fails. """ # Execute a non-existing command. self.execute('bioehfwefw -dkqjid', assertion=False) def execute(self, command, assertion=None): """ """ request = self.client.api.execute(command) self.client.send(request) responses = self.client.get_responses() if assertion is True: for resp in responses: self.assertTrue(resp['collection'] == 'executables') self.assertTrue(resp['resource_id'] == command) self.assertTrue(resp['status']) self.assertTrue(resp['status']['cmd'] == command) self.assertTrue(resp['status']['returncode'] == 0) self.assertTrue(resp['status']['stdout']) self.assertFalse(resp['status']['stderr']) elif assertion is False: for resp in responses: self.assertTrue(resp['collection'] == 'executables') self.assertTrue(resp['resource_id'] == command) self.assertTrue(resp['status']) self.assertTrue(resp['status']['cmd'] == command) self.assertTrue(resp['status']['returncode'] != 0) self.assertTrue(resp['status']['stderr']) else: return responses
class HostsApiTest(unittest.TestCase): @classmethod def setUpClass(self): self.client = TestClient(HostsApi) @classmethod def tearDownClass(self): self.client.disconnect() def test_status(self): request = self.client.api.infos() self.client.send(request) responses = self.client.get_responses() for resp in responses: self.assertTrue('error' not in resp) def test_status_attr(self): for attr in self.client.api._attrs: request = self.client.api.infos([attr]) self.client.send(request) responses = self.client.get_responses() for resp in responses: self.assertTrue(attr in resp['status']) self.assertTrue('error' not in resp) def test_status_attrs(self): attrs = self.client.api._attrs request = self.client.api.infos(attrs) self.client.send(request) responses = self.client.get_responses() for resp in responses: # Ensure that resp['attributes'] contains all # self.client.api._attrs attributes. difference = set(resp['status']) - set(attrs) self.assertFalse(len(difference)) self.assertTrue('error' not in resp)
class FilesApiTest(unittest.TestCase): content = "All your files are belong to us !" @classmethod def setUpClass(self): self.client = TestClient(FilesApi) @classmethod def tearDownClass(self): self.client.disconnect() def test_infos_success(self): """ Retrieve the status of a file. """ filename = "/tmp/test" self.create(filename, self.content, assertion=None) self.read(filename, assertion=True) self.delete(filename, assertion=None) def test_infos_failure(self): """ Read on non-existing file produces an error. """ filename = "/tmp/test" self.delete(filename, assertion=None) self.read(filename, assertion=False) def test_create_success(self): """ Create a file. """ filename = "/tmp/test" self.create(filename, self.content, assertion=True) self.delete(filename, assertion=None) def test_update_meta_success(self): """ Update the metadata of a file. """ filename = "/tmp/test" mode = '755' self.create(filename, self.content, mode=mode, assertion=True) self.delete(filename, assertion=None) def test_update_meta_failure(self): """ Set mode 999 on a file must fail. """ filename = "/tmp/test" mode = '999' self.create(filename, self.content, mode=mode, assertion=True) self.delete(filename, assertion=None) def test_delete_success(self): """ Delete a file. """ filename = "/tmp/test" self.create(filename, self.content, assertion=None) self.delete(filename, assertion=True) def test_delete_failure(self): """ Delete an non-existing file must fail. """ filename = "/tmp/test" self.create(filename, self.content, assertion=None) self.delete(filename, assertion=None) self.delete(filename, assertion=False) def read(self, path, assertion=None): request = self.client.api.infos(path, md5=True, content=True) self.client.send(request) responses = self.client.get_responses() content_md5 = self.md5_str(self.content) for resp in responses: if assertion: self.assertTrue(resp['status']['present'] is True) self.assertTrue('md5' in resp['status']) remote_md5 = resp['status']['md5'] self.assertTrue(remote_md5 == content_md5) if assertion is False: self.assertFalse('error' not in resp) def create(self, path, content, owner=None, group=None, mode=None, assertion=None): request = self.client.api.create(path, content) self.client.send(request) responses = self.client.get_responses() for resp in responses: if assertion: self.assertTrue('error' not in resp) self.assertTrue(resp['status']['present'] is True) def update_meta(self, path, owner=None, group=None, mode=None, assertion=None): request = self.client.api.update_meta(path, mode=mode) self.client.send(request) responses = self.client.get_responses() for resp in responses: if assertion: self.assertTrue('error' not in resp) self.assertTrue(resp['status']['mode'] == mode) if assertion is False: self.assertFalse('error' not in resp) def delete(self, path, assertion=None): request = self.client.api.delete(path) self.client.send(request) responses = self.client.get_responses() for resp in responses: if assertion: self.assertTrue('error' not in resp) self.assertTrue(resp['status']['present'] is False) if assertion is False: self.assertFalse('error' not in resp) def md5_str(self, content): m = hashlib.md5() m.update(content) return m.hexdigest()
class PackagesApiTest(unittest.TestCase): @classmethod def setUpClass(self): self.client = TestClient(PackagesApi) @classmethod def tearDownClass(self): self.client.disconnect() def setUp(self): self.assertTrue(self.client.disco_hosts) def test_install_success(self): """ Install a package. """ self.install('htop', assertion=True) self.uninstall('htop', assertion=None) def test_install_failure(self): """ Install a package fails. """ package = "%s" % uuid.uuid4() self.install(package, assertion=False) def test_remove_success(self): """ Remove a package. """ self.install('htop', assertion=None) self.uninstall('htop', assertion=True) def test_remove_failure(self): """ Remove a package fails. """ package = "%s" % uuid.uuid4() self.uninstall(package, assertion=False) def test_update_success(self): """ Update a package. """ self.install('htop', assertion=None) self.update('htop', assertion=True) def test_update_failure(self): """ Update a package fails. """ package = "%s" % uuid.uuid4() self.update(package, assertion=False) #def test_update_whole_success(self): # """ Update the whole system package. # """ # self.update(assertion=True) def install(self, package, assertion=None): request = self.client.api.install(package) self.client.send(request) responses = self.client.get_responses() if assertion is True: for resp in responses: self.assertTrue(resp['status']) self.assertTrue(resp['status']['installed'] == True) self.assertTrue(resp['resource_id'] == package) self.assertTrue(resp['collection'] == 'packages') elif assertion is False: for resp in responses: self.assertTrue(resp['status']) self.assertTrue(resp['resource_id'] == package) self.assertTrue(resp['collection'] == 'packages') self.assertTrue('error' in resp) else: return responses def uninstall(self, package, assertion=None): request = self.client.api.remove(package) self.client.send(request) responses = self.client.get_responses() if assertion is True: for resp in responses: self.assertTrue(resp['status']) self.assertTrue(resp['status']['installed'] == False) self.assertTrue(resp['resource_id'] == package) self.assertTrue(resp['collection'] == 'packages') elif assertion is False: for resp in responses: self.assertTrue(resp['status']) self.assertTrue(resp['resource_id'] == package) self.assertTrue(resp['collection'] == 'packages') else: return responses def update(self, package=None, assertion=None): request = self.client.api.update(package) self.client.send(request) responses = self.client.get_responses() if assertion is True: for resp in responses: self.assertTrue(resp['collection'] == 'packages') if package: self.assertTrue(resp['status']) self.assertTrue(resp['status']['installed'] == True) self.assertTrue(resp['resource_id'] == package) elif assertion is False: for resp in responses: self.assertTrue(resp['status']) self.assertTrue(resp['resource_id'] == package) self.assertTrue(resp['collection'] == 'packages') self.assertTrue('error' in resp) else: return responses
class UserssApiTest(unittest.TestCase): @classmethod def setUpClass(self): self.client = TestClient(UsersApi) @classmethod def tearDownClass(self): self.client.disconnect() def setUp(self): self.assertTrue(self.client.disco_hosts) def test_create_success(self): """ Create a user. """ self.remove('unittest', assertion=None) self.create('unittest', assertion=True) self.remove('unittest', assertion=None) def test_create_failure(self): """ Create a user fails. """ self.create('root', assertion=False) def test_create_password_success(self): """ Create a user with a password. """ self.remove('unittest', assertion=None) self.create('unittest', 'secret', assertion=True) self.remove('unittest', assertion=None) def test_remove_success(self): """ Remove a user. """ self.create('unittest', assertion=None) self.remove('unittest', assertion=True) def test_remove_failure(self): """ Remove a user fails. """ username = '******' % uuid.uuid4() self.remove(username, assertion=False) def test_add_group_success(self): """ Add a group (as list) to the user. """ self.remove('unittest', assertion=None) self.create('unittest', assertion=None) responses = self.update('unittest', add_to_groups=['root'], assertion=True) for resp in responses: self.assertTrue('root' in resp['status']['groups']) self.remove('unittest', assertion=None) def test_add_str_group_success(self): """ Add a group (as string) to the user. """ self.remove('unittest', assertion=None) self.create('unittest', assertion=None) responses = self.update('unittest', add_to_groups='root', assertion=True) for resp in responses: self.assertTrue('root' in resp['status']['groups']) self.remove('unittest', assertion=None) def test_add_group_failure(self): """ Add a group to the user fails. """ group_name = "%s" % uuid.uuid4() self.remove('unittest', assertion=None) self.create('unittest', assertion=None) self.update('unittest', add_to_groups=group_name, assertion=False) self.remove('unittest', assertion=None) def create(self, username, password=None, assertion=None): request = self.client.api.create(username, password) self.client.send(request) responses = self.client.get_responses() if assertion is True: for resp in responses: self.assertTrue(resp['collection'] == 'users') self.assertTrue(resp['resource_id'] == username) self.assertTrue(resp['status']) self.assertTrue(resp['status']['shell']) self.assertTrue(resp['status']['uid']) self.assertTrue(resp['status']['gid']) self.assertTrue(resp['status']['groups']) self.assertTrue(resp['status']['dir']) self.assertTrue(resp['status']['name'] == username) self.assertFalse('error' in resp) elif assertion is False: for resp in responses: self.assertTrue(resp['collection'] == 'users') self.assertTrue(resp['resource_id'] == username) self.assertTrue('error' in resp) else: return responses def remove(self, username, assertion=None): request = self.client.api.remove(username) self.client.send(request) responses = self.client.get_responses() if assertion is True: for resp in responses: self.assertTrue(resp['collection'] == 'users') self.assertTrue(resp['resource_id'] == username) self.assertFalse('error' in resp) elif assertion is False: for resp in responses: self.assertTrue(resp['collection'] == 'users') self.assertTrue(resp['resource_id'] == username) self.assertTrue('error' in resp) else: return responses def update(self, name, password=None, login_group=None, add_to_groups=None, remove_from_groups=None, set_groups=None, monitor=None, assertion=None): request = self.client.api.update(name, password, login_group, add_to_groups, remove_from_groups, set_groups, monitor) self.client.send(request) responses = self.client.get_responses() if assertion is True: for resp in responses: self.assertTrue(resp['collection'] == 'users') self.assertTrue(resp['resource_id'] == name) self.assertTrue(resp['status']) self.assertTrue(resp['status']['groups']) self.assertFalse('error' in resp) elif assertion is False: for resp in responses: self.assertTrue(resp['collection'] == 'users') self.assertTrue(resp['resource_id'] == name) self.assertTrue('error' in resp) return responses
class ServicesApiTest(unittest.TestCase): @classmethod def setUpClass(self): self.client = TestClient(ServicesApi) @classmethod def tearDownClass(self): self.client.disconnect() def test_status_success(self): """Check if we can retrieve the status of an existing service""" name = "rsyslog" self.stop(name, assertion=None) self.status(name, assertion=True) def test_status_failure(self): """Check if retrieving status of non existing service returns an error.""" name = "qenqzeofnpndj" self.stop(name, assertion=None) self.status(name, assertion=False) def test_start_success(self): """Try starting an existing service""" name = "rsyslog" self.stop(name, assertion=None) self.start(name, assertion=True) def test_start_failure(self): """Try starting an unknown service""" name = "qenqzeofnpndj" self.start(name, assertion=False) def test_stop_success(self): """Try stopping an unknown service""" name = "rsyslog" self.start(name, assertion=None) self.stop(name, assertion=True) def test_stop_failure(self): """Try stopping an unknown service""" name = "qenqzeofnpndj" self.stop(name, assertion=False) def status(self, name, assertion=None): request = self.client.api.status(name) self.client.send(request) responses = self.client.get_responses() for resp in responses: if assertion: self.assertTrue('error' not in resp) if assertion is False: self.assertTrue(resp['status']['running'] is False) self.assertTrue(resp['status']['enabled'] is False) def start(self, name, assertion=None): request = self.client.api.start(name) self.client.send(request) responses = self.client.get_responses() for resp in responses: if assertion: self.assertTrue('error' not in resp) self.assertTrue(resp['status']['running'] is True) if assertion is False: self.assertFalse('error' not in resp) #self.assertFalse(resp['status']['running'] is True) def stop(self, name, assertion=None): request = self.client.api.stop(name) self.client.send(request) responses = self.client.get_responses() for resp in responses: if assertion: self.assertTrue('error' not in resp) self.assertTrue(resp['status']['running'] is False) if assertion is False: #self.assertFalse('error' not in resp) self.assertTrue(resp['status']['running'] is False)
class GroupsApiTest(unittest.TestCase): @classmethod def setUpClass(self): self.client = TestClient(GroupsApi) @classmethod def tearDownClass(self): self.client.disconnect() def setUp(self): self.assertTrue(self.client.disco_hosts) def test_create_success(self): """ Create a group. """ self.remove('unittest', assertion=None) self.create('unittest', assertion=True) self.remove('unittest', assertion=None) def test_create_failure(self): """ Group creation fails when the group already exists. """ self.create('root', assertion=False) def test_update_success(self): """ Group update works. """ self.remove('unittest', assertion=None) self.remove('unittest2', assertion=None) self.create('unittest', assertion=None) self.update('unittest', 'unittest2', assertion=True) self.remove('unittest2', assertion=None) def test_update_failure(self): """ Group update fails. """ self.remove('unittest', assertion=None) self.create('unittest', assertion=None) self.update('unittest', 'root', assertion=False) self.remove('unittest', assertion=None) def test_remove_success(self): """ Group deletion works. """ self.create('unittest', assertion=None) self.remove('unittest', assertion=True) def test_remove_failure(self): """ Group deletion fails when the group does not exists. """ # Generate a uuid group name to be sure that the group name # does not exist. group_name = '%s' % uuid.uuid4() self.remove(group_name, assertion=False) def update(self, group_name, new_name, assertion=None): request = self.client.api.update(group_name, new_name) self.client.send(request) responses = self.client.get_responses() if assertion is True: for resp in responses: self.assertTrue(resp['collection'] == 'groups') self.assertTrue(resp['status']) self.assertTrue(resp['status']) self.assertTrue(resp['status'].get('name') == 'unittest2') self.assertTrue(resp['resource_id'] == 'unittest') self.assertFalse('error' in resp) elif assertion is False: for resp in responses: self.assertTrue(resp['collection'] == 'groups') self.assertTrue(resp['status']) self.assertTrue(resp['resource_id'] == 'unittest') self.assertTrue('error' in resp) else: return responses def create(self, group_name, assertion=None): request = self.client.api.create(group_name) self.client.send(request) responses = self.client.get_responses() if assertion is True: for resp in responses: self.assertTrue(resp['collection'] == 'groups') self.assertTrue('gid' in resp['status']) self.assertTrue('members' in resp['status']) self.assertTrue('name' in resp['status']) self.assertTrue(resp['status']['name'] == 'unittest') self.assertFalse('error' in resp) elif assertion is False: for resp in responses: self.assertTrue(resp['collection'] == 'groups') self.assertTrue(resp['status']) self.assertTrue(resp['resource_id'] == 'root') self.assertTrue('error' in resp) else: return responses def remove(self, group_name, assertion=None): request = self.client.api.remove(group_name) self.client.send(request) responses = self.client.get_responses() if assertion is True: for resp in responses: self.assertTrue(resp['collection'] == 'groups') self.assertTrue(resp['status']) self.assertTrue(resp['resource_id'] == 'unittest') self.assertFalse('error' in resp) elif assertion is False: for resp in responses: self.assertTrue(resp['collection'] == 'groups') self.assertTrue(resp['status']) self.assertTrue(resp['resource_id'] == group_name) self.assertTrue('error' in resp) else: return responses