Beispiel #1
0
def test_update(site, client):
    """Test update of an existing interface w/ an address."""
    ifc_uri = site.list_uri('interface')
    dev_uri = site.list_uri('device')
    net_uri = site.list_uri('network')

    dev_resp = client.create(dev_uri, hostname='foo-bar1')
    dev = get_result(dev_resp)

    net_resp = client.create(net_uri, cidr='10.1.1.0/24')
    net = get_result(net_resp)

    # Create eth0 w/ an address
    addresses = ['10.1.1.1/32']
    ifc_resp = client.create(ifc_uri,
                             device=dev['id'],
                             name='eth0',
                             addresses=addresses)
    ifc = get_result(ifc_resp)
    ifc_obj_uri = site.detail_uri('interface', id=ifc['id'])

    # Create eth1 w/ no address
    ifc2_resp = client.create(ifc_uri, device=dev['id'], name='eth1')
    ifc2 = get_result(ifc2_resp)
    ifc2_obj_uri = site.detail_uri('interface', id=ifc2['id'])

    # Assigning eth0's address to eth1 should fail.
    params = ifc2.copy()
    params['addresses'] = addresses
    assert_error(client.update(ifc2_obj_uri, **params),
                 status.HTTP_400_BAD_REQUEST)

    # (device_id, name) must be unique
    params['name'] = 'eth0'
    assert_error(client.update(ifc2_obj_uri, **params),
                 status.HTTP_409_CONFLICT)

    # Test zeroing out addresses on the Interface
    params['name'] = 'eth1'
    params['addresses'] = []
    ifc2.update(params)
    assert_success(client.update(ifc2_obj_uri, **params), ifc2)

    # Assign addresses by natural key
    natural_key = slugify_interface(**ifc2)
    ifc2_natural_uri = site.detail_uri('interface', id=natural_key)
    params['name'] = 'eth3'
    payload = copy.deepcopy(params)
    payload['name_slug'] = slugify_interface(**payload)

    assert_success(client.update(ifc2_natural_uri, **params), payload)

    # Update parent by natural key (set `ifc` as parent to `ifc2`)
    params['parent_id'] = ifc['name_slug']
    payload['parent_id'] = ifc['id']
    payload['parent'] = ifc['name_slug']
    payload['name_slug'] = slugify_interface(**payload)

    assert_success(client.update(ifc2_obj_uri, **params), payload)
Beispiel #2
0
def test_deletion(site, client):
    """Test deletion of Interfaces."""
    ifc_uri = site.list_uri('interface')
    dev_uri = site.list_uri('device')

    dev1_resp = client.create(dev_uri, hostname='foo-bar1')
    dev1 = get_result(dev1_resp)

    # Create Interfaces
    dev1_eth0_resp = client.create(ifc_uri, device=dev1['id'], name='eth0')
    dev1_eth0 = get_result(dev1_eth0_resp)
    dev1_eth0_uri = site.detail_uri('interface', id=dev1_eth0['id'])

    dev1_eth1_resp = client.create(ifc_uri,
                                   device=dev1['id'],
                                   name='eth1',
                                   parent_id=dev1_eth0['id'])
    dev1_eth1 = get_result(dev1_eth1_resp)
    dev1_eth1_uri = site.detail_uri('interface', id=dev1_eth1['id'])

    # Don't allow delete when there's a child Interface
    assert_error(client.delete(dev1_eth0_uri), status.HTTP_409_CONFLICT)

    # Delete the child Interface
    client.delete(dev1_eth1_uri)

    # And safely delete the parent Network
    assert_deleted(client.delete(dev1_eth0_uri))

    # Delete based on natural key
    dev1_eth2_resp = client.create(ifc_uri, device=dev1['id'], name='eth2')
    dev1_eth2 = get_result(dev1_eth2_resp)
    natural_key = slugify_interface(**dev1_eth2)
    dev1_eth2_natural_uri = site.detail_uri('interface', id=natural_key)
    assert_deleted(client.delete(dev1_eth2_natural_uri))
