summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorBen Howard <ben.howard@ubuntu.com>2015-07-02 15:14:26 -0600
committerusd-importer <ubuntu-server@lists.ubuntu.com>2015-07-03 16:03:20 +0000
commite1187be5b25316b2a1a9250b2aa4450179fe1f7d (patch)
tree88d6c1409913e3a630a8e82c5aed5edd26ab16ef /tests
parent5af8d1f80caf5d49988dbb0cf95ad7c58250b90f (diff)
downloadvyos-walinuxagent-e1187be5b25316b2a1a9250b2aa4450179fe1f7d.tar.gz
vyos-walinuxagent-e1187be5b25316b2a1a9250b2aa4450179fe1f7d.zip
Import patches-unapplied version 2.0.13-0ubuntu1 to ubuntu/wily-proposed
Imported using git-ubuntu import. Changelog parent: 5af8d1f80caf5d49988dbb0cf95ad7c58250b90f New changelog entries: * New upstream release (LP: #1449369). * Rebased patches for 2.0.12 onto 2.0.13.
Diffstat (limited to 'tests')
-rw-r--r--tests/__init__.py19
-rw-r--r--tests/env.py6
-rw-r--r--tests/test_http.py147
-rw-r--r--tests/test_shared_config.py10
-rw-r--r--tests/test_util.py61
-rw-r--r--tests/test_utils.py36
-rw-r--r--tests/tools.py61
7 files changed, 337 insertions, 3 deletions
diff --git a/tests/__init__.py b/tests/__init__.py
new file mode 100644
index 0000000..9bdb27e
--- /dev/null
+++ b/tests/__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/env.py b/tests/env.py
index 4e9e052..513df7f 100644
--- a/tests/env.py
+++ b/tests/env.py
@@ -15,9 +15,11 @@
import imp
import os
+import sys
-projet_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
-waagent = imp.load_source('waagent', os.path.join(projet_root, 'waagent'))
+project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
+waagent = imp.load_source('waagent', os.path.join(project_root, 'waagent'))
+sys.path.insert(0, project_root)
waagent.LoggerInit('/dev/stdout', '/dev/null')
diff --git a/tests/test_http.py b/tests/test_http.py
new file mode 100644
index 0000000..eab1382
--- /dev/null
+++ b/tests/test_http.py
@@ -0,0 +1,147 @@
+# 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.
+#
+
+import unittest
+from env import waagent
+import sys
+from tests.tools import *
+
+class MockHTTPResponse(object):
+ def __init__(self, status=200):
+ self.status = status
+ self.reason = "foo"
+
+ def getheaders(*args, **kwargs):
+ return {"hehe" : "haha"}
+
+ def read(*args, **kwargs):
+ return "bar"
+
+class MockOldHTTPConnection(object):
+ MockHost=None
+ MockPort=None
+ MockUrl=None
+ MockCallCount=0
+
+ def __init__(self, host, port):
+ self.__class__.MockHost = host
+ self.__class__.MockPort = port
+
+ def request(self, method, url, data, headers = None):
+ self.__class__.MockUrl = url
+ self.__class__.MockCallCount += 1
+
+ def getresponse(*args, **kwargs):
+ return MockHTTPResponse()
+
+class MockHTTPConnection(MockOldHTTPConnection):
+ def set_tunnel(*args, **kwargs):
+ pass
+
+class MockBadHTTPConnection(MockHTTPConnection):
+ def getresponse(*args, **kwargs):
+ return MockHTTPResponse(500)
+
+class MockHttpLib(object):
+ def __init__(self):
+ self.HTTPConnection = MockHTTPConnection
+ self.OK = 200
+
+MockOSEnv = {
+ "http_proxy":"http://httpproxy:8888",
+ "https_proxy":"https://httpsproxy:8888"
+}
+
+class TestHttp(unittest.TestCase):
+
+ def test_parseurl(self):
+ httputil = waagent.Util()
+ host, port, secure, path = httputil._ParseUrl("http://foo:8/bar?hehe")
+ self.assertEquals("foo", host)
+ self.assertEquals(8, port)
+ self.assertEquals(False, secure)
+ self.assertEquals("/bar?hehe", path)
+
+ host, port, secure, path = httputil._ParseUrl("http://foo.bar/")
+ self.assertEquals("foo.bar", host)
+ self.assertEquals(80, port)
+ self.assertEquals(False, secure)
+ self.assertEquals("/", path)
+
+ host, port, secure, path= httputil._ParseUrl("https://foo.bar/")
+ self.assertEquals("foo.bar", host)
+ self.assertEquals(80, port)
+ self.assertEquals(True, secure)
+ self.assertEquals("/", path)
+
+ self.assertRaises(ValueError, httputil._ParseUrl,
+ "https://a:b@foo.bar/")
+
+ host, port, secure, path = httputil._ParseUrl("https://foo.bar")
+ self.assertEquals("foo.bar", host)
+ self.assertEquals(80, port)
+ self.assertEquals(True, secure)
+ self.assertEquals("/", path)
+
+ host, port, secure, path = httputil._ParseUrl("http://a:b@foo.bar:8888")
+ self.assertEquals("a:b@foo.bar", host)
+ self.assertEquals(8888, port)
+ self.assertEquals(False, secure)
+ self.assertEquals("/", path)
+
+ @Mockup(waagent.httplib, "HTTPConnection", MockHTTPConnection)
+ @Mockup(waagent.os, "environ", MockOSEnv)
+ def test_http_request(self):
+ httputil = waagent.Util()
+
+ #If chkProxy is on, host and port should point to proxy server
+ httputil.HttpRequest("GET", "http://foo.bar/get", chkProxy=True)
+ self.assertEquals("httpproxy", MockHTTPConnection.MockHost)
+ self.assertEquals(8888, MockHTTPConnection.MockPort)
+ self.assertEquals("http://foo.bar:80/get", MockHTTPConnection.MockUrl)
+
+ #If chkProxy is off, ignore proxy
+ httputil.HttpRequest("GET", "http://foo.bar/get", chkProxy=False)
+ self.assertEquals("foo.bar", MockHTTPConnection.MockHost)
+ self.assertEquals(80, MockHTTPConnection.MockPort)
+ self.assertEquals("/get", MockHTTPConnection.MockUrl)
+
+ @Mockup(waagent, "httplib" , MockHttpLib())
+ def test_https_fallback(self):
+ httputil = waagent.Util()
+ print "The bellowing warning log is expected:"
+ httputil.HttpRequest("GET", "https://foo.bar/get")
+ self.assertEquals("/get", MockHTTPConnection.MockUrl)
+
+ @Mockup(waagent.httplib, "HTTPConnection", MockOldHTTPConnection)
+ @Mockup(waagent.httplib, "HTTPSConnection", MockOldHTTPConnection)
+ @Mockup(waagent.os, "environ", MockOSEnv)
+ def test_https_fallback2(self):
+ httputil = waagent.Util()
+ print "The bellowing warning log is expected:"
+ httputil.HttpRequest("GET", "https://foo.bar/get", chkProxy=True)
+ self.assertEquals("http://foo.bar:80/get", MockOldHTTPConnection.MockUrl)
+
+ @Mockup(waagent.Util, "RetryWaitingInterval", 0)
+ @Mockup(waagent.httplib, "HTTPConnection", MockBadHTTPConnection)
+ def test_retry(self):
+ httputil = waagent.Util()
+ MockBadHTTPConnection.MockCallCount=0
+ print "The bellowing error log is expected:"
+ httputil.HttpRequest("GET", "http://foo.bar", chkProxy=False, maxRetry=1)
+ self.assertEquals(2, MockBadHTTPConnection.MockCallCount)
+
+if __name__ == '__main__':
+ unittest.main()
diff --git a/tests/test_shared_config.py b/tests/test_shared_config.py
index 75220b8..ede2766 100644
--- a/tests/test_shared_config.py
+++ b/tests/test_shared_config.py
@@ -16,6 +16,12 @@
import unittest
from env import waagent
+class MockDistro(object):
+ def getInterfaceNameByMac(self, mac):
+ pass
+
+ def configIpV4(self, ifName, addr):
+ pass
class TestSharedConfig(unittest.TestCase):
@@ -24,10 +30,12 @@ class TestSharedConfig(unittest.TestCase):
self.assertNotEquals(None, conf)
self.assertNotEquals(None, conf.RdmaMacAddress)
self.assertNotEquals(None, conf.RdmaIPv4Address)
+ self.assertEquals("00:15:5D:34:00:44", conf.RdmaMacAddress)
return conf
def test_config_rdma(self):
- waagent.LoggerInit("/dev/stdout", "/dev/null", verbose=True)
+ #waagent.LoggerInit("/dev/stdout", "/dev/null", verbose=True)
+ waagent.MyDistro= MockDistro()
testDev = "/tmp/hvnd_rdma"
waagent.SetFileContents(testDev, "")
conf = self.test_parse_shared_config()
diff --git a/tests/test_util.py b/tests/test_util.py
new file mode 100644
index 0000000..6e3ff27
--- /dev/null
+++ b/tests/test_util.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.
+#
+
+import unittest
+from env import waagent
+import sys
+from tests.tools import *
+
+SampleInterfaceInfo="""\
+eth0 Link encap:Ethernet HWaddr ff:ff:ff:ff:ff:ff
+ inet addr:10.94.20.249 Bcast:10.94.23.255 Mask:255.255.252.0
+ inet6 addr: fe80::215:5dff:fe5f:bf03/64 Scope:Link
+ UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
+ RX packets:3789880 errors:0 dropped:0 overruns:0 frame:0
+ TX packets:80973 errors:0 dropped:0 overruns:0 carrier:0
+ collisions:0 txqueuelen:1000
+ RX bytes:388563383 (388.5 MB) TX bytes:21484571 (21.4 MB)
+
+eth1 Link encap:Ethernet HWaddr 00:00:00:00:00:00
+ inet addr:192.168.1.1 Bcast:192.168.1.255 Mask:255.255.255.0
+ inet6 addr: fe80::215:5dff:fe5f:bf08/64 Scope:Link
+ UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
+ RX packets:386614 errors:0 dropped:0 overruns:0 frame:0
+ TX packets:201356 errors:0 dropped:0 overruns:0 carrier:0
+ collisions:0 txqueuelen:1000
+ RX bytes:32507619 (32.5 MB) TX bytes:78342503 (78.3 MB)
+
+lo Link encap:Local Loopback
+ inet addr:127.0.0.1 Mask:255.0.0.0
+ inet6 addr: ::1/128 Scope:Host
+ UP LOOPBACK RUNNING MTU:65536 Metric:1
+ RX packets:2561 errors:0 dropped:0 overruns:0 frame:0
+ TX packets:2561 errors:0 dropped:0 overruns:0 carrier:0
+ collisions:0 txqueuelen:0
+"""
+
+class TestUtil(unittest.TestCase):
+
+ @Mockup(waagent, "RunGetOutput", MockFunc('', (0, SampleInterfaceInfo)))
+ def test_getInterfaceNameByMac(self):
+ distro = waagent.AbstractDistro()
+ ifName = distro.getInterfaceNameByMac("ff:ff:ff:ff:ff:ff")
+ self.assertEquals("eth0", ifName)
+ ifName = distro.getInterfaceNameByMac("00:00:00:00:00:00")
+ self.assertEquals("eth1", ifName)
+
+
+if __name__ == '__main__':
+ unittest.main()
diff --git a/tests/test_utils.py b/tests/test_utils.py
index 00feb6c..e4c1c45 100644
--- a/tests/test_utils.py
+++ b/tests/test_utils.py
@@ -14,6 +14,8 @@
#
import unittest
+import tempfile
+import os
from env import waagent
sample_mount_list = """\
@@ -43,5 +45,39 @@ class TestWAAgentUtils(unittest.TestCase):
mp = waagent.GetMountPoint(malformed, device_name)
self.assertEqual(mp, None)
+ def test_replace_in_file_found(self):
+ tmpfilename = tempfile.mkstemp('', 'tmp', None, True)[1]
+ try:
+ tmpfile = open(tmpfilename, 'w')
+ tmpfile.write('Replace Me')
+ tmpfile.close()
+
+ result = waagent.ReplaceStringInFile(tmpfilename, r'c. ', 'ced ')
+
+ tmpfile = open(tmpfilename, 'r')
+ newcontents = tmpfile.read();
+ tmpfile.close()
+
+ self.assertEqual('Replaced Me', str(newcontents))
+ finally:
+ os.remove(tmpfilename)
+
+ def test_replace_in_file_not_found(self):
+ tmpfilename = tempfile.mkstemp('', 'tmp', None, True)[1]
+ try:
+ tmpfile = open(tmpfilename, 'w')
+ tmpfile.write('Replace Me')
+ tmpfile.close()
+
+ result = waagent.ReplaceStringInFile(tmpfilename, r'not here ', 'ced ')
+
+ tmpfile = open(tmpfilename, 'r')
+ newcontents = tmpfile.read();
+ tmpfile.close()
+
+ self.assertEqual('Replace Me', str(newcontents))
+ finally:
+ os.remove(tmpfilename)
+
if __name__ == '__main__':
unittest.main()
diff --git a/tests/tools.py b/tests/tools.py
new file mode 100644
index 0000000..27e16d3
--- /dev/null
+++ b/tests/tools.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
+
+
+import os
+import sys
+
+parent = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
+sys.path.append(parent)
+
+def simple_file_grep(file_path, search_str):
+ for line in open(file_path):
+ if search_str in line:
+ return line
+
+def Mockup(target, name, mock):
+ def Decorator(func):
+ def Wrapper(*args, **kwargs):
+ origin = getattr(target, name)
+ setattr(target, name, mock)
+ try:
+ result = func(*args, **kwargs)
+ except:
+ raise
+ finally:
+ setattr(target, name, origin)
+ return result
+ return Wrapper
+ return Decorator
+
+class MockFunc():
+ def __init__(self, name='', retval=None):
+ self.name = name
+ self.retval = retval
+
+ def __call__(*args, **kwargs):
+ self = args[0]
+ self.args = args[1:]
+ self.kwargs = kwargs
+ return self.retval
+
+def Dummy():
+ pass
+