Example #1
0
def test_arrayitem():
    node = parse('myval[0]')
    assert node == ('S', {
        '_children': [('L', ('R', {
            'module': None,
            'name': 'myval',
            'index': ('C', 0)
        }), 1)]
    })

    node = parse('myval[ 10 ]')
    assert node == ('S', {
        '_children': [('L', ('R', {
            'module': None,
            'name': 'myval',
            'index': ('C', 10)
        }), 1)]
    })

    node = parse('myval.asdf[ 10 ]')
    assert node == ('S', {
        '_children': [('L', ('R', {
            'module': 'myval',
            'name': 'asdf',
            'index': ('C', 10)
        }), 1)]
    })
Example #2
0
def test_maps():
    node = parse('{}')
    assert node == ('S', {'_children': [('L', ('M', {}), 1)]})

    node = parse('{ }')
    assert node == ('S', {'_children': [('L', ('M', {}), 1)]})

    node = parse('{asdf=10}')
    assert node == ('S', {'_children': [('L', ('M', {'asdf': ('C', 10)}), 1)]})

    node = parse('{ asdf = 10 }')
    assert node == ('S', {'_children': [('L', ('M', {'asdf': ('C', 10)}), 1)]})

    node = parse('{ asdf = "10" }')
    assert node == ('S', {
        '_children': [('L', ('M', {
            'asdf': ('C', '10')
        }), 1)]
    })

    node = parse('{ asdf = "10", qwerty = 10 }')
    assert node == ('S', {
        '_children': [('L', ('M', {
            'asdf': ('C', '10'),
            'qwerty': ('C', 10)
        }), 1)]
    })

    with pytest.raises(ParserError):
        parse('{ asdf: "10" }')

    with pytest.raises(ParserError):
        parse('{ "10" }')
Example #3
0
def test_not():
    node = parse('False')
    assert node == ('S', {'_children': [('L', ('C', False), 1)]})

    node = parse('not False')
    assert node == ('S', {
        '_children': [('L', ('X', {
            'operator': 'not',
            'left': ('C', False),
            'right': ('C', None)
        }), 1)]
    })

    node = parse('Not True')
    assert node == ('S', {
        '_children': [('L', ('X', {
            'operator': 'not',
            'left': ('C', True),
            'right': ('C', None)
        }), 1)]
    })

    node = parse('not adsf')
    assert node == ('S', {
        '_children': [('L', ('X', {
            'operator': 'not',
            'left': ('V', {
                'name': 'adsf',
                'module': None
            }),
            'right': ('C', None)
        }), 1)]
    })

    node = parse('not asdf.wert')
    assert node == ('S', {
        '_children': [('L', ('X', {
            'operator': 'not',
            'left': ('V', {
                'name': 'wert',
                'module': 'asdf'
            }),
            'right': ('C', None)
        }), 1)]
    })
Example #4
0
def test_block_paramaters():
    node = parse('begin( )\nend')
    assert node == ('S', {'_children': [('L', ('S', {'_children': []}), 1)]})

    node = parse('begin( )end')
    assert node == ('S', {'_children': [('L', ('S', {'_children': []}), 1)]})

    node = parse('begin( description="test" )\nend')
    assert node == ('S', {
        '_children': [('L', ('S', {
            '_children': [],
            'description': 'test'
        }), 1)]
    })

    node = parse('begin( description="" )\nend')
    assert node == ('S', {
        '_children': [('L', ('S', {
            '_children': [],
            'description': ''
        }), 1)]
    })

    node = parse('begin( description="test" )end')
    assert node == ('S', {
        '_children': [('L', ('S', {
            '_children': [],
            'description': 'test'
        }), 1)]
    })

    node = parse('begin( description="test", expected_time=12:34 )end')
    assert node == ('S', {
        '_children': [('L', ('S', {
            '_children': [],
            'description': 'test',
            'expected_time': timedelta(minutes=12, seconds=34)
        }), 1)]
    })

    node = parse(
        'begin( description="test", expected_time=12:34, max_time=1:00:00 )end'
    )
    assert node == ('S', {
        '_children': [('L', ('S', {
            '_children': [],
            'description': 'test',
            'expected_time': timedelta(minutes=12, seconds=34),
            'max_time': timedelta(hours=1)
        }), 1)]
    })
Example #5
0
def test_comment():
    node = parse('#stuff')
    assert node == ('S', {'_children': []})

    node = parse('\n#stuff')
    assert node == ('S', {'_children': []})

    node = parse('#stuff\n')
    assert node == ('S', {'_children': []})

    node = parse('#stuff\n#other#stuff')
    assert node == ('S', {'_children': []})

    node = parse('3 #stuff')
    assert node == ('S', {'_children': [('L', ('C', 3), 1)]})

    node = parse('3#stuff')
    assert node == ('S', {'_children': [('L', ('C', 3), 1)]})