Beispiel #3
0
def test_deletion(site, client):
    """Test deletion of Interfaces."""
    ifc_uri = site.list_uri('interface')
    dev_uri = site.list_uri('device')

    dev1_resp = client.create(dev_uri, hostname='foo-bar1')
    dev1 = get_result(dev1_resp)

    # Create Interfaces
    dev1_eth0_resp = client.create(ifc_uri, device=dev1['id'], name='eth0')
    dev1_eth0 = get_result(dev1_eth0_resp)
    dev1_eth0_uri = site.detail_uri('interface', id=dev1_eth0['id'])

    dev1_eth1_resp = client.create(
        ifc_uri, device=dev1['id'], name='eth1', parent_id=dev1_eth0['id']
    )
    dev1_eth1 = get_result(dev1_eth1_resp)
    dev1_eth1_uri = site.detail_uri('interface', id=dev1_eth1['id'])

    # Don't allow delete when there's a child Interface
    assert_error(client.delete(dev1_eth0_uri), status.HTTP_409_CONFLICT)

    # Delete the child Interface
    client.delete(dev1_eth1_uri)

    # And safely delete the parent Network
    assert_deleted(client.delete(dev1_eth0_uri))

    # Delete based on natural key
    dev1_eth2_resp = client.create(ifc_uri, device=dev1['id'], name='eth2')
    dev1_eth2 = get_result(dev1_eth2_resp)
    natural_key = slugify_interface(**dev1_eth2)
    dev1_eth2_natural_uri = site.detail_uri('interface', id=natural_key)
    assert_deleted(client.delete(dev1_eth2_natural_uri))
def add_name_slug(apps, schema_editor):
    """Correctly name_slug for every Interface with slash in the name."""
    Interface = apps.get_model('nsot', 'Interface')
    for i in Interface.objects.iterator():
        name_slug = slugify_interface(device_hostname=i.device_hostname,
                                      name=i.name)
        i.name_slug = name_slug
        i.save()
def add_name_slug(apps, schema_editor):
    """Correctly name_slug for every Interface with slash in the name."""
    Interface = apps.get_model('nsot', 'Interface')
    for i in Interface.objects.iterator():
        name_slug = slugify_interface(
            device_hostname=i.device_hostname, name=i.name
        )
        i.name_slug = name_slug
        i.save()
Beispiel #6
0
def test_slugify_interface():
    """Test ``util.slugify_interface``."""

    # Test interface dict input
    interface = {'device_hostname': 'foo-bar1', 'name': 'ge-0/0/1'}
    expected = 'foo-bar1:ge-0/0/1'
    assert util.slugify_interface(interface) == expected

    # Test kwarg input
    assert util.slugify_interface(**interface) == expected

    # Test bad inputs
    with pytest.raises(RuntimeError):
        util.slugify_interface()

    with pytest.raises(RuntimeError):
        util.slugify_interface(device_hostname='bogus')

    with pytest.raises(RuntimeError):
        util.slugify_interface(name='bogus')
Beispiel #7
0
def test_slugify_interface():
    """Test ``util.slugify_interface``."""

    # Test interface dict input
    interface = {'device_hostname': 'foo-bar1', 'name': 'ge-0/0/1'}
    expected = 'foo-bar1:ge-0/0/1'
    assert util.slugify_interface(interface) == expected

    # Test kwarg input
    assert util.slugify_interface(**interface) == expected

    # Test bad inputs
    with pytest.raises(RuntimeError):
        util.slugify_interface()

    with pytest.raises(RuntimeError):
        util.slugify_interface(device_hostname='bogus')

    with pytest.raises(RuntimeError):
        util.slugify_interface(name='bogus')
