def populate_group_vars(self, ansible_dir): main_vars = ObjDict() main_vars['admin_user'] = self.cluster_model.specification.admin_user main_vars['validate_certs'] = Config().validate_certs main_vars['offline_requirements'] = Config().offline_requirements main_vars['wait_for_pods'] = Config().wait_for_pods main_vars['is_upgrade_run'] = self.is_upgrade_run main_vars['roles_with_generated_vars'] = sorted( self.roles_with_generated_vars) if self.is_upgrade_run: shared_config_doc = self.get_shared_config_from_manifest() else: shared_config_doc = select_first( self.config_docs, lambda x: x.kind == 'configuration/shared-config') if shared_config_doc is None: shared_config_doc = load_yaml_obj(types.DEFAULT, 'common', 'configuration/shared-config') self.set_vault_path(shared_config_doc) main_vars.update(shared_config_doc.specification) vars_dir = os.path.join(ansible_dir, 'group_vars') if not os.path.exists(vars_dir): os.makedirs(vars_dir) vars_file_name = 'all.yml' vars_file_path = os.path.join(vars_dir, vars_file_name) with open(vars_file_path, 'a') as stream: dump(main_vars, stream)
def test_objdict_to_dict(): base = ObjDict({ 'field1': ObjDict({'field2': ObjDict({'field3': ObjDict({'field4': 'val'})})}) }) converted = objdict_to_dict(base) assert type(converted) is dict assert type(converted['field1']) is dict assert type(converted['field1']['field2']) is dict assert type(converted['field1']['field2']['field3']) is dict assert type(converted['field1']['field2']['field3']['field4']) is str assert converted['field1']['field2']['field3']['field4'] == 'val'
def populate_group_vars(self, ansible_dir): main_vars = ObjDict() main_vars['admin_user'] = self.cluster_model.specification.admin_user main_vars['validate_certs'] = Config().validate_certs main_vars['offline_requirements'] = Config().offline_requirements main_vars['wait_for_pods'] = Config().wait_for_pods shared_config_doc = select_first(self.config_docs, lambda x: x.kind == 'configuration/shared-config') if shared_config_doc == None: shared_config_doc = load_yaml_obj(types.DEFAULT, 'common', 'configuration/shared-config') main_vars.update(shared_config_doc.specification) vars_dir = os.path.join(ansible_dir, 'group_vars') if not os.path.exists(vars_dir): os.makedirs(vars_dir) vars_file_name = 'all.yml' vars_file_path = os.path.join(vars_dir, vars_file_name) with open(vars_file_path, 'a') as stream: dump(main_vars, stream)
def populate_group_vars(self, ansible_dir): main_vars = ObjDict() main_vars = self.add_admin_user_name(main_vars) main_vars = self.add_validate_certs(main_vars) vars_dir = os.path.join(ansible_dir, 'group_vars') if not os.path.exists(vars_dir): os.makedirs(vars_dir) vars_file_name = 'all.yml' vars_file_path = os.path.join(vars_dir, vars_file_name) with open(vars_file_path, 'w') as stream: dump(main_vars, stream)
def test_dict_to_objdict_different_dict_types(): base = { 'field1': ObjDict({'field2': { 'field3': OrderedDict({'field4': 'val'}) }}) } converted = dict_to_objdict(base) assert type(converted) is ObjDict assert type(converted.field1) is ObjDict assert type(converted.field1.field2) is ObjDict assert type(converted.field1.field2.field3) is ObjDict assert type(converted.field1.field2.field3.field4) is str assert converted.field1.field2.field3.field4 == 'val'
def nested_dict_to_objdict(d): for k, v in d.items(): if isinstance(v, dict): nested_dict_to_objdict(v) d[k] = ObjDict(v)
def dict_to_objdict(d): dc = deepcopy(d) nested_dict_to_objdict(dc) return ObjDict(dc)
from cli.helpers.ObjDict import ObjDict import pytest obj = ObjDict({'read': 1, 'set_existing_field': 1, 'delete_existing_field': 1}) def test_objdict_access_existing_field(): assert obj.read == 1 assert obj['read'] == 1 def test_objdict_access_non_existing_field_exception(): with pytest.raises(AttributeError) as excinfo: obj.non_existing assert 'No such attribute: non_existing' in str(excinfo.value) def test_objdict_set_existing_field(): assert obj.set_existing_field == 1 assert obj['set_existing_field'] == 1 obj.set_existing_field = 2 assert obj.set_existing_field == 2 assert obj['set_existing_field'] == 2 def test_objdict_set_non_existing_field_as_member(): with pytest.raises(AttributeError) as excinfo: obj.set_non_existing_field_as_member assert 'No such attribute: set_non_existing_field_as_member' in str(
import pytest from cli.helpers.doc_list_helpers import select_first, select_all from cli.helpers.doc_list_helpers import select_single, ExpectedSingleResultException from cli.helpers.ObjDict import ObjDict DATA = [ ObjDict({ 'index': 1, 'name': 'test-name-1' }), ObjDict({ 'index': 2, 'name': 'test-name23' }), ObjDict({ 'index': 3, 'name': 'test-name23' }) ] def test_select_first_should_return_first_matching_element(): actual = select_first(DATA, lambda item: item.name == 'test-name-1') assert (actual.index == 1) def test_select_first_should_return_first_matching_element_when_many_elements_matching( ):