func TestNetworkList(t *testing.T) {
	client := contrail.NewClient("localhost", 8082)

	project_id, err := config.CreateProject(client, "demo", true)
	if err != nil {
		t.Fatal(err)
	}
	defer config.DeleteProject(client, project_id)

	type TestData struct {
		Subnet string
		InList bool
	}

	data := map[string]*TestData{
		"test1": &TestData{"192.168.0.0/24", false},
		"test2": &TestData{"192.168.1.0/24", false},
		"test3": &TestData{"10.0.0.0/8", false},
		"test4": &TestData{"100.64.0.0/24", false},
	}

	for name, value := range data {
		nid, err := config.CreateNetworkWithSubnet(
			client, project_id, name, value.Subnet)
		if err != nil {
			t.Fatal(err)
		}
		defer client.DeleteByUuid("virtual-network", nid)
	}

	netList, err := config.NetworkList(client, project_id, false)
	if err != nil {
		t.Fatal(err)
	}

	if len(netList) != len(data) {
		t.Errorf("Expected %d networks, got %d",
			len(data), len(netList))
	}

	for _, net := range netList {
		fmt.Printf("%s %s %s\n", net.Uuid, net.Name, net.Subnets)
		if data[net.Name].Subnet != net.Subnets[0] {
			t.Errorf("%s subnet %s, expected %s", net.Name,
				net.Subnets, data[net.Name].Subnet)
		}
		data[net.Name].InList = true
	}
	for name, value := range data {
		if !value.InList {
			t.Errorf("%s not present", name)
		}
	}
}
Beispiel #2
0
func networkList(client *contrail.Client, flagSet *flag.FlagSet) {
	var parent_id string

	if !networkListOpts.allTenants {
		var err error
		parent_id, err = config.GetProjectId(
			client, networkCommonOpts.project,
			networkCommonOpts.project_id)
		if err != nil {
			fmt.Fprintln(os.Stderr, err)
			os.Exit(1)
		}
	}

	networkList, err := config.NetworkList(client, parent_id,
		networkListOpts.detail)
	if err != nil {
		fmt.Fprint(os.Stderr, err)
		os.Exit(1)
	}

	if networkListOpts.brief || networkListOpts.detail {
		var tmpl string
		if networkListOpts.detail {
			tmpl = networkShowDetail
		} else {
			tmpl = networkShowBrief
		}
		t := template.Must(template.New("network-list").Parse(tmpl))
		for _, n := range networkList {
			t.Execute(os.Stdout, n)
		}
	} else {
		// terse format (wide line)
		writer := new(tabwriter.Writer)
		writer.Init(os.Stdout, 0, 0, 1, ' ', 0)
		fmt.Fprintln(writer, "Network\tUuid\tSubnets")
		for _, n := range networkList {
			fmt.Fprintf(writer, "%s\t%s\t%s\n", n.Name, n.Uuid,
				strings.Join(n.Subnets, ", "))
		}
		writer.Flush()
	}
}