Beispiel #8
0
def test_detail_routes(site, client):
    """Test detail routes for Interfaces objects."""
    ifc_uri = site.list_uri('interface')
    dev_uri = site.list_uri('device')
    net_uri = site.list_uri('network')

    dev_resp = client.create(dev_uri, hostname='foo-bar1')
    dev = get_result(dev_resp)

    net_resp = client.create(net_uri, cidr='10.1.1.0/24')
    net = get_result(net_resp)

    # Create a simple interface with addresses

    set_addresses = ['10.1.1.1/32', '10.1.1.2/32', '10.1.1.3/32']

    ifc_resp = client.create(ifc_uri,
                             device=dev['id'],
                             name='eth0',
                             addresses=set_addresses)
    ifc = get_result(ifc_resp)
    ifc_obj_uri = site.detail_uri('interface', id=ifc['id'])

    # Fetch the Network address objects
    addresses_resp = client.retrieve(net_uri,
                                     include_ips=True,
                                     include_networks=False)
    addresses = get_result(addresses_resp)
    expected = addresses

    # Verify Interface.addresses
    addresses_uri = reverse('interface-addresses', args=(site.id, ifc['id']))
    assert_success(client.retrieve(addresses_uri), expected)

    # Verify Interface.addresses by natural key
    natural_key = slugify_interface(**ifc)
    addresses_natural_uri = reverse('interface-addresses',
                                    args=(site.id, natural_key))
    assert_success(client.retrieve(addresses_natural_uri), expected)

    # Verify Interface.networks
    networks_uri = reverse('interface-networks', args=(site.id, ifc['id']))
    expected = [net]
    assert_success(client.retrieve(networks_uri), expected)

    # Verify Interface.networks by natural key
    networks_natural_uri = reverse('interface-networks',
                                   args=(site.id, natural_key))
    assert_success(client.retrieve(networks_natural_uri), expected)

    # Update the interface name to be more complex, and test again to verify
    # detail routes for more complex interface names
    params = {'name': 'xe-1/2/3:10.0'}
    ifc_resp = client.partial_update(ifc_obj_uri, **params)
    ifc = get_result(ifc_resp)
    # Build the new natural key
    natural_key = slugify_interface(**ifc)

    # Verify Interface.addresses by natural key with a complex interface name
    addresses_natural_uri = reverse('interface-addresses',
                                    args=(site.id, natural_key))
    expected = addresses
    assert_success(client.retrieve(addresses_natural_uri), expected)

    # Verify Interface.networks by natural key with a complex interface name
    networks_natural_uri = reverse('interface-networks',
                                   args=(site.id, natural_key))
    expected = [net]
    assert_success(client.retrieve(networks_natural_uri), expected)
Beispiel #9
0
def test_partial_update(site, client):
    """Test PATCH operations to partially update an Interface."""
    ifc_uri = site.list_uri('interface')
    dev_uri = site.list_uri('device')
    net_uri = site.list_uri('network')
    attr_uri = site.list_uri('attribute')

    dev_resp = client.create(dev_uri, hostname='foo-bar1')
    dev = get_result(dev_resp)

    net_resp = client.create(net_uri, cidr='10.1.1.0/24')
    net = get_result(net_resp)

    client.create(attr_uri, name='attr1', resource_name='Interface')

    # Create eth0 w/ an address
    addresses = ['10.1.1.1/32']
    ifc_resp = client.create(ifc_uri,
                             device=dev['id'],
                             name='eth0',
                             addresses=addresses,
                             attributes={'attr1': 'value'})
    ifc = get_result(ifc_resp)
    ifc_pk_uri = site.detail_uri('interface', id=ifc['id'])

    # Assert that a partial update on PUT will fail
    params = {'name': 'ge-0/0/1'}
    assert_error(client.update(ifc_pk_uri, **params),
                 status.HTTP_400_BAD_REQUEST)

    # Update only name
    payload = copy.deepcopy(ifc)
    params = {'name': 'ge-0/0/1'}
    payload.update(params)
    payload['name_slug'] = slugify_interface(**payload)
    assert_success(client.partial_update(ifc_pk_uri, **params), payload)

    # Update only attributes
    params = {'attributes': {}}  # Nuke 'em
    payload.update(params)
    assert_success(client.partial_update(ifc_pk_uri, **params), payload)

    # Update attributes by natural key
    natural_key = slugify_interface(**payload)
    ifc_natural_uri = site.detail_uri('interface', id=natural_key)
    params = {'attributes': {'attr1': 'bar'}}
    payload.update(params)
    assert_success(client.partial_update(ifc_natural_uri, **params), payload)

    # Update name and confirm natural key change
    params = {'name': 'xe-1/2/3:10.0'}
    payload.update(params)
    payload['name_slug'] = slugify_interface(**payload)
    assert_success(client.partial_update(ifc_natural_uri, **params), payload)
    # Old natural key URI should fail
    assert_error(client.get(ifc_natural_uri), status.HTTP_404_NOT_FOUND)
    # Build new natural key
    natural_key = slugify_interface(**payload)
    ifc_natural_uri = site.detail_uri('interface', id=natural_key)
    # Confirm new URI works
    assert_success(client.get(ifc_natural_uri), payload)

    # Update only addresses
    params = {'addresses': ['10.1.1.2/32']}
    payload.update(params)
    assert_success(client.partial_update(ifc_pk_uri, **params), payload)

    # Nuke addresses
    params = {'addresses': []}  # Nuke 'em!
    payload.update(params)
    payload['networks'] = []  # This will be empty, too.
    assert_success(client.partial_update(ifc_pk_uri, **params), payload)
