Merge lp:~rvb/maas-test/fix-cache-config into lp:maas-test

Proposed by Raphaël Badin
Status: Merged
Merged at revision: 72
Proposed branch: lp:~rvb/maas-test/fix-cache-config
Merge into: lp:maas-test
Diff against target: 142 lines (+67/-3) (has conflicts)
2 files modified
maastest/proxyfixture.py (+22/-1)
maastest/tests/test_proxyfixture.py (+45/-2)
Text conflict in maastest/tests/test_proxyfixture.py
To merge this branch: bzr merge lp:~rvb/maas-test/fix-cache-config
Reviewer Review Type Date Requested Status
Gavin Panella (community) Approve
Review via email: mp+196737@code.launchpad.net

Commit message

Configure the proxy to actually cache things.

Description of the change

By default, the proxy was configured to cache things in /var/cache/polipo… which, obviously, didn't work at all since the proxy is not run as root. This branch fixes that by configuring the proxy to write its cache into '~/.maas-test/proxy_cache/'.

To post a comment you must log in.
Revision history for this message
Gavin Panella (allenap) wrote :

Looks good. A couple of things need fixing, but there's no need to block
landing once they're done.

[1]

+# TODO: '.maas-test' shouln't be harcoded like that, it should
+# be a parameter passed to LocalProxyFixture().
+DISK_CACHE_ROOT = '~/.maas-test/proxy_cache/'

Or refer to the XDG spec, and use XDG_CACHE_HOME. There's an xdg module
which you could use like so:

   from xdg.BaseDirectory import save_cache_path
   DISK_CACHE_ROOT = save_cache_path("maas-test", "proxy-cache")

See the xdg.BaseDirectory module source for more.

The packages are python-xdg and python3-xdg.

[2]

+    def create_cache_root(self):
+        cache_root = os.path.expanduser(DISK_CACHE_ROOT)
+        try:
+            os.makedirs(cache_root)
+        except OSError:
+            # Directory already exists.
+            pass

Please check for os.EEXIST:

        try:
            os.makedirs(cache_root)
        except OSError as error:
            if error.code != errno.EEXIST:
                raise

review: Approve
lp:~rvb/maas-test/fix-cache-config updated
70. By Raphaël Badin

Review fixes.

Revision history for this message
Raphaël Badin (rvb) wrote :

> Looks good. A couple of things need fixing, but there's no need to block
> landing once they're done.
>
>
> [1]
>
> +# TODO: '.maas-test' shouln't be harcoded like that, it should
> +# be a parameter passed to LocalProxyFixture().
> +DISK_CACHE_ROOT = '~/.maas-test/proxy_cache/'
>
> Or refer to the XDG spec, and use XDG_CACHE_HOME. There's an xdg module
> which you could use like so:
>
>   from xdg.BaseDirectory import save_cache_path
>   DISK_CACHE_ROOT = save_cache_path("maas-test", "proxy-cache")
>
> See the xdg.BaseDirectory module source for more.
>
> The packages are python-xdg and python3-xdg.

Not sure what the benefit would be… plus it would need another dependency. I won't change it now but if you think it's worth it and explain to me why, I'm happy to fix it later.
>
>
> [2]
>
> +    def create_cache_root(self):
> +        cache_root = os.path.expanduser(DISK_CACHE_ROOT)
> +        try:
> +            os.makedirs(cache_root)
> +        except OSError:
> +            # Directory already exists.
> +            pass
>
> Please check for os.EEXIST:
>
>        try:
>            os.makedirs(cache_root)
>        except OSError as error:
>            if error.code != errno.EEXIST:
>                raise

Arg! You're right. Fixed. (The fact that Python forces us to do that is really ugly…)

Revision history for this message
Gavin Panella (allenap) wrote :

> > [1]
> >
> > +# TODO: '.maas-test' shouln't be harcoded like that, it should
> > +# be a parameter passed to LocalProxyFixture().
> > +DISK_CACHE_ROOT = '~/.maas-test/proxy_cache/'
> >
> > Or refer to the XDG spec, and use XDG_CACHE_HOME. There's an xdg module
> > which you could use like so:
> >
> >   from xdg.BaseDirectory import save_cache_path
> >   DISK_CACHE_ROOT = save_cache_path("maas-test", "proxy-cache")
> >
> > See the xdg.BaseDirectory module source for more.
> >
> > The packages are python-xdg and python3-xdg.
>
> Not sure what the benefit would be… plus it would need another
> dependency. I won't change it now but if you think it's worth it and
> explain to me why, I'm happy to fix it later.

I think it's The Right Thing To Do, basically, in the absence of
anything better. It means that people know which directories can be
safely left out of backups, which can be purged when space gets short,
and so on. It's what I ought to have used for maas-cli.

I wouldn't worry about the dependency at all: python-xdg is almost
certain to be installed already, and it's in main. Dependencies are not
evil either. As a team we seem to be slipping into that mindset. They're
tricky now as we transition to Python 3, but the reason stuff is
packaged is so it can be used! We should depend on anything we need.

Fwiw, save_cache_path() also creates the directory, which would save you
the makedirs shenanigans.

Revision history for this message
Julian Edwards (julian-edwards) wrote :

On Tuesday 26 Nov 2013 23:02:48 you wrote:
> I wouldn't worry about the dependency at all: python-xdg is almost
> certain to be installed already, and it's in main. Dependencies are not
> evil either. As a team we seem to be slipping into that mindset. They're
> tricky now as we transition to Python 3, but the reason stuff is
> packaged is so it can be used! We should depend on anything we need.

