Browse Source

Add imageList to Openstack wrapper

This patch integrated glance into Openstack wrapper, allowing
imageList to be  used

Change-Id: I64e5d432224aa673ab1e6ace3e927af2f8b3a13f
changes/77/400377/2
Corentin Ardeois 2 years ago
parent
commit
0ba0ea9668
4 changed files with 111 additions and 1 deletions
  1. 2
    1
      src/glance.js
  2. 25
    0
      src/openstack.js
  3. 13
    0
      test/functional/openstackTest.js
  4. 71
    0
      test/unit/openstackTest.js

+ 2
- 1
src/glance.js View File

@@ -58,7 +58,8 @@ export default class Glance extends AbstractService {
58 58
   /**
59 59
    * List the images available on glance.
60 60
    *
61
-   * @param {String} token An authorization token, or a promise which will resolve into one.
61
+   * @param {(String|Promise.<T>)} token An authorization token, or a promise which will resolve
62
+   * into one.
62 63
    * @returns {Promise.<T>} A promise which will resolve with the list of images.
63 64
    */
64 65
   imageList(token = null) {

+ 25
- 0
src/openstack.js View File

@@ -1,5 +1,6 @@
1 1
 import Keystone from "./keystone";
2 2
 import Neutron from "./neutron";
3
+import Glance from "./glance";
3 4
 
4 5
 export default class OpenStack {
5 6
   /**
@@ -34,6 +35,16 @@ export default class OpenStack {
34 35
       .then((neutron) => neutron.networkList(this._token));
35 36
   }
36 37
 
38
+  /**
39
+   * List the images available on glance.
40
+   *
41
+   * @returns {Promise.<T>} A promise which will resolve with the list of images.
42
+   */
43
+  imageList() {
44
+    return this._glance
45
+      .then((glance) => glance.imageList(this._token));
46
+  }
47
+
37 48
   /**
38 49
    * Keystone component.
39 50
    *
@@ -62,6 +73,20 @@ export default class OpenStack {
62 73
     return this._neutronPromise;
63 74
   }
64 75
 
76
+  /**
77
+   * Glance component.
78
+   *
79
+   * @returns {Promise.<Glance>} A promise which will resolve with Glance instance.
80
+   * @private
81
+   */
82
+  get _glance() {
83
+    if (!this._glancePromise) {
84
+      this._glancePromise = this._getComponentConfigFor('glance')
85
+        .then((componentConfig) => new Glance(componentConfig));
86
+    }
87
+    return this._glancePromise;
88
+  }
89
+
65 90
   /**
66 91
    * Token issued from Keystone.
67 92
    *

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

@@ -20,4 +20,17 @@ describe("OpenStack", () => {
20 20
     });
21 21
   });
22 22
 
23
+  describe("imageList()", () => {
24
+    it("should return the images as an array.", (done) => {
25
+      const openstack = new OpenStack(devstackConfig);
26
+
27
+      openstack.imageList()
28
+        .then((images) => {
29
+          expect(images.length > 0).toBeTruthy();
30
+          done();
31
+        })
32
+        .catch((error) => done.fail(error));
33
+    });
34
+  });
35
+
23 36
 });

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

@@ -2,9 +2,11 @@ import OpenStack from "../../src/openstack";
2 2
 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
+import * as glanceMockData from './helpers/data/glance';
5 6
 import fetchMock from 'fetch-mock';
6 7
 import Neutron from "../../src/neutron";
7 8
 import Keystone from "../../src/keystone";
9
+import Glance from "../../src/glance";
8 10
 
9 11
 describe("Simple test", () => {
10 12
 
@@ -47,6 +49,23 @@ describe("Simple test", () => {
47 49
     });
48 50
   });
49 51
 
52
+  describe('imageList', () => {
53
+    it('should fetch imageList from glance', (done) => {
54
+      const openstack = new OpenStack(openStackMockData.config());
55
+      const glance = mockGlance(openstack);
56
+      const imagesData = glanceMockData.imageList('token').response.images;
57
+
58
+      spyOn(glance, 'imageList').and.returnValue(Promise.resolve(imagesData));
59
+
60
+      openstack.imageList()
61
+        .then((images) => {
62
+          expect(images.length).toBe(3);
63
+          done();
64
+        })
65
+        .catch((error) => done.fail(error));
66
+    });
67
+  });
68
+
50 69
   describe('_neutron', () => {
51 70
     it('creates Neutron instance with the correct endpoint', (done) => {
52 71
       const token = 'test_token';
@@ -111,6 +130,52 @@ describe("Simple test", () => {
111 130
     });
112 131
   });
113 132
 
133
+  describe('_glance', () => {
134
+    it('creates Glance instance with the correct endpoint', (done) => {
135
+      const token = 'test_token';
136
+      const openstack = new OpenStack(openStackMockData.config());
137
+      const keystone = mockKeystone(openstack);
138
+      const catalogData = keystoneMockData.catalogList(token).response.catalog;
139
+
140
+      spyOn(keystone, 'tokenIssue').and.returnValue(Promise.resolve(token));
141
+      spyOn(keystone, 'catalogList').and.returnValue(Promise.resolve(catalogData));
142
+
143
+      openstack._glance
144
+        .then((glance) => {
145
+          expect(keystone.catalogList).toHaveBeenCalledWith(token);
146
+          expect(glance).toEqual(jasmine.any(Glance));
147
+          expect(glance.endpointUrl).toEqual('http://192.168.99.99:9292');
148
+          done();
149
+        })
150
+        .catch((error) => done.fail(error));
151
+    });
152
+
153
+    it('should cache Glance instance and Keystone token', (done) => {
154
+      const openstack = new OpenStack(openStackMockData.config());
155
+      const tokenIssueMock = keystoneMockData.tokenIssue();
156
+      const catalogListMock = keystoneMockData.catalogList('test_token');
157
+
158
+      fetchMock.mock(keystoneMockData.root());
159
+      fetchMock.mock(tokenIssueMock);
160
+      fetchMock.mock(catalogListMock);
161
+
162
+      openstack._glance
163
+        .then((glance) => {
164
+          expect(glance).toEqual(jasmine.any(Glance));
165
+          expect(fetchMock.calls(tokenIssueMock.matcher).length).toEqual(1);
166
+          expect(fetchMock.calls(catalogListMock.matcher).length).toEqual(1);
167
+          return openstack._glance;
168
+        })
169
+        .then((glance) => {
170
+          expect(glance).toEqual(jasmine.any(Glance));
171
+          expect(fetchMock.calls(tokenIssueMock.matcher).length).toEqual(1);
172
+          expect(fetchMock.calls(catalogListMock.matcher).length).toEqual(1);
173
+          done();
174
+        })
175
+        .catch((error) => done.fail(error));
176
+    });
177
+  });
178
+
114 179
   describe('_token', () => {
115 180
     it('should fetch the token and cache it', (done) => {
116 181
       const openstack = new OpenStack(openStackMockData.config());
@@ -145,4 +210,10 @@ describe("Simple test", () => {
145 210
     return neutron;
146 211
   }
147 212
 
213
+  function mockGlance(openstack) {
214
+    const glance = new Glance(glanceMockData.config);
215
+    openstack._glancePromise = Promise.resolve(glance);
216
+    return glance;
217
+  }
218
+
148 219
 });

Loading…
Cancel
Save