Example #6
0
def test_arrays():
    node = parse('[]')
    assert node == ('S', {'_children': [('L', ('Y', []), 1)]})

    node = parse('[ ]')
    assert node == ('S', {'_children': [('L', ('Y', []), 1)]})

    node = parse('[1]')
    assert node == ('S', {'_children': [('L', ('Y', [('C', 1)]), 1)]})

    node = parse('[ 1 ]')
    assert node == ('S', {'_children': [('L', ('Y', [('C', 1)]), 1)]})

    node = parse('[ 1, 2 ]')
    assert node == ('S', {
        '_children': [('L', ('Y', [('C', 1), ('C', 2)]), 1)]
    })

    node = parse('[1,2]')
    assert node == ('S', {
        '_children': [('L', ('Y', [('C', 1), ('C', 2)]), 1)]
    })

    node = parse('[ 1, 2, "asdf", myval, callme() ]')
    assert node == ('S', {
        '_children': [('L', ('Y', [('C', 1), ('C', 2), ('C', "asdf"),
                                   ('V', {
                                       'module': None,
                                       'name': 'myval'
                                   }),
                                   ('F', {
                                       'module': None,
                                       'name': 'callme',
                                       'paramaters': {}
                                   })]), 1)]
    })
Example #7
0
def test_begin():
    node = parse('')
    assert node == ('S', {'_children': []})

    node = parse(' ')
    assert node == ('S', {'_children': []})

    node = parse('\n')
    assert node == ('S', {'_children': []})

    node = parse(' \n')
    assert node == ('S', {'_children': []})

    node = parse('\n\n')
    assert node == ('S', {'_children': []})

    node = parse('10')
    assert node == ('S', {'_children': [('L', ('C', 10), 1)]})

    node = parse('10\n42')
    assert node == ('S', {
        '_children': [('L', ('C', 10), 1), ('L', ('C', 42), 2)]
    })

    node = parse('10\n  42  \n21')
    assert node == ('S', {
        '_children': [('L', ('C', 10), 1), ('L', ('C', 42), 2),
                      ('L', ('C', 21), 3)]
    })

    node = parse('begin()end')
    assert node == ('S', {'_children': [('L', ('S', {'_children': []}), 1)]})

    node = parse('begin()\nend')
    assert node == ('S', {'_children': [('L', ('S', {'_children': []}), 1)]})

    node = parse('begin( )\nend')
    assert node == ('S', {'_children': [('L', ('S', {'_children': []}), 1)]})

    with pytest.raises(ParseError) as e:
        node = parse('begin()')

    assert str(e.value) == 'ParseError, line: 1, column: 1, "Incomplete Parse"'

    assert lint('begin()') == 'Incomplete Parsing on line: 1 column: 1'
    assert lint('begin()end') is None
    assert lint('') is None

    node = parse('begin()\n10\nend')
    assert node == ('S', {
        '_children': [('L', ('S', {
            '_children': [('L', ('C', 10), 2)]
        }), 1)]
    })

    node = parse('begin()\n10\nbegin()end\nend')
    assert node == ('S', {
        '_children': [('L', ('S', {
            '_children': [('L', ('C', 10), 2),
                          ('L', ('S', {
                              '_children': []
                          }), 3)]
        }), 1)]
    })

    node = parse('begin()\n10\nbegin()\n42\nend\nend')
    assert node == ('S', {
        '_children': [('L', ('S', {
            '_children': [('L', ('C', 10), 2),
                          ('L', ('S', {
                              '_children': [('L', ('C', 42), 4)]
                          }), 3)]
        }), 1)]
    })
