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_temp_dir_keep(self): with temp_dir() as p: with temp_dir(parent=p, keep=True) as d: self.assertTrue(os.path.isdir(d)) open(os.path.join(d, "a-file"), "w").close() self.assertTrue(os.path.exists(d)) self.assertTrue(os.path.exists(os.path.join(d, "a-file"))) self.assertFalse(os.path.exists(p))
def test_ignore_older_revision_build(self): with temp_dir() as root: path_1234 = make_candidate_dir( root, '1234-artifacts', 'mybranch', '1234') make_candidate_dir(root, '1233', 'mybranch', '1233') self.assertEqual(find_latest_branch_candidates(root), [ (path_1234, 1234)])
def test_ignore_older_revision_build(self): with temp_dir() as root: path_1234 = make_candidate_dir(root, '1234-artifacts', 'mybranch', '1234') make_candidate_dir(root, '1233', 'mybranch', '1233') self.assertEqual(find_latest_branch_candidates(root), [(path_1234, 1234)])
def test__find_candidates_artifacts_enabled(self): with temp_dir() as root: make_candidate_dir(root, 'master-artifacts') make_candidate_dir(root, '1.25') candidate = os.path.join(root, 'candidate', 'master-artifacts') self.assertEqual(list(_find_candidates(root, artifacts=True)), [ (candidate, os.path.join(candidate, 'buildvars.json'))])
def test_find_candidates_find_all(self): with temp_dir() as root: a_week_ago = time() - timedelta(days=7, seconds=1).total_seconds() master_path = make_candidate_dir(root, '1.23', modified=a_week_ago) master_path_2 = make_candidate_dir(root, '1.24') self.assertItemsEqual(list(find_candidates(root)), [master_path_2]) self.assertItemsEqual(list(find_candidates(root, find_all=True)), [master_path, master_path_2])
def test__find_candidates_artifacts_enabled(self): with temp_dir() as root: make_candidate_dir(root, 'master-artifacts') make_candidate_dir(root, '1.25') candidate = os.path.join(root, 'candidate', 'master-artifacts') self.assertEqual( list(_find_candidates(root, artifacts=True)), [(candidate, os.path.join(candidate, 'buildvars.json'))])
def test_include_older_revision_build_different_branch(self): with temp_dir() as root: path_1234 = make_candidate_dir(root, '1234-artifacts', 'branch_foo', '1234') path_1233 = make_candidate_dir(root, '1233-artifacts', 'branch_bar', '1233') self.assertItemsEqual(find_latest_branch_candidates(root), [(path_1233, 1233), (path_1234, 1234)])
def test_include_older_revision_build_different_branch(self): with temp_dir() as root: path_1234 = make_candidate_dir( root, '1234-artifacts', 'branch_foo', '1234') path_1233 = make_candidate_dir( root, '1233-artifacts', 'branch_bar', '1233') self.assertItemsEqual( find_latest_branch_candidates(root), [ (path_1233, 1233), (path_1234, 1234)])
def client_and_plan(): cur_client = fake_juju_client() cur_client.bootstrap() with patch('hammer_time.hammer_time.client_for_existing', return_value=cur_client, autospec=True) as cfe_mock: with temp_dir() as plan_dir: plan_file = os.path.join(plan_dir, 'asdf.yaml') yield (cur_client, cfe_mock, plan_file)
def test_no_warn_on_empty_logs(self): """Special case a file named 'empty' doesn't make log dir dirty""" with warnings.catch_warnings(record=True) as warned: with temp_dir() as log_dir: open(os.path.join(log_dir, "empty"), "w").close() cmd_line = ['local', '/a/juju', log_dir, 'testtest'] parser = add_basic_testing_arguments(ArgumentParser()) parser.parse_args(cmd_line) self.assertEqual(warned, []) self.assertEqual("", self.log_stream.getvalue())
def test_warns_on_dirty_logs(self): with warnings.catch_warnings(record=True) as warned: with temp_dir() as log_dir: open(os.path.join(log_dir, "existing.log"), "w").close() cmd_line = ['local', '/a/juju', log_dir, 'testtest'] parser = add_basic_testing_arguments(ArgumentParser()) parser.parse_args(cmd_line) self.assertEqual(len(warned), 1) self.assertRegexpMatches( str(warned[0].message), r"^Directory '.*' has existing contents.$") self.assertEqual("", self.log_stream.getvalue())
def test_find_candidates_old_buildvars(self): with temp_dir() as root: a_week_ago = time() - timedelta(days=7, seconds=1).total_seconds() make_candidate_dir(root, 'master', modified=a_week_ago) self.assertEqual(list(find_candidates(root)), [])
def test_find_latest_branch_candidates(self): with temp_dir() as root: master_path = make_candidate_dir(root, 'master-artifacts') self.assertEqual(find_latest_branch_candidates(root), [(master_path, 1234)])
def test_find_candidates_artifacts(self): with temp_dir() as root: make_candidate_dir(root, 'master-artifacts') self.assertEqual(list(find_candidates(root)), [])
def test_find_candidates(self): with temp_dir() as root: master_path = make_candidate_dir(root, 'master') self.assertEqual(list(find_candidates(root)), [master_path])
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))