diff options
author | Ben Howard <ben.howard@ubuntu.com> | 2016-02-08 16:33:07 -0700 |
---|---|---|
committer | usd-importer <ubuntu-server@lists.ubuntu.com> | 2016-02-09 00:59:05 +0000 |
commit | a00729ff7421b3661e8b1a1e0fa46393379f2e96 (patch) | |
tree | 4563b927e3a57446a4a928a72a92d72c9ad4f6e6 /tests/distro | |
parent | 53f54030cae2de3d5fa474a61fe51f16c7a07c79 (diff) | |
download | vyos-walinuxagent-a00729ff7421b3661e8b1a1e0fa46393379f2e96.tar.gz vyos-walinuxagent-a00729ff7421b3661e8b1a1e0fa46393379f2e96.zip |
Import patches-unapplied version 2.1.3-0ubuntu1 to ubuntu/xenial-proposed
Imported using git-ubuntu import.
Changelog parent: 53f54030cae2de3d5fa474a61fe51f16c7a07c79
New changelog entries:
* New upstream release (LP: #1543359):
- Bug fixes for extension handling
- Feature enablement for AzureStack.
Diffstat (limited to 'tests/distro')
-rw-r--r-- | tests/distro/__init__.py | 19 | ||||
-rw-r--r-- | tests/distro/test_daemon.py | 65 | ||||
-rw-r--r-- | tests/distro/test_extension.py | 191 | ||||
-rw-r--r-- | tests/distro/test_loader.py | 36 | ||||
-rw-r--r-- | tests/distro/test_monitor.py | 32 | ||||
-rw-r--r-- | tests/distro/test_protocol_util.py | 89 | ||||
-rw-r--r-- | tests/distro/test_provision.py | 47 |
7 files changed, 479 insertions, 0 deletions
diff --git a/tests/distro/__init__.py b/tests/distro/__init__.py new file mode 100644 index 0000000..9bdb27e --- /dev/null +++ b/tests/distro/__init__.py @@ -0,0 +1,19 @@ +# 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+ +# +# Implements parts of RFC 2131, 1541, 1497 and +# http://msdn.microsoft.com/en-us/library/cc227282%28PROT.10%29.aspx +# http://msdn.microsoft.com/en-us/library/cc227259%28PROT.13%29.aspx diff --git a/tests/distro/test_daemon.py b/tests/distro/test_daemon.py new file mode 100644 index 0000000..9d3c45b --- /dev/null +++ b/tests/distro/test_daemon.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+ +# +# Implements parts of RFC 2131, 1541, 1497 and +# http://msdn.microsoft.com/en-us/library/cc227282%28PROT.10%29.aspx +# http://msdn.microsoft.com/en-us/library/cc227259%28PROT.13%29.aspx + +from tests.tools import * +from azurelinuxagent.distro.loader import get_distro +from azurelinuxagent.exception import * +from azurelinuxagent.distro.default.daemon import * + +class MockDaemonCall(object): + def __init__(self, daemon_handler, count): + self.daemon_handler = daemon_handler + self.count = count + + def __call__(self, *args, **kw): + self.count = self.count - 1 + #Stop daemon after restarting for n times + if self.count <= 0: + self.daemon_handler.running = False + raise Exception("Mock unhandled exception") + +@patch("time.sleep") +class TestDaemon(AgentTestCase): + def test_daemon_restart(self, mock_sleep): + distro = get_distro() + mock_daemon = Mock(side_effect=MockDaemonCall(distro.daemon_handler, 2)) + distro.daemon_handler.daemon = mock_daemon + distro.daemon_handler.check_pid = Mock() + distro.daemon_handler.run() + + mock_sleep.assert_any_call(15) + self.assertEquals(2, distro.daemon_handler.daemon.call_count) + + @patch("azurelinuxagent.distro.default.daemon.conf") + @patch("azurelinuxagent.distro.default.daemon.sys.exit") + def test_check_pid(self, mock_exit, mock_conf, mock_sleep): + distro = get_distro() + mock_pid_file = os.path.join(self.tmp_dir, "pid") + mock_conf.get_agent_pid_file_path = Mock(return_value=mock_pid_file) + + distro.daemon_handler.check_pid() + self.assertTrue(os.path.isfile(mock_pid_file)) + + distro.daemon_handler.check_pid() + mock_exit.assert_any_call(0) + +if __name__ == '__main__': + unittest.main() + diff --git a/tests/distro/test_extension.py b/tests/distro/test_extension.py new file mode 100644 index 0000000..d0b631f --- /dev/null +++ b/tests/distro/test_extension.py @@ -0,0 +1,191 @@ +# 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+ +# +# Implements parts of RFC 2131, 1541, 1497 and +# http://msdn.microsoft.com/en-us/library/cc227282%28PROT.10%29.aspx +# http://msdn.microsoft.com/en-us/library/cc227259%28PROT.13%29.aspx + +from tests.tools import * +from tests.protocol.mockwiredata import * +from azurelinuxagent.exception import * +from azurelinuxagent.distro.loader import get_distro +from azurelinuxagent.protocol.restapi import get_properties +from azurelinuxagent.protocol.wire import WireProtocol + +@patch("time.sleep") +@patch("azurelinuxagent.protocol.wire.CryptUtil") +@patch("azurelinuxagent.utils.restutil.http_get") +class TestExtension(AgentTestCase): + + def _assert_handler_status(self, report_vm_status, expected_status, + expected_ext_count, version): + self.assertTrue(report_vm_status.called) + args, kw = report_vm_status.call_args + vm_status = args[0] + self.assertNotEquals(0, len(vm_status.vmAgent.extensionHandlers)) + handler_status = vm_status.vmAgent.extensionHandlers[0] + self.assertEquals(expected_status, handler_status.status) + self.assertEquals("OSTCExtensions.ExampleHandlerLinux", + handler_status.name) + self.assertEquals(version, handler_status.version) + self.assertEquals(expected_ext_count, len(handler_status.extensions)) + + def _assert_no_handler_status(self, report_vm_status): + self.assertTrue(report_vm_status.called) + args, kw = report_vm_status.call_args + vm_status = args[0] + self.assertEquals(0, len(vm_status.vmAgent.extensionHandlers)) + + def _create_mock(self, test_data, mock_http_get, MockCryptUtil, _): + """Test enable/disable/unistall of an extension""" + distro = get_distro() + + #Mock protocol to return test data + mock_http_get.side_effect = test_data.mock_http_get + MockCryptUtil.side_effect = test_data.mock_crypt_util + + protocol = WireProtocol("foo.bar") + protocol.detect() + protocol.report_ext_status = MagicMock() + protocol.report_vm_status = MagicMock() + distro.protocol_util.get_protocol = Mock(return_value=protocol) + + return distro, protocol + + def test_ext_handler(self, *args): + test_data = WireProtocolData(DATA_FILE) + distro, protocol = self._create_mock(test_data, *args) + + #Test enable scenario. + distro.ext_handlers_handler.run() + self._assert_handler_status(protocol.report_vm_status, "Ready", 1, "1.0") + self._assert_ext_status(protocol.report_ext_status, "success", 0) + + #Test goal state not changed + distro.ext_handlers_handler.run() + self._assert_handler_status(protocol.report_vm_status, "Ready", 1, "1.0") + + #Test goal state changed + test_data.goal_state = test_data.goal_state.replace("<Incarnation>1<", + "<Incarnation>2<") + test_data.ext_conf = test_data.ext_conf.replace("seqNo=\"0\"", + "seqNo=\"1\"") + distro.ext_handlers_handler.run() + self._assert_handler_status(protocol.report_vm_status, "Ready", 1, "1.0") + self._assert_ext_status(protocol.report_ext_status, "success", 1) + + #Test upgrade + test_data.goal_state = test_data.goal_state.replace("<Incarnation>2<", + "<Incarnation>3<") + test_data.ext_conf = test_data.ext_conf.replace("1.0", "1.1") + test_data.ext_conf = test_data.ext_conf.replace("seqNo=\"1\"", + "seqNo=\"2\"") + distro.ext_handlers_handler.run() + self._assert_handler_status(protocol.report_vm_status, "Ready", 1, "1.1") + self._assert_ext_status(protocol.report_ext_status, "success", 2) + + #Test disable + test_data.goal_state = test_data.goal_state.replace("<Incarnation>3<", + "<Incarnation>4<") + test_data.ext_conf = test_data.ext_conf.replace("enabled", "disabled") + distro.ext_handlers_handler.run() + self._assert_handler_status(protocol.report_vm_status, "NotReady", + 1, "1.1") + + #Test uninstall + test_data.goal_state = test_data.goal_state.replace("<Incarnation>4<", + "<Incarnation>5<") + test_data.ext_conf = test_data.ext_conf.replace("disabled", "uninstall") + distro.ext_handlers_handler.run() + self._assert_no_handler_status(protocol.report_vm_status) + + #Test uninstall again! + test_data.goal_state = test_data.goal_state.replace("<Incarnation>5<", + "<Incarnation>6<") + distro.ext_handlers_handler.run() + self._assert_no_handler_status(protocol.report_vm_status) + + def test_ext_handler_no_settings(self, *args): + test_data = WireProtocolData(DATA_FILE_EXT_NO_SETTINGS) + distro, protocol = self._create_mock(test_data, *args) + + distro.ext_handlers_handler.run() + self._assert_handler_status(protocol.report_vm_status, "Ready", 0, "1.0") + + def test_ext_handler_no_public_settings(self, *args): + test_data = WireProtocolData(DATA_FILE_EXT_NO_PUBLIC) + distro, protocol = self._create_mock(test_data, *args) + + distro.ext_handlers_handler.run() + self._assert_handler_status(protocol.report_vm_status, "Ready", 1, "1.0") + + def test_ext_handler_no_ext(self, *args): + test_data = WireProtocolData(DATA_FILE_NO_EXT) + distro, protocol = self._create_mock(test_data, *args) + + #Assert no extension handler status + distro.ext_handlers_handler.run() + self._assert_no_handler_status(protocol.report_vm_status) + + @patch('azurelinuxagent.distro.default.extension.add_event') + def test_ext_handler_download_failure(self, mock_add_event, *args): + test_data = WireProtocolData(DATA_FILE) + distro, protocol = self._create_mock(test_data, *args) + protocol.download_ext_handler_pkg = Mock(side_effect=ProtocolError) + + distro.ext_handlers_handler.run() + args, kw = mock_add_event.call_args + self.assertEquals(False, kw['is_success']) + self.assertEquals("OSTCExtensions.ExampleHandlerLinux", kw['name']) + self.assertEquals("Download", kw['op']) + + @patch('azurelinuxagent.distro.default.extension.fileutil') + def test_ext_handler_io_error(self, mock_fileutil, *args): + test_data = WireProtocolData(DATA_FILE) + distro, protocol = self._create_mock(test_data, *args) + + mock_fileutil.write_file.return_value = IOError("Mock IO Error") + distro.ext_handlers_handler.run() + + def _assert_ext_status(self, report_ext_status, expected_status, + expected_seq_no): + self.assertTrue(report_ext_status.called) + args, kw = report_ext_status.call_args + ext_status = args[-1] + self.assertEquals(expected_status, ext_status.status) + self.assertEquals(expected_seq_no, ext_status.sequenceNumber) + + def test_ext_handler_no_reporting_status(self, *args): + test_data = WireProtocolData(DATA_FILE) + distro, protocol = self._create_mock(test_data, *args) + distro.ext_handlers_handler.run() + self._assert_handler_status(protocol.report_vm_status, "Ready", 1, "1.0") + + #Remove status file and re-run collecting extension status + status_file = os.path.join(self.tmp_dir, + "OSTCExtensions.ExampleHandlerLinux-1.0", + "status", "0.status") + self.assertTrue(os.path.isfile(status_file)) + os.remove(status_file) + + distro.ext_handlers_handler.run() + self._assert_handler_status(protocol.report_vm_status, "Ready", 1, "1.0") + self._assert_ext_status(protocol.report_ext_status, "error", 0) + + +if __name__ == '__main__': + unittest.main() + diff --git a/tests/distro/test_loader.py b/tests/distro/test_loader.py new file mode 100644 index 0000000..94ca913 --- /dev/null +++ b/tests/distro/test_loader.py @@ -0,0 +1,36 @@ +# 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+ +# +# Implements parts of RFC 2131, 1541, 1497 and +# http://msdn.microsoft.com/en-us/library/cc227282%28PROT.10%29.aspx +# http://msdn.microsoft.com/en-us/library/cc227259%28PROT.13%29.aspx + +from tests.tools import * +from azurelinuxagent.distro.loader import get_distro +from azurelinuxagent.distro.default.distro import DefaultDistro + +class TestDistroLoader(AgentTestCase): + + @distros() + def test_distro_loader(self, *distro_args): + distro = get_distro(*distro_args) + self.assertNotEquals(None, distro) + self.assertNotEquals(DefaultDistro, type(distro)) + + +if __name__ == '__main__': + unittest.main() + diff --git a/tests/distro/test_monitor.py b/tests/distro/test_monitor.py new file mode 100644 index 0000000..1dd7740 --- /dev/null +++ b/tests/distro/test_monitor.py @@ -0,0 +1,32 @@ +# 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+ +# +# Implements parts of RFC 2131, 1541, 1497 and +# http://msdn.microsoft.com/en-us/library/cc227282%28PROT.10%29.aspx +# http://msdn.microsoft.com/en-us/library/cc227259%28PROT.13%29.aspx + +from tests.tools import * +from azurelinuxagent.exception import * +from azurelinuxagent.distro.default.monitor import * + +class TestMonitor(AgentTestCase): + def test_parse_xml_event(self): + data_str = load_data('ext/event.xml') + event = parse_xml_event(data_str) + self.assertNotEquals(None, event) + self.assertNotEquals(0, event.parameters) + self.assertNotEquals(None, event.parameters[0]) + diff --git a/tests/distro/test_protocol_util.py b/tests/distro/test_protocol_util.py new file mode 100644 index 0000000..61339f3 --- /dev/null +++ b/tests/distro/test_protocol_util.py @@ -0,0 +1,89 @@ +# 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+ +# +# Implements parts of RFC 2131, 1541, 1497 and +# http://msdn.microsoft.com/en-us/library/cc227282%28PROT.10%29.aspx +# http://msdn.microsoft.com/en-us/library/cc227259%28PROT.13%29.aspx + +from tests.tools import * +from azurelinuxagent.distro.loader import get_distro +from azurelinuxagent.exception import * +from azurelinuxagent.distro.default.protocolUtil import * + +@patch("time.sleep") +class TestProtocolUtil(AgentTestCase): + + @distros() + @patch("azurelinuxagent.distro.default.protocolUtil.MetadataProtocol") + @patch("azurelinuxagent.distro.default.protocolUtil.WireProtocol") + def test_detect_protocol(self, distro_name, distro_version, distro_full_name, + WireProtocol, MetadataProtocol, _, *distro_args): + + WireProtocol.return_value = MagicMock() + MetadataProtocol.return_value = MagicMock() + + distro = get_distro(distro_name, distro_version, distro_full_name) + distro.dhcp_handler = MagicMock() + distro.dhcp_handler.endpoint = "foo.bar" + + #Test wire protocol is available + protocol = distro.protocol_util.detect_protocol() + self.assertEquals(WireProtocol.return_value, protocol) + + #Test wire protocol is not available + distro.protocol_util.protocol = None + WireProtocol.side_effect = ProtocolError() + + protocol = distro.protocol_util.detect_protocol() + self.assertEquals(MetadataProtocol.return_value, protocol) + + #Test no protocol is available + distro.protocol_util.protocol = None + WireProtocol.side_effect = ProtocolError() + MetadataProtocol.side_effect = ProtocolError() + self.assertRaises(ProtocolError, distro.protocol_util.detect_protocol) + + @distros() + def test_detect_protocol_by_file(self, distro_name, distro_version, + distro_full_name, _): + distro = get_distro(distro_name, distro_version, distro_full_name) + protocol_util = distro.protocol_util + + protocol_util._detect_wire_protocol = Mock() + protocol_util._detect_metadata_protocol = Mock() + + tag_file = os.path.join(self.tmp_dir, TAG_FILE_NAME) + + #Test tag file doesn't exist + protocol_util.detect_protocol_by_file() + protocol_util._detect_wire_protocol.assert_any_call() + protocol_util._detect_metadata_protocol.assert_not_called() + + #Test tag file exists + protocol_util.protocol = None + protocol_util._detect_wire_protocol.reset_mock() + protocol_util._detect_metadata_protocol.reset_mock() + with open(tag_file, "w+") as tag_fd: + tag_fd.write("") + + protocol_util.detect_protocol_by_file() + protocol_util._detect_metadata_protocol.assert_any_call() + protocol_util._detect_wire_protocol.assert_not_called() + + +if __name__ == '__main__': + unittest.main() + diff --git a/tests/distro/test_provision.py b/tests/distro/test_provision.py new file mode 100644 index 0000000..60249ce --- /dev/null +++ b/tests/distro/test_provision.py @@ -0,0 +1,47 @@ +# 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+ +# +# Implements parts of RFC 2131, 1541, 1497 and +# http://msdn.microsoft.com/en-us/library/cc227282%28PROT.10%29.aspx +# http://msdn.microsoft.com/en-us/library/cc227259%28PROT.13%29.aspx + +from tests.tools import * +from azurelinuxagent.distro.loader import get_distro +from azurelinuxagent.distro.default.protocolUtil import * +import azurelinuxagent.utils.fileutil as fileutil + +class TestProvision(AgentTestCase): + + @distros("redhat") + def test_provision(self, distro_name, distro_version, distro_full_name): + distro = get_distro(distro_name, distro_version, distro_full_name) + distro.osutil = MagicMock() + distro.osutil.decode_customdata = Mock(return_value="") + + distro.protocol_util.detect_protocol_by_file = MagicMock() + distro.protocol_util.get_protocol = MagicMock() + conf.get_dvd_mount_point = Mock(return_value=self.tmp_dir) + + ovfenv_file = os.path.join(self.tmp_dir, OVF_FILE_NAME) + ovfenv_data = load_data("ovf-env.xml") + fileutil.write_file(ovfenv_file, ovfenv_data) + + handler = distro.provision_handler + handler.run() + +if __name__ == '__main__': + unittest.main() + |