예제 #1
0
func TestPropertyUpdate(t *testing.T) {
	client := contrail.NewClient("localhost", 8082)
	net := types.VirtualNetwork{}
	net.SetName("test")

	err := client.Create(&net)
	if err != nil {
		t.Error(err)
	}

	defer client.Delete(&net)

	props := net.GetVirtualNetworkProperties()
	if len(props.ForwardingMode) > 0 {
		t.Error(props.ForwardingMode)
	}
	props.ForwardingMode = "l2_l3"
	net.SetVirtualNetworkProperties(&props)
	err = client.Update(&net)
	if err != nil {
		t.Error(err)
	}

	obj, err := client.FindByUuid("virtual-network", net.GetUuid())
	if err != nil {
		t.Fatal(err)
	}
	netPtr := obj.(*types.VirtualNetwork)
	p2 := netPtr.GetVirtualNetworkProperties()
	if p2.ForwardingMode != "l2_l3" {
		t.Errorf("Expected: l2_l3 got: %s", p2.ForwardingMode)
	}
}
예제 #2
0
func CreateNetworkWithSubnet(
	client contrail.ApiClient, project_id, name, prefix string) (
	string, error) {

	expr := regexp.MustCompile(`(([0-9]{1,3}\.){3}[0-9]{1,3})/([0-9]{1,2})`)
	match := expr.FindStringSubmatch(prefix)
	if match == nil {
		return "", fmt.Errorf("Invalid subnet prefix %s", prefix)
	}
	address := match[1]
	prefixlen, _ := strconv.Atoi(match[3])

	obj, err := client.FindByUuid("project", project_id)
	if err != nil {
		return "", err
	}

	project := obj.(*types.Project)
	refList, err := project.GetNetworkIpams()
	if err != nil {
		return "", err
	}

	var ipam *types.NetworkIpam
	if len(refList) > 0 {
		obj, err := client.FindByUuid("network-ipam", refList[0].Uuid)
		if err != nil {
			return "", err
		}
		ipam = obj.(*types.NetworkIpam)
	} else {
		obj, err := client.FindByName("network-ipam",
			"default-domain:default-project:default-network-ipam")
		if err != nil {
			return "", err
		}
		ipam = obj.(*types.NetworkIpam)
	}

	net := new(types.VirtualNetwork)
	net.SetParent(project)
	net.SetName(name)

	subnets := types.VnSubnetsType{}
	subnets.AddIpamSubnets(
		&types.IpamSubnetType{
			Subnet: &types.SubnetType{address, prefixlen}})
	net.AddNetworkIpam(ipam, subnets)
	err = client.Create(net)
	if err != nil {
		return "", err
	}
	return net.GetUuid(), nil
}
예제 #3
0
func TestCreate(t *testing.T) {
	client := contrail.NewClient("localhost", 8082)
	net := types.VirtualNetwork{}
	net.SetName("test")
	ipam, err := client.FindByName("network-ipam",
		"default-domain:default-project:default-network-ipam")
	if err != nil {
		t.Fatal(err)
	}
	subnets := types.VnSubnetsType{}
	subnets.AddIpamSubnets(
		&types.IpamSubnetType{
			Subnet: &types.SubnetType{"10.0.0.0", 8}})
	net.AddNetworkIpam(ipam.(*types.NetworkIpam), subnets)
	err = client.Create(&net)
	if err != nil {
		t.Error(err)
	}
	if len(net.GetUuid()) == 0 {
		t.Error("No uuid assigned")
	}

	netPtr, err := client.FindByUuid("virtual-network", net.GetUuid())
	if err != nil {
		t.Error(err)
	} else {
		xnet := netPtr.(*types.VirtualNetwork)
		refs, err := xnet.GetNetworkIpamRefs()
		if err != nil {
			t.Error(err)
		} else if len(refs) > 0 {
			xattr := refs[0].Attr.(types.VnSubnetsType)
			if len(xattr.IpamSubnets) == 1 {
				isubnet := xattr.IpamSubnets[0]
				if isubnet.Subnet.IpPrefix != "10.0.0.0" ||
					isubnet.Subnet.IpPrefixLen != 8 {
					t.Errorf("Bad subnet %s/%d",
						isubnet.Subnet.IpPrefix,
						isubnet.Subnet.IpPrefixLen)
				}
			} else {
				t.Errorf("%d subnets", len(xattr.IpamSubnets))
			}
		} else {
			t.Error("Empty network-ipam reference list")
		}
	}

	err = client.Delete(&net)
	if err != nil {
		t.Error(err)
	}
}
예제 #4
0
func CreateNetwork(client contrail.ApiClient, project_id, name string) (
	string, error) {

	obj, err := client.FindByUuid("project", project_id)
	if err != nil {
		return "", err
	}

	project := obj.(*types.Project)

	net := new(types.VirtualNetwork)
	net.SetParent(project)
	net.SetName(name)

	err = client.Create(net)
	if err != nil {
		return "", err
	}
	return net.GetUuid(), nil
}
예제 #5
0
func TestReadRefs(t *testing.T) {
	client := new(ApiClient)
	client.Init()

	project := new(types.Project)
	project.SetFQName("domain", []string{"default-domain", "p1"})
	assert.NoError(t, client.Create(project))

	net := new(types.VirtualNetwork)
	net.SetFQName("project", []string{"default-domain", "p1", "n1"})
	assert.NoError(t, client.Create(net))

	vmi1 := new(types.VirtualMachineInterface)
	vmi1.SetFQName("project", []string{"default-domain", "p1", "port1"})
	vmi1.AddVirtualNetwork(net)
	assert.NoError(t, client.Create(vmi1))

	refs, err := vmi1.GetVirtualNetworkRefs()
	assert.NoError(t, err)
	assert.Len(t, refs, 1)
	assert.Equal(t, net.GetUuid(), refs[0].Uuid)
}
예제 #6
0
func buildNetworkInfo(net *types.VirtualNetwork, detail bool) (
	*NetworkInfo, error) {
	var subnets []string
	var policies []string

	refList, err := net.GetNetworkIpamRefs()
	if err != nil {
		return nil, err
	}

	for _, ref := range refList {
		attr := ref.Attr.(types.VnSubnetsType)
		for _, ipamSubnet := range attr.IpamSubnets {
			subnets = append(subnets, fmt.Sprintf("%s/%d",
				ipamSubnet.Subnet.IpPrefix,
				ipamSubnet.Subnet.IpPrefixLen))
		}
	}

	if detail {
		refList, err = net.GetNetworkPolicyRefs()
		for _, ref := range refList {
			policies = append(policies, strings.Join(ref.To, ":"))
		}
	}

	info := &NetworkInfo{
		net.GetUuid(),
		net.GetName(),
		net.GetIdPerms().Enable,
		net.GetVirtualNetworkProperties().NetworkId,
		net.GetVirtualNetworkProperties().AllowTransit,
		net.GetVirtualNetworkProperties().ForwardingMode,
		subnets,
		policies,
		net.GetRouteTargetList().RouteTarget,
	}
	return info, err
}
예제 #7
0
func TestReference(t *testing.T) {
	db := NewInMemDatabase()

	ipam := new(types.NetworkIpam)
	ipam.SetUuid(uuid.New())
	ipam.SetFQName("project", strings.Split("default-domain:p1:ipam", ":"))
	assert.NoError(t, db.Put(ipam, nil, UIDList{}))

	net := new(types.VirtualNetwork)
	net.SetUuid(uuid.New())
	net.SetFQName("project", strings.Split("default-domain:p1:network", ":"))
	subnets := types.VnSubnetsType{}
	subnets.AddIpamSubnets(
		&types.IpamSubnetType{
			Subnet: &types.SubnetType{"10.0.0.0", 8}})

	net.AddNetworkIpam(ipam, subnets)
	refs := GetReferenceList(net)
	assert.NoError(t, db.Put(net, nil, refs))

	result, err := db.GetBackReferences(parseUID(ipam.GetUuid()), "virtual_network")
	assert.NoError(t, err)
	assert.Contains(t, result, parseUID(net.GetUuid()))
}
예제 #8
0
func TestReferenceUpdate(t *testing.T) {
	client := contrail.NewClient("localhost", 8082)
	net := types.VirtualNetwork{}
	net.SetName("test2")
	ipam, err := client.FindByName("network-ipam",
		"default-domain:default-project:default-network-ipam")
	if err != nil {
		t.Fatal(err)
	}
	subnets := types.VnSubnetsType{}
	subnets.AddIpamSubnets(
		&types.IpamSubnetType{
			Subnet: &types.SubnetType{"10.0.0.0", 8}})
	net.AddNetworkIpam(ipam.(*types.NetworkIpam), subnets)
	err = client.Create(&net)
	if err != nil {
		t.Error(err)
	}

	defer client.Delete(&net)

	obj, err := client.FindByUuid("virtual-network", net.GetUuid())
	if err != nil {
		t.Fatal(err)
	}

	netPtr := obj.(*types.VirtualNetwork)
	refs, err := netPtr.GetNetworkIpamRefs()
	if err != nil {
		t.Error(err)
	} else if len(refs) > 0 {
		netPtr.DeleteNetworkIpam(refs[0].Uuid)
		nsubnets := types.VnSubnetsType{}
		nsubnets.AddIpamSubnets(
			&types.IpamSubnetType{
				Subnet: &types.SubnetType{"192.168.0.0", 16}})
		netPtr.AddNetworkIpam(ipam.(*types.NetworkIpam), nsubnets)
		client.Update(netPtr)
	} else {
		t.Error("Empty network-ipam reference list")
	}

	obj, err = client.FindByUuid("virtual-network", net.GetUuid())
	if err != nil {
		t.Fatal(err)
	}
	netPtr = obj.(*types.VirtualNetwork)
	refs, err = netPtr.GetNetworkIpamRefs()
	if err != nil {
		t.Error(err)
	} else if len(refs) != 1 {
		t.Errorf("Expected: 1 reference, %d actual", len(refs))
	} else {
		xattr := refs[0].Attr.(types.VnSubnetsType)
		if len(xattr.IpamSubnets) == 1 {
			isubnet := xattr.IpamSubnets[0]
			if isubnet.Subnet.IpPrefix != "192.168.0.0" ||
				isubnet.Subnet.IpPrefixLen != 16 {
				t.Errorf("Bad subnet %s/%d",
					isubnet.Subnet.IpPrefix,
					isubnet.Subnet.IpPrefixLen)
			}
		} else {
			t.Errorf("%d subnets", len(xattr.IpamSubnets))
		}
	}

	netPtr.ClearNetworkIpam()
	client.Update(netPtr)

	obj, err = client.FindByUuid("virtual-network", net.GetUuid())
	if err != nil {
		t.Fatal(err)
	}
	netPtr = obj.(*types.VirtualNetwork)
	refs, err = netPtr.GetNetworkIpamRefs()
	if err != nil {
		t.Error(err)
	} else if len(refs) != 0 {
		t.Errorf("Expected: 1 reference, %d actual", len(refs))
	}
}