Beispiel #10
0
def test_creation(site, client):
    """Test basic creation of an Interface."""
    ifc_uri = site.list_uri('interface')
    dev_uri = site.list_uri('device')
    net_uri = site.list_uri('network')

    dev_resp = client.create(dev_uri, hostname='foo-bar1')
    dev = get_result(dev_resp)

    dev_resp1 = client.create(dev_uri, hostname='foo-bar2')
    dev1 = get_result(dev_resp1)

    net_resp = client.create(net_uri, cidr='10.1.1.0/24')
    net = get_result(net_resp)

    # Missing required field (device, name)
    assert_error(client.create(ifc_uri, attributes={'attr1': 'foo'}),
                 status.HTTP_400_BAD_REQUEST)

    # Null name
    assert_error(client.create(ifc_uri, device=dev['id'], name=None),
                 status.HTTP_400_BAD_REQUEST)

    # Verify successful creation w/ null MAC
    ifc1_resp = client.create(
        ifc_uri,
        device=dev['id'],
        name='eth0',
        parent_id=None,
        mac_address=None,
    )
    ifc1 = get_result(ifc1_resp)
    ifc1_obj_uri = site.detail_uri('interface', id=ifc1['id'])

    assert_created(ifc1_resp, ifc1_obj_uri)

    # Verify that creating a device with parent as
    # ifc1 but device as foo-bar2 will cause error
    assert_error(
        client.create(ifc_uri,
                      device=dev1['id'],
                      name='eth0.1',
                      parent_id=ifc1['id']), status.HTTP_400_BAD_REQUEST)

    # Make sure MAC is None
    assert ifc1['mac_address'] is None

    # Create another interface with ifc1 as parent, w/ 0 MAC
    ifc2_resp = client.create(ifc_uri,
                              device=dev['id'],
                              name='eth0.0',
                              parent_id=ifc1['id'],
                              mac_address=0)
    ifc2 = get_result(ifc2_resp)
    ifc2_obj_uri = site.detail_uri('interface', id=ifc2['id'])

    assert_created(ifc2_resp, ifc2_obj_uri)

    # Create yet another interface using Device hostname
    ifc3_resp = client.create(ifc_uri, device=dev['hostname'], name='eth1.0')
    ifc3 = get_result(ifc3_resp)
    ifc3_obj_uri = site.detail_uri('interface', id=ifc3['id'])

    assert_created(ifc3_resp, ifc3_obj_uri)

    # Create an interface using Device hostname with parent_id specified by natural
    # key (!!)
    ifc4_resp = client.create(ifc_uri,
                              device=dev['hostname'],
                              name='eth1.1',
                              parent_id=ifc3['name_slug'])
    ifc4 = get_result(ifc4_resp)
    ifc4_obj_uri = site.detail_uri('interface', id=ifc4['id'])

    assert_created(ifc4_resp, ifc4_obj_uri)

    # Verify successful get of single Interface
    assert_success(client.get(ifc1_obj_uri), ifc1)

    # Verify successful get of single Interface by natural key
    natural_key = slugify_interface(**ifc1)
    ifc1_natural_uri = site.detail_uri('interface', id=natural_key)
    assert_success(client.get(ifc1_natural_uri), ifc1)

    # Verify successful retrieval of all Interfaces
    interfaces = [ifc1, ifc2, ifc3, ifc4]
    expected = interfaces
    assert_success(client.get(ifc_uri), expected)