Example #8
0
def test_ifelse():
    with pytest.raises(ParseError) as e:
        node = parse('if True 10')

    assert str(e.value) == 'ParseError, line: 1, column: 1, "Incomplete Parse"'

    assert lint('if True 10') == 'Incomplete Parsing on line: 1 column: 1'

    node = parse('if True then 10')
    assert node == ('S', {
        '_children': [('L', ('I', [{
            'condition': ('C', True),
            'expression': ('C', 10)
        }]), 1)]
    })

    node = parse('if True then 10 else 42')
    assert node == ('S', {
        '_children': [('L', ('I', [{
            'condition': ('C', True),
            'expression': ('C', 10)
        }, {
            'condition': None,
            'expression': ('C', 42)
        }]), 1)]
    })

    node = parse('if True then\n10\nelse\n42')
    assert node == ('S', {
        '_children': [('L', ('I', [{
            'condition': ('C', True),
            'expression': ('C', 10)
        }, {
            'condition': None,
            'expression': ('C', 42)
        }]), 1)]
    })

    with pytest.raises(ParseError) as e:
        node = parse('if True then 10 elif False 200 else 42')

    assert str(e.value) == 'ParseError, line: 1, column: 1, "Incomplete Parse"'

    assert lint('if True then 10 elif False 200 else 42'
                ) == 'Incomplete Parsing on line: 1 column: 1'

    node = parse('if True then 10 elif False then 200 else 42')
    assert node == ('S', {
        '_children': [('L', ('I', [{
            'condition': ('C', True),
            'expression': ('C', 10)
        }, {
            'condition': ('C', False),
            'expression': ('C', 200)
        }, {
            'condition': None,
            'expression': ('C', 42)
        }]), 1)]
    })

    node = parse('if True then\n10\nelif False then\n200\nelse\n42')
    assert node == ('S', {
        '_children': [('L', ('I', [{
            'condition': ('C', True),
            'expression': ('C', 10)
        }, {
            'condition': ('C', False),
            'expression': ('C', 200)
        }, {
            'condition': None,
            'expression': ('C', 42)
        }]), 1)]
    })

    node = parse(
        'if True then 10 elif ( asdf == "a" ) then 100 elif False then 200 else 42'
    )
    assert node == ('S', {
        '_children': [('L', ('I', [{
            'condition': ('C', True),
            'expression': ('C', 10)
        }, {
            'condition': ('X', {
                'left': ('V', {
                    'module': None,
                    'name': 'asdf'
                }),
                'operator': '==',
                'right': ('C', 'a')
            }),
            'expression': ('C', 100)
        }, {
            'condition': ('C', False),
            'expression': ('C', 200)
        }, {
            'condition': None,
            'expression': ('C', 42)
        }]), 1)]
    })

    node = parse(
        'if True then\n10\nelif ( asdf == "a" ) then\n100\nelif False then\n200\nelse\n42'
    )
    assert node == ('S', {
        '_children': [('L', ('I', [{
            'condition': ('C', True),
            'expression': ('C', 10)
        }, {
            'condition': ('X', {
                'left': ('V', {
                    'module': None,
                    'name': 'asdf'
                }),
                'operator': '==',
                'right': ('C', 'a')
            }),
            'expression': ('C', 100)
        }, {
            'condition': ('C', False),
            'expression': ('C', 200)
        }, {
            'condition': None,
            'expression': ('C', 42)
        }]), 1)]
    })

    node = parse(
        'if ( myobj.value >= "my string" ) then\nbegin( testing="this" )\n42\nend'
    )
    assert node == ('S', {
        '_children': [('L', ('I', [{
            'condition': ('X', {
                'left': ('V', {
                    'module': 'myobj',
                    'name': 'value'
                }),
                'operator': '>=',
                'right': ('C', "my string")
            }),
            'expression': ('S', {
                'testing': 'this',
                '_children': [('L', ('C', 42), 3)]
            }),
        }]), 1)]
    })

    node = parse(
        'if ( myobj.value >= "my string" ) then\nbegin( testing="this" )\n42\nend\nelse\nbegin( more="fun" )\n56\nend'
    )
    assert node == ('S', {
        '_children': [('L', ('I', [{
            'condition': ('X', {
                'left': ('V', {
                    'module': 'myobj',
                    'name': 'value'
                }),
                'operator': '>=',
                'right': ('C', "my string")
            }),
            'expression': ('S', {
                'testing': 'this',
                '_children': [('L', ('C', 42), 3)]
            }),
        }, {
            'condition':
            None,
            'expression': ('S', {
                'more': 'fun',
                '_children': [('L', ('C', 56), 7)]
            }),
        }]), 1)]
    })
Example #9
0
def test_while():
    with pytest.raises(ParseError) as e:
        node = parse('while True 10')

    assert str(e.value) == 'ParseError, line: 1, column: 1, "Incomplete Parse"'

    assert lint('while True 10') == 'Incomplete Parsing on line: 1 column: 1'

    node = parse('while true do 10')
    assert node == ('S', {
        '_children': [('L', ('W', {
            'condition': ('C', True),
            'expression': ('C', 10)
        }), 1)]
    })

    node = parse('while true do \n 10')
    assert node == ('S', {
        '_children': [('L', ('W', {
            'condition': ('C', True),
            'expression': ('C', 10)
        }), 1)]
    })

    node = parse('while true do \n\n 10')
    assert node == ('S', {
        '_children': [('L', ('W', {
            'condition': ('C', True),
            'expression': ('C', 10)
        }), 1)]
    })

    node = parse('while myval do 10')
    assert node == ('S', {
        '_children': [('L', ('W', {
            'condition': ('V', {
                'module': None,
                'name': 'myval'
            }),
            'expression': ('C', 10)
        }), 1)]
    })

    node = parse('while ( True == myval ) do 10')
    assert node == ('S', {
        '_children': [('L', ('W', {
            'condition': ('X', {
                'left': ('C', True),
                'operator': '==',
                'right': ('V', {
                    'module': None,
                    'name': 'myval'
                })
            }),
            'expression': ('C', 10)
        }), 1)]
    })

    node = parse('while myval do\nbegin()end')
    assert node == ('S', {
        '_children': [('L', ('W', {
            'condition': ('V', {
                'module': None,
                'name': 'myval'
            }),
            'expression': ('S', {
                '_children': []
            })
        }), 1)]
    })

    node = parse('while myval do\nbegin()\n10\nend')
    assert node == ('S', {
        '_children': [('L', ('W', {
            'condition': ('V', {
                'module': None,
                'name': 'myval'
            }),
            'expression': ('S', {
                '_children': [('L', ('C', 10), 3)]
            })
        }), 1)]
    })

    node = parse('while myval do\nbegin(thep=5)\n10\nend')
    assert node == ('S', {
        '_children': [('L', ('W', {
            'condition': ('V', {
                'module': None,
                'name': 'myval'
            }),
            'expression': ('S', {
                'thep': 5,
                '_children': [('L', ('C', 10), 3)]
            })
        }), 1)]
    })

    node = parse('continue')
    assert node == ('S', {'_children': [('L', ('O', 'continue'), 1)]})

    node = parse('break')
    assert node == ('S', {'_children': [('L', ('O', 'break'), 1)]})

    node = parse('while myval do\nbegin()\n10\ncontinue\nend')
    assert node == ('S', {
        '_children': [('L', ('W', {
            'condition': ('V', {
                'module': None,
                'name': 'myval'
            }),
            'expression': ('S', {
                '_children': [('L', ('C', 10), 3), ('L', ('O', 'continue'), 4)]
            })
        }), 1)]
    })
Example #10
0
def test_jumppoint():
    node = parse(':here')
    assert node == ('S', {'_children': [('L', ('J', 'here'), 1)]})

    node = parse('goto there')
    assert node == ('S', {'_children': [('L', ('G', 'there'), 1)]})
Example #11
0
def createJob(script_name, target, creator):
    if not creator:
        raise ForemanException('INVALID_CREATOR', 'creator is blank')

    target_class = _target_class(target)

    if isinstance(target,
                  Dependency) and script_name not in ('create', 'destroy'):
        raise ForemanException(
            'INVALID_SCRIPT',
            'Dependency Job can only have a create or destroy script_name')

    try:
        if getattr(target, JOB_LOOKUP_MAP[target_class]) is not None:
            raise ForemanException('JOB_EXISTS', 'target has an existing job')
    except ObjectDoesNotExist:
        pass

    if script_name == 'create':
        if target.state == 'built':
            raise ForemanException('ALLREADY_BUILT', 'target allready built')

        for item in DEPENDENCY_LOOKUP_MAP[target_class]:
            try:
                target_dependency = getattr(target, item)
            except ObjectDoesNotExist:
                target_dependency = None

            if target_dependency is not None:
                try:
                    dependency_job = getattr(
                        target_dependency,
                        JOB_LOOKUP_MAP[_target_class(target_dependency)])
                    if dependency_job.script_name != 'create':
                        raise ForemanException(
                            'JOB_EXISTS',
                            'target\'s dependency has an existing non-create job'
                        )
                except ObjectDoesNotExist:
                    pass

    elif script_name == 'destroy':
        if target.state != 'built':
            raise ForemanException('NOT_BUILT', 'target not built')

        for item in DEPENDENCY_LOOKUP_MAP[target_class]:
            try:
                target_dependency = getattr(target, item)
            except ObjectDoesNotExist:
                target_dependency = None

            if target_dependency is not None:
                try:
                    dependency_job = getattr(
                        target_dependency,
                        JOB_LOOKUP_MAP[_target_class(target_dependency)])
                    if dependency_job.script_name != 'destroy':
                        raise ForemanException(
                            'JOB_EXISTS',
                            'target\'s dependency has an existing non-destroy job'
                        )
                except ObjectDoesNotExist:
                    pass

                if target_dependency.state != 'built':  # this one should not happen, but just in case
                    raise ForemanException(
                        'NOT_BUILT',
                        'the supporting target to the target is not built')

    else:
        if target.state != 'built':
            raise ForemanException('NOT_BUILT', 'target not built')

    blueprint = target.blueprint

    obj_list = []
    if isinstance(target, Structure):
        job = StructureJob()
        job.structure = target
        obj_list.append(ROFoundationPlugin(target.foundation))
        obj_list.append(StructurePlugin(target))
        obj_list.append(ConfigPlugin(target))

    elif isinstance(target, Foundation):
        job = FoundationJob()
        job.foundation = target
        obj_list.append(FoundationPlugin(target))
        obj_list.append(ConfigPlugin(target))

    elif isinstance(target, Dependency):
        job = DependencyJob()
        job.dependency = target
        structure = None
        if target.script_structure:
            structure = target.script_structure
        elif target.structure is not None:
            structure = target.structure
        else:
            # dependency = target.dependency
            pass

        if structure is not None:
            # TODO: need a plugin to bring in the target foundation
            obj_list.append(ROFoundationPlugin(structure.foundation))
            obj_list.append(ROStructurePlugin(structure))
            obj_list.append(ConfigPlugin(structure))

        else:
            # TODO: this is not done, think this out, what dependancy vaultes need to be loaded
            # obj_list.append( ConfigPlugin( dependency ) )  ConfigPlugin does not support dependancy yet
            pass

    job.site = target.site

    script = blueprint.get_script(script_name)
    if script is None:
        script = '# empty place holder'

    runner = Runner(parse(script))
    for module in RUNNER_MODULE_LIST:
        runner.registerModule(module)

    for module in ('contractor.Foreman.runner_plugins.dhcp', ):
        runner.registerModule(module)

    for obj in obj_list:
        runner.registerObject(obj)

    job.state = 'waiting'
    job.script_name = script_name
    job.script_runner = pickle.dumps(runner)
    job.full_clean()
    job.save()

    JobLog.fromJob(job, creator)

    return job.pk
Example #12
0
def test_constants():
    node = parse('10')
    assert node == ('S', {'_children': [('L', ('C', 10), 1)]})

    node = parse('100')
    assert node == ('S', {'_children': [('L', ('C', 100), 1)]})

    node = parse('100000')
    assert node == ('S', {'_children': [('L', ('C', 100000), 1)]})

    node = parse('+120')
    assert node == ('S', {'_children': [('L', ('C', 120), 1)]})

    node = parse('-43')
    assert node == ('S', {'_children': [('L', ('C', -43), 1)]})

    node = parse('10.0')
    assert node == ('S', {'_children': [('L', ('C', 10.0), 1)]})

    node = parse('+120.3')
    assert node == ('S', {'_children': [('L', ('C', 120.3), 1)]})

    node = parse('-43.1')
    assert node == ('S', {'_children': [('L', ('C', -43.1), 1)]})

    node = parse('0.1')
    assert node == ('S', {'_children': [('L', ('C', 0.1), 1)]})

    node = parse('+0.12')
    assert node == ('S', {'_children': [('L', ('C', .12), 1)]})

    node = parse('-0.43')
    assert node == ('S', {'_children': [('L', ('C', -.43), 1)]})

    node = parse('true')
    assert node == ('S', {'_children': [('L', ('C', True), 1)]})

    node = parse('True')
    assert node == ('S', {'_children': [('L', ('C', True), 1)]})

    node = parse('false')
    assert node == ('S', {'_children': [('L', ('C', False), 1)]})

    node = parse('False')
    assert node == ('S', {'_children': [('L', ('C', False), 1)]})

    node = parse('0:12')
    assert node == ('S', {
        '_children': [('L', ('C', timedelta(minutes=0, seconds=12)), 1)]
    })

    node = parse('8:22')
    assert node == ('S', {
        '_children': [('L', ('C', timedelta(minutes=8, seconds=22)), 1)]
    })

    node = parse('2:5:43')
    assert node == ('S', {
        '_children': [('L', ('C', timedelta(hours=2, minutes=5,
                                            seconds=43)), 1)]
    })

    node = parse('9:21:10:01')
    assert node == ('S', {
        '_children':
        [('L', ('C', timedelta(days=9, hours=21, minutes=10, seconds=1)), 1)]
    })

    node = parse('"hello"')
    assert node == ('S', {'_children': [('L', ('C', 'hello'), 1)]})

    node = parse('"isn\'t"')
    assert node == ('S', {'_children': [('L', ('C', 'isn\'t'), 1)]})

    node = parse("'He said \"hi\"'")
    assert node == ('S', {'_children': [('L', ('C', 'He said "hi"'), 1)]})

    node = parse("'nice'")
    assert node == ('S', {'_children': [('L', ('C', 'nice'), 1)]})

    node = parse('none')
    assert node == ('S', {'_children': [('L', ('C', None), 1)]})

    node = parse('None')
    assert node == ('S', {'_children': [('L', ('C', None), 1)]})
Example #13
0
def test_variables():
    node = parse('myval')
    assert node == ('S', {
        '_children': [('L', ('V', {
            'module': None,
            'name': 'myval'
        }), 1)]
    })

    node = parse('myobj.somthing')
    assert node == ('S', {
        '_children': [('L', ('V', {
            'module': 'myobj',
            'name': 'somthing'
        }), 1)]
    })

    node = parse('myobj.somthing')
    assert node == ('S', {
        '_children': [('L', ('V', {
            'module': 'myobj',
            'name': 'somthing'
        }), 1)]
    })

    node = parse('good2go')
    assert node == ('S', {
        '_children': [('L', ('V', {
            'module': None,
            'name': 'good2go'
        }), 1)]
    })

    node = parse('good2go.go4win')
    assert node == ('S', {
        '_children': [('L', ('V', {
            'module': 'good2go',
            'name': 'go4win'
        }), 1)]
    })

    node = parse('var2')
    assert node == ('S', {
        '_children': [('L', ('V', {
            'module': None,
            'name': 'var2'
        }), 1)]
    })

    node = parse('mod1.var2')
    assert node == ('S', {
        '_children': [('L', ('V', {
            'module': 'mod1',
            'name': 'var2'
        }), 1)]
    })

    # all invalid names
    with pytest.raises(ParseError):
        node = parse('a')

    with pytest.raises(ParseError):
        node = parse('a.b')

    with pytest.raises(ParseError):
        node = parse('2a')

    with pytest.raises(ParseError):
        node = parse('adsf.2a')
Example #14
0
def test_exists():
    node = parse('exists( bob )')
    assert node == ('S', {
        '_children': [('L', ('E', ('V', {
            'module': None,
            'name': 'bob'
        })), 1)]
    })

    node = parse('exists( top.bottom )')
    assert node == ('S', {
        '_children': [('L', ('E', ('V', {
            'module': 'top',
            'name': 'bottom'
        })), 1)]
    })

    node = parse('exists(bob2)')
    assert node == ('S', {
        '_children': [('L', ('E', ('V', {
            'module': None,
            'name': 'bob2'
        })), 1)]
    })

    node = parse('exists( bob3)')
    assert node == ('S', {
        '_children': [('L', ('E', ('V', {
            'module': None,
            'name': 'bob3'
        })), 1)]
    })

    node = parse('exists(bob4 )')
    assert node == ('S', {
        '_children': [('L', ('E', ('V', {
            'module': None,
            'name': 'bob4'
        })), 1)]
    })

    node = parse('exists( bob[1] )')
    assert node == ('S', {
        '_children': [('L', ('E', ('R', {
            'index': ('C', 1),
            'module': None,
            'name': 'bob'
        })), 1)]
    })

    node = parse('exists( bob[gh] )')
    assert node == ('S', {
        '_children': [('L', ('E', ('R', {
            'index': ('V', {
                'module': None,
                'name': 'gh'
            }),
            'module':
            None,
            'name':
            'bob'
        })), 1)]
    })

    node = parse('aa = exists( bob )')
    assert node == ('S', {
        '_children': [('L', ('A', {
            'target': ('V', {
                'module': None,
                'name': 'aa'
            }),
            'value': ('E', ('V', {
                'module': None,
                'name': 'bob'
            }))
        }), 1)]
    })

    node = parse('if exists( bob ) then 10')
    assert node == ('S', {
        '_children': [('L', ('I', [{
            'condition': ('E', ('V', {
                'module': None,
                'name': 'bob'
            })),
            'expression': ('C', 10)
        }]), 1)]
    })
Example #15
0
def createJob(script_name, target):
    obj_list = []
    if isinstance(target, Structure):
        job = StructureJob()
        job.structure = target
        obj_list.append(ROFoundationPlugin(target.foundation))
        obj_list.append(StructurePlugin(target))
        obj_list.append(ConfigPlugin(target))

    elif isinstance(target, Foundation):
        try:
            StructureJob.objects.get(structure=target.structure)
            raise ValueError(
                'Structure associated with this Foundation has a job')
        except StructureJob.DoesNotExist:
            pass

        job = FoundationJob()
        job.foundation = target
        obj_list.append(FoundationPlugin(target))
        obj_list.append(ConfigPlugin(target))

    elif isinstance(target, Dependancy):
        if target.structure.state != 'built':
            raise ValueError(
                'Can not start Dependancy job until Structure is built')

        job = DependancyJob()
        job.dependancy = target
        obj_list.append(ROFoundationPlugin(target.foundation))
        obj_list.append(ROStructurePlugin(target.structure))
        obj_list.append(ConfigPlugin(target.structure))

    else:
        raise ValueError(
            'target must be a Structure, Foundation, or Dependancy')

    if script_name == 'create':
        if target.state == 'built':
            raise ValueError('target allready built')

        if isinstance(target, Foundation):
            if target.state != 'located':
                raise ValueError(
                    'can not do create job until Foundation is located')

    elif script_name == 'destroy':
        if target.state != 'built':
            raise ValueError('can only destroy built targets')

    else:
        if isinstance(target, Foundation) and target.state != 'located':
            raise ValueError(
                'can only run utility jobs on located Foundations')

    job.site = target.site

    runner = Runner(parse(target.blueprint.get_script(script_name)))
    for module in RUNNER_MODULE_LIST:
        runner.registerModule(module)

    for module in ('contractor.Foreman.runner_plugins.dhcp', ):
        runner.registerModule(module)

    for obj in obj_list:
        runner.registerObject(obj)

    job.state = 'queued'
    job.script_name = script_name
    job.script_runner = pickle.dumps(runner)
    job.save()

    print('**************** Created  Job "{0}" for "{1}"'.format(
        script_name, target))

    return job.pk
Example #16
0
def test_function():
    node = parse('hello()')
    assert node == ('S', {
        '_children': [('L', ('F', {
            'module': None,
            'name': 'hello',
            'paramaters': {}
        }), 1)]
    })

    node = parse('hello( )')
    assert node == ('S', {
        '_children': [('L', ('F', {
            'module': None,
            'name': 'hello',
            'paramaters': {}
        }), 1)]
    })

    node = parse('more.hello()')
    assert node == ('S', {
        '_children': [('L', ('F', {
            'module': 'more',
            'name': 'hello',
            'paramaters': {}
        }), 1)]
    })

    with pytest.raises(ParseError) as e:
        node = parse('hello( 10 )')

    assert str(e.value) == 'ParseError, line: 1, column: 1, "Incomplete Parse"'

    assert lint('hello( 10 )') == 'Incomplete Parsing on line: 1 column: 1'

    node = parse('hello( asdf = 10 )')
    assert node == ('S', {
        '_children': [('L', ('F', {
            'module': None,
            'name': 'hello',
            'paramaters': {
                'asdf': ('C', 10)
            }
        }), 1)]
    })

    node = parse('hello( asdf = "" )')
    assert node == ('S', {
        '_children': [('L', ('F', {
            'module': None,
            'name': 'hello',
            'paramaters': {
                'asdf': ('C', '')
            }
        }), 1)]
    })

    node = parse('hello(asdf=10)')
    assert node == ('S', {
        '_children': [('L', ('F', {
            'module': None,
            'name': 'hello',
            'paramaters': {
                'asdf': ('C', 10)
            }
        }), 1)]
    })

    node = parse('hello( asdf = 10, zxcv="hi", qwerty=123 )')
    assert node == ('S', {
        '_children': [('L', ('F', {
            'module': None,
            'name': 'hello',
            'paramaters': {
                'asdf': ('C', 10),
                'zxcv': ('C', 'hi'),
                'qwerty': ('C', 123)
            }
        }), 1)]
    })
Example #17
0
def test_infix():
    node = parse('( 1 + 2 )')
    assert node == ('S', {
        '_children': [('L', ('X', {
            'left': ('C', 1),
            'operator': '+',
            'right': ('C', 2)
        }), 1)]
    })

    node = parse('(1 + 2)')
    assert node == ('S', {
        '_children': [('L', ('X', {
            'left': ('C', 1),
            'operator': '+',
            'right': ('C', 2)
        }), 1)]
    })

    node = parse('( 1+2 )')
    assert node == ('S', {
        '_children': [('L', ('X', {
            'left': ('C', 1),
            'operator': '+',
            'right': ('C', 2)
        }), 1)]
    })

    node = parse('(1+2)')
    assert node == ('S', {
        '_children': [('L', ('X', {
            'left': ('C', 1),
            'operator': '+',
            'right': ('C', 2)
        }), 1)]
    })

    node = parse('(    1   +   2   )')
    assert node == ('S', {
        '_children': [('L', ('X', {
            'left': ('C', 1),
            'operator': '+',
            'right': ('C', 2)
        }), 1)]
    })

    node = parse('( ( 1 * 2 ) + 3 )')
    assert node == ('S', {
        '_children': [('L', ('X', {
            'left': ('X', {
                'left': ('C', 1),
                'operator': '*',
                'right': ('C', 2)
            }),
            'operator':
            '+',
            'right': ('C', 3)
        }), 1)]
    })

    node = parse('( 1 * ( 2 + 3 ) )')
    assert node == ('S', {
        '_children': [('L', ('X', {
            'left': ('C', 1),
            'operator':
            '*',
            'right': ('X', {
                'left': ('C', 2),
                'operator': '+',
                'right': ('C', 3)
            })
        }), 1)]
    })
Example #18
0
def test_assignment():
    with pytest.raises(ParseError) as e:
        node = parse('asdf=')

    assert str(e.value) == 'ParseError, line: 1, column: 1, "Incomplete Parse"'

    assert lint('asdf=') == 'Incomplete Parsing on line: 1 column: 1'

    with pytest.raises(ParseError) as e:
        node = parse('asdf =')

    assert str(e.value) == 'ParseError, line: 1, column: 1, "Incomplete Parse"'

    assert lint('asdf =') == 'Incomplete Parsing on line: 1 column: 1'

    node = parse('asdf=5')
    assert node == ('S', {
        '_children': [('L', ('A', {
            'target': ('V', {
                'module': None,
                'name': 'asdf'
            }),
            'value': ('C', 5)
        }), 1)]
    })

    node = parse('asdf = 5')
    assert node == ('S', {
        '_children': [('L', ('A', {
            'target': ('V', {
                'module': None,
                'name': 'asdf'
            }),
            'value': ('C', 5)
        }), 1)]
    })

    node = parse('asdf.fdsa = 5')
    assert node == ('S', {
        '_children': [('L', ('A', {
            'target': ('V', {
                'module': 'asdf',
                'name': 'fdsa'
            }),
            'value': ('C', 5)
        }), 1)]
    })

    node = parse('asdf = myfunc()')
    assert node == ('S', {
        '_children': [('L', ('A', {
            'target': ('V', {
                'module': None,
                'name': 'asdf'
            }),
            'value': ('F', {
                'module': None,
                'name': 'myfunc',
                'paramaters': {}
            })
        }), 1)]
    })

    node = parse('asdf = myval')
    assert node == ('S', {
        '_children': [('L', ('A', {
            'target': ('V', {
                'module': None,
                'name': 'asdf'
            }),
            'value': ('V', {
                'module': None,
                'name': 'myval'
            })
        }), 1)]
    })

    node = parse('asdf = not myval')
    assert node == ('S', {
        '_children': [('L', ('A', {
            'target': ('V', {
                'module': None,
                'name': 'asdf'
            }),
            'value': ('X', {
                'operator': 'not',
                'left': ('V', {
                    'module': None,
                    'name': 'myval'
                }),
                'right': ('C', None)
            })
        }), 1)]
    })

    node = parse('asdf[3] = myval')
    assert node == ('S', {
        '_children': [('L', ('A', {
            'target': ('R', {
                'module': None,
                'name': 'asdf',
                'index': ('C', 3)
            }),
            'value': ('V', {
                'module': None,
                'name': 'myval'
            })
        }), 1)]
    })
