Skip to content

Commit

Permalink
Fix existing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
caseydavenport committed May 23, 2016
1 parent a51a6f5 commit b00bc37
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 15 deletions.
7 changes: 4 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ deploy-rkt: dist/calicoctl
ut: update-version
docker run --rm -v `pwd`:/code \
calico/test \
nosetests tests/unit -c nose.cfg
sh -c "pip install pykube && nosetests tests/unit -c nose.cfg"

# Run the fv tests.
fv: update-version
docker run --rm -v `pwd`:/code \
calico/test \
nosetests tests/fv -c nose.cfg
sh -c "pip install pykube && nosetests tests/fv -c nose.cfg"

# Makes tests on Circle CI.
test-circle: update-version dist/calico dist/calico-ipam
Expand All @@ -63,7 +63,8 @@ test-circle: update-version dist/calico dist/calico-ipam
-v $(CIRCLE_TEST_REPORTS):/circle_output \
-e COVERALLS_REPO_TOKEN=$(COVERALLS_REPO_TOKEN) \
calico/test sh -c \
'nosetests tests -c nose.cfg \
'pip install pykube && \
nosetests tests -c nose.cfg \
--with-xunit --xunit-file=/circle_output/output.xml; RC=$$?;\
[[ ! -z "$$COVERALLS_REPO_TOKEN" ]] && coveralls || true; exit $$RC'

Expand Down
7 changes: 5 additions & 2 deletions calico.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,7 @@ def _call_ipam_plugin(self, env):
_log.debug("Calling host-local IPAM plugin")
code, response = self._call_binary_ipam_plugin(env)
else:
# Using some other IPAM plugin - call it.
_log.debug("Using binary plugin")
code, response = self._call_binary_ipam_plugin(env)

Expand All @@ -486,7 +487,8 @@ def _get_kubernetes_pod_cidr(self):
pod_cidr = f.read().strip()
_log.debug("Loaded podCidr from disk: %s", pod_cidr)
else:
_log.info("Getting node.spec.podCidr from API")
_log.info("Getting node.spec.podCidr from API, kubeconfig: %s",
self.kubeconfig_path)
if not self.kubeconfig_path:
# Fow now, kubeconfig is the only supported auth method.
print_cni_error(ERR_CODE_GENERIC, "Missing kubeconfig",
Expand All @@ -504,7 +506,8 @@ def _get_kubernetes_pod_cidr(self):
break
if not node:
raise KeyError("Unable to find node in API: %s", self.k8s_node_name)
_log.info("Found node: %s: ", node.obj)
_log.debug("Found node %s: %s: ", node.obj["metadata"]["name"],
node.obj["spec"])
except Exception:
print_cni_error(ERR_CODE_GENERIC, "Error querying Kubernetes API",
"Failed to get podCidr from Kubernetes API")
Expand Down
2 changes: 1 addition & 1 deletion calico_cni/policy_drivers.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ def generate_tags(self):
def _get_api_pod(self):
"""Get the pod resource from the API.
:return: JSON object containing the pod spec
:return: Dictionary representation of Pod from k8s API.
"""
# If kubeconfig was specified, use the pykube library.
if self.kubeconfig_path:
Expand Down
39 changes: 30 additions & 9 deletions tests/unit/test_policy_drivers.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def setUp(self):
"policy": {}
}
self.driver = KubernetesAnnotationDriver(self.pod_name, self.namespace,
self.auth_token, self.api_root, None, None, None)
self.auth_token, self.api_root, None, None, None, None)
assert_equal(self.driver.profile_name, self.profile_name)

# Mock the DatastoreClient
Expand All @@ -161,7 +161,7 @@ def test_generate_rules_kube_system(self, m_get_pod):
# Generate rules
rules = self.driver.generate_rules()

