def test_import_file_clobber(self): content1 = '''\ [something] fruit = kiwi ''' content2 = '''\ [something] cheese = brie ''' content3 = '''\ [something] fruit = lemon ''' tmp = temp_file.make_temp_file() e = CFE(tmp) e.import_file(temp_file.make_temp_file(content=content1)) e.import_file(temp_file.make_temp_file(content=content2)) e.import_file(temp_file.make_temp_file(content=content3)) expected = '''\ [something] fruit = lemon cheese = brie ''' self.assertMultiLineEqual(expected, file_util.read(tmp, codec='utf-8'))
def test_import_file_clobber(self): content1 = '''\ something fruit: kiwi ''' content2 = '''\ something cheese: brie ''' content3 = '''\ something fruit: lemon ''' tmp = temp_file.make_temp_file() e = SCE(tmp) e.import_file(temp_file.make_temp_file(content=content1)) e.import_file(temp_file.make_temp_file(content=content2)) e.import_file(temp_file.make_temp_file(content=content3)) expected = '''\ something fruit: lemon cheese: brie ''' self.assert_text_file_equal(expected, tmp, native_line_breaks=True)
def test_import_file_empty_config(self): content1 = '''\ [something] fruit = kiwi ''' content2 = '''\ [something] cheese = brie ''' content3 = '''\ [something] wine = barolo ''' content4 = '''\ ''' tmp = temp_file.make_temp_file() e = CFE(tmp) e.import_file(temp_file.make_temp_file(content=content1)) e.import_file(temp_file.make_temp_file(content=content2)) e.import_file(temp_file.make_temp_file(content=content3)) e.import_file(temp_file.make_temp_file(content=content4)) expected = '''\ [something] fruit = kiwi cheese = brie wine = barolo ''' self.assertMultiLineEqual(expected, file_util.read(tmp, codec='utf-8'))
def test_import_file_empty_config(self): content1 = '''\ something fruit: kiwi ''' content2 = '''\ something cheese: brie ''' content3 = '''\ something wine: barolo ''' content4 = '''\ ''' tmp = temp_file.make_temp_file() e = SCE(tmp) e.import_file(temp_file.make_temp_file(content=content1)) e.import_file(temp_file.make_temp_file(content=content2)) e.import_file(temp_file.make_temp_file(content=content3)) e.import_file(temp_file.make_temp_file(content=content4)) expected = '''\ something fruit: kiwi cheese: brie wine: barolo ''' self.assert_text_file_equal(expected, tmp, native_line_breaks=True)
def diff_manifest(clazz, archive1, archive2, strip_common_ancestor=False): 'Return the output of diffing the contents of 2 archives.' members1 = archiver.members(archive1) members2 = archiver.members(archive2) content1 = '\n'.join(members1) content2 = '\n'.join(members2) tmp_file1 = temp_file.make_temp_file(content=content1) tmp_file2 = temp_file.make_temp_file(content=content2) cmd = ['diff', '-u', '-r', tmp_file1, tmp_file2] rv = execute.execute(cmd, raise_error=False, stderr_to_stdout=True) return rv
def _make_temp_archive_xz(clazz, items, filename, mode): tmp_dir = temp_file.make_temp_dir() for item in items: assert item assert item.arcname file_util.save(path.join(tmp_dir, item.arcname), content=item.content) tmp_xz = temp_file.make_temp_file() manifest_content = '\n'.join([item.arcname for item in items]) manifest = temp_file.make_temp_file(content=manifest_content) cmd = 'tar Jcf %s -C %s -T %s' % (filename, tmp_dir, manifest) execute.execute(cmd) file_util.remove(tmp_dir)
def test_import_file_empty_config(self): content = '''\ something fruit: kiwi ''' content2 = '''\ ''' tmp = temp_file.make_temp_file(content=content) e = SCE(tmp) e.import_file(temp_file.make_temp_file(content=content2)) expected = '''\ something fruit: kiwi ''' self.assert_text_file_equal(expected, tmp, native_line_breaks=True)
def test_update_config(self): content = '''\ [something] fruit = kiwi cheese = brie wine = barolo ''' tmp = temp_file.make_temp_file(content=content) e = CFE(tmp) e.update_config({ 'something': { 'fruit': 'lemon', 'candy': 'chocolate', }, 'foo': { 'bread': 'baguette', 'drink': 'water', }, }) expected = '''\ [something] fruit = lemon cheese = brie wine = barolo candy = chocolate [foo] bread = baguette drink = water ''' self.assertMultiLineEqual(expected, file_util.read(tmp, codec='utf-8'))
def test_import_file_empty_config(self): content = '''\ [something] fruit = kiwi ''' content2 = '''\ ''' tmp = temp_file.make_temp_file(content=content) e = CFE(tmp) e.import_file(temp_file.make_temp_file(content=content2)) expected = '''\ [something] fruit = kiwi ''' self.assertMultiLineEqual(expected, file_util.read(tmp, codec='utf-8'))
def test__make_parser_from_file(self): text = '''\ [default] color = "red" fruit = "apple" [new_zealand] color = "green" fruit = "kiwi" [indonesia] color = "yellow" fruit = "durian" [antartica] ''' self.assert_dict_as_text_equal( { 'default': { 'color': '"red"', 'fruit': '"apple"', }, 'new_zealand': { 'color': '"green"', 'fruit': '"kiwi"', }, 'indonesia': { 'color': '"yellow"', 'fruit': '"durian"', }, 'antartica': { }, }, config._make_parser_from_file(temp_file.make_temp_file(content = text)).to_dict() )
def run_script(clazz, exe, script, args=None): 'Run the script and return the result.' check.check_string(exe) check.check_string(script) check.check_string_seq(args, allow_none=True) args = list(args or []) tmp_script = temp_file.make_temp_file(content=script, suffix='.py') cmd = [exe, tmp_script] + args clazz._log.log_d('run_script: exe={} cmd={}'.format(exe, cmd)) clazz._log.log_d('run_script: script=\n{}\n'.format(script)) try: output_bytes = subprocess.check_output(cmd, stderr=subprocess.STDOUT) exit_code = 0 clazz._log.log_d('run_script: success') except subprocess.CalledProcessError as ex: clazz._log.log_d('run_script: caught: {}'.format(str(ex))) output_bytes = ex.output exit_code = ex.returncode clazz._log.log_d('run_script: failed') finally: file_util.remove(tmp_script) output = codecs.decode(output_bytes, 'utf-8').strip() clazz._log.log_d('run_script: exit_code={} output="{}"') return clazz._run_script_result(exit_code, output)
def test_set_value_empty_file(self): 'Set the first value for a non existent properties file.' tmp = temp_file.make_temp_file(content = '') e = PE(tmp) e.set_value('fruit', 'kiwi') expected = """fruit: kiwi\n""" self.assertMultiLineEqual(expected, file_util.read(tmp, codec = 'utf-8') )
def _do_set_credentials(clazz, username, password, print_error): expect_script = clazz._EXPECT_SCRIPT_TEMPLATE.format(username = username, password = password) # Remove ~/.vmrestCfg if corrupt clazz._vmrest_config_remove_corrupt() # Make sure there no stale vmrest processes vmware_util.killall_vmrest() tmp_script = temp_file.make_temp_file(suffix = '.expect', perm = 0o755, content = expect_script, delete = False) try: cmd = [ 'expect', '-d', tmp_script ] rv = execute.execute(cmd, raise_error = False, shell = False) clazz._log.log_d('_do_set_credentials: exit_code={} stdout="{}" stderr="{}"'.format(rv.exit_code, rv.stdout, rv.stderr)) if rv.exit_code != 0: raise vmware_error(clazz._EXPECT_SCRIPT_MSG_MAP[rv.exit_code]) finally: file_util.remove(tmp_script) if clazz._vmrest_config_is_corrupt(): msg = 'Failed to set credentials. Corrupt {}'.format(clazz._VMREST_CONFIG_FILENAME) if print_error: clazz._log.log_i(msg) raise vmware_error(msg)
def test_env_and_gradle(self): cm = credentials_manager() cm.add_source( credentials_source_env({ 'username': '******', 'password': '******' })) text = '''\ [email protected] devPassword=xflintpass systemProp.gradle.wrapperUser=tuser systemProp.gradle.wrapperPassword=tpassword ''' cm.add_source( credentials_source_gradle(temp_file.make_temp_file(content=text), 'dev')) with env_override({ 'MY_USERNAME': '******', 'MY_PASSWORD': '******' }) as env: self.assertEqual(True, cm.is_valid()) c = cm.credentials() self.assertEqual('fred', c.username) self.assertEqual('flintpass', c.password) with env_override() as env: self.assertEqual(True, cm.is_valid()) c = cm.credentials() self.assertEqual('*****@*****.**', c.username) self.assertEqual('xflintpass', c.password)
def backup(self, output_archive): 'Backup image to an gzipped tarball.' tmp_tar = temp_file.make_temp_file(suffix='.tar') args = ['save', self.tagged_repository, '-o', tmp_tar] docker_exe.call_docker(args, non_blocking=False) compressed_file.compress(tmp_tar, output_archive) file_util.remove(tmp_tar)
def make_temp_archive(clazz, items, extension, delete=True, prefix=None): prefix = prefix or 'tmp_' assert archive_extension.is_valid_ext(extension) ext_type = clazz._determine_type(extension) assert ext_type temp_archive_filename = temp_file.make_temp_file(suffix='.' + extension, prefix=prefix, delete=delete) archive_mode = archive_extension.write_format(extension) if ext_type == 'zip': clazz._make_temp_archive_zip(items, temp_archive_filename, archive_mode) elif ext_type == 'tar': clazz._make_temp_archive_tar(items, temp_archive_filename, archive_mode) elif ext_type == 'dmg': clazz._make_temp_archive_dmg(items, temp_archive_filename, archive_mode) elif ext_type == 'xz': clazz._make_temp_archive_tar(items, temp_archive_filename, archive_mode) # clazz._make_temp_archive_xz(items, temp_archive_filename, archive_mode) return temp_archive_filename
def _make_temp_askpass(clazz, password): content = '''\ #!/bin/sh echo "{password}" '''.format(password=password) return temp_file.make_temp_file(content=content, delete=True, perm=0o700)
def test_read_to_tuple_layered(self): text1 = """\ fruit: 'kiwi' status: 'doomed' version: '1.2.3' """ text2 = """\ fruit: 'orange' status: 'doomed' version: '1.2.4' """ tmp1 = temp_file.make_temp_file(content=text1) tmp2 = temp_file.make_temp_file(content=text2) TC = namedtuple('TC', 'fruit, status, version') self.assertEqual(('orange', 'doomed', '1.2.4'), PF.read_to_tuple_layered([tmp1, tmp2], TC))
def test_change_version(self): content = """\ version: 1.2.3 """ tmp = temp_file.make_temp_file(content = content) e = PE(tmp) e.change_version('version', 'major', 9) self.assertEqual( '9.2.3', e.get_value('version') )
def test_properties(self): content = """\ color: 'green' fruit: 'kiwi' """ tmp = temp_file.make_temp_file(content = content) e = PE(tmp) self.assertEqual( { 'color': 'green', 'fruit': 'kiwi' }, e.properties() )
def _local_checksum(self, filename): if self.compressed: tmp_uncompressed_file = temp_file.make_temp_file() compressed_file.uncompress(filename, tmp_uncompressed_file) result = file_util.checksum('sha256', tmp_uncompressed_file) file_util.remove(tmp_uncompressed_file) else: result = file_util.checksum('sha256', filename)
def test_keys(self): 'Get all the keys.' content = """\ fruit: kiwi """ tmp = temp_file.make_temp_file(content = content) e = PE(tmp) self.assertEqual( [ 'fruit' ], e.keys() )
def test_get_value(self): content = """\ color: 'green' fruit: 'kiwi' """ tmp = temp_file.make_temp_file(content = content) e = PE(tmp) self.assertEqual( 'green', e.get_value('color') ) self.assertEqual( 'kiwi', e.get_value('fruit') )
def commit(clazz, root_dir, message, filenames): filenames = object_util.listify(filenames) tmp_msg = temp_file.make_temp_file(content = message) args = [ 'commit', '-F', tmp_msg ] + filenames try: rv = git_exe.call_git(root_dir, args) finally: file_util.remove(tmp_msg) return clazz.last_commit_hash(root_dir, short_hash = True)
def test_get_value_existing_file(self): content = '''\ something fruit: kiwi ''' tmp = temp_file.make_temp_file(content=content) e = SCE(tmp) self.assert_text_file_equal(content, tmp, native_line_breaks=True) self.assertEqual('kiwi', e.get_value('something', 'fruit'))
def test_read_to_tuple_missing_fields(self): text = """\ fruit: 'kiwi' version: '1.2.3' """ tmp = temp_file.make_temp_file(content=text) TC = namedtuple('TC', 'fruit, status, version') self.assertEqual(('kiwi', None, '1.2.3'), PF.read_to_tuple(tmp, TC))
def test_get_value_existing_file(self): content = '''\ [something] fruit = kiwi ''' tmp = temp_file.make_temp_file(content=content) e = CFE(tmp) self.assertMultiLineEqual(content, file_util.read(tmp, codec='utf-8')) self.assertEqual('kiwi', e.get_value('something', 'fruit'))
def test_get_value_missing_key(self): content = '''\ something fruit: kiwi ''' tmp = temp_file.make_temp_file(content=content) e = SCE(tmp) self.assert_text_file_equal(content, tmp, native_line_breaks=True) with self.assertRaises(KeyError) as ctx: self.assertEqual(None, e.get_value('something', 'color'))
def test_get_value_missing_key(self): content = '''\ [something] fruit = kiwi ''' tmp = temp_file.make_temp_file(content=content) e = CFE(tmp) self.assertMultiLineEqual(content, file_util.read(tmp, codec='utf-8')) with self.assertRaises(KeyError) as ctx: self.assertEqual(None, e.get_value('something', 'color'))
def test_remove_value(self): content = """\ color: 'green' fruit: 'kiwi' """ tmp = temp_file.make_temp_file(content = content) e = PE(tmp) self.assertEqual( 'green', e.get_value('color') ) e.remove_value('color') with self.assertRaises(KeyError) as ctx: e.get_value('color')