aboutsummaryrefslogtreecommitdiffstats
path: root/client/go/cmd/command_tester.go
diff options
context:
space:
mode:
Diffstat (limited to 'client/go/cmd/command_tester.go')
-rw-r--r--client/go/cmd/command_tester.go58
1 files changed, 58 insertions, 0 deletions
diff --git a/client/go/cmd/command_tester.go b/client/go/cmd/command_tester.go
new file mode 100644
index 00000000000..40c3d04446e
--- /dev/null
+++ b/client/go/cmd/command_tester.go
@@ -0,0 +1,58 @@
+// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+// A helper for testing commands
+// Author: bratseth
+
+package cmd
+
+import (
+ "bytes"
+ "github.com/vespa-engine/vespa/util"
+ "github.com/stretchr/testify/assert"
+ "io/ioutil"
+ "net/http"
+ "testing"
+ "strconv"
+ "time"
+)
+
+func executeCommand(t *testing.T, client *mockHttpClient, args []string, moreArgs []string) (standardout string) {
+ util.ActiveHttpClient = client
+
+ // Reset - persistent flags in Cobra persists over tests
+ util.Out = bytes.NewBufferString("")
+ rootCmd.SetArgs([]string{"status", "-t", ""})
+ rootCmd.Execute()
+
+ b := bytes.NewBufferString("")
+ util.Out = b
+ rootCmd.SetArgs(append(args, moreArgs...))
+ rootCmd.Execute()
+ out, err := ioutil.ReadAll(b)
+ assert.Empty(t, err, "No error")
+ return string(out)
+}
+
+type mockHttpClient struct {
+ // The HTTP status code that will be returned from the next invocation. Default: 200
+ nextStatus int
+
+ // The response body code that will be returned from the next invocation. Default: ""
+ nextBody string
+
+ // A recording of the last HTTP request made through this
+ lastRequest *http.Request
+}
+
+func (c *mockHttpClient) Do(request *http.Request, timeout time.Duration) (response *http.Response, error error) {
+ if c.nextStatus == 0 {
+ c.nextStatus = 200
+ }
+ c.lastRequest = request
+ return &http.Response{
+ Status: "Status " + strconv.Itoa(c.nextStatus),
+ StatusCode: c.nextStatus,
+ Body: ioutil.NopCloser(bytes.NewBufferString(c.nextBody)),
+ Header: make(http.Header),
+ },
+ nil
+}