From 9677a06a2dbce932e0d58e6cf8bf08ffaa9f69be Mon Sep 17 00:00:00 2001 From: Dong Ma Date: Tue, 13 Sep 2016 18:31:34 +0800 Subject: [PATCH] OpenStack wrapper instance Create wrapper class that takes clouds.yaml instance Change-Id: I37429b7bda35ca09efc3bf153841d4dc92d96b55 --- src/index.js | 23 ++++++++++++++++++ test/unit/helpers/data/openstack.js | 37 +++++++++++++++++++++++++++++ test/unit/indexTest.js | 32 +++++++++++++++++++++++++ 3 files changed, 92 insertions(+) create mode 100644 test/unit/helpers/data/openstack.js create mode 100644 test/unit/indexTest.js diff --git a/src/index.js b/src/index.js index bbff897..a5404a3 100644 --- a/src/index.js +++ b/src/index.js @@ -1,3 +1,26 @@ export {default as Keystone} from './keystone'; export {default as Glance} from './glance'; export {default as Neutron} from './neutron'; + +export default class OpenStack { + /** + * Create wrapper class that takes clouds.yaml instance + * + * @param {{}} cloudConfig The configuration object for a specific cloud. + */ + constructor(cloudConfig) { + // Sanity checks. + if (!cloudConfig) { + throw new Error('A configuration is required.'); + } + // Clone the config, so that this instance is immutable + // at runtime (no modifying the config after the fact). + cloudConfig = Object.assign({}, cloudConfig); + + this.cloudConfig = cloudConfig; + } + getConfig() { + // Returns the config instance + return this.cloudConfig; + } +} diff --git a/test/unit/helpers/data/openstack.js b/test/unit/helpers/data/openstack.js new file mode 100644 index 0000000..131aadd --- /dev/null +++ b/test/unit/helpers/data/openstack.js @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2016 Hewlett Packard Enterprise Development L.P. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy + * of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See + * the License for the specific language governing permissions and limitations + * under the License. + */ + +/** + * This file contains test data for fetchMock, to simplify bootstrapping of unit tests for + * openstack class. + */ + +/** + * Mock cloud configuration that matches our test data below. This is not a full clouds.yaml + * format, rather just the subsection pointing to a particular cloud. + */ +const cloudConfig = { + region_name: 'Region1', + auth: { + username: 'user', + password: 'pass', + project_name: 'js-openstack-lib', + auth_url: 'http://192.168.99.99/' + } +}; +export { + cloudConfig as config, +}; diff --git a/test/unit/indexTest.js b/test/unit/indexTest.js new file mode 100644 index 0000000..829e46e --- /dev/null +++ b/test/unit/indexTest.js @@ -0,0 +1,32 @@ +import OpenStack from "../../src/index"; +import * as mockData from './helpers/data/openstack'; + +const FetchMock = require('fetch-mock'); + +describe("Simple test", () => { + + afterEach(() => { + FetchMock.reset(); + }); + + it("should export a class", () => { + let t = new OpenStack(mockData.config); + expect(t).toBeDefined(); + }); + + it("should throw an error for an empty config", () => { + try { + let t = new OpenStack(); + t.getConfig(); + } catch (e) { + expect(e.message).toEqual('A configuration is required.'); + } + }); + + it("getConfig should returns the config", () => { + let openstack = new OpenStack(mockData.config); + let config = openstack.getConfig(); + expect(config.region_name).toEqual('Region1'); + }); + +});