Beispiel #11
0
def test_detail_routes(site, client):
    """Test detail routes for Interfaces objects."""
    ifc_uri = site.list_uri('interface')
    dev_uri = site.list_uri('device')
    net_uri = site.list_uri('network')

    dev_resp = client.create(dev_uri, hostname='foo-bar1')
    dev = get_result(dev_resp)

    net_resp = client.create(net_uri, cidr='10.1.1.0/24')
    net = get_result(net_resp)

    # Create a simple interface with addresses

    set_addresses = ['10.1.1.1/32', '10.1.1.2/32', '10.1.1.3/32']

    ifc_resp = client.create(
        ifc_uri, device=dev['id'], name='eth0', addresses=set_addresses
    )
    ifc = get_result(ifc_resp)
    ifc_obj_uri = site.detail_uri('interface', id=ifc['id'])

    # Fetch the Network address objects
    addresses_resp = client.retrieve(
        net_uri, include_ips=True, include_networks=False
    )
    addresses = get_result(addresses_resp)
    expected = addresses

    # Verify Interface.addresses
    addresses_uri = reverse('interface-addresses', args=(site.id, ifc['id']))
    assert_success(client.retrieve(addresses_uri), expected)

    # Verify Interface.addresses by natural key
    natural_key = slugify_interface(**ifc)
    addresses_natural_uri = reverse(
        'interface-addresses', args=(site.id, natural_key))
    assert_success(client.retrieve(addresses_natural_uri), expected)

    # Verify Interface.networks
    networks_uri = reverse('interface-networks', args=(site.id, ifc['id']))
    expected = [net]
    assert_success(client.retrieve(networks_uri), expected)

    # Verify Interface.networks by natural key
    networks_natural_uri = reverse(
        'interface-networks', args=(site.id, natural_key))
    assert_success(client.retrieve(networks_natural_uri), expected)

    # Update the interface name to be more complex, and test again to verify
    # detail routes for more complex interface names
    params = {'name': 'xe-1/2/3:10.0'}
    ifc_resp = client.partial_update(ifc_obj_uri, **params)
    ifc = get_result(ifc_resp)
    # Build the new natural key
    natural_key = slugify_interface(**ifc)

    # Verify Interface.addresses by natural key with a complex interface name
    addresses_natural_uri = reverse(
        'interface-addresses', args=(site.id, natural_key))
    expected = addresses
    assert_success(client.retrieve(addresses_natural_uri), expected)

    # Verify Interface.networks by natural key with a complex interface name
    networks_natural_uri = reverse(
        'interface-networks', args=(site.id, natural_key))
    expected = [net]
    assert_success(client.retrieve(networks_natural_uri), expected)
