Merge lp:~mandel/ubuntuone-dev-tools/fix-squid-tests into lp:ubuntuone-dev-tools

Proposed by Manuel de la Peña
Status: Merged
Approved by: Manuel de la Peña
Approved revision: 76
Merged at revision: 70
Proposed branch: lp:~mandel/ubuntuone-dev-tools/fix-squid-tests
Merge into: lp:ubuntuone-dev-tools
Diff against target: 509 lines (+266/-33)
9 files modified
data/squid.conf.in (+5/-5)
run-tests (+2/-2)
run-tests.bat (+3/-1)
setup.py (+17/-9)
ubuntuone/devtools/services/squid.py (+54/-9)
ubuntuone/devtools/services/tests/test_squid.py (+9/-6)
ubuntuone/devtools/services/tests/test_squid_linux.py (+67/-0)
ubuntuone/devtools/services/tests/test_squid_windows.py (+108/-0)
ubuntuone/devtools/testcases/tests/test_squid_testcase.py (+1/-1)
To merge this branch: bzr merge lp:~mandel/ubuntuone-dev-tools/fix-squid-tests
Reviewer Review Type Date Requested Status
dobey (community) Approve
Eric Casteleijn (community) Approve
Review via email: mp+103325@code.launchpad.net

Commit message

- Ensured that the ncsa_auth.exe can be found on windows (LP: #987213).
- Formatted the path correctly so that the squid config can read them (LP: #987225).
- Use win32api to kill the squid process (LP: #987256)
- Use dirspec to place the data files correctly on windows (LP: #988228).

Description of the change

- Ensured that the ncsa_auth.exe can be found on windows (LP: #987213).
- Formatted the path correctly so that the squid config can read them (LP: #987225).
- Use win32api to kill the squid process (LP: #987256)
- Use dirspec to place the data files correctly on windows (LP: #988228).

In order to tests the branch you have to follow these steps:

1- Download and copy to C:\squid the windows squid binaries from here: http://squid.acmeconsulting.it/
2- Downlaod the apacache msi and install it: http://httpd.apache.org/download.cgi
3- Ensure the following is in your path: C:\squid\sbin;C:\squid\libexec;C:\Program Files\Apache Software Foundation\Apache2.2\bin

All the above will allow you to run the proxy tests on windows.

Note: The path to the apache bins might differ depending on your system.

To post a comment you must log in.
Revision history for this message
Eric Casteleijn (thisfred) wrote :

98 +if sys.platform == 'win32':
99 + from ubuntuone.devtools.services.squid import windows
100 + source = windows
101 +else:
102 + from ubuntuone.devtools.services.squid import linux
103 + source = linux

why not import windows as source, and import linux as source?

Revision history for this message
Eric Casteleijn (thisfred) wrote :

Also, what does "source" mean in this context?

Revision history for this message
Eric Casteleijn (thisfred) wrote :

I would suggest something like "squid_util" or "squid_helpers" instead of source.

review: Needs Information
Revision history for this message
Eric Casteleijn (thisfred) wrote :

Looks good otherwise, use your own judgement on the above...

review: Approve
Revision history for this message
dobey (dobey) wrote :

+# Do use doble \ because squids config needs \ to be escaped
+ """Return the path for the formated for the config."""

Some spelling and grammar fixes need to happen. :)

Also, why do we need to split the Linux and Windows pieces up like this? We need to find a way to run all the tests on all platforms, and just skip the ones that are platform-specific on other platforms.

review: Needs Fixing
Revision history for this message
Manuel de la Peña (mandel) wrote :

> +# Do use doble \ because squids config needs \ to be escaped
> + """Return the path for the formated for the config."""
>
> Some spelling and grammar fixes need to happen. :)

Fixing!

>
> Also, why do we need to split the Linux and Windows pieces up like this? We
> need to find a way to run all the tests on all platforms, and just skip the
> ones that are platform-specific on other platforms.

Well this is not splitting the tests according to the platform but working around the fact that squid on windows is not a good citizen. The following is a list of the workarounds we have to do to ensure that we can run all squid tests on windows:

* os.kill does not work on windows, we need to use the win api or we will leave the proxy running.
* The squid config is stupid, it needs paths to be C:\\path\\to\\cache rather than C:\path\to\cache, if you don't do that squid wont start.
* The location of the ncsa_auth is diff.

With that fixed we are able to run all tests as long as squid and htpasswd are present in the path.

Revision history for this message
Manuel de la Peña (mandel) wrote :

> I would suggest something like "squid_util" or "squid_helpers" instead of
> source.

Fixing!

Revision history for this message
dobey (dobey) :
review: Approve
Revision history for this message
Ubuntu One Auto Pilot (otto-pilot) wrote :

Attempt to merge into lp:ubuntuone-dev-tools failed due to conflicts:

text conflict in run-tests.bat

76. By Manuel de la Peña

Fixed conflicts issues with trunk.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'data/squid.conf.in'
--- data/squid.conf.in 2012-01-05 16:42:37 +0000
+++ data/squid.conf.in 2012-04-30 11:07:20 +0000
@@ -80,19 +80,19 @@
80cache_dir ufs ${spool_temp} 1000 16 25680cache_dir ufs ${spool_temp} 1000 16 256
8181
82# access log settings82# access log settings
83access_log ${squid_temp}/access.log squid83access_log ${squid_temp}access.log squid
8484
85# cache log settings85# cache log settings
86cache_log ${squid_temp}/cache.log86cache_log ${squid_temp}cache.log
8787
88# cache store log settings88# cache store log settings
89cache_store_log ${squid_temp}/store.log89cache_store_log ${squid_temp}store.log
9090
91# mime table conf91# mime table conf
92# mime_table /usr/share/squid/mime.conf92# mime_table /usr/share/squid/mime.conf
9393
94#Default pid file name94#Default pid file name
95pid_filename ${squid_temp}/squid.pid95pid_filename ${squid_temp}squid.pid
9696
97# debug options (Full debugging)97# debug options (Full debugging)
98debug_options ALL,198debug_options ALL,1
@@ -117,4 +117,4 @@
117hosts_file /etc/hosts117hosts_file /etc/hosts
118118
119# Leave coredumps in the first cache dir119# Leave coredumps in the first cache dir
120coredump_dir ${spool_temp}/squid120coredump_dir ${spool_temp}squid
121121
=== modified file 'run-tests'
--- run-tests 2012-01-12 12:08:22 +0000
+++ run-tests 2012-04-30 11:07:20 +0000
@@ -16,8 +16,8 @@
16# with this program. If not, see <http://www.gnu.org/licenses/>.16# with this program. If not, see <http://www.gnu.org/licenses/>.
17set -e17set -e
1818
19bin/u1trial -c ubuntuone19bin/u1trial -i "test_squid_windows.py" -c ubuntuone
20bin/u1trial --reactor=twisted ubuntuone20bin/u1trial --reactor=twisted -i "test_squid_windows.py" ubuntuone
21echo "Running style checks..."21echo "Running style checks..."
22bin/u1lint22bin/u1lint
23pep8 --repeat . bin/* --exclude=*.bat23pep8 --repeat . bin/* --exclude=*.bat
2424
=== modified file 'run-tests.bat'
--- run-tests.bat 2012-04-25 12:33:48 +0000
+++ run-tests.bat 2012-04-30 11:07:20 +0000
@@ -28,6 +28,8 @@
28:: files in the program, then also delete it here.28:: files in the program, then also delete it here.
29@ECHO off29@ECHO off
3030
31SET IGNORED="test_squid_linux.py"
32
31ECHO Checking for Python on the path33ECHO Checking for Python on the path
32:: Look for Python from buildout34:: Look for Python from buildout
33FOR %%A in (python.exe) do (SET PYTHONEXEPATH=%%~$PATH:A)35FOR %%A in (python.exe) do (SET PYTHONEXEPATH=%%~$PATH:A)
@@ -67,7 +69,7 @@
6769
68ECHO Python found at %PYTHONEXEPATH%, executing the tests...70ECHO Python found at %PYTHONEXEPATH%, executing the tests...
69:: execute the tests with a number of ignored linux only modules71:: execute the tests with a number of ignored linux only modules
70"%PYTHONEXEPATH%" "%TRIALPATH%" --reactor=twisted -c %PARAMS% ubuntuone72"%PYTHONEXEPATH%" bin/u1trial --reactor=twisted -i %IGNORED% -c %PARAMS% ubuntuone
7173
72IF %SKIPLINT% == 1 (74IF %SKIPLINT% == 1 (
73 ECHO Skipping style checks75 ECHO Skipping style checks
7476
=== modified file 'setup.py'
--- setup.py 2012-03-30 17:44:03 +0000
+++ setup.py 2012-04-30 11:07:20 +0000
@@ -28,10 +28,12 @@
28# files in the program, then also delete it here.28# files in the program, then also delete it here.
29"""setup.py"""29"""setup.py"""
3030
31import os
31import subprocess32import subprocess
32import sys33import sys
3334
34from distutils.core import setup, Command35from distutils.core import setup, Command
36from dirspec import basedir
3537
36PACKAGE = 'ubuntuone-dev-tools'38PACKAGE = 'ubuntuone-dev-tools'
37VERSION = '3.1'39VERSION = '3.1'
@@ -65,6 +67,20 @@
65if sys.platform == 'win32':67if sys.platform == 'win32':
66 # lets add the .bat so that windows users are happy68 # lets add the .bat so that windows users are happy
67 scripts.extend(['bin/u1lint.bat', 'bin/u1trial.bat'])69 scripts.extend(['bin/u1lint.bat', 'bin/u1trial.bat'])
70 DATA_FILES = [(os.path.join(basedir.default_data_path, PACKAGE),
71 ['pylintrc',
72 'data/dbus-session.conf.in',
73 'data/squid.conf.in']),
74 ]
75else:
76 DATA_FILES = [('share/%s' % PACKAGE,
77 ['pylintrc',
78 'data/dbus-session.conf.in',
79 'data/squid.conf.in']),
80 ('share/man/man1',
81 ['man/u1lint.1',
82 'man/u1trial.1']),
83 ]
6884
69setup(name=PACKAGE,85setup(name=PACKAGE,
70 version=VERSION,86 version=VERSION,
@@ -78,15 +94,7 @@
78 'ubuntuone.devtools.testcases'],94 'ubuntuone.devtools.testcases'],
79 extra_path='ubuntuone-dev-tools',95 extra_path='ubuntuone-dev-tools',
80 scripts=scripts,96 scripts=scripts,
81 data_files=[('share/%s' % PACKAGE,97 data_files=DATA_FILES,
82 ['pylintrc',
83 'data/dbus-session.conf.in',
84 'data/squid.conf.in']),
85 ('share/man/man1',
86 ['man/u1lint.1',
87 'man/u1trial.1']),
88 ],
89
90 cmdclass={98 cmdclass={
91 'lint': Lint,99 'lint': Lint,
92 },100 },
93101
=== modified file 'ubuntuone/devtools/services/squid.py'
--- ubuntuone/devtools/services/squid.py 2012-04-20 17:00:51 +0000
+++ ubuntuone/devtools/services/squid.py 2012-04-30 11:07:20 +0000
@@ -33,10 +33,11 @@
33import string33import string
34# pylint:enable=W040234# pylint:enable=W0402
35import subprocess35import subprocess
36import sys
36import time37import time
3738
38from json import dumps, loads39from json import dumps, loads
39from os import environ, makedirs, kill, unlink40from os import environ, kill, makedirs, unlink
40from os.path import abspath, exists, join41from os.path import abspath, exists, join
4142
42from distutils.spawn import find_executable43from distutils.spawn import find_executable
@@ -46,24 +47,52 @@
46 get_arbitrary_port,47 get_arbitrary_port,
47)48)
4849
50# pylint: disable=C0103
51if sys.platform == 'win32':
52 AUTH_PROCESS_PATH = r'C:\squid\libexec\ncsa_auth.exe'
53 SQUID_START_ARGS = ['-f']
54else:
55 AUTH_PROCESS_PATH = '/usr/lib/%s/ncsa_auth'
56 SQUID_START_ARGS = ['-N', '-X', '-f']
57# pylint: enable=C0103
58
49SQUID_CONFIG_FILE = 'squid.conf.in'59SQUID_CONFIG_FILE = 'squid.conf.in'
50SQUID_DIR = 'squid'60SQUID_DIR = 'squid'
51AUTH_PROCESS_PATH = '/usr/lib/%s/ncsa_auth'
52SPOOL_DIR = 'spool'61SPOOL_DIR = 'spool'
53AUTH_FILE = 'htpasswd'62AUTH_FILE = 'htpasswd'
54PROXY_ENV_VAR = 'SQUID_PROXY_SETTINGS'63PROXY_ENV_VAR = 'SQUID_PROXY_SETTINGS'
5564
5665
66def format_config_path(path):
67 """Return the path correctly formatted for the config file."""
68 # squid cannot handle correctly paths with a single \
69 return path.replace('\\', '\\\\')
70
71
72def get_auth_process_path(squid_version):
73 """Return the path to the auth executable."""
74 if sys.platform == 'win32':
75 path = find_executable('ncsa_auth')
76 if path is None:
77 path = AUTH_PROCESS_PATH
78 return format_config_path(path)
79 else:
80 squid = 'squid3' if squid_version == 3 else 'squid'
81 path = AUTH_PROCESS_PATH % squid
82 return path
83
84
57def get_squid_executable():85def get_squid_executable():
58 """Return the squid executable of the system."""86 """Return the squid executable of the system."""
59 # try with squid and if not present try with squid3 for newer systems87 # try with squid and if not present try with squid3 for newer systems
60 # (Ubuntu P). We also return the path to the auth process so that we can88 # (Ubuntu P). We also return the path to the auth process so that we can
61 # point to the correct one.89 # point to the correct one.
62 squid = find_executable('squid3')90 squid = find_executable('squid3')
63 auth_process = AUTH_PROCESS_PATH % 'squid3'91 version = 3
64 if squid is None:92 if squid is None:
93 version = 2
65 squid = find_executable('squid')94 squid = find_executable('squid')
66 auth_process = AUTH_PROCESS_PATH % 'squid'95 auth_process = get_auth_process_path(version)
67 return squid, auth_process96 return squid, auth_process
6897
6998
@@ -72,6 +101,21 @@
72 return find_executable('htpasswd')101 return find_executable('htpasswd')
73102
74103
104def kill_squid(squid_pid):
105 """Kill the squid process."""
106 if sys.platform == 'win32':
107 # pylint: disable=F0401
108 import win32api
109 import win32con
110 # pylint: enable=F0401
111
112 handle = win32api.OpenProcess(win32con.PROCESS_TERMINATE, 0, squid_pid)
113 win32api.TerminateProcess(handle, 0)
114 win32api.CloseHandle(handle)
115 else:
116 kill(squid_pid, signal.SIGKILL)
117
118
75def _make_random_string(count):119def _make_random_string(count):
76 """Make a random string of the given length."""120 """Make a random string of the given length."""
77 entropy = random.SystemRandom()121 entropy = random.SystemRandom()
@@ -95,7 +139,7 @@
95 path = abspath(path)139 path = abspath(path)
96 if not exists(path):140 if not exists(path):
97 makedirs(path)141 makedirs(path)
98 return path142 return format_config_path(path)
99143
100144
101def _get_squid_temp_path(tempdir=''):145def _get_squid_temp_path(tempdir=''):
@@ -105,7 +149,7 @@
105 path = abspath(path)149 path = abspath(path)
106 if not exists(path):150 if not exists(path):
107 makedirs(path)151 makedirs(path)
108 return path152 return format_config_path(join(path, ''))
109153
110154
111def _get_auth_temp_path(tempdir=''):155def _get_auth_temp_path(tempdir=''):
@@ -114,7 +158,7 @@
114 auth_file = join(basedir, AUTH_FILE)158 auth_file = join(basedir, AUTH_FILE)
115 if not exists(basedir):159 if not exists(basedir):
116 makedirs(basedir)160 makedirs(basedir)
117 return auth_file161 return format_config_path(auth_file)
118162
119163
120def store_proxy_settings(settings):164def store_proxy_settings(settings):
@@ -238,7 +282,8 @@
238 self._generate_auth_file(tempdir)282 self._generate_auth_file(tempdir)
239 self._generate_config_file(tempdir)283 self._generate_config_file(tempdir)
240 self._generate_swap(self.config_file)284 self._generate_swap(self.config_file)
241 squid_args = ['-N', '-X', '-f', self.config_file]285 squid_args = SQUID_START_ARGS
286 squid_args.append(self.config_file)
242 sp = subprocess.Popen([self.squid] + squid_args,287 sp = subprocess.Popen([self.squid] + squid_args,
243 stdout=subprocess.PIPE,288 stdout=subprocess.PIPE,
244 stderr=subprocess.PIPE)289 stderr=subprocess.PIPE)
@@ -255,7 +300,7 @@
255300
256 def stop_service(self):301 def stop_service(self):
257 """Stop our proxy,"""302 """Stop our proxy,"""
258 kill(self.squid_pid, signal.SIGKILL)303 kill_squid(self.squid_pid)
259 delete_proxy_settings()304 delete_proxy_settings()
260 self.running = False305 self.running = False
261 unlink(self.config_file)306 unlink(self.config_file)
262307
=== modified file 'ubuntuone/devtools/services/tests/test_squid.py'
--- ubuntuone/devtools/services/tests/test_squid.py 2012-04-20 16:42:54 +0000
+++ ubuntuone/devtools/services/tests/test_squid.py 2012-04-30 11:07:20 +0000
@@ -72,6 +72,7 @@
72 self.patch(squid, '_get_basedir', fake_basedir_fn)72 self.patch(squid, '_get_basedir', fake_basedir_fn)
73 self.patch(squid, 'makedirs', fake_makedirs)73 self.patch(squid, 'makedirs', fake_makedirs)
74 self.patch(squid, 'exists', fake_exists)74 self.patch(squid, 'exists', fake_exists)
75 self.patch(squid, 'format_config_path', lambda path: path)
7576
76 def test_get_basedir_missing(self):77 def test_get_basedir_missing(self):
77 """Test the base dir creation."""78 """Test the base dir creation."""
@@ -116,24 +117,26 @@
116117
117 def test_get_squid_temp_path_missing(self):118 def test_get_squid_temp_path_missing(self):
118 """Test the squid path creation."""119 """Test the squid path creation."""
119 expected_path = os.path.join(self.basedir, squid.SQUID_DIR)120 expected_path = os.path.join(self.basedir, squid.SQUID_DIR, '')
121 abspath = os.path.abspath(expected_path)
120 result = squid._get_squid_temp_path()122 result = squid._get_squid_temp_path()
121 self.assertEqual(expected_path, result)123 self.assertEqual(expected_path, result)
122 self.assertTrue(('fake_basedir_fn', '') in self.called)124 self.assertTrue(('fake_basedir_fn', '') in self.called)
123 self.assertTrue(('fake_makedirs', expected_path) in self.called)125 self.assertTrue(('fake_makedirs', abspath) in self.called)
124 self.assertTrue(expected_path in self.created_paths)126 self.assertTrue(abspath in self.created_paths)
125 self.assertTrue(('fake_exists', expected_path) in self.called)127 self.assertTrue(('fake_exists', abspath) in self.called)
126128
127 def test_get_squid_temp_path_present(self):129 def test_get_squid_temp_path_present(self):
128 """Test the squid path creation."""130 """Test the squid path creation."""
129 self.path_exists = True131 self.path_exists = True
130 expected_path = os.path.join(self.basedir, squid.SQUID_DIR)132 expected_path = os.path.join(self.basedir, squid.SQUID_DIR, '')
131 result = squid._get_squid_temp_path()133 result = squid._get_squid_temp_path()
132 self.assertEqual(expected_path, result)134 self.assertEqual(expected_path, result)
133 self.assertTrue(('fake_basedir_fn', '') in self.called)135 self.assertTrue(('fake_basedir_fn', '') in self.called)
134 self.assertTrue(('fake_makedirs', expected_path) not in self.called)136 self.assertTrue(('fake_makedirs', expected_path) not in self.called)
135 self.assertTrue(expected_path not in self.created_paths)137 self.assertTrue(expected_path not in self.created_paths)
136 self.assertTrue(('fake_exists', expected_path) in self.called)138 self.assertTrue(('fake_exists',
139 os.path.abspath(expected_path)) in self.called)
137140
138 def test_get_auth_temp_path(self):141 def test_get_auth_temp_path(self):
139 """Test the creation of the auth path."""142 """Test the creation of the auth path."""
140143
=== added file 'ubuntuone/devtools/services/tests/test_squid_linux.py'
--- ubuntuone/devtools/services/tests/test_squid_linux.py 1970-01-01 00:00:00 +0000
+++ ubuntuone/devtools/services/tests/test_squid_linux.py 2012-04-30 11:07:20 +0000
@@ -0,0 +1,67 @@
1#
2# Copyright 2012 Canonical Ltd.
3#
4# This program is free software: you can redistribute it and/or modify it
5# under the terms of the GNU General Public License version 3, as published
6# by the Free Software Foundation.
7#
8# This program is distributed in the hope that it will be useful, but
9# WITHOUT ANY WARRANTY; without even the implied warranties of
10# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11# PURPOSE. See the GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License along
14# with this program. If not, see <http://www.gnu.org/licenses/>.
15#
16# In addition, as a special exception, the copyright holders give
17# permission to link the code of portions of this program with the
18# OpenSSL library under certain conditions as described in each
19# individual source file, and distribute linked combinations
20# including the two.
21# You must obey the GNU General Public License in all respects
22# for all of the code used other than OpenSSL. If you modify
23# file(s) with this exception, you may extend this exception to your
24# version of the file(s), but you are not obligated to do so. If you
25# do not wish to do so, delete this exception statement from your
26# version. If you delete this exception statement from all source
27# files in the program, then also delete it here.
28"""Tests for the linux squid bits."""
29
30import signal
31
32from twisted.trial.unittest import TestCase
33
34from ubuntuone.devtools.services import squid
35
36
37class SquidLinuxTestCase(TestCase):
38 """"Test the different linux bits."""
39
40 def test_get_auth_process_3(self):
41 """Test getting the auth process for squid3."""
42 expected = squid.AUTH_PROCESS_PATH % 'squid3'
43 self.assertEqual(expected, squid.get_auth_process_path(3))
44
45 def test_get_auth_process(self):
46 """Test getting the auth process."""
47 expected = squid.AUTH_PROCESS_PATH % 'squid'
48 self.assertEqual(expected, squid.get_auth_process_path(2))
49
50 def test_format_config_path(self):
51 """Test formating a config path."""
52 path = '/a/config/path'
53 self.assertEqual(path, squid.format_config_path(path))
54
55 def test_kill_squid(self):
56 """Test killing squid."""
57 called = []
58
59 def fake_kill(pid, kill_signal):
60 """Fake os.kill."""
61 called.append(('kill', pid, kill_signal))
62
63 self.patch(squid, 'kill', fake_kill)
64
65 squid_pid = 4
66 squid.kill_squid(squid_pid)
67 self.assertIn(('kill', squid_pid, signal.SIGKILL), called)
068
=== added file 'ubuntuone/devtools/services/tests/test_squid_windows.py'
--- ubuntuone/devtools/services/tests/test_squid_windows.py 1970-01-01 00:00:00 +0000
+++ ubuntuone/devtools/services/tests/test_squid_windows.py 2012-04-30 11:07:20 +0000
@@ -0,0 +1,108 @@
1#
2# Copyright 2012 Canonical Ltd.
3#
4# This program is free software: you can redistribute it and/or modify it
5# under the terms of the GNU General Public License version 3, as published
6# by the Free Software Foundation.
7#
8# This program is distributed in the hope that it will be useful, but
9# WITHOUT ANY WARRANTY; without even the implied warranties of
10# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11# PURPOSE. See the GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License along
14# with this program. If not, see <http://www.gnu.org/licenses/>.
15#
16# In addition, as a special exception, the copyright holders give
17# permission to link the code of portions of this program with the
18# OpenSSL library under certain conditions as described in each
19# individual source file, and distribute linked combinations
20# including the two.
21# You must obey the GNU General Public License in all respects
22# for all of the code used other than OpenSSL. If you modify
23# file(s) with this exception, you may extend this exception to your
24# version of the file(s), but you are not obligated to do so. If you
25# do not wish to do so, delete this exception statement from your
26# version. If you delete this exception statement from all source
27# files in the program, then also delete it here.
28"""Tests for the windows squid bits."""
29
30# pylint: disable=F0401
31import win32api
32import win32con
33# pylint: enable=F0401
34
35from twisted.trial.unittest import TestCase
36
37from ubuntuone.devtools.services import squid
38
39
40class SquidWindowsTestCase(TestCase):
41 """"Test the different windows bits."""
42
43 def test_get_auth_process(self):
44 """Test getting the auth process for squid3."""
45 called = []
46
47 self.patch(squid, 'find_executable', lambda _: None)
48
49 def fake_format(path):
50 """Fake format of a config path."""
51 called.append(('format', path))
52 return path
53
54 self.patch(squid, 'format_config_path', fake_format)
55 self.assertEqual(squid.AUTH_PROCESS_PATH,
56 squid.get_auth_process_path(3))
57 self.assertIn(('format', squid.AUTH_PROCESS_PATH), called)
58
59 def test_get_auth_process_path(self):
60 """Test getting the auth process."""
61 called = []
62
63 exec_path = '/path/to/exec'
64 self.patch(squid, 'find_executable', lambda _: exec_path)
65
66 def fake_format(path):
67 """Fake format of a config path."""
68 called.append(('format', path))
69 return path
70
71 self.patch(squid, 'format_config_path', fake_format)
72 self.assertEqual(exec_path, squid.get_auth_process_path(3))
73 self.assertIn(('format', exec_path), called)
74
75 def test_format_config_path(self):
76 """Test formating a config path."""
77 path = '\\a\\config\\path'
78 expected = path.replace('\\', '\\\\')
79 self.assertEqual(expected, squid.format_config_path(path))
80
81 def test_kill_squid(self):
82 """Test killing squid."""
83 called = []
84
85 def fake_open_process(access, inherit, pid):
86 """A fake open process."""
87 called.append(('OpenProcess', access, inherit, pid))
88 return pid
89
90 self.patch(win32api, 'OpenProcess', fake_open_process)
91
92 def fake_terminate(handle, exit_code):
93 """Fake terminate the process."""
94 called.append(('TerminateProcess', handle, exit_code))
95
96 self.patch(win32api, 'TerminateProcess', fake_terminate)
97
98 def fake_close_handle(handle):
99 """Fale closing a handle."""
100 called.append(('CloseHandle', handle))
101
102 self.patch(win32api, 'CloseHandle', fake_close_handle)
103 squid_pid = 4
104 squid.kill_squid(squid_pid)
105 self.assertIn(('OpenProcess', win32con.PROCESS_TERMINATE, 0,
106 squid_pid), called)
107 self.assertIn(('TerminateProcess', squid_pid, 0), called)
108 self.assertIn(('CloseHandle', squid_pid), called)
0109
=== modified file 'ubuntuone/devtools/testcases/tests/test_squid_testcase.py'
--- ubuntuone/devtools/testcases/tests/test_squid_testcase.py 2012-03-30 17:44:03 +0000
+++ ubuntuone/devtools/testcases/tests/test_squid_testcase.py 2012-04-30 11:07:20 +0000
@@ -192,7 +192,7 @@
192 """Build the url for this mock server."""192 """Build the url for this mock server."""
193 #pylint: disable=W0212193 #pylint: disable=W0212
194 port_num = self.tcpserver._port.getHost().port194 port_num = self.tcpserver._port.getHost().port
195 return "http://localhost:%d/" % port_num195 return "http://127.0.0.1:%d/" % port_num
196196
197 @defer.inlineCallbacks197 @defer.inlineCallbacks
198 def stop(self):198 def stop(self):

Subscribers

People subscribed via source and target branches

to all changes: