summaryrefslogtreecommitdiff
path: root/tests/common
diff options
context:
space:
mode:
Diffstat (limited to 'tests/common')
-rw-r--r--tests/common/__init__.py16
-rw-r--r--tests/common/dhcp/__init__.py16
-rw-r--r--tests/common/dhcp/test_dhcp.py107
-rw-r--r--tests/common/osutil/__init__.py16
-rw-r--r--tests/common/osutil/test_default.py146
-rw-r--r--tests/common/test_version.py65
6 files changed, 366 insertions, 0 deletions
diff --git a/tests/common/__init__.py b/tests/common/__init__.py
new file mode 100644
index 0000000..2ef4c16
--- /dev/null
+++ b/tests/common/__init__.py
@@ -0,0 +1,16 @@
+# Copyright 2014 Microsoft Corporation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+# Requires Python 2.4+ and Openssl 1.0+
+#
diff --git a/tests/common/dhcp/__init__.py b/tests/common/dhcp/__init__.py
new file mode 100644
index 0000000..2ef4c16
--- /dev/null
+++ b/tests/common/dhcp/__init__.py
@@ -0,0 +1,16 @@
+# Copyright 2014 Microsoft Corporation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+# Requires Python 2.4+ and Openssl 1.0+
+#
diff --git a/tests/common/dhcp/test_dhcp.py b/tests/common/dhcp/test_dhcp.py
new file mode 100644
index 0000000..c27ffe8
--- /dev/null
+++ b/tests/common/dhcp/test_dhcp.py
@@ -0,0 +1,107 @@
+# Copyright 2014 Microsoft Corporation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+# Requires Python 2.4+ and Openssl 1.0+
+#
+
+import mock
+import azurelinuxagent.common.dhcp as dhcp
+import azurelinuxagent.common.osutil.default as osutil
+from tests.tools import *
+
+
+class TestDHCP(AgentTestCase):
+ def test_wireserver_route_exists(self):
+ # setup
+ dhcp_handler = dhcp.get_dhcp_handler()
+ self.assertTrue(dhcp_handler.endpoint is None)
+ self.assertTrue(dhcp_handler.routes is None)
+ self.assertTrue(dhcp_handler.gateway is None)
+
+ # execute
+ routing_table = "\
+ Iface Destination Gateway Flags RefCnt Use Metric " \
+ "Mask MTU Window IRTT \n\
+ eth0 00000000 10813FA8 0003 0 0 5 " \
+ "00000000 0 0 0 \n\
+ eth0 00345B0A 00000000 0001 0 0 5 " \
+ "00000000 0 0 0 \n\
+ lo 00000000 01345B0A 0003 0 0 1 " \
+ "00FCFFFF 0 0 0 \n"
+
+ with patch("os.path.exists", return_value=True):
+ mo = mock.mock_open(read_data=routing_table)
+ with patch(open_patch(), mo):
+ self.assertTrue(dhcp_handler.wireserver_route_exists)
+
+ # test
+ self.assertTrue(dhcp_handler.endpoint is not None)
+ self.assertTrue(dhcp_handler.routes is None)
+ self.assertTrue(dhcp_handler.gateway is None)
+
+ def test_wireserver_route_not_exists(self):
+ # setup
+ dhcp_handler = dhcp.get_dhcp_handler()
+ self.assertTrue(dhcp_handler.endpoint is None)
+ self.assertTrue(dhcp_handler.routes is None)
+ self.assertTrue(dhcp_handler.gateway is None)
+
+ # execute
+ self.assertFalse(dhcp_handler.wireserver_route_exists)
+
+ # test
+ self.assertTrue(dhcp_handler.endpoint is None)
+ self.assertTrue(dhcp_handler.routes is None)
+ self.assertTrue(dhcp_handler.gateway is None)
+
+ def test_dhcp_cache_exists(self):
+ dhcp_handler = dhcp.get_dhcp_handler()
+ dhcp_handler.osutil = osutil.DefaultOSUtil()
+ with patch.object(osutil.DefaultOSUtil, 'get_dhcp_lease_endpoint',
+ return_value=None):
+ self.assertFalse(dhcp_handler.dhcp_cache_exists)
+ self.assertEqual(dhcp_handler.endpoint, None)
+ with patch.object(osutil.DefaultOSUtil, 'get_dhcp_lease_endpoint',
+ return_value="foo"):
+ self.assertTrue(dhcp_handler.dhcp_cache_exists)
+ self.assertEqual(dhcp_handler.endpoint, "foo")
+
+ def test_dhcp_skip_cache(self):
+ handler = dhcp.get_dhcp_handler()
+ handler.osutil = osutil.DefaultOSUtil()
+ with patch('os.path.exists', return_value=False):
+ with patch.object(osutil.DefaultOSUtil, 'get_dhcp_lease_endpoint')\
+ as patch_dhcp_cache:
+ with patch.object(dhcp.DhcpHandler, 'send_dhcp_req') \
+ as patch_dhcp_send:
+
+ endpoint = 'foo'
+ patch_dhcp_cache.return_value = endpoint
+
+ # endpoint comes from cache
+ self.assertFalse(handler.skip_cache)
+ handler.run()
+ self.assertTrue(patch_dhcp_cache.call_count == 1)
+ self.assertTrue(patch_dhcp_send.call_count == 0)
+ self.assertTrue(handler.endpoint == endpoint)
+
+ # reset
+ handler.skip_cache = True
+ handler.endpoint = None
+
+ # endpoint comes from dhcp request
+ self.assertTrue(handler.skip_cache)
+ handler.run()
+ self.assertTrue(patch_dhcp_cache.call_count == 1)
+ self.assertTrue(patch_dhcp_send.call_count == 1)
diff --git a/tests/common/osutil/__init__.py b/tests/common/osutil/__init__.py
new file mode 100644
index 0000000..2ef4c16
--- /dev/null
+++ b/tests/common/osutil/__init__.py
@@ -0,0 +1,16 @@
+# Copyright 2014 Microsoft Corporation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+# Requires Python 2.4+ and Openssl 1.0+
+#
diff --git a/tests/common/osutil/test_default.py b/tests/common/osutil/test_default.py
new file mode 100644
index 0000000..d9d00f6
--- /dev/null
+++ b/tests/common/osutil/test_default.py
@@ -0,0 +1,146 @@
+# Copyright 2014 Microsoft Corporation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+# Requires Python 2.4+ and Openssl 1.0+
+#
+
+import socket
+import glob
+import mock
+import azurelinuxagent.common.osutil.default as osutil
+import azurelinuxagent.common.utils.shellutil as shellutil
+from azurelinuxagent.common.osutil import get_osutil
+from tests.tools import *
+
+
+class TestOSUtil(AgentTestCase):
+ def test_restart(self):
+ # setup
+ retries = 3
+ ifname = 'dummy'
+ with patch.object(shellutil, "run") as run_patch:
+ run_patch.return_value = 1
+
+ # execute
+ osutil.DefaultOSUtil.restart_if(osutil.DefaultOSUtil(), ifname=ifname, retries=retries, wait=0)
+
+ # assert
+ self.assertEqual(run_patch.call_count, retries)
+ self.assertEqual(run_patch.call_args_list[0][0][0], 'ifdown {0} && ifup {0}'.format(ifname))
+
+ def test_get_first_if(self):
+ ifname, ipaddr = osutil.DefaultOSUtil().get_first_if()
+ self.assertTrue(ifname.startswith('eth'))
+ self.assertTrue(ipaddr is not None)
+ try:
+ socket.inet_aton(ipaddr)
+ except socket.error:
+ self.fail("not a valid ip address")
+
+ def test_isloopback(self):
+ self.assertTrue(osutil.DefaultOSUtil().is_loopback(b'lo'))
+ self.assertFalse(osutil.DefaultOSUtil().is_loopback(b'eth0'))
+
+ def test_isprimary(self):
+ routing_table = "\
+ Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT \n\
+ eth0 00000000 01345B0A 0003 0 0 5 00000000 0 0 0 \n\
+ eth0 00345B0A 00000000 0001 0 0 5 00000000 0 0 0 \n\
+ lo 00000000 01345B0A 0003 0 0 1 00FCFFFF 0 0 0 \n"
+
+ mo = mock.mock_open(read_data=routing_table)
+ with patch(open_patch(), mo):
+ self.assertFalse(osutil.DefaultOSUtil().is_primary_interface('lo'))
+ self.assertTrue(osutil.DefaultOSUtil().is_primary_interface('eth0'))
+
+ def test_multiple_default_routes(self):
+ routing_table = "\
+ Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT \n\
+ high 00000000 01345B0A 0003 0 0 5 00000000 0 0 0 \n\
+ low1 00000000 01345B0A 0003 0 0 1 00FCFFFF 0 0 0 \n"
+
+ mo = mock.mock_open(read_data=routing_table)
+ with patch(open_patch(), mo):
+ self.assertTrue(osutil.DefaultOSUtil().is_primary_interface('low1'))
+
+ def test_multiple_interfaces(self):
+ routing_table = "\
+ Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT \n\
+ first 00000000 01345B0A 0003 0 0 1 00000000 0 0 0 \n\
+ secnd 00000000 01345B0A 0003 0 0 1 00FCFFFF 0 0 0 \n"
+
+ mo = mock.mock_open(read_data=routing_table)
+ with patch(open_patch(), mo):
+ self.assertTrue(osutil.DefaultOSUtil().is_primary_interface('first'))
+
+ def test_interface_flags(self):
+ routing_table = "\
+ Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT \n\
+ nflg 00000000 01345B0A 0001 0 0 1 00000000 0 0 0 \n\
+ flgs 00000000 01345B0A 0003 0 0 1 00FCFFFF 0 0 0 \n"
+
+ mo = mock.mock_open(read_data=routing_table)
+ with patch(open_patch(), mo):
+ self.assertTrue(osutil.DefaultOSUtil().is_primary_interface('flgs'))
+
+ def test_no_interface(self):
+ routing_table = "\
+ Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT \n\
+ ndst 00000001 01345B0A 0003 0 0 1 00000000 0 0 0 \n\
+ nflg 00000000 01345B0A 0001 0 0 1 00FCFFFF 0 0 0 \n"
+
+ mo = mock.mock_open(read_data=routing_table)
+ with patch(open_patch(), mo):
+ self.assertFalse(osutil.DefaultOSUtil().is_primary_interface('ndst'))
+ self.assertFalse(osutil.DefaultOSUtil().is_primary_interface('nflg'))
+ self.assertFalse(osutil.DefaultOSUtil().is_primary_interface('invalid'))
+
+ def test_no_primary_does_not_throw(self):
+ with patch.object(osutil.DefaultOSUtil, 'get_primary_interface') \
+ as patch_primary:
+ exception = False
+ patch_primary.return_value = ''
+ try:
+ osutil.DefaultOSUtil().get_first_if()[0]
+ except Exception as e:
+ exception = True
+ self.assertFalse(exception)
+
+ def test_dhcp_lease_default(self):
+ self.assertTrue(osutil.DefaultOSUtil().get_dhcp_lease_endpoint() is None)
+
+ def test_dhcp_lease_ubuntu(self):
+ with patch.object(glob, "glob", return_value=['/var/lib/dhcp/dhclient.eth0.leases']):
+ with patch(open_patch(), mock.mock_open(read_data=load_data("dhcp.leases"))):
+ endpoint = get_osutil(distro_name='ubuntu', distro_version='12.04').get_dhcp_lease_endpoint()
+ self.assertTrue(endpoint is not None)
+ self.assertEqual(endpoint, "168.63.129.16")
+
+ endpoint = get_osutil(distro_name='ubuntu', distro_version='12.04').get_dhcp_lease_endpoint()
+ self.assertTrue(endpoint is not None)
+ self.assertEqual(endpoint, "168.63.129.16")
+
+ endpoint = get_osutil(distro_name='ubuntu', distro_version='14.04').get_dhcp_lease_endpoint()
+ self.assertTrue(endpoint is not None)
+ self.assertEqual(endpoint, "168.63.129.16")
+
+ def test_dhcp_lease_multi(self):
+ with patch.object(glob, "glob", return_value=['/var/lib/dhcp/dhclient.eth0.leases']):
+ with patch(open_patch(), mock.mock_open(read_data=load_data("dhcp.leases.multi"))):
+ endpoint = get_osutil(distro_name='ubuntu', distro_version='12.04').get_dhcp_lease_endpoint()
+ self.assertTrue(endpoint is not None)
+ self.assertEqual(endpoint, "second")
+
+if __name__ == '__main__':
+ unittest.main()
diff --git a/tests/common/test_version.py b/tests/common/test_version.py
new file mode 100644
index 0000000..6a4dc38
--- /dev/null
+++ b/tests/common/test_version.py
@@ -0,0 +1,65 @@
+# Copyright 2014 Microsoft Corporation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+# Requires Python 2.4+ and Openssl 1.0+
+#
+
+from __future__ import print_function
+
+import copy
+import glob
+import json
+import os
+import platform
+import random
+import subprocess
+import sys
+import tempfile
+import zipfile
+
+from tests.protocol.mockwiredata import *
+from tests.tools import *
+
+import azurelinuxagent.common.conf as conf
+import azurelinuxagent.common.logger as logger
+import azurelinuxagent.common.utils.fileutil as fileutil
+
+from azurelinuxagent.common.utils.flexible_version import FlexibleVersion
+from azurelinuxagent.common.version import *
+
+
+class TestCurrentAgentName(AgentTestCase):
+ def setUp(self):
+ AgentTestCase.setUp(self)
+ return
+
+ @patch("os.getcwd", return_value="/default/install/directory")
+ def test_extract_name_finds_installed(self, mock_cwd):
+ current_agent, current_version = set_current_agent()
+ self.assertEqual(AGENT_LONG_VERSION, current_agent)
+ self.assertEqual(AGENT_VERSION, str(current_version))
+ return
+
+ @patch("os.getcwd")
+ def test_extract_name_finds_latest_agent(self, mock_cwd):
+ path = os.path.join(conf.get_lib_dir(), "{0}-{1}".format(
+ AGENT_NAME,
+ "1.2.3"))
+ mock_cwd.return_value = path
+ agent = os.path.basename(path)
+ version = AGENT_NAME_PATTERN.match(agent).group(1)
+ current_agent, current_version = set_current_agent()
+ self.assertEqual(agent, current_agent)
+ self.assertEqual(version, str(current_version))
+ return