UNPKG

4.73 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('images', () => {
17 const image = {
18 id: '2b683a82-a066-11e3-97ab-2faa44701c5a',
19 name: 'base',
20 version: '13.4.0',
21 os: 'smartos',
22 requirements: {},
23 type: 'zone-dataset',
24 description: 'A 32-bit SmartOS image with just essential packages installed. Ideal for users who are comfortable with setting up their own environment and tools.',
25 files: [
26 {
27 compression: 'gzip',
28 sha1: '3bebb6ae2cdb26eef20cfb30fdc4a00a059a0b7b',
29 size: 110742036
30 }
31 ],
32 tags: {
33 role: 'os',
34 group: 'base-32'
35 },
36 homepage: 'https://docs.joyent.com/images/smartos/base',
37 published_at: '2014-02-28T10:50:42Z',
38 owner: '930896af-bf8c-48d4-885c-6573a94b1853',
39 public: true,
40 state: 'active'
41 };
42
43 afterEach(() => {
44 StandIn.restoreAll();
45 });
46
47 it('can get all images', async () => {
48 const server = new Hapi.Server();
49 StandIn.replaceOnce(CloudApi.prototype, 'fetch', (stand) => {
50 return [image];
51 });
52
53 await server.register({ plugin: CloudApiGql, options: { keyPath: Path.join(__dirname, 'test.key') } });
54 await server.initialize();
55 const res = await server.inject({
56 url: '/graphql',
57 method: 'post',
58 payload: { query: 'query { images(name: "base") { name os type } }' }
59 });
60 expect(res.statusCode).to.equal(200);
61 expect(res.result.data.images).to.exist();
62 expect(res.result.data.images[0].name).to.equal(image.name);
63 expect(res.result.data.images[0].os).to.equal(image.os.toUpperCase());
64 expect(res.result.data.images[0].type).to.equal(image.type.toUpperCase().replace('-', '_'));
65 });
66
67
68 it('can get a single image', async () => {
69 const server = new Hapi.Server();
70 StandIn.replaceOnce(CloudApi.prototype, 'fetch', (stand) => {
71 return image;
72 });
73
74 await server.register({ plugin: CloudApiGql, options: { keyPath: Path.join(__dirname, 'test.key') } });
75 await server.initialize();
76 const res = await server.inject({
77 url: '/graphql',
78 method: 'post',
79 payload: { query: `query { image(id: "${image.id}") { name os type state } }` }
80 });
81 expect(res.statusCode).to.equal(200);
82 expect(res.result.data.image).to.exist();
83 expect(res.result.data.image.name).to.equal(image.name);
84 expect(res.result.data.image.os).to.equal(image.os.toUpperCase());
85 expect(res.result.data.image.type).to.equal(image.type.toUpperCase().replace('-', '_'));
86 });
87
88 it('can create an image from a machine', async () => {
89 const server = new Hapi.Server();
90 StandIn.replace(CloudApi.prototype, 'fetch', (stand) => {
91 return image;
92 }, { stopAfter: 2 });
93
94 await server.register({ plugin: CloudApiGql, options: { keyPath: Path.join(__dirname, 'test.key') } });
95 await server.initialize();
96 const res = await server.inject({
97 url: '/graphql',
98 method: 'post',
99 payload: { query: `mutation {
100 createImageFromMachine(machine: "${image.id}", name: "base", version: "13.4.0") {
101 name os type version
102 }
103 }` }
104 });
105 expect(res.statusCode).to.equal(200);
106 expect(res.result.data.createImageFromMachine).to.exist();
107 expect(res.result.data.createImageFromMachine.name).to.equal(image.name);
108 expect(res.result.data.createImageFromMachine.os).to.equal(image.os.toUpperCase());
109 expect(res.result.data.createImageFromMachine.type).to.equal(image.type.toUpperCase().replace('-', '_'));
110 });
111
112 it('can update an image', async () => {
113 const server = new Hapi.Server();
114 StandIn.replace(CloudApi.prototype, 'fetch', (stand) => {
115 return image;
116 }, { stopAfter: 2 });
117
118 await server.register({ plugin: CloudApiGql, options: { keyPath: Path.join(__dirname, 'test.key') } });
119 await server.initialize();
120 const res = await server.inject({
121 url: '/graphql',
122 method: 'post',
123 payload: { query: `mutation {
124 updateImage(id: "${image.id}", name: "base", version: "13.4.0") {
125 name os type version
126 }
127 }` }
128 });
129 expect(res.statusCode).to.equal(200);
130 expect(res.result.data.updateImage).to.exist();
131 expect(res.result.data.updateImage.name).to.equal(image.name);
132 expect(res.result.data.updateImage.os).to.equal(image.os.toUpperCase());
133 expect(res.result.data.updateImage.type).to.equal(image.type.toUpperCase().replace('-', '_'));
134 });
135});