예제 #1
0
    def test_custom_template(self):

        template = '\n'.join([
            'concurrencyPolicy: Allow', 'namespace: test',
            'restartPolicy: Never',
            'image: 000000000000.dkr.ecr.us-east-1.amazonaws.com/non-existent-image',
            'labelKey: kronjob/job.test-template', 'memoryRequest: 4096Mi',
            'memoryLimit: 6144Mi', 'nodeSelector:', '  group: jobs', 'env:',
            '  - name: ENV', '    value: test'
        ])
        template_yaml = yaml.load(template, Loader=yaml.FullLoader)
        cronner = Cronner()
        cronner.configure(kronjob_template=template)

        @cronner.register('* * * * *')
        def fn():
            pass

        spec = yaml.load(cronner.get_entries(), Loader=yaml.FullLoader)
        self.assertListEqual(
            [spec[k] for k in sorted(template_yaml.keys())],
            [template_yaml[k] for k in sorted(template_yaml.keys())])
        self.assertEqual(spec['jobs'][0]['schedule'], '* * * * *')
        self.assertEqual(
            spec['jobs'][0]['name'],
            '{}.{}'.format(fn.__module__,
                           fn.__name__).lower().replace('_', '-').strip('-'))
        self.assertEqual(spec['jobs'][0]['command'], [
            sys.executable,
            os.path.abspath(sys.argv[0]), 'run', '{}.{}'.format(
                fn.__module__, fn.__name__)
        ])
예제 #2
0
    def test_custom_serializer(self):
        cronner = Cronner()
        cronner.configure(serializer=lambda _: 'custom_template')

        @cronner.register('* * * * *')
        def fn():
            pass

        line = cronner.get_entries()
        self.assertEqual(line, 'custom_template')
예제 #3
0
    def test_main_gen_cfg(self):
        cronner = Cronner()
        cronner.configure(serializer=lambda es: '\n'.join(
            '{}'.format(e['schedule']) for e in es))

        @cronner.register('* * * * *')
        def fn():
            pass

        with self.captureOutput(assert_stdout='* * * * *\n'):
            cronner.main(['gen-cfg'])
예제 #4
0
    def test_template_vars(self):
        cronner = Cronner()
        cronner.configure(
            serializer=lambda es: '\n'.join(e['var'] for e in es))

        @cronner.register('* * * * *', template_vars={'var': 'template_var'})
        def fn():
            pass

        line = cronner.get_entries()
        self.assertEqual(line, 'template_var')
예제 #5
0
    def test_template_vars_custom_template(self):
        template = '\n'.join([
            'concurrencyPolicy: Allow', 'namespace: test',
            'restartPolicy: Never',
            'image: 000000000000.dkr.ecr.us-east-1.amazonaws.com/non-existent-image',
            'labelKey: kronjob/job.test-template', 'memoryRequest: 4096Mi',
            'memoryLimit: 6144Mi', 'nodeSelector:', '  group: jobs', 'env:',
            '  - name: ENV', '    value: test'
        ])
        template_yaml = yaml.load(template, Loader=yaml.FullLoader)
        cronner = Cronner()
        cronner.configure(kronjob_template=template)

        @cronner.register('* * * * *', template_vars={'foo': 'bar'})
        def fn():
            pass

        spec = yaml.load(cronner.get_entries(), Loader=yaml.FullLoader)
        self.assertEqual(spec['jobs'][0]['foo'], 'bar')