FWIW I don't think we are slipping into that mindset, but we are looking to
reduce *unnecessary* dependencies as they create an additional maintenance and
security burden.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'maastest/proxyfixture.py'
2--- maastest/proxyfixture.py 2013-11-26 13:50:33 +0000
3+++ maastest/proxyfixture.py 2013-11-26 22:44:24 +0000
4@@ -21,9 +21,14 @@
5 from fixtures import Fixture
6 from maastest import utils
7 from netaddr import IPNetwork
8+import errno
9 import netifaces
10
11
12+# TODO: '.maas-test' shouln't be harcoded like that, it should
13+# be a parameter passed to LocalProxyFixture().
14+DISK_CACHE_ROOT = '~/.maas-test/proxy_cache/'
15+
16 PROXY_CONFIG_ITEMS = {
17 'daemonise': 'true',
18 'logFile': '%(config_dir)s/proxy.log',
19@@ -31,6 +36,7 @@
20 'proxyAddress': "::0",
21 'proxyPort': '%(port)d',
22 'allowedClients': '127.0.0.1,%(ip_address)s/%(netmask)s',
23+ 'diskCacheRoot': DISK_CACHE_ROOT
24 }
25
26 IP_ADDRESS_RE = re.compile(
27@@ -66,6 +72,8 @@
28 """
29 super(LocalProxyFixture, self).__init__()
30 self.port = port
31+ # TODO: '.maas-test' shouln't be harcoded like that, it should
32+ # be a parameter passed to LocalProxyFixture().
33 self.config_dir = os.path.join(
34 os.path.expanduser('~'), '.maas-test')
35 self.pid_file = os.path.join(self.config_dir, 'proxy.pid')
36@@ -107,6 +115,11 @@
37 def setUp(self):
38 """Configure the polipo caching http proxy."""
39 super(LocalProxyFixture, self).setUp()
40+ self.create_cache_root()
41+ self.start_proxy()
42+ self.addCleanup(self.kill_running_proxy)
43+
44+ def start_proxy(self):
45 logging.info("Checking for running proxy instance...")
46 if os.path.exists(self.pid_file):
47 raise Exception(
48@@ -115,9 +128,17 @@
49 logging.info("Starting proxy...")
50 utils.run_command(
51 ['polipo'] + self._get_config_arguments(), check_call=True)
52- self.addCleanup(self.kill_running_proxy)
53 logging.info("Done starting proxy.")
54
55+ def create_cache_root(self):
56+ cache_root = os.path.expanduser(DISK_CACHE_ROOT)
57+ try:
58+ os.makedirs(cache_root)
59+ except OSError as error:
60+ if error.errno != errno.EEXIST:
61+ raise
62+ # Directory already exists.
63+
64 def kill_running_proxy(self):
65 """Kill a running proxy."""
66 logging.info("Killing proxy...")
67
68=== modified file 'maastest/tests/test_proxyfixture.py'
69--- maastest/tests/test_proxyfixture.py 2013-11-26 17:22:35 +0000
70+++ maastest/tests/test_proxyfixture.py 2013-11-26 22:44:24 +0000
71@@ -18,8 +18,15 @@
72
73 from maastest import utils
74 from maastest.proxyfixture import LocalProxyFixture
75+from maastest import proxyfixture
76 import mock
77 import netifaces
78+<<<<<<< TREE
79+=======
80+import random
81+from fixtures import TempDir
82+from testtools.matchers import DirExists
83+>>>>>>> MERGE-SOURCE
84 import testtools
85
86
87@@ -56,17 +63,53 @@
88 "http://192.168.0.1:%s" % port,
89 proxy_fixture.get_url())
90
91- def test_set_up_runs_polipo_with_config(self):
92+ def test_set_up_calls_all_setup_methods(self):
93+ self.patch(LocalProxyFixture, 'create_cache_root', mock.MagicMock())
94+ self.patch(LocalProxyFixture, 'start_proxy', mock.MagicMock())
95+
96+ random_port = self.get_random_port_number()
97+ with LocalProxyFixture(port=random_port) as fixture:
98+ pass
99+
100+ self.assertEqual(
101+ [[mock.call()], [mock.call()]],
102+ [
103+ fixture.create_cache_root.mock_calls,
104+ fixture.start_proxy.mock_calls,
105+ ])
106+
107+ def test_start_proxy_starts_proxy(self):
108 mock_run_command = mock.MagicMock()
109 self.patch(utils, 'run_command', mock_run_command)
110 proxy_fixture = LocalProxyFixture(port=self.get_random_port_number())
111- proxy_fixture.setUp()
112+
113+ proxy_fixture.start_proxy()
114+
115 self.assertEqual(
116 mock.call(
117 ['polipo'] + proxy_fixture._get_config_arguments(),
118 check_call=True),
119 mock_run_command.mock_calls[-1])
120
121+ def test_create_cache_root_creates_directory(self):
122+ temp_dir = self.useFixture(TempDir()).path
123+ test_cache_path = temp_dir + "path"
124+ self.patch(proxyfixture, 'DISK_CACHE_ROOT', test_cache_path)
125+ proxy_fixture = LocalProxyFixture(port=self.get_random_port_number())
126+
127+ proxy_fixture.create_cache_root()
128+
129+ self.assertThat(test_cache_path, DirExists())
130+
131+ def test_create_cache_root_ignores_existing_directory(self):
132+ temp_dir = self.useFixture(TempDir()).path
133+ self.patch(proxyfixture, 'DISK_CACHE_ROOT', temp_dir)
134+ proxy_fixture = LocalProxyFixture(port=self.get_random_port_number())
135+
136+ proxy_fixture.create_cache_root()
137+
138+ self.assertThat(temp_dir, DirExists())
139+
140 def test_get_network_address(self):
141 proxy_fixture = LocalProxyFixture(port=self.get_random_port_number())
142 proxy_adddress = proxy_fixture._get_network_address()

Subscribers

People subscribed via source and target branches