Skip to content

Commit

Permalink
Merge pull request #39 from caseydavenport/cd4-fvs-with-ipam
Browse files Browse the repository at this point in the history
Return error as json, not dict
  • Loading branch information
Casey Davenport committed Jan 19, 2016
2 parents d590af9 + 3b07cab commit 466f559
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
5 changes: 4 additions & 1 deletion calico_cni/calico_cni.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,13 +370,16 @@ def _call_ipam_plugin(self, env):
except CniError as e:
# We hit a CNI error - return the appropriate CNI formatted
# error dictionary.
response = {"code": e.code, "msg": e.msg, "details": e.details}
response = json.dumps({"code": e.code,
"msg": e.msg,
"details": e.details})
code = e.code
else:
_log.debug("Using binary plugin")
code, response = self._call_binary_ipam_plugin(env)

# Return the IPAM return code and output.
_log.debug("IPAM response (rc=%s): %s", code, response)
return code, response

def _call_binary_ipam_plugin(self, env):
Expand Down
35 changes: 35 additions & 0 deletions calico_cni/tests/fv/test_calico_cni.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,3 +516,38 @@ def test_delete_no_endpoint(self):

# Assert success.
assert_equal(e.code, 0)


class CniPluginFvWithIpamTest(CniPluginFvTest):
"""
Runs tests from CniPluginFvTest but using calico-ipam.
Exercises the ipam.IpamPlugin code.
"""
def setUp(self):
CniPluginFvTest.setUp(self)
self.ipam_type = "calico-ipam"

def set_ipam_result(self, rc, stdout, stderr):
"""
Set up the correct mocks based on the desired stdout.
"""
if stdout and not rc:
# A successful add response.
ip4 = json.loads(stdout)["ip4"]["ip"]
ip6 = json.loads(stdout)["ip6"]["ip"]
ip4s = [ip4] if ip4 else []
ip6s = [ip6] if ip6 else []
self.m_ipam_plugin_client().auto_assign_ips.return_value = ip4s, ip6s

def test_add_ipam_error(self):
# Mock out auto_assign_ips to throw an error.
self.m_ipam_plugin_client().auto_assign_ips.side_effect = RuntimeError

# Assert we handle the error properly in calling code.
CniPluginFvTest.test_add_ipam_error(self)

def test_add_ipam_error_invalid_response(self):
# Not applicable.
pass

2 changes: 1 addition & 1 deletion calico_cni/tests/unit/test_cni_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ def test_call_ipam_plugin_calico_error(self, m_ipam_plugin):
rc, result = self.plugin._call_ipam_plugin(env)

# Assert.
expected = {"code": 150, "msg": "message", "details": "details"}
expected = '{"msg": "message", "code": 150, "details": "details"}'
assert_equal(rc, 150)
assert_equal(result, expected)

Expand Down

0 comments on commit 466f559

Please sign in to comment.