Beispiel #12
0
def test_partial_update(site, client):
    """Test PATCH operations to partially update an Interface."""
    ifc_uri = site.list_uri('interface')
    dev_uri = site.list_uri('device')
    net_uri = site.list_uri('network')
    attr_uri = site.list_uri('attribute')

    dev_resp = client.create(dev_uri, hostname='foo-bar1')
    dev = get_result(dev_resp)

    net_resp = client.create(net_uri, cidr='10.1.1.0/24')
    net = get_result(net_resp)

    client.create(attr_uri, name='attr1', resource_name='Interface')

    # Create eth0 w/ an address
    addresses = ['10.1.1.1/32']
    ifc_resp = client.create(
        ifc_uri, device=dev['id'], name='eth0', addresses=addresses,
        attributes={'attr1': 'value'}

    )
    ifc = get_result(ifc_resp)
    ifc_pk_uri = site.detail_uri('interface', id=ifc['id'])

    # Assert that a partial update on PUT will fail
    params = {'name': 'ge-0/0/1'}
    assert_error(
        client.update(ifc_pk_uri, **params),
        status.HTTP_400_BAD_REQUEST
    )

    # Update only name
    payload = copy.deepcopy(ifc)
    params = {'name': 'ge-0/0/1'}
    payload.update(params)
    payload['name_slug'] = slugify_interface(**payload)
    assert_success(
        client.partial_update(ifc_pk_uri, **params),
        payload
    )

    # Update only attributes
    params = {'attributes': {}}  # Nuke 'em
    payload.update(params)
    assert_success(
        client.partial_update(ifc_pk_uri, **params),
        payload
    )

    # Update attributes by natural key
    natural_key = slugify_interface(**payload)
    ifc_natural_uri = site.detail_uri('interface', id=natural_key)
    params = {'attributes': {'attr1': 'bar'}}
    payload.update(params)
    assert_success(
        client.partial_update(ifc_natural_uri, **params),
        payload
    )

    # Update name and confirm natural key change
    params = {'name': 'xe-1/2/3:10.0'}
    payload.update(params)
    payload['name_slug'] = slugify_interface(**payload)
    assert_success(
        client.partial_update(ifc_natural_uri, **params),
        payload
    )
    # Old natural key URI should fail
    assert_error(client.get(ifc_natural_uri), status.HTTP_404_NOT_FOUND)
    # Build new natural key
    natural_key = slugify_interface(**payload)
    ifc_natural_uri = site.detail_uri('interface', id=natural_key)
    # Confirm new URI works
    assert_success(client.get(ifc_natural_uri), payload)

    # Update only addresses
    params = {'addresses': ['10.1.1.2/32']}
    payload.update(params)
    assert_success(
        client.partial_update(ifc_pk_uri, **params),
        payload
    )

    # Nuke addresses
    params = {'addresses': []}  # Nuke 'em!
    payload.update(params)
    payload['networks'] = []  # This will be empty, too.
    assert_success(
        client.partial_update(ifc_pk_uri, **params),
        payload
    )
Beispiel #13
0
def test_creation(site, client):
    """Test basic creation of an Interface."""
    ifc_uri = site.list_uri('interface')
    dev_uri = site.list_uri('device')
    net_uri = site.list_uri('network')

    dev_resp = client.create(dev_uri, hostname='foo-bar1')
    dev = get_result(dev_resp)

    dev_resp1 = client.create(dev_uri, hostname='foo-bar2')
    dev1 = get_result(dev_resp1)

    net_resp = client.create(net_uri, cidr='10.1.1.0/24')
    net = get_result(net_resp)

    # Missing required field (device, name)
    assert_error(
        client.create(ifc_uri, attributes={'attr1': 'foo'}),
        status.HTTP_400_BAD_REQUEST
    )

    # Null name
    assert_error(
        client.create(ifc_uri, device=dev['id'], name=None),
        status.HTTP_400_BAD_REQUEST
    )

    # Verify successful creation w/ null MAC
    ifc1_resp = client.create(
        ifc_uri, device=dev['id'], name='eth0', parent_id=None,
        mac_address=None,
    )
    ifc1 = get_result(ifc1_resp)
    ifc1_obj_uri = site.detail_uri('interface', id=ifc1['id'])

    assert_created(ifc1_resp, ifc1_obj_uri)

    # Verify that creating a device with parent as
    # ifc1 but device as foo-bar2 will cause error
    assert_error(
        client.create(ifc_uri, device=dev1['id'], name='eth0.1', parent_id=ifc1['id']),
        status.HTTP_400_BAD_REQUEST
    )

    # Make sure MAC is None
    assert ifc1['mac_address'] is None

    # Create another interface with ifc1 as parent, w/ 0 MAC
    ifc2_resp = client.create(
        ifc_uri, device=dev['id'], name='eth0.0', parent_id=ifc1['id'],
        mac_address=0
    )
    ifc2 = get_result(ifc2_resp)
    ifc2_obj_uri = site.detail_uri('interface', id=ifc2['id'])

    assert_created(ifc2_resp, ifc2_obj_uri)

    # Create yet another interface using Device hostname
    ifc3_resp = client.create(ifc_uri, device=dev['hostname'], name='eth1.0')
    ifc3 = get_result(ifc3_resp)
    ifc3_obj_uri = site.detail_uri('interface', id=ifc3['id'])

    assert_created(ifc3_resp, ifc3_obj_uri)

    # Create an interface using Device hostname with parent_id specified by natural
    # key (!!)
    ifc4_resp = client.create(
        ifc_uri, device=dev['hostname'], name='eth1.1',
        parent_id=ifc3['name_slug']
    )
    ifc4 = get_result(ifc4_resp)
    ifc4_obj_uri = site.detail_uri('interface', id=ifc4['id'])

    assert_created(ifc4_resp, ifc4_obj_uri)

    # Verify successful get of single Interface
    assert_success(client.get(ifc1_obj_uri), ifc1)

    # Verify successful get of single Interface by natural key
    natural_key = slugify_interface(**ifc1)
    ifc1_natural_uri = site.detail_uri('interface', id=natural_key)
    assert_success(client.get(ifc1_natural_uri), ifc1)

    # Verify successful retrieval of all Interfaces
    interfaces = [ifc1, ifc2, ifc3, ifc4]
    expected = interfaces
    assert_success(client.get(ifc_uri), expected)
