github.com/vmware/govmomi@v0.51.0/cli/namespace/cluster/enable_test.go (about)

     1  // © Broadcom. All Rights Reserved.
     2  // The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
     3  // SPDX-License-Identifier: Apache-2.0
     4  
     5  package cluster
     6  
     7  import (
     8  	"fmt"
     9  	"strings"
    10  	"testing"
    11  )
    12  
    13  func TestSplitCommaSeparated_Empty(t *testing.T) {
    14  	list := splitCommaSeparatedList("")
    15  	if list != nil || len(list) != 0 {
    16  		t.Fatalf("Nonempty list produced from empty string: %#v", list)
    17  	}
    18  	list = splitCommaSeparatedList(",,,")
    19  	if list != nil || len(list) != 0 {
    20  		t.Fatalf("Nonempty list produced from empty list items: %#v", list)
    21  	}
    22  }
    23  
    24  func TestSplitCommaSeparated_Values(t *testing.T) {
    25  	list := splitCommaSeparatedList("0.0.0.0/0")
    26  	if list == nil || len(list) != 1 || list[0] != "0.0.0.0/0" {
    27  		t.Fatalf("Incorrect list produced for single value: %#v", list)
    28  	}
    29  	list = splitCommaSeparatedList(",0.0.0.0/0,1.1.1.1/1,2.2.2.2/2,")
    30  	if list == nil || len(list) != 3 || list[0] != "0.0.0.0/0" || list[1] != "1.1.1.1/1" || list[2] != "2.2.2.2/2" {
    31  		t.Fatalf("Incorrect list produced for multiple values: %#v", list)
    32  	}
    33  }
    34  
    35  func TestSplitCidrList_Valid(t *testing.T) {
    36  	list := []string{"0.0.0.0/0", "1.1.1.1/1", "2.2.2.2/2"}
    37  	cidrs, err := splitCidrList(list)
    38  	if err != nil || len(cidrs) != 3 {
    39  		t.Fatalf("Wrong size list produced in cidr splitting: %#v", list)
    40  	}
    41  	for i, cidr := range cidrs {
    42  		if cidr.Prefix != i || cidr.Address != fmt.Sprintf("%[1]d.%[1]d.%[1]d.%[1]d", i) {
    43  			t.Fatalf("Invalid value set in cidr %d: %#v", i, cidr)
    44  		}
    45  	}
    46  }
    47  
    48  func TestSplitCidrList_Invalid(t *testing.T) {
    49  	list := []string{"abc", "1.1.1.1/1", "2.2.2.2/2"}
    50  	_, err := splitCidrList(list)
    51  	if err == nil {
    52  		t.Error("Error not produced trying to split an invalid cidr")
    53  	} else if !strings.Contains(err.Error(), "invalid cidr") {
    54  		t.Errorf("Unexpected error produced trying to split an invalid cidr: %s", err)
    55  	}
    56  
    57  	list = []string{"/24", "1.1.1.1/1", "2.2.2.2/2"}
    58  	_, err = splitCidrList(list)
    59  	if err == nil {
    60  		t.Error("Error not produced trying to split an invalid cidr")
    61  	} else if !strings.Contains(err.Error(), "parsing cidr") {
    62  		t.Errorf("Unexpected error produced trying to split an invalid cidr: %s", err)
    63  	}
    64  
    65  	list = []string{"abc/abc", "1.1.1.1/1", "2.2.2.2/2"}
    66  	_, err = splitCidrList(list)
    67  	if err == nil {
    68  		t.Error("Error not produced trying to split an invalid cidr")
    69  	} else if !strings.Contains(err.Error(), "parsing cidr") {
    70  		t.Errorf("Unexpected error produced trying to split an invalid cidr: %s", err)
    71  	}
    72  
    73  }