Skip to content

Commit

Permalink
add support for ipv6 addressing
Browse files Browse the repository at this point in the history
  • Loading branch information
alrighttheresham committed Oct 26, 2018
1 parent 21be115 commit 6a8cb7f
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 24 deletions.
58 changes: 36 additions & 22 deletions action/netconf.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"regexp"
"strconv"
"strings"
"time"

"github.com/damianoneill/nc-hammer/result"
Expand All @@ -17,6 +18,12 @@ var (
diagnosticContext = context.Background()
)

const (
colon = ":"
left = "["
right = "]"
)

// CreateDiagnosticContext creates a context used for instantiating new Netconf sessions, with the option
// of enabling netconf client diagnostics (diagFlag == true).
func CreateDiagnosticContext(diagFlag bool) {
Expand Down Expand Up @@ -53,16 +60,16 @@ func operationOrMessage(netconf *suite.Netconf) string {
// ExecuteNetconf invoked when a NETCONF Action is identified
func ExecuteNetconf(tsStart time.Time, cID int, action suite.Action, config *suite.Sshconfig, resultChannel chan result.NetconfResult) {

var result result.NetconfResult
result.Client = cID
result.Hostname = action.Netconf.Hostname
result.Operation = operationOrMessage(action.Netconf)
var r result.NetconfResult
r.Client = cID
r.Hostname = action.Netconf.Hostname
r.Operation = operationOrMessage(action.Netconf)

session, err := getSession(cID, config.Hostname+":"+strconv.Itoa(config.Port), config.Username, config.Password, config.Reuseconnection)
session, err := getSession(cID, handleIpv6(config.Hostname)+":"+strconv.Itoa(config.Port), config.Username, config.Password, config.Reuseconnection)
if err != nil {
fmt.Printf("E")
result.Err = err.Error()
resultChannel <- result
r.Err = err.Error()
resultChannel <- r
return
}

Expand All @@ -73,53 +80,53 @@ func ExecuteNetconf(tsStart time.Time, cID int, action suite.Action, config *sui
}

if session != nil {
result.SessionID = session.ID()
r.SessionID = session.ID()
} else {
fmt.Printf("E")
result.Err = "session has expired"
resultChannel <- result
r.Err = "session has expired"
resultChannel <- r
return
}

xml, err := action.Netconf.ToXMLString()
if err != nil {
fmt.Printf("E")
result.Err = err.Error()
resultChannel <- result
r.Err = err.Error()
resultChannel <- r
return
}

raw := netconf.Request(xml)
start := time.Now()
rpcReply, err := session.Execute(raw)
if err != nil {
result.Err = err.Error()
r.Err = err.Error()
fmt.Printf("e")
resultChannel <- result
resultChannel <- r
return
}
elapsed := time.Since(start)
result.When = float64(time.Since(tsStart).Nanoseconds() / int64(time.Millisecond))
result.Latency = float64(elapsed.Nanoseconds() / int64(time.Millisecond))
r.When = float64(time.Since(tsStart).Nanoseconds() / int64(time.Millisecond))
r.Latency = float64(elapsed.Nanoseconds() / int64(time.Millisecond))

result.MessageID = rpcReply.MessageID
r.MessageID = rpcReply.MessageID

if action.Netconf.Expected != nil {
match, err := regexp.MatchString(*action.Netconf.Expected, rpcReply.Data)
if err != nil {
fmt.Printf("E")
result.Err = err.Error()
resultChannel <- result
r.Err = err.Error()
resultChannel <- r
return
}
if !match {
fmt.Printf("e")
result.Err = "expected response did not match, expected: " + *action.Netconf.Expected + " actual: " + rpcReply.Data
resultChannel <- result
r.Err = "expected response did not match, expected: " + *action.Netconf.Expected + " actual: " + rpcReply.Data
resultChannel <- r
return
}
}
resultChannel <- result
resultChannel <- r
}

// getSession returns a NETCONF Session, either a new one or a pre existing one if resuseConnection is valid for client/host
Expand Down Expand Up @@ -150,3 +157,10 @@ var createNewSession = func(hostname, username, password string) (netconf.Sessio

return netconf.NewRPCSession(diagnosticContext, sshConfig, hostname)
}

func handleIpv6(host string) string {
if strings.Contains(host, colon) {
return left + host + right
}
return host
}
24 changes: 22 additions & 2 deletions action/netconf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@ import (
"testing"
"time"

"github.com/damianoneill/nc-hammer/mocks/github.com/damianoneill/net/netconf"
"github.com/damianoneill/nc-hammer/result"
"github.com/damianoneill/nc-hammer/suite"
"github.com/damianoneill/net/netconf"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"

"github.com/damianoneill/nc-hammer/mocks/github.com/damianoneill/net/netconf"
)

//helper function to capture ExecuteNetconf output
Expand Down Expand Up @@ -109,3 +108,24 @@ func Test_NetconfDiagnosticContext(t *testing.T) {
assert.Equal(t, netconf.DefaultLoggingHooks, netconf.ContextClientTrace(nonDiagContext), "Expect context not to enable diagnostics")
assert.Equal(t, netconf.DiagnosticLoggingHooks, netconf.ContextClientTrace(diagContext), "Expect context to enable diagnostics")
}

func Test_handleIpv6(t *testing.T) {
type args struct {
host string
}
tests := []struct {
name string
args args
want string
}{
{"valid ip4", args{"127.0.0.1"}, "127.0.0.1"},
{"valid ipv6", args{"::1"}, "[::1]"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := handleIpv6(tt.args.host); got != tt.want {
t.Errorf("handleIpv6() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 6a8cb7f

Please sign in to comment.