Beispiel #14
0
def test_update(site, client):
    """Test update of an existing interface w/ an address."""
    ifc_uri = site.list_uri('interface')
    dev_uri = site.list_uri('device')
    net_uri = site.list_uri('network')

    dev_resp = client.create(dev_uri, hostname='foo-bar1')
    dev = get_result(dev_resp)

    net_resp = client.create(net_uri, cidr='10.1.1.0/24')
    net = get_result(net_resp)

    # Create eth0 w/ an address
    addresses = ['10.1.1.1/32']
    ifc_resp = client.create(
        ifc_uri, device=dev['id'], name='eth0', addresses=addresses
    )
    ifc = get_result(ifc_resp)
    ifc_obj_uri = site.detail_uri('interface', id=ifc['id'])

    # Create eth1 w/ no address
    ifc2_resp = client.create(ifc_uri, device=dev['id'], name='eth1')
    ifc2 = get_result(ifc2_resp)
    ifc2_obj_uri = site.detail_uri('interface', id=ifc2['id'])

    # Assigning eth0's address to eth1 should fail.
    params = ifc2.copy()
    params['addresses'] = addresses
    assert_error(
        client.update(ifc2_obj_uri, **params),
        status.HTTP_400_BAD_REQUEST
    )

    # (device_id, name) must be unique
    params['name'] = 'eth0'
    assert_error(
        client.update(ifc2_obj_uri, **params),
        status.HTTP_409_CONFLICT
    )

    # Test zeroing out addresses on the Interface
    params['name'] = 'eth1'
    params['addresses'] = []
    ifc2.update(params)
    assert_success(
        client.update(ifc2_obj_uri, **params),
        ifc2
    )

    # Assign addresses by natural key
    natural_key = slugify_interface(**ifc2)
    ifc2_natural_uri = site.detail_uri('interface', id=natural_key)
    params['name'] = 'eth3'
    payload = copy.deepcopy(params)
    payload['name_slug'] = slugify_interface(**payload)

    assert_success(
        client.update(ifc2_natural_uri, **params),
        payload
    )

    # Update parent by natural key (set `ifc` as parent to `ifc2`)
    params['parent_id'] = ifc['name_slug']
    payload['parent_id'] = ifc['id']
    payload['parent'] = ifc['name_slug']
    payload['name_slug'] = slugify_interface(**payload)

    assert_success(
        client.update(ifc2_obj_uri, **params),
        payload
    )