def test_temp_dir_parent(self): with temp_dir() as p: with temp_dir(parent=p) as d: self.assertTrue(os.path.isdir(d)) self.assertEqual(p, os.path.dirname(d)) self.assertFalse(os.path.exists(d)) self.assertFalse(os.path.exists(p))
def test_copytree_force(self): with temp_dir() as src: with temp_dir() as dst: sub_src_dir = os.path.join(src, 'tmp') os.mkdir(sub_src_dir) sub_dst_dir = os.path.join(dst, 'tmp') os.mkdir(sub_dst_dir) copytree_force(src, dst) self.assertTrue(os.path.exists(sub_dst_dir))
def test_make_parameters(self): with temp_dir() as test_dir: test_plan = self.fake_parameters(test_dir) args = Namespace(controllers=['default-aws']) parameters = make_parameters(test_plan, args) expected = { 'controllers': 'default-aws', 'bundle_name': 'make_life_easy', 'test_plan': test_plan} self.assertEqual(parameters, expected)
def test_build_jobs(self): credentials = Credentials('joe', 'pass') args = Namespace(cwr_test_token='fake', controllers=['default-aws', 'default-gce'], test_plan_dir='') with patch('buildcloud.schedule_cwr_jobs.Jenkins', autospec=True) as jenkins_mock: with patch('buildcloud.schedule_cwr_jobs.generate_test_id', side_effect=['1', '2', '3', '4']) as gti_mock: with temp_dir() as test_dir: args.test_plan_dir = test_dir test_plan1 = os.path.join(test_dir, 'test1.yaml') test_plan2 = os.path.join(test_dir, 'test2.yaml') self.fake_parameters(test_dir) self.fake_parameters(test_dir, 2) test_plans = [test_plan1, test_plan2] build_jobs(credentials, test_plans, args) jenkins_mock.assert_called_once_with( 'http://juju-ci.vapour.ws:8080', 'joe', 'pass') self.assertEqual(gti_mock.mock_calls, [call(), call()]) calls = [ call('cwr-aws', { 'controllers': 'default-aws', 'bundle_name': 'make_life_easy', 'test_id': '1', 'test_plan': test_plan1 }, token='fake'), call('cwr-gce', { 'controllers': 'default-gce', 'bundle_name': 'make_life_easy', 'test_id': '1', 'test_plan': test_plan1 }, token='fake'), call('cwr-aws', { 'controllers': 'default-aws', 'bundle_name': 'make_life_easy', 'test_id': '2', 'test_plan': test_plan2 }, token='fake'), call('cwr-gce', { 'controllers': 'default-gce', 'bundle_name': 'make_life_easy', 'test_id': '2', 'test_plan': test_plan2 }, token='fake') ] self.assertEqual(jenkins_mock.return_value.build_job.mock_calls, calls)
def env(args): with temp_dir() as root: tmp_juju_home = os.path.join(root, 'tmp_juju_home') shutil.copytree(args.juju_home, tmp_juju_home, ignore=shutil.ignore_patterns('environments')) juju_repository = ensure_dir('juju_repository', parent=root) test_results = ensure_dir('results', parent=root) tmp = ensure_dir('tmp', parent=root) ssh_dir = os.path.join(tmp, 'ssh') os.mkdir(ssh_dir) shutil.copyfile(os.path.join(tmp_juju_home, 'staging-juju-rsa'), os.path.join(ssh_dir, 'id_rsa')) ssh_path = os.path.join(tmp, 'ssh') new_names = [] for model in args.model: prefix = 'cwr-' if 'azure' in model.lower(): # Use Jenkins BUILD_NUMBER if it is available as a unique name. u = os.environ.get('BUILD_NUMBER') or str(time()).split('.')[0] prefix = '{}{}-'.format(prefix, u) name = rename_env(model, prefix, os.path.join(tmp_juju_home, 'environments.yaml')) new_names.append(name) host = Host(tmp_juju_home=tmp_juju_home, juju_repository=juju_repository, test_results=test_results, tmp=tmp, ssh_path=ssh_path, root=root, models=new_names) Container = namedtuple('Container', [ 'user', 'name', 'home', 'ssh_home', 'juju_home', 'test_results', 'juju_repository', 'test_plans' ]) container_user = '******' container_home = os.path.join('/home', container_user) container_juju_home = os.path.join(container_home, '.juju') container_ssh_home = os.path.join(container_home, '.ssh') container_test_results = os.path.join(container_home, 'results') container_repository = os.path.join(container_home, 'charm-repo') container_test_plans = os.path.join(container_home, 'test_plans') container = Container(user=container_user, name='seman/cwrbox', home=container_home, ssh_home=container_ssh_home, juju_home=container_juju_home, test_results=container_test_results, juju_repository=container_repository, test_plans=container_test_plans) yield host, container
def test_make_parameters(self): args = Namespace(controllers=['default-aws']) with temp_dir() as test_dir: test_plan = self.fake_parameters(test_dir) parameters = make_parameters(test_plan, args, 'default-aws', '1234') expected = { 'controllers': 'default-aws', 'bundle_name': 'make_life_easy', 'test_id': '1234', 'test_plan': test_plan } self.assertEqual(parameters, expected)
def test_rename_env(self): with temp_dir() as tmp_dir: env = {'environments': { 'old-env': {'access-key': 'my_access_key'}}} file_path = os.path.join(tmp_dir, 't.yaml') with open(file_path, 'w') as f: yaml.dump(env, f, default_flow_style=True) rename_env('old-env', 'cwr-', file_path) with open(file_path) as f: new_env = yaml.load(f) expected_env = {'environments': {'cwr-old-env': { 'access-key': 'my_access_key'}}} self.assertEqual(new_env, expected_env)
def test_build_jobs(self): credentials = Credentials('joe', 'pass') args = Namespace(cwr_test_token='fake', controllers=['default-aws', 'default-gce'], test_plan_dir='') with patch('buildcloud.schedule_cwr_jobs.Jenkins', autospec=True) as jenkins_mock: with patch('buildcloud.schedule_cwr_jobs.generate_test_id', side_effect=['1', '2', '3', '4']) as gti_mock: with temp_dir() as test_dir: args.test_plan_dir = test_dir test_plan1 = os.path.join(test_dir, 'test1.yaml') test_plan2 = os.path.join(test_dir, 'test2.yaml') self.fake_parameters(test_dir) self.fake_parameters(test_dir, 2) test_plans = [test_plan1, test_plan2] build_jobs(credentials, test_plans, args) jenkins_mock.assert_called_once_with('http://juju-ci.vapour.ws:8080', 'joe', 'pass') self.assertEqual(gti_mock.mock_calls, [call(), call()]) calls = [ call('cwr-aws', { 'controllers': 'default-aws', 'bundle_name': 'make_life_easy', 'test_id': '1', 'test_plan': test_plan1 }, token='fake'), call('cwr-gce', { 'controllers': 'default-gce', 'bundle_name': 'make_life_easy', 'test_id': '1', 'test_plan': test_plan1 }, token='fake'), call('cwr-aws', { 'controllers': 'default-aws', 'bundle_name': 'make_life_easy', 'test_id': '2', 'test_plan': test_plan2 }, token='fake'), call('cwr-gce', { 'controllers': 'default-gce', 'bundle_name': 'make_life_easy', 'test_id': '2', 'test_plan': test_plan2 }, token='fake') ] self.assertEqual(jenkins_mock.return_value.build_job.mock_calls, calls)
def env(args): with temp_dir() as root: tmp_juju_home = os.path.join(root, 'tmp_juju_home') shutil.copytree(args.juju_home, tmp_juju_home, ignore=shutil.ignore_patterns('environments')) juju_repository = ensure_dir('juju_repository', parent=root) test_results = ensure_dir('results', parent=root) tmp = ensure_dir('tmp', parent=root) ssh_dir = os.path.join(tmp, 'ssh') os.mkdir(ssh_dir) shutil.copyfile(os.path.join(tmp_juju_home, 'staging-juju-rsa'), os.path.join(ssh_dir, 'id_rsa')) ssh_path = os.path.join(tmp, 'ssh') new_names = [] for model in args.model: name = rename_env(model, 'cwr-', os.path.join( tmp_juju_home, 'environments.yaml')) new_names.append(name) Host = namedtuple( 'Host', ['tmp_juju_home', 'juju_repository', 'test_results', 'tmp', 'ssh_path', 'root', 'models']) host = Host( tmp_juju_home=tmp_juju_home, juju_repository=juju_repository, test_results=test_results, tmp=tmp, ssh_path=ssh_path, root=root, models=new_names) Container = namedtuple( 'Container', ['user', 'name', 'home', 'ssh_home', 'juju_home', 'test_results', 'juju_repository', 'test_plans']) container_user = '******' container_home = os.path.join('/home', container_user) container_juju_home = os.path.join(container_home, '.juju') container_ssh_home = os.path.join(container_home, '.ssh') container_test_results = os.path.join(container_home, 'results') container_repository = os.path.join(container_home, 'charm-repo') container_test_plans = os.path.join(container_home, 'test_plans') container = Container(user=container_user, name='seman/cwrbox', home=container_home, ssh_home=container_ssh_home, juju_home=container_juju_home, test_results=container_test_results, juju_repository=container_repository, test_plans=container_test_plans) yield host, container
def env(args): with temp_dir() as root: tmp_juju_home = os.path.join(root, 'tmp_juju_home') shutil.copytree(args.juju_home, tmp_juju_home, ignore=shutil.ignore_patterns('environments')) juju_repository = ensure_dir('juju_repository', parent=root) test_results = ensure_dir('results', parent=root) tmp = ensure_dir('tmp', parent=root) ssh_dir = os.path.join(tmp, 'ssh') os.mkdir(ssh_dir) shutil.copyfile(os.path.join(tmp_juju_home, 'staging-juju-rsa'), os.path.join(ssh_dir, 'id_rsa')) ssh_path = os.path.join(tmp, 'ssh') new_names = [] for model in args.model: prefix = 'cwr-' if 'azure' in model.lower(): # Use Jenkins BUILD_NUMBER if it is available as a unique name. u = os.environ.get('BUILD_NUMBER') or str(time()).split('.')[0] prefix = '{}{}-'.format(prefix, u) name = rename_env(model, prefix, os.path.join( tmp_juju_home, 'environments.yaml')) new_names.append(name) host = Host( tmp_juju_home=tmp_juju_home, juju_repository=juju_repository, test_results=test_results, tmp=tmp, ssh_path=ssh_path, root=root, models=new_names) Container = namedtuple( 'Container', ['user', 'name', 'home', 'ssh_home', 'juju_home', 'test_results', 'juju_repository', 'test_plans']) container_user = '******' container_home = os.path.join('/home', container_user) container_juju_home = os.path.join(container_home, '.juju') container_ssh_home = os.path.join(container_home, '.ssh') container_test_results = os.path.join(container_home, 'results') container_repository = os.path.join(container_home, 'charm-repo') container_test_plans = os.path.join(container_home, 'test_plans') container = Container(user=container_user, name='seman/cwrbox', home=container_home, ssh_home=container_ssh_home, juju_home=container_juju_home, test_results=container_test_results, juju_repository=container_repository, test_plans=container_test_plans) yield host, container
def test_get_test_plans(self): args = Namespace(controllers=['default-aws'], password='******', test_plan_dir='', test_plans=None, user='******') with temp_dir() as test_dir: args.test_plan_dir = test_dir self.fake_parameters(test_dir) self.fake_parameters(test_dir, 2) self.fake_parameters(test_dir, 3, ext='.py') parameters = list(get_test_plans(args)) expected = [ os.path.join(test_dir, 'test1.yaml'), os.path.join(test_dir, 'test2.yaml'), ] self.assertItemsEqual(parameters, expected)
def env(args): with temp_dir() as root: tmp_juju_home = os.path.join(root, 'tmp_juju_home') shutil.copytree(args.juju_home, tmp_juju_home, ignore=shutil.ignore_patterns('environments')) juju_repository = ensure_dir('juju_repository', parent=root) test_results = ensure_dir('results', parent=root) tmp = ensure_dir('tmp', parent=root) ssh_dir = os.path.join(tmp, 'ssh') os.mkdir(ssh_dir) shutil.copyfile(os.path.join(tmp_juju_home, 'staging-juju-rsa'), os.path.join(ssh_dir, 'id_rsa')) ssh_path = os.path.join(tmp, 'ssh') if args.controllers_bootstrapped: new_names = args.controllers else: new_names = generate_controller_names(args.controllers) host = Host(tmp_juju_home=tmp_juju_home, juju_repository=juju_repository, test_results=test_results, tmp=tmp, ssh_path=ssh_path, root=root, controllers=new_names) Container = namedtuple('Container', [ 'user', 'name', 'home', 'ssh_home', 'juju_home', 'test_results', 'juju_repository', 'test_plans' ]) container_user = '******' container_home = os.path.join('/home', container_user) container_juju_home = os.path.join(container_home, '.juju') container_ssh_home = os.path.join(container_home, '.ssh') container_test_results = os.path.join(container_home, 'results') container_repository = os.path.join(container_home, 'charm-repo') container_test_plans = os.path.join(container_home, 'test_plans') container = Container(user=container_user, name='jujusolutions/cwrbox', home=container_home, ssh_home=container_ssh_home, juju_home=container_juju_home, test_results=container_test_results, juju_repository=container_repository, test_plans=container_test_plans) yield host, container
def test_make_parameters(self): with temp_dir() as test_dir: test_plan = self.fake_parameters( test_dir, bucket='foo-bucket', results_dir='foo-results', s3_private=True) parameters = make_parameters(test_plan, 'default-aws', '1234') expected = { 'bucket': 'foo-bucket', 'controllers': 'default-aws', 'bundle_name': 'make_life_easy', 'results_dir': 'foo-results', 'test_id': '1234', 'test_plan': test_plan, 's3_private': True, } self.assertEqual(parameters, expected)
def test_make_jobs(self): args = Namespace(controllers=['default-aws'], password='******', test_plan_dir='', test_plans=None, user='******') with temp_dir() as test_dir: args.test_plan_dir = test_dir test_plan = self.fake_parameters(test_dir) test_plan_2 = self.fake_parameters(test_dir, 2) self.fake_parameters(test_dir, 3, ext='.py') parameters = list(make_jobs(args)) expected = [ {'controllers': 'default-aws', 'bundle_name': 'make_life_easy', 'test_plan': test_plan}, {'controllers': 'default-aws', 'bundle_name': 'make_life_easy', 'test_plan': test_plan_2}] self.assertItemsEqual(parameters, expected)
def env(args): with temp_dir() as root: tmp_juju_home = os.path.join(root, 'tmp_juju_home') shutil.copytree(args.juju_home, tmp_juju_home, ignore=shutil.ignore_patterns('environments')) juju_repository = ensure_dir('juju_repository', parent=root) test_results = ensure_dir('results', parent=root) tmp = ensure_dir('tmp', parent=root) ssh_dir = os.path.join(tmp, 'ssh') os.mkdir(ssh_dir) shutil.copyfile(os.path.join(tmp_juju_home, 'staging-juju-rsa'), os.path.join(ssh_dir, 'id_rsa')) ssh_path = os.path.join(tmp, 'ssh') if args.controllers_bootstrapped: new_names = args.controllers else: new_names = generate_controller_names(args.controllers) host = Host(tmp_juju_home=tmp_juju_home, juju_repository=juju_repository, test_results=test_results, tmp=tmp, ssh_path=ssh_path, root=root, controllers=new_names) Container = namedtuple( 'Container', ['user', 'name', 'home', 'ssh_home', 'juju_home', 'test_results', 'juju_repository', 'test_plans']) container_user = '******' container_home = os.path.join('/home', container_user) container_juju_home = os.path.join(container_home, '.juju') container_ssh_home = os.path.join(container_home, '.ssh') container_test_results = os.path.join(container_home, 'results') container_repository = os.path.join(container_home, 'charm-repo') container_test_plans = os.path.join(container_home, 'test_plans') container = Container(user=container_user, name='jujusolutions/cwrbox', home=container_home, ssh_home=container_ssh_home, juju_home=container_juju_home, test_results=container_test_results, juju_repository=container_repository, test_plans=container_test_plans) yield host, container
def test_rename_env(self): with temp_dir() as tmp_dir: env = { 'environments': { 'old-env': { 'access-key': 'my_access_key' } } } file_path = os.path.join(tmp_dir, 't.yaml') with open(file_path, 'w') as f: yaml.dump(env, f, default_flow_style=True) rename_env('old-env', 'cwr-', file_path) with open(file_path) as f: new_env = yaml.load(f) expected_env = { 'environments': { 'cwr-old-env': { 'access-key': 'my_access_key' } } } self.assertEqual(new_env, expected_env)
def test_temp_dir_contents(self): with temp_dir() as d: self.assertTrue(os.path.isdir(d)) open(os.path.join(d, "a-file"), "w").close() self.assertFalse(os.path.exists(d))
def test_temp_dir(self): with temp_dir() as d: self.assertTrue(os.path.isdir(d)) self.assertFalse(os.path.exists(d))