This repository has been archived on 2020-01-20. You can view files and clone it, but cannot push or open issues or pull requests.
node-spearhead/test/integration/api-networks.test.js
Marsell Kukuljevic 0bc11c1e33 TRITON-401 Add support for fabric vlans and networks to node-triton
Reviewed by: Trent Mick <trent.mick@joyent.com>
Reviewed by: Julien Gilli <julien.gilli@joyent.com>
Reviewed by: Pedro P. Candel <pedro@joyent.com>
Approved by: Pedro P. Candel <pedro@joyent.com>
2018-06-25 17:42:33 +02:00

146 lines
3.8 KiB
JavaScript

/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
/*
* Copyright 2018 Joyent, Inc.
*/
/*
* Integration tests for using network-related APIs as a module.
*/
var h = require('./helpers');
var test = require('tape');
// --- Globals
var NET_NAME = 'node-triton-testnet967';
var CLIENT;
var NET;
// --- Tests
test('TritonApi networks', function (tt) {
tt.test(' setup', function (t) {
h.createClient(function (err, client_) {
t.error(err);
CLIENT = client_;
t.end();
});
});
tt.test(' cleanup: rm network ' + NET_NAME + ' if exists', function (t) {
CLIENT.deleteFabricNetwork({id: NET_NAME}, function () {
t.end();
});
});
tt.test(' setup: net', function (t) {
CLIENT.cloudapi.listNetworks({}, function (err, nets) {
if (h.ifErr(t, err))
return t.end();
t.ok(Array.isArray(nets), 'networks');
NET = nets[0];
t.end();
});
});
tt.test(' TritonApi getNetwork', function (t) {
if (!NET) {
return t.end();
}
function check(val, valName, next) {
CLIENT.getNetwork(val, function (err, net) {
if (h.ifErr(t, err, 'no err'))
return t.end();
t.deepEqual(net, NET, valName);
next();
});
}
var shortId = NET.id.split('-')[0];
check(NET.id, 'id', function () {
check(NET.name, 'name', function () {
check(shortId, 'shortId', function () {
t.end();
});
});
});
});
tt.test(' TritonApi deleteFabricNetwork', function (t) {
function check(genId, idType, vlanId, cb) {
CLIENT.cloudapi.createFabricNetwork({
name: NET_NAME,
subnet: '192.168.97.0/24',
provision_start_ip: '192.168.97.1',
provision_end_ip: '192.168.97.254',
vlan_id: vlanId
}, function onCreate(err, net) {
if (h.ifErr(t, err, 'Error creating network')) {
t.end();
return;
}
var id = genId(net);
CLIENT.deleteFabricNetwork({id: id}, function onDelete(err2) {
if (h.ifErr(t, err, 'Error deleting net by ' + idType)) {
t.end();
return;
}
CLIENT.cloudapi.getNetwork(net.id, function onGet(err3) {
t.ok(err3, 'Network should be gone');
cb();
});
});
});
}
// get a VLAN, then create and delete a set of fabrics to check it's
// possible to delete by id, shortId and name
CLIENT.cloudapi.listFabricVlans({}, function onList(err, vlans) {
if (vlans.length === 0) {
t.end();
return;
}
function getId(net) { return net.id; }
function getName(net) { return net.name; }
function getShort(net) { return net.id.split('-')[0]; }
var vlanId = +vlans[0].vlan_id;
check(getId, 'id', vlanId, function onId() {
check(getName, 'name', vlanId, function onName() {
check(getShort, 'shortId', vlanId, function onShort() {
t.end();
});
});
});
});
});
tt.test(' teardown: client', function (t) {
CLIENT.close();
t.end();
});
});