github.com/vmware/govmomi@v0.37.1/govc/namespace/cluster/enable_test.go (about) 1 /* 2 Copyright (c) 2020 VMware, Inc. All Rights Reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package cluster 18 19 import ( 20 "fmt" 21 "strings" 22 "testing" 23 ) 24 25 func TestSplitCommaSeparated_Empty(t *testing.T) { 26 list := splitCommaSeparatedList("") 27 if list != nil || len(list) != 0 { 28 t.Fatalf("Nonempty list produced from empty string: %#v", list) 29 } 30 list = splitCommaSeparatedList(",,,") 31 if list != nil || len(list) != 0 { 32 t.Fatalf("Nonempty list produced from empty list items: %#v", list) 33 } 34 } 35 36 func TestSplitCommaSeparated_Values(t *testing.T) { 37 list := splitCommaSeparatedList("0.0.0.0/0") 38 if list == nil || len(list) != 1 || list[0] != "0.0.0.0/0" { 39 t.Fatalf("Incorrect list produced for single value: %#v", list) 40 } 41 list = splitCommaSeparatedList(",0.0.0.0/0,1.1.1.1/1,2.2.2.2/2,") 42 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" { 43 t.Fatalf("Incorrect list produced for multiple values: %#v", list) 44 } 45 } 46 47 func TestSplitCidrList_Valid(t *testing.T) { 48 list := []string{"0.0.0.0/0", "1.1.1.1/1", "2.2.2.2/2"} 49 cidrs, err := splitCidrList(list) 50 if err != nil || len(cidrs) != 3 { 51 t.Fatalf("Wrong size list produced in cidr splitting: %#v", list) 52 } 53 for i, cidr := range cidrs { 54 if cidr.Prefix != i || cidr.Address != fmt.Sprintf("%[1]d.%[1]d.%[1]d.%[1]d", i) { 55 t.Fatalf("Invalid value set in cidr %d: %#v", i, cidr) 56 } 57 } 58 } 59 60 func TestSplitCidrList_Invalid(t *testing.T) { 61 list := []string{"abc", "1.1.1.1/1", "2.2.2.2/2"} 62 _, err := splitCidrList(list) 63 if err == nil { 64 t.Error("Error not produced trying to split an invalid cidr") 65 } else if !strings.Contains(err.Error(), "invalid cidr") { 66 t.Errorf("Unexpected error produced trying to split an invalid cidr: %s", err) 67 } 68 69 list = []string{"/24", "1.1.1.1/1", "2.2.2.2/2"} 70 _, err = splitCidrList(list) 71 if err == nil { 72 t.Error("Error not produced trying to split an invalid cidr") 73 } else if !strings.Contains(err.Error(), "parsing cidr") { 74 t.Errorf("Unexpected error produced trying to split an invalid cidr: %s", err) 75 } 76 77 list = []string{"abc/abc", "1.1.1.1/1", "2.2.2.2/2"} 78 _, err = splitCidrList(list) 79 if err == nil { 80 t.Error("Error not produced trying to split an invalid cidr") 81 } else if !strings.Contains(err.Error(), "parsing cidr") { 82 t.Errorf("Unexpected error produced trying to split an invalid cidr: %s", err) 83 } 84 85 }