Example #1
0
    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)
Example #2
0
    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)
Example #3
0
 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)
Example #4
0
    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)
Example #5
0
 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)
Example #6
0
 def test_50_launch_ansible_thread(self, queue, ansible_thread):
     ctxt_agent = CtxtAgent(None)
     ctxt_agent.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 = ctxt_agent.LaunchAnsiblePlaybook(self.logger, "/tmp", "play.yml",
                                                       vm, 1, "/tmp/inv", "/tmp/pk.pem",
                                                       3, True, None)
     res = ctxt_agent.wait_thread(ansible_thread, "All was OK.")
     self.assertEqual(res, True)
Example #7
0
    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)
Example #8
0
 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")