From 9b144e7f31ff844d27695b9e8b2646f8cd357126 Mon Sep 17 00:00:00 2001 From: David Dworken Date: Sun, 9 Jan 2022 14:34:59 -0800 Subject: [PATCH] working integration tests --- README.md | 2 +- clients/local/client.go | 7 ++- clients/local/client_test.go | 62 +++++++++++++++++++++++---- clients/local/test_interaction.sh | 8 ---- clients/remote/client.go | 4 +- clients/remote/client_test.go | 71 +++++++++++++++++++++++++++++++ shared/client.go | 13 ++++-- shared/testutils.go | 6 +++ 8 files changed, 150 insertions(+), 23 deletions(-) delete mode 100644 clients/local/test_interaction.sh create mode 100644 clients/remote/client_test.go diff --git a/README.md b/README.md index 4b349c9..442e8a6 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ Installation: ``` -export PROMPT_COMMAND='~/.hishtory-client upload $? "`history 1`"' +export PROMPT_COMMAND='~/.hishtory-client saveHistoryEntry $? "`history 1`"' ``` diff --git a/clients/local/client.go b/clients/local/client.go index 0f22749..0e9c365 100644 --- a/clients/local/client.go +++ b/clients/local/client.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "os" "strings" @@ -19,6 +20,8 @@ func main() { shared.CheckFatalError(shared.Enable()) case "disable": shared.CheckFatalError(shared.Disable()) + default: + shared.CheckFatalError(fmt.Errorf("unknown command: %s", os.Args[1])) } } @@ -35,9 +38,9 @@ func query() { db, err := shared.OpenDB() shared.CheckFatalError(err) query := strings.Join(os.Args[2:], " ") - data, err := shared.Search(db, query, userSecret, 25) + data, err := shared.Search(db, userSecret, query, 25) shared.CheckFatalError(err) - shared.DisplayResults(data) + shared.DisplayResults(data, false) } func saveHistoryEntry() { diff --git a/clients/local/client_test.go b/clients/local/client_test.go index 28be339..fa987c1 100644 --- a/clients/local/client_test.go +++ b/clients/local/client_test.go @@ -2,23 +2,69 @@ package main import ( "bytes" - "fmt" + "io/ioutil" "os/exec" + "regexp" + "strings" "testing" "github.com/ddworken/hishtory/shared" ) +func RunInteractiveBashCommands(t *testing.T, script string) string { + shared.Check(t, ioutil.WriteFile("/tmp/hishtory-test-in.sh", []byte(script), 0600)) + cmd := exec.Command("bash", "-i") + cmd.Stdin = strings.NewReader(script) + var out bytes.Buffer + cmd.Stdout = &out + var err bytes.Buffer + cmd.Stderr = &err + shared.CheckWithInfo(t, cmd.Run(), out.String()+err.String()) + return out.String() +} + +const ( + PROMPT_COMMAND = "export PROMPT_COMMAND='/tmp/client saveHistoryEntry $? \"`history 1`\"'" +) + func TestIntegration(t *testing.T) { // Set up defer shared.BackupAndRestore(t) - // Run the test - cmd := exec.Command("bash", "--init-file", "test_interaction.sh") - var out bytes.Buffer - cmd.Stdout = &out - if err := cmd.Run(); err != nil { - t.Fatalf("unexpected error when running test script: %v", err) + // Test init + out := RunInteractiveBashCommands(t, ` + gvm use go1.17 + cd ../../ + go build -o /tmp/client clients/local/client.go + /tmp/client init`) + match, err := regexp.MatchString(`Setting secret hishtory key to .*`, out) + shared.Check(t, err) + if !match { + t.Fatalf("unexpected output from init: %v", out) + } + + // Test recording commands + out = RunInteractiveBashCommands(t, PROMPT_COMMAND+` + ls /a + ls /bar + ls /foo + echo foo + echo bar + /tmp/client disable + echo thisisnotrecorded + /tmp/client enable + echo thisisrecorded + `) + if out != "foo\nbar\nthisisnotrecorded\nthisisrecorded\n" { + t.Fatalf("unexpected output from running commands: %#v", out) + } + + // Test querying for all commands + out = RunInteractiveBashCommands(t, "/tmp/client query") + expected := []string{"echo thisisrecorded", "/tmp/client enable", "echo bar", "echo foo", "ls /foo", "ls /bar", "ls /a"} + for _, item := range expected { + if !strings.Contains(out, item) { + t.Fatalf("output is missing expected item %#v: %#v", item, out) + } } - fmt.Printf("%q\n", out.String()) } diff --git a/clients/local/test_interaction.sh b/clients/local/test_interaction.sh deleted file mode 100644 index 65b9995..0000000 --- a/clients/local/test_interaction.sh +++ /dev/null @@ -1,8 +0,0 @@ -go build -o /tmp/client clients/local/client.go -/tmp/client init -export PROMPT_COMMAND='/tmp/client upload $? "`history 1`"' -ls /a -ls /bar -ls /foo -echo foo -/tmp/client query \ No newline at end of file diff --git a/clients/remote/client.go b/clients/remote/client.go index 1d5b49b..6ed379e 100644 --- a/clients/remote/client.go +++ b/clients/remote/client.go @@ -27,6 +27,8 @@ func main() { shared.CheckFatalError(shared.Enable()) case "disable": shared.CheckFatalError(shared.Disable()) + default: + shared.CheckFatalError(fmt.Errorf("unknown command: %s", os.Args[1])) } } @@ -63,7 +65,7 @@ func query() { var data []*shared.HistoryEntry err = json.Unmarshal(resp_body, &data) shared.CheckFatalError(err) - shared.DisplayResults(data) + shared.DisplayResults(data, true) } func saveHistoryEntry() { diff --git a/clients/remote/client_test.go b/clients/remote/client_test.go new file mode 100644 index 0000000..ceb2c99 --- /dev/null +++ b/clients/remote/client_test.go @@ -0,0 +1,71 @@ +package main + +import ( + "bytes" + "io/ioutil" + "os/exec" + "regexp" + "strings" + "testing" + + "github.com/ddworken/hishtory/shared" +) + +func RunInteractiveBashCommands(t *testing.T, script string) string { + shared.Check(t, ioutil.WriteFile("/tmp/hishtory-test-in.sh", []byte(script), 0600)) + cmd := exec.Command("bash", "-i") + cmd.Stdin = strings.NewReader(script) + var out bytes.Buffer + cmd.Stdout = &out + var err bytes.Buffer + cmd.Stderr = &err + shared.CheckWithInfo(t, cmd.Run(), out.String()+err.String()) + return out.String() +} + +const ( + PROMPT_COMMAND = "export PROMPT_COMMAND='/tmp/client saveHistoryEntry $? \"`history 1`\"'" +) + +func TestIntegration(t *testing.T) { + // Set up + defer shared.BackupAndRestore(t) + + // Test init + out := RunInteractiveBashCommands(t, ` + gvm use go1.17 + cd ../../ + go build -o /tmp/client clients/remote/client.go + go build -o /tmp/server server/server.go + /tmp/client init`) + match, err := regexp.MatchString(`Setting secret hishtory key to .*`, out) + shared.Check(t, err) + if !match { + t.Fatalf("unexpected output from init: %v", out) + } + + // Test recording commands + out = RunInteractiveBashCommands(t, `/tmp/server &`+PROMPT_COMMAND+` + ls /a + ls /bar + ls /foo + echo foo + echo bar + /tmp/client disable + echo thisisnotrecorded + /tmp/client enable + echo thisisrecorded + `) + if out != "Listening on localhost:8080\nfoo\nbar\nthisisnotrecorded\nthisisrecorded\n" { + t.Fatalf("unexpected output from running commands: %#v", out) + } + + // Test querying for all commands + out = RunInteractiveBashCommands(t, `/tmp/server & /tmp/client query`) + expected := []string{"echo thisisrecorded", "/tmp/client enable", "echo bar", "echo foo", "ls /foo", "ls /bar", "ls /a"} + for _, item := range expected { + if !strings.Contains(out, item) { + t.Fatalf("output is missing expected item %#v: %#v", item, out) + } + } +} diff --git a/shared/client.go b/shared/client.go index ea309dc..cead645 100644 --- a/shared/client.go +++ b/shared/client.go @@ -113,13 +113,20 @@ func Setup(args []string) error { return SetConfig(config) } -func DisplayResults(results []*HistoryEntry) { +func DisplayResults(results []*HistoryEntry, displayHostname bool) { headerFmt := color.New(color.FgGreen, color.Underline).SprintfFunc() - tbl := table.New("Hostname", "CWD", "Timestamp", "Exit Code", "Command") + tbl := table.New("CWD", "Timestamp", "Exit Code", "Command") + if displayHostname { + tbl = table.New("Hostname", "CWD", "Timestamp", "Exit Code", "Command") + } tbl.WithHeaderFormatter(headerFmt) for _, result := range results { - tbl.AddRow(result.Hostname, result.CurrentWorkingDirectory, result.EndTime.Format("Jan 2 2006 15:04:05 MST"), result.ExitCode, result.Command) + if displayHostname { + tbl.AddRow(result.Hostname, result.CurrentWorkingDirectory, result.EndTime.Format("Jan 2 2006 15:04:05 MST"), result.ExitCode, result.Command) + } else { + tbl.AddRow(result.CurrentWorkingDirectory, result.EndTime.Format("Jan 2 2006 15:04:05 MST"), result.ExitCode, result.Command) + } } tbl.Print() diff --git a/shared/testutils.go b/shared/testutils.go index 7520438..148f266 100644 --- a/shared/testutils.go +++ b/shared/testutils.go @@ -13,6 +13,12 @@ func Check(t *testing.T, err error) { } } +func CheckWithInfo(t *testing.T, err error, additionalInfo string) { + if err != nil { + t.Fatalf("Unexpected error: %v! Additional info: %v", err, additionalInfo) + } +} + func BackupAndRestore(t *testing.T) func() { homedir, err := os.UserHomeDir() if err != nil {