summaryrefslogtreecommitdiff
path: root/tests/unittests
diff options
context:
space:
mode:
Diffstat (limited to 'tests/unittests')
-rw-r--r--tests/unittests/test_distros/test_hosts.py41
-rw-r--r--tests/unittests/test_distros/test_resolv.py61
2 files changed, 102 insertions, 0 deletions
diff --git a/tests/unittests/test_distros/test_hosts.py b/tests/unittests/test_distros/test_hosts.py
new file mode 100644
index 00000000..621837ab
--- /dev/null
+++ b/tests/unittests/test_distros/test_hosts.py
@@ -0,0 +1,41 @@
+from mocker import MockerTestCase
+
+from cloudinit.distros import helpers
+
+
+BASE_ETC = '''
+# Example
+127.0.0.1 localhost
+192.168.1.10 foo.mydomain.org foo
+192.168.1.10 bar.mydomain.org bar
+146.82.138.7 master.debian.org master
+209.237.226.90 www.opensource.org
+'''
+BASE_ETC = BASE_ETC.strip()
+
+
+class TestHostsHelper(MockerTestCase):
+ def test_parse(self):
+ eh = helpers.HostsConf(BASE_ETC)
+ self.assertEquals(eh.get_entry('127.0.0.1'), [['localhost']])
+ self.assertEquals(eh.get_entry('192.168.1.10'),
+ [['foo.mydomain.org', 'foo'],
+ ['bar.mydomain.org', 'bar']])
+ eh = str(eh)
+ self.assertTrue(eh.startswith('# Example'))
+
+ def test_add(self):
+ eh = helpers.HostsConf(BASE_ETC)
+ eh.add_entry('127.0.0.0', 'blah')
+ self.assertEquals(eh.get_entry('127.0.0.0'), [['blah']])
+ eh.add_entry('127.0.0.3', 'blah', 'blah2', 'blah3')
+ self.assertEquals(eh.get_entry('127.0.0.3'),
+ [['blah', 'blah2', 'blah3']])
+
+ def test_del(self):
+ eh = helpers.HostsConf(BASE_ETC)
+ eh.add_entry('127.0.0.0', 'blah')
+ self.assertEquals(eh.get_entry('127.0.0.0'), [['blah']])
+
+ eh.del_entries('127.0.0.0')
+ self.assertEquals(eh.get_entry('127.0.0.0'), [])
diff --git a/tests/unittests/test_distros/test_resolv.py b/tests/unittests/test_distros/test_resolv.py
new file mode 100644
index 00000000..5f122833
--- /dev/null
+++ b/tests/unittests/test_distros/test_resolv.py
@@ -0,0 +1,61 @@
+from mocker import MockerTestCase
+
+from cloudinit.distros import helpers
+
+
+BASE_RESOLVE = '''
+; generated by /sbin/dhclient-script
+search blah.yahoo.com yahoo.com
+nameserver 10.15.44.14
+nameserver 10.15.30.92
+'''
+BASE_RESOLVE = BASE_RESOLVE.strip()
+
+
+class TestResolvHelper(MockerTestCase):
+ def test_parse_same(self):
+ rp = helpers.ResolvConf(BASE_RESOLVE)
+ rp_r = str(rp).strip()
+ self.assertEquals(BASE_RESOLVE, rp_r)
+
+ def test_local_domain(self):
+ rp = helpers.ResolvConf(BASE_RESOLVE)
+ self.assertEquals(None, rp.local_domain)
+
+ rp.local_domain = "bob"
+ self.assertEquals('bob', rp.local_domain)
+ self.assertIn('domain bob', str(rp))
+
+ def test_nameservers(self):
+ rp = helpers.ResolvConf(BASE_RESOLVE)
+ self.assertIn('10.15.44.14', rp.nameservers)
+ self.assertIn('10.15.30.92', rp.nameservers)
+ rp.add_nameserver('10.2')
+ self.assertIn('10.2', rp.nameservers)
+ self.assertIn('nameserver 10.2', str(rp))
+ self.assertNotIn('10.3', rp.nameservers)
+ self.assertEquals(len(rp.nameservers), 3)
+ rp.add_nameserver('10.2')
+ with self.assertRaises(ValueError):
+ rp.add_nameserver('10.3')
+ self.assertNotIn('10.3', rp.nameservers)
+
+ def test_search_domains(self):
+ rp = helpers.ResolvConf(BASE_RESOLVE)
+ self.assertIn('yahoo.com', rp.search_domains)
+ self.assertIn('blah.yahoo.com', rp.search_domains)
+ rp.add_search_domain('bbb.y.com')
+ self.assertIn('bbb.y.com', rp.search_domains)
+ self.assertRegexpMatches(str(rp), r'search(.*)bbb.y.com(.*)')
+ self.assertIn('bbb.y.com', rp.search_domains)
+ rp.add_search_domain('bbb.y.com')
+ self.assertEquals(len(rp.search_domains), 3)
+ rp.add_search_domain('bbb2.y.com')
+ self.assertEquals(len(rp.search_domains), 4)
+ rp.add_search_domain('bbb3.y.com')
+ self.assertEquals(len(rp.search_domains), 5)
+ rp.add_search_domain('bbb4.y.com')
+ self.assertEquals(len(rp.search_domains), 6)
+ with self.assertRaises(ValueError):
+ rp.add_search_domain('bbb5.y.com')
+ self.assertEquals(len(rp.search_domains), 6)