From 3b07cab8f62dd3d9687ada2078f030d8b76482fa Mon Sep 17 00:00:00 2001 From: Casey D Date: Mon, 18 Jan 2016 09:04:56 -0800 Subject: [PATCH] FVs cover inline IPAM --- calico_cni/calico_cni.py | 5 +++- calico_cni/tests/fv/test_calico_cni.py | 35 ++++++++++++++++++++++++ calico_cni/tests/unit/test_cni_plugin.py | 2 +- 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/calico_cni/calico_cni.py b/calico_cni/calico_cni.py index a289ca0cb..ffd7a8a37 100755 --- a/calico_cni/calico_cni.py +++ b/calico_cni/calico_cni.py @@ -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): diff --git a/calico_cni/tests/fv/test_calico_cni.py b/calico_cni/tests/fv/test_calico_cni.py index fb9d9e523..41809859f 100644 --- a/calico_cni/tests/fv/test_calico_cni.py +++ b/calico_cni/tests/fv/test_calico_cni.py @@ -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 + diff --git a/calico_cni/tests/unit/test_cni_plugin.py b/calico_cni/tests/unit/test_cni_plugin.py index 54c8679b5..d7c09f099 100644 --- a/calico_cni/tests/unit/test_cni_plugin.py +++ b/calico_cni/tests/unit/test_cni_plugin.py @@ -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)