def test_95_install_ansible_modules(self): ctxt_agent = CtxtAgent("") ctxt_agent.logger = self.logger general_conf_data = self.gen_general_conf() general_conf_data['ansible_modules'] = ["ansible_role", "git+https://github.com/micafer/ansible-role-hadoop|hadoop"] with open("/tmp/playbook.yaml", 'w') as f: f.write("- tasks: []") res = ctxt_agent.install_ansible_modules(general_conf_data, "/tmp/playbook.yaml") self.assertEqual(res, "/tmp/mod_playbook.yaml") with open("/tmp/mod_playbook.yaml", 'r') as f: data = f.read() yaml_data = yaml.safe_load(data) self.assertEqual(yaml_data[0]['tasks'][0]['package'], "name=git state=present") self.assertEqual(yaml_data[0]['tasks'][1]['copy'][:23], "dest=/tmp/galaxy_roles_") pos = yaml_data[0]['tasks'][1]['copy'].find('content="') copy_content = yaml_data[0]['tasks'][1]['copy'][pos + 9:-2] self.assertEqual(copy_content, "[{src: ansible_role}, {name: hadoop, src: " "'git+https://github.com/micafer/ansible-role-hadoop'}]") self.assertEqual(yaml_data[0]['tasks'][2]['command'][:47], "ansible-galaxy install -c -r /tmp/galaxy_roles_") os.unlink(res)
def test_20_wait_winrm_access(self, socket_connect_ex): socket_connect_ex.return_value = 0 vm = self.gen_vm_data("windows") ctxt_agent = CtxtAgent(None) ctxt_agent.logger = self.logger res = ctxt_agent.wait_winrm_access(vm) self.assertTrue(res)
def test_90_replace_vm_ip(self): vm_data = self.gen_vm_data() ctxt_agent = CtxtAgent("/tmp/gen_data.json") with open("/tmp/gen_data.json", "w+") as f: json.dump(self.gen_general_conf(), f) with open("/tmp/hosts", "w+") as f: f.write("%s_%s " % (vm_data['ip'], vm_data['id'])) f.write(" ansible_host=%s" % vm_data['ip']) f.write(" ansible_ssh_host=%s \n" % vm_data['ip']) vm_data['ctxt_ip'] = "10.0.0.2" vm_data['ctxt_port'] = 22 ctxt_agent.replace_vm_ip(vm_data) with open("/tmp/gen_data.json", "r") as f: general_conf_data = json.load(f) for vm in general_conf_data['vms']: if vm['id'] == vm_data['id']: self.assertEqual(vm['ctxt_ip'], vm_data['ctxt_ip']) with open("/tmp/hosts", "r") as f: data = f.read() self.assertIn( " ansible_host=%s ansible_ssh_host=%s \n" % (vm_data['ctxt_ip'], vm_data['ctxt_ip']), data)
def test_70_contextualize_vm(self, test_connectivity): ctxt_agent = CtxtAgent(None) ctxt_agent.logger = self.logger ctxt_agent.changeVMCredentials = MagicMock() ctxt_agent.changeVMCredentials.return_value = True ctxt_agent.LaunchAnsiblePlaybook = MagicMock() queue = MagicMock() queue.get.return_value = None, 0, None ctxt_agent.LaunchAnsiblePlaybook.return_value = (MagicMock(), queue) ctxt_agent.wait_winrm_access = MagicMock() ctxt_agent.wait_winrm_access.return_value = True ctxt_agent.wait_ssh_access = MagicMock() ctxt_agent.wait_ssh_access.return_value = 'init' ctxt_agent.removeRequiretty = MagicMock() ctxt_agent.removeRequiretty.return_value = True res = ctxt_agent.contextualize_vm(self.gen_general_conf(), self.gen_vm_conf(["basic"])) expected_res = { 'SSH_WAIT': True, 'OK': True, 'CHANGE_CREDS': True, 'basic': True } self.assertEqual(res, expected_res) res = ctxt_agent.contextualize_vm(self.gen_general_conf(), self.gen_vm_conf(["main", "front"])) expected_res = {'OK': True, 'front': True, 'main': True} self.assertEqual(res, expected_res)
def test_30_removeRequiretty(self, execute_timeout): execute_timeout.return_value = "", "", 0 vm = self.gen_vm_data() vm['ctxt_ip'] = "10.0.0.1" vm['ctxt_port'] = 22 ctxt_agent = CtxtAgent(None) ctxt_agent.logger = self.logger res = ctxt_agent.removeRequiretty(vm, None) self.assertTrue(res)
def test_50_launch_ansible_thread(self, queue, ansible_thread): CtxtAgent.logger = self.logger vm = self.gen_vm_data() queue_mock = MagicMock() queue.return_value = queue_mock queue_mock.get.return_value = None, (0, []), None ansible_thread = CtxtAgent.LaunchAnsiblePlaybook(self.logger, "/tmp", "play.yml", vm, 1, "/tmp/inv", "/tmp/pk.pem", 3, True, None) res = CtxtAgent.wait_thread(ansible_thread, "All was OK.") self.assertEqual(res, (True, []))
def test_80_run(self): ctxt_agent = CtxtAgent("/tmp/gen_data.json") ctxt_agent.logger = self.logger ctxt_agent.contextualize_vm = MagicMock() ctxt_agent.contextualize_vm.return_value = {'SSH_WAIT': True, 'OK': True, 'CHANGE_CREDS': True, 'basic': True} with open("/tmp/gen_data.json", "w+") as f: json.dump(self.gen_general_conf(), f) with open("/tmp/vm_data.json", "w+") as f: json.dump(self.gen_vm_conf(["basic"]), f) res = ctxt_agent.run("/tmp/vm_data.json") self.assertTrue(res)
def test_60_changeVMCredentials(self, winrm_session, execute, execute_timeout): ctxt_agent = CtxtAgent(None) ctxt_agent.logger = self.logger execute.return_value = "", "", 0 execute_timeout.return_value = "", "", 0 vm = self.gen_vm_data() vm['ctxt_ip'] = "10.0.0.1" vm['ctxt_port'] = 22 res = ctxt_agent.changeVMCredentials(vm, None) self.assertTrue(res) vm = self.gen_vm_data() del vm['new_passwd'] vm['new_public_key'] = "new_public_key" vm['new_private_key'] = "new_private_key" vm['ctxt_ip'] = "10.0.0.1" vm['ctxt_port'] = 22 res = ctxt_agent.changeVMCredentials(vm, None) self.assertTrue(res) session = MagicMock() req = MagicMock req.status_code = 0 session.run_cmd.return_value = req winrm_session.return_value = session vm = self.gen_vm_data("windows") res = ctxt_agent.changeVMCredentials(vm, None) self.assertTrue(res)
def test_40_run_command(self): CtxtAgent.logger = self.logger res = CtxtAgent.run_command("ls -l", 2, 0.1) self.assertIn("total", str(res))
def test_30_removeRequiretty(self, execute_timeout): CtxtAgent.logger = self.logger execute_timeout.return_value = "", "", 0 vm = self.gen_vm_data() res = CtxtAgent.removeRequiretty(vm, None) self.assertTrue(res)
def test_10_wait_ssh_access(self, test_connectivity): CtxtAgent.logger = self.logger vm = self.gen_vm_data() res = CtxtAgent.wait_ssh_access(vm) self.assertEqual(res, "init")
def test_10_wait_ssh_access(self, test_connectivity): vm = self.gen_vm_data() ctxt_agent = CtxtAgent(None) ctxt_agent.logger = self.logger res = ctxt_agent.wait_ssh_access(vm) self.assertEqual(res, "init")