Browse Source

Add flavorList to Openstack wrapper

This patch integrated nova into Openstack wrapper, allowing
flavorList to be used

Change-Id: Ic149ce2bd11bc439431cca0e2aa294874708d234
Dong Ma 2 years ago
parent
commit
cfc2f665d4
3 changed files with 109 additions and 0 deletions
  1. 25
    0
      src/openstack.js
  2. 13
    0
      test/functional/openstackTest.js
  3. 71
    0
      test/unit/openstackTest.js

+ 25
- 0
src/openstack.js View File

@@ -1,6 +1,7 @@
1 1
 import Keystone from "./keystone";
2 2
 import Neutron from "./neutron";
3 3
 import Glance from "./glance";
4
+import Nova from "./nova";
4 5
 
5 6
 export default class OpenStack {
6 7
   /**
@@ -45,6 +46,16 @@ export default class OpenStack {
45 46
       .then((glance) => glance.imageList(this._token));
46 47
   }
47 48
 
49
+  /**
50
+   * List the flavors available on nova.
51
+   *
52
+   * @returns {Promise.<T>} A promise which will resolve with the list of flavors.
53
+   */
54
+  flavorList() {
55
+    return this._nova
56
+      .then((nova) => nova.flavorList(this._token));
57
+  }
58
+
48 59
   /**
49 60
    * Keystone component.
50 61
    *
@@ -87,6 +98,20 @@ export default class OpenStack {
87 98
     return this._glancePromise;
88 99
   }
89 100
 
101
+  /**
102
+   * Nova component.
103
+   *
104
+   * @returns {Promise.<Nova>} A promise which will resolve with Nova instance.
105
+   * @private
106
+   */
107
+  get _nova() {
108
+    if (!this._novaPromise) {
109
+      this._novaPromise = this._getComponentConfigFor('nova')
110
+        .then((componentConfig) => new Nova(componentConfig));
111
+    }
112
+    return this._novaPromise;
113
+  }
114
+
90 115
   /**
91 116
    * Token issued from Keystone.
92 117
    *

+ 13
- 0
test/functional/openstackTest.js View File

@@ -33,4 +33,17 @@ describe("OpenStack", () => {
33 33
     });
34 34
   });
35 35
 
36
+  describe("flavorList()", () => {
37
+    it("should return the flavors as an array.", (done) => {
38
+      const openstack = new OpenStack(devstackConfig);
39
+
40
+      openstack.flavorList()
41
+        .then((flavors) => {
42
+          expect(flavors.length > 0).toBeTruthy();
43
+          done();
44
+        })
45
+        .catch((error) => done.fail(error));
46
+    });
47
+  });
48
+
36 49
 });

+ 71
- 0
test/unit/openstackTest.js View File

@@ -3,10 +3,12 @@ import * as openStackMockData from './helpers/data/openstack';
3 3
 import * as neutronMockData from './helpers/data/neutron';
4 4
 import * as keystoneMockData from './helpers/data/keystone';
5 5
 import * as glanceMockData from './helpers/data/glance';
6
+import * as novaMockData from './helpers/data/nova';
6 7
 import fetchMock from 'fetch-mock';
7 8
 import Neutron from "../../src/neutron";
8 9
 import Keystone from "../../src/keystone";
9 10
 import Glance from "../../src/glance";
11
+import Nova from "../../src/nova";
10 12
 
11 13
 describe("Simple test", () => {
12 14
 
@@ -66,6 +68,23 @@ describe("Simple test", () => {
66 68
     });
67 69
   });
68 70
 
71
+  describe('flavorList', () => {
72
+    it('should fetch flavorList from nova', (done) => {
73
+      const openstack = new OpenStack(openStackMockData.config());
74
+      const nova = mockNova(openstack);
75
+      const flavorsData = novaMockData.flavorList('token').response.flavors;
76
+
77
+      spyOn(nova, 'flavorList').and.returnValue(Promise.resolve(flavorsData));
78
+
79
+      openstack.flavorList()
80
+        .then((flavors) => {
81
+          expect(flavors.length).toBe(12);
82
+          done();
83
+        })
84
+        .catch((error) => done.fail(error));
85
+    });
86
+  });
87
+
69 88
   describe('_neutron', () => {
70 89
     it('creates Neutron instance with the correct endpoint', (done) => {
71 90
       const token = 'test_token';
@@ -176,6 +195,52 @@ describe("Simple test", () => {
176 195
     });
177 196
   });
178 197
 
198
+  describe('_nova', () => {
199
+    it('creates Nova instance with the correct endpoint', (done) => {
200
+      const token = 'test_token';
201
+      const openstack = new OpenStack(openStackMockData.config());
202
+      const keystone = mockKeystone(openstack);
203
+      const catalogData = keystoneMockData.catalogList(token).response.catalog;
204
+
205
+      spyOn(keystone, 'tokenIssue').and.returnValue(Promise.resolve(token));
206
+      spyOn(keystone, 'catalogList').and.returnValue(Promise.resolve(catalogData));
207
+
208
+      openstack._nova
209
+        .then((nova) => {
210
+          expect(keystone.catalogList).toHaveBeenCalledWith(token);
211
+          expect(nova).toEqual(jasmine.any(Nova));
212
+          expect(nova.endpointUrl).toEqual('http://192.168.99.99:8774/v2.1');
213
+          done();
214
+        })
215
+        .catch((error) => done.fail(error));
216
+    });
217
+
218
+    it('should cache Nova instance and Keystone token', (done) => {
219
+      const openstack = new OpenStack(openStackMockData.config());
220
+      const tokenIssueMock = keystoneMockData.tokenIssue();
221
+      const catalogListMock = keystoneMockData.catalogList('test_token');
222
+
223
+      fetchMock.mock(keystoneMockData.root());
224
+      fetchMock.mock(tokenIssueMock);
225
+      fetchMock.mock(catalogListMock);
226
+
227
+      openstack._nova
228
+        .then((nova) => {
229
+          expect(nova).toEqual(jasmine.any(Nova));
230
+          expect(fetchMock.calls(tokenIssueMock.matcher).length).toEqual(1);
231
+          expect(fetchMock.calls(catalogListMock.matcher).length).toEqual(1);
232
+          return openstack._nova;
233
+        })
234
+        .then((nova) => {
235
+          expect(nova).toEqual(jasmine.any(Nova));
236
+          expect(fetchMock.calls(tokenIssueMock.matcher).length).toEqual(1);
237
+          expect(fetchMock.calls(catalogListMock.matcher).length).toEqual(1);
238
+          done();
239
+        })
240
+        .catch((error) => done.fail(error));
241
+    });
242
+  });
243
+
179 244
   describe('_token', () => {
180 245
     it('should fetch the token and cache it', (done) => {
181 246
       const openstack = new OpenStack(openStackMockData.config());
@@ -216,4 +281,10 @@ describe("Simple test", () => {
216 281
     return glance;
217 282
   }
218 283
 
284
+  function mockNova(openstack) {
285
+    const nova = new Nova(novaMockData.config);
286
+    openstack._novaPromise = Promise.resolve(nova);
287
+    return nova;
288
+  }
289
+
219 290
 });

Loading…
Cancel
Save