Mercurial > piecrust2
annotate tests/conftest.py @ 397:879b6b5647a8
tests: Move all bakes/cli/servings tests files to have a YAML extension.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Mon, 18 May 2015 15:48:45 -0700 |
parents | 3e4bb57d8506 |
children | af17c143b9ab |
rev | line source |
---|---|
385
2d5f2289885a
tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
365
diff
changeset
|
1 import io |
86
1cd67680c38c
Ability to output debug logging to `stdout` when running unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
2 import sys |
347
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
3 import pprint |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
4 import os.path |
86
1cd67680c38c
Ability to output debug logging to `stdout` when running unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
5 import logging |
347
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
6 import pytest |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
7 import yaml |
385
2d5f2289885a
tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
365
diff
changeset
|
8 import colorama |
347
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
9 from piecrust.configuration import merge_dicts |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
10 from .mockutil import mock_fs, mock_fs_scope |
86
1cd67680c38c
Ability to output debug logging to `stdout` when running unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
11 |
1cd67680c38c
Ability to output debug logging to `stdout` when running unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
12 |
1cd67680c38c
Ability to output debug logging to `stdout` when running unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
13 def pytest_runtest_setup(item): |
1cd67680c38c
Ability to output debug logging to `stdout` when running unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
14 pass |
1cd67680c38c
Ability to output debug logging to `stdout` when running unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
15 |
1cd67680c38c
Ability to output debug logging to `stdout` when running unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
16 |
1cd67680c38c
Ability to output debug logging to `stdout` when running unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
17 def pytest_addoption(parser): |
347
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
18 parser.addoption( |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
19 '--log-debug', |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
20 action='store_true', |
86
1cd67680c38c
Ability to output debug logging to `stdout` when running unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
21 help="Sets the PieCrust logger to output debug info to stdout.") |
1cd67680c38c
Ability to output debug logging to `stdout` when running unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
22 |
1cd67680c38c
Ability to output debug logging to `stdout` when running unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
23 |
1cd67680c38c
Ability to output debug logging to `stdout` when running unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
24 def pytest_configure(config): |
1cd67680c38c
Ability to output debug logging to `stdout` when running unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
25 if config.getoption('--log-debug'): |
1cd67680c38c
Ability to output debug logging to `stdout` when running unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
26 hdl = logging.StreamHandler(stream=sys.stdout) |
1cd67680c38c
Ability to output debug logging to `stdout` when running unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
27 logging.getLogger('piecrust').addHandler(hdl) |
1cd67680c38c
Ability to output debug logging to `stdout` when running unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
28 logging.getLogger('piecrust').setLevel(logging.DEBUG) |
1cd67680c38c
Ability to output debug logging to `stdout` when running unit-tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
29 |
347
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
30 |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
31 def pytest_collect_file(parent, path): |
397
879b6b5647a8
tests: Move all bakes/cli/servings tests files to have a YAML extension.
Ludovic Chabant <ludovic@chabant.com>
parents:
391
diff
changeset
|
32 if path.ext == '.yaml' and path.basename.startswith("test"): |
879b6b5647a8
tests: Move all bakes/cli/servings tests files to have a YAML extension.
Ludovic Chabant <ludovic@chabant.com>
parents:
391
diff
changeset
|
33 category = os.path.basename(path.dirname) |
879b6b5647a8
tests: Move all bakes/cli/servings tests files to have a YAML extension.
Ludovic Chabant <ludovic@chabant.com>
parents:
391
diff
changeset
|
34 if category == 'bakes': |
391
3e4bb57d8506
tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents:
385
diff
changeset
|
35 return BakeTestFile(path, parent) |
397
879b6b5647a8
tests: Move all bakes/cli/servings tests files to have a YAML extension.
Ludovic Chabant <ludovic@chabant.com>
parents:
391
diff
changeset
|
36 elif category == 'cli': |
391
3e4bb57d8506
tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents:
385
diff
changeset
|
37 return ChefTestFile(path, parent) |
397
879b6b5647a8
tests: Move all bakes/cli/servings tests files to have a YAML extension.
Ludovic Chabant <ludovic@chabant.com>
parents:
391
diff
changeset
|
38 elif category == 'servings': |
391
3e4bb57d8506
tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents:
385
diff
changeset
|
39 return ServeTestFile(path, parent) |
347
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
40 |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
41 |
385
2d5f2289885a
tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
365
diff
changeset
|
42 class YamlTestFileBase(pytest.File): |
347
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
43 def collect(self): |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
44 spec = yaml.load_all(self.fspath.open()) |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
45 for i, item in enumerate(spec): |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
46 name = '%s_%d' % (self.fspath.basename, i) |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
47 if 'test_name' in item: |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
48 name += '_%s' % item['test_name'] |
385
2d5f2289885a
tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
365
diff
changeset
|
49 yield self.__item_class__(name, self, item) |
347
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
50 |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
51 |
385
2d5f2289885a
tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
365
diff
changeset
|
52 class YamlTestItemBase(pytest.Item): |
347
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
53 def __init__(self, name, parent, spec): |
385
2d5f2289885a
tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
365
diff
changeset
|
54 super(YamlTestItemBase, self).__init__(name, parent) |
347
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
55 self.spec = spec |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
56 |
385
2d5f2289885a
tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
365
diff
changeset
|
57 def _prepareMockFs(self): |
347
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
58 fs = mock_fs() |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
59 |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
60 # Website config. |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
61 config = { |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
62 'site': { |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
63 'default_format': 'none', |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
64 'default_page_layout': 'none', |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
65 'default_post_layout': 'none'} |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
66 } |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
67 test_config = self.spec.get('config') |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
68 if test_config is not None: |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
69 merge_dicts(config, test_config) |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
70 fs.withConfig(config) |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
71 |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
72 # Input file-system. |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
73 input_files = self.spec.get('in') |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
74 if input_files is not None: |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
75 _add_mock_files(fs, '/kitchen', input_files) |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
76 |
385
2d5f2289885a
tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
365
diff
changeset
|
77 return fs |
2d5f2289885a
tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
365
diff
changeset
|
78 |
2d5f2289885a
tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
365
diff
changeset
|
79 |
2d5f2289885a
tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
365
diff
changeset
|
80 class ChefTestItem(YamlTestItemBase): |
2d5f2289885a
tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
365
diff
changeset
|
81 __initialized_logging__ = False |
2d5f2289885a
tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
365
diff
changeset
|
82 |
2d5f2289885a
tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
365
diff
changeset
|
83 def runtest(self): |
2d5f2289885a
tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
365
diff
changeset
|
84 if not ChefTestItem.__initialized_logging__: |
2d5f2289885a
tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
365
diff
changeset
|
85 colorama.init() |
2d5f2289885a
tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
365
diff
changeset
|
86 hdl = logging.StreamHandler(stream=sys.stdout) |
2d5f2289885a
tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
365
diff
changeset
|
87 logging.getLogger().addHandler(hdl) |
2d5f2289885a
tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
365
diff
changeset
|
88 logging.getLogger().setLevel(logging.INFO) |
2d5f2289885a
tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
365
diff
changeset
|
89 ChefTestItem.__initialized_logging__ = True |
2d5f2289885a
tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
365
diff
changeset
|
90 |
2d5f2289885a
tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
365
diff
changeset
|
91 fs = self._prepareMockFs() |
2d5f2289885a
tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
365
diff
changeset
|
92 |
2d5f2289885a
tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
365
diff
changeset
|
93 argv = self.spec['args'] |
2d5f2289885a
tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
365
diff
changeset
|
94 if isinstance(argv, str): |
2d5f2289885a
tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
365
diff
changeset
|
95 argv = argv.split(' ') |
2d5f2289885a
tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
365
diff
changeset
|
96 |
2d5f2289885a
tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
365
diff
changeset
|
97 expected_code = self.spec.get('code', 0) |
2d5f2289885a
tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
365
diff
changeset
|
98 expected_out = self.spec.get('out', '') |
2d5f2289885a
tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
365
diff
changeset
|
99 |
2d5f2289885a
tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
365
diff
changeset
|
100 with mock_fs_scope(fs): |
2d5f2289885a
tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
365
diff
changeset
|
101 memstream = io.StringIO() |
2d5f2289885a
tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
365
diff
changeset
|
102 hdl = logging.StreamHandler(stream=memstream) |
2d5f2289885a
tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
365
diff
changeset
|
103 logging.getLogger().addHandler(hdl) |
2d5f2289885a
tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
365
diff
changeset
|
104 try: |
2d5f2289885a
tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
365
diff
changeset
|
105 from piecrust.main import PreParsedChefArgs, _run_chef |
2d5f2289885a
tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
365
diff
changeset
|
106 pre_args = PreParsedChefArgs( |
2d5f2289885a
tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
365
diff
changeset
|
107 root=fs.path('/kitchen')) |
2d5f2289885a
tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
365
diff
changeset
|
108 exit_code = _run_chef(pre_args, argv) |
2d5f2289885a
tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
365
diff
changeset
|
109 finally: |
2d5f2289885a
tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
365
diff
changeset
|
110 logging.getLogger().removeHandler(hdl) |
2d5f2289885a
tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
365
diff
changeset
|
111 |
2d5f2289885a
tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
365
diff
changeset
|
112 assert expected_code == exit_code |
2d5f2289885a
tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
365
diff
changeset
|
113 assert expected_out == memstream.getvalue() |
2d5f2289885a
tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
365
diff
changeset
|
114 |
2d5f2289885a
tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
365
diff
changeset
|
115 def reportinfo(self): |
2d5f2289885a
tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
365
diff
changeset
|
116 return self.fspath, 0, "bake: %s" % self.name |
2d5f2289885a
tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
365
diff
changeset
|
117 |
2d5f2289885a
tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
365
diff
changeset
|
118 def repr_failure(self, excinfo): |
2d5f2289885a
tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
365
diff
changeset
|
119 if isinstance(excinfo.value, ExpectedChefOutputError): |
2d5f2289885a
tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
365
diff
changeset
|
120 return ('\n'.join( |
2d5f2289885a
tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
365
diff
changeset
|
121 ['Unexpected command output. Left is expected output, ' |
2d5f2289885a
tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
365
diff
changeset
|
122 'right is actual output'] + |
2d5f2289885a
tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
365
diff
changeset
|
123 excinfo.value.args[0])) |
2d5f2289885a
tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
365
diff
changeset
|
124 return super(ChefTestItem, self).repr_failure(excinfo) |
2d5f2289885a
tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
365
diff
changeset
|
125 |
2d5f2289885a
tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
365
diff
changeset
|
126 |
2d5f2289885a
tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
365
diff
changeset
|
127 class ExpectedChefOutputError(Exception): |
2d5f2289885a
tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
365
diff
changeset
|
128 pass |
2d5f2289885a
tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
365
diff
changeset
|
129 |
2d5f2289885a
tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
365
diff
changeset
|
130 |
2d5f2289885a
tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
365
diff
changeset
|
131 class ChefTestFile(YamlTestFileBase): |
2d5f2289885a
tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
365
diff
changeset
|
132 __item_class__ = ChefTestItem |
2d5f2289885a
tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
365
diff
changeset
|
133 |
2d5f2289885a
tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
365
diff
changeset
|
134 |
2d5f2289885a
tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
365
diff
changeset
|
135 class BakeTestItem(YamlTestItemBase): |
2d5f2289885a
tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
365
diff
changeset
|
136 def runtest(self): |
2d5f2289885a
tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
365
diff
changeset
|
137 fs = self._prepareMockFs() |
2d5f2289885a
tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
365
diff
changeset
|
138 |
347
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
139 # Output file-system. |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
140 expected_output_files = self.spec.get('out') |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
141 expected_partial_files = self.spec.get('outfiles') |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
142 |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
143 # Bake! |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
144 from piecrust.baking.baker import Baker |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
145 with mock_fs_scope(fs): |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
146 out_dir = fs.path('kitchen/_counter') |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
147 app = fs.getApp() |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
148 baker = Baker(app, out_dir) |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
149 baker.bake() |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
150 |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
151 if expected_output_files: |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
152 actual = fs.getStructure('kitchen/_counter') |
351
1f22d4b10fef
tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents:
347
diff
changeset
|
153 error = _compare_dicts(expected_output_files, actual) |
347
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
154 if error: |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
155 raise ExpectedBakeOutputError(error) |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
156 |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
157 if expected_partial_files: |
351
1f22d4b10fef
tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents:
347
diff
changeset
|
158 keys = list(sorted(expected_partial_files.keys())) |
1f22d4b10fef
tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents:
347
diff
changeset
|
159 for key in keys: |
347
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
160 try: |
351
1f22d4b10fef
tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents:
347
diff
changeset
|
161 actual = fs.getFileEntry('kitchen/_counter/' + |
347
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
162 key.lstrip('/')) |
351
1f22d4b10fef
tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents:
347
diff
changeset
|
163 except Exception as e: |
347
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
164 raise ExpectedBakeOutputError([ |
351
1f22d4b10fef
tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents:
347
diff
changeset
|
165 "Can't access output file %s: %s" % (key, e)]) |
1f22d4b10fef
tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents:
347
diff
changeset
|
166 |
1f22d4b10fef
tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents:
347
diff
changeset
|
167 expected = expected_partial_files[key] |
1f22d4b10fef
tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents:
347
diff
changeset
|
168 # HACK because for some reason PyYAML adds a new line for those |
1f22d4b10fef
tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents:
347
diff
changeset
|
169 # and I have no idea why. |
365
a9929e0b8f66
tests: Changes to output report and hack for comparing outputs.
Ludovic Chabant <ludovic@chabant.com>
parents:
351
diff
changeset
|
170 actual = actual.rstrip('\n') |
351
1f22d4b10fef
tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents:
347
diff
changeset
|
171 expected = expected.rstrip('\n') |
1f22d4b10fef
tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents:
347
diff
changeset
|
172 cmpres = _compare_str(expected, actual, key) |
1f22d4b10fef
tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents:
347
diff
changeset
|
173 if cmpres: |
1f22d4b10fef
tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents:
347
diff
changeset
|
174 raise ExpectedBakeOutputError(cmpres) |
347
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
175 |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
176 def reportinfo(self): |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
177 return self.fspath, 0, "bake: %s" % self.name |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
178 |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
179 def repr_failure(self, excinfo): |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
180 if isinstance(excinfo.value, ExpectedBakeOutputError): |
351
1f22d4b10fef
tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents:
347
diff
changeset
|
181 return ('\n'.join( |
1f22d4b10fef
tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents:
347
diff
changeset
|
182 ['Unexpected bake output. Left is expected output, ' |
1f22d4b10fef
tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents:
347
diff
changeset
|
183 'right is actual output'] + |
1f22d4b10fef
tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents:
347
diff
changeset
|
184 excinfo.value.args[0])) |
347
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
185 return super(BakeTestItem, self).repr_failure(excinfo) |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
186 |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
187 |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
188 class ExpectedBakeOutputError(Exception): |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
189 pass |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
190 |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
191 |
385
2d5f2289885a
tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
365
diff
changeset
|
192 class BakeTestFile(YamlTestFileBase): |
2d5f2289885a
tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
365
diff
changeset
|
193 __item_class__ = BakeTestItem |
2d5f2289885a
tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
365
diff
changeset
|
194 |
2d5f2289885a
tests: Add support for "Chef tests", which are direct CLI tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
365
diff
changeset
|
195 |
391
3e4bb57d8506
tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents:
385
diff
changeset
|
196 class ServeTestItem(YamlTestItemBase): |
3e4bb57d8506
tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents:
385
diff
changeset
|
197 class _TestApp(object): |
3e4bb57d8506
tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents:
385
diff
changeset
|
198 def __init__(self, server): |
3e4bb57d8506
tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents:
385
diff
changeset
|
199 self.server = server |
3e4bb57d8506
tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents:
385
diff
changeset
|
200 |
3e4bb57d8506
tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents:
385
diff
changeset
|
201 def __call__(self, environ, start_response): |
3e4bb57d8506
tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents:
385
diff
changeset
|
202 return self.server._try_run_request(environ, start_response) |
3e4bb57d8506
tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents:
385
diff
changeset
|
203 |
3e4bb57d8506
tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents:
385
diff
changeset
|
204 def runtest(self): |
3e4bb57d8506
tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents:
385
diff
changeset
|
205 fs = self._prepareMockFs() |
3e4bb57d8506
tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents:
385
diff
changeset
|
206 |
3e4bb57d8506
tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents:
385
diff
changeset
|
207 url = self.spec.get('url') |
3e4bb57d8506
tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents:
385
diff
changeset
|
208 if url is None: |
3e4bb57d8506
tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents:
385
diff
changeset
|
209 raise Exception("Missing URL in test spec.") |
3e4bb57d8506
tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents:
385
diff
changeset
|
210 |
3e4bb57d8506
tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents:
385
diff
changeset
|
211 expected_status = self.spec.get('status', 200) |
3e4bb57d8506
tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents:
385
diff
changeset
|
212 expected_headers = self.spec.get('headers') |
3e4bb57d8506
tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents:
385
diff
changeset
|
213 expected_output = self.spec.get('out') |
3e4bb57d8506
tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents:
385
diff
changeset
|
214 expected_contains = self.spec.get('out_contains') |
3e4bb57d8506
tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents:
385
diff
changeset
|
215 |
3e4bb57d8506
tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents:
385
diff
changeset
|
216 from werkzeug.test import Client |
3e4bb57d8506
tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents:
385
diff
changeset
|
217 from werkzeug.wrappers import BaseResponse |
3e4bb57d8506
tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents:
385
diff
changeset
|
218 from piecrust.serving.server import Server |
3e4bb57d8506
tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents:
385
diff
changeset
|
219 with mock_fs_scope(fs): |
3e4bb57d8506
tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents:
385
diff
changeset
|
220 server = Server(fs.path('/kitchen')) |
3e4bb57d8506
tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents:
385
diff
changeset
|
221 test_app = self._TestApp(server) |
3e4bb57d8506
tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents:
385
diff
changeset
|
222 client = Client(test_app, BaseResponse) |
3e4bb57d8506
tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents:
385
diff
changeset
|
223 resp = client.get(url) |
3e4bb57d8506
tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents:
385
diff
changeset
|
224 assert expected_status == resp.status_code |
3e4bb57d8506
tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents:
385
diff
changeset
|
225 |
3e4bb57d8506
tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents:
385
diff
changeset
|
226 if expected_headers: |
3e4bb57d8506
tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents:
385
diff
changeset
|
227 for k, v in expected_headers.items(): |
3e4bb57d8506
tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents:
385
diff
changeset
|
228 assert v == resp.headers.get(k) |
3e4bb57d8506
tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents:
385
diff
changeset
|
229 |
3e4bb57d8506
tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents:
385
diff
changeset
|
230 actual = resp.data.decode('utf8').rstrip() |
3e4bb57d8506
tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents:
385
diff
changeset
|
231 if expected_output: |
3e4bb57d8506
tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents:
385
diff
changeset
|
232 assert expected_output.rstrip() == actual |
3e4bb57d8506
tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents:
385
diff
changeset
|
233 |
3e4bb57d8506
tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents:
385
diff
changeset
|
234 if expected_contains: |
3e4bb57d8506
tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents:
385
diff
changeset
|
235 assert expected_contains.rstrip() in actual |
3e4bb57d8506
tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents:
385
diff
changeset
|
236 |
3e4bb57d8506
tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents:
385
diff
changeset
|
237 def reportinfo(self): |
3e4bb57d8506
tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents:
385
diff
changeset
|
238 return self.fspath, 0, "serve: %s" % self.name |
3e4bb57d8506
tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents:
385
diff
changeset
|
239 |
3e4bb57d8506
tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents:
385
diff
changeset
|
240 |
3e4bb57d8506
tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents:
385
diff
changeset
|
241 class ServeTestFile(YamlTestFileBase): |
3e4bb57d8506
tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents:
385
diff
changeset
|
242 __item_class__ = ServeTestItem |
3e4bb57d8506
tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents:
385
diff
changeset
|
243 |
3e4bb57d8506
tests: Add support for testing the Chef server.
Ludovic Chabant <ludovic@chabant.com>
parents:
385
diff
changeset
|
244 |
347
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
245 def _add_mock_files(fs, parent_path, spec): |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
246 for name, subspec in spec.items(): |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
247 path = os.path.join(parent_path, name) |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
248 if isinstance(subspec, str): |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
249 fs.withFile(path, subspec) |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
250 elif isinstance(subspec, dict): |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
251 _add_mock_files(fs, path, subspec) |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
252 |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
253 |
351
1f22d4b10fef
tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents:
347
diff
changeset
|
254 def _compare(left, right, path): |
1f22d4b10fef
tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents:
347
diff
changeset
|
255 if type(left) != type(right): |
1f22d4b10fef
tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents:
347
diff
changeset
|
256 return (["Different items: ", |
1f22d4b10fef
tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents:
347
diff
changeset
|
257 "%s: %s" % (path, pprint.pformat(left)), |
1f22d4b10fef
tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents:
347
diff
changeset
|
258 "%s: %s" % (path, pprint.pformat(right))]) |
1f22d4b10fef
tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents:
347
diff
changeset
|
259 if isinstance(left, str): |
1f22d4b10fef
tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents:
347
diff
changeset
|
260 return _compare_str(left, right, path) |
1f22d4b10fef
tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents:
347
diff
changeset
|
261 elif isinstance(left, dict): |
1f22d4b10fef
tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents:
347
diff
changeset
|
262 return _compare_dicts(left, right, path) |
1f22d4b10fef
tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents:
347
diff
changeset
|
263 elif isinstance(left, list): |
1f22d4b10fef
tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents:
347
diff
changeset
|
264 return _compare_lists(left, right, path) |
1f22d4b10fef
tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents:
347
diff
changeset
|
265 elif left != right: |
1f22d4b10fef
tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents:
347
diff
changeset
|
266 return (["Different items: ", |
1f22d4b10fef
tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents:
347
diff
changeset
|
267 "%s: %s" % (path, pprint.pformat(left)), |
1f22d4b10fef
tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents:
347
diff
changeset
|
268 "%s: %s" % (path, pprint.pformat(right))]) |
1f22d4b10fef
tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents:
347
diff
changeset
|
269 |
1f22d4b10fef
tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents:
347
diff
changeset
|
270 |
347
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
271 def _compare_dicts(left, right, basepath=''): |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
272 key_diff = set(left.keys()) ^ set(right.keys()) |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
273 if key_diff: |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
274 extra_left = set(left.keys()) - set(right.keys()) |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
275 if extra_left: |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
276 return (["Left contains more items: "] + |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
277 ['- %s/%s' % (basepath, k) for k in extra_left]) |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
278 extra_right = set(right.keys()) - set(left.keys()) |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
279 if extra_right: |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
280 return (["Right contains more items: "] + |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
281 ['- %s/%s' % (basepath, k) for k in extra_right]) |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
282 return ["Unknown difference"] |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
283 |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
284 for key in left.keys(): |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
285 lv = left[key] |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
286 rv = right[key] |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
287 childpath = basepath + '/' + key |
351
1f22d4b10fef
tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents:
347
diff
changeset
|
288 cmpres = _compare(lv, rv, childpath) |
1f22d4b10fef
tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents:
347
diff
changeset
|
289 if cmpres: |
1f22d4b10fef
tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents:
347
diff
changeset
|
290 return cmpres |
1f22d4b10fef
tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents:
347
diff
changeset
|
291 return None |
1f22d4b10fef
tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents:
347
diff
changeset
|
292 |
347
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
293 |
351
1f22d4b10fef
tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents:
347
diff
changeset
|
294 def _compare_lists(left, right, path): |
1f22d4b10fef
tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents:
347
diff
changeset
|
295 for i in range(min(len(left), len(right))): |
1f22d4b10fef
tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents:
347
diff
changeset
|
296 l = left[i] |
1f22d4b10fef
tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents:
347
diff
changeset
|
297 r = right[i] |
1f22d4b10fef
tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents:
347
diff
changeset
|
298 cmpres = _compare(l, r, path) |
1f22d4b10fef
tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents:
347
diff
changeset
|
299 if cmpres: |
1f22d4b10fef
tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents:
347
diff
changeset
|
300 return cmpres |
1f22d4b10fef
tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents:
347
diff
changeset
|
301 if len(left) > len(right): |
1f22d4b10fef
tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents:
347
diff
changeset
|
302 return (["Left '%s' contains more items. First extra item: " % path, |
1f22d4b10fef
tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents:
347
diff
changeset
|
303 left[len(right)]]) |
1f22d4b10fef
tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents:
347
diff
changeset
|
304 if len(right) > len(left): |
1f22d4b10fef
tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents:
347
diff
changeset
|
305 return (["Right '%s' contains more items. First extra item: " % path, |
1f22d4b10fef
tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents:
347
diff
changeset
|
306 right[len(left)]]) |
347
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
307 return None |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
308 |
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
309 |
351
1f22d4b10fef
tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents:
347
diff
changeset
|
310 def _compare_str(left, right, path): |
1f22d4b10fef
tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents:
347
diff
changeset
|
311 if left == right: |
1f22d4b10fef
tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents:
347
diff
changeset
|
312 return None |
347
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
313 for i in range(min(len(left), len(right))): |
351
1f22d4b10fef
tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents:
347
diff
changeset
|
314 if left[i] != right[i]: |
365
a9929e0b8f66
tests: Changes to output report and hack for comparing outputs.
Ludovic Chabant <ludovic@chabant.com>
parents:
351
diff
changeset
|
315 start = max(0, i - 15) |
a9929e0b8f66
tests: Changes to output report and hack for comparing outputs.
Ludovic Chabant <ludovic@chabant.com>
parents:
351
diff
changeset
|
316 marker_offset = min(15, (i - start)) + 3 |
a9929e0b8f66
tests: Changes to output report and hack for comparing outputs.
Ludovic Chabant <ludovic@chabant.com>
parents:
351
diff
changeset
|
317 |
a9929e0b8f66
tests: Changes to output report and hack for comparing outputs.
Ludovic Chabant <ludovic@chabant.com>
parents:
351
diff
changeset
|
318 lend = min(len(left), i + 15) |
a9929e0b8f66
tests: Changes to output report and hack for comparing outputs.
Ludovic Chabant <ludovic@chabant.com>
parents:
351
diff
changeset
|
319 rend = min(len(right), i + 15) |
a9929e0b8f66
tests: Changes to output report and hack for comparing outputs.
Ludovic Chabant <ludovic@chabant.com>
parents:
351
diff
changeset
|
320 |
a9929e0b8f66
tests: Changes to output report and hack for comparing outputs.
Ludovic Chabant <ludovic@chabant.com>
parents:
351
diff
changeset
|
321 return ["Items '%s' differ at index %d:" % (path, i), '', |
a9929e0b8f66
tests: Changes to output report and hack for comparing outputs.
Ludovic Chabant <ludovic@chabant.com>
parents:
351
diff
changeset
|
322 "Left:", left, '', |
a9929e0b8f66
tests: Changes to output report and hack for comparing outputs.
Ludovic Chabant <ludovic@chabant.com>
parents:
351
diff
changeset
|
323 "Right:", right, '', |
a9929e0b8f66
tests: Changes to output report and hack for comparing outputs.
Ludovic Chabant <ludovic@chabant.com>
parents:
351
diff
changeset
|
324 "Difference:", |
a9929e0b8f66
tests: Changes to output report and hack for comparing outputs.
Ludovic Chabant <ludovic@chabant.com>
parents:
351
diff
changeset
|
325 repr(left[start:lend]), |
a9929e0b8f66
tests: Changes to output report and hack for comparing outputs.
Ludovic Chabant <ludovic@chabant.com>
parents:
351
diff
changeset
|
326 (' ' * marker_offset + '^'), |
a9929e0b8f66
tests: Changes to output report and hack for comparing outputs.
Ludovic Chabant <ludovic@chabant.com>
parents:
351
diff
changeset
|
327 repr(right[start:rend]), |
a9929e0b8f66
tests: Changes to output report and hack for comparing outputs.
Ludovic Chabant <ludovic@chabant.com>
parents:
351
diff
changeset
|
328 (' ' * marker_offset + '^')] |
351
1f22d4b10fef
tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents:
347
diff
changeset
|
329 if len(left) > len(right): |
1f22d4b10fef
tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents:
347
diff
changeset
|
330 return ["Left is longer.", |
1f22d4b10fef
tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents:
347
diff
changeset
|
331 "Left '%s': " % path, left, |
1f22d4b10fef
tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents:
347
diff
changeset
|
332 "Right '%s': " % path, right, |
1f22d4b10fef
tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents:
347
diff
changeset
|
333 "Extra items: %r" % left[len(right):]] |
1f22d4b10fef
tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents:
347
diff
changeset
|
334 if len(right) > len(left): |
1f22d4b10fef
tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents:
347
diff
changeset
|
335 return ["Right is longer.", |
1f22d4b10fef
tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents:
347
diff
changeset
|
336 "Left '%s': " % path, left, |
1f22d4b10fef
tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents:
347
diff
changeset
|
337 "Right '%s': " % path, right, |
1f22d4b10fef
tests: Improve bake tests output, add support for partial output checks.
Ludovic Chabant <ludovic@chabant.com>
parents:
347
diff
changeset
|
338 "Extra items: %r" % right[len(left):]] |
347
76c838453dbe
tests: Support for YAML-based baking tests. Convert old code-based ones.
Ludovic Chabant <ludovic@chabant.com>
parents:
86
diff
changeset
|
339 |