Ejemplo n.º 1
0
    def test_probe_and_enlist_doesnt_find_any_vms(self):
        user = factory.make_name("user")
        hostname = factory.make_ipv4_address()
        username = factory.make_name("username")
        password = factory.make_name("password")
        token_name = factory.make_name("token_name")
        token_secret = factory.make_name("token_secret")
        domain = factory.make_name("domain")
        mock_webhook_request = self.patch(
            proxmox_module.ProxmoxPowerDriver, "_webhook_request"
        )
        mock_webhook_request.return_value = succeed(json.dumps({"data": []}))

        with ExpectedException(
            PowerActionError, "No VMs returned! Are permissions set correctly?"
        ):
            yield proxmox_module.probe_proxmox_and_enlist(
                user,
                hostname,
                username,
                password,
                token_name,
                token_secret,
                False,
                False,
                domain,
                None,
            )
Ejemplo n.º 2
0
    def test_probe_and_enlist_ignores_create_node_error(self):
        user = factory.make_name("user")
        hostname = factory.make_ipv4_address()
        username = factory.make_name("username")
        password = factory.make_name("password")
        token_name = factory.make_name("token_name")
        token_secret = factory.make_name("token_secret")
        domain = factory.make_name("domain")
        node1 = factory.make_name("node1")
        vmid1 = random.randint(0, 100)
        mac11 = factory.make_mac_address()
        mac12 = factory.make_mac_address()
        self.mock_create_node.return_value = succeed(None)
        mock_webhook_request = self.patch(
            proxmox_module.ProxmoxPowerDriver, "_webhook_request"
        )
        mock_webhook_request.side_effect = [
            succeed(
                json.dumps(
                    {
                        "data": [
                            {
                                "node": node1,
                                "vmid": vmid1,
                                "name": f"vm {vmid1}",
                                "type": "qemu",
                                "status": "running",
                            },
                        ]
                    }
                ).encode()
            ),
            succeed(
                b"{'data': {"
                b"'net1':'virtio=%s,bridge=vmbr0,firewall=1'"
                b"'net2':'virtio=%s,bridge=vmbr0,firewall=1'"
                b"}}" % (mac11.encode(), mac12.encode())
            ),
            succeed(None),
        ]

        yield proxmox_module.probe_proxmox_and_enlist(
            user,
            hostname,
            username,
            password,
            token_name,
            token_secret,
            False,
            True,
            domain,
            None,
        )

        self.assertThat(
            self.mock_create_node,
            MockCalledOnceWith(
                [mac11, mac12],
                "amd64",
                "proxmox",
                {
                    "power_vm_name": vmid1,
                    "power_address": hostname,
                    "power_user": username,
                    "power_pass": password,
                    "power_token_name": token_name,
                    "power_token_secret": token_secret,
                    "power_verify_ssl": SSL_INSECURE_NO,
                },
                domain,
                hostname=f"vm-{vmid1}",
            ),
        )
        self.assertThat(self.mock_commission_node, MockNotCalled())
Ejemplo n.º 3
0
    def test_probe_and_enlist_filters(self):
        user = factory.make_name("user")
        hostname = factory.make_ipv4_address()
        username = factory.make_name("username")
        password = factory.make_name("password")
        token_name = factory.make_name("token_name")
        token_secret = factory.make_name("token_secret")
        domain = factory.make_name("domain")
        node1 = factory.make_name("node1")
        mac11 = factory.make_mac_address()
        mac12 = factory.make_mac_address()
        node2 = factory.make_name("node2")
        mac21 = factory.make_mac_address()
        mac22 = factory.make_mac_address()
        mock_webhook_request = self.patch(
            proxmox_module.ProxmoxPowerDriver, "_webhook_request"
        )
        mock_webhook_request.side_effect = [
            succeed(
                json.dumps(
                    {
                        "data": [
                            {
                                "node": node1,
                                "vmid": 100,
                                "name": "vm 100",
                                "type": "qemu",
                                "status": "stopped",
                            },
                            {
                                "node": node2,
                                "vmid": 200,
                                "name": "vm 200",
                                "type": "qemu",
                                "status": "stopped",
                            },
                        ]
                    }
                ).encode()
            ),
            succeed(
                b"{'data': {"
                b"'net1':'virtio=%s,bridge=vmbr0,firewall=1'"
                b"'net2':'virtio=%s,bridge=vmbr0,firewall=1'"
                b"}}" % (mac11.encode(), mac12.encode())
            ),
            succeed(
                b"{'data': {"
                b"'net1':'virtio=%s,bridge=vmbr0,firewall=1'"
                b"'net2':'virtio=%s,bridge=vmbr0,firewall=1'"
                b"}}" % (mac21.encode(), mac22.encode())
            ),
        ]

        yield proxmox_module.probe_proxmox_and_enlist(
            user,
            hostname,
            username,
            password,
            token_name,
            token_secret,
            False,
            False,
            domain,
            "vm 1",
        )

        self.assertThat(
            self.mock_create_node,
            MockCalledOnceWith(
                [mac11, mac12],
                "amd64",
                "proxmox",
                {
                    "power_vm_name": 100,
                    "power_address": hostname,
                    "power_user": username,
                    "power_pass": password,
                    "power_token_name": token_name,
                    "power_token_secret": token_secret,
                    "power_verify_ssl": SSL_INSECURE_NO,
                },
                domain,
                hostname="vm-100",
            ),
        )
        self.assertThat(self.mock_commission_node, MockNotCalled())