@vmngr/libvirt
Version:
Libvirt bindings for Node.js®
320 lines (319 loc) • 17.2 kB
JavaScript
"use strict";
/**
* Copyright 2019 Leon Rinkel <leon@rinkel.me> and vmngr/libvirt contributers.
*
* This file is part of the vmngr/libvirt project and is subject to the MIT
* license as in the LICENSE file in the project root.
*
* @brief Tests for domain-xml.ts.
*/
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var chai_1 = require("chai");
var xml2js_1 = __importDefault(require("xml2js"));
var domain_xml_1 = require("./domain-xml");
describe("DomainOs", function () {
describe("serialize", function () {
it("should work", function () {
var os = domain_xml_1.domainOsXml.serialize({
type: { arch: "x86_64", machine: "q35", value: "hvm" },
boot: { dev: "hd" },
});
var builder = new xml2js_1.default.Builder({ headless: true });
var actualXml = builder.buildObject({ os: os });
var expectedXml = "<os>\n <type arch=\"x86_64\" machine=\"q35\">hvm</type>\n <boot dev=\"hd\"/>\n</os>";
chai_1.expect(actualXml).to.equal(expectedXml);
});
});
describe("deserialize", function () {
it("should work", function () { return __awaiter(void 0, void 0, void 0, function () {
var parsed, actualOsDesc, expectedOsDesc;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, xml2js_1.default.parseStringPromise("<os>\n <type arch=\"x86_64\" machine=\"q35\">hvm</type>\n <boot dev=\"hd\"/>\n</os>")];
case 1:
parsed = _a.sent();
actualOsDesc = domain_xml_1.domainOsXml.deserialize(parsed.os);
expectedOsDesc = {
type: { arch: "x86_64", machine: "q35", value: "hvm" },
boot: { dev: "hd" },
};
chai_1.expect(actualOsDesc).to.deep.equal(expectedOsDesc);
return [2 /*return*/];
}
});
}); });
});
});
describe("DomainDisk", function () {
describe("serialize", function () {
it("should work", function () {
var disk = domain_xml_1.domainDiskXml.serialize({
type: "file", device: "disk",
driver: { name: "qemu", type: "qcow2" },
source: { file: "/home/leon/test1.img" },
target: { dev: "vda", bus: "virtio" },
});
var builder = new xml2js_1.default.Builder({ headless: true });
var actualXml = builder.buildObject({ disk: disk });
var expectedXml = "<disk type=\"file\" device=\"disk\">\n <driver name=\"qemu\" type=\"qcow2\"/>\n <source file=\"/home/leon/test1.img\"/>\n <target dev=\"vda\" bus=\"virtio\"/>\n</disk>";
chai_1.expect(actualXml).to.equal(expectedXml);
});
});
describe("deserialize", function () {
it("should work", function () { return __awaiter(void 0, void 0, void 0, function () {
var parsed, actualDiskDesc, expectedDiskDesc;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, xml2js_1.default.parseStringPromise("<disk type=\"file\" device=\"disk\">\n <driver name=\"qemu\" type=\"qcow2\"/>\n <source file=\"/home/leon/test1.img\"/>\n <target dev=\"vda\" bus=\"virtio\"/>\n</disk>")];
case 1:
parsed = _a.sent();
actualDiskDesc = domain_xml_1.domainDiskXml.deserialize(parsed.disk);
expectedDiskDesc = {
type: "file", device: "disk",
driver: { name: "qemu", type: "qcow2" },
source: { file: "/home/leon/test1.img" },
target: { dev: "vda", bus: "virtio" },
};
chai_1.expect(actualDiskDesc).to.deep.equal(expectedDiskDesc);
return [2 /*return*/];
}
});
}); });
});
});
describe("DomainInterface", function () {
describe("serialize", function () {
it("should work", function () {
var iface = domain_xml_1.domainInterfaceXml.serialize({
type: "network",
source: { network: "default" },
mac: { address: "52:54:00:8e:c6:5f" },
model: { type: "virtio" },
});
var builder = new xml2js_1.default.Builder({ headless: true });
var actualXml = builder.buildObject({ interface: iface });
var expectedXml = "<interface type=\"network\">\n <source network=\"default\"/>\n <mac address=\"52:54:00:8e:c6:5f\"/>\n <model type=\"virtio\"/>\n</interface>";
chai_1.expect(actualXml).to.equal(expectedXml);
});
});
describe("deserialize", function () {
it("should work", function () { return __awaiter(void 0, void 0, void 0, function () {
var parsed, actualInterfaceDesc, expectedInterfaceDesc;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, xml2js_1.default.parseStringPromise("<interface type=\"network\">\n<source network=\"default\"/>\n<mac address=\"52:54:00:8e:c6:5f\"/>\n<model type=\"virtio\"/>\n</interface>")];
case 1:
parsed = _a.sent();
actualInterfaceDesc = domain_xml_1.domainInterfaceXml
.deserialize(parsed.interface);
expectedInterfaceDesc = {
type: "network",
source: { network: "default" },
mac: { address: "52:54:00:8e:c6:5f" },
model: { type: "virtio" },
};
chai_1.expect(actualInterfaceDesc).to.deep.equal(expectedInterfaceDesc);
return [2 /*return*/];
}
});
}); });
});
});
describe("DomainGraphics", function () {
describe("serialize", function () {
it("should work", function () {
var graphics = domain_xml_1.domainGraphicsXml.serialize({
type: "vnc",
port: -1,
listen: "0.0.0.0",
passwd: "test1",
});
var builder = new xml2js_1.default.Builder({ headless: true });
var actualXml = builder.buildObject({ graphics: graphics });
var expectedXml = "<graphics type=\"vnc\" port=\"-1\" listen=\"0.0.0.0\" passwd=\"test1\"/>";
chai_1.expect(actualXml).to.equal(expectedXml);
});
});
describe("deserialize", function () {
it("should work", function () { return __awaiter(void 0, void 0, void 0, function () {
var parsed, actualGraphicsDesc, expectedGraphicsDesc;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, xml2js_1.default.parseStringPromise("<graphics type=\"vnc\" port=\"-1\" listen=\"0.0.0.0\" passwd=\"test1\"/>")];
case 1:
parsed = _a.sent();
actualGraphicsDesc = domain_xml_1.domainGraphicsXml
.deserialize(parsed.graphics);
expectedGraphicsDesc = {
type: "vnc",
port: -1,
listen: "0.0.0.0",
passwd: "test1",
};
chai_1.expect(actualGraphicsDesc).to.deep.equal(expectedGraphicsDesc);
return [2 /*return*/];
}
});
}); });
});
});
describe("domainDescToXml", function () {
it("should work", function () {
var domain = {
type: "kvm",
name: "test1",
uuid: "148d0864-2354-4c27-b82c-731bdd3f320c",
memory: { value: 1048576 },
currentMemory: { value: 1048576 },
vcpu: { value: 1 },
os: {
type: { arch: "x86_64", machine: "q35", value: "hvm" },
boot: { dev: "hd" },
},
devices: [
{
type: "emulator",
emulator: { value: "/usr/bin/qemu-system-x86_64" },
},
{
type: "disk",
disk: {
type: "file", device: "disk",
driver: { name: "qemu", type: "qcow2" },
source: { file: "/home/leon/test1.img" },
target: { dev: "vda", bus: "virtio" },
},
},
{
type: "interface",
interface: {
type: "network",
source: { network: "default" },
mac: { address: "52:54:00:8e:c6:5f" },
model: { type: "virtio" },
},
},
{
type: "console",
console: { type: "pty" },
},
{
type: "graphics",
graphics: {
type: "vnc",
port: -1,
listen: "0.0.0.0",
passwd: "test1",
}
},
],
};
var actualXml = domain_xml_1.domainDescToXml(domain);
var expectedXml = "<domain type=\"kvm\">\n <name>test1</name>\n <uuid>148d0864-2354-4c27-b82c-731bdd3f320c</uuid>\n <memory>1048576</memory>\n <currentMemory>1048576</currentMemory>\n <vcpu>1</vcpu>\n <os>\n <type arch=\"x86_64\" machine=\"q35\">hvm</type>\n <boot dev=\"hd\"/>\n </os>\n <devices>\n <emulator>/usr/bin/qemu-system-x86_64</emulator>\n <disk type=\"file\" device=\"disk\">\n <driver name=\"qemu\" type=\"qcow2\"/>\n <source file=\"/home/leon/test1.img\"/>\n <target dev=\"vda\" bus=\"virtio\"/>\n </disk>\n <interface type=\"network\">\n <source network=\"default\"/>\n <mac address=\"52:54:00:8e:c6:5f\"/>\n <model type=\"virtio\"/>\n </interface>\n <console type=\"pty\"/>\n <graphics type=\"vnc\" port=\"-1\" listen=\"0.0.0.0\" passwd=\"test1\"/>\n </devices>\n</domain>";
chai_1.expect(actualXml).to.equal(expectedXml);
});
});
describe("domainDescFromXml", function () {
it("should work", function () { return __awaiter(void 0, void 0, void 0, function () {
var xml, actualDomainDesc, expectedDomainDesc;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
xml = "<domain type=\"kvm\">\n <name>test1</name>\n <uuid>148d0864-2354-4c27-b82c-731bdd3f320c</uuid>\n <memory>1048576</memory>\n <currentMemory>1048576</currentMemory>\n <vcpu>1</vcpu>\n <os>\n <type arch=\"x86_64\" machine=\"q35\">hvm</type>\n <boot dev=\"hd\"/>\n </os>\n <devices>\n <emulator>/usr/bin/qemu-system-x86_64</emulator>\n <disk type=\"file\" device=\"disk\">\n <driver name=\"qemu\" type=\"qcow2\"/>\n <source file=\"/home/leon/test1.img\"/>\n <target dev=\"vda\" bus=\"virtio\"/>\n </disk>\n <interface type=\"network\">\n <source network=\"default\"/>\n <mac address=\"52:54:00:8e:c6:5f\"/>\n <model type=\"virtio\"/>\n </interface>\n <console type=\"pty\"/>\n <graphics type=\"vnc\" port=\"-1\" listen=\"0.0.0.0\" passwd=\"test1\"/>\n </devices>\n</domain>";
return [4 /*yield*/, domain_xml_1.domainDescFromXml(xml)];
case 1:
actualDomainDesc = _a.sent();
expectedDomainDesc = {
type: "kvm",
name: "test1",
uuid: "148d0864-2354-4c27-b82c-731bdd3f320c",
memory: { value: 1048576 },
currentMemory: { value: 1048576 },
vcpu: { value: 1 },
os: {
type: { arch: "x86_64", machine: "q35", value: "hvm" },
boot: { dev: "hd" },
},
devices: [
{
type: "emulator",
emulator: { value: "/usr/bin/qemu-system-x86_64" },
},
{
type: "disk",
disk: {
type: "file", device: "disk",
driver: { name: "qemu", type: "qcow2" },
source: { file: "/home/leon/test1.img" },
target: { dev: "vda", bus: "virtio" },
},
},
{
type: "interface",
interface: {
type: "network",
source: { network: "default" },
mac: { address: "52:54:00:8e:c6:5f" },
model: { type: "virtio" },
},
},
{
type: "console",
console: { type: "pty" },
},
{
type: "graphics",
graphics: {
type: "vnc",
port: -1,
listen: "0.0.0.0",
passwd: "test1",
}
},
],
};
chai_1.expect(actualDomainDesc).to.deep.equal(expectedDomainDesc);
return [2 /*return*/];
}
});
}); });
});