Пример #1
0
def test_updateConfig_foundation(mocker):
    mocker.patch('contractor.Records.lib._connect', fake_connect)
    global fake_key
    global fake_item

    s = Site()
    s.name = 'site_test'
    s.description = 'testing Site'
    s.full_clean()
    s.save()

    fb = FoundationBluePrint()
    fb.name = 'fdnbp_test'
    fb.description = 'testing FBP'
    fb.foundation_type_list = ['Unknown']
    fb.full_clean()
    fb.save()

    fake_key = None
    fake_item = None
    fdn = Foundation()
    fdn.locator = 'bobmachine'
    fdn.blueprint = fb
    fdn.site = s
    fdn.full_clean()
    fdn.save()

    assert fake_key == {'_id': 'bobmachine'}
    assert _tweek_variables(fake_item) == {
        '__last_modified': '*DATETIME*',
        '__timestamp': '*DATETIME*',
        '_site': 'site_test',
        '_blueprint': 'fdnbp_test',
        '_foundation_class_list': [],
        '_foundation_id': 'bobmachine',
        '_foundation_id_map': None,
        '_foundation_interface_list': [],
        '_foundation_locator': 'bobmachine',
        '_foundation_state': 'planned',
        '_foundation_type': 'Unknown',
        '_provisioning_interface': None,
        '_provisioning_interface_mac': None
    }

    fake_key = None
    fdn.delete()
    assert fake_key == {'_id': 'bobmachine'}
Пример #2
0
def test_collection(mocker):
    mocker.patch('contractor.Records.lib._connect', fake_connect)

    t = Site()
    assert collection(t).name == 'Site'

    t = Structure()
    assert collection(t).name == 'Structure'

    t = Foundation()
    assert collection(t).name == 'Foundation'

    t = StructureBluePrint()
    assert collection(t).name == 'BluePrint'

    t = FoundationBluePrint()
    assert collection(t).name == 'BluePrint'
Пример #3
0
def test_updateConfig_structure(mocker):
    mocker.patch('contractor.Records.lib._connect', fake_connect)
    global fake_key
    global fake_item

    s = Site()
    s.name = 'site_test'
    s.description = 'testing Site'
    s.full_clean()
    s.save()

    fb = FoundationBluePrint()
    fb.name = 'fdnbp_test'
    fb.description = 'testing FBP'
    fb.foundation_type_list = ['Unknown']
    fb.full_clean()
    fb.save()

    fdn = Foundation()
    fdn.locator = 'fdn_test'
    fdn.blueprint = fb
    fdn.site = s
    fdn.full_clean()
    fdn.save()

    sb = StructureBluePrint()
    sb.name = 'strbp_test'
    sb.description = 'testing SBP'
    sb.full_clean()
    sb.save()

    sb.foundation_blueprint_list = [fb]
    sb.full_clean()
    sb.save()

    fake_key = None
    fake_item = None
    str = Structure()
    str.hostname = 'testme'
    str.site = s
    str.blueprint = sb
    str.foundation = fdn
    str.full_clean()
    str.save()

    pk = str.pk

    assert fake_key == {
        '_id': pk
    }  # the auto inc value could be all sorts of values
    assert _tweek_variables(fake_item) == {
        '__last_modified': '*DATETIME*',
        '__timestamp': '*DATETIME*',
        '_site': 'site_test',
        '_blueprint': 'strbp_test',
        '_foundation_class_list': [],
        '_foundation_id': 'fdn_test',
        '_foundation_id_map': None,
        '_foundation_interface_list': [],
        '_foundation_locator': 'fdn_test',
        '_foundation_state': 'planned',
        '_foundation_type': 'Unknown',
        '_fqdn': 'testme',
        '_hostname': 'testme',
        '_domain_name': None,
        '_interface_map': {},
        '_provisioning_interface': None,
        '_provisioning_interface_mac': None,
        '_provisioning_address': None,
        '_primary_interface': None,
        '_primary_interface_mac': None,
        '_primary_address': None,
        '_structure_config_uuid': '*UUID*',
        '_structure_id': '*ID*',
        '_structure_state': 'planned'
    }

    fake_key = None
    fake_item = None
    str.config_values = {'sdf': {'a': 'rrrr', 'b': [1, 2, 3, 4]}}
    str.full_clean()
    str.save()

    assert fake_key == {'_id': pk}
    assert _tweek_variables(fake_item) == {
        '__last_modified': '*DATETIME*',
        '__timestamp': '*DATETIME*',
        '_site': 'site_test',
        '_blueprint': 'strbp_test',
        '_foundation_class_list': [],
        '_foundation_id': 'fdn_test',
        '_foundation_id_map': None,
        '_foundation_interface_list': [],
        '_foundation_locator': 'fdn_test',
        '_foundation_state': 'planned',
        '_foundation_type': 'Unknown',
        '_fqdn': 'testme',
        '_hostname': 'testme',
        '_domain_name': None,
        '_interface_map': {},
        '_provisioning_interface': None,
        '_provisioning_interface_mac': None,
        '_provisioning_address': None,
        '_primary_interface': None,
        '_primary_interface_mac': None,
        '_primary_address': None,
        '_structure_config_uuid': '*UUID*',
        '_structure_id': '*ID*',
        '_structure_state': 'planned',
        'sdf': {
            'a': 'rrrr',
            'b': [1, 2, 3, 4]
        }
    }

    fake_key = None
    str.delete()
    assert fake_key == {'_id': pk}
