Skip to content

Commit

Permalink
Merge pull request #429 from tannoa2/obmServiceForUcs
Browse files Browse the repository at this point in the history
WIP: Add UCS obm service:Fix Unit Tests
  • Loading branch information
geoff-reid committed Mar 24, 2017
2 parents 4321ae5 + d59f617 commit 4fc16fb
Show file tree
Hide file tree
Showing 5 changed files with 213 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/jobs/ucs-discovery.js
Expand Up @@ -211,6 +211,7 @@ function UcsDiscoveryJobFactory(
id = [config.ucsHost, data.path];
}

config.dn = data.path;
var obm = {
config: config,
service: 'ucs-obm-service'
Expand Down
116 changes: 116 additions & 0 deletions lib/services/ucs-obm-service.js
@@ -0,0 +1,116 @@
// Copyright 2017, EMC, Inc.

'use strict';

var di = require('di'),
util = require('util');

module.exports = UcsObmServiceFactory;

di.annotate(UcsObmServiceFactory,
new di.Provide('ucs-obm-service')
);
di.annotate(UcsObmServiceFactory,
new di.Inject(
'OBM.base',
'Promise',
'Services.Waterline',
'Assert',
'_',
'JobUtils.UcsTool',
'HttpTool'
)
);
function UcsObmServiceFactory(
BaseObmService,
Promise,
waterline,
assert,
_,
UcsTool
) {
function UcsObmService(options) {
BaseObmService.call(this, options);
this.requiredKeys = ['uri'];
this.params = options.params;
this.config = options.config;
this.ucs = this.initClient(this.config);

}
util.inherits(UcsObmService, BaseObmService);

UcsObmService.prototype.initClient = function(settings) {
var ucs = new UcsTool();
ucs.settings = settings;
return ucs;
};

UcsObmService.prototype.powerOn = function() {
return this._runInternal('on');
};

UcsObmService.prototype.powerOff = function() {
return this._runInternal('off');
};

UcsObmService.prototype.reboot = function() {
return this._runInternal('cycle-immediate');
};

UcsObmService.prototype.reset = function() {
return this.reboot();
};

UcsObmService.prototype.powerButton = function() {
return this._runInternal('ipmi-reset');
};

UcsObmService.prototype.powerStatus = function() {
var self = this;
var dn = self.ucs.settings.dn;
var url= "/power?identifier=" + dn;
return self.ucs.clientRequest(url)
.then(function(res) {
assert.object(res, 'Response');
if (res.body.serverState === "down"){
return false;
}
else if(res.body.serverState === "up"){
return true;
}
else {
throw new Error(
'Unknown Power State: ' + self.config.root
);
}
});
};

UcsObmService.prototype._runInternal = function (action) {
return this.run({
action:action
});
};

UcsObmService.prototype.run = function (options) {
var self = this;
return Promise.try(function(){
assert.object(options);
assert.string(options.action);
var action = options.action;
var nodeId = self.options.nodeId;
return waterline.nodes.findOne(nodeId)
.then(function(data){
var url= "/power?" + "identifier=" + data.name + "&action=" + action;
return self.ucs.clientRequest(url, "POST");
});
});
};


UcsObmService.create = function(options) {
return BaseObmService.create(UcsObmService, options);
};

return UcsObmService;
}
3 changes: 2 additions & 1 deletion lib/task-data/base-tasks/obm.js
Expand Up @@ -37,7 +37,8 @@ module.exports = {
"redfish-obm-service",
"servertech-obm-service",
"vbox-obm-service",
"vmrun-obm-service"
"vmrun-obm-service",
"ucs-obm-service"
]
},
},
Expand Down
1 change: 1 addition & 0 deletions spec/lib/services/base-obm-services-spec.js
Expand Up @@ -94,6 +94,7 @@ var base = {
.finally(function() {
// exclude noop and redfish services that don't use run()
if (service.constructor.name !== 'NoopObmService' &&
service.constructor.name !== 'UcsObmService' &&
service.constructor.name !== 'RedfishObmService') {
expect(service.run.callCount).to.be.above(0);
}
Expand Down
93 changes: 93 additions & 0 deletions spec/lib/services/ucs-obm-service-spec.js
@@ -0,0 +1,93 @@
// Copyright 2017, EMC, Inc.
/* jshint node: true */

'use strict';

var base = require('./base-obm-services-spec');


describe('UCS OBM Service', function() {
var servicePath = [ '/lib/services/ucs-obm-service',
'/lib/utils/job-utils/ucs-tool'],
sandbox = sinon.sandbox.create(),
testOptions = {
config: {
'uri': 'localhost',
'ucs-user': 'test',
'ucs-password': 'test'
},
params: {
target: '/sys/chassis-3/blade-3'
},
dn : "/sys/chassis-3/blade-3"
},
testSystem = {
body: {
PowerState : 'On'
}
},

waterline = {};
var node = {
id: 'abc',
type: 'enclosure',
name: 'Node',
identifiers: [],
relations: [
{
relationType: 'encloses',
targets: ['/fake']
},
{
relationType: 'enclosedBy',
targets: ['/fake']
}
]
};

before(function() {
helper.setupInjector([
helper.di.simpleWrapper(waterline,'Services.Waterline')
]);
waterline.nodes = {
findOne: sandbox.stub().resolves(node)
};
});

after(function() {
sandbox.restore();
});

var tool = {
clientRequest: sandbox.stub(),
settings : {
dn : "dn"
}
};

describe('base', function() {
var waterline;
base.before('UCSObmService before', servicePath, function(self) {
waterline = helper.injector.get('Services.Waterline');
waterline.nodes = {
findOne: sandbox.stub().resolves(node)
};
self.serviceOptions = testOptions;
self.Service = helper.injector.get('ucs-obm-service');

sandbox.stub(self.Service.prototype, 'initClient')
.returns(tool);
tool.clientRequest.resolves(testSystem);
});

base.beforeEach('UcsObmService beforeEach');
base.after('UcsObmService after');
base.runInterfaceTestCases(['powerOn',
'powerOff',
'reboot',
'NMI',
'powerButton',
'powerStatus']);
});

});

0 comments on commit 4fc16fb

Please sign in to comment.