Skip to content

Commit

Permalink
Allow setting of Source IP for Addr Test (#344)
Browse files Browse the repository at this point in the history
* 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 e5d28f3
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 17 deletions.
2 changes: 2 additions & 0 deletions docs/manual.md
Expand Up @@ -425,6 +425,8 @@ addr:
tcp://ip-address-or-domain-name:80:
reachable: true
timeout: 500
# optional attributes
local-address: 127.0.0.1
```
Expand Down
5 changes: 5 additions & 0 deletions integration-tests/goss/alpine3/goss.yaml
Expand Up @@ -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": {}
5 changes: 5 additions & 0 deletions integration-tests/goss/centos7/goss.yaml
Expand Up @@ -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": {}
4 changes: 4 additions & 0 deletions integration-tests/goss/goss-shared.yaml
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions integration-tests/goss/precise/goss.yaml
Expand Up @@ -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": {}
5 changes: 5 additions & 0 deletions integration-tests/goss/wheezy/goss.yaml
Expand Up @@ -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": {}
4 changes: 2 additions & 2 deletions integration-tests/test.sh
Expand Up @@ -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
Expand Down
21 changes: 12 additions & 9 deletions resource/addr.go
Expand Up @@ -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 }
Expand All @@ -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))
Expand All @@ -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
}
18 changes: 12 additions & 6 deletions system/addr.go
Expand Up @@ -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,
}
}

Expand All @@ -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
}
Expand All @@ -52,7 +59,6 @@ func splitAddress(fulladdress string) (network, address string) {
return split[0], split[1]
}
return "tcp", fulladdress

}

func normalizeAddress(fulladdress string) string {
Expand Down
1 change: 1 addition & 0 deletions util/config.go
Expand Up @@ -17,6 +17,7 @@ type Config struct {
Server string
Username string
Password string
LocalAddress string
}

type OutputConfig struct {
Expand Down

0 comments on commit e5d28f3

Please sign in to comment.