Skip to content
Permalink
Browse files

Allow setting of Source IP for Addr Test (#344)

* Allow setting of Source IP for Addr Test

This Change allows the Source IP with which the Addr test is run to be
set.

* Gofmt the code and add tests

* More gofmt fixes

* Rename localaddress -> local-address
  • Loading branch information
CBeerta authored and aelsabbahy committed Dec 18, 2019
1 parent 5476d20 commit e5d28f3ae133657f709278190747de653638d681
@@ -425,6 +425,8 @@ addr:
tcp://ip-address-or-domain-name:80:
reachable: true
timeout: 500
# optional attributes
local-address: 127.0.0.1
```
@@ -23,5 +23,10 @@ port:
listening: true
ip:
- "::"
addr:
tcp://127.0.0.1:80:
reachable: true
timeout: 500
local-address: 127.0.0.1
gossfile:
"../goss-s*.yaml": {}
@@ -23,5 +23,10 @@ port:
listening: true
ip:
- '0.0.0.0'
addr:
tcp://127.0.0.1:80:
reachable: true
timeout: 500
local-address: 127.0.0.1
gossfile:
"../goss-s*.yaml": {}
@@ -68,6 +68,10 @@ addr:
tcp://google.com:443:
reachable: true
timeout: 5000
tcp://google.com:80:
reachable: false
timeout: 5000
local-address: 127.0.0.1
port:
tcp:9999:
listening: false
@@ -23,5 +23,10 @@ port:
listening: true
ip:
- 0.0.0.0
addr:
tcp://127.0.0.1:80:
reachable: true
timeout: 500
local-address: 127.0.0.1
gossfile:
"../goss-s*.yaml": {}
@@ -23,5 +23,10 @@ port:
listening: true
ip:
- '0.0.0.0'
addr:
tcp://127.0.0.1:80:
reachable: true
timeout: 500
local-address: 127.0.0.1
gossfile:
"../goss-s*.yaml": {}
@@ -44,9 +44,9 @@ out=$(docker_exec "/goss/$os/goss-linux-$arch" --vars "/goss/vars.yaml" -g "/gos
echo "$out"

if [[ $os == "arch" ]]; then
egrep -q 'Count: 83, Failed: 0, Skipped: 3' <<<"$out"
egrep -q 'Count: 84, Failed: 0, Skipped: 3' <<<"$out"
else
egrep -q 'Count: 99, Failed: 0, Skipped: 5' <<<"$out"
egrep -q 'Count: 101, Failed: 0, Skipped: 5' <<<"$out"
fi

if [[ ! $os == "arch" ]]; then
@@ -6,11 +6,12 @@ import (
)

type Addr struct {
Title string `json:"title,omitempty" yaml:"title,omitempty"`
Meta meta `json:"meta,omitempty" yaml:"meta,omitempty"`
Address string `json:"-" yaml:"-"`
Reachable matcher `json:"reachable" yaml:"reachable"`
Timeout int `json:"timeout" yaml:"timeout"`
Title string `json:"title,omitempty" yaml:"title,omitempty"`
Meta meta `json:"meta,omitempty" yaml:"meta,omitempty"`
Address string `json:"-" yaml:"-"`
LocalAddress string `json:"local-address,omitempty" yaml:"local-address,omitempty"`
Reachable matcher `json:"reachable" yaml:"reachable"`
Timeout int `json:"timeout" yaml:"timeout"`
}

func (a *Addr) ID() string { return a.Address }
@@ -25,7 +26,8 @@ func (a *Addr) Validate(sys *system.System) []TestResult {
if a.Timeout == 0 {
a.Timeout = 500
}
sysAddr := sys.NewAddr(a.Address, sys, util.Config{Timeout: a.Timeout})

sysAddr := sys.NewAddr(a.Address, sys, util.Config{Timeout: a.Timeout, LocalAddress: a.LocalAddress})

var results []TestResult
results = append(results, ValidateValue(a, "reachable", a.Reachable, sysAddr.Reachable, skip))
@@ -36,9 +38,10 @@ func NewAddr(sysAddr system.Addr, config util.Config) (*Addr, error) {
address := sysAddr.Address()
reachable, err := sysAddr.Reachable()
a := &Addr{
Address: address,
Reachable: reachable,
Timeout: config.Timeout,
Address: address,
Reachable: reachable,
Timeout: config.Timeout,
LocalAddress: config.LocalAddress,
}
return a, err
}
@@ -15,15 +15,17 @@ type Addr interface {
}

type DefAddr struct {
address string
Timeout int
address string
LocalAddress string
Timeout int
}

func NewDefAddr(address string, system *System, config util.Config) Addr {
addr := normalizeAddress(address)
return &DefAddr{
address: addr,
Timeout: config.Timeout,
address: addr,
LocalAddress: config.LocalAddress,
Timeout: config.Timeout,
}
}

@@ -38,7 +40,12 @@ func (a *DefAddr) Exists() (bool, error) { return a.Reachable() }
func (a *DefAddr) Reachable() (bool, error) {
network, address := splitAddress(a.address)

conn, err := net.DialTimeout(network, address, time.Duration(a.Timeout)*time.Millisecond)
localAddr := &net.TCPAddr{
IP: net.ParseIP(a.LocalAddress),
}

d := net.Dialer{LocalAddr: localAddr, Timeout: time.Duration(a.Timeout) * time.Millisecond}
conn, err := d.Dial(network, address)
if err != nil {
return false, nil
}
@@ -52,7 +59,6 @@ func splitAddress(fulladdress string) (network, address string) {
return split[0], split[1]
}
return "tcp", fulladdress

}

func normalizeAddress(fulladdress string) string {
@@ -17,6 +17,7 @@ type Config struct {
Server string
Username string
Password string
LocalAddress string
}

type OutputConfig struct {

0 comments on commit e5d28f3

Please sign in to comment.
You can’t perform that action at this time.