From 345d80c6a6be7d25a1dec80a2edf6881c1032d36 Mon Sep 17 00:00:00 2001 From: kennethgrace Date: Mon, 20 Jun 2022 14:22:03 -0700 Subject: [PATCH] no JSON and CSV handle testing --- README.md | 3 +- tests/Handler_test.go | 102 +++++++++++++++++++++++++++++++++++++++--- 2 files changed, 97 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 9f93d4f..bbd1bbe 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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. diff --git a/tests/Handler_test.go b/tests/Handler_test.go index 26b2756..fe11f11 100644 --- a/tests/Handler_test.go +++ b/tests/Handler_test.go @@ -12,6 +12,7 @@ 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) @@ -19,7 +20,28 @@ func TestConnectHandler(t *testing.T) { // 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 { @@ -34,7 +56,8 @@ 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) @@ -42,7 +65,41 @@ func TestSendHandler(t *testing.T) { // 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 { @@ -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 { @@ -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) @@ -96,7 +187,6 @@ func TestMultiSendHandler(t *testing.T) { panic(err) } response = strings.TrimSpace(response) - fmt.Println(response) } } // Cleanup