aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/vendor/github.com/google/pprof/internal/driver/interactive_test.go
blob: 758adf9bdced1cf73a2a1bdb07a64295c002f093 (plain)
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
// Copyright 2014 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package driver

import (
	"fmt"
	"math/rand"
	"strings"
	"testing"

	"github.com/google/pprof/internal/plugin"
	"github.com/google/pprof/internal/proftest"
	"github.com/google/pprof/internal/report"
	"github.com/google/pprof/internal/transport"
	"github.com/google/pprof/profile"
)

func TestShell(t *testing.T) {
	p := &profile.Profile{}
	generateReportWrapper = checkValue
	defer func() { generateReportWrapper = generateReport }()

	// Use test commands and variables to exercise interactive processing
	var savedCommands commands
	savedCommands, pprofCommands = pprofCommands, testCommands
	defer func() { pprofCommands = savedCommands }()

	savedVariables := pprofVariables
	defer func() { pprofVariables = savedVariables }()

	// Random interleave of independent scripts
	pprofVariables = testVariables(savedVariables)

	// pass in HTTPTransport when setting defaults, because otherwise default
	// transport will try to add flags to the default flag set.
	o := setDefaults(&plugin.Options{HTTPTransport: transport.New(nil)})
	o.UI = newUI(t, interleave(script, 0))
	if err := interactive(p, o); err != nil {
		t.Error("first attempt:", err)
	}
	// Random interleave of independent scripts
	pprofVariables = testVariables(savedVariables)
	o.UI = newUI(t, interleave(script, 1))
	if err := interactive(p, o); err != nil {
		t.Error("second attempt:", err)
	}

	// Random interleave of independent scripts with shortcuts
	pprofVariables = testVariables(savedVariables)
	var scScript []string
	pprofShortcuts, scScript = makeShortcuts(interleave(script, 2), 1)
	o.UI = newUI(t, scScript)
	if err := interactive(p, o); err != nil {
		t.Error("first shortcut attempt:", err)
	}

	// Random interleave of independent scripts with shortcuts
	pprofVariables = testVariables(savedVariables)
	pprofShortcuts, scScript = makeShortcuts(interleave(script, 1), 2)
	o.UI = newUI(t, scScript)
	if err := interactive(p, o); err != nil {
		t.Error("second shortcut attempt:", err)
	}

	// Group with invalid value
	pprofVariables = testVariables(savedVariables)
	ui := &proftest.TestUI{
		T:       t,
		Input:   []string{"cumulative=this"},
		AllowRx: `Unrecognized value for cumulative: "this". Use one of cum, flat`,
	}
	o.UI = ui
	if err := interactive(p, o); err != nil {
		t.Error("invalid group value:", err)
	}
	// Confirm error message written out once.
	if ui.NumAllowRxMatches != 1 {
		t.Errorf("want error message to be printed 1 time, got %v", ui.NumAllowRxMatches)
	}
	// Verify propagation of IO errors
	pprofVariables = testVariables(savedVariables)
	o.UI = newUI(t, []string{"**error**"})
	if err := interactive(p, o); err == nil {
		t.Error("expected IO error, got nil")
	}

}

var testCommands = commands{
	"check": &command{report.Raw, nil, nil, true, "", ""},
}

func testVariables(base variables) variables {
	v := base.makeCopy()

	v["b"] = &variable{boolKind, "f", "", ""}
	v["bb"] = &variable{boolKind, "f", "", ""}
	v["i"] = &variable{intKind, "0", "", ""}
	v["ii"] = &variable{intKind, "0", "", ""}
	v["f"] = &variable{floatKind, "0", "", ""}
	v["ff"] = &variable{floatKind, "0", "", ""}
	v["s"] = &variable{stringKind, "", "", ""}
	v["ss"] = &variable{stringKind, "", "", ""}

	v["ta"] = &variable{boolKind, "f", "radio", ""}
	v["tb"] = &variable{boolKind, "f", "radio", ""}
	v["tc"] = &variable{boolKind, "t", "radio", ""}

	return v
}

// script contains sequences of commands to be executed for testing. Commands
// are split by semicolon and interleaved randomly, so they must be
// independent from each other.
var script = []string{
	"bb=true;bb=false;check bb=false;bb=yes;check bb=true",
	"b=1;check b=true;b=n;check b=false",
	"i=-1;i=-2;check i=-2;i=999999;check i=999999",
	"check ii=0;ii=-1;check ii=-1;ii=100;check ii=100",
	"f=-1;f=-2.5;check f=-2.5;f=0.0001;check f=0.0001",
	"check ff=0;ff=-1.01;check ff=-1.01;ff=100;check ff=100",
	"s=one;s=two;check s=two",
	"ss=tree;check ss=tree;ss=;check ss;ss=forest;check ss=forest",
	"ta=true;check ta=true;check tb=false;check tc=false;tb=1;check tb=true;check ta=false;check tc=false;tc=yes;check tb=false;check ta=false;check tc=true",
}

func makeShortcuts(input []string, seed int) (shortcuts, []string) {
	rand.Seed(int64(seed))

	s := shortcuts{}
	var output, chunk []string
	for _, l := range input {
		chunk = append(chunk, l)
		switch rand.Intn(3) {
		case 0:
			// Create a macro for commands in 'chunk'.
			macro := fmt.Sprintf("alias%d", len(s))
			s[macro] = chunk
			output = append(output, macro)
			chunk = nil
		case 1:
			// Append commands in 'chunk' by themselves.
			output = append(output, chunk...)
			chunk = nil
		case 2:
			// Accumulate commands into 'chunk'
		}
	}
	output = append(output, chunk...)
	return s, output
}

func newUI(t *testing.T, input []string) plugin.UI {
	return &proftest.TestUI{
		T:     t,
		Input: input,
	}
}

func checkValue(p *profile.Profile, cmd []string, vars variables, o *plugin.Options) error {
	if len(cmd) != 2 {
		return fmt.Errorf("expected len(cmd)==2, got %v", cmd)
	}

	input := cmd[1]
	args := strings.SplitN(input, "=", 2)
	if len(args) == 0 {
		return fmt.Errorf("unexpected empty input")
	}
	name, value := args[0], ""
	if len(args) == 2 {
		value = args[1]
	}

	gotv := vars[name]
	if gotv == nil {
		return fmt.Errorf("Could not find variable named %s", name)
	}

	if got := gotv.stringValue(); got != value {
		return fmt.Errorf("Variable %s, want %s, got %s", name, value, got)
	}
	return nil
}

func interleave(input []string, seed int) []string {
	var inputs [][]string
	for _, s := range input {
		inputs = append(inputs, strings.Split(s, ";"))
	}
	rand.Seed(int64(seed))
	var output []string
	for len(inputs) > 0 {
		next := rand.Intn(len(inputs))
		output = append(output, inputs[next][0])
		if tail := inputs[next][1:]; len(tail) > 0 {
			inputs[next] = tail
		} else {
			inputs = append(inputs[:next], inputs[next+1:]...)
		}
	}
	return output
}

func TestInteractiveCommands(t *testing.T) {
	type interactiveTestcase struct {
		input string
		want  map[string]string
	}

	testcases := []interactiveTestcase{
		{
			"top 10 --cum focus1 -ignore focus2",
			map[string]string{
				"functions": "true",
				"nodecount": "10",
				"cum":       "true",
				"focus":     "focus1|focus2",
				"ignore":    "ignore",
			},
		},
		{
			"top10 --cum focus1 -ignore focus2",
			map[string]string{
				"functions": "true",
				"nodecount": "10",
				"cum":       "true",
				"focus":     "focus1|focus2",
				"ignore":    "ignore",
			},
		},
		{
			"dot",
			map[string]string{
				"functions": "true",
				"nodecount": "80",
				"cum":       "false",
			},
		},
		{
			"tags   -ignore1 -ignore2 focus1 >out",
			map[string]string{
				"functions": "true",
				"nodecount": "80",
				"cum":       "false",
				"output":    "out",
				"tagfocus":  "focus1",
				"tagignore": "ignore1|ignore2",
			},
		},
		{
			"weblist  find -test",
			map[string]string{
				"functions": "false",
				"addresses": "true",
				"noinlines": "true",
				"nodecount": "0",
				"cum":       "false",
				"flat":      "true",
				"ignore":    "test",
			},
		},
		{
			"callgrind   fun -ignore  >out",
			map[string]string{
				"functions": "false",
				"addresses": "true",
				"nodecount": "0",
				"cum":       "false",
				"flat":      "true",
				"output":    "out",
			},
		},
		{
			"999",
			nil, // Error
		},
	}

	for _, tc := range testcases {
		cmd, vars, err := parseCommandLine(strings.Fields(tc.input))
		if tc.want == nil && err != nil {
			// Error expected
			continue
		}
		if err != nil {
			t.Errorf("failed on %q: %v", tc.input, err)
			continue
		}

		// Get report output format
		c := pprofCommands[cmd[0]]
		if c == nil {
			t.Errorf("unexpected nil command")
		}
		vars = applyCommandOverrides(cmd[0], c.format, vars)

		for n, want := range tc.want {
			if got := vars[n].stringValue(); got != want {
				t.Errorf("failed on %q, cmd=%q, %s got %s, want %s", tc.input, cmd, n, got, want)
			}
		}
	}
}