Example #19
0
def test_job_locking(mocker):
    global _from_can_continue, _to_can_continue, _process_jobs_can_finish

    mocker.patch(
        'contractor.tscript.runner_plugins_test.Remote.fromSubcontractor',
        _fake_fromSubcontractor)
    mocker.patch(
        'contractor.tscript.runner_plugins_test.Remote.toSubcontractor',
        _fake_toSubcontractor)

    with transaction.atomic():
        s = Site(name='test', description='test')
        s.full_clean()
        s.save()

        runner = Runner(parse('testing.remote()'))
        runner.registerModule('contractor.tscript.runner_plugins_test')
        job = BaseJob(site=s)
        job.state = 'queued'
        job.script_name = 'test'
        job.script_runner = pickle.dumps(runner)
        job.full_clean()
        job.save()

        j = BaseJob.objects.get()
        assert j.state == 'queued'
        assert j.jobRunnerState() == {'cur_line': 0, 'state': []}

    _to_can_continue = True
    _from_can_continue = True

    with transaction.atomic():
        assert processJobs(s, [], 10) == []

        j = BaseJob.objects.get()
        assert j.state == 'queued'
        assert j.jobRunnerState()['state'][2][1]['dispatched'] is False

    with transaction.atomic():
        cookie, rc = _stripcookie(processJobs(s, ['testing'], 10))
        assert rc == [{
            'function': 'remote_func',
            'job_id': 1,
            'module': 'testing',
            'paramaters': 'the count "2"'
        }]

    with transaction.atomic():
        j = BaseJob.objects.get()
        assert j.state == 'queued'
        assert j.jobRunnerState()['state'][2][1]['dispatched'] is True

    with transaction.atomic():
        assert processJobs(s, ['testing'], 10) == []

    with transaction.atomic():
        j = BaseJob.objects.get()
        assert j.state == 'queued'
        assert j.jobRunnerState()['state'][2][1]['dispatched'] is True

    with transaction.atomic():
        jobResults(rc[0]['job_id'], cookie, None)

    with transaction.atomic():
        j = BaseJob.objects.get()
        assert j.state == 'queued'
        assert j.jobRunnerState()['state'][2][1]['dispatched'] is False

    with transaction.atomic():
        cookie, rc = _stripcookie(processJobs(s, ['testing'], 10))
        assert rc == [{
            'function': 'remote_func',
            'job_id': 1,
            'module': 'testing',
            'paramaters': 'the count "4"'
        }]

    with transaction.atomic():
        j = BaseJob.objects.get()
        assert j.state == 'queued'
        assert j.jobRunnerState()['state'][2][1]['dispatched'] is True

    # first test, running status check during a statesave
    _from_can_continue = False
    t = threading.Thread(target=_do_jobResults,
                         args=(rc[0]['job_id'], cookie, None))
    try:
        t.start()
        time.sleep(0.5)

        with transaction.atomic():
            j = BaseJob.objects.get()
            assert j.state == 'queued'
            assert j.jobRunnerState()['state'][2][1]['dispatched'] is True

        time.sleep(0.5)
        _from_can_continue = True
        t.join()

    except Exception as e:
        _from_can_continue = True
        t.join()
        raise e

    with transaction.atomic():
        j = BaseJob.objects.get()
        assert j.state == 'queued'
        assert j.jobRunnerState()['state'][2][1]['dispatched'] is False

    # now we test intrupting the job checking with results, first during a slow toSubcontractor
    with transaction.atomic():
        cookie, rc = _stripcookie(processJobs(s, ['testing'], 10))
        assert rc == [{
            'function': 'remote_func',
            'job_id': 1,
            'module': 'testing',
            'paramaters': 'the count "5"'
        }]

    with transaction.atomic():
        j = BaseJob.objects.get()
        assert j.state == 'queued'
        assert j.jobRunnerState()['state'][2][1]['dispatched'] is True

    _to_can_continue = False
    _process_jobs_can_finish = True
    t = threading.Thread(target=_do_processJobs, args=(s, ['testing'], 10))
    try:
        t.start()
        time.sleep(0.5)

        with transaction.atomic():
            j = BaseJob.objects.get()
            assert j.state == 'queued'
            assert j.jobRunnerState()['state'][2][1]['dispatched'] is True

            jobResults(rc[0]['job_id'], cookie, None)

            j = BaseJob.objects.get()
            assert j.state == 'queued'
            assert j.jobRunnerState()['state'][2][1]['dispatched'] is False

        time.sleep(0.5)
        _to_can_continue = True
        t.join()

    except Exception as e:
        _to_can_continue = True
        t.join()
        raise e

    with transaction.atomic():
        j = BaseJob.objects.get()
        assert j.state == 'queued'
        assert j.jobRunnerState()['state'][2][1]['dispatched'] is False

    # then just before the transaction commits
    with transaction.atomic():
        cookie, rc = _stripcookie(processJobs(s, ['testing'], 10))
        assert rc == [{
            'function': 'remote_func',
            'job_id': 1,
            'module': 'testing',
            'paramaters': 'the count "7"'
        }]

    with transaction.atomic():
        j = BaseJob.objects.get()
        assert j.state == 'queued'
        assert j.jobRunnerState()['state'][2][1]['dispatched'] is True

    _to_can_continue = True
    _process_jobs_can_finish = False
    t = threading.Thread(target=_do_processJobs, args=(s, ['testing'], 10))
    try:
        t.start()
        time.sleep(0.5)

        with transaction.atomic():
            j = BaseJob.objects.get()
            assert j.state == 'queued'
            assert j.jobRunnerState()['state'][2][1]['dispatched'] is True

            t2 = threading.Thread(
                target=_do_jobResults, args=(
                    rc[0]['job_id'], cookie,
                    None))  # jobResults should block b/c the record is locked
            t2.start()

            j = BaseJob.objects.get()
            assert j.state == 'queued'
            assert j.jobRunnerState()['state'][2][1]['dispatched'] is True

        time.sleep(0.5)
        _process_jobs_can_finish = True

        t2.join()
        with transaction.atomic():
            j = BaseJob.objects.get()
            assert j.state == 'queued'
            assert j.jobRunnerState()['state'][2][1]['dispatched'] is False

        t.join()
        with transaction.atomic():
            j = BaseJob.objects.get()
            assert j.state == 'queued'
            assert j.jobRunnerState()['state'][2][1]['dispatched'] is False

    except Exception as e:
        _process_jobs_can_finish = True
        t.join()
        raise e

    with transaction.atomic():
        j = BaseJob.objects.get()
        assert j.state == 'queued'
        assert j.jobRunnerState()['state'][2][1]['dispatched'] is False

    # and finish up the job
    with transaction.atomic():
        cookie, rc = _stripcookie(processJobs(s, ['testing'], 10))
        assert rc == [{
            'function': 'remote_func',
            'job_id': 1,
            'module': 'testing',
            'paramaters': 'the count "9"'
        }]

    with transaction.atomic():
        jobResults(rc[0]['job_id'], cookie, 'adf')

    with transaction.atomic():
        assert processJobs(s, ['testing'], 10) == []

    with transaction.atomic():
        j = BaseJob.objects.get()
        assert j.state == 'queued'
        assert j.jobRunnerState() == {'cur_line': None, 'state': 'DONE'}