Exemplo n.º 1
0
    def test_python_module_with_json(self):
        self.write_module(
            'print.py', '''from catcher.steps.external_step import ExternalStep
from catcher.steps.step import update_variables

class Print(ExternalStep):

    @update_variables
    def action(self, includes: dict, variables: dict) -> (dict, str):
        body = self.simple_input(variables)
        data = body['the']
        return variables, 'data is: {}'.format(data)
                    ''')
        self.populate_file(
            'inventory.yml', '''---
                complex_conf:
                  field: 'value'
                  json_field: '{"host": "http://minio:9000","aws_access_key_id":"minio","aws_secret_access_key":"minio123"}'
                ''')
        self.populate_file(
            'main.yaml', '''---
                    steps:
                        - print:
                            the: '{{ complex_conf }}'
                    ''')
        load_external_actions(join(self.test_dir, 'print.py'))
        runner = Runner(self.test_dir,
                        join(self.test_dir, 'main.yaml'),
                        join(self.test_dir, 'inventory.yml'),
                        modules=[self.test_dir])
        self.assertTrue(runner.run_tests())
Exemplo n.º 2
0
def __load_modules(modules):
    """
    Load catcher core modules, carcher-modules extensions (if available) and try to load all modules from cmd args
    """
    load_external_actions('catcher.steps')
    load_external_actions('catcher_modules')
    [load_external_actions(m) for m in modules]
Exemplo n.º 3
0
 def _compose_arg(self, variables):
     """
     Search for the method in generated _pb2. Resolve input type by the method's input.
     """
     schema = fill_template(self.schema, variables)
     service = fill_template(self.service, variables)
     method = fill_template(self.method, variables)
     # search for the service
     mod = module_utils.load_external_actions(
         join(variables['RESOURCES_DIR'],
              file_utils.get_filename(schema) + '_pb2.py'))
     services = {
         str(k).lower(): v
         for k, v in mod.DESCRIPTOR.services_by_name.items()
     }
     service = services.get(service)
     if not service:
         raise Exception('Unable to find service {} in {}'.format(
             service,
             file_utils.get_filename(schema) + '_pb2'))
     # find service's method
     methods = dict([(f.name.lower(), f) for f in service.methods])
     method = methods.get(method)
     if not method:
         raise Exception('No method {} in service {}'.format(
             method, service))
     # find method's input type
     input_type = method.input_type
     classes = module_utils.get_all_classes(mod)
     data = try_get_objects(fill_template_str(self.data, variables))
     return method.name, classes[input_type.name](**data)
Exemplo n.º 4
0
    def test_python_module(self):
        self.write_module(
            'hello.py', '''from catcher.steps.external_step import ExternalStep
from catcher.steps.step import update_variables


class Hello(ExternalStep):
    """
    Very important and useful step. Says hello to input. Return as a string.
    Example usage.
    ::
        hello:
            say: 'John Doe'
            register: {greeting='{{ OUTPUT }}'}

    """
    @update_variables
    def action(self, includes: dict, variables: dict) -> (dict, str):
        body = self.simple_input(variables)
        person = body['say']
        return variables, 'hello {}'.format(person)
                ''')
        self.populate_file(
            'main.yaml', '''---
                variables:
                    person: 'John Doe'
                steps:
                    - hello:
                        say: '{{ person }}'
                        register: {greeting: '{{ OUTPUT }}'}
                    - check: {equals: {the: '{{ greeting }}', is: 'hello John Doe'}}
                ''')
        load_external_actions(join(self.test_dir, 'hello.py'))
        runner = Runner(self.test_dir,
                        join(self.test_dir, 'main.yaml'),
                        None,
                        modules=[self.test_dir])
        self.assertTrue(runner.run_tests())
Exemplo n.º 5
0
 def _open_channel(self, channel, variables):
     """
     Search for stub in generated module _pb2_grpc. Instantiate it with channel and return.
     """
     schema = fill_template(self.schema, variables)
     mod = module_utils.load_external_actions(
         join(variables['RESOURCES_DIR'],
              file_utils.get_filename(schema) + '_pb2_grpc.py'))
     classes = module_utils.get_all_classes(mod)
     classes = {k.lower(): v for k, v in classes.items()}
     stub = classes.get(fill_template(self.service, variables) + 'stub')
     if not stub:
         raise Exception(
             'Can\'t find stub in generated code. Something went wrong')
     return stub(channel)
Exemplo n.º 6
0
from os.path import join

from catcher.utils.module_utils import load_external_actions

TEST_DIR = 'test/tmp'

load_external_actions('catcher.steps')


def get_test_dir(testcase: str) -> str:
    return join(TEST_DIR, testcase)
Exemplo n.º 7
0
 def __enter__(self):
     resources = join(test.get_test_dir('grpc'), 'private_resource')
     mod = module_utils.load_external_actions(join(resources, self.server))
     self.server = mod.server
     self.server.start()
     return self