Skip to content

Commit

Permalink
Merge pull request #2 from dyntek-services-inc/testing-update
Browse files Browse the repository at this point in the history
no JSON and CSV handle testing
  • Loading branch information
KennethGrace authored Jun 20, 2022
2 parents aedca82 + 345d80c commit 295f8f9
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 8 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ entered manually. Interactive mode does **not** support configuration templating

Optional arguments include `-i inventory_filename` and `-t template_filename`. The template
should be defined in a plain-text document and the inventory filename should be defined in a
YAML, CSV, or JSON formatted document. YAML and JSON should be supplied according to the
YAML or CSV formatted document. YAML should be supplied according to the
following schema;

```yaml
Expand Down Expand Up @@ -56,7 +56,6 @@ get us to release v1.0.0.
- ~~Basic user/pass authentication~~.
- Key based authentication.
- ~~Implement YAML Inventories~~.
- Implement JSON Inventories.
- ~~Implement CSV Inventories~~.
- Implement interactive operation.
- Index responses by command.
Expand Down
102 changes: 96 additions & 6 deletions tests/Handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,36 @@ import (
)

func TestConnectHandler(t *testing.T) {
t.Log("Testing connections from YAML inventory")
inv, err := inventory.LoadFromYAML("secrets/hosts.yml")
if err != nil {
panic(err)
}
// BasicConnect to Hosts
var handlers []client.Handler
for _, host := range inv.Hosts {
t.Logf("Connectiong to %s", host.Hostname)
t.Logf("Connecting to %s", host.Hostname)
h, err := client.BasicConnect(host)
handlers = append(handlers, h)
if err != nil {
panic(err)
}
}
// Cleanup
for _, h := range handlers {
if err := h.Close(); err != nil {
log.Fatal("handlers failed to close properly", err)
}
}
t.Log("Testing connections from CSV inventory")
inv, err = inventory.LoadFromCSV("secrets/hosts.csv")
if err != nil {
panic(err)
}
// BasicConnect to Hosts
handlers = []client.Handler{}
for _, host := range inv.Hosts {
t.Logf("Connecting to %s", host.Hostname)
h, err := client.BasicConnect(host)
handlers = append(handlers, h)
if err != nil {
Expand All @@ -34,15 +56,50 @@ func TestConnectHandler(t *testing.T) {
}
}

func TestSendHandler(t *testing.T) {
func TestEchoHandler(t *testing.T) {
t.Log("Testing connections from YAML inventory")
inv, err := inventory.LoadFromYAML("secrets/hosts.yml")
if err != nil {
panic(err)
}
// BasicConnect to Hosts
var handlers []client.Handler
for _, host := range inv.Hosts {
t.Logf("Connectiong to %s", host.Hostname)
t.Logf("Connecting to %s", host.Hostname)
h, err := client.BasicConnect(host)
handlers = append(handlers, h)
if err != nil {
panic(err)
}
}
// Send Command to Host
for _, h := range handlers {
response, err := h.Send("echo \"hello world!\"")
if err != nil {
panic(err)
}
response = strings.TrimSpace(response)
if response != "hello world!" {
panic(errors.New(fmt.Sprintf("response %s not equal to %s", response, "hello world!")))
} else {
t.Logf("response %s succesfully matches %s", response, "hello world!")
}
}
// Cleanup
for _, h := range handlers {
if err := h.Close(); err != nil {
log.Fatal("handlers failed to close properly", err)
}
}
t.Log("Testing connections from CSV inventory")
inv, err = inventory.LoadFromCSV("secrets/hosts.csv")
if err != nil {
panic(err)
}
// BasicConnect to Hosts
handlers = []client.Handler{}
for _, host := range inv.Hosts {
t.Logf("Connecting to %s", host.Hostname)
h, err := client.BasicConnect(host)
handlers = append(handlers, h)
if err != nil {
Expand Down Expand Up @@ -70,7 +127,8 @@ func TestSendHandler(t *testing.T) {
}
}

func TestMultiSendHandler(t *testing.T) {
func TestTemplateHandler(t *testing.T) {
t.Log("Testing connections from YAML inventory")
inv, err := inventory.LoadFromYAML("secrets/hosts.yml")
tplString, err := render.FileToString("secrets/example.txt")
if err != nil {
Expand All @@ -80,7 +138,40 @@ func TestMultiSendHandler(t *testing.T) {
var handlers []client.Handler
var tpls [][]string
for _, host := range inv.Hosts {
t.Logf("Connectiong to %s", host.Hostname)
t.Logf("Connecting to %s", host.Hostname)
h, err := client.BasicConnect(host)
if err != nil {
panic(err)
}
handlers = append(handlers, h)
tpls = append(tpls, render.RenderCommands(host.Data, tplString))
}
// Send Commands to Host
for i, h := range handlers {
for _, command := range tpls[i] {
response, err := h.Send(command)
if err != nil {
panic(err)
}
response = strings.TrimSpace(response)
}
}
// Cleanup
for _, h := range handlers {
if err := h.Close(); err != nil {
log.Fatal("handlers failed to close properly", err)
}
}
t.Log("Testing connections from CSV inventory")
inv, err = inventory.LoadFromCSV("secrets/hosts.csv")
if err != nil {
panic(err)
}
// BasicConnect to Hosts and Render Template
handlers = []client.Handler{}
tpls = [][]string{{}}
for _, host := range inv.Hosts {
t.Logf("Connecting to %s", host.Hostname)
h, err := client.BasicConnect(host)
if err != nil {
panic(err)
Expand All @@ -96,7 +187,6 @@ func TestMultiSendHandler(t *testing.T) {
panic(err)
}
response = strings.TrimSpace(response)
fmt.Println(response)
}
}
// Cleanup
Expand Down

0 comments on commit 295f8f9

Please # to comment.