1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
|
package cidr
import (
"fmt"
"testing"
"github.com/zclconf/go-cty/cty"
)
func TestSubnets(t *testing.T) {
tests := []struct {
Prefix cty.Value
Newbits []cty.Value
Want cty.Value
Err string
}{
{
cty.StringVal("10.0.0.0/21"),
[]cty.Value{
cty.NumberIntVal(3),
cty.NumberIntVal(3),
cty.NumberIntVal(3),
cty.NumberIntVal(4),
cty.NumberIntVal(4),
cty.NumberIntVal(4),
cty.NumberIntVal(7),
cty.NumberIntVal(7),
cty.NumberIntVal(7),
},
cty.ListVal([]cty.Value{
cty.StringVal("10.0.0.0/24"),
cty.StringVal("10.0.1.0/24"),
cty.StringVal("10.0.2.0/24"),
cty.StringVal("10.0.3.0/25"),
cty.StringVal("10.0.3.128/25"),
cty.StringVal("10.0.4.0/25"),
cty.StringVal("10.0.4.128/28"),
cty.StringVal("10.0.4.144/28"),
cty.StringVal("10.0.4.160/28"),
}),
``,
},
{
cty.StringVal("10.0.0.0/30"),
[]cty.Value{
cty.NumberIntVal(1),
cty.NumberIntVal(3),
},
cty.UnknownVal(cty.List(cty.String)),
`would extend prefix to 33 bits, which is too long for an IPv4 address`,
},
{
cty.StringVal("10.0.0.0/8"),
[]cty.Value{
cty.NumberIntVal(1),
cty.NumberIntVal(1),
cty.NumberIntVal(1),
},
cty.UnknownVal(cty.List(cty.String)),
`not enough remaining address space for a subnet with a prefix of 9 bits after 10.128.0.0/9`,
},
{
cty.StringVal("10.0.0.0/8"),
[]cty.Value{
cty.NumberIntVal(1),
cty.NumberIntVal(0),
},
cty.UnknownVal(cty.List(cty.String)),
`must extend prefix by at least one bit`,
},
{
cty.StringVal("10.0.0.0/8"),
[]cty.Value{
cty.NumberIntVal(1),
cty.NumberIntVal(-1),
},
cty.UnknownVal(cty.List(cty.String)),
`must extend prefix by at least one bit`,
},
{
cty.StringVal("fe80::/48"),
[]cty.Value{
cty.NumberIntVal(1),
cty.NumberIntVal(33),
},
cty.UnknownVal(cty.List(cty.String)),
`may not extend prefix by more than 32 bits`,
},
}
for _, test := range tests {
t.Run(fmt.Sprintf("cidrsubnets(%#v, %#v)", test.Prefix, test.Newbits), func(t *testing.T) {
got, err := Subnets(test.Prefix, test.Newbits...)
wantErr := test.Err != ""
if wantErr {
if err == nil {
t.Fatal("succeeded; want error")
}
if err.Error() != test.Err {
t.Fatalf("wrong error\ngot: %s\nwant: %s", err.Error(), test.Err)
}
return
} else if err != nil {
t.Fatalf("unexpected error: %s", err)
}
if !got.RawEquals(test.Want) {
t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, test.Want)
}
})
}
}
|