def test_download(self): content = "hello" key = KeyStub("test", "hello") with temp_dir() as d: dst = os.path.join(d, "test.txt") _download(key, dst) self.assertEqual(content, get_file_content(dst))
def test_go_test_package(self): with temp_dir() as gopath: package_path = os.path.join( gopath, 'src', 'github.com', 'juju', 'juju') os.makedirs(package_path) with patch('gotesttarfile.run', return_value=0, autospec=True) as run_mock: devnull = open(os.devnull, 'w') with patch('sys.stdout', devnull): returncode = go_test_package( 'github.com/juju/juju', 'go', gopath) self.assertEqual(0, returncode) self.assertEqual(run_mock.call_count, 5) args, kwargs = run_mock.call_args_list[0] self.assertEqual((['go', 'version'],), args) args, kwargs = run_mock.call_args_list[1] self.assertEqual((['go', 'env'],), args) args, kwargs = run_mock.call_args_list[2] self.assertEqual((['go', 'test', '-i', './...'],), args) args, kwargs = run_mock.call_args_list[3] self.assertEqual( (['go', 'test', '-timeout=1200s', './...'],), args) self.assertEqual('amd64', kwargs['env'].get('GOARCH')) self.assertEqual(gopath, kwargs['env'].get('GOPATH')) run_mock.assert_called_with(['sudo', 'killall', '-SIGABRT', 'mongod'])
def test_download_files__suffix(self): keys = [KeyStub("test.txt", "foo"), KeyStub("test2.tar", "foo2")] with temp_dir() as dst: downloaded_files = download_files(keys, dst, suffix=".tar") self.assertItemsEqual(os.listdir(dst), ['test2.tar']) self.assertEqual("foo2", get_file_content('test2.tar', dst)) self.assertItemsEqual(downloaded_files, ['test2.tar'])
def test_main(self): with temp_dir() as tempdir: buildvars_path = os.path.join(tempdir, 'buildvars') with open(buildvars_path, 'w') as buildvars_file: json.dump({'foo': 'bar'}, buildvars_file) output_path = os.path.join(tempdir, 'output') envs = {'environments': {'foo': {'type': 'ec2'}}} with _temp_env(envs): with patch('subprocess.check_output', return_value='1.20'): main([buildvars_path, 'foo', output_path]) with open(output_path) as output_file: output = output_file.read() expected = dedent("""\ { "environment": { "name": "foo",\x20 "substrate": "AWS" },\x20 "new_client": { "buildvars": { "foo": "bar" },\x20 "type": "build" },\x20 "old_client": { "type": "release",\x20 "version": "1.20" } } """) self.assertEqual(output, expected)
def iter_steps(self, client): yield self.prepare.as_result() with temp_dir() as temp_repository: charm = Charm('mycharm', 'Test charm', series=['trusty']) charm.metadata['description'] = 'Charm for industrial testing.' charm_root = charm.to_repo_dir(temp_repository) charm_path = local_charm_path( charm='mycharm', juju_ver=client.version, series='trusty', repository=os.path.dirname(charm_root)) client.deploy(charm_path, temp_repository) yield self.prepare.as_result() client.wait_for_started() yield self.prepare.as_result() charm.add_hook_script('config-changed', dedent("""\ #!/bin/sh open-port 34 """)) charm.add_hook_script('upgrade-charm', dedent("""\ #!/bin/sh open-port 42 """)) shutil.rmtree(charm_root) charm.to_repo_dir(temp_repository) yield self.prepare.as_result(True) yield self.upgrade.as_result() client.upgrade_charm('mycharm', charm_root) yield self.upgrade.as_result() for status in client.status_until(300): ports = status.get_open_ports('mycharm/0') if '42/tcp' in ports and '34/tcp' in ports: break else: raise Exception('42 and/or 34 not opened.') yield self.upgrade.as_result(True)
def test_assess_multi_series_charms_juju1x(self): mock_client = Mock( spec=[ "deploy", "get_juju_output", "wait_for_started", "is_juju1x"]) mock_client.version = '1.25.5' mock_client.is_juju1x.return_value = True mock_client.get_juju_output.return_value = "Codename: trusty" mock_client.deploy.return_value = None with temp_dir() as charm_dir: with patch('assess_multi_series_charms.temp_dir', autospec=True) as td_mock: td_mock.return_value.__enter__.return_value = charm_dir with patch('assess_multi_series_charms.check_series', autospec=True) as cs_mock: assess_multi_series_charms(mock_client, 'angsty') self.assertEqual(mock_client.wait_for_started.call_count, 2) charm = os.path.join('local:trusty', 'dummy') calls = [ call(charm=charm, force=False, repository=charm_dir, series=None, service='test1'), call(charm=charm, force=False, repository=charm_dir, series='trusty', service='test2') ] self.assertEqual(mock_client.deploy.mock_calls, calls) td_mock.assert_called_once_with() cs_calls = [ call(mock_client, machine='0', series=None), call(mock_client, machine='1', series='trusty')] self.assertEqual(cs_mock.mock_calls, cs_calls)
def test_make_metadata(self): with temp_dir() as tempdir: buildvars_path = os.path.join(tempdir, 'buildvars') with open(buildvars_path, 'w') as buildvars_file: json.dump({'foo': 'bar'}, buildvars_file) with patch('subprocess.check_output', return_value='1.20'): envs = {'environments': {'foobar': {'type': 'local'}}} with _temp_env(envs): metadata = make_metadata(buildvars_path, 'foobar') self.assertEqual( metadata, { 'new_client': { 'buildvars': { 'foo': 'bar' }, 'type': 'build', }, 'old_client': { 'type': 'release', 'version': '1.20', }, 'environment': { 'name': 'foobar', 'substrate': 'LXC (local)', } })
def main(argv=None): """Run go test against the content of a tarfile.""" returncode = 0 args = parse_args(argv) tarfile_path = args.tarfile tarfile_name = os.path.basename(tarfile_path) version = tarfile_name.split('_')[-1].replace('.tar.gz', '') try: if args.verbose: print_now('Testing juju %s from %s' % (version, tarfile_name)) with temp_dir() as workspace: gopath = os.path.join(workspace, 'gogo') untar_gopath(tarfile_path, gopath, delete=args.remove_tarfile, verbose=args.verbose) returncode = go_test_package(args.package, args.go, gopath, verbose=args.verbose) except Exception as e: print_now(str(e)) print_now(traceback.print_exc()) return 3 return returncode
def test_parse_args_provisioning_config(self): with temp_dir() as log_dir: args = parse_args([ 'provisioning', 'foo', 'bar', 'baz', log_dir, 'qux', '--config', 'quxx' ]) self.assertEqual('quxx', args.config)
def test_parse_args_provisioning_machine_series(self): with temp_dir() as log_dir: args = parse_args([ 'provisioning', 'foo', 'bar', 'baz', log_dir, 'qux', '--machine-series', 'quxx', '--machine-series', 'quxxx' ]) self.assertEqual(args.machine_series, ['quxx', 'quxxx'])
def test_parse_args_provisioning(self): with temp_dir() as log_dir: args = parse_args( ['provisioning', 'foo', 'bar', 'baz', log_dir, 'qux']) self.assertEqual( args, Namespace( agent_stream=None, agent_url=None, bootstrap_host=None, cloud='bar', clouds_file='foo', deadline=None, debug=False, juju_bin='baz', keep_env=False, logs=log_dir, machine=[], region=None, series=None, temp_env_name='qux', upload_tools=False, verbose=logging.INFO, test='provisioning', config=None, machine_series=None, to=None, ))
def test_parse_args_kill_controller_config(self): with temp_dir() as log_dir: args = parse_args([ 'kill-controller', 'foo', 'bar', 'baz', log_dir, 'qux', '--config', 'quxx' ]) self.assertEqual('quxx', args.config)
def test_parse_args_combined_config(self): with temp_dir() as log_dir: args = parse_args([ 'combined', 'foo', 'bar', 'baz', log_dir, 'qux', '--config', 'quxx' ]) self.assertEqual('quxx', args.config)
def test_encoded_copy_to_dir_many(self): output = "test one|K0ktLuECAA==\r\ntest two|K0ktLuECAA==\r\n\r\n" with temp_dir() as dest: WinRmRemote._encoded_copy_to_dir(dest, output) for name in ("test one", "test two"): with open(os.path.join(dest, name)) as f: self.assertEqual(f.read(), "test\n")
def assess_deploy_storage(client, charm_series, charm_name, provider_type, pool=None): """Set up the test for deploying charm with storage.""" if provider_type == "block": storage = { "disks": { "type": provider_type, "multiple": { "range": "0-10" } } } else: storage = {"data": {"type": provider_type, "location": "/srv/data"}} with temp_dir() as charm_dir: charm_root = create_storage_charm(charm_dir, charm_name, 'Test charm for storage', storage) platform = 'ubuntu' charm = local_charm_path(charm=charm_name, juju_ver=client.version, series=charm_series, repository=os.path.dirname(charm_root), platform=platform) deploy_storage(client, charm, charm_series, pool, "1G", charm_dir)
def test_main(self): with temp_dir() as log_dir: argv = ["an-env", "/bin/juju", log_dir, "an-env-mod", "both-proxied", "--verbose"] client = Mock(spec=["is_jes_enabled"]) with patch("assess_proxy.configure_logging", autospec=True) as mock_cl: with patch("assess_proxy.BootstrapManager.booted_context", autospec=True) as mock_bc: with patch('deploy_stack.client_from_config', return_value=client) as mock_cfc: with patch("assess_proxy.assess_proxy", autospec=True) as mock_assess: with patch("assess_proxy.check_network", autospec=True, return_value='FORWARD') as mock_check: with patch("assess_proxy.set_firewall", autospec=True) as mock_set: with patch("assess_proxy.reset_firewall", autospec=True) as mock_reset: returecode = assess_proxy.main(argv) self.assertEqual(0, returecode) mock_cl.assert_called_once_with(logging.DEBUG) mock_cfc.assert_called_once_with( 'an-env', "/bin/juju", debug=False, soft_deadline=None) mock_check.assert_called_once_with('eth0', 'lxdbr0') mock_set.assert_called_once_with( 'both-proxied', 'eth0', 'lxdbr0', 'FORWARD') mock_reset.assert_called_once_with() self.assertEqual(mock_bc.call_count, 1) mock_assess.assert_called_once_with(client, 'both-proxied')
def assess_deploy_storage(client, charm_series, charm_name, provider_type, pool=None): """Set up the test for deploying charm with storage.""" if provider_type == "block": storage = { "disks": { "type": provider_type, "multiple": { "range": "0-10" } } } else: storage = { "data": { "type": provider_type, "location": "/srv/data" } } with temp_dir() as charm_dir: charm_root = create_storage_charm(charm_dir, charm_name, 'Test charm for storage', storage) platform = 'ubuntu' charm = local_charm_path(charm=charm_name, juju_ver=client.version, series=charm_series, repository=os.path.dirname(charm_root), platform=platform) deploy_storage(client, charm, charm_series, pool, "1G", charm_dir)
def test_build_jobs(self): credentials = Credentials('jrandom', 'password1') jobs = [] for os_str in ('ubuntu', 'osx', 'windows'): jobs.append({ 'old_version': "1.18.4", 'candidate': "1.24.5", 'new_to_old': 'true', 'candidate_path': "1.24.5", 'client_os': os_str, 'revision_build': "2999" }) calls = [ call('compatibility-control', {'candidate_path': '1.24.5', 'candidate': '1.24.5', 'new_to_old': 'true', 'revision_build': '2999', 'old_version': '1.18.4', 'client_os': 'ubuntu'}), call('compatibility-control-osx', {'candidate_path': '1.24.5', 'candidate': '1.24.5', 'new_to_old': 'true', 'revision_build': '2999', 'old_version': '1.18.4', 'client_os': 'osx'}), call('compatibility-control-windows', {'candidate_path': '1.24.5', 'candidate': '1.24.5', 'new_to_old': 'true', 'revision_build': '2999', 'old_version': '1.18.4', 'client_os': 'windows'})] with temp_dir() as root: write_config(root, 'compatibility-control', 'asdf') with patch('schedule_hetero_control.Jenkins', autospec=True) as jenkins_mock: build_jobs(credentials, root, jobs) jenkins_mock.assert_called_once_with( 'http://juju-ci.vapour.ws:8080', 'jrandom', 'password1') self.assertEqual( jenkins_mock.return_value.build_job.call_args_list, calls)
def assess_user_permission_model_migrations(source_client, dest_client): """Run migration tests for user permissions.""" with temp_dir() as temp: ensure_migrating_with_insufficient_user_permissions_fails( source_client, dest_client, temp) ensure_migrating_with_superuser_user_permissions_succeeds( source_client, dest_client, temp)
def test_random_assert_run_command_method_called(self): with patch('utility.check_output', autospec=True): with patch('runner.Runner._run_command', autospec=True) as cm_mock: with temp_dir() as directory: runner = Runner(directory, ChaosMonkey.factory()) runner.random_chaos(run_timeout=1, enablement_timeout=1) cm_mock.assert_called_with(runner, 1)
def test_write_openstack_config_file_writes_credentials_file(self): """Ensure the file created contains the correct details.""" credential_details = dict( os_tenant_name='tenant_name', os_password='******', os_auth_url='url', os_region_name='region') user = '******' with temp_dir() as tmp_dir: credentials_file = aac.write_openstack_config_file( tmp_dir, user, credential_details) with open(credentials_file, 'r') as f: credential_contents = f.read() expected = dedent("""\ export OS_USERNAME={user} export OS_PASSWORD={password} export OS_TENANT_NAME={tenant_name} export OS_AUTH_URL={auth_url} export OS_REGION_NAME={region} """.format( user=user, password=credential_details['os_password'], tenant_name=credential_details['os_tenant_name'], auth_url=credential_details['os_auth_url'], region=credential_details['os_region_name'], )) self.assertEqual(credential_contents, expected)
def test_prepare_dir_create_new(self): with temp_dir() as base_dir: os.makedirs(os.path.join(base_dir, 'candidate')) candidate_dir_path = os.path.join(base_dir, 'candidate', 'master') prepare_dir(candidate_dir_path) self.assertTrue(os.path.isdir(candidate_dir_path)) self.assertEqual([], os.listdir(candidate_dir_path))
def test_calls_provided_test(self): client = fake_juju_client() with temp_dir() as juju_home: client.env.juju_home = juju_home bs_manager = make_bootstrap_manager(client) bs_manager.log_dir = os.path.join(juju_home, 'log-dir') os.mkdir(bs_manager.log_dir) timing = gpr.TimingData(datetime.utcnow(), datetime.utcnow()) deploy_details = gpr.DeployDetails('test', dict(), timing) noop_test = Mock(return_value=deploy_details) pprof_collector = Mock() with patch.object(gpr, 'dump_performance_metrics_logs', autospec=True): with patch.object(gpr, 'generate_reports', autospec=True): with patch.object( gpr, 'PPROFCollector', autospec=True) as p_pc: p_pc.return_value = pprof_collector gpr.run_perfscale_test( noop_test, bs_manager, get_default_args()) noop_test.assert_called_once_with( client, pprof_collector, get_default_args())
def test_verbose_default_values(self): env = 'env' juju_bin = '/bin/juju' temp_env_name = 'functional-autoload-credentials' with temp_dir() as log: args = aac.parse_args([env, juju_bin, log, temp_env_name]) self.assertEqual( args, Namespace( agent_stream=None, agent_url=None, bootstrap_host=None, debug=False, deadline=None, env='env', juju_bin='/bin/juju', keep_env=False, logs=log, machine=[], region=None, series=None, to=None, temp_env_name='functional-autoload-credentials', upload_tools=False, verbose=logging.INFO, ))
def test_random_enablement_zero(self): with patch('utility.check_output', autospec=True) as mock: with temp_dir() as directory: runner = Runner(directory, ChaosMonkey.factory()) runner.random_chaos(run_timeout=1, enablement_timeout=0, exclude_command=Kill.restart_cmd) self.assertEqual(mock.called, True)
def test_write_openstack_config_file_writes_credentials_file(self): """Ensure the file created contains the correct details.""" credential_details = dict(os_tenant_name='tenant_name', os_password='******', os_auth_url='url', os_region_name='region') user = '******' with temp_dir() as tmp_dir: credentials_file = aac.write_openstack_config_file( tmp_dir, user, credential_details) with open(credentials_file, 'r') as f: credential_contents = f.read() expected = dedent("""\ export OS_USERNAME={user} export OS_PASSWORD={password} export OS_TENANT_NAME={tenant_name} export OS_AUTH_URL={auth_url} export OS_REGION_NAME={region} """.format( user=user, password=credential_details['os_password'], tenant_name=credential_details['os_tenant_name'], auth_url=credential_details['os_auth_url'], region=credential_details['os_region_name'], )) self.assertEqual(credential_contents, expected)
def test_calls_provided_test(self): client = fake_juju_client() with temp_dir() as juju_home: client.env.juju_home = juju_home bs_manager = make_bootstrap_manager(client) bs_manager.log_dir = os.path.join(juju_home, 'log-dir') os.mkdir(bs_manager.log_dir) timing = gpr.TimingData(datetime.utcnow(), datetime.utcnow()) deploy_details = gpr.DeployDetails('test', dict(), timing) noop_test = Mock(return_value=deploy_details) pprof_collector = Mock() with patch.object(gpr, 'dump_performance_metrics_logs', autospec=True): with patch.object(gpr, 'generate_reports', autospec=True): with patch.object(gpr, 'PPROFCollector', autospec=True) as p_pc: p_pc.return_value = pprof_collector gpr.run_perfscale_test(noop_test, bs_manager, get_default_args()) noop_test.assert_called_once_with(client, pprof_collector, get_default_args())
def test_main_expected_arguments(self): os.environ['HOME'] = '/home/fake-juju-user' with temp_dir() as juju_home: os.environ['JUJU_HOME'] = juju_home temp_file = NamedTemporaryFile(delete=False) with patch('run_download_juju.get_revisions', autospec=True) as gr_mock: with patch('run_download_juju.create_workspace_yaml', autospec=True) as cwy_mock: with patch('run_download_juju.NamedTemporaryFile', autospec=True, return_value=temp_file) as ntf_mock: with patch('subprocess.check_output') as sco_mock: main([]) sco_calls = [ call(['workspace-run', temp_file.name, '*****@*****.**']), call(['workspace-run', temp_file.name, '*****@*****.**'])] cwy_calls = [ call(juju_home, ('C:\\\\Users\\\\Administrator\\\\juju-ci-tools' '\\\\download_juju.py'), temp_file, gr_mock.return_value), call(juju_home, '$HOME/juju-ci-tools/download_juju.py', temp_file, gr_mock.return_value)] self.assertEqual(sco_mock.call_args_list, sco_calls) ntf_mock.assert_called_once_with() gr_mock.assert_called_once_with(os.environ['HOME']) self.assertEqual(cwy_mock.call_args_list, cwy_calls)
def setup_extract_candidates(self, dry_run=False, verbose=False): version = '1.2.3' with temp_dir() as base_dir: artifacts_dir_path = os.path.join(base_dir, 'master-artifacts') os.makedirs(artifacts_dir_path) buildvars_path = os.path.join(artifacts_dir_path, 'buildvars.json') with open(buildvars_path, 'w') as bv: json.dump(dict(version=version), bv) os.utime(buildvars_path, (1426186437, 1426186437)) master_dir_path = os.path.join(base_dir, version) os.makedirs(master_dir_path) package_path = os.path.join(master_dir_path, 'foo.deb') with patch('candidate.prepare_dir') as pd_mock: with patch('candidate.get_package', return_value=package_path) as gp_mock: with patch('subprocess.check_call') as cc_mock: extract_candidates( base_dir, dry_run=dry_run, verbose=verbose) copied_path = os.path.join(master_dir_path, 'buildvars.json') if not dry_run: self.assertTrue(os.path.isfile(copied_path)) self.assertEqual(os.stat(copied_path).st_mtime, 1426186437) else: self.assertFalse(os.path.isfile(copied_path)) return (pd_mock, gp_mock, cc_mock, artifacts_dir_path, buildvars_path, master_dir_path, package_path)
def test_filter_command_include_command(self): include_command = 'deny-all' with temp_dir() as directory: runner = Runner(directory, ChaosMonkey.factory()) runner.filter_commands(include_command=include_command) self.assertEqual(len(runner.chaos_monkey.chaos), 1) self.assertEqual(runner.chaos_monkey.chaos[0].command_str, 'deny-all')
def test_acquire_lock_fails_when_existing_lockfile(self): with temp_dir() as directory: expected_file = os.path.join(directory, 'chaos_runner.lock') open(expected_file, 'a').close() runner = Runner(directory, None) with self.assertRaises(SystemExit): runner.acquire_lock()
def test_acquire_lock_fails_without_workspace(self): with temp_dir() as directory: runner = Runner(directory, None) # Call runner.acquire_lock at this level, at which point directory # will have already been cleaned up. with self.assertRaises(SystemExit): runner.acquire_lock()
def test_filter_commands_exclude_incorrect_group(self): exclude_group = 'net,killl' with temp_dir() as directory: runner = Runner(directory, ChaosMonkey.factory()) with self.assertRaisesRegexp( BadRequest, "Invalid value given on command line: killl"): runner.filter_commands(exclude_group=exclude_group)
def test_filter_commands_exclude_group(self): exclude_group = 'net' with temp_dir() as directory: runner = Runner(directory, ChaosMonkey.factory()) runner.filter_commands(exclude_group=exclude_group) self.assertGreaterEqual(len(runner.chaos_monkey.chaos), 2) self.assertTrue(all(c.group != 'net' for c in runner.chaos_monkey.chaos))
def test_init_systems_with_local_cache(self): with temp_dir() as workspace: index_path = make_local_cache(workspace) lxc_cache = LxcCache(workspace) systems, data = lxc_cache.init_systems(index_path) expected_systems = make_systems() self.assertEqual(expected_systems, systems) self.assertEqual(INDEX_DATA, data)
def test_filter_command_exclude_incorrect_command(self): exclude_command = 'deny-all,deny-net,{}'.format(Kill.jujud_cmd) with temp_dir() as directory: runner = Runner(directory, ChaosMonkey.factory()) with self.assertRaisesRegexp( BadRequest, "Invalid value given on command line: deny-net"): runner.filter_commands(exclude_command=exclude_command)
def test_filter_command_include_incorrect_command(self): include_command = 'deny-all,deny-net' with temp_dir() as directory: runner = Runner(directory, ChaosMonkey.factory()) with self.assertRaisesRegexp( BadRequest, "Invalid value given on command line: deny-net"): runner.filter_commands(include_command=include_command)
def test_filter_command_exclude_command(self): exclude_command = Kill.jujud_cmd with temp_dir() as directory: runner = Runner(directory, ChaosMonkey.factory()) runner.filter_commands(exclude_command=exclude_command) self.assertGreaterEqual(len(runner.chaos_monkey.chaos), 1) self.assertTrue(all(c.command_str != Kill.jujud_cmd for c in runner.chaos_monkey.chaos))
def setUp(self): super(FakeHomeTestCase, self).setUp() self.home_dir = use_context(self, utility.temp_dir()) os.environ['HOME'] = self.home_dir os.environ['PATH'] = os.path.join(self.home_dir, '.local', 'bin') self.juju_home = os.path.join(self.home_dir, '.juju') os.mkdir(self.juju_home) self.set_public_clouds(get_default_public_clouds())
def prepare_constraint_test(client, constraints, charm_name, charm_series='xenial'): """Deploy a charm with constraints and data to see if it meets them.""" with temp_dir() as charm_dir: deploy_charm_constraint(client, constraints, charm_name, charm_series, charm_dir) client.wait_for_started() return application_hardware(client, charm_name)
def test_random_chaos_passes_timeout(self): with patch('utility.check_output', autospec=True): with patch('runner.Runner._run_command', autospec=True) as mock: with temp_dir() as directory: runner = Runner(directory, ChaosMonkey.factory()) runner.random_chaos(run_timeout=3, enablement_timeout=2) self.assertEqual(mock.call_args_list[0][0][1], 2)
def test_returns_created_clients(self): source_client = Mock() dest_client = Mock() with temp_dir() as tmp: new_source, new_dest = amm.create_user_on_controllers( source_client, dest_client, tmp, 'foo-username', 'write') self.assertEqual(new_source, source_client.register_user.return_value) self.assertEqual(new_dest, dest_client.register_user.return_value)