# Assert correct. Should allow all.
# Assert correct. Should allow all.
expected = Rules(id=self.profile_name,
inbound_rules=[Rule(action="allow")],
outbound_rules=[Rule(action="allow")])
Expand All @@ -187,7 +187,7 @@ def test_generate_rules_annotations(self, m_get_pod):
# Generate rules
rules = self.driver.generate_rules()

# Assert correct. Should allow all.
# Assert correct. Should allow all.
expected = Rules(id=self.profile_name,
inbound_rules=[Rule(action="allow", protocol="tcp")],
outbound_rules=[Rule(action="allow")])
Expand All @@ -202,7 +202,7 @@ def test_generate_rules_parse_error(self, m_get_pod):

# Mock to raise error
self.driver.policy_parser = MagicMock(spec=self.driver.policy_parser)
self.driver.policy_parser.parse_line.side_effect = ValueError
self.driver.policy_parser.parse_line.side_effect = ValueError

# Generate rules
assert_raises(ApplyProfileError, self.driver.generate_rules)
Expand All @@ -211,7 +211,7 @@ def test_generate_tags(self):
# Mock get_metadata to return labels.
labels = {"key": "value"}
self.driver._get_metadata = MagicMock(spec=self.driver._get_metadata)
self.driver._get_metadata.return_value = labels
self.driver._get_metadata.return_value = labels

# Call
tags = self.driver.generate_tags()
Expand Down Expand Up @@ -257,6 +257,28 @@ def test_get_api_pod(self, m_json_load, m_session):
verify=False)
m_json_load.assert_called_once_with(pod1)

@patch("calico_cni.policy_drivers.HTTPClient", autospec=True)
@patch("calico_cni.policy_drivers.Query", autospec=True)
@patch("calico_cni.policy_drivers.KubeConfig", autospec=True)
def test_get_api_pod_kubeconfig(self, m_kcfg, m_query, m_http):
# Set up driver.
self.driver.pod_name = 'pod-1'
self.driver.namespace = 'a'

pod = Mock()
pod.obj = '{"metadata": {"namespace": "a", "name": "pod-1"}}'
m_query().get_by_name.return_value = pod

api_root = "http://kubernetesapi:8080/api/v1/"
self.driver.api_root = api_root
self.driver.kubeconfig_path = "/path/to/kubeconfig"

# Call method under test
p = self.driver._get_api_pod()

# Assert
assert_equal(p, pod.obj)

@patch('calico_cni.policy_drivers.requests.Session', autospec=True)
@patch('json.loads', autospec=True)
def test_get_api_pod_with_client_certs(self, m_json_load, m_session):
Expand All @@ -271,7 +293,6 @@ def test_get_api_pod_with_client_certs(self, m_json_load, m_session):
self.driver.client_key = "key.pem"
self.driver.certificate_authority = "ca.pem"


get_obj = Mock()
get_obj.status_code = 200
get_obj.text = pod1
Expand Down Expand Up @@ -353,7 +374,7 @@ def test_get_api_pod_parse_error(self, m_json_load, m_session):
# Set up mock objects
self.driver.auth_token = 'TOKEN'

m_json_load.side_effect = TypeError
m_json_load.side_effect = TypeError

get_obj = Mock()
get_obj.status_code = 200
Expand Down Expand Up @@ -388,7 +409,7 @@ def setUp(self):
self.network_name = "net-name"
self.namespace = "default"
self.driver = KubernetesPolicyDriver(self.network_name, self.namespace,
None, None, None, None, None)
None, None, None, None, None, None)

# Mock the DatastoreClient
self.client = MagicMock(spec=DatastoreClient)
Expand Down Expand Up @@ -417,7 +438,7 @@ def test_remove_profile(self):
self.driver.remove_profile()


class GetPolicyDriverTest(unittest.TestCase):
class GetPolicyDriverTest(unittest.TestCase):

def test_get_policy_driver_default_k8s(self):
cni_plugin = Mock(spec=CniPlugin)
Expand Down

0 comments on commit b00bc37

Please sign in to comment.