diff options
Diffstat (limited to 'vendor/github.com/hashicorp/go-sockaddr/template')
5 files changed, 0 insertions, 752 deletions
diff --git a/vendor/github.com/hashicorp/go-sockaddr/template/GNUmakefile b/vendor/github.com/hashicorp/go-sockaddr/template/GNUmakefile deleted file mode 100644 index ce1e274e4..000000000 --- a/vendor/github.com/hashicorp/go-sockaddr/template/GNUmakefile +++ /dev/null @@ -1,2 +0,0 @@ -test:: - go test diff --git a/vendor/github.com/hashicorp/go-sockaddr/template/README.md b/vendor/github.com/hashicorp/go-sockaddr/template/README.md deleted file mode 100644 index c40905af7..000000000 --- a/vendor/github.com/hashicorp/go-sockaddr/template/README.md +++ /dev/null @@ -1,6 +0,0 @@ -# sockaddr/template - -sockaddr's template library. See -the -[sockaddr/template](https://godoc.org/github.com/hashicorp/go-sockaddr/template) -docs for details on how to use this template. diff --git a/vendor/github.com/hashicorp/go-sockaddr/template/doc.go b/vendor/github.com/hashicorp/go-sockaddr/template/doc.go deleted file mode 100644 index 8cc6730a4..000000000 --- a/vendor/github.com/hashicorp/go-sockaddr/template/doc.go +++ /dev/null @@ -1,311 +0,0 @@ -/* - -Package sockaddr/template provides a text/template interface the SockAddr helper -functions. The primary entry point into the sockaddr/template package is -through its Parse() call. For example: - - import ( - "fmt" - - template "github.com/hashicorp/go-sockaddr/template" - ) - - results, err := template.Parse(`{{ GetPrivateIP }}`) - if err != nil { - fmt.Errorf("Unable to find a private IP address: %v", err) - } - fmt.Printf("My Private IP address is: %s\n", results) - -Below is a list of builtin template functions and details re: their usage. It -is possible to add additional functions by calling ParseIfAddrsTemplate -directly. - -In general, the calling convention for this template library is to seed a list -of initial interfaces via one of the Get*Interfaces() calls, then filter, sort, -and extract the necessary attributes for use as string input. This template -interface is primarily geared toward resolving specific values that are only -available at runtime, but can be defined as a heuristic for execution when a -config file is parsed. - -All functions, unless noted otherwise, return an array of IfAddr structs making -it possible to `sort`, `filter`, `limit`, seek (via the `offset` function), or -`unique` the list. To extract useful string information, the `attr` and `join` -functions return a single string value. See below for details. - -Important note: see the -https://github.com/hashicorp/go-sockaddr/tree/master/cmd/sockaddr utility for -more examples and for a CLI utility to experiment with the template syntax. - -`GetAllInterfaces` - Returns an exhaustive set of IfAddr structs available on -the host. `GetAllInterfaces` is the initial input and accessible as the initial -"dot" in the pipeline. - -Example: - - {{ GetAllInterfaces }} - - -`GetDefaultInterfaces` - Returns one IfAddr for every IP that is on the -interface containing the default route for the host. - -Example: - - {{ GetDefaultInterfaces }} - -`GetPrivateInterfaces` - Returns one IfAddr for every forwardable IP address -that is included in RFC 6890 and whose interface is marked as up. NOTE: RFC 6890 is a more exhaustive -version of RFC1918 because it spans IPv4 and IPv6, however, RFC6890 does permit the -inclusion of likely undesired addresses such as multicast, therefore our version -of "private" also filters out non-forwardable addresses. - -Example: - - {{ GetPrivateInterfaces | sort "default" | join "address" " " }} - - -`GetPublicInterfaces` - Returns a list of IfAddr structs whos IPs are -forwardable, do not match RFC 6890, and whose interface is marked up. - -Example: - - {{ GetPublicInterfaces | sort "default" | join "name" " " }} - - -`GetPrivateIP` - Helper function that returns a string of the first IP address -from GetPrivateInterfaces. - -Example: - - {{ GetPrivateIP }} - - -`GetPrivateIPs` - Helper function that returns a string of the all private IP -addresses on the host. - -Example: - - {{ GetPrivateIPs }} - - -`GetPublicIP` - Helper function that returns a string of the first IP from -GetPublicInterfaces. - -Example: - - {{ GetPublicIP }} - -`GetPublicIPs` - Helper function that returns a space-delimited string of the -all public IP addresses on the host. - -Example: - - {{ GetPrivateIPs }} - - -`GetInterfaceIP` - Helper function that returns a string of the first IP from -the named interface. - -Example: - - {{ GetInterfaceIP "en0" }} - - - -`GetInterfaceIPs` - Helper function that returns a space-delimited list of all -IPs on a given interface. - -Example: - - {{ GetInterfaceIPs "en0" }} - - -`sort` - Sorts the IfAddrs result based on its arguments. `sort` takes one -argument, a list of ways to sort its IfAddrs argument. The list of sort -criteria is comma separated (`,`): - - `address`, `+address`: Ascending sort of IfAddrs by Address - - `-address`: Descending sort of IfAddrs by Address - - `default`, `+default`: Ascending sort of IfAddrs, IfAddr with a default route first - - `-default`: Descending sort of IfAddrs, IfAttr with default route last - - `name`, `+name`: Ascending sort of IfAddrs by lexical ordering of interface name - - `-name`: Descending sort of IfAddrs by lexical ordering of interface name - - `port`, `+port`: Ascending sort of IfAddrs by port number - - `-port`: Descending sort of IfAddrs by port number - - `private`, `+private`: Ascending sort of IfAddrs with private addresses first - - `-private`: Descending sort IfAddrs with private addresses last - - `size`, `+size`: Ascending sort of IfAddrs by their network size as determined - by their netmask (larger networks first) - - `-size`: Descending sort of IfAddrs by their network size as determined by their - netmask (smaller networks first) - - `type`, `+type`: Ascending sort of IfAddrs by the type of the IfAddr (Unix, - IPv4, then IPv6) - - `-type`: Descending sort of IfAddrs by the type of the IfAddr (IPv6, IPv4, Unix) - -Example: - - {{ GetPrivateInterfaces | sort "default,-type,size,+address" }} - - -`exclude` and `include`: Filters IfAddrs based on the selector criteria and its -arguments. Both `exclude` and `include` take two arguments. The list of -available filtering criteria is: - - "address": Filter IfAddrs based on a regexp matching the string representation - of the address - - "flag","flags": Filter IfAddrs based on the list of flags specified. Multiple - flags can be passed together using the pipe character (`|`) to create an inclusive - bitmask of flags. The list of flags is included below. - - "name": Filter IfAddrs based on a regexp matching the interface name. - - "network": Filter IfAddrs based on whether a netowkr is included in a given - CIDR. More than one CIDR can be passed in if each network is separated by - the pipe character (`|`). - - "port": Filter IfAddrs based on an exact match of the port number (number must - be expressed as a string) - - "rfc", "rfcs": Filter IfAddrs based on the matching RFC. If more than one RFC - is specified, the list of RFCs can be joined together using the pipe character (`|`). - - "size": Filter IfAddrs based on the exact match of the mask size. - - "type": Filter IfAddrs based on their SockAddr type. Multiple types can be - specified together by using the pipe character (`|`). Valid types include: - `ip`, `ipv4`, `ipv6`, and `unix`. - -Example: - - {{ GetPrivateInterfaces | exclude "type" "IPv6" }} - - -`unique`: Removes duplicate entries from the IfAddrs list, assuming the list has -already been sorted. `unique` only takes one argument: - - "address": Removes duplicates with the same address - - "name": Removes duplicates with the same interface names - -Example: - - {{ GetAllInterfaces | sort "default,-type,address" | unique "name" }} - - -`limit`: Reduces the size of the list to the specified value. - -Example: - - {{ GetPrivateInterfaces | limit 1 }} - - -`offset`: Seeks into the list by the specified value. A negative value can be -used to seek from the end of the list. - -Example: - - {{ GetPrivateInterfaces | offset "-2" | limit 1 }} - - -`math`: Perform a "math" operation on each member of the list and return new -values. `math` takes two arguments, the attribute to operate on and the -operation's value. - -Supported operations include: - - - `address`: Adds the value, a positive or negative value expressed as a - decimal string, to the address. The sign is required. This value is - allowed to over or underflow networks (e.g. 127.255.255.255 `"address" "+1"` - will return "128.0.0.0"). Addresses will wrap at IPv4 or IPv6 boundaries. - - `network`: Add the value, a positive or negative value expressed as a - decimal string, to the network address. The sign is required. Positive - values are added to the network address. Negative values are subtracted - from the network's broadcast address (e.g. 127.0.0.1 `"network" "-1"` will - return "127.255.255.255"). Values that overflow the network size will - safely wrap. - - `mask`: Applies the given network mask to the address. The network mask is - expressed as a decimal value (e.g. network mask "24" corresponds to - `255.255.255.0`). After applying the network mask, the network mask of the - resulting address will be either the applied network mask or the network mask - of the input address depending on which network is larger - (e.g. 192.168.10.20/24 `"mask" "16"` will return "192.168.0.0/16" but - 192.168.10.20/24 `"mask" "28"` will return "192.168.10.16/24"). - -Example: - - {{ GetPrivateInterfaces | include "type" "IP" | math "address" "+256" | attr "address" }} - {{ GetPrivateInterfaces | include "type" "IP" | math "address" "-256" | attr "address" }} - {{ GetPrivateInterfaces | include "type" "IP" | math "network" "+2" | attr "address" }} - {{ GetPrivateInterfaces | include "type" "IP" | math "network" "-2" | attr "address" }} - {{ GetPrivateInterfaces | include "type" "IP" | math "mask" "24" | attr "address" }} - {{ GetPrivateInterfaces | include "flags" "forwardable|up" | include "type" "IPv4" | math "network" "+2" | attr "address" }} - - -`attr`: Extracts a single attribute of the first member of the list and returns -it as a string. `attr` takes a single attribute name. The list of available -attributes is type-specific and shared between `join`. See below for a list of -supported attributes. - -Example: - - {{ GetAllInterfaces | exclude "flags" "up" | attr "address" }} - - -`Attr`: Extracts a single attribute from an `IfAttr` and in every other way -performs the same as the `attr`. - -Example: - - {{ with $ifAddrs := GetAllInterfaces | include "type" "IP" | sort "+type,+address" -}} - {{- range $ifAddrs -}} - {{- Attr "address" . }} -- {{ Attr "network" . }}/{{ Attr "size" . -}} - {{- end -}} - {{- end }} - - -`join`: Similar to `attr`, `join` extracts all matching attributes of the list -and returns them as a string joined by the separator, the second argument to -`join`. The list of available attributes is type-specific and shared between -`join`. - -Example: - - {{ GetAllInterfaces | include "flags" "forwardable" | join "address" " " }} - - -`exclude` and `include` flags: - - `broadcast` - - `down`: Is the interface down? - - `forwardable`: Is the IP forwardable? - - `global unicast` - - `interface-local multicast` - - `link-local multicast` - - `link-local unicast` - - `loopback` - - `multicast` - - `point-to-point` - - `unspecified`: Is the IfAddr the IPv6 unspecified address? - - `up`: Is the interface up? - - -Attributes for `attr`, `Attr`, and `join`: - -SockAddr Type: - - `string` - - `type` - -IPAddr Type: - - `address` - - `binary` - - `first_usable` - - `hex` - - `host` - - `last_usable` - - `mask_bits` - - `netmask` - - `network` - - `octets`: Decimal values per byte - - `port` - - `size`: Number of hosts in the network - -IPv4Addr Type: - - `broadcast` - - `uint32`: unsigned integer representation of the value - -IPv6Addr Type: - - `uint128`: unsigned integer representation of the value - -UnixSock Type: - - `path` - -*/ -package template diff --git a/vendor/github.com/hashicorp/go-sockaddr/template/template.go b/vendor/github.com/hashicorp/go-sockaddr/template/template.go deleted file mode 100644 index bbed51361..000000000 --- a/vendor/github.com/hashicorp/go-sockaddr/template/template.go +++ /dev/null @@ -1,155 +0,0 @@ -package template - -import ( - "bytes" - "fmt" - "text/template" - - "github.com/hashicorp/errwrap" - sockaddr "github.com/hashicorp/go-sockaddr" -) - -var ( - // SourceFuncs is a map of all top-level functions that generate - // sockaddr data types. - SourceFuncs template.FuncMap - - // SortFuncs is a map of all functions used in sorting - SortFuncs template.FuncMap - - // FilterFuncs is a map of all functions used in sorting - FilterFuncs template.FuncMap - - // HelperFuncs is a map of all functions used in sorting - HelperFuncs template.FuncMap -) - -func init() { - SourceFuncs = template.FuncMap{ - // GetAllInterfaces - Returns an exhaustive set of IfAddr - // structs available on the host. `GetAllInterfaces` is the - // initial input and accessible as the initial "dot" in the - // pipeline. - "GetAllInterfaces": sockaddr.GetAllInterfaces, - - // GetDefaultInterfaces - Returns one IfAddr for every IP that - // is on the interface containing the default route for the - // host. - "GetDefaultInterfaces": sockaddr.GetDefaultInterfaces, - - // GetPrivateInterfaces - Returns one IfAddr for every IP that - // matches RFC 6890, are attached to the interface with the - // default route, and are forwardable IP addresses. NOTE: RFC - // 6890 is a more exhaustive version of RFC1918 because it spans - // IPv4 and IPv6, however it doespermit the inclusion of likely - // undesired addresses such as multicast, therefore our - // definition of a "private" address also excludes - // non-forwardable IP addresses (as defined by the IETF). - "GetPrivateInterfaces": sockaddr.GetPrivateInterfaces, - - // GetPublicInterfaces - Returns a list of IfAddr that do not - // match RFC 6890, are attached to the default route, and are - // forwardable. - "GetPublicInterfaces": sockaddr.GetPublicInterfaces, - } - - SortFuncs = template.FuncMap{ - "sort": sockaddr.SortIfBy, - } - - FilterFuncs = template.FuncMap{ - "exclude": sockaddr.ExcludeIfs, - "include": sockaddr.IncludeIfs, - } - - HelperFuncs = template.FuncMap{ - // Misc functions that operate on IfAddrs inputs - "attr": Attr, - "join": sockaddr.JoinIfAddrs, - "limit": sockaddr.LimitIfAddrs, - "offset": sockaddr.OffsetIfAddrs, - "unique": sockaddr.UniqueIfAddrsBy, - - // Misc math functions that operate on a single IfAddr input - "math": sockaddr.IfAddrsMath, - - // Return a Private RFC 6890 IP address string that is attached - // to the default route and a forwardable address. - "GetPrivateIP": sockaddr.GetPrivateIP, - - // Return all Private RFC 6890 IP addresses as a space-delimited string of - // IP addresses. Addresses returned do not have to be on the interface with - // a default route. - "GetPrivateIPs": sockaddr.GetPrivateIPs, - - // Return a Public RFC 6890 IP address string that is attached - // to the default route and a forwardable address. - "GetPublicIP": sockaddr.GetPublicIP, - - // Return allPublic RFC 6890 IP addresses as a space-delimited string of IP - // addresses. Addresses returned do not have to be on the interface with a - // default route. - "GetPublicIPs": sockaddr.GetPublicIPs, - - // Return the first IP address of the named interface, sorted by - // the largest network size. - "GetInterfaceIP": sockaddr.GetInterfaceIP, - - // Return all IP addresses on the named interface, sorted by the largest - // network size. - "GetInterfaceIPs": sockaddr.GetInterfaceIPs, - } -} - -// Attr returns the attribute from the ifAddrRaw argument. If the argument is -// an IfAddrs, only the first element will be evaluated for resolution. -func Attr(selectorName string, ifAddrsRaw interface{}) (string, error) { - switch v := ifAddrsRaw.(type) { - case sockaddr.IfAddr: - return sockaddr.IfAttr(selectorName, v) - case sockaddr.IfAddrs: - return sockaddr.IfAttrs(selectorName, v) - default: - return "", fmt.Errorf("unable to obtain attribute %s from type %T (%v)", selectorName, ifAddrsRaw, ifAddrsRaw) - } -} - -// Parse parses input as template input using the addresses available on the -// host, then returns the string output if there are no errors. -func Parse(input string) (string, error) { - addrs, err := sockaddr.GetAllInterfaces() - if err != nil { - return "", errwrap.Wrapf("unable to query interface addresses: {{err}}", err) - } - - return ParseIfAddrs(input, addrs) -} - -// ParseIfAddrs parses input as template input using the IfAddrs inputs, then -// returns the string output if there are no errors. -func ParseIfAddrs(input string, ifAddrs sockaddr.IfAddrs) (string, error) { - return ParseIfAddrsTemplate(input, ifAddrs, template.New("sockaddr.Parse")) -} - -// ParseIfAddrsTemplate parses input as template input using the IfAddrs inputs, -// then returns the string output if there are no errors. -func ParseIfAddrsTemplate(input string, ifAddrs sockaddr.IfAddrs, tmplIn *template.Template) (string, error) { - // Create a template, add the function map, and parse the text. - tmpl, err := tmplIn.Option("missingkey=error"). - Funcs(SourceFuncs). - Funcs(SortFuncs). - Funcs(FilterFuncs). - Funcs(HelperFuncs). - Parse(input) - if err != nil { - return "", errwrap.Wrapf(fmt.Sprintf("unable to parse template %+q: {{err}}", input), err) - } - - var outWriter bytes.Buffer - err = tmpl.Execute(&outWriter, ifAddrs) - if err != nil { - return "", errwrap.Wrapf(fmt.Sprintf("unable to execute sockaddr input %+q: {{err}}", input), err) - } - - return outWriter.String(), nil -} diff --git a/vendor/github.com/hashicorp/go-sockaddr/template/template_test.go b/vendor/github.com/hashicorp/go-sockaddr/template/template_test.go deleted file mode 100644 index ec9822e77..000000000 --- a/vendor/github.com/hashicorp/go-sockaddr/template/template_test.go +++ /dev/null @@ -1,278 +0,0 @@ -package template_test - -import ( - "testing" - - socktmpl "github.com/hashicorp/go-sockaddr/template" -) - -func TestSockAddr_Parse(t *testing.T) { - tests := []struct { - name string - input string - output string - fail bool - requireOnline bool - }{ - { - name: `basic include "name"`, - input: `{{GetAllInterfaces | include "name" "lo0" | printf "%v"}}`, - output: `[127.0.0.1/8 {1 16384 lo0 up|loopback|multicast} ::1 {1 16384 lo0 up|loopback|multicast} fe80::1/64 {1 16384 lo0 up|loopback|multicast}]`, - }, - { - name: "invalid input", - input: `{{`, - output: ``, - fail: true, - }, - { - name: "GetDefaultInterface", - input: `{{GetDefaultInterfaces | include "type" "IPv4" | attr "name" }}`, - output: `en0`, - }, - { - name: `include "name" regexp`, - input: `{{GetAllInterfaces | include "name" "^(en|lo)0$" | exclude "name" "^en0$" | sort "type" | sort "address" | join "address" " " }}`, - output: `127.0.0.1 ::1 fe80::1`, - }, - { - name: `exclude "name"`, - input: `{{. | include "name" "^(en|lo)0$" | exclude "name" "^en0$" | sort "type" | sort "address" | join "address" " " }}`, - output: `127.0.0.1 ::1 fe80::1`, - }, - { - name: `"dot" pipeline, IPv4 type`, - input: `{{. | include "type" "IPv4" | include "name" "^lo0$" | sort "type" | sort "address" }}`, - output: `[127.0.0.1/8 {1 16384 lo0 up|loopback|multicast}]`, - }, - { - name: `include "type" "IPv6`, - input: `{{. | include "type" "IPv6" | include "name" "^lo0$" | sort "address" }}`, - output: `[::1 {1 16384 lo0 up|loopback|multicast} fe80::1/64 {1 16384 lo0 up|loopback|multicast}]`, - }, - { - name: "better example for IP types", - input: `{{. | include "type" "IPv4|IPv6" | include "name" "^lo0$" | sort "type" | sort "address" }}`, - output: `[127.0.0.1/8 {1 16384 lo0 up|loopback|multicast} ::1 {1 16384 lo0 up|loopback|multicast} fe80::1/64 {1 16384 lo0 up|loopback|multicast}]`, - }, - { - name: "ifAddrs1", - input: `{{. | include "type" "IPv4" | include "name" "^lo0$"}}`, - output: `[127.0.0.1/8 {1 16384 lo0 up|loopback|multicast}]`, - }, - { - name: "ifAddrs2", - input: `{{. | include "type" "IP" | include "name" "^lo0$" | sort "type" | sort "address" }}`, - output: `[127.0.0.1/8 {1 16384 lo0 up|loopback|multicast} ::1 {1 16384 lo0 up|loopback|multicast} fe80::1/64 {1 16384 lo0 up|loopback|multicast}]`, - }, - { - name: `range "dot" example`, - input: `{{range . | include "type" "IP" | include "name" "^lo0$"}}{{.Name}} {{.SockAddr}} {{end}}`, - output: `lo0 127.0.0.1/8 lo0 ::1 lo0 fe80::1/64 `, - }, - { - name: `exclude "type"`, - input: `{{. | exclude "type" "IPv4" | include "name" "^lo0$" | sort "address" | unique "name" | join "name" " "}} {{range . | exclude "type" "IPv4" | include "name" "^lo0$"}}{{.SockAddr}} {{end}}`, - output: `lo0 ::1 fe80::1/64 `, - }, - { - name: "with variable pipeline", - input: `{{with $ifSet := include "type" "IPv4" . | include "name" "^lo0$"}}{{range $ifSet }}{{.Name}} {{end}}{{range $ifSet}}{{.SockAddr}} {{end}}{{end}}`, - output: `lo0 127.0.0.1/8 `, - }, - { - name: "range sample on lo0", - input: `{{with $ifAddrs := . | exclude "rfc" "1918" | include "name" "lo0" | sort "type,address" }}{{range $ifAddrs }}{{.Name}}/{{.SockAddr.NetIP}} {{end}}{{end}}`, - output: `lo0/127.0.0.1 lo0/::1 lo0/fe80::1 `, - }, - { - name: "non-RFC1918 on on lo0", - input: `{{. | exclude "rfc" "1918" | include "name" "lo0" | sort "type,address" | len | eq 3}}`, - output: `true`, - }, - { - // NOTE(sean@): Difficult to reliably test includeByRFC. - // In this case, we ass-u-me that the host running the - // test has at least one RFC1918 address on their host. - name: `include "rfc"`, - input: `{{(. | include "rfc" "1918" | attr "name")}}`, - output: `en0`, - requireOnline: true, - }, - { - name: "test for non-empty array", - input: `{{. | include "type" "IPv4" | include "rfc" "1918" | print | len | eq (len "[]")}}`, - output: `false`, - }, - { - // NOTE(sean@): This will fail if there is a public IPv4 address on loopback. - name: "non-IPv4 RFC1918", - input: `{{. | include "name" "lo0" | exclude "type" "IPv4" | include "rfc" "1918" | len | eq 0}}`, - output: `true`, - }, - { - // NOTE(sean@): There are no RFC6598 addresses on most testing hosts so this should be empty. - name: "rfc6598", - input: `{{. | include "type" "IPv4" | include "rfc" "6598" | print | len | eq (len "[]")}}`, - output: `true`, - }, - { - name: "invalid RFC", - input: `{{. | include "type" "IPv4" | include "rfc" "99999999999" | print | len | eq (len "[]")}}`, - output: `true`, - fail: true, - }, - { - name: `sort asc address`, - input: `{{ . | include "name" "lo0" | sort "type,address" | join "address" " " }}`, - output: `127.0.0.1 ::1 fe80::1`, - }, - { - name: `sort asc address old`, - input: `{{with $ifSet := include "name" "lo0" . }}{{ range include "type" "IPv4" $ifSet | sort "address"}}{{ .SockAddr }} {{end}}{{ range include "type" "IPv6" $ifSet | sort "address"}}{{ .SockAddr }} {{end}}{{end}}`, - output: `127.0.0.1/8 ::1 fe80::1/64 `, - }, - { - name: `sort desc address`, - input: `{{ . | include "name" "lo0" | sort "type,-address" | join "address" " " }}`, - output: `127.0.0.1 fe80::1 ::1`, - }, - { - name: `sort desc address`, - input: `{{ . | include "name" "lo0" | include "type" "IPv6" | sort "type,-address" | join "address" " " }}`, - output: `fe80::1 ::1`, - }, - { - name: `sort asc address`, - input: `{{with $ifSet := include "name" "lo0" . }}{{ range include "type" "IPv6" $ifSet | sort "address"}}{{ .SockAddr }} {{end}}{{end}}`, - output: `::1 fe80::1/64 `, - }, - { - name: "lo0 limit 1", - input: `{{. | include "name" "lo0" | include "type" "IPv6" | sort "address" | limit 1 | len}}`, - output: `1`, - }, - { - name: "join address", - input: `{{. | include "name" "lo0" | include "type" "IPv6" | sort "address" | join "address" " " }}`, - output: `::1 fe80::1`, - }, - { - name: "join name", - input: `{{. | include "name" "lo0" | include "type" "IPv6" | sort "address" | join "name" " " }}`, - output: `lo0 lo0`, - }, - { - name: "lo0 flags up and limit 1", - input: `{{. | include "name" "lo0" | include "flag" "up" | sort "-type,+address" | attr "address" }}`, - output: `::1`, - }, - { - // NOTE(sean@): This is the HashiCorp default in 2016. - // Indented for effect. Using "true" as the output - // instead of printing the correct $rfc*Addrs values. - name: "HashiCorpDefault2016", - input: ` -{{- with $addr := GetAllInterfaces | include "type" "IP" | include "rfc" "1918|6598" | sort "address" | attr "address" -}} - - {{- if ($addr | len) gt 0 -}} - {{- print "true" -}}{{/* print $addr*/ -}} - {{- end -}} -{{- end -}}`, - output: `true`, - }, - { - name: "math address +", - input: `{{GetAllInterfaces | include "name" "^lo0$" | include "type" "IP" | math "address" "+2" | sort "+type,+address" | join "address" " " }}`, - output: `127.0.0.3 ::3 fe80::3`, - }, - { - name: "math address + overflow", - input: `|{{- with $ifAddrs := GetAllInterfaces | include "name" "^lo0$" | include "type" "IP" | math "address" "+16777217" | sort "+type,+address" -}} - {{- range $ifAddrs -}} - {{- attr "address" . }} -- {{ attr "network" . }}/{{ attr "size" . }}|{{ end -}} -{{- end -}}`, - output: `|128.0.0.2 -- 128.0.0.0/16777216|::100:2 -- ::100:2/1|fe80::100:2 -- fe80::/18446744073709551616|`, - }, - { - name: "math address + overflow+wrap", - input: `{{GetAllInterfaces | include "name" "^lo0$" | include "type" "IP" | math "address" "+4294967294" | sort "+type,+address" | join "address" " " }}`, - output: `126.255.255.255 ::ffff:ffff fe80::ffff:ffff`, - }, - { - name: "math address -", - input: `{{GetAllInterfaces | include "name" "^lo0$" | include "type" "IP" | math "address" "-256" | sort "+type,+address" | join "address" " " }}`, - output: `126.255.255.1 fe7f:ffff:ffff:ffff:ffff:ffff:ffff:ff01 ffff:ffff:ffff:ffff:ffff:ffff:ffff:ff01`, - }, - { - name: "math address - underflow", - input: `{{GetAllInterfaces | include "name" "^lo0$" | include "type" "IP" | math "address" "-4278190082" | sort "+type,+address" | join "address" " " }}`, - output: `127.255.255.255 fe7f:ffff:ffff:ffff:ffff:ffff:ff:ffff ffff:ffff:ffff:ffff:ffff:ffff:ff:ffff`, - }, - { - // Note to readers: lo0's link-local address (::1) address has a mask of - // /128 which means its value never changes and this is expected. lo0's - // site-local address has a /64 address and is expected to change. - name: "math network", - input: `{{GetAllInterfaces | include "name" "^lo0$" | include "type" "IP" | math "network" "+2" | sort "+type,+address" | join "address" " " }}`, - output: `127.0.0.2 ::1 fe80::2`, - }, - { - // Assume an IPv4 input of 127.0.0.1. With a value of 0xff00ff01, we wrap once on purpose. - name: "math network + wrap", - input: `{{GetAllInterfaces | include "name" "^lo0$" | include "type" "IP" | math "network" "+4278255368" | sort "+type,+address" | join "address" " " }}`, - output: `127.0.255.8 ::1 fe80::ff00:ff08`, - }, - { - name: "math network -", - input: `{{GetAllInterfaces | include "name" "^lo0$" | include "type" "IP" | math "network" "-2" | sort "+type,+address" | join "address" " " }}`, - output: `127.255.255.254 ::1 fe80::ffff:ffff:ffff:fffe`, - }, - { - // Assume an IPv4 input of 127.0.0.1. With a value of 0xff000008 it - // should wrap and underflow by 8. Assume an IPv6 input of ::1. With a - // value of -0xff000008 the value underflows and wraps. - name: "math network - underflow+wrap", - input: `{{GetAllInterfaces | include "name" "^lo0$" | include "type" "IP" | sort "+type,+address" | math "network" "-4278190088" | join "address" " " }}`, - output: `127.255.255.248 ::1 fe80::ffff:ffff:ff:fff8`, - }, - { - // Assume the private IPs available on the host are: 10.1.2.3 - // fe80::1025:f732:1001:203 - name: "GetPrivateIPs", - input: `{{GetPrivateIPs}}`, - output: `10.1.2.3 fe80::1025:f732:1001:203`, - }, - { - // Assume the public IPs available on the host are: 1.2.3.4 6.7.8.9 - name: "GetPublicIPs", - input: `{{GetPublicIPs}}`, - output: `1.2.3.4 6.7.8.9`, - }, - { - // Assume the private IPs on this host are just the IPv4 addresses: - // 10.1.2.3 and 172.16.4.6 - name: "GetInterfaceIPs", - input: `{{GetInterfaceIPs "en0"}}`, - output: `10.1.2.3 and 172.16.4.6`, - }, - } - - for i, test := range tests { - test := test // capture range variable - if test.name == "" { - t.Fatalf("test number %d has an empty test name", i) - } - t.Run(test.name, func(t *testing.T) { - t.Parallel() - out, err := socktmpl.Parse(test.input) - if err != nil && !test.fail { - t.Fatalf("%q: bad: %v", test.name, err) - } - - if out != test.output && !test.fail { - t.Fatalf("%q: Expected %+q, received %+q\n%+q", test.name, test.output, out, test.input) - } - }) - } -} |