Added IronicNodePowerTransition resource

A mock resource, much like IronicNodeProvisionTransition, that
permits querying and filtering the power states available to a node.

Change-Id: I5ec5c23d87436662c1b4b4a11144c9a9a91669da
This commit is contained in:
Michael Krotscheck 2016-03-31 14:15:32 -07:00
parent 5ffb01ae7b
commit 175dd0280d
2 changed files with 210 additions and 0 deletions

View File

@ -0,0 +1,93 @@
/*
* Copyright (c) 2016 Hewlett-Packard Enterprise Development Company, LP
*
* 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.
*/
/**
* IronicNodePowerTransition is a mock API that returns the list of valid power
* transitions for nodes.
*/
angular.module('ironic.api').factory('IronicNodePowerTransition',
function($q) {
'use strict';
// Build a dummy resource. The API will return this wrapped in an additional 'transitions'
// field, but we'll just use the raw array.
var transitions = [
{
from_state: "power on",
event: "power off",
target_state: "power off",
actor: "user"
},
{
from_state: "power off",
event: "power on",
target_state: "power on",
actor: "user"
},
{
from_state: "power on",
event: "reboot",
target_state: "rebooting",
actor: "user"
},
{
from_state: "rebooting",
event: "power on",
target_state: "power on",
actor: "conductor"
}
];
return {
query: function(params, successHandler, errorHandler) {
var deferred = $q.defer();
// Build our result array.
var queryResults = [];
queryResults.$promise = deferred.promise;
queryResults.$resolved = false;
deferred.promise.then(function(results) {
angular.forEach(results, function(result) {
queryResults.push(result);
});
});
deferred.promise.finally(function() {
queryResults.$resolved = true;
});
// Check for a filter
if (params) {
var filteredResults = transitions.filter(function(item) {
var approved = true;
angular.forEach(params, function(value, key) {
if (!item.hasOwnProperty(key) || item[key] !== value) {
approved = false;
}
});
return approved;
});
deferred.resolve(filteredResults);
} else {
deferred.resolve(transitions);
}
queryResults.$promise.then(successHandler || null, errorHandler || null);
return queryResults;
}
};
});

View File

@ -0,0 +1,117 @@
/*
* Copyright (c) 2016 Hewlett-Packard Enterprise Development Company, LP
*
* 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.
*/
/**
* Unit tests for ironic's ngResource IronicNode implementation.
*/
describe('Unit: OpenStack Ironic Node Resource',
function() {
'use strict';
var $rootScope;
// We are testing the ironic.api module.
beforeEach(module('ironic.api'));
beforeEach(inject(function($injector) {
// Set up the mock http service
$rootScope = $injector.get('$rootScope');
}));
/**
* Assertion helper, make sure a list of transitions contains one we're looking for.
*
* @param {Array} transitions The transitions array to check.
* @param {String} from The name of the originating state.
* @param {String} actor The name of the actor.
* @param {String} action The name of the transition action name.
* @param {String} target The name of the target state.
* @return {void}
*/
function assertHasTransition (transitions, from, actor, action, target) {
expect(transitions).toContain({
from_state: from,
event: action,
target_state: target,
actor: actor
});
}
it('should implement the query method',
inject(function(IronicNodePowerTransition) {
expect(IronicNodePowerTransition.query).toBeDefined();
// CRUD methods not supported on this resource.
expect(IronicNodePowerTransition.create).not.toBeDefined();
expect(IronicNodePowerTransition.read).not.toBeDefined();
expect(IronicNodePowerTransition.update).not.toBeDefined();
expect(IronicNodePowerTransition.remove).not.toBeDefined();
}));
it('should return a "resource-like" array"',
inject(function(IronicNodePowerTransition) {
var transitions = IronicNodePowerTransition.query();
$rootScope.$apply();
expect(transitions.$resolved).toBeTruthy();
expect(transitions.$promise.$$state.status).toBe(1);
}));
it('should permit filtering.',
inject(function(IronicNodePowerTransition) {
var transitions = IronicNodePowerTransition.query({
from_state: 'power on'
});
$rootScope.$apply();
expect(transitions.length).toEqual(2);
var conductorTransitions = IronicNodePowerTransition.query({
from_state: 'rebooting',
actor: 'conductor'
});
$rootScope.$apply();
expect(conductorTransitions.length).toEqual(1);
}));
it('should permit query promise handlers.',
inject(function(IronicNodePowerTransition) {
var success = false;
IronicNodePowerTransition.query({
from_state: 'power on'
}, function() {
success = true;
});
$rootScope.$apply();
expect(success).toBeTruthy();
}));
it('should return the expected transition graph.',
inject(function(IronicNodePowerTransition) {
var transitions = IronicNodePowerTransition.query();
$rootScope.$apply();
var USER = "user";
var CONDUCTOR = "conductor";
// Power states
assertHasTransition(transitions, "power off", USER, "power on", "power on");
assertHasTransition(transitions, "power on", USER, "power off", "power off");
assertHasTransition(transitions, "power on", USER, "reboot", "rebooting");
assertHasTransition(transitions, "rebooting", CONDUCTOR, "power on", "power on");
}));
});