File: cmd_debug_timings_test.go

package info (click to toggle)
snapd 2.72-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 80,412 kB
  • sloc: sh: 16,506; ansic: 16,211; python: 11,213; makefile: 1,919; exp: 190; awk: 58; xml: 22
file content (348 lines) | stat: -rw-r--r-- 14,619 bytes parent folder | download | duplicates (3)
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// -*- Mode: Go; indent-tabs-mode: t -*-

/*
 * Copyright (C) 2019 Canonical Ltd
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package main_test

import (
	"fmt"
	"net/http"
	"strings"
	"time"

	. "gopkg.in/check.v1"

	"github.com/snapcore/snapd/cmd/snap"
)

type timingsCmdArgs struct {
	args, stdout, stderr, error string
}

var timingsTests = []timingsCmdArgs{{
	args:  "debug timings",
	error: "please provide change ID or type with --last=<type>, or query for --ensure=<name> or --startup=<name>",
}, {
	args:  "debug timings --ensure=seed 9",
	error: "cannot use change id, 'startup' or 'ensure' together",
}, {
	args:  "debug timings --ensure=seed --startup=ifacemgr",
	error: "cannot use change id, 'startup' or 'ensure' together",
}, {
	args:  "debug timings --last=install --all",
	error: "cannot use 'all' with change id or 'last'",
}, {
	args:  "debug timings --last=remove",
	error: `no changes of type "remove" found`,
}, {
	args:  "debug timings --startup=load-state 9",
	error: "cannot use change id, 'startup' or 'ensure' together",
}, {
	args:  "debug timings --all 9",
	error: "cannot use 'all' with change id or 'last'",
}, {
	args: "debug timings --last=install",
	stdout: "ID   Status        Doing      Undoing  Summary\n" +
		"40   Doing         910ms            -  lane 0 task bar summary\n" +
		" ^                   1ms            -    foo summary\n" +
		"  ^                  1ms            -      bar summary\n" +
		"41   Done          210ms            -  lane 1 task baz summary\n" +
		"42   Done          310ms            -  lane 1 task boo summary\n" +
		"43   Done          310ms            -  lane 0 task doh summary\n\n",
}, {
	args: "debug timings 1",
	stdout: "ID   Status        Doing      Undoing  Summary\n" +
		"40   Doing         910ms            -  lane 0 task bar summary\n" +
		" ^                   1ms            -    foo summary\n" +
		"  ^                  1ms            -      bar summary\n" +
		"41   Done          210ms            -  lane 1 task baz summary\n" +
		"42   Done          310ms            -  lane 1 task boo summary\n" +
		"43   Done          310ms            -  lane 0 task doh summary\n\n",
}, {
	args: "debug timings 1 --verbose",
	stdout: "ID   Status        Doing      Undoing  Label  Summary\n" +
		"40   Doing         910ms            -  bar    lane 0 task bar summary\n" +
		" ^                   1ms            -  foo      foo summary\n" +
		"  ^                  1ms            -  bar        bar summary\n" +
		"41   Done          210ms            -  baz    lane 1 task baz summary\n" +
		"42   Done          310ms            -  boo    lane 1 task boo summary\n" +
		"43   Done          310ms            -  doh    lane 0 task doh summary\n\n",
}, {
	args: "debug timings --ensure=seed",
	stdout: "ID    Status        Doing      Undoing  Summary\n" +
		"seed                  8ms            -  \n" +
		" ^                    8ms            -    baz summary\n" +
		"  ^                   8ms            -      booze summary\n" +
		"40    Doing         910ms            -  task bar summary\n" +
		" ^                    1ms            -    foo summary\n" +
		"  ^                   1ms            -      bar summary\n\n",
}, {
	args: "debug timings --ensure=seed --all",
	stdout: "ID    Status        Doing      Undoing  Summary\n" +
		"seed                  8ms            -  \n" +
		" ^                    8ms            -    bar summary 1\n" +
		" ^                    8ms            -    bar summary 2\n" +
		"40    Doing         910ms            -  task bar summary\n" +
		" ^                    1ms            -    foo summary\n" +
		"  ^                   1ms            -      bar summary\n" +
		"seed                  7ms            -  \n" +
		" ^                    7ms            -    baz summary 2\n" +
		"60    Doing         910ms            -  task bar summary\n" +
		" ^                    1ms            -    foo summary\n" +
		"  ^                   1ms            -      bar summary\n\n",
}, {
	args: "debug timings --ensure=seed --all --verbose",
	stdout: "ID    Status        Doing      Undoing  Label  Summary\n" +
		"seed                  8ms            -         \n" +
		" ^                    8ms            -  abc      bar summary 1\n" +
		" ^                    8ms            -  abc      bar summary 2\n" +
		"40    Doing         910ms            -  bar    task bar summary\n" +
		" ^                    1ms            -  foo      foo summary\n" +
		"  ^                   1ms            -  bar        bar summary\n" +
		"seed                  7ms            -         \n" +
		" ^                    7ms            -  ghi      baz summary 2\n" +
		"60    Doing         910ms            -  bar    task bar summary\n" +
		" ^                    1ms            -  foo      foo summary\n" +
		"  ^                   1ms            -  bar        bar summary\n\n",
}, {
	args: "debug timings --startup=ifacemgr",
	stdout: "ID        Status        Doing      Undoing  Summary\n" +
		"ifacemgr                  8ms            -  \n" +
		" ^                        8ms            -    baz summary\n" +
		"  ^                       8ms            -      booze summary\n\n",
}, {
	args: "debug timings --startup=ifacemgr --all",
	stdout: "ID        Status        Doing      Undoing  Summary\n" +
		"ifacemgr                  8ms            -  \n" +
		" ^                        8ms            -    baz summary\n" +
		"ifacemgr                  9ms            -  \n" +
		" ^                        9ms            -    baz summary\n\n",
}, {
	args: "debug timings 2",
	stdout: "ID   Status        Doing      Undoing  Summary\n" +
		"41   Undone            -        210ms  lane 0 task bar summary\n\n",
},
}

func (s *SnapSuite) TestGetDebugTimings(c *C) {
	s.mockCmdTimingsAPI(c)

	restore := main.MockIsStdinTTY(true)
	defer restore()

	for _, test := range timingsTests {
		s.stdout.Truncate(0)
		s.stderr.Truncate(0)

		c.Logf("Test: %s", test.args)

		_, err := main.Parser(main.Client()).ParseArgs(strings.Fields(test.args))
		if test.error != "" {
			c.Check(err, ErrorMatches, test.error)
		} else {
			c.Check(err, IsNil)
			c.Check(s.Stderr(), Equals, test.stderr)
			c.Check(s.Stdout(), Equals, test.stdout)
		}
	}
}

func (s *SnapSuite) mockCmdTimingsAPI(c *C) {
	s.RedirectClientToTestServer(func(w http.ResponseWriter, r *http.Request) {
		c.Assert(r.Method, Equals, "GET")

		if r.URL.Path == "/v2/debug" {
			q := r.URL.Query()
			aspect := q.Get("aspect")
			c.Assert(aspect, Equals, "change-timings")

			changeID := q.Get("change-id")
			ensure := q.Get("ensure")
			startup := q.Get("startup")
			all := q.Get("all")

			switch {
			case changeID == "1":
				// lane 0 and lane 1 tasks, interleaved
				fmt.Fprintln(w, `{"type":"sync","status-code":200,"status":"OK","result":[
				{"change-id":"1", "change-timings":{
					"41":{"doing-time":210000000, "status": "Done", "lane": 1, "ready-time": "2016-04-22T01:02:04Z", "kind": "baz", "summary": "lane 1 task baz summary"},
					"43":{"doing-time":310000000, "status": "Done", "ready-time": "2016-04-25T01:02:04Z", "kind": "doh", "summary": "lane 0 task doh summary"},
					"40":{"doing-time":910000000, "status": "Doing", "ready-time": "2016-04-20T00:00:00Z", "kind": "bar", "summary": "lane 0 task bar summary",
						"doing-timings":[
							{"label":"foo", "summary": "foo summary", "duration": 1000001},
							{"level":1, "label":"bar", "summary": "bar summary", "duration": 1000002}
						]},
					"42":{"doing-time":310000000, "status": "Done", "lane": 1, "ready-time": "2016-04-23T01:02:04Z", "kind": "boo", "summary": "lane 1 task boo summary"}
				}}]}`)
			case changeID == "2":
				// lane 0 tasks, interleaved
				fmt.Fprintln(w, `{"type":"sync","status-code":200,"status":"OK","result":[
				{"change-id":"1", "change-timings":{
					"41":{"undoing-time":210000000, "status": "Undone", "lane": 0, "ready-time": "2016-04-22T01:02:04Z", "kind": "baz", "summary": "lane 0 task bar summary"}
				}}]}`)
			case ensure == "seed" && all == "false":
				fmt.Fprintln(w, `{"type":"sync","status-code":200,"status":"OK","result":[
					{"change-id":"1",
					    "total-duration": 8000002,
						"ensure-timings": [
								{"label":"baz", "summary": "baz summary", "duration": 8000001},
								{"level":1, "label":"booze", "summary": "booze summary", "duration": 8000002}
							],
						"change-timings":{
							"40":{"doing-time":910000000, "status": "Doing", "kind": "bar", "summary": "task bar summary",
								"doing-timings":[
									{"label":"foo", "summary": "foo summary", "duration": 1000001},
									{"level":1, "label":"bar", "summary": "bar summary", "duration": 1000002}
					]}}}]}`)
			case ensure == "seed" && all == "true":
				fmt.Fprintln(w, `{"type":"sync","status-code":200,"status":"OK","result":[
						{"change-id":"1",
							"total-duration": 8000002,
							"ensure-timings": [
									{"label":"abc", "summary": "bar summary 1", "duration": 8000001},
									{"label":"abc", "summary": "bar summary 2", "duration": 8000002}
								],
							"change-timings":{
								"40":{"doing-time":910000000, "status": "Doing", "kind": "bar", "summary": "task bar summary",
									"doing-timings":[
										{"label":"foo", "summary": "foo summary", "duration": 1000001},
										{"level":1, "label":"bar", "summary": "bar summary", "duration": 1000002}
								]}}},
						{"change-id":"2",
							"total-duration": 7000002,
							"ensure-timings": [{"label":"ghi", "summary": "baz summary 2", "duration": 7000002}],
							"change-timings":{
								"60":{"doing-time":910000000, "status": "Doing", "kind": "bar", "summary": "task bar summary",
									"doing-timings":[
										{"label":"foo", "summary": "foo summary", "duration": 1000001},
										{"level":1, "label":"bar", "summary": "bar summary", "duration": 1000002}
								]}}}]}`)
			case startup == "ifacemgr" && all == "false":
				fmt.Fprintln(w, `{"type":"sync","status-code":200,"status":"OK","result":[
					{"total-duration": 8000002, "startup-timings": [
								{"label":"baz", "summary": "baz summary", "duration": 8000001},
								{"level":1, "label":"booze", "summary": "booze summary", "duration": 8000002}
					]}]}`)
			case startup == "ifacemgr" && all == "true":
				fmt.Fprintln(w, `{"type":"sync","status-code":200,"status":"OK","result":[
					{"total-duration": 8000002, "startup-timings": [
						{"label":"baz", "summary": "baz summary", "duration": 8000001}
					]},
					{"total-duration": 9000002, "startup-timings": [
						{"label":"baz", "summary": "baz summary", "duration": 9000001}
					]}]}`)
			default:
				c.Errorf("unexpected request: %s, %s, %s", changeID, ensure, all)
			}
			return
		}

		// request for all changes on --last=...
		if r.URL.Path == "/v2/changes" {
			fmt.Fprintln(w, `{"type":"sync","status-code":200,"status":"OK","result":[{
				"id":   "1",
				"kind": "install-snap",
				"summary": "a",
				"status": "Doing",
				"ready": false,
				"spawn-time": "2016-04-21T01:02:03Z",
				"ready-time": "2016-04-21T01:02:04Z",
				"tasks": [{"id":"99", "kind": "bar", "summary": ".", "status": "Doing", "progress": {"done": 0, "total": 1}, "spawn-time": "2016-04-21T01:02:03Z", "ready-time": "2016-04-21T01:02:04Z"}]
			  }]}`)
			return
		}
		c.Errorf("unexpected path %q", r.URL.Path)
	})
}

type TaskDef struct {
	TaskID    string
	Lane      int
	ReadyTime time.Time
}

func (s *SnapSuite) TestSortTimingsTasks(c *C) {
	mkTime := func(timeStr string) time.Time {
		t, err := time.Parse(time.RFC3339, timeStr)
		c.Assert(err, IsNil)
		return t
	}

	testData := []struct {
		ChangeTimings map[string]main.ChangeTimings
		Expected      []string
	}{{
		// nothing to do
		ChangeTimings: map[string]main.ChangeTimings{},
		Expected:      []string{},
	}, {
		ChangeTimings: map[string]main.ChangeTimings{
			// tasks in lane 0 only
			"1": {ReadyTime: mkTime("2019-04-21T00:00:00Z")},
			"2": {ReadyTime: mkTime("2019-05-21T00:00:00Z")},
			"3": {ReadyTime: mkTime("2019-02-21T00:00:00Z")},
			"4": {ReadyTime: mkTime("2019-03-21T00:00:00Z")},
			"5": {ReadyTime: mkTime("2019-01-21T00:00:00Z")},
		},
		Expected: []string{"5", "3", "4", "1", "2"},
	}, {
		// task in lane 1 with a task in lane 0 before and after it
		ChangeTimings: map[string]main.ChangeTimings{
			"1": {Lane: 1, ReadyTime: mkTime("2019-01-21T00:00:00Z")},
			"2": {Lane: 0, ReadyTime: mkTime("2019-01-20T00:00:00Z")},
			"3": {Lane: 0, ReadyTime: mkTime("2019-01-22T00:00:00Z")},
		},
		Expected: []string{"2", "1", "3"},
	}, {
		// tasks in lane 1 only
		ChangeTimings: map[string]main.ChangeTimings{
			"1": {Lane: 1, ReadyTime: mkTime("2019-01-21T00:00:00Z")},
			"2": {Lane: 1, ReadyTime: mkTime("2019-01-20T00:00:00Z")},
			"3": {Lane: 1, ReadyTime: mkTime("2019-01-16T00:00:00Z")},
		},
		Expected: []string{"3", "2", "1"},
	}, {
		// tasks in lanes 0, 1, 2 with tasks from line 0 before and after lanes 1, 2
		ChangeTimings: map[string]main.ChangeTimings{
			"1": {Lane: 1, ReadyTime: mkTime("2019-01-21T00:00:00Z")},
			"2": {Lane: 0, ReadyTime: mkTime("2019-01-19T00:00:00Z")},
			"3": {Lane: 2, ReadyTime: mkTime("2019-01-20T00:00:00Z")},
			"4": {Lane: 0, ReadyTime: mkTime("2019-01-25T00:00:00Z")},
			"5": {Lane: 1, ReadyTime: mkTime("2019-01-20T00:00:00Z")},
			"6": {Lane: 2, ReadyTime: mkTime("2019-01-21T00:00:00Z")},
			"7": {Lane: 0, ReadyTime: mkTime("2019-01-18T00:00:00Z")},
			"8": {Lane: 0, ReadyTime: mkTime("2019-01-27T00:00:00Z")},
		},
		Expected: []string{"7", "2", "5", "1", "3", "6", "4", "8"},
	}, {
		// pathological case: lane 0 tasks have ready-time between lane 1 tasks
		ChangeTimings: map[string]main.ChangeTimings{
			"1": {Lane: 1, ReadyTime: mkTime("2019-01-20T00:00:00Z")},
			"2": {Lane: 1, ReadyTime: mkTime("2019-01-30T00:00:00Z")},
			"3": {Lane: 0, ReadyTime: mkTime("2019-01-27T00:00:00Z")},
			"4": {Lane: 0, ReadyTime: mkTime("2019-01-25T00:00:00Z")},
		},
		Expected: []string{"1", "2", "4", "3"},
	}}

	for _, data := range testData {
		tasks := main.SortTimingsTasks(data.ChangeTimings)
		c.Check(tasks, DeepEquals, data.Expected)
	}
}