Пример #4
0
def test_handler():
    s = Site(name='test', description='test site')
    s.full_clean()
    s.save()

    ab = AddressBlock(name='test',
                      site=s,
                      subnet='10.0.0.0',
                      gateway_offset=1,
                      prefix=24)
    ab.full_clean()
    ab.save()

    n = Network(name='test', site=s)
    n.full_clean()
    n.save()

    fbp = FoundationBluePrint(name='fdn_test',
                              description='foundation test bp')
    fbp.foundation_type_list = 'Unknown'
    fbp.full_clean()
    fbp.save()

    sbp = StructureBluePrint(name='str_test', description='structure test bp')
    sbp.full_clean()
    sbp.save()
    sbp.foundation_blueprint_list.add(fbp)

    pxe = PXE(name='testpxe',
              boot_script='boot',
              template='this is for the testing')
    pxe.full_clean()
    pxe.save()

    fdn = Foundation(locator='ftester', blueprint=fbp, site=s)
    fdn.full_clean()
    fdn.save()

    iface = RealNetworkInterface(name='eth0', is_provisioning=True)
    iface.foundation = fdn
    iface.physical_location = 'eth0'
    iface.network = n
    iface.full_clean()
    iface.save()

    str = Structure(hostname='stester', foundation=fdn, blueprint=sbp, site=s)
    str.full_clean()
    str.save()
    str.config_uuid = '8b6663f9-efa8-467c-b973-ac79e66e3c78'
    str.save()

    addr = Address(networked=str,
                   address_block=ab,
                   interface_name='eth0',
                   offset=5,
                   is_primary=True)
    addr.full_clean()
    addr.save()

    assert handler(Request('', None)).http_code == 400
    assert handler(Request('/config/config', None)).http_code == 400
    assert handler(Request('/config/234/', None)).http_code == 400
    assert handler(Request('/config/config/', '127.0.0.1')).http_code == 404
    with pytest.raises(ValueError):
        handler(Request('/config/config/', None))

    assert handler(Request('/config/config/s/1000', None)).http_code == 404
    assert handler(Request('/config/config/f/test', None)).http_code == 404
    assert handler(
        Request('/config/config/c/00000000-0000-4000-0000-000000000000',
                None)).http_code == 404

    assert handler(Request('/config/config/s/asdf', None)).http_code == 400
    assert handler(
        Request('/config/config/c/00000000-0000-4000-0000-00000',
                None)).http_code == 400

    assert handler(Request('/config/config/', '10.0.0.1')).http_code == 404

    resp = handler(Request('/config/config/', '10.0.0.5'))
    assert resp.http_code == 200
    _test_dict(
        resp.data, {
            '_blueprint': 'str_test',
            '_foundation_id': 'ftester',
            '_foundation_locator': 'ftester',
            '_foundation_state': 'planned',
            '_foundation_type': 'Unknown',
            '_fqdn': 'stester',
            '_hostname': 'stester',
            '_structure_config_uuid': '8b6663f9-efa8-467c-b973-ac79e66e3c78'
        })

    resp = handler(Request('/config/config/s/{0}'.format(str.pk), None))
    assert resp.http_code == 200
    _test_dict(
        resp.data, {
            '_blueprint': 'str_test',
            '_foundation_id': 'ftester',
            '_foundation_locator': 'ftester',
            '_foundation_state': 'planned',
            '_foundation_type': 'Unknown',
            '_fqdn': 'stester',
            '_hostname': 'stester',
            '_structure_config_uuid': '8b6663f9-efa8-467c-b973-ac79e66e3c78',
        })

    resp = handler(
        Request('/config/config/c/8b6663f9-efa8-467c-b973-ac79e66e3c78', None))
    assert resp.http_code == 200
    _test_dict(
        resp.data, {
            '_blueprint': 'str_test',
            '_foundation_id': 'ftester',
            '_foundation_locator': 'ftester',
            '_foundation_state': 'planned',
            '_foundation_type': 'Unknown',
            '_fqdn': 'stester',
            '_hostname': 'stester',
            '_structure_config_uuid': '8b6663f9-efa8-467c-b973-ac79e66e3c78',
        })

    resp = handler(Request('/config/config/f/ftester', None))
    assert resp.http_code == 200
    _test_dict(
        resp.data, {
            '_blueprint': 'fdn_test',
            '_foundation_id': 'ftester',
            '_foundation_locator': 'ftester',
            '_foundation_state': 'planned',
            '_foundation_type': 'Unknown',
        })

    resp = handler(Request('/config/pxe_template/', '10.0.0.5'))
    assert resp.http_code == 200
    assert resp.data == ''

    resp = handler(Request('/config/boot_script/', '10.0.0.5'))
    assert resp.http_code == 200
    assert resp.data == ''

    iface.pxe = pxe
    iface.full_clean()
    iface.save()

    resp = handler(Request('/config/pxe_template/', '10.0.0.5'))
    assert resp.http_code == 200
    assert resp.data == 'this is for the testing'

    resp = handler(Request('/config/boot_script/', '10.0.0.5'))
    assert resp.http_code == 200
    assert resp.data == '#!ipxe\n\nboot'
Пример #5
0
def test_updateConfig_blueprint(mocker):
    mocker.patch('contractor.Records.lib._connect', fake_connect)
    global fake_key
    global fake_item

    fake_key = None
    fake_item = None
    sb = StructureBluePrint()
    sb.name = 'sbptest'
    sb.description = 'testing SBP'
    sb.full_clean()
    sb.save()

    assert fake_key == {'_id': 'sbptest'}
    assert _tweek_variables(fake_item) == {
        '__last_modified': '*DATETIME*',
        '__timestamp': '*DATETIME*',
        '_blueprint': 'sbptest'
    }

    fake_key = None
    fake_item = None
    sb.config_values = {'er': 'bob', 'um': 'sally'}
    sb.full_clean()
    sb.save()

    assert fake_key == {'_id': 'sbptest'}
    assert _tweek_variables(fake_item) == {
        '__last_modified': '*DATETIME*',
        '__timestamp': '*DATETIME*',
        '_blueprint': 'sbptest',
        'er': 'bob',
        'um': 'sally'
    }

    fake_key = None
    fake_item = None
    fb = FoundationBluePrint()
    fb.name = 'fdntest'
    fb.description = 'testing FBP'
    fb.foundation_type_list = ['nope']
    fb.full_clean()
    fb.save()

    assert fake_key == {'_id': 'fdntest'}
    assert _tweek_variables(fake_item) == {
        '__last_modified': '*DATETIME*',
        '__timestamp': '*DATETIME*',
        '_blueprint': 'fdntest'
    }

    fake_key = None
    fake_item = None
    fb.config_values = {'qwert': [1, 2, 3, 'z']}
    fb.full_clean()
    fb.save()

    assert fake_key == {'_id': 'fdntest'}
    assert _tweek_variables(fake_item) == {
        '__last_modified': '*DATETIME*',
        '__timestamp': '*DATETIME*',
        '_blueprint': 'fdntest',
        'qwert': [1, 2, 3, 'z']
    }

    fake_key = None
    fake_item = None
    sb.foundation_blueprint_list = [fb]
    sb.full_clean()
    sb.save()

    assert fake_key == {'_id': 'sbptest'}
    assert _tweek_variables(fake_item) == {
        '__last_modified': '*DATETIME*',
        '__timestamp': '*DATETIME*',
        '_blueprint': 'sbptest',
        'er': 'bob',
        'um': 'sally'
    }

    fake_key = None
    fb.delete()
    assert fake_key == {'_id': 'fdntest'}

    fake_key = None
    sb.delete()
    assert fake_key == {'_id': 'sbptest'}
