def test_run_override_main(self): self.populate_file( 'main.yaml', '''--- variables: id: '{{ RANDOM_STR }}' include: - file: one.yaml as: one steps: - echo: {from: '{{ id }}', to: main.output} - run: include: one ''') self.populate_file( 'one.yaml', '''--- variables: id: 1234 steps: - echo: {from: '{{ id }}', to: one.output} ''') runner = Runner(self.test_dir, join(self.test_dir, 'main.yaml'), None) runner.run_tests() main = read_file(join(self.test_dir, 'main.output')) one = read_file(join(self.test_dir, 'one.output')) self.assertEqual(main, one) self.assertTrue(one)
def test_template_in_template_resource(self, m): self.populate_file( 'main.yaml', '''--- variables: bar: '{{ RANDOM_STR }}' include: - file: one.yaml as: one steps: - echo: {from: '{{ bar }}', to: main.output} - run: one ''') self.populate_file( 'one.yaml', '''--- variables: foo: '{{ bar }}' steps: - http: post: url: 'http://test.com' files: file: 'foo.json' ''') self.populate_resource('foo.json', "{\"key\":\"{{ foo }}\"}") adapter = m.post('http://test.com') runner = Runner(self.test_dir, join(self.test_dir, 'main.yaml'), None) self.assertTrue(runner.run_tests()) main = read_file(join(self.test_dir, 'main.output')) self.assertTrue("{\"key\":\"" + main + "\"}" in adapter.last_request.text)
def form_body(message, file, variables): data = message if data is None: if file is None: raise ValueError('Either data or data_from_file must be set.') data = read_file( join(variables['RESOURCES_DIR'], fill_template_str(file, variables))) return fill_template_str(data, variables)
def __prepare_file(file: dict, variables: dict): resources = variables['RESOURCES_DIR'] [file_key] = [k for k in file.keys() if k != 'type'] filepath = file[file_key] file_type = file.get('type', 'text/plain') filename = file_utils.get_filename(filepath) file = fill_template_str( file_utils.read_file(os.path.join(resources, filepath)), variables) return file_key, (filename, file, file_type)
def execute(self, body: dict, variables: dict): in_data = body['request'] conf = in_data['conf'] sql = in_data.get('sql', in_data.get('query')) if sql is None: raise Exception('Either sql or query param is required') if sql.endswith('.sql'): sql = fill_template_str( read_file(join(variables['RESOURCES_DIR'], sql)), variables) return self.__execute(conf, sql)
def __form_body(self, variables) -> str or dict: if self.method == 'get': return False, None body = self.body if body is None: body = read_file(fill_template_str(self.file, variables)) if isinstance(body, dict): # dump body to json to be able fill templates in body = json.dumps(body) isjson = 'tojson' in body return isjson, fill_template(body, variables, isjson=isjson)
def test_run_register(self): self.populate_file( 'main.yaml', '''--- steps: - echo: {from: 'user-{{ RANDOM_STR }}', register: {uuid: '{{ OUTPUT }}'}} - echo: {from: '{{ uuid }}', to: main.output} ''') runner = Runner(self.test_dir, join(self.test_dir, 'main.yaml'), None) runner.run_tests() file = read_file(join(self.test_dir, 'main.output')) self.assertTrue(file.startswith('user')) self.assertTrue('{{' not in file) # template was filled in
def test_vars_included_filled_in(self): self.populate_file('main.yaml', '''--- include: - file: one.yaml as: one steps: - echo: {from: '{{ RANDOM_STR }}', to: main.output, register: {uuid: '{{ OUTPUT }}'}} - run: include: one variables: id: '{{ uuid }}' ''') self.populate_file('one.yaml', '''--- steps: - echo: {from: '{{ id }}', to: one.output} ''') runner = Runner(self.test_dir, join(self.test_dir, 'main.yaml'), None) runner.run_tests() main = read_file(join(self.test_dir, 'main.output')) one = read_file(join(self.test_dir, 'one.output')) self.assertEqual(main, one) self.assertTrue(one)
def test_template_in_template(self): self.populate_file( 'main.yaml', '''--- variables: bar: '{{ RANDOM_STR }}' include: - file: one.yaml as: one steps: - echo: {from: '{{ bar }}', to: main.output} - run: one ''') self.populate_file( 'one.yaml', '''--- variables: foo: '{{ bar }}' include: - file: two.yaml as: two steps: - echo: {from: '{{ foo }}', to: one.output} - run: two ''') self.populate_file( 'two.yaml', '''--- variables: final: '{{ foo }}' steps: - echo: {from: '{{ final }}', to: two.output} ''') runner = Runner(self.test_dir, join(self.test_dir, 'main.yaml'), None) runner.run_tests() main = read_file(join(self.test_dir, 'main.output')) one = read_file(join(self.test_dir, 'one.output')) two = read_file(join(self.test_dir, 'two.output')) self.assertEqual(main, one) self.assertEqual(main, two)
def action(self, includes: dict, variables: dict) -> tuple: if self.source_file: # read from file resources = variables['RESOURCES_DIR'] out = fill_template_str( read_file(os.path.join(resources, self.source_file)), variables) else: out = fill_template(self.source, variables) if self.dst is None: info(out) else: dst = fill_template(self.dst, variables) with open(join(self.path, dst), 'w') as f: f.write(str(out)) return variables, out
def test_variables_registered_in_include(self): self.populate_file( 'main.yaml', '''--- include: - file: one.yaml as: one steps: - run: one - echo: {from: '{{ generated_email }}', to: two.output} ''') self.populate_file( 'one.yaml', '''--- variables: generated_email: '{{ random("email") }}' steps: - echo: {from: '{{ generated_email }}', to: one.output} ''') runner = Runner(self.test_dir, join(self.test_dir, 'main.yaml'), None) runner.run_tests() two = read_file(join(self.test_dir, 'two.output')) one = read_file(join(self.test_dir, 'one.output')) self.assertEqual(two, one) self.assertNotEqual('{{ random("email") }}', one)
def __form_body(self, variables) -> tuple: if self.method == 'get': return False, None body = self.body if body is None and self.file is not None: resources = variables['RESOURCES_DIR'] body = file_utils.read_file( fill_template_str(os.path.join(resources, self.file), variables)) if isinstance(body, dict) or isinstance( body, list): # dump body to json to be able fill templates in body = json.dumps(body) if body is None: return False, None isjson = 'tojson' in body return isjson, fill_template(body, variables, isjson=isjson)
def action(self, includes: dict, variables: dict) -> tuple: if self.source_file: # read from file resources = variables['RESOURCES_DIR'] out = fill_template_str(read_file(join(resources, fill_template_str(self.source_file, variables))), variables) else: out = fill_template(self.source, variables) if self.dst is None: info(out) else: dst = fill_template(self.dst, variables) path = fill_template(self.path, variables) filename = join(path, dst) file_utils.ensure_dir(os.path.dirname(os.path.abspath(filename))) with open(filename, 'w') as f: f.write(str(out)) return variables, out
def __form_body(self, variables): data = self.data if data is None: data = read_file(fill_template_str(self.file, variables)) return fill_template_str(data, variables)