UNPKG

4 kBJavaScriptView Raw
1'use strict';
2
3const Path = require('path');
4const { expect } = require('code');
5const Hapi = require('hapi');
6const Lab = require('lab');
7const StandIn = require('stand-in');
8const CloudApiGql = require('../lib/');
9const CloudApi = require('../lib/cloudapi');
10
11
12const lab = exports.lab = Lab.script();
13const { describe, it, afterEach } = lab;
14
15
16describe('machines', () => {
17 afterEach(() => {
18 StandIn.restoreAll();
19 });
20
21 const register = {
22 plugin: CloudApiGql,
23 options: {
24 keyPath: Path.join(__dirname, 'test.key'),
25 keyId: 'test',
26 apiBaseUrl: 'http://localhost'
27 }
28 };
29
30 const network = {
31 id: '7326787b-8039-436c-a533-5038f7280f04',
32 name: 'default',
33 public: false,
34 fabric: true,
35 gateway: '192.168.128.1',
36 internet_nat: true,
37 provision_end_ip: '192.168.131.250',
38 provision_start_ip: '192.168.128.5',
39 resolvers: [
40 '8.8.8.8',
41 '8.8.4.4'
42 ],
43 subnet: '192.168.128.0/22',
44 vlan_id: 2
45 };
46
47 const machine = {
48 id: 'b6979942-7d5d-4fe6-a2ec-b812e950625a',
49 name: 'test',
50 type: 'smartmachine',
51 brand: 'joyent',
52 state: 'running',
53 image: '2b683a82-a066-11e3-97ab-2faa44701c5a',
54 ips: [
55 '10.88.88.26',
56 '192.168.128.5'
57 ],
58 memory: 128,
59 disk: 12288,
60 metadata: {
61 root_authorized_keys: '...'
62 },
63 tags: {},
64 created: '2016-01-04T12:55:50.539Z',
65 updated: '2016-01-21T08:56:59.000Z',
66 networks: [
67 '7326787b-8039-436c-a533-5038f7280f04',
68 '45607081-4cd2-45c8-baf7-79da760fffaa'
69 ],
70 primaryIp: '10.88.88.26',
71 firewall_enabled: false,
72 compute_node: '564d0b8e-6099-7648-351e-877faf6c56f6',
73 package: 'sdc_128'
74 };
75
76 it('can get a single network', async () => {
77 const server = new Hapi.Server();
78 StandIn.replace(CloudApi.prototype, 'fetch', (stand) => {
79 if (stand.invocations === 1) {
80 return network;
81 }
82
83 return [machine];
84 }, { stopAfter: 2 });
85
86 await server.register(register);
87 await server.initialize();
88 const res = await server.inject({
89 url: '/graphql',
90 method: 'post',
91 payload: { query: `query { network(id: "${network.id}") { id name machines { name } } }` }
92 });
93 expect(res.statusCode).to.equal(200);
94 expect(res.result.data.network).to.exist();
95 expect(res.result.data.network.id).to.equal(network.id);
96 expect(res.result.data.network.name).to.equal(network.name);
97 expect(res.result.data.network.machines[0].name).to.equal(machine.name);
98 });
99
100 it('can get all networks', async () => {
101 const server = new Hapi.Server();
102 StandIn.replaceOnce(CloudApi.prototype, 'fetch', (stand) => {
103 return [network];
104 });
105
106 await server.register(register);
107 await server.initialize();
108 const res = await server.inject({
109 url: '/graphql',
110 method: 'post',
111 payload: { query: 'query { networks { id name } }' }
112 });
113 expect(res.statusCode).to.equal(200);
114 expect(res.result.data.networks).to.exist();
115 expect(res.result.data.networks[0].id).to.equal(network.id);
116 expect(res.result.data.networks[0].name).to.equal(network.name);
117 });
118
119 it('can create a VLAN', async () => {
120 const vlan = {
121 vlan_id: 1,
122 name: 'test',
123 description: 'test'
124 };
125 const server = new Hapi.Server();
126 StandIn.replaceOnce(CloudApi.prototype, 'fetch', (stand) => {
127 return vlan;
128 });
129
130 await server.register(register);
131 await server.initialize();
132 const res = await server.inject({
133 url: '/graphql',
134 method: 'post',
135 payload: { query: 'mutation { createVLAN(id: 1, name: "test", description: "test") { id name description } }' }
136 });
137 expect(res.statusCode).to.equal(200);
138 expect(res.result.data.createVLAN).to.exist();
139 expect(res.result.data.createVLAN.id).to.equal(vlan.vlan_id);
140 expect(res.result.data.createVLAN.name).to.equal(vlan.name);
141 expect(res.result.data.createVLAN.description).to.equal(vlan.description);
142 });
143});