summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDave Henderson <dhenderson@gmail.com>2022-03-31 18:29:20 -0400
committerDave Henderson <dhenderson@gmail.com>2022-05-06 22:58:04 -0400
commit254de4e4e97fe1550d7092bbf25a8a6c31f0fd74 (patch)
tree43130543e5ddb045db63a8b90d689c44ceb17e6d
parent2f294d7884667800579e3ac3a16241e2df8698bb (diff)
Rename to CIDR*, generate docs
Signed-off-by: Dave Henderson <dhenderson@gmail.com>
-rw-r--r--docs-src/content/functions/net.yml42
-rw-r--r--docs/content/functions/net.md139
-rw-r--r--funcs/net.go108
-rw-r--r--funcs/net_test.go70
-rw-r--r--internal/tests/integration/integration_test.go9
-rw-r--r--internal/tests/integration/net_test.go32
6 files changed, 293 insertions, 107 deletions
diff --git a/docs-src/content/functions/net.yml b/docs-src/content/functions/net.yml
index ffc7f661..67b7664a 100644
--- a/docs-src/content/functions/net.yml
+++ b/docs-src/content/functions/net.yml
@@ -193,15 +193,16 @@ funcs:
$ gomplate -i '{{ $range := net.ParseIPRange "1.2.3.0-1.2.3.233" -}}
{{ $range.Prefixes }}'
[1.2.3.0/25 1.2.3.128/26 1.2.3.192/27]
- - name: net.CidrHost
+ - name: net.CIDRHost
+ experimental: true
description: |
Calculates a full host IP address for a given host number within a given IP network address prefix.
The IP network can be in the form `"192.168.1.0/24"` or `"2001::db8::/32"`,
the CIDR notations defined in [RFC 4632][] and [RFC 4291][].
- Any of `net.IP`'s methods may be called on the resulting value. See
- [the docs](https://pkg.go.dev/net) for details.
+ Any of `netip.Addr`'s methods may be called on the resulting value. See
+ [the docs](https://pkg.go.dev/net/netip#Addr) for details.
pipeline: true
arguments:
- name: hostnum
@@ -212,14 +213,15 @@ funcs:
description: Must be given in CIDR notation. It must represent either an IPv4 or IPv6 prefix, containing a `/`. String or [`net.IPNet`](https://pkg.go.dev/net#IPNet) object returned from `net.ParseIPPrefix` can by used.
examples:
- |
- $ gomplate -i '{{ "10.12.127.0/20" | net.CidrHost 268 }}'
+ $ gomplate -i '{{ "10.12.127.0/20" | net.CIDRHost 268 }}'
10.12.113.12
- - name: net.CidrNetmask
+ - name: net.CIDRNetmask
+ experimental: true
description: |
The result is a subnet address formatted in the conventional dotted-decimal IPv4 address syntax or hexadecimal IPv6 address syntax, as expected by some software.
- Any of `net.IP`'s methods may be called on the resulting value. See
- [the docs](https://pkg.go.dev/net) for details.
+ Any of `netip.Addr`'s methods may be called on the resulting value. See
+ [the docs](https://pkg.go.dev/net/netip#Addr) for details.
pipeline: true
arguments:
- name: prefix
@@ -227,16 +229,17 @@ funcs:
description: Must be given in CIDR notation. It must represent either an IPv4 or IPv6 prefix, containing a `/`. String or [`net.IPNet`](https://pkg.go.dev/net#IPNet) object returned from `net.ParseIPPrefix` can by used.
examples:
- |
- $ gomplate -i '{{ net.CidrNetmask "10.12.127.0/20" }}'
+ $ gomplate -i '{{ net.CIDRNetmask "10.12.127.0/20" }}'
255.255.240.0
- $ gomplate -i '{{ net.CidrNetmask "fd00:fd12:3456:7890:00a2::/72" }}'
+ $ gomplate -i '{{ net.CIDRNetmask "fd00:fd12:3456:7890:00a2::/72" }}'
ffff:ffff:ffff:ffff:ff00::
- - name: net.CidrSubnets
+ - name: net.CIDRSubnets
+ experimental: true
description: |
Calculates a subnet address within given IP network address prefix.
- Any of `net.IPNet`'s methods may be called on the resulting value. See
- [the docs](https://pkg.go.dev/net) for details.
+ Any of `netip.Prefix`'s methods may be called on the resulting values. See
+ [the docs](https://pkg.go.dev/net/netip#Prefix) for details.
pipeline: true
arguments:
- name: newbits
@@ -247,16 +250,17 @@ funcs:
description: Must be given in CIDR notation. It must represent either an IPv4 or IPv6 prefix, containing a `/`. String or [`net.IPNet`](https://pkg.go.dev/net#IPNet) object returned from `net.ParseIPPrefix` can by used.
examples:
- |
- $ gomplate -i '{{ index ("10.0.0.0/16" | net.CidrSubnets 2) 1 }}'
+ $ gomplate -i '{{ index ("10.0.0.0/16" | net.CIDRSubnets 2) 1 }}'
10.0.64.0/18
- $ gomplate -i '{{ net.CidrSubnets 2 "10.0.0.0/16" -}}'
+ $ gomplate -i '{{ net.CIDRSubnets 2 "10.0.0.0/16" -}}'
[10.0.0.0/18 10.0.64.0/18 10.0.128.0/18 10.0.192.0/18]
- - name: net.CidrSubnetSizes
+ - name: net.CIDRSubnetSizes
+ experimental: true
description: |
Calculates a sequence of consecutive IP address ranges within a particular CIDR prefix.
- Any of `net.IPNet`'s methods may be called on the resulting value. See
- [the docs](https://pkg.go.dev/net) for details.
+ Any of `netip.Prefix`'s methods may be called on the resulting values. See
+ [the docs](https://pkg.go.dev/net/netip#Prefix) for details.
pipeline: true
arguments:
- name: newbits...
@@ -267,5 +271,5 @@ funcs:
description: Must be given in CIDR notation. It must represent either an IPv4 or IPv6 prefix, containing a `/`. String or [`net.IPNet`](https://pkg.go.dev/net#IPNet) object returned from `net.ParseIPPrefix` can by used.
examples:
- |
- $ gomplate -i '{{ net.CidrSubnetSizes 4 4 8 4 "10.1.0.0/16" -}}'
- [10.1.0.0/20 10.1.16.0/20 10.1.32.0/24 10.1.48.0/20] \ No newline at end of file
+ $ gomplate -i '{{ net.CIDRSubnetSizes 4 4 8 4 "10.1.0.0/16" -}}'
+ [10.1.0.0/20 10.1.16.0/20 10.1.32.0/24 10.1.48.0/20]
diff --git a/docs/content/functions/net.md b/docs/content/functions/net.md
index bbea71e0..ca0d8da1 100644
--- a/docs/content/functions/net.md
+++ b/docs/content/functions/net.md
@@ -13,6 +13,7 @@ calculations.
[RFC 4632]: http://tools.ietf.org/html/rfc4632
[RFC 4291]: http://tools.ietf.org/html/rfc4291
[`inet.af/netaddr`]: https://pkg.go.dev/inet.af/netaddr
+[`net`]: https://pkg.go.dev/net
## `net.LookupIP`
@@ -320,3 +321,141 @@ $ gomplate -i '{{ $range := net.ParseIPRange "1.2.3.0-1.2.3.233" -}}
{{ $range.Prefixes }}'
[1.2.3.0/25 1.2.3.128/26 1.2.3.192/27]
```
+
+## `net.CIDRHost` _(experimental)_
+**Experimental:** This function is [_experimental_][experimental] and may be enabled with the [`--experimental`][experimental] flag.
+
+[experimental]: ../config/#experimental
+
+Calculates a full host IP address for a given host number within a given IP network address prefix.
+
+The IP network can be in the form `"192.168.1.0/24"` or `"2001::db8::/32"`,
+the CIDR notations defined in [RFC 4632][] and [RFC 4291][].
+
+Any of `netip.Addr`'s methods may be called on the resulting value. See
+[the docs](https://pkg.go.dev/net/netip#Addr) for details.
+
+### Usage
+
+```go
+net.CIDRHost hostnum prefix
+```
+```go
+prefix | net.CIDRHost hostnum
+```
+
+### Arguments
+
+| name | description |
+|------|-------------|
+| `hostnum` | _(required)_ Is a whole number that can be represented as a binary integer with no more than the number of digits remaining in the address after the given prefix. |
+| `prefix` | _(required)_ Must be given in CIDR notation. It must represent either an IPv4 or IPv6 prefix, containing a `/`. String or [`net.IPNet`](https://pkg.go.dev/net#IPNet) object returned from `net.ParseIPPrefix` can by used. |
+
+### Examples
+
+```console
+$ gomplate -i '{{ "10.12.127.0/20" | net.CIDRHost 268 }}'
+10.12.113.12
+```
+
+## `net.CIDRNetmask` _(experimental)_
+**Experimental:** This function is [_experimental_][experimental] and may be enabled with the [`--experimental`][experimental] flag.
+
+[experimental]: ../config/#experimental
+
+The result is a subnet address formatted in the conventional dotted-decimal IPv4 address syntax or hexadecimal IPv6 address syntax, as expected by some software.
+
+Any of `netip.Addr`'s methods may be called on the resulting value. See
+[the docs](https://pkg.go.dev/net/netip#Addr) for details.
+
+### Usage
+
+```go
+net.CIDRNetmask prefix
+```
+```go
+prefix | net.CIDRNetmask
+```
+
+### Arguments
+
+| name | description |
+|------|-------------|
+| `prefix` | _(required)_ Must be given in CIDR notation. It must represent either an IPv4 or IPv6 prefix, containing a `/`. String or [`net.IPNet`](https://pkg.go.dev/net#IPNet) object returned from `net.ParseIPPrefix` can by used. |
+
+### Examples
+
+```console
+$ gomplate -i '{{ net.CIDRNetmask "10.12.127.0/20" }}'
+255.255.240.0
+$ gomplate -i '{{ net.CIDRNetmask "fd00:fd12:3456:7890:00a2::/72" }}'
+ffff:ffff:ffff:ffff:ff00::
+```
+
+## `net.CIDRSubnets` _(experimental)_
+**Experimental:** This function is [_experimental_][experimental] and may be enabled with the [`--experimental`][experimental] flag.
+
+[experimental]: ../config/#experimental
+
+Calculates a subnet address within given IP network address prefix.
+
+Any of `netip.Prefix`'s methods may be called on the resulting values. See
+[the docs](https://pkg.go.dev/net/netip#Prefix) for details.
+
+### Usage
+
+```go
+net.CIDRSubnets newbits prefix
+```
+```go
+prefix | net.CIDRSubnets newbits
+```
+
+### Arguments
+
+| name | description |
+|------|-------------|
+| `newbits` | _(required)_ Is the number of additional bits with which to extend the prefix. For example, if given a prefix ending in `/16` and a `newbits` value of `4`, the resulting subnet address will have length `/20`. |
+| `prefix` | _(required)_ Must be given in CIDR notation. It must represent either an IPv4 or IPv6 prefix, containing a `/`. String or [`net.IPNet`](https://pkg.go.dev/net#IPNet) object returned from `net.ParseIPPrefix` can by used. |
+
+### Examples
+
+```console
+$ gomplate -i '{{ index ("10.0.0.0/16" | net.CIDRSubnets 2) 1 }}'
+10.0.64.0/18
+$ gomplate -i '{{ net.CIDRSubnets 2 "10.0.0.0/16" -}}'
+[10.0.0.0/18 10.0.64.0/18 10.0.128.0/18 10.0.192.0/18]
+```
+
+## `net.CIDRSubnetSizes` _(experimental)_
+**Experimental:** This function is [_experimental_][experimental] and may be enabled with the [`--experimental`][experimental] flag.
+
+[experimental]: ../config/#experimental
+
+Calculates a sequence of consecutive IP address ranges within a particular CIDR prefix.
+
+Any of `netip.Prefix`'s methods may be called on the resulting values. See
+[the docs](https://pkg.go.dev/net/netip#Prefix) for details.
+
+### Usage
+
+```go
+net.CIDRSubnetSizes newbits... prefix
+```
+```go
+prefix | net.CIDRSubnetSizes newbits...
+```
+
+### Arguments
+
+| name | description |
+|------|-------------|
+| `newbits...` | _(required)_ Numbers of additional network prefix bits for returned address range. |
+| `prefix` | _(required)_ Must be given in CIDR notation. It must represent either an IPv4 or IPv6 prefix, containing a `/`. String or [`net.IPNet`](https://pkg.go.dev/net#IPNet) object returned from `net.ParseIPPrefix` can by used. |
+
+### Examples
+
+```console
+$ gomplate -i '{{ net.CIDRSubnetSizes 4 4 8 4 "10.1.0.0/16" -}}'
+[10.1.0.0/20 10.1.16.0/20 10.1.32.0/24 10.1.48.0/20]
+```
diff --git a/funcs/net.go b/funcs/net.go
index 2755b9b1..2790b194 100644
--- a/funcs/net.go
+++ b/funcs/net.go
@@ -86,70 +86,87 @@ func (f NetFuncs) ParseIPRange(iprange interface{}) (netaddr.IPRange, error) {
}
func (f NetFuncs) parseStdnetIPNet(prefix interface{}) (*stdnet.IPNet, error) {
- if n, ok := prefix.(*stdnet.IPNet); ok {
- return n, nil
- }
- if n, ok := prefix.(netaddr.IPPrefix); ok {
- return n.Masked().IPNet(), nil
- }
- if n, ok := prefix.(netip.Prefix); ok {
+ switch p := prefix.(type) {
+ case *stdnet.IPNet:
+ return p, nil
+ case netaddr.IPPrefix:
+ return p.Masked().IPNet(), nil
+ case netip.Prefix:
net := &stdnet.IPNet{
- IP: n.Masked().Addr().AsSlice(),
- Mask: stdnet.CIDRMask(n.Bits(), n.Addr().BitLen()),
+ IP: p.Masked().Addr().AsSlice(),
+ Mask: stdnet.CIDRMask(p.Bits(), p.Addr().BitLen()),
}
return net, nil
+ default:
+ _, network, err := stdnet.ParseCIDR(conv.ToString(prefix))
+ return network, err
}
+}
- _, network, err := stdnet.ParseCIDR(conv.ToString(prefix))
- return network, err
+func (f NetFuncs) parseNetipPrefix(prefix interface{}) (netip.Prefix, error) {
+ switch p := prefix.(type) {
+ case *stdnet.IPNet:
+ return f.ipPrefixFromIPNet(p), nil
+ case netaddr.IPPrefix:
+ return f.ipPrefixFromIPNet(p.Masked().IPNet()), nil
+ case netip.Prefix:
+ return p, nil
+ default:
+ return netip.ParsePrefix(conv.ToString(prefix))
+ }
}
-func (f NetFuncs) ipFromNetIp(n stdnet.IP) netaddr.IP {
- ip, _ := netaddr.FromStdIP(n)
+func (f NetFuncs) ipFromNetIP(n stdnet.IP) netip.Addr {
+ ip, _ := netip.AddrFromSlice(n)
return ip
}
-func (f NetFuncs) ipPrefixFromIpNet(n *stdnet.IPNet) netaddr.IPPrefix {
- prefix, _ := netaddr.FromStdIPNet(n)
- return prefix
+func (f NetFuncs) ipPrefixFromIPNet(n *stdnet.IPNet) netip.Prefix {
+ ip, _ := netip.AddrFromSlice(n.IP)
+ ones, _ := n.Mask.Size()
+ return netip.PrefixFrom(ip, ones)
}
-// func (f NetFuncs) addrFromNetIp(n stdnet.IP) netip.Addr {
-// ip, _ := netip.AddrFromSlice(n)
-// return ip
-// }
-
-// func (f NetFuncs) prefixFromIpNet(n *stdnet.IPNet) netip.Prefix {
-// ip, _ := netip.AddrFromSlice(n.IP)
-// bits, _ := n.Mask.Size()
-// prefix := netip.PrefixFrom(ip, bits)
-// return prefix
-// }
+// CIDRHost -
+// Experimental!
+func (f NetFuncs) CIDRHost(hostnum interface{}, prefix interface{}) (netip.Addr, error) {
+ if err := checkExperimental(f.ctx); err != nil {
+ return netip.Addr{}, err
+ }
-// CidrHost -
-func (f NetFuncs) CidrHost(hostnum interface{}, prefix interface{}) (netaddr.IP, error) {
network, err := f.parseStdnetIPNet(prefix)
if err != nil {
- return netaddr.IP{}, err
+ return netip.Addr{}, err
}
ip, err := cidr.HostBig(network, big.NewInt(conv.ToInt64(hostnum)))
- return f.ipFromNetIp(ip), err
+
+ return f.ipFromNetIP(ip), err
}
-// CidrNetmask -
-func (f NetFuncs) CidrNetmask(prefix interface{}) (netaddr.IP, error) {
+// CIDRNetmask -
+// Experimental!
+func (f NetFuncs) CIDRNetmask(prefix interface{}) (netip.Addr, error) {
+ if err := checkExperimental(f.ctx); err != nil {
+ return netip.Addr{}, err
+ }
+
network, err := f.parseStdnetIPNet(prefix)
if err != nil {
- return netaddr.IP{}, err
+ return netip.Addr{}, err
}
netmask := stdnet.IP(network.Mask)
- return f.ipFromNetIp(netmask), nil
+ return f.ipFromNetIP(netmask), nil
}
-// CidrSubnets -
-func (f NetFuncs) CidrSubnets(newbits interface{}, prefix interface{}) ([]netaddr.IPPrefix, error) {
+// CIDRSubnets -
+// Experimental!
+func (f NetFuncs) CIDRSubnets(newbits interface{}, prefix interface{}) ([]netip.Prefix, error) {
+ if err := checkExperimental(f.ctx); err != nil {
+ return nil, err
+ }
+
network, err := f.parseStdnetIPNet(prefix)
if err != nil {
return nil, err
@@ -161,20 +178,25 @@ func (f NetFuncs) CidrSubnets(newbits interface{}, prefix interface{}) ([]netadd
}
maxNetNum := int64(1 << uint64(nBits))
- retValues := make([]netaddr.IPPrefix, maxNetNum)
+ retValues := make([]netip.Prefix, maxNetNum)
for i := int64(0); i < maxNetNum; i++ {
subnet, err := cidr.SubnetBig(network, nBits, big.NewInt(i))
if err != nil {
return nil, err
}
- retValues[i] = f.ipPrefixFromIpNet(subnet)
+ retValues[i] = f.ipPrefixFromIPNet(subnet)
}
return retValues, nil
}
-// CidrSubnetSizes -
-func (f NetFuncs) CidrSubnetSizes(args ...interface{}) ([]netaddr.IPPrefix, error) {
+// CIDRSubnetSizes -
+// Experimental!
+func (f NetFuncs) CIDRSubnetSizes(args ...interface{}) ([]netip.Prefix, error) {
+ if err := checkExperimental(f.ctx); err != nil {
+ return nil, err
+ }
+
if len(args) < 2 {
return nil, errors.Errorf("wrong number of args: want 2 or more, got %d", len(args))
}
@@ -189,7 +211,7 @@ func (f NetFuncs) CidrSubnetSizes(args ...interface{}) ([]netaddr.IPPrefix, erro
firstLength := newbits[0]
firstLength += startPrefixLen
- retValues := make([]netaddr.IPPrefix, len(newbits))
+ retValues := make([]netip.Prefix, len(newbits))
current, _ := cidr.PreviousSubnet(network, firstLength)
@@ -226,7 +248,7 @@ func (f NetFuncs) CidrSubnetSizes(args ...interface{}) ([]netaddr.IPPrefix, erro
return nil, errors.Errorf("not enough remaining address space for a subnet with a prefix of %d bits after %s", length, current.String())
}
current = next
- retValues[i] = f.ipPrefixFromIpNet(current)
+ retValues[i] = f.ipPrefixFromIPNet(current)
}
return retValues, nil
diff --git a/funcs/net_test.go b/funcs/net_test.go
index e640e471..0b20ade9 100644
--- a/funcs/net_test.go
+++ b/funcs/net_test.go
@@ -7,6 +7,7 @@ import (
"strconv"
"testing"
+ "github.com/hairyhenderson/gomplate/v3/internal/config"
"github.com/stretchr/testify/assert"
"inet.af/netaddr"
)
@@ -72,79 +73,90 @@ func TestParseIPRange(t *testing.T) {
assert.Equal(t, "192.168.0.2-192.168.23.255", iprange.String())
}
-func TestCidrHost(t *testing.T) {
- n := NetFuncs{}
+func testNetNS() *NetFuncs {
+ ctx := context.Background()
+ cfg := config.FromContext(ctx)
+ cfg.Experimental = true
+ ctx = config.ContextWithConfig(ctx, cfg)
+
+ return &NetFuncs{
+ ctx: ctx,
+ }
+}
+
+func TestCIDRHost(t *testing.T) {
+ n := testNetNS()
// net.IPNet
_, netIP, _ := stdnet.ParseCIDR("10.12.127.0/20")
- ip, err := n.CidrHost(16, netIP)
+ ip, err := n.CIDRHost(16, netIP)
assert.NoError(t, err)
assert.Equal(t, "10.12.112.16", ip.String())
- ip, err = n.CidrHost(268, netIP)
+ ip, err = n.CIDRHost(268, netIP)
assert.NoError(t, err)
assert.Equal(t, "10.12.113.12", ip.String())
_, netIP, _ = stdnet.ParseCIDR("fd00:fd12:3456:7890:00a2::/72")
- ip, err = n.CidrHost(34, netIP)
+ ip, err = n.CIDRHost(34, netIP)
assert.NoError(t, err)
assert.Equal(t, "fd00:fd12:3456:7890::22", ip.String())
// inet.af/netaddr.IPPrefix
ipPrefix, _ := n.ParseIPPrefix("10.12.127.0/20")
- ip, err = n.CidrHost(16, ipPrefix)
+ ip, err = n.CIDRHost(16, ipPrefix)
assert.NoError(t, err)
assert.Equal(t, "10.12.112.16", ip.String())
- ip, err = n.CidrHost(268, ipPrefix)
+ ip, err = n.CIDRHost(268, ipPrefix)
assert.NoError(t, err)
assert.Equal(t, "10.12.113.12", ip.String())
ipPrefix, _ = n.ParseIPPrefix("fd00:fd12:3456:7890:00a2::/72")
- ip, err = n.CidrHost(34, ipPrefix)
+ ip, err = n.CIDRHost(34, ipPrefix)
assert.NoError(t, err)
assert.Equal(t, "fd00:fd12:3456:7890::22", ip.String())
// net/netip.Prefix
- prefix, _ := netip.ParsePrefix("10.12.127.0/20")
+ prefix := netip.MustParsePrefix("10.12.127.0/20")
- ip, err = n.CidrHost(16, prefix)
+ ip, err = n.CIDRHost(16, prefix)
assert.NoError(t, err)
assert.Equal(t, "10.12.112.16", ip.String())
- ip, err = n.CidrHost(268, prefix)
+ ip, err = n.CIDRHost(268, prefix)
assert.NoError(t, err)
assert.Equal(t, "10.12.113.12", ip.String())
- prefix, _ = netip.ParsePrefix("fd00:fd12:3456:7890:00a2::/72")
- ip, err = n.CidrHost(34, prefix)
+ prefix = netip.MustParsePrefix("fd00:fd12:3456:7890:00a2::/72")
+ ip, err = n.CIDRHost(34, prefix)
assert.NoError(t, err)
assert.Equal(t, "fd00:fd12:3456:7890::22", ip.String())
}
-func TestCidrNetmask(t *testing.T) {
- n := NetFuncs{}
+func TestCIDRNetmask(t *testing.T) {
+ n := testNetNS()
- ip, err := n.CidrNetmask("10.0.0.0/12")
+ ip, err := n.CIDRNetmask("10.0.0.0/12")
assert.NoError(t, err)
assert.Equal(t, "255.240.0.0", ip.String())
- ip, err = n.CidrNetmask("fd00:fd12:3456:7890:00a2::/72")
+ ip, err = n.CIDRNetmask("fd00:fd12:3456:7890:00a2::/72")
assert.NoError(t, err)
assert.Equal(t, "ffff:ffff:ffff:ffff:ff00::", ip.String())
}
-func TestCidrSubnets(t *testing.T) {
- n := NetFuncs{}
- _, network, _ := stdnet.ParseCIDR("10.0.0.0/16")
+func TestCIDRSubnets(t *testing.T) {
+ n := testNetNS()
+ network := netip.MustParsePrefix("10.0.0.0/16")
- subnets, err := n.CidrSubnets(-1, network)
+ subnets, err := n.CIDRSubnets(-1, network)
assert.Nil(t, subnets)
assert.Error(t, err)
- subnets, err = n.CidrSubnets(2, network)
+ subnets, err = n.CIDRSubnets(2, network)
assert.NoError(t, err)
assert.Len(t, subnets, 4)
assert.Equal(t, "10.0.0.0/18", subnets[0].String())
@@ -153,23 +165,23 @@ func TestCidrSubnets(t *testing.T) {
assert.Equal(t, "10.0.192.0/18", subnets[3].String())
}
-func TestCidrSubnetSizes(t *testing.T) {
- n := NetFuncs{}
- _, network, _ := stdnet.ParseCIDR("10.1.0.0/16")
+func TestCIDRSubnetSizes(t *testing.T) {
+ n := testNetNS()
+ network := netip.MustParsePrefix("10.1.0.0/16")
- subnets, err := n.CidrSubnetSizes(network)
+ subnets, err := n.CIDRSubnetSizes(network)
assert.Nil(t, subnets)
assert.Error(t, err)
- subnets, err = n.CidrSubnetSizes(32, network)
+ subnets, err = n.CIDRSubnetSizes(32, network)
assert.Nil(t, subnets)
assert.Error(t, err)
- subnets, err = n.CidrSubnetSizes(-1, network)
+ subnets, err = n.CIDRSubnetSizes(-1, network)
assert.Nil(t, subnets)
assert.Error(t, err)
- subnets, err = n.CidrSubnetSizes(4, 4, 8, 4, network)
+ subnets, err = n.CIDRSubnetSizes(4, 4, 8, 4, network)
assert.NoError(t, err)
assert.Len(t, subnets, 4)
assert.Equal(t, "10.1.0.0/20", subnets[0].String())
diff --git a/internal/tests/integration/integration_test.go b/internal/tests/integration/integration_test.go
index 7b8b7694..b1fd3c63 100644
--- a/internal/tests/integration/integration_test.go
+++ b/internal/tests/integration/integration_test.go
@@ -33,6 +33,15 @@ func inOutTest(t *testing.T, i, o string) {
assert.Equal(t, o, stdout)
}
+func inOutTestExperimental(t *testing.T, i, o string) {
+ t.Helper()
+
+ stdout, stderr, err := cmd(t, "--experimental", "-i", i).run()
+ assert.NoError(t, err)
+ assert.Equal(t, "", stderr)
+ assert.Equal(t, o, stdout)
+}
+
func inOutContains(t *testing.T, i, o string) {
t.Helper()
diff --git a/internal/tests/integration/net_test.go b/internal/tests/integration/net_test.go
index ac2c8240..35218eaf 100644
--- a/internal/tests/integration/net_test.go
+++ b/internal/tests/integration/net_test.go
@@ -8,22 +8,22 @@ func TestNet_LookupIP(t *testing.T) {
inOutTest(t, `{{ net.LookupIP "localhost" }}`, "127.0.0.1")
}
-func TestNet_CidrHost(t *testing.T) {
- inOutTest(t, `{{ net.ParseIPPrefix "10.12.127.0/20" | net.CidrHost 16 }}`, "10.12.112.16")
- inOutTest(t, `{{ "10.12.127.0/20" | net.CidrHost 16 }}`, "10.12.112.16")
- inOutTest(t, `{{ net.CidrHost 268 "10.12.127.0/20" }}`, "10.12.113.12")
- inOutTest(t, `{{ net.CidrHost 34 "fd00:fd12:3456:7890:00a2::/72" }}`, "fd00:fd12:3456:7890::22")
+func TestNet_CIDRHost(t *testing.T) {
+ inOutTestExperimental(t, `{{ net.ParseIPPrefix "10.12.127.0/20" | net.CIDRHost 16 }}`, "10.12.112.16")
+ inOutTestExperimental(t, `{{ "10.12.127.0/20" | net.CIDRHost 16 }}`, "10.12.112.16")
+ inOutTestExperimental(t, `{{ net.CIDRHost 268 "10.12.127.0/20" }}`, "10.12.113.12")
+ inOutTestExperimental(t, `{{ net.CIDRHost 34 "fd00:fd12:3456:7890:00a2::/72" }}`, "fd00:fd12:3456:7890::22")
}
-func TestNet_CidrNetmask(t *testing.T) {
- inOutTest(t, `{{ "10.12.127.0/20" | net.CidrNetmask }}`, "255.255.240.0")
- inOutTest(t, `{{ net.CidrNetmask "10.0.0.0/12" }}`, "255.240.0.0")
- inOutTest(t, `{{ net.CidrNetmask "fd00:fd12:3456:7890:00a2::/72" }}`, "ffff:ffff:ffff:ffff:ff00::")
+func TestNet_CIDRNetmask(t *testing.T) {
+ inOutTestExperimental(t, `{{ "10.12.127.0/20" | net.CIDRNetmask }}`, "255.255.240.0")
+ inOutTestExperimental(t, `{{ net.CIDRNetmask "10.0.0.0/12" }}`, "255.240.0.0")
+ inOutTestExperimental(t, `{{ net.CIDRNetmask "fd00:fd12:3456:7890:00a2::/72" }}`, "ffff:ffff:ffff:ffff:ff00::")
}
-func TestNet_CidrSubnets(t *testing.T) {
- inOutTest(t, `{{ index ("10.0.0.0/16" | net.CidrSubnets 2) 1 }}`, "10.0.64.0/18")
- inOutTest(t, `{{ range net.CidrSubnets 2 "10.0.0.0/16" }}
+func TestNet_CIDRSubnets(t *testing.T) {
+ inOutTestExperimental(t, `{{ index ("10.0.0.0/16" | net.CIDRSubnets 2) 1 }}`, "10.0.64.0/18")
+ inOutTestExperimental(t, `{{ range net.CIDRSubnets 2 "10.0.0.0/16" }}
{{ . }}{{ end }}`, `
10.0.0.0/18
10.0.64.0/18
@@ -31,10 +31,10 @@ func TestNet_CidrSubnets(t *testing.T) {
10.0.192.0/18`)
}
-func TestNet_CidrSubnetSizes(t *testing.T) {
- inOutTest(t, `{{ index ("10.0.0.0/16" | net.CidrSubnetSizes 1) 0 }}`, "10.0.0.0/17")
- inOutTest(t, `{{ index ("10.1.0.0/16" | net.CidrSubnetSizes 4 4 8 4) 1 }}`, "10.1.16.0/20")
- inOutTest(t, `{{ range net.CidrSubnetSizes 16 16 16 32 "fd00:fd12:3456:7890::/56" }}
+func TestNet_CIDRSubnetSizes(t *testing.T) {
+ inOutTestExperimental(t, `{{ index ("10.0.0.0/16" | net.CIDRSubnetSizes 1) 0 }}`, "10.0.0.0/17")
+ inOutTestExperimental(t, `{{ index ("10.1.0.0/16" | net.CIDRSubnetSizes 4 4 8 4) 1 }}`, "10.1.16.0/20")
+ inOutTestExperimental(t, `{{ range net.CIDRSubnetSizes 16 16 16 32 "fd00:fd12:3456:7890::/56" }}
{{ . }}{{ end }}`, `
fd00:fd12:3456:7800::/72
fd00:fd12:3456:7800:100::/72