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/protocol | |
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/protocol')
-rw-r--r-- | tests/protocol/__init__.py | 19 | ||||
-rw-r--r-- | tests/protocol/mockmetadata.py | 61 | ||||
-rw-r--r-- | tests/protocol/mockwiredata.py | 101 | ||||
-rw-r--r-- | tests/protocol/test_metadata.py | 48 | ||||
-rw-r--r-- | tests/protocol/test_restapi.py | 53 | ||||
-rw-r--r-- | tests/protocol/test_wire.py | 85 |
6 files changed, 367 insertions, 0 deletions
diff --git a/tests/protocol/__init__.py b/tests/protocol/__init__.py new file mode 100644 index 0000000..9bdb27e --- /dev/null +++ b/tests/protocol/__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/protocol/mockmetadata.py b/tests/protocol/mockmetadata.py new file mode 100644 index 0000000..0f7b568 --- /dev/null +++ b/tests/protocol/mockmetadata.py @@ -0,0 +1,61 @@ +# 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.future import httpclient +from azurelinuxagent.utils.cryptutil import CryptUtil + +DATA_FILE = { + "identity": "metadata/identity.json", + "certificates": "metadata/certificates.json", + "ext_handlers": "metadata/ext_handlers.json", + "ext_handler_pkgs": "metadata/ext_handler_pkgs.json", +} + +DATA_FILE_NO_EXT = DATA_FILE.copy() +DATA_FILE_NO_EXT["ext_handlers"] = "metadata/ext_handlers_no_ext.json" + +class MetadataProtocolData(object): + def __init__(self, data_files): + self.identity = load_data(data_files.get("identity")) + self.certificates = load_data(data_files.get("certificates")) + self.ext_handlers = load_data(data_files.get("ext_handlers")) + self.ext_handler_pkgs = load_data(data_files.get("ext_handler_pkgs")) + + def mock_http_get(self, url, *args, **kwargs): + content = None + if url.count(u"identity?") > 0: + content = self.identity + elif url.count(u"certificates") > 0: + content = self.certificates + elif url.count(u"extensionHandlers") > 0: + content = self.ext_handlers + elif url.count(u"versionUri") > 0: + content = self.ext_handler_pkgs + else: + raise Exception("Bad url {0}".format(url)) + resp = MagicMock() + resp.status = httpclient.OK + if content is None: + resp.read = Mock(return_value=None) + else: + resp.read = Mock(return_value=content.encode("utf-8")) + return resp + diff --git a/tests/protocol/mockwiredata.py b/tests/protocol/mockwiredata.py new file mode 100644 index 0000000..6ffd19c --- /dev/null +++ b/tests/protocol/mockwiredata.py @@ -0,0 +1,101 @@ +# 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.future import httpclient +from azurelinuxagent.utils.cryptutil import CryptUtil + +DATA_FILE = { + "version_info": "wire/version_info.xml", + "goal_state": "wire/goal_state.xml", + "hosting_env": "wire/hosting_env.xml", + "shared_config": "wire/shared_config.xml", + "certs": "wire/certs.xml", + "ext_conf": "wire/ext_conf.xml", + "manifest": "wire/manifest.xml", + "trans_prv": "wire/trans_prv", + "trans_cert": "wire/trans_cert", + "test_ext": "ext/sample_ext.zip" +} + +DATA_FILE_NO_EXT = DATA_FILE.copy() +DATA_FILE_NO_EXT["goal_state"] = "wire/goal_state_no_ext.xml" + +DATA_FILE_EXT_NO_SETTINGS = DATA_FILE.copy() +DATA_FILE_EXT_NO_SETTINGS["ext_conf"] = "wire/ext_conf_no_settings.xml" + +DATA_FILE_EXT_NO_PUBLIC = DATA_FILE.copy() +DATA_FILE_EXT_NO_PUBLIC["ext_conf"] = "wire/ext_conf_no_public.xml" + +class WireProtocolData(object): + def __init__(self, data_files=DATA_FILE): + self.version_info = load_data(data_files.get("version_info")) + self.goal_state = load_data(data_files.get("goal_state")) + self.hosting_env = load_data(data_files.get("hosting_env")) + self.shared_config = load_data(data_files.get("shared_config")) + self.certs = load_data(data_files.get("certs")) + self.ext_conf = load_data(data_files.get("ext_conf")) + self.manifest = load_data(data_files.get("manifest")) + self.trans_prv = load_data(data_files.get("trans_prv")) + self.trans_cert = load_data(data_files.get("trans_cert")) + self.ext = load_bin_data(data_files.get("test_ext")) + + def mock_http_get(self, url, *args, **kwargs): + content = None + if "versions" in url: + content = self.version_info + elif "goalstate" in url: + content = self.goal_state + elif "hostingenvuri" in url: + content = self.hosting_env + elif "sharedconfiguri" in url: + content = self.shared_config + elif "certificatesuri" in url: + content = self.certs + elif "extensionsconfiguri" in url: + content = self.ext_conf + elif "manifest.xml" in url: + content = self.manifest + elif "ExampleHandlerLinux" in url: + content = self.ext + resp = MagicMock() + resp.status = httpclient.OK + resp.read = Mock(return_value=content) + return resp + else: + raise Exception("Bad url {0}".format(url)) + resp = MagicMock() + resp.status = httpclient.OK + resp.read = Mock(return_value=content.encode("utf-8")) + return resp + + def mock_crypt_util(self, *args, **kw): + #Partially patch instance method of class CryptUtil + cryptutil = CryptUtil(*args, **kw) + cryptutil.gen_transport_cert = Mock(side_effect=self.mock_gen_trans_cert) + return cryptutil + + def mock_gen_trans_cert(self, trans_prv_file, trans_cert_file): + with open(trans_prv_file, 'w+') as prv_file: + prv_file.write(self.trans_prv) + + with open(trans_cert_file, 'w+') as cert_file: + cert_file.write(self.trans_cert) + diff --git a/tests/protocol/test_metadata.py b/tests/protocol/test_metadata.py new file mode 100644 index 0000000..fca1a82 --- /dev/null +++ b/tests/protocol/test_metadata.py @@ -0,0 +1,48 @@ +# 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.mockmetadata import * +from azurelinuxagent.utils.restutil import httpclient +from azurelinuxagent.protocol.metadata import MetadataProtocol + +@patch("time.sleep") +@patch("azurelinuxagent.protocol.metadata.restutil") +class TestWireProtocolGetters(AgentTestCase): + def _test_getters(self, test_data, mock_restutil ,_): + mock_restutil.http_get.side_effect = test_data.mock_http_get + + protocol = MetadataProtocol() + protocol.detect() + protocol.get_vminfo() + protocol.get_certs() + ext_handlers, etag= protocol.get_ext_handlers() + for ext_handler in ext_handlers.extHandlers: + protocol.get_ext_handler_pkgs(ext_handler) + + def test_getters(self, *args): + test_data = MetadataProtocolData(DATA_FILE) + self._test_getters(test_data, *args) + + def test_getters_no(self, *args): + test_data = MetadataProtocolData(DATA_FILE_NO_EXT) + self._test_getters(test_data, *args) + + diff --git a/tests/protocol/test_restapi.py b/tests/protocol/test_restapi.py new file mode 100644 index 0000000..656ecc6 --- /dev/null +++ b/tests/protocol/test_restapi.py @@ -0,0 +1,53 @@ +# 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 * +import uuid +import unittest +import os +import shutil +import time +from azurelinuxagent.protocol.restapi import * + +class SampleDataContract(DataContract): + def __init__(self): + self.foo = None + self.bar = DataContractList(int) + +class TestDataContract(unittest.TestCase): + def test_get_properties(self): + obj = SampleDataContract() + obj.foo = "foo" + obj.bar.append(1) + data = get_properties(obj) + self.assertEquals("foo", data["foo"]) + self.assertEquals(list, type(data["bar"])) + + def test_set_properties(self): + obj = SampleDataContract() + data = { + 'foo' : 1, + 'baz': 'a' + } + set_properties('sample', obj, data) + self.assertFalse(hasattr(obj, 'baz')) + +if __name__ == '__main__': + unittest.main() diff --git a/tests/protocol/test_wire.py b/tests/protocol/test_wire.py new file mode 100644 index 0000000..4c38c13 --- /dev/null +++ b/tests/protocol/test_wire.py @@ -0,0 +1,85 @@ +# 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 * +import uuid +import unittest +import os +import time +from azurelinuxagent.utils.restutil import httpclient +from azurelinuxagent.utils.cryptutil import CryptUtil +from azurelinuxagent.protocol.restapi import * +from azurelinuxagent.protocol.wire import WireClient, WireProtocol, \ + TRANSPORT_PRV_FILE_NAME, \ + TRANSPORT_CERT_FILE_NAME + +data_with_bom = b'\xef\xbb\xbfhehe' + +@patch("time.sleep") +@patch("azurelinuxagent.protocol.wire.CryptUtil") +@patch("azurelinuxagent.protocol.wire.restutil") +class TestWireProtocolGetters(AgentTestCase): + + def _test_getters(self, test_data, mock_restutil, MockCryptUtil, _): + mock_restutil.http_get.side_effect = test_data.mock_http_get + MockCryptUtil.side_effect = test_data.mock_crypt_util + + protocol = WireProtocol("foo.bar") + protocol.detect() + protocol.get_vminfo() + protocol.get_certs() + ext_handlers, etag = protocol.get_ext_handlers() + for ext_handler in ext_handlers.extHandlers: + protocol.get_ext_handler_pkgs(ext_handler) + + crt1 = os.path.join(self.tmp_dir, + '33B0ABCE4673538650971C10F7D7397E71561F35.crt') + crt2 = os.path.join(self.tmp_dir, + '4037FBF5F1F3014F99B5D6C7799E9B20E6871CB3.crt') + prv2 = os.path.join(self.tmp_dir, + '4037FBF5F1F3014F99B5D6C7799E9B20E6871CB3.prv') + + self.assertTrue(os.path.isfile(crt1)) + self.assertTrue(os.path.isfile(crt2)) + self.assertTrue(os.path.isfile(prv2)) + + def test_getters(self, *args): + """Normal case""" + test_data = WireProtocolData(DATA_FILE) + self._test_getters(test_data, *args) + + def test_getters_no_ext(self, *args): + """Provision with agent is not checked""" + test_data = WireProtocolData(DATA_FILE_NO_EXT) + self._test_getters(test_data, *args) + + def test_getters_ext_no_settings(self, *args): + """Extensions without any settings""" + test_data = WireProtocolData(DATA_FILE_EXT_NO_SETTINGS) + self._test_getters(test_data, *args) + + def test_getters_ext_no_public(self, *args): + """Extensions without any public settings""" + test_data = WireProtocolData(DATA_FILE_EXT_NO_PUBLIC) + self._test_getters(test_data, *args) + +if __name__ == '__main__': + unittest.main() |