Skip to content

Commit

Permalink
Merge pull request #103 from QualiSystems/wip/adam.s/fix_str_lines
Browse files Browse the repository at this point in the history
fix str\bytes issue and tests
  • Loading branch information
nahumtimerman committed Apr 20, 2021
2 parents e7b045e + 34b973e commit be50cb8
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def __exit__(self, type, value, traceback):
lines = ['[defaults]']
for key, value in self.config_keys.items():
lines.append(key + ' = ' + value)
file_stream.write(os.linesep.join(lines))
file_stream.write(os.linesep.join(lines).encode('utf-8'))
self.logger.debug(os.linesep.join(lines))
self.logger.info('Done.')

Expand Down
2 changes: 1 addition & 1 deletion package/cloudshell/cm/ansible/domain/host_vars_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def __exit__(self, type, value, traceback):
lines = ['---']
for key, value in sorted(self.vars.items()):
lines.append(str(key) + ': "' + str(value) + '"')
file_stream.write(os.linesep.join(lines))
file_stream.write(bytes(os.linesep.join(lines), 'utf-8'))
self.logger.debug(os.linesep.join(lines))
self.logger.info('Done.')

Expand Down
2 changes: 1 addition & 1 deletion package/cloudshell/cm/ansible/domain/inventory_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def __exit__(self, type, value, traceback):
lines.append(host.name)
if len(lines) > 0 and lines[0] == '':
del lines[0]
file_stream.write(os.linesep.join(lines))
file_stream.write(os.linesep.join(lines).encode('utf-8'))
self.logger.debug(os.linesep.join(lines))
self.logger.info('Done (%s groups, with %s hosts).'%(str(len(self.groups)), str(len(self.hosts))))

Expand Down
2 changes: 1 addition & 1 deletion package/cloudshell/cm/ansible/domain/stdout_accumulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def read_all_txt(self):
try:
lines = []
while True:
lines.append(self.queue.get_nowait())
lines.append(self.queue.get_nowait().decode())
except Empty:
pass
finally:
Expand Down
4 changes: 2 additions & 2 deletions package/tests/test_ansible_config_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def setUp(self):
def test_can_add_ignore_ssh_key_checking(self):
with AnsibleConfigFile(self.file_system, Mock()) as f:
f.ignore_ssh_key_checking()
self.assertEqual(os.linesep.join(['[defaults]', 'host_key_checking = False']), self.file_system.read_all_lines('ansible.cfg'))
self.assertEqual(os.linesep.join(['[defaults]', 'host_key_checking = False']), self.file_system.read_all_lines('ansible.cfg').decode())

def test_can_add_force_color(self):
with AnsibleConfigFile(self.file_system, Mock()) as f:
Expand All @@ -22,4 +22,4 @@ def test_can_add_force_color(self):
def test_can_add_set_retry_path(self):
with AnsibleConfigFile(self.file_system, Mock()) as f:
f.set_retry_path(678)
self.assertEqual(os.linesep.join(['[defaults]', 'retry_files_save_path = 678']), self.file_system.read_all_lines('ansible.cfg'))
self.assertEqual(os.linesep.join(['[defaults]', 'retry_files_save_path = 678']), self.file_system.read_all_lines('ansible.cfg').decode())
12 changes: 6 additions & 6 deletions package/tests/test_host_vars_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,35 +19,35 @@ def test_can_add_connection_type(self):
with HostVarsFile(self.file_system, 'host1', Mock()) as f:
f.add_connection_type('winrm')
self.assertEqual(os.linesep.join(['---', 'ansible_connection: "winrm"']),
self.file_system.read_all_lines('host_vars', 'host1'))
self.file_system.read_all_lines('host_vars', 'host1').decode())

def test_can_add_connection_type(self):
with HostVarsFile(self.file_system, 'host1', Mock()) as f:
f.add_username('admin')
self.assertEqual(os.linesep.join(['---', 'ansible_user: "admin"']),
self.file_system.read_all_lines('host_vars', 'host1'))
self.file_system.read_all_lines('host_vars', 'host1').decode())

def test_can_add_connection_type(self):
with HostVarsFile(self.file_system, 'host1', Mock()) as f:
f.add_password('1234')
self.assertEqual(os.linesep.join(['---', 'ansible_ssh_pass: "1234"']),
self.file_system.read_all_lines('host_vars', 'host1'))
self.file_system.read_all_lines('host_vars', 'host1').decode())

def test_can_add_connection_key_file(self):
with HostVarsFile(self.file_system, 'host1', Mock()) as f:
f.add_conn_file('mycert.pem')
self.assertEqual(os.linesep.join(['---', 'ansible_ssh_private_key_file: "mycert.pem"']),
self.file_system.read_all_lines('host_vars', 'host1'))
self.file_system.read_all_lines('host_vars', 'host1').decode())

def test_can_add_port(self):
with HostVarsFile(self.file_system, 'host1', Mock()) as f:
f.add_port('1234')
self.assertEqual(os.linesep.join(['---', 'ansible_port: "1234"']),
self.file_system.read_all_lines('host_vars', 'host1'))
self.file_system.read_all_lines('host_vars', 'host1').decode())

def test_can_add_custom_vars(self):
with HostVarsFile(self.file_system, 'host1', Mock()) as f:
f.add_vars({'param1': 'abc', 'param2': '123'})
f.add_vars({'param3': 'W'})
self.assertEqual(os.linesep.join(['---', 'param1: "abc"', 'param2: "123"', 'param3: "W"']),
self.file_system.read_all_lines('host_vars', 'host1'))
self.file_system.read_all_lines('host_vars', 'host1').decode())
6 changes: 3 additions & 3 deletions package/tests/test_inventory_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def test_adding_hosts_without_groups_add_them_to_all(self):
with InventoryFile(self.file_system, 'hosts', Mock()) as f:
f.add_host_and_groups('host1')
f.add_host_and_groups('host2')
self.assertEqual(os.linesep.join(['[all]','host1','host2']), self.file_system.read_all_lines('hosts'))
self.assertEqual(os.linesep.join(['[all]','host1','host2']), self.file_system.read_all_lines('hosts').decode())

def test_cannot_add_same_host_twice(self):
with InventoryFile(self.file_system, 'hosts', Mock()) as f:
Expand All @@ -24,9 +24,9 @@ def test_cannot_add_same_host_twice(self):
def test_can_add_host_with_multiple_root_groups(self):
with InventoryFile(self.file_system, 'hosts', Mock()) as f:
f.add_host_and_groups('host1', ['group1','group2'])
self.assertEqual(os.linesep.join(['[group1]','host1','','[group2]','host1']), self.file_system.read_all_lines('hosts'))
self.assertEqual(os.linesep.join(['[group1]','host1','','[group2]','host1']), self.file_system.read_all_lines('hosts').decode())

def test_can_add_host_with_multiple_sub_groups(self):
with InventoryFile(self.file_system, 'hosts', Mock()) as f:
f.add_host_and_groups('host1', ['group1/sub1', 'group1/sub2'])
self.assertEqual(os.linesep.join(['[group1:children]','sub1','sub2','','[sub1]','host1','','[sub2]','host1']), self.file_system.read_all_lines('hosts'))
self.assertEqual(os.linesep.join(['[group1:children]','sub1','sub2','','[sub1]','host1','','[sub2]','host1']), self.file_system.read_all_lines('hosts').decode())

0 comments on commit be50cb8

Please sign in to comment.