Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #429 from tannoa2/obmServiceForUcs
WIP: Add UCS obm service:Fix Unit Tests
- Loading branch information
Showing
5 changed files
with
213 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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']); | ||
}); | ||
|
||
}); |