Merge lp:~bcsaller/charm-tools/test-minimal-working-set into lp:charm-tools/1.2

Proposed by Benjamin Saller
Status: Merged
Merged at revision: 306
Proposed branch: lp:~bcsaller/charm-tools/test-minimal-working-set
Merge into: lp:charm-tools/1.2
Diff against target: 184 lines (+59/-46)
4 files modified
charmtools/test.py (+24/-20)
tests/test_juju_test.py (+32/-26)
tests_functional/charms/test/tests/00_setup (+2/-0)
tests_functional/charms/test/tests/helper.bash (+1/-0)
To merge this branch: bzr merge lp:~bcsaller/charm-tools/test-minimal-working-set
Reviewer Review Type Date Requested Status
charmers Pending
Review via email: mp+202400@code.launchpad.net

Description of the change

Don't bootstrap for non-executable tests

This makes an effort to skip tests that can't exec. It does
change the policy that tests that expect to execute must
be executable, but we don't know the interpreter w/o that
anyway.

This also moves the find_test tests from being all mocks
to using real code which is a win IMO.

https://codereview.appspot.com/54870043/

To post a comment you must log in.
Revision history for this message
Benjamin Saller (bcsaller) wrote :

Reviewers: mp+202400_code.launchpad.net,

Message:
Please take a look.

Description:
Don't bootstrap for non-executable tests

This makes an effort to skip tests that can't exec. It does
change the policy that tests that expect to execute must
be executable, but we don't know the interpreter w/o that
anyway.

This also moves the find_test tests from being all mocks
to using real code which is a win IMO.

https://code.launchpad.net/~bcsaller/charm-tools/test-minimal-working-set/+merge/202400

(do not edit description out of merge proposal)

Please review this at https://codereview.appspot.com/54870043/

Affected files (+61, -46 lines):
   A [revision details]
   M charmtools/test.py
   M tests/test_juju_test.py
   A tests_functional/charms/test/tests/00_setup
   A tests_functional/charms/test/tests/helper.bash

Revision history for this message
Marco Ceppi (marcoceppi) wrote :

On 2014/01/21 06:08:01, bcsaller wrote:
> Please take a look.

LGTM +1

https://codereview.appspot.com/54870043/

Revision history for this message
Benjamin Saller (bcsaller) wrote :

*** Submitted:

Don't bootstrap for non-executable tests

This makes an effort to skip tests that can't exec. It does
change the policy that tests that expect to execute must
be executable, but we don't know the interpreter w/o that
anyway.

This also moves the find_test tests from being all mocks
to using real code which is a win IMO.

R=Marco Ceppi
CC=
https://codereview.appspot.com/54870043

https://codereview.appspot.com/54870043/

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'charmtools/test.py'
2--- charmtools/test.py 2014-01-15 00:18:41 +0000
3+++ charmtools/test.py 2014-01-21 06:07:38 +0000
4@@ -12,6 +12,7 @@
5 import time
6 import yaml
7 from datetime import timedelta
8+from collections import OrderedDict
9 from contextlib import contextmanager
10
11 TEST_PASS = '✔'
12@@ -64,35 +65,30 @@
13 self.args = arguments
14 self.env = {'JUJU_HOME': os.path.expanduser('~/.juju')}
15 self.log = logging.getLogger('juju-test.conductor')
16- self.tests = self.args.tests or self.find_tests()
17- self.tests_supplied = bool(self.args.tests)
18+ self.tests = self.find_tests()
19+ self.tests_requested = self.args.tests
20 self.juju_version = None
21 self.juju_env = self.args.juju_env
22 self.errors = 0
23 self.fails = 0
24 self.passes = 0
25
26+ if self.tests_requested:
27+ self.tests_requested = [os.path.basename(t) for t in
28+ self.tests_requested]
29+
30 if not self.tests:
31 raise NoTests()
32
33 def run(self):
34 self.juju_version = get_juju_version()
35- available_tests = self.find_tests()
36-
37- for test in self.tests:
38- if not os.path.basename(test) in available_tests:
39- self.log.error('%s is not an available test. Skipping' % test)
40- self.errors += 1
41- continue
42-
43- if not os.path.isfile(test):
44- if not os.path.isfile(os.path.join('tests', test)):
45- self.log.error('%s was not found. Skipping' % test)
46- self.errors += 1
47- continue
48- else:
49- test = os.path.join('tests', test)
50-
51+ requested_tests = self.tests
52+ if self.tests_requested:
53+ for test in self.tests:
54+ if test not in self.tests_requested:
55+ del self.tests[test]
56+
57+ for test in requested_tests.values():
58 try:
59 self.bootstrap(self.juju_env, self.args.setup_timeout)
60 except Exception, e:
61@@ -130,9 +126,16 @@
62 return None
63
64 # Filter out only the files in tests/ then get the test names.
65- tests = [os.path.basename(t) for t in tests_dir if os.path.isfile(t)]
66+ tests = [t for t in tests_dir if os.path.isfile(t)]
67+ # only include executables
68+ tests = [(os.path.basename(t), t) for t in tests if
69+ os.access(t, os.R_OK | os.X_OK)]
70
71- return sorted(tests)
72+ result = OrderedDict()
73+ # keep sort order as well as indexed lookups
74+ for basename, test in sorted(tests):
75+ result[basename] = test
76+ return result
77
78 def safe_test_name(self, test_name):
79 return test_name
80@@ -615,6 +618,7 @@
81 # TODO: Need to add some way to specify a particular test without
82 # colliding with CHARM
83 parser.add_argument('tests', metavar="TEST", nargs='*',
84+ default=None,
85 help="tests to execute, relative to tests/ directory, \
86 default is all")
87 return parser
88
89=== modified file 'tests/test_juju_test.py'
90--- tests/test_juju_test.py 2014-01-15 00:18:41 +0000
91+++ tests/test_juju_test.py 2014-01-21 06:07:38 +0000
92@@ -4,6 +4,7 @@
93 import unittest
94 import yaml
95
96+from contextlib import contextmanager
97 from charmtools import test as juju_test
98 from mock import patch, call, Mock, MagicMock
99 from StringIO import StringIO
100@@ -30,6 +31,25 @@
101
102 PARSED_ENVIRONMENTS_YAML = yaml.safe_load(RAW_ENVIRONMENTS_YAML)
103
104+@contextmanager
105+def cd(directory):
106+ """A context manager to temporarily change current working dir, e.g.::
107+
108+ >>> import os
109+ >>> os.chdir('/tmp')
110+ >>> with cd('/bin'): print os.getcwd()
111+ /bin
112+ >>> print os.getcwd()
113+ /tmp
114+ """
115+ cwd = os.getcwd()
116+ os.chdir(directory)
117+ try:
118+ yield
119+ finally:
120+ os.chdir(cwd)
121+
122+
123
124 class Arguments(object):
125 def __init__(self, **kwargs):
126@@ -82,32 +102,18 @@
127 self.assertEqual(60, juju_test.convert_to_timedelta('1m'))
128 self.assertEqual(60 * 60, juju_test.convert_to_timedelta('1h'))
129
130- @patch('glob.glob')
131- @patch('os.path.isfile')
132- def test_conductor_find_tests(self, mock_isfile, mock_glob):
133- tests_directory = ['tests/00-test', 'tests/02-juju', 'tests/lib',
134- 'tests/01-ubuntu']
135- test_names = ['00-test', '01-ubuntu', '02-juju']
136- files_exist = [True, True, False, True]
137- mock_isfile.side_effect = files_exist
138- mock_glob.return_value = tests_directory
139-
140- args = Arguments(tests='dummy')
141- c = juju_test.Conductor(args)
142- results = c.find_tests()
143-
144- mock_glob.assert_called_with('tests/*')
145- self.assertEqual(results, test_names)
146-
147- @patch('glob.glob')
148- def test_conductor_find_tests_fails(self, mock_glob):
149- mock_glob.return_value = []
150-
151- args = Arguments(tests='dummy')
152- c = juju_test.Conductor(args)
153- results = c.find_tests()
154-
155- self.assertEqual(results, None)
156+ def test_conductor_find_tests(self):
157+ with cd('tests_functional/charms/test/'):
158+ args = Arguments(tests="dummy")
159+ c = juju_test.Conductor(args)
160+ results = c.find_tests()
161+ self.assertIn('00_setup', results)
162+ self.assertNotIn('helpers.bash', results)
163+
164+ def test_conductor_find_tests_fails(self):
165+ with cd('tests_functional/charms/mod-spdy/'):
166+ args = Arguments(tests="dummy")
167+ self.assertRaises(juju_test.NoTests, juju_test.Conductor, args)
168
169 @patch.object(juju_test.Conductor, 'find_tests')
170 def test_conductor_find_tests_exception(self, mfind_tests):
171
172=== added directory 'tests_functional/charms/test/tests'
173=== added file 'tests_functional/charms/test/tests/00_setup'
174--- tests_functional/charms/test/tests/00_setup 1970-01-01 00:00:00 +0000
175+++ tests_functional/charms/test/tests/00_setup 2014-01-21 06:07:38 +0000
176@@ -0,0 +1,2 @@
177+#!/bin/bash
178+exit 0
179
180=== added file 'tests_functional/charms/test/tests/helper.bash'
181--- tests_functional/charms/test/tests/helper.bash 1970-01-01 00:00:00 +0000
182+++ tests_functional/charms/test/tests/helper.bash 2014-01-21 06:07:38 +0000
183@@ -0,0 +1,1 @@
184+# non executable

Subscribers

People subscribed via source and target branches

to all changes: