summaryrefslogtreecommitdiffstats
path: root/client/go/internal/cli/cmd/feed_test.go
blob: d1904ad815e3a03e07de5b468373e49d8379d302 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package cmd

import (
	"bytes"
	"fmt"
	"os"
	"path/filepath"
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"github.com/vespa-engine/vespa/client/go/internal/mock"
)

type manualClock struct {
	t    time.Time
	tick time.Duration
}

func (c *manualClock) now() time.Time {
	t := c.t
	c.t = c.t.Add(c.tick)
	return t
}

func TestFeed(t *testing.T) {
	clock := &manualClock{tick: time.Second}
	cli, stdout, stderr := newTestCLI(t)
	httpClient := cli.httpClient.(*mock.HTTPClient)
	httpClient.ReadBody = true
	cli.now = clock.now

	td := t.TempDir()
	doc := []byte(`{
  "put": "id:ns:type::doc1",
  "fields": {"foo": "123"}
}`)
	jsonFile1 := filepath.Join(td, "docs1.jsonl")
	jsonFile2 := filepath.Join(td, "docs2.jsonl")
	require.Nil(t, os.WriteFile(jsonFile1, doc, 0644))
	require.Nil(t, os.WriteFile(jsonFile2, doc, 0644))

	httpClient.NextResponseString(200, `{"message":"OK"}`)
	httpClient.NextResponseString(200, `{"message":"OK"}`)
	require.Nil(t, cli.Run("feed", jsonFile1, jsonFile2))

	assert.Equal(t, "", stderr.String())
	want := `{
  "feeder.seconds": 5.000,
  "feeder.ok.count": 2,
  "feeder.ok.rate": 0.400,
  "feeder.error.count": 0,
  "feeder.inflight.count": 0,
  "http.request.count": 2,
  "http.request.bytes": 50,
  "http.request.MBps": 0.000,
  "http.exception.count": 0,
  "http.response.count": 2,
  "http.response.bytes": 32,
  "http.response.MBps": 0.000,
  "http.response.error.count": 0,
  "http.response.latency.millis.min": 1000,
  "http.response.latency.millis.avg": 1000,
  "http.response.latency.millis.max": 1000,
  "http.response.code.counts": {
    "200": 2
  }
}
`
	assert.Equal(t, want, stdout.String())

	stdout.Reset()
	var stdinBuf bytes.Buffer
	stdinBuf.Write(doc)
	stdinBuf.Write(doc)
	cli.Stdin = &stdinBuf
	httpClient.NextResponseString(200, `{"message":"OK"}`)
	httpClient.NextResponseString(200, `{"message":"OK"}`)
	require.Nil(t, cli.Run("feed", "-"))
	assert.Equal(t, want, stdout.String())

	httpClient.NextResponseString(500, `{"message":"it's broken yo"}`)
	require.Nil(t, cli.Run("feed", jsonFile1))
	assert.Equal(t, "feed: got status 500 ({\"message\":\"it's broken yo\"}) for put id:ns:type::doc1: retrying\n", stderr.String())
	stderr.Reset()
	httpClient.NextResponseError(fmt.Errorf("something else is broken"))
	require.Nil(t, cli.Run("feed", jsonFile1))
	assert.Equal(t, "feed: got error \"something else is broken\" (no body) for put id:ns:type::doc1: retrying\n", stderr.String())
}

func TestFeedInvalid(t *testing.T) {
	clock := &manualClock{tick: time.Second}
	cli, stdout, stderr := newTestCLI(t)
	httpClient := cli.httpClient.(*mock.HTTPClient)
	httpClient.ReadBody = true
	cli.now = clock.now

	td := t.TempDir()
	doc := []byte(`
{
  "put": "id:ns:type::doc1",
  "fields": {"foo": "123"}
},
{
  "put": "id:ns:type::doc2",
  "fields": {"foo": "invalid json"
}`)
	jsonFile := filepath.Join(td, "docs.jsonl")
	require.Nil(t, os.WriteFile(jsonFile, doc, 0644))
	httpClient.NextResponseString(200, `{"message":"OK"}`)
	require.NotNil(t, cli.Run("feed", jsonFile))

	want := `{
  "feeder.seconds": 3.000,
  "feeder.ok.count": 1,
  "feeder.ok.rate": 0.333,
  "feeder.error.count": 0,
  "feeder.inflight.count": 0,
  "http.request.count": 1,
  "http.request.bytes": 25,
  "http.request.MBps": 0.000,
  "http.exception.count": 0,
  "http.response.count": 1,
  "http.response.bytes": 16,
  "http.response.MBps": 0.000,
  "http.response.error.count": 0,
  "http.response.latency.millis.min": 1000,
  "http.response.latency.millis.avg": 1000,
  "http.response.latency.millis.max": 1000,
  "http.response.code.counts": {
    "200": 1
  }
}
`
	assert.Equal(t, want, stdout.String())
	assert.Contains(t, stderr.String(), "Error: failed to decode document")
}