Пример #6
0
def test_dependency_job_create():
    si = Site()
    si.name = 'test'
    si.description = 'test'
    si.full_clean()
    si.save()

    fb = FoundationBluePrint(name='fdnb1',
                             description='Foundation BluePrint 1')
    fb.foundation_type_list = ['Unknown']
    fb.full_clean()
    fb.save()

    f = Foundation()
    f.locator = 'test'
    f.site = si
    f.blueprint = fb
    f.full_clean()
    f.save()

    sb = StructureBluePrint(name='strb1', description='Structure BluePrint 1')
    sb.full_clean()
    sb.save()
    sb.foundation_blueprint_list.add(fb)

    s = Structure()
    s.foundation = f
    s.hostname = 'test'
    s.site = si
    s.blueprint = sb
    s.full_clean()
    s.save()

    f.setBuilt()

    d = Dependency()
    d.structure = s
    d.link = 'soft'
    d.full_clean()
    d.save()

    with pytest.raises(Exception) as execinfo:
        createJob('destroy', d, TestUser())
    assert str(execinfo.value.code) == 'NOT_BUILT'

    with pytest.raises(Exception) as execinfo:
        createJob('other', d, TestUser())
    assert str(execinfo.value.code) == 'INVALID_SCRIPT'

    assert createJob('create', d, TestUser()) is not None
    assert d.state == 'planned'
    with pytest.raises(Exception) as execinfo:
        createJob('create', d, TestUser())
    assert str(execinfo.value.code) == 'JOB_EXISTS'
    s.setBuilt()
    d.dependencyjob.delete()
    d = Dependency.objects.get(pk=d.pk)

    with pytest.raises(Exception) as execinfo:
        createJob('destroy', d, TestUser())
    assert str(execinfo.value.code) == 'NOT_BUILT'

    d.setBuilt()
    with pytest.raises(Exception) as execinfo:
        createJob('create', d, TestUser())
    assert str(execinfo.value.code) == 'ALLREADY_BUILT'
    assert d.state == 'built'

    assert createJob('destroy', d, TestUser()) is not None
    assert d.state == 'built'
    with pytest.raises(Exception) as execinfo:
        createJob('destroy', d, TestUser())
    assert str(execinfo.value.code) == 'JOB_EXISTS'
    d.dependencyjob.delete()
    d = Dependency.objects.get(pk=d.pk)

    # test job type checking
    d.setDestroyed()
    s.setDestroyed()
    d = Dependency.objects.get(pk=d.pk)
    s = Structure.objects.get(pk=s.pk)
    assert createJob('create', s, TestUser()) is not None
    assert createJob('create', d, TestUser()) is not None
    d.dependencyjob.delete()
    s.structurejob.delete()
    d = Dependency.objects.get(pk=d.pk)
    s = Structure.objects.get(pk=s.pk)

    d.setDestroyed()
    s.setBuilt()
    d = Dependency.objects.get(pk=d.pk)
    s = Structure.objects.get(pk=s.pk)
    assert createJob('other', s, TestUser()) is not None
    with pytest.raises(ValueError) as execinfo:
        createJob('create', d, TestUser())
    assert str(execinfo.value.code) == 'JOB_EXISTS'
    s.structurejob.delete()
    d = Dependency.objects.get(pk=d.pk)
    s = Structure.objects.get(pk=s.pk)

    d.setBuilt()
    s.setBuilt()
    d = Dependency.objects.get(pk=d.pk)
    s = Structure.objects.get(pk=s.pk)
    assert createJob('destroy', s, TestUser()) is not None
    assert createJob('destroy', d, TestUser()) is not None
    d.dependencyjob.delete()
    s.structurejob.delete()
    d = Dependency.objects.get(pk=d.pk)
    s = Structure.objects.get(pk=s.pk)

    d.setBuilt()
    s.setBuilt()
    d = Dependency.objects.get(pk=d.pk)
    s = Structure.objects.get(pk=s.pk)
    assert createJob('other', s, TestUser()) is not None
    with pytest.raises(ValueError) as execinfo:
        createJob('destroy', d, TestUser())
    assert str(execinfo.value.code) == 'JOB_EXISTS'
    s.structurejob.delete()
    d = Dependency.objects.get(pk=d.pk)
    s = Structure.objects.get(pk=s.pk)
Пример #7
0
def test_can_start_destroy(mocker):
    mocker.patch('contractor.Building.models.Foundation._canSetState',
                 fake_canSetState)  # disable the job checking
    mocker.patch('contractor.Building.models.Structure._canSetState',
                 fake_canSetState)
    mocker.patch('contractor.Building.models.Dependency._canSetState',
                 fake_canSetState)

    si = Site()
    si.name = 'test'
    si.description = 'test'
    si.full_clean()
    si.save()

    fb = FoundationBluePrint(name='fdnb1',
                             description='Foundation BluePrint 1')
    fb.foundation_type_list = ['Unknown']
    fb.full_clean()
    fb.save()

    f = Foundation()
    f.locator = 'test'
    f.site = si
    f.blueprint = fb
    f.full_clean()
    f.save()

    f.setBuilt()

    createJob('destroy', f, TestUser())
    assert f.foundationjob.can_start is True

    sb = StructureBluePrint(name='strb1', description='Structure BluePrint 1')
    sb.full_clean()
    sb.save()
    sb.foundation_blueprint_list.add(fb)

    s = Structure()
    s.foundation = f
    s.hostname = 'test'
    s.site = si
    s.blueprint = sb
    s.full_clean()
    s.save()

    s.setBuilt()

    createJob('destroy', s, TestUser())
    assert f.foundationjob.can_start is False
    assert s.structurejob.can_start is True

    d = Dependency()
    d.structure = s
    d.link = 'soft'
    d.full_clean()
    d.save()

    d.setBuilt()

    createJob('destroy', d, TestUser())
    assert f.foundationjob.can_start is False
    assert s.structurejob.can_start is False
    assert d.dependencyjob.can_start is True

    d2 = Dependency()
    d2.dependency = d
    d2.link = 'soft'
    d2.full_clean()
    d2.save()

    d2.setBuilt()

    createJob('destroy', d2, TestUser())
    assert f.foundationjob.can_start is False
    assert s.structurejob.can_start is False
    assert d.dependencyjob.can_start is False
    assert d2.dependencyjob.can_start is True

    d2.setDestroyed()
    assert f.foundationjob.can_start is False
    assert s.structurejob.can_start is False
    assert d.dependencyjob.can_start is True
    assert d2.dependencyjob.can_start is False

    d.setDestroyed()
    assert f.foundationjob.can_start is False
    assert s.structurejob.can_start is False
    assert d.dependencyjob.can_start is False
    assert d2.dependencyjob.can_start is False

    s.setDestroyed()
    assert f.foundationjob.can_start is False
    assert s.structurejob.can_start is False
    assert d.dependencyjob.can_start is False
    assert d2.dependencyjob.can_start is False

    f.setDestroyed()
    assert f.foundationjob.can_start is False
    assert s.structurejob.can_start is False
    assert d.dependencyjob.can_start is False
    assert d2.dependencyjob.can_start is False

    d2.setDestroyed()
    d.setBuilt()
    s.setBuilt()
    f.setBuilt()
    assert f.foundationjob.can_start is False
    assert s.structurejob.can_start is False
    assert d.dependencyjob.can_start is True
    assert d2.dependencyjob.can_start is False

    d = Dependency.objects.get(pk=d.pk)
    d.foundation = f
    d.full_clean()
    d.save()
    assert f.foundationjob.can_start is False
    assert s.structurejob.can_start is False
    assert d.dependencyjob.can_start is False
    assert d2.dependencyjob.can_start is False

    f.setDestroyed()
    s.setBuilt(
    )  # setting foundation to destoyed, destroys the structure, which destroys the dependency
    d.setBuilt()
    assert f.foundationjob.can_start is False
    assert s.structurejob.can_start is False
    assert d.dependencyjob.can_start is False
    assert d2.dependencyjob.can_start is False

    f.setBuilt()
    assert f.foundationjob.can_start is False
    assert s.structurejob.can_start is False
    assert d.dependencyjob.can_start is False
    assert d2.dependencyjob.can_start is False

    d2.dependencyjob.delete()
    d2.setDestroyed()
    f = Foundation.objects.get(pk=f.pk)
    s = Structure.objects.get(pk=s.pk)
    d = Dependency.objects.get(pk=d.pk)
    assert f.foundationjob.can_start is False
    assert s.structurejob.can_start is False
    assert d.dependencyjob.can_start is False

    d.dependencyjob.delete()
    d.setDestroyed()
    f = Foundation.objects.get(pk=f.pk)
    s = Structure.objects.get(pk=s.pk)
    assert f.foundationjob.can_start is False
    assert s.structurejob.can_start is True

    s.structurejob.delete()
    s.setDestroyed()
    f = Foundation.objects.get(pk=f.pk)
    assert f.foundationjob.can_start is True

    s.delete()  # setting foundation.structure to None
    f = Foundation.objects.get(pk=f.pk)
    assert f.foundationjob.can_start is True
Пример #8
0
def test_foundation_with_structure_job_create():
    si = Site()
    si.name = 'test'
    si.description = 'test'
    si.full_clean()
    si.save()

    fb = FoundationBluePrint(name='fdnb1',
                             description='Foundation BluePrint 1')
    fb.foundation_type_list = ['Unknown']
    fb.full_clean()
    fb.save()

    f = Foundation()
    f.locator = 'test'
    f.site = si
    f.blueprint = fb
    f.full_clean()
    f.save()

    sb = StructureBluePrint(name='strb1', description='Structure BluePrint 1')
    sb.full_clean()
    sb.save()
    sb.foundation_blueprint_list.add(fb)

    s = Structure()
    s.foundation = f
    s.hostname = 'test'
    s.site = si
    s.blueprint = sb
    s.full_clean()
    s.save()

    f.setDestroyed()
    assert createJob('create', f, TestUser()) is not None
    f.foundationjob.delete()
    f = Foundation.objects.get(pk=f.pk)
    f.setLocated()
    assert createJob('create', f, TestUser()) is not None
    f.foundationjob.delete()
    f = Foundation.objects.get(pk=f.pk)
    f.setBuilt()
    with pytest.raises(Exception) as execinfo:
        createJob('create', f, TestUser())
    assert str(execinfo.value.code) == 'ALLREADY_BUILT'

    f.setDestroyed()
    s.setBuilt()
    assert createJob('create', f, TestUser()) is not None
    f.foundationjob.delete()
    f = Foundation.objects.get(pk=f.pk)
    f.setLocated()
    s.setBuilt()
    assert createJob('create', f, TestUser()) is not None
    f.foundationjob.delete()
    f = Foundation.objects.get(pk=f.pk)
    f.setBuilt()
    s.setBuilt()
    with pytest.raises(Exception) as execinfo:
        createJob('create', f, TestUser())
    assert str(execinfo.value.code) == 'ALLREADY_BUILT'

    s.setDestroyed()
    f.setBuilt()
    assert createJob('destroy', f, TestUser()) is not None
    f.foundationjob.delete()
    f = Foundation.objects.get(pk=f.pk)
    f.setLocated()
    with pytest.raises(Exception) as execinfo:
        createJob('destroy', f, TestUser())
    assert str(execinfo.value.code) == 'NOT_BUILT'
    f.setDestroyed()
    with pytest.raises(Exception) as execinfo:
        createJob('destroy', f, TestUser())
    assert str(execinfo.value.code) == 'NOT_BUILT'

    s.setBuilt()
    f.setBuilt()
    assert createJob('destroy', f, TestUser()) is not None
    f.foundationjob.delete()
    f = Foundation.objects.get(pk=f.pk)
    f.setLocated()
    s.setBuilt()
    with pytest.raises(Exception) as execinfo:
        createJob('destroy', f, TestUser())
    assert str(execinfo.value.code) == 'NOT_BUILT'
    f.setDestroyed()
    s.setBuilt()
    with pytest.raises(Exception) as execinfo:
        createJob('destroy', f, TestUser())
    assert str(execinfo.value.code) == 'NOT_BUILT'
Пример #9
0
def test_structure_job_create():  # TODO: test structures with dependency
    si = Site()
    si.name = 'test'
    si.description = 'test'
    si.full_clean()
    si.save()

    fb = FoundationBluePrint(name='fdnb1',
                             description='Foundation BluePrint 1')
    fb.foundation_type_list = ['Unknown']
    fb.full_clean()
    fb.save()

    f = Foundation()
    f.locator = 'test'
    f.site = si
    f.blueprint = fb
    f.full_clean()
    f.save()

    sb = StructureBluePrint(name='strb1', description='Structure BluePrint 1')
    sb.full_clean()
    sb.save()
    sb.foundation_blueprint_list.add(fb)

    s = Structure()
    s.foundation = f
    s.hostname = 'test'
    s.site = si
    s.blueprint = sb
    s.full_clean()
    s.save()

    with pytest.raises(Exception) as execinfo:
        createJob('destroy', s, TestUser())
    assert str(execinfo.value.code) == 'NOT_BUILT'
    with pytest.raises(Exception) as execinfo:
        createJob('other', s, TestUser())
    assert str(execinfo.value.code) == 'NOT_BUILT'

    assert createJob('create', s, TestUser()) is not None
    assert s.state == 'planned'
    with pytest.raises(Exception) as execinfo:
        createJob('create', s, TestUser())
    assert str(execinfo.value.code) == 'JOB_EXISTS'
    s.structurejob.delete()
    s = Structure.objects.get(pk=s.pk)
    f.setBuilt()

    with pytest.raises(Exception) as execinfo:
        createJob('destroy', s, TestUser())
    assert str(execinfo.value.code) == 'NOT_BUILT'
    with pytest.raises(Exception) as execinfo:
        createJob('other', s, TestUser())
    assert str(execinfo.value.code) == 'NOT_BUILT'

    s.setBuilt()
    with pytest.raises(Exception) as execinfo:
        createJob('create', s, TestUser())
    assert str(execinfo.value.code) == 'ALLREADY_BUILT'
    assert s.state == 'built'

    assert createJob('other', s, TestUser()) is not None
    assert s.state == 'built'
    with pytest.raises(Exception) as execinfo:
        createJob('other', s, TestUser())
    assert str(execinfo.value.code) == 'JOB_EXISTS'
    s.structurejob.delete()
    s = Structure.objects.get(pk=s.pk)

    assert createJob('destroy', s, TestUser()) is not None
    assert s.state == 'built'
    with pytest.raises(Exception) as execinfo:
        createJob('destroy', s, TestUser())
    assert str(execinfo.value.code) == 'JOB_EXISTS'
    s.structurejob.delete()
    s = Structure.objects.get(pk=s.pk)

    f.setLocated()
    with pytest.raises(Exception) as execinfo:
        createJob('destroy', s, TestUser())
    assert str(execinfo.value.code) == 'NOT_BUILT'

    # test job type checking
    s.setDestroyed()
    f.setDestroyed()
    s = Structure.objects.get(pk=s.pk)
    f = Foundation.objects.get(pk=f.pk)
    assert createJob('create', f, TestUser()) is not None
    assert createJob('create', s, TestUser()) is not None
    s.structurejob.delete()
    f.foundationjob.delete()
    s = Structure.objects.get(pk=s.pk)
    f = Foundation.objects.get(pk=f.pk)

    s.setDestroyed()
    f.setBuilt()
    s = Structure.objects.get(pk=s.pk)
    f = Foundation.objects.get(pk=f.pk)
    assert createJob('other', f, TestUser()) is not None
    with pytest.raises(Exception) as execinfo:
        createJob('create', s, TestUser())
    assert str(execinfo.value.code) == 'JOB_EXISTS'
    f.foundationjob.delete()
    s = Structure.objects.get(pk=s.pk)
    f = Foundation.objects.get(pk=f.pk)

    s.setBuilt()
    f.setBuilt()
    s = Structure.objects.get(pk=s.pk)
    f = Foundation.objects.get(pk=f.pk)
    assert createJob('destroy', f, TestUser()) is not None
    assert createJob('destroy', s, TestUser()) is not None
    s.structurejob.delete()
    f.foundationjob.delete()
    s = Structure.objects.get(pk=s.pk)
    f = Foundation.objects.get(pk=f.pk)

    s.setBuilt()
    f.setBuilt()
    s = Structure.objects.get(pk=s.pk)
    f = Foundation.objects.get(pk=f.pk)
    assert createJob('other', f, TestUser()) is not None
    with pytest.raises(Exception) as execinfo:
        createJob('destroy', s, TestUser())
    assert str(execinfo.value.code) == 'JOB_EXISTS'
    f.foundationjob.delete()
    s = Structure.objects.get(pk=s.pk)
    f = Foundation.objects.get(pk=f.pk)
Пример #10
0
def test_foundation_job_create(
):  # TODO: should also do tests depending on a Dependency
    si = Site()
    si.name = 'test'
    si.description = 'test'
    si.full_clean()
    si.save()

    fb = FoundationBluePrint(name='fdnb1',
                             description='Foundation BluePrint 1')
    fb.foundation_type_list = ['Unknown']
    fb.full_clean()
    fb.save()

    f = Foundation()
    f.locator = 'test'
    f.site = si
    f.blueprint = fb
    f.full_clean()
    f.save()

    with pytest.raises(Exception) as execinfo:
        createJob('destroy', f, TestUser())
    assert str(execinfo.value.code) == 'NOT_BUILT'
    with pytest.raises(Exception) as execinfo:
        createJob('other', f, TestUser())
    assert str(execinfo.value.code) == 'NOT_BUILT'

    assert createJob('create', f, TestUser()) is not None
    assert f.state == 'planned'
    with pytest.raises(Exception) as execinfo:
        createJob('create', f, TestUser())
    assert str(execinfo.value.code) == 'JOB_EXISTS'
    f.foundationjob.delete()
    f = Foundation.objects.get(pk=f.pk)
    f.setLocated()

    with pytest.raises(Exception) as execinfo:
        createJob('destroy', f, TestUser())
    assert str(execinfo.value.code) == 'NOT_BUILT'
    with pytest.raises(ValueError) as execinfo:
        createJob('other', f, TestUser())
    assert str(execinfo.value.code) == 'NOT_BUILT'

    assert createJob('create', f, TestUser()) is not None
    assert f.state == 'located'
    with pytest.raises(Exception) as execinfo:
        createJob('create', f, TestUser())
    assert str(execinfo.value.code) == 'JOB_EXISTS'
    f.foundationjob.delete()
    f = Foundation.objects.get(pk=f.pk)

    f.setBuilt()
    with pytest.raises(Exception) as execinfo:
        createJob('create', f, TestUser())
    assert str(execinfo.value.code) == 'ALLREADY_BUILT'
    assert f.state == 'built'

    assert createJob('other', f, TestUser()) is not None
    assert f.state == 'built'
    with pytest.raises(Exception) as execinfo:
        createJob('other', f, TestUser())
    assert str(execinfo.value.code) == 'JOB_EXISTS'
    f.foundationjob.delete()
    f = Foundation.objects.get(pk=f.pk)

    assert createJob('destroy', f, TestUser()) is not None
    assert f.state == 'built'
    with pytest.raises(Exception) as execinfo:
        createJob('destroy', f, TestUser())
    assert str(execinfo.value.code) == 'JOB_EXISTS'
    f.foundationjob.delete()
    f = Foundation.objects.get(pk=f.pk)
Пример #11
0
def test_structure():
  s1 = Site( name='site1', description='test site 1' )
  s1.full_clean()
  s1.save()

  fb1 = FoundationBluePrint( name='fdnb1', description='Foundation BluePrint 1' )
  fb1.foundation_type_list = [ 'Unknown' ]
  fb1.full_clean()
  fb1.save()

  f1 = Foundation( site=s1, locator='fdn1', blueprint=fb1 )
  f1.full_clean()
  f1.save()

  sb1 = StructureBluePrint( name='strb1', description='Structure BluePrint 1' )
  sb1.full_clean()
  sb1.save()
  sb1.foundation_blueprint_list.add( fb1 )

  str1 = Structure( foundation=f1, site=s1, hostname='struct1', blueprint=sb1 )
  str1.full_clean()
  str1.save()

  now = datetime.now( timezone.utc )
  tmp = getConfig( str1 )
  assert now - tmp[ '__last_modified' ] < timedelta( seconds=5 )
  assert now - tmp[ '__timestamp' ] < timedelta( seconds=5 )

  del tmp[ '__last_modified' ]
  del tmp[ '__timestamp' ]
  del tmp[ '_structure_config_uuid' ]
  del tmp[ '__pxe_template_location' ]

  assert tmp == {
                  '__contractor_host': 'http://contractor/',
                  '__pxe_location': 'http://static/pxe/',
                  '_foundation_class_list': [],
                  '_foundation_id': 'fdn1',
                  '_foundation_id_map': None,
                  '_foundation_interface_list': [],
                  '_foundation_locator': 'fdn1',
                  '_foundation_state': 'planned',
                  '_foundation_type': 'Unknown',
                  '_provisioning_interface': None,
                  '_provisioning_interface_mac': None,
                  '_provisioning_address': None,
                  '_primary_interface': None,
                  '_primary_interface_mac': None,
                  '_primary_address': None,
                  '_structure_id': str1.pk,
                  '_structure_state': 'planned',
                  '_fqdn': 'struct1',
                  '_hostname': 'struct1',
                  '_domain_name': None,
                  '_interface_map': {},
                  '_blueprint': 'strb1',
                  '_site': 'site1'
                }

  assert _strip_base( getConfig( str1 ) ) == {
                                              '_foundation_class_list': [],
                                              '_foundation_id': 'fdn1',
                                              '_foundation_id_map': None,
                                              '_foundation_interface_list': [],
                                              '_foundation_locator': 'fdn1',
                                              '_foundation_state': 'planned',
                                              '_foundation_type': 'Unknown',
                                              '_provisioning_interface': None,
                                              '_provisioning_interface_mac': None,
                                              '_provisioning_address': None,
                                              '_primary_interface': None,
                                              '_primary_interface_mac': None,
                                              '_primary_address': None,
                                              '_structure_id': str1.pk,
                                              '_structure_state': 'planned',
                                              '_fqdn': 'struct1',
                                              '_hostname': 'struct1',
                                              '_domain_name': None,
                                              '_interface_map': {},
                                              '_blueprint': 'strb1',
                                              '_site': 'site1'
                                             }

  fb1.config_values[ 'bob' ] = 'foundation blueprint'
  fb1.full_clean()
  fb1.save()

  assert _strip_base( getConfig( str1 ) ) == {
                                              '_foundation_class_list': [],
                                              '_foundation_id': 'fdn1',
                                              '_foundation_id_map': None,
                                              '_foundation_interface_list': [],
                                              '_foundation_locator': 'fdn1',
                                              '_foundation_state': 'planned',
                                              '_foundation_type': 'Unknown',
                                              '_provisioning_interface': None,
                                              '_provisioning_interface_mac': None,
                                              '_provisioning_address': None,
                                              '_primary_interface': None,
                                              '_primary_interface_mac': None,
                                              '_primary_address': None,
                                              '_structure_id': str1.pk,
                                              '_structure_state': 'planned',
                                              '_fqdn': 'struct1',
                                              '_hostname': 'struct1',
                                              '_domain_name': None,
                                              '_interface_map': {},
                                              '_blueprint': 'strb1',
                                              '_site': 'site1'
                                             }

  sb1.config_values[ 'bob' ] = 'structure blueprint'
  sb1.full_clean()
  sb1.save()

  assert _strip_base( getConfig( str1 ) ) == {
                                             '_foundation_class_list': [],
                                             '_foundation_id': 'fdn1',
                                             '_foundation_id_map': None,
                                             '_foundation_interface_list': [],
                                             '_foundation_locator': 'fdn1',
                                             '_foundation_state': 'planned',
                                             '_foundation_type': 'Unknown',
                                             '_provisioning_interface': None,
                                             '_provisioning_interface_mac': None,
                                             '_provisioning_address': None,
                                             '_primary_interface': None,
                                             '_primary_interface_mac': None,
                                             '_primary_address': None,
                                             '_structure_id': str1.pk,
                                             '_structure_state': 'planned',
                                             '_fqdn': 'struct1',
                                             '_hostname': 'struct1',
                                             '_domain_name': None,
                                             '_interface_map': {},
                                             '_blueprint': 'strb1',
                                             '_site': 'site1',
                                             'bob': 'structure blueprint'
                                            }

  str1.config_values[ 'bob' ] = 'structure'
  str1.full_clean()
  str1.save()

  assert _strip_base( getConfig( str1 ) ) == {
                                             '_foundation_class_list': [],
                                             '_foundation_id': 'fdn1',
                                             '_foundation_id_map': None,
                                             '_foundation_interface_list': [],
                                             '_foundation_locator': 'fdn1',
                                             '_foundation_state': 'planned',
                                             '_foundation_type': 'Unknown',
                                             '_provisioning_interface': None,
                                             '_provisioning_interface_mac': None,
                                             '_provisioning_address': None,
                                             '_primary_interface': None,
                                             '_primary_interface_mac': None,
                                             '_primary_address': None,
                                             '_structure_id': str1.pk,
                                             '_structure_state': 'planned',
                                             '_fqdn': 'struct1',
                                             '_hostname': 'struct1',
                                             '_domain_name': None,
                                             '_interface_map': {},
                                             '_blueprint': 'strb1',
                                             '_site': 'site1',
                                             'bob': 'structure'
                                            }
Пример #12
0
def test_foundation():
  s1 = Site( name='site1', description='test site 1' )
  s1.config_values = {}
  s1.full_clean()
  s1.save()

  fb1 = FoundationBluePrint( name='fdnb1', description='Foundation BluePrint 1' )
  fb1.foundation_type_list = [ 'Unknown' ]
  fb1.full_clean()
  fb1.save()

  f1 = Foundation( site=s1, locator='fdn1', blueprint=fb1 )
  f1.full_clean()
  f1.save()

  now = datetime.now( timezone.utc )
  tmp = getConfig( f1 )
  assert now - tmp[ '__last_modified' ] < timedelta( seconds=5 )
  assert now - tmp[ '__timestamp' ] < timedelta( seconds=5 )

  del tmp[ '__last_modified' ]
  del tmp[ '__timestamp' ]
  del tmp[ '__pxe_template_location' ]

  assert tmp == {
                  '__contractor_host': 'http://contractor/',
                  '__pxe_location': 'http://static/pxe/',
                  '_foundation_class_list': [],
                  '_foundation_id': 'fdn1',
                  '_foundation_id_map': None,
                  '_foundation_interface_list': [],
                  '_foundation_locator': 'fdn1',
                  '_foundation_state': 'planned',
                  '_foundation_type': 'Unknown',
                  '_provisioning_interface': None,
                  '_provisioning_interface_mac': None,
                  '_blueprint': 'fdnb1',
                  '_site': 'site1'
                }

  assert _strip_base( getConfig( f1 ) ) == {
                                            '_foundation_class_list': [],
                                            '_foundation_id': 'fdn1',
                                            '_foundation_id_map': None,
                                            '_foundation_interface_list': [],
                                            '_foundation_locator': 'fdn1',
                                            '_foundation_state': 'planned',
                                            '_foundation_type': 'Unknown',
                                            '_provisioning_interface': None,
                                            '_provisioning_interface_mac': None,
                                            '_blueprint': 'fdnb1',
                                            '_site': 'site1'
                                           }

  fb1.config_values[ 'lucky' ] = 'blueprint'
  fb1.full_clean()
  fb1.save()

  assert _strip_base( getConfig( f1 ) ) == {
                                            '_foundation_class_list': [],
                                            '_foundation_id': 'fdn1',
                                            '_foundation_id_map': None,
                                            '_foundation_interface_list': [],
                                            '_foundation_locator': 'fdn1',
                                            '_foundation_state': 'planned',
                                            '_foundation_type': 'Unknown',
                                            '_provisioning_interface': None,
                                            '_provisioning_interface_mac': None,
                                            '_blueprint': 'fdnb1',
                                            '_site': 'site1',
                                            'lucky': 'blueprint'
                                           }

  s1.config_values[ 'lucky' ] = 'site'
  s1.full_clean()
  s1.save()

  assert _strip_base( getConfig( f1 ) ) == {
                                            '_foundation_class_list': [],
                                            '_foundation_id': 'fdn1',
                                            '_foundation_id_map': None,
                                            '_foundation_interface_list': [],
                                            '_foundation_locator': 'fdn1',
                                            '_foundation_state': 'planned',
                                            '_foundation_type': 'Unknown',
                                            '_provisioning_interface': None,
                                            '_provisioning_interface_mac': None,
                                            '_blueprint': 'fdnb1',
                                            '_site': 'site1',
                                            'lucky': 'site'
                                           }
Пример #13
0
def test_blueprint():
  fb1 = FoundationBluePrint( name='fdnb1', description='Foundation BluePrint 1' )
  fb1.foundation_type_list = [ 'Unknown' ]
  fb1.full_clean()
  fb1.save()

  now = datetime.now( timezone.utc )
  tmp = getConfig( fb1 )
  assert now - tmp[ '__last_modified' ] < timedelta( seconds=5 )
  assert now - tmp[ '__timestamp' ] < timedelta( seconds=5 )

  del tmp[ '__last_modified' ]
  del tmp[ '__timestamp' ]
  del tmp[ '__pxe_template_location' ]

  assert tmp == {
                  '__contractor_host': 'http://contractor/',
                  '__pxe_location': 'http://static/pxe/',
                  '_blueprint': 'fdnb1'
                }

  assert _strip_base( getConfig( fb1 ) ) == {
                                              '_blueprint': 'fdnb1'
                                            }

  fb2 = FoundationBluePrint( name='fdnb2', description='Foundation BluePrint 2' )
  fb2.foundation_type_list = [ 'Unknown' ]
  fb2.config_values = { 'the': 'Nice value' }
  fb2.full_clean()
  fb2.save()
  fb2.parent_list.add( fb1 )

  assert _strip_base( getConfig( fb2 ) ) == {
                                              '_blueprint': 'fdnb2',
                                              'the': 'Nice value'
                                            }

  fb1.config_values = { 'from': 'inside the house' }
  fb1.full_clean()
  fb1.save()

  assert _strip_base( getConfig( fb2 ) ) == {
                                              '_blueprint': 'fdnb2',
                                              'the': 'Nice value',
                                              'from': 'inside the house'
                                            }

  sb1 = StructureBluePrint( name='strb1', description='Structure BluePrint 1' )
  sb1.full_clean()
  sb1.save()

  now = datetime.now( timezone.utc )
  tmp = getConfig( sb1 )
  assert now - tmp[ '__last_modified' ] < timedelta( seconds=5 )
  assert now - tmp[ '__timestamp' ] < timedelta( seconds=5 )

  del tmp[ '__last_modified' ]
  del tmp[ '__timestamp' ]
  del tmp[ '__pxe_template_location' ]

  assert tmp == {
                  '__contractor_host': 'http://contractor/',
                  '__pxe_location': 'http://static/pxe/',
                  '_blueprint': 'strb1'
                }

  assert _strip_base( getConfig( sb1 ) ) == {
                                              '_blueprint': 'strb1'
                                            }

  sb2 = StructureBluePrint( name='strb2', description='Structure BluePrint 2' )
  sb2.full_clean()
  sb2.save()
  sb2.parent_list.add( sb1 )

  assert _strip_base( getConfig( sb2 ) ) == {
                                              '_blueprint': 'strb2'
                                            }

  sb2.foundation_blueprint_list.add( fb2 )

  assert _strip_base( getConfig( sb2 ) ) == {
                                              '_blueprint': 'strb2'
                                            }
Пример #14
0
def test_valid_names():
  s1 = Site( name='site1', description='test site 1' )
  s1.full_clean()
  s1.save()

  s1.config_values[ 'valid' ] = 'value 1'
  s1.full_clean()

  s1.config_values[ '_nope' ] = 'more value 1'
  with pytest.raises( ValidationError ):
    s1.full_clean()

  s1 = Site.objects.get( name='site1' )
  s1.full_clean()

  s1.config_values[ '__bad' ] = 'bad bad bad 1'
  with pytest.raises( ValidationError ):
    s1.full_clean()
  del s1.config_values[ '__bad' ]

  fb1 = FoundationBluePrint( name='fdnb1', description='Foundation BluePrint 1' )
  fb1.foundation_type_list = [ 'Unknown' ]
  fb1.full_clean()
  fb1.save()

  fb1.config_values[ 'valid' ] = 'value 2'
  fb1.full_clean()

  fb1.config_values[ '_nope' ] = 'more value 2'
  with pytest.raises( ValidationError ):
    fb1.full_clean()

  fb1 = FoundationBluePrint.objects.get( pk='fdnb1' )
  fb1.full_clean()

  fb1.config_values[ '__bad' ] = 'bad bad bad 2'
  with pytest.raises( ValidationError ):
    fb1.full_clean()
  del fb1.config_values[ '__bad' ]

  fb1 = FoundationBluePrint.objects.get( pk='fdnb1' )
  fb1.full_clean()

  sb1 = StructureBluePrint( name='strb1', description='Structure BluePrint 1' )
  sb1.full_clean()
  sb1.save()
  sb1.foundation_blueprint_list.add( fb1 )

  sb1.config_values[ 'valid' ] = 'value 3'
  sb1.full_clean()

  sb1.config_values[ '_nope' ] = 'more value 3'
  with pytest.raises( ValidationError ):
    sb1.full_clean()

  sb1 = StructureBluePrint.objects.get( name='strb1' )
  sb1.full_clean()

  sb1.config_values[ '__bad' ] = 'bad bad bad 3'
  with pytest.raises( ValidationError ):
    sb1.full_clean()
  del sb1.config_values[ '__bad' ]

  f1 = Foundation( site=s1, locator='fdn1', blueprint=fb1 )
  f1.full_clean()
  f1.save()

  str1 = Structure( foundation=f1, site=s1, hostname='struct1', blueprint=sb1 )
  str1.full_clean()
  str1.save()

  str1.config_values[ 'valid' ] = 'value 4'
  str1.full_clean()

  str1.config_values[ '_nope' ] = 'more value 4'
  with pytest.raises( ValidationError ):
    str1.full_clean()

  str1 = Structure.objects.get( hostname='struct1' )
  str1.full_clean()

  str1.config_values[ '__bad' ] = 'bad bad bad 4'
  with pytest.raises( ValidationError ):
    str1.full_clean()