1
from mocker import MockerTestCase
3
1
from cloudinit import util
5
3
from cloudinit.config import cc_apt_configure
4
from ..helpers import TestCase
11
class TestAptProxyConfig(MockerTestCase):
13
class TestAptProxyConfig(TestCase):
13
15
super(TestAptProxyConfig, self).setUp()
14
self.tmp = self.makeDir()
16
self.tmp = tempfile.mkdtemp()
17
self.addCleanup(shutil.rmtree, self.tmp)
15
18
self.pfile = os.path.join(self.tmp, "proxy.cfg")
16
19
self.cfile = os.path.join(self.tmp, "config.cfg")
18
21
def _search_apt_config(self, contents, ptype, value):
20
r"acquire::%s::proxy\s+[\"']%s[\"'];\n" % (ptype, value),
21
contents, "flags=re.IGNORECASE")
23
r"acquire::%s::proxy\s+[\"']%s[\"'];\n" % (ptype, value),
24
contents, flags=re.IGNORECASE))
23
r"acquire::%s::proxy\s+[\"']%s[\"'];\n" % (ptype, value),
24
contents, flags=re.IGNORECASE)
26
26
def test_apt_proxy_written(self):
27
27
cfg = {'apt_proxy': 'myproxy'}
30
30
self.assertTrue(os.path.isfile(self.pfile))
31
31
self.assertFalse(os.path.isfile(self.cfile))
33
contents = str(util.read_file_or_url(self.pfile))
33
contents = util.load_tfile_or_url(self.pfile)
34
34
self.assertTrue(self._search_apt_config(contents, "http", "myproxy"))
36
36
def test_apt_http_proxy_written(self):
40
40
self.assertTrue(os.path.isfile(self.pfile))
41
41
self.assertFalse(os.path.isfile(self.cfile))
43
contents = str(util.read_file_or_url(self.pfile))
43
contents = util.load_tfile_or_url(self.pfile)
44
44
self.assertTrue(self._search_apt_config(contents, "http", "myproxy"))
46
46
def test_apt_all_proxy_written(self):
58
58
self.assertTrue(os.path.isfile(self.pfile))
59
59
self.assertFalse(os.path.isfile(self.cfile))
61
contents = str(util.read_file_or_url(self.pfile))
61
contents = util.load_tfile_or_url(self.pfile)
63
for ptype, pval in values.iteritems():
63
for ptype, pval in values.items():
64
64
self.assertTrue(self._search_apt_config(contents, ptype, pval))
66
66
def test_proxy_deleted(self):
74
74
cc_apt_configure.apply_apt_config({'apt_proxy': "foo"},
75
75
self.pfile, self.cfile)
76
76
self.assertTrue(os.path.isfile(self.pfile))
77
contents = str(util.read_file_or_url(self.pfile))
77
contents = util.load_tfile_or_url(self.pfile)
78
78
self.assertTrue(self._search_apt_config(contents, "http", "foo"))
80
80
def test_config_written(self):
86
86
self.assertTrue(os.path.isfile(self.cfile))
87
87
self.assertFalse(os.path.isfile(self.pfile))
89
self.assertEqual(str(util.read_file_or_url(self.cfile)), payload)
89
self.assertEqual(util.load_tfile_or_url(self.cfile), payload)
91
91
def test_config_replaced(self):
92
92
util.write_file(self.pfile, "content doesnt matter")
93
93
cc_apt_configure.apply_apt_config({'apt_config': "foo"},
94
94
self.pfile, self.cfile)
95
95
self.assertTrue(os.path.isfile(self.cfile))
96
self.assertEqual(str(util.read_file_or_url(self.cfile)), "foo")
96
self.assertEqual(util.load_tfile_or_url(self.cfile), "foo")
98
98
def test_config_deleted(self):
99
99
# if no 'apt_config' is provided, delete any previously written file