Skip to content

Conversation

@lanchongyizu
Copy link
Member

@lanchongyizu lanchongyizu commented Jul 17, 2017

Backgroud
This is for the story https://rackhd.atlassian.net/browse/RAC-5639.

During work of https://rackhd.atlassian.net/browse/RAC-5593, we found many unit test cases won't do the clean-up work if running into failure (aka exception handler) , so the plan is to add clean-up work in unit test cases.

DONE

  1. add helper.httpServerBefore and helper.httpServerAfter as common
    methods to start and stop mock HTTP server.
  2. fix the issue that some unit test file doesn't stop mock HTTP server
    when starting mock server timeouts.
  3. add global sandbox in beforeEach, and restore it in afterEach
  4. remove unnecessary resetStubs and restoreStubs

TEST
It's tested in sandbox:
http://10.62.59.175:8080/job/on-core_jamie/

@RackHD/corecommitters

workflowApiService.workflowsGetGraphsByName = this.sandbox.stub(),
workflowApiService.defineTaskGraph = this.sandbox.stub(),
workflowApiService.destroyGraphDefinition = this.sandbox.stub()
arpCache.getCurrent = this.sandbox.stub().resolves([])

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing semicolon.

workflowApiService.getGraphDefinitions = this.sandbox.stub(),
workflowApiService.workflowsGetGraphsByName = this.sandbox.stub(),
workflowApiService.defineTaskGraph = this.sandbox.stub(),
workflowApiService.destroyGraphDefinition = this.sandbox.stub()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing semicolon.

return helper.startServer(overrides, endpointOpt);
});

beforeEach(function () {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since on-core helper.js has already create the sandbox, my suggestion is directly calling on-core's before (https://github.com/RackHD/on-core/blob/master/spec/helper.js#L268) rather than writing some duplicated code.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think your idea is calling

helper.before();
helper.httpServerBefore();

instead of

helper.httpServerBefore();

But we don't need helper.start in on-http, so none of the unit test files calls helper.before.

};

helper.httpServerAfter = function() {
after('helper.httpServer.after', function() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about moving this to httpServerBefore, so that the unit-test doesn't need to call the httpServerAfter

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't make sense here if we want to insert an after block before helper.httpServer.after.
E.g.:

after(function() {
//call something
});
helper.httpServerAfter();

afterEach('tear down mocks', function () {
configuration.set.reset();
configuration.getAll.reset();
helper.httpServerBefore([]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to remove the argument [], since an empty array seems redundant

@lanchongyizu lanchongyizu force-pushed the bugfix/unit-test branch 2 times, most recently from 2fb94e2 to 44e16d4 Compare July 24, 2017 05:26
@lanchongyizu lanchongyizu changed the title stop mock HTTP server when starting it timeouts clean up unit-test errors when it timeouts Jul 27, 2017
}
before(function () {
waterline = helper.injector.get('Services.Waterline');
Errors = helper.injector.get('Errors');helper.injector.get("Services.Configuration");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the purpose of 'helper.injector.get("Services.Configuration");'?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good finding, it's not my purpose here. It's from the original code, and I think we should delete it.

@anhou
Copy link
Member

anhou commented Jul 31, 2017

@lanchongyizu Could you help post the errors in the RAC or in this PR's message? I see lots of minor changes which is not related to exception handler and this PR's topic

spec/helper.js Outdated

helper.httpServerBefore = function(overrides, endpointOpt) {
before('helper.httpServer.before', function() {
this.timeout(5000);
Copy link
Member

@anhou anhou Jul 31, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that some cases are setting timeout to 10000ms previously, if all are set to 5000ms, is it sure that it doesn't have random issues?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously, they might include other tasks, so it was set to 10000ms, but here we just start HTTP server, so it is all set to 5000ms.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After group discussion, it will be set to 10000ms.

@JenkinsRHD
Copy link
Contributor

BUILD on-http #359 : FAILURE

BUILD on-http #359 Error Logs ▼Test Name: Redfish Systems Root should return a valid Redfish storage list Error Details: expected 200 "OK", got 500 "Internal Server Error" Response body: { error: { code: 'Base.1.0.GeneralError', message: 'A general error has occurred. See ExtendedInfo for more information.', '@Message.ExtendedInfo': [ [Object], [Object] ] } } Error: expected 200 "OK", got 500 "Internal Server Error" at Test._assertStatus (/home/jenkins/workspace/on-http/build-deps/on-http/node_modules/supertest/lib/test.js:232:12) at Test._assertFunction (/home/jenkins/workspace/on-http/build-deps/on-http/node_modules/supertest/lib/test.js:247:11) at Test.assert (/home/jenkins/workspace/on-http/build-deps/on-http/node_modules/supertest/lib/test.js:148:18) at assert (/home/jenkins/workspace/on-http/build-deps/on-http/node_modules/supertest/lib/test.js:127:12) at /home/jenkins/workspace/on-http/build-deps/on-http/node_modules/supertest/lib/test.js:124:5 at Test.Request.callback (/home/jenkins/workspace/on-http/build-deps/on-http/node_modules/supertest/node_modules/superagent/lib/node/index.js:703:3) at IncomingMessage.<anonymous> (/home/jenkins/workspace/on-http/build-deps/on-http/node_modules/supertest/node_modules/superagent/lib/node/index.js:922:12) at emitNone (events.js:72:20) at IncomingMessage.emit (events.js:166:7) at endReadableNT (_stream_readable.js:923:12) at nextTickCallbackWith2Args (node.js:458:9) at process._tickDomainCallback (node.js:413:17) Stack Trace: Error: expected 200 "OK", got 500 "Internal Server Error" at Test._assertStatus (node_modules/supertest/lib/test.js:232:12) at Test._assertFunction (node_modules/supertest/lib/test.js:247:11) at Test.assert (node_modules/supertest/lib/test.js:148:18) at assert (node_modules/supertest/lib/test.js:127:12) at node_modules/supertest/lib/test.js:124:5 at Test.Request.callback (node_modules/supertest/node_modules/superagent/lib/node/index.js:703:3) at IncomingMessage.<anonymous> (node_modules/supertest/node_modules/superagent/lib/node/index.js:922:12) at endReadableNT (_stream_readable.js:923:12)

@lanchongyizu
Copy link
Member Author

The main errors are in SH sandbox Jenkins, such as http://10.62.59.175:8080/blue/organizations/jenkins/on-core_jamie/detail/on-core_jamie/29/tests

@lanchongyizu lanchongyizu force-pushed the bugfix/unit-test branch 3 times, most recently from 711a66a to 33b6a09 Compare August 1, 2017 09:10
@JenkinsRHD
Copy link
Contributor

BUILD on-http #368 : FAILURE

Errors = helper.injector.get('Errors');
waterline = helper.injector.get('Services.Waterline');
return helper.injector.get('Views').load();
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see two "before" here after this modification. Will they be called by parallel? If yes, as the previous code is a promise chain, will this change bring any race condition during the initialization?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, they are called one by one.


helper.stopServer = function () {
return helper.injector.get('app').stop();
if (helper.injector) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am curious about the case when helper.injector is null, since all the logic in https://github.com/RackHD/on-core/blob/master/spec/helper.js#L123 looks synchronized.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could imagine that some unhandled exceptions are thrown before https://github.com/RackHD/on-core/blob/master/spec/helper.js#L163, then mocha will go on with the after section, then helper.injector would be undefined.

waterline = helper.injector.get('Services.Waterline');
waterline.localusers = {
findOne: sinon.stub()
findOne: sandbox.stub()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to use "this.sandbox" here to align with other files?

Copy link
Member Author

@lanchongyizu lanchongyizu Aug 2, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this specific file, it defines its own sandbox, so we keep it here. And before section doesn't have this.sandbox, which only exists in beforeEach section.

before('start HTTP server', function () {
this.timeout(5000);

before(function() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Originally, this is to stub redfishTool before httpServer is started, so we can't combine these two before section.

1) add helper.httpServerBefore and helper.httpServerAfter as common
methods to start and stop mock HTTP server.
2) fix the issue that some unit test file doesn't stop mock HTTP server
when starting mock server timeouts.
3) add global sandbox in beforeEach, and restore it in afterEach
4) remove unnecessary resetStubs and restoreStubs
@iceiilin iceiilin merged commit 1f9aa49 into RackHD:master Aug 3, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants