summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJon Bratseth <bratseth@gmail.com>2021-08-20 09:06:24 +0200
committerJon Bratseth <bratseth@gmail.com>2021-08-20 09:06:24 +0200
commitf1974d5aea3acef63c30588199c2314c1285a550 (patch)
treebe444eff46257a79f429c93e9d7cb26d379e980d
parentf3020ddc8dfdfb9427b1b9140586bc45d6db3577 (diff)
Pass client explicitly
-rw-r--r--client/go/src/cmd/command_tester.go36
-rw-r--r--client/go/src/cmd/deploy_test.go30
-rw-r--r--client/go/src/cmd/document_test.go11
-rw-r--r--client/go/src/cmd/init_test.go2
-rw-r--r--client/go/src/cmd/query_test.go6
-rw-r--r--client/go/src/cmd/status_test.go18
6 files changed, 54 insertions, 49 deletions
diff --git a/client/go/src/cmd/command_tester.go b/client/go/src/cmd/command_tester.go
index ffdebf0445d..2c3186a7aff 100644
--- a/client/go/src/cmd/command_tester.go
+++ b/client/go/src/cmd/command_tester.go
@@ -14,27 +14,14 @@ import (
"time"
)
-// The HTTP status code that will be returned from the next invocation. Default: 200
-var nextStatus int
-
-// The response body code that will be returned from the next invocation. Default: ""
-var nextBody string
-
-// A recording of the last HTTP request made through this
-var lastRequest *http.Request
-
func reset() {
// Persistent flags in Cobra persists over tests
rootCmd.SetArgs([]string{"status", "-t", ""})
rootCmd.Execute()
-
- lastRequest = nil
- nextStatus = 200
- nextBody = ""
}
-func executeCommand(t *testing.T, args []string, moreArgs []string) (standardout string) {
- utils.ActiveHttpClient = mockHttpClient{}
+func executeCommand(t *testing.T, client *mockHttpClient, args []string, moreArgs []string) (standardout string) {
+ utils.ActiveHttpClient = client
b := bytes.NewBufferString("")
utils.Out = b
rootCmd.SetArgs(concat(args, moreArgs))
@@ -45,13 +32,24 @@ func executeCommand(t *testing.T, args []string, moreArgs []string) (standardout
}
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) {
- lastRequest = 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{
- StatusCode: nextStatus,
- Body: ioutil.NopCloser(bytes.NewBufferString(nextBody)),
+ StatusCode: c.nextStatus,
+ Body: ioutil.NopCloser(bytes.NewBufferString(c.nextBody)),
Header: make(http.Header),
},
nil
diff --git a/client/go/src/cmd/deploy_test.go b/client/go/src/cmd/deploy_test.go
index 4d80f25a0d0..8cd984e7ddb 100644
--- a/client/go/src/cmd/deploy_test.go
+++ b/client/go/src/cmd/deploy_test.go
@@ -11,41 +11,45 @@ import (
func TestDeployZip(t *testing.T) {
reset()
+ client := &mockHttpClient{}
assert.Equal(t,
"\x1b[32mSuccess\n",
- executeCommand(t, []string{"deploy", "testdata/application.zip"}, []string{}))
- assertDeployRequestMade("http://127.0.0.1:19071", t)
+ executeCommand(t, client, []string{"deploy", "testdata/application.zip"}, []string{}))
+ assertDeployRequestMade("http://127.0.0.1:19071", client, t)
}
func TestDeployZipWithURLTargetArgument(t *testing.T) {
reset()
+ client := &mockHttpClient{}
assert.Equal(t,
"\x1b[32mSuccess\n",
- executeCommand(t, []string{"deploy", "testdata/application.zip", "-t", "http://target:19071"}, []string{}))
- assertDeployRequestMade("http://target:19071", t)
+ executeCommand(t, client, []string{"deploy", "testdata/application.zip", "-t", "http://target:19071"}, []string{}))
+ assertDeployRequestMade("http://target:19071", client, t)
}
func TestDeployZipWitLocalTargetArgument(t *testing.T) {
reset()
+ client := &mockHttpClient{}
assert.Equal(t,
"\x1b[32mSuccess\n",
- executeCommand(t, []string{"deploy", "testdata/application.zip", "-t", "local"}, []string{}))
- assertDeployRequestMade("http://127.0.0.1:19071", t)
+ executeCommand(t, client, []string{"deploy", "testdata/application.zip", "-t", "local"}, []string{}))
+ assertDeployRequestMade("http://127.0.0.1:19071", client, t)
}
func TestDeployDirectory(t *testing.T) {
reset()
+ client := &mockHttpClient{}
assert.Equal(t,
"\x1b[32mSuccess\n",
- executeCommand(t, []string{"deploy", "testdata/src/main/application"}, []string{}))
- assertDeployRequestMade("http://127.0.0.1:19071", t)
+ executeCommand(t, client, []string{"deploy", "testdata/src/main/application"}, []string{}))
+ assertDeployRequestMade("http://127.0.0.1:19071", client, t)
}
-func assertDeployRequestMade(target string, t *testing.T) {
- assert.Equal(t, target + "/application/v2/tenant/default/prepareandactivate", lastRequest.URL.String())
- assert.Equal(t, "application/zip", lastRequest.Header.Get("Content-Type"))
- assert.Equal(t, "POST", lastRequest.Method)
- var body = lastRequest.Body
+func assertDeployRequestMade(target string, client *mockHttpClient, t *testing.T) {
+ assert.Equal(t, target + "/application/v2/tenant/default/prepareandactivate", client.lastRequest.URL.String())
+ assert.Equal(t, "application/zip", client.lastRequest.Header.Get("Content-Type"))
+ assert.Equal(t, "POST", client.lastRequest.Method)
+ var body = client.lastRequest.Body
assert.NotNil(t, body)
buf := make([]byte, 7) // Just check the first few bytes
body.Read(buf)
diff --git a/client/go/src/cmd/document_test.go b/client/go/src/cmd/document_test.go
index 04e7b1f9240..a7f975114a6 100644
--- a/client/go/src/cmd/document_test.go
+++ b/client/go/src/cmd/document_test.go
@@ -17,14 +17,15 @@ func TestDocumentPut(t *testing.T) {
func assertDocumentPut(documentId string, jsonFile string, t *testing.T) {
reset()
+ client := &mockHttpClient{}
assert.Equal(t,
"\x1b[32mSuccess\n",
- executeCommand(t, []string{"document", documentId, jsonFile}, []string{}))
+ executeCommand(t, client, []string{"document", documentId, jsonFile}, []string{}))
target := getTarget(documentContext).document
- assert.Equal(t, target + "/document/v1/" + documentId, lastRequest.URL.String())
- assert.Equal(t, "application/json", lastRequest.Header.Get("Content-Type"))
- assert.Equal(t, "POST", lastRequest.Method)
+ assert.Equal(t, target + "/document/v1/" + documentId, client.lastRequest.URL.String())
+ assert.Equal(t, "application/json", client.lastRequest.Header.Get("Content-Type"))
+ assert.Equal(t, "POST", client.lastRequest.Method)
fileContent, _ := ioutil.ReadFile(jsonFile)
- assert.Equal(t, string(fileContent), utils.ReaderToString(lastRequest.Body))
+ assert.Equal(t, string(fileContent), utils.ReaderToString(client.lastRequest.Body))
}
diff --git a/client/go/src/cmd/init_test.go b/client/go/src/cmd/init_test.go
index 91bb7827700..9f748f88483 100644
--- a/client/go/src/cmd/init_test.go
+++ b/client/go/src/cmd/init_test.go
@@ -19,7 +19,7 @@ func TestInit(t *testing.T) {
func assertCreated(app string, sampleAppName string, t *testing.T) {
reset()
existingSampleAppsZip = "testdata/sample-apps-master.zip"
- standardOut := executeCommand(t, []string{"init", app, sampleAppName}, []string{})
+ standardOut := executeCommand(t, &mockHttpClient{}, []string{"init", app, sampleAppName}, []string{})
defer os.RemoveAll(app)
assert.Equal(t, "\x1b[32mCreated " + app + "\n", standardOut)
assert.True(t, utils.PathExists(filepath.Join(app, "README.md")))
diff --git a/client/go/src/cmd/query_test.go b/client/go/src/cmd/query_test.go
index 8c9b330a890..90c635089e2 100644
--- a/client/go/src/cmd/query_test.go
+++ b/client/go/src/cmd/query_test.go
@@ -29,10 +29,10 @@ func IgnoreTestSimpleQueryMissingQuestionMarkAndQueryEquals(t *testing.T) {
func assertQuery(expectedQuery string, query string, t *testing.T) {
reset()
- nextBody = "query result"
+ client := &mockHttpClient{ nextBody: "query result", }
assert.Equal(t,
"query result\n",
- executeCommand(t, []string{"query", query},[]string{}),
+ executeCommand(t, client, []string{"query", query},[]string{}),
"query output")
- assert.Equal(t, getTarget(queryContext).query + "/search/" + expectedQuery, lastRequest.URL.String())
+ assert.Equal(t, getTarget(queryContext).query + "/search/" + expectedQuery, client.lastRequest.URL.String())
}
diff --git a/client/go/src/cmd/status_test.go b/client/go/src/cmd/status_test.go
index c3d6e15acc6..6fc5cf6a2f6 100644
--- a/client/go/src/cmd/status_test.go
+++ b/client/go/src/cmd/status_test.go
@@ -45,31 +45,33 @@ func TestStatusErrorResponse(t *testing.T) {
}
func assertConfigServerStatus(target string, args []string, t *testing.T) {
+ client := &mockHttpClient{}
assert.Equal(t,
"\x1b[32mConfig server at " + target + " is ready\n",
- executeCommand(t, []string{"status", "config-server"}, args),
+ executeCommand(t, client, []string{"status", "config-server"}, args),
"vespa status config-server")
- assert.Equal(t, target + "/ApplicationStatus", lastRequest.URL.String())
+ assert.Equal(t, target + "/ApplicationStatus", client.lastRequest.URL.String())
}
func assertContainerStatus(target string, args []string, t *testing.T) {
+ client := &mockHttpClient{}
assert.Equal(t,
"\x1b[32mContainer at " + target + " is ready\n",
- executeCommand(t, []string{"status", "container"}, args),
+ executeCommand(t, client, []string{"status", "container"}, args),
"vespa status container")
- assert.Equal(t, target + "/ApplicationStatus", lastRequest.URL.String())
+ assert.Equal(t, target + "/ApplicationStatus", client.lastRequest.URL.String())
assert.Equal(t,
"\x1b[32mContainer at " + target + " is ready\n",
- executeCommand(t, []string{"status"}, args),
+ executeCommand(t, client, []string{"status"}, args),
"vespa status (the default)")
- assert.Equal(t, target + "/ApplicationStatus", lastRequest.URL.String())
+ assert.Equal(t, target + "/ApplicationStatus", client.lastRequest.URL.String())
}
func assertContainerError(target string, args []string, t *testing.T) {
- nextStatus = 500
+ client := &mockHttpClient{ nextStatus: 500,}
assert.Equal(t,
"\x1b[31mContainer at " + target + " is not ready\n\x1b[33mResponse status: \n",
- executeCommand(t, []string{"status", "container"}, args),
+ executeCommand(t, client, []string{"status", "container"}, args),
"vespa status container")
} \ No newline at end of file