Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
Merge pull request #640 from kadams54/fix-js-errors-1388465
Browse files Browse the repository at this point in the history
Gracefully handle imperfect unit state data.

When inspecting units for errors, we need to be able to deal with malformed or missing data returned from the API. We already know an error condition exists, so there's no guarantee things will be as they should. Inspect as many fields as possible to determine if there's a relation error. Don't assume a field is actually present; if it isn't, move on.
  • Loading branch information
jujugui committed Nov 5, 2014
2 parents 0ef98f3 + 61c1f87 commit 97f114e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
21 changes: 16 additions & 5 deletions app/views/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -1412,11 +1412,22 @@ YUI.add('juju-view-utils', function(Y) {
// '||' will short-circuit if the source units are in error.
return Y.Array.some(service.units.toArray(), function(unit) {
// Figure out whether or not the unit is in error.
return unit.agent_state === 'error' &&
// Then figure out if the error is pertinent to the relation at
// hand.
unit.agent_state_data.hook.indexOf(endpoint[1].name + '-' +
'relation') === 0;
var relationName = endpoint[1].name + '-' + 'relation',
relationError = false,
agentStateData = unit.agent_state_data,
agentStateInfo = unit.agent_state_info;
// Now we need to determine if the error is relation-related.
// First check the agent_state_data field.
if (agentStateData && agentStateData.hook) {
relationError = (agentStateData.hook.indexOf(relationName) === 0);
}
// Next check the agent_state_info field. In error situations, this
// field may have a message like 'hook failed: "foobar-relation-joined"'
// so we just need to see if the relation name is a substring.
if (!relationError && agentStateInfo) {
relationError = (agentStateInfo.indexOf(relationName) >= 0);
}
return unit.agent_state === 'error' && relationError;
});
};
/**
Expand Down
16 changes: 16 additions & 0 deletions test/test_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -1151,14 +1151,30 @@ describe('utilities', function() {
source._units = [Y.clone(unit)];
target._units = [Y.clone(unit)];
var relation = views.DecoratedRelation(inputRelation, source, target);
// Test no error scenario.
assert.isFalse(relation.sourceHasError());
assert.isFalse(relation.targetHasError());
assert.isFalse(relation.hasRelationError());
// Test error scenario.
source._units[0].agent_state = 'error';
source._units[0].agent_state_data.hook = 'endpoint-1-relation';
assert.isTrue(relation.sourceHasError());
assert.isFalse(relation.targetHasError());
assert.isTrue(relation.hasRelationError());
// Verify that we degrade gracefully with less data.
source._units[0].agent_state = 'error';
source._units[0].agent_state_data = null;
source._units[0].agent_state_info = 'hook failed: "endpoint-1-relation"';
assert.isTrue(relation.sourceHasError());
assert.isFalse(relation.targetHasError());
assert.isTrue(relation.hasRelationError());
// Verify that we degrade gracefull with no data.
source._units[0].agent_state = 'error';
source._units[0].agent_state_data = null;
source._units[0].agent_state_info = null;
assert.isFalse(relation.sourceHasError());
assert.isFalse(relation.targetHasError());
assert.isFalse(relation.hasRelationError());
});

it('can store relations in collections', function() {
Expand Down

0 comments on commit 97f114e

Please sign in to comment.