def go(self, engine): """ Lists information registered at the highest level of fittings plan :param engine: the plumbery engine itself :type engine: :class:`plumbery.PlumberyEngine` """ self.engine = engine self.information = [] environment = PlumberyContext(context=self.engine) lines = [] for line in engine.information: tokens = line.split(' ') if tokens[0] == 'echo': tokens.pop(0) message = ' '.join(tokens) message = PlumberyText.expand_string(message, environment) lines.append(message) if len(lines) < 1: return self.information.append("About this fittings plan:") for line in lines: self.information.append("- {}".format(line))
def test_bad_string(self): self.text.expand_string(1234, {}) self.text.expand_string({}, {}) template = 'little {{ test with multiple {{test and {{ as well' context = PlumberyContext(dictionary={'test': 'toast'}) self.assertEqual(self.text.expand_string(template, context), template) template = 'little {{ }} test and {{ }} as well' context = PlumberyContext(dictionary={'test': 'toast'}) self.assertEqual(self.text.expand_string(template, context), template) template = "{{ secret.is.unknown }}" context = PlumberyContext(dictionary={'test': 'toast'}) self.assertEqual(self.text.expand_string(template, context), template)
def test_input2(self): context = PlumberyContext(dictionary={}) transformed = yaml.load(self.text.expand_string(input2, context)) unmatched = { o: (input2[o], transformed[o]) for o in input2.keys() if input2[o] != transformed[o] } if unmatched != {}: print(unmatched) self.assertEqual(len(unmatched), 0) context = PlumberyContext(dictionary={'node.private': '12.34.56.78'}) transformed = yaml.load(self.text.expand_string(input2, context)) unmatched = { o: (expected2[o], transformed[o]) for o in expected2.keys() if expected2[o] != transformed[o] } if unmatched != {}: print(unmatched) self.assertEqual(len(unmatched), 0)
def test_bad_parameters(self): with self.assertRaises(TypeError): self.text.expand_parameters(1234, {}) with self.assertRaises(TypeError): self.text.expand_parameters({}, {}) template = 'little {{ test with multiple {{test and {{ as well' context = PlumberyContext(dictionary={'test': 'toast'}) self.assertEqual(self.text.expand_parameters(template, context), template) template = 'little {{ }} test and {{ }} as well' context = PlumberyContext(dictionary={'test': 'toast'}) self.assertEqual(self.text.expand_parameters(template, context), template) template = "{{ parameter.is.unknown }}" context = PlumberyContext(dictionary={'test': 'toast'}) with self.assertRaises(KeyError): self.text.expand_parameters(template, context)
def test_engine(self): engine = PlumberyEngine() context = PlumberyContext(context=engine) template = "we are running plumbery {{ plumbery.version }}" expected = "we are running plumbery " + __version__ self.assertEqual(self.text.expand_string(template, context), expected) engine.set_user_name('fake_name') engine.set_user_password('fake_password') template = "{{ name.credentials }} {{ password.credentials }}" expected = engine.get_user_name() + " " + engine.get_user_password() self.assertEqual(self.text.expand_string(template, context), expected)
def move_to(self, facility): """ Lists information registered in one document of fittings plan :param facility: a target facility :type facility: :class:`plumbery.PlumberyFacility` This function is called once for each facility that is visited during the polishing process. You can override it for any specific initialisation that you would require. """ self.facility = facility environment = PlumberyContext(context=self.facility) if ('information' in facility.settings and isinstance(facility.settings['information'], str)): facility.settings['information'] = \ facility.settings['information'].strip('\n').split('\n') lines = [] if ('information' in facility.settings and isinstance(facility.settings['information'], list) and len(facility.settings['information']) > 0): for line in facility.settings['information']: tokens = line.split(' ') if tokens[0] == 'echo': tokens.pop(0) message = ' '.join(tokens) message = PlumberyText.expand_string( message, environment) lines.append(message) if len(lines) < 1: return self.information.append("About '{}':".format( facility.get_location_id())) for line in lines: self.information.append("- {}".format(line))
def shine_container(self, container): """ Lists information registered at the container level :param container: the container to be polished :type container: :class:`plumbery.PlumberyInfrastructure` """ environment = PlumberyNodeContext(node=None, container=container, context=self.facility) if ('information' in container.blueprint.keys() and isinstance(container.blueprint['information'], str)): container.blueprint['information'] = \ container.blueprint['information'].strip('\n').split('\n') lines = [] if ('information' in container.blueprint.keys() and isinstance(container.blueprint['information'], list) and len(container.blueprint['information']) > 0): for line in container.blueprint['information']: tokens = line.split(' ') if tokens[0] == 'echo': tokens.pop(0) message = ' '.join(tokens) message = PlumberyText.expand_string(message, environment) lines.append(message) if len(lines) > 0: self.information.append("About '{}':".format( container.blueprint['target'])) for line in lines: self.information.append("- {}".format(line)) if 'balancers' in container.blueprint.keys(): for item in container.blueprint['balancers']: if isinstance(item, dict): label = list(item)[0] settings = item[label] else: label = str(item) settings = {} if 'information' not in settings: continue name = container.name_balancer(label, settings) balancer = container._get_balancer(name) print(balancer) context = { 'balancer.name': name, 'balancer.ip': balancer.ip, 'balancer.port': balancer.port, } environment = PlumberyContext(dictionary=context, context=self.facility) lines = [] for line in settings['information']: message = PlumberyText.expand_string(line, environment) lines.append(message) if len(lines) > 0: self.information.append("About '{}':".format(name)) for line in lines: self.information.append("- {}".format(line))
def test_input10(self): loaded = yaml.load(input10) context = PlumberyContext(dictionary={}) expanded = self.text.expand_string(loaded, context) self.assertEqual(expanded.strip(), input10.strip())
def test_input9(self): loaded = yaml.load(input9) context = PlumberyContext(context=PlumberyEngine()) expanded = self.text.expand_string(loaded, context) self.assertEqual((' - |' in expanded), False)
def test_input7(self): loaded = yaml.load(input7) context = PlumberyContext(dict7) transformed = self.text.expand_string(loaded, context) self.assertEqual(transformed.strip(), expected7.strip())
def test_input5(self): loaded = yaml.load(input5) context = PlumberyContext(dictionary={}) transformed = yaml.load(self.text.expand_string(loaded, context)) self.assertEqual(transformed, loaded)
def test_input3(self): loaded = yaml.load(input3) context = PlumberyContext(dictionary={'node.public': '12.34.56.78'}) transformed = yaml.load(self.text.expand_string(loaded, context)) self.assertEqual(transformed, yaml.load(expected3))
def test_input1(self): context = PlumberyContext(dictionary={'node.private': '12.34.56.78'}) self.assertEqual(self.text.expand_string(input1, context), expected1)
def test_dictionary(self): template = 'little {{ test }} with multiple {{test}} and {{}} as well' context = PlumberyContext(dictionary={'test': 'toast'}) expected = 'little toast with multiple toast and {{}} as well' self.assertEqual(self.text.expand_string(template, context), expected)