def test_config_parent(self): parent = config.Parser() parent.update({'foo': 'bar', 'alpha': 'beta'}) parser = config.Parser(parent=parent) parser['alpha'] = 'gamma' self.assertEqual(parser.alpha, 'gamma') self.assertEqual(parser.foo, 'bar')
def test_timeout(self): model = models.Bundle({ 'directory': '', 'testdir': '', 'bundle': 'mybundle.yaml', }) class options(object): tests_yaml = None verbose = True deployment = None suite = spec.Suite(model, options) suite._config = config.Parser(**{ 'bundle_deploy': True, 'deployment_timeout': 60, }) with mock.patch('bundletester.spec.os.path.exists') as exists: def _exists(path): if path == model['bundle']: return True return os.path.exists(path) exists.side_effect = _exists self.assertEqual( suite.deploy_cmd(), ['juju-deployer', '-Wvd', '-c', model['bundle'], '-t', '60'])
def Spec(cmd, parent=None, dirname=None, suite=None, name=None): testfile = cmd if isinstance(cmd, list): testfile = find_executable(cmd[0]) if not testfile: raise OSError("Couldn't find executable for command '%s'" % cmd[0]) cmd[0] = testfile else: testfile = os.path.abspath(testfile) cmd = [testfile] if not os.path.exists(testfile) or \ not os.access(testfile, os.X_OK | os.R_OK): raise OSError('Expected executable test file: %s' % testfile) base, ext = os.path.splitext(testfile) control_file = "%s.yaml" % base if not os.path.exists(control_file): control_file = None result = config.Parser(path=control_file, parent=parent) result['name'] = name or os.path.basename(testfile) result['executable'] = cmd result['dirname'] = dirname result['suite'] = suite return result
def find_tests(self): if not self.testdir: return if self.excluded(): return testpat = self.options.test_pattern or \ self.config.get('tests', 'test*') tests = set(glob.glob(os.path.join(self.testdir, testpat))) if self.options.tests: filterset = [ os.path.join(self.testdir, f) for f in self.options.tests ] tests = tests.intersection(set(filterset)) exec_tests = [] for test in sorted(tests): if os.path.isfile(test) and os.access(test, os.X_OK | os.R_OK): exec_tests.append(test) self.spec(test, dirname=self.model['directory'], suite=self) # When a test pattern is provided (other than the default), expect # at least one executable glob match, otherwise fail. if (testpat and testpat != config.Parser().__defaults__()['tests'] and not exec_tests): raise OSError('Expected at least one executable pattern ' 'match for: {}'.format(testpat)) # When one or more test name arguments are provided, expect ALL # arguments to exist as executable files, otherwise fail. if self.options.tests and len(self.options.tests) != len(exec_tests): raise OSError('Expected executable test files: ' '{}'.format(self.options.tests))
def test_bundle_deploy_file(self): model = fake_model() class options(object): tests_yaml = None suite = spec.Suite(model, options) suite._config = config.Parser(**{'bundle_deploy': 'mydeployfile'}) with mock.patch('bundletester.spec.os.path.isfile') as isfile, \ mock.patch('bundletester.spec.os.access') as access: fullpath = os.path.join(model['testdir'], suite.config.bundle_deploy) def _isfile(path): if path == fullpath: return True return os.path.isfile(path) def _access(path, flags): if path == fullpath: return True return os.access(path, flags) isfile.side_effect = _isfile access.side_effect = _access self.assertEqual(suite.deploy_cmd(), [fullpath])
def test_builder_bootstrap_dryrun(self, mcall): parser = config.Parser() f = O() f.dryrun = True f.environment = 'local' b = builder.Builder(parser, f) b.bootstrap() self.assertFalse(mcall.called)
def test_builder_packages(self, mcall): parser = config.Parser() b = builder.Builder(parser, None) parser.packages.extend(['a', 'b']) b.install_packages() self.assertEqual( mcall.call_args, mock.call(['sudo', 'apt-get', 'install', '-qq', '-y', 'a', 'b']))
def config(self): if not self._config: testcfg = None if self.testdir: testcfg = os.path.join(self.testdir, "tests.yaml") if not os.path.exists(testcfg): testcfg = None self._config = config.Parser(testcfg, parent=self._parent_config) return self._config
def test_builder_sources(self, mcall): parser = config.Parser() b = builder.Builder(parser, None) parser.sources.append('ppa:foo') b.add_sources(False) self.assertEqual( mcall.call_args, mock.call(['sudo', 'apt-add-repository', '--yes', 'ppa:foo']))
def test_config_parse(self): parser = config.Parser(locate('sample.yaml')) self.assertTrue(parser.bootstrap) self.assertTrue(parser.reset) self.assertFalse(parser.virtualenv) self.assertEqual(parser.sources, []) self.assertEqual(parser.packages, []) self.assertEqual(parser.setup, ['setupAll']) self.assertEqual(parser.requirements, [])
def test_spec_config_parent(self): parent = config.Parser() parent['bootstrap'] = False test = spec.Spec(locate('test02'), parent) self.assertEqual(test.name, 'test02') self.assertEqual(test.executable, [os.path.abspath(locate('test02'))]) self.assertEqual(test.setup, ['setup02']) self.assertEqual(test.bootstrap, False) self.assertEqual(test.reset, True)
def test_parser_defaults(self): parser = config.Parser() self.assertTrue(parser.bootstrap) self.assertTrue(parser.reset) self.assertFalse(parser.virtualenv) self.assertEqual(parser.sources, []) self.assertEqual(parser.packages, []) self.assertEqual(parser.makefile, ['lint', 'test']) self.assertEqual(parser.setup, []) self.assertEqual(parser.requirements, [])
def test_bundle_deploy_is_false(self): model = models.Bundle({'directory': '', 'testdir': ''}) class options(object): tests_yaml = None suite = spec.Suite(model, options) suite._config = config.Parser(**{ 'bundle_deploy': False, }) self.assertIsNone(suite.deploy_cmd())
def test_bundle_deploy_is_true_juju_1(self): model = fake_model() options = FakeOptions(juju_major_version=1) suite = spec.Suite(model, options) suite._config = config.Parser(**{'bundle_deploy': True}) with mock.patch('bundletester.spec.os.path.exists') as exists: def _exists(path): if path == model['bundle']: return True return os.path.exists(path) exists.side_effect = _exists self.assertEqual(suite.deploy_cmd(), ['juju-deployer', '-Wvd', '-c', model['bundle']])
def config(self): if not self._config: testcfg = None if self.options.tests_yaml: file_path = os.path.abspath( os.path.expanduser(self.options.tests_yaml)) if os.path.exists(file_path): testcfg = file_path else: raise OSError( 'Invalid -y argument. File not found: %s' % file_path) elif self.testdir: testcfg = os.path.join(self.testdir, "tests.yaml") if not os.path.exists(testcfg): testcfg = None self._config = config.Parser(testcfg, parent=self._parent_config) return self._config
def test_timeout(self): model = fake_model() options = FakeOptions(juju_major_version=2) suite = spec.Suite(model, options) suite._config = config.Parser(**{ 'bundle_deploy': True, 'deployment_timeout': 60, }) with mock.patch('bundletester.spec.os.path.exists') as exists: def _exists(path): if path == model['bundle']: return True return os.path.exists(path) exists.side_effect = _exists self.assertEqual(suite.deploy_cmd(), ['juju', 'deploy', 'mybundle.yaml']) cmd = suite.wait_cmd() self.assertEqual(cmd, ['juju-wait', '-v', '-t', '60'])
def test_run_suite(self): logging.basicConfig(level=logging.CRITICAL) parser = config.Parser() parser.bootstrap = False options = O() options.dryrun = True options.environment = 'local' options.failfast = True options.tests_yaml=None model = models.TestDir({'name': 'testdir', 'directory': TEST_FILES, 'testdir': TEST_FILES}) suite = spec.Suite(model, options=options) suite.spec(locate('test02')) self.assertEqual(suite[0].name, 'test02') run = runner.Runner(suite, options) results = list(run()) self.assertEqual(len(results), 1) self.assertEqual(results[0]['returncode'], 0)
def config(self): if not self._config: testcfg = None if self._parent_config and self.excluded(self._parent_config): # If we're an excluded charm, ignore our tests.yaml. This # will avoid installing packages/ppas for a charm that will # not be tested. pass elif self.options.tests_yaml: file_path = os.path.abspath( os.path.expanduser(self.options.tests_yaml)) if os.path.exists(file_path): testcfg = file_path else: raise OSError('Invalid -y argument. File not found: %s' % file_path) elif self.testdir: testcfg = os.path.join(self.testdir, "tests.yaml") if not os.path.exists(testcfg): testcfg = None self._config = config.Parser(testcfg, parent=self._parent_config) return self._config
def test_merge_list_single(self): parent = config.Parser(source=[1, 2]) parser = config.Parser(source=3, parent=parent) self.assertEqual(parser.source, [1, 2, 3])
def test_merge_bool(self): parent = config.Parser(alpha=True) parser = config.Parser(alpha=False, parent=parent) self.assertFalse(parser.alpha)
def test_merge_list_list(self): parent = config.Parser(source=[1, 2]) parser = config.Parser(source=[4, 5], parent=parent) self.assertEqual(parser.source, [1, 2, 4, 5])
def test_builder_virtualenv(self, mcall): parser = config.Parser() b = builder.Builder(parser, None) b.build_virtualenv('venv') self.assertEqual(mcall.call_args[0][0], ['virtualenv', 'venv'])
def test_spec_init(self): parent = config.Parser() parent['bootstrap'] = False test = spec.Spec(locate('test02'), parent) self.assertEqual(test.name, 'test02')