test_spec.lua 14.8 KB
Newer Older
Hisham Muhammad's avatar
Hisham Muhammad committed
1
2
#!/usr/bin/env lua

3
local VISUALDELAY = os.getenv("VISUALDELAY")
Hisham Muhammad's avatar
Hisham Muhammad committed
4
 
5
6
local visual = VISUALDELAY or false
local visual_delay = VISUALDELAY and (tonumber(VISUALDELAY)) or 0.1
Hisham Muhammad's avatar
Hisham Muhammad committed
7
8
9
10
11
12
13
14

local unistd = require("posix.unistd")
local time = require("posix.time")
local curses = require("posix.curses")
local rote = require("rote")

local rt = rote.RoteTerm(24, 80)

15
16
17
18
19
20
21
22
23
24
local function os_execread(cmd)
   local fd = io.popen(cmd, "r")
   local out = fd:read("*a")
   fd:close()
   return (out:gsub("\n$", ""))
end
local branch = os_execread("git branch | grep '*'"):sub(3)

print("Running in branch "..branch)

Hisham Muhammad's avatar
Hisham Muhammad committed
25
26
27
28
os.execute("make coverage")
os.execute("rm -f *.gcda */*.gcda")
os.execute("rm -f coverage.info test.htoprc")
os.execute("rm -rf lcov")
Hisham Muhammad's avatar
Hisham Muhammad committed
29
30
os.execute("killall htop")
os.execute("ps aux | grep '[s]leep 12345' | awk '{print $2}' | xargs kill 2> /dev/null")
Hisham Muhammad's avatar
Hisham Muhammad committed
31

Hisham Muhammad's avatar
Hisham Muhammad committed
32
os.execute("cp ./default.htoprc ./test.htoprc")
Hisham Muhammad's avatar
Hisham Muhammad committed
33
rt:forkPty("LC_ALL=C HTOPRC=./test.htoprc ./htop")
Hisham Muhammad's avatar
Hisham Muhammad committed
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

local stdscr, term_win
-- Curses initalization needed even when not in visual mode
-- because luaposix only initializes KEY_* constants after initscr().
stdscr = curses.initscr()
if visual then
   curses.echo(false)
   curses.start_color()
   curses.raw(true)
   curses.halfdelay(1)
   stdscr:keypad(true)
   term_win = curses.newwin(24, 80, 0, 0)
   local function makePair(foreground, background)
      return background * 8 + 7 - foreground
   end
   -- initialize the color pairs the way rt:draw() expects it
   for foreground = 0, 7 do
      for background = 0, 7 do
         if foreground ~= 7 or background ~= 0 then
            local pair = makePair(foreground, background)
            curses.init_pair(pair, foreground, background)
         end
      end
   end
else
   curses.endwin()
end

local function delay(t)
   time.nanosleep({ tv_sec = math.floor(t), tv_nsec = (t - math.floor(t)) * 1000000000 })
end

local function show(key)
   rt:update()
   if visual then
      rt:draw(term_win, 0, 0)
      if key then
         term_win:mvaddstr(0, 0, tostring(key))
      end
      term_win:refresh()
      
      delay(visual_delay)
   end
end

Hisham Muhammad's avatar
Hisham Muhammad committed
79
local function send(key, times, quick)
Hisham Muhammad's avatar
Hisham Muhammad committed
80
   if times == 0 then return end
Hisham Muhammad's avatar
Hisham Muhammad committed
81
   for _ = 1, times or 1 do
Hisham Muhammad's avatar
Hisham Muhammad committed
82
83
84
85
86
87
88
89
      delay(0.003) -- 30ms delay to avoid clobbering Esc sequences
      if type(key) == "string" then
         for c in key:gmatch('.') do
            rt:keyPress(string.byte(c))
         end
      else
         rt:keyPress(key)
      end
Hisham Muhammad's avatar
Hisham Muhammad committed
90
91
92
93
94
      if not quick then
         show(key)
      end
   end
   if quick then
Hisham Muhammad's avatar
Hisham Muhammad committed
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
      show(key)
   end
end

local function string_at(x, y, len)
   rt:update()
   local out = {}
   for i = 1, len do
      out[#out+1] = rt:cellChar(y-1, x+i-2)
   end
   return table.concat(out)
end

local function is_string_at(x, y, str)
   return string_at(x, y, #str) == str
end

local function check_string_at(x, y, str)
   return { str, string_at(x, y, #str) }
end

Hisham Muhammad's avatar
Hisham Muhammad committed
116
local ESC = "\27\27"
Hisham Muhammad's avatar
Hisham Muhammad committed
117
118
119

time.nanosleep({ tv_sec = 0, tv_nsec = 150000000 }) -- give some time for htop to initialize.

Hisham Muhammad's avatar
Hisham Muhammad committed
120
121
122
123
local y_panelhdr = (function()
   for y = 1, 24 do
      if is_string_at(3, y, "PID") then
         return y
Hisham Muhammad's avatar
Hisham Muhammad committed
124
125
126
127
      end
   end
end)() or 1

128
local x_metercol2 = (branch == "wip") and 41 or 43
Hisham Muhammad's avatar
Hisham Muhammad committed
129

Hisham Muhammad's avatar
Hisham Muhammad committed
130
131
show()

Hisham Muhammad's avatar
Hisham Muhammad committed
132
133
134
135
136
os.execute("sleep 12345 &")

local function terminated()
   return not os.execute("ps aux | grep -q '\\./[h]top'")
end
Hisham Muhammad's avatar
Hisham Muhammad committed
137
138
139

local function running_it(desc, fn)
   it(desc, function()
Hisham Muhammad's avatar
Hisham Muhammad committed
140
      assert(not terminated())
Hisham Muhammad's avatar
Hisham Muhammad committed
141
142
      show()
      fn()
Hisham Muhammad's avatar
Hisham Muhammad committed
143
      assert(not terminated())
Hisham Muhammad's avatar
Hisham Muhammad committed
144
145
146
147
148
149
150
   end)
end

local function check(t)
   return t[1], t[2]
end

Hisham Muhammad's avatar
Hisham Muhammad committed
151
152
153
local attrs = {
   black_on_cyan = 6,
   red_on_cyan = 22,
Hisham Muhammad's avatar
Hisham Muhammad committed
154
155
   white_on_black = 176,
   yellow_on_black = 112,
Hisham Muhammad's avatar
Hisham Muhammad committed
156
157
}

Hisham Muhammad's avatar
Hisham Muhammad committed
158
159
160
local function find_selected_y(from)
   rt:update()
   for y = from or (y_panelhdr + 1), rt:rows() - 1 do
Hisham Muhammad's avatar
Hisham Muhammad committed
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
      local attr = rt:cellAttr(y-1, 1)
      if attr == attrs.black_on_cyan then
         return y
      end
   end
   return y_panelhdr + 1
end

local function find_command_x()
   for x = 1, 80 do
      if is_string_at(x, y_panelhdr, "Command") then
         return x
      end
   end
   return 64
end

Hisham Muhammad's avatar
Hisham Muhammad committed
178
179
180
181
182
183
184
185
186
local function set_display_option(n)
   send("S")
   send(curses.KEY_DOWN)
   send(curses.KEY_RIGHT)
   send(curses.KEY_DOWN, n)
   send("\n")
   send(curses.KEY_F10)
end

Hisham Muhammad's avatar
Hisham Muhammad committed
187
describe("htop test suite", function()
Hisham Muhammad's avatar
Hisham Muhammad committed
188
189
190
   
   running_it("performs incremental filter", function()
      send("\\")
Hisham Muhammad's avatar
Hisham Muhammad committed
191
      send("x\127bux\127sted") -- test backspace
Hisham Muhammad's avatar
Hisham Muhammad committed
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
      send("\n")
      delay(0.2)
      rt:update()
      local pid = ("      "..tostring(unistd.getpid())):sub(-5)
      local ourpid = check_string_at(1, y_panelhdr + 1, pid)
      send("\\")
      send(ESC)
      send(curses.KEY_F5)
      send(curses.KEY_HOME)
      delay(0.15)
      rt:update()
      local initpid = check_string_at(1, y_panelhdr + 1, "    1")
      delay(0.15)
      rt:update()
      send(curses.KEY_F5)
      assert.equal(check(ourpid))
      assert.equal(check(initpid))
   end)

   running_it("performs incremental search", function()
      send(curses.KEY_HOME)
      send("/")
      send("busted")
      local attr = rt:cellAttr(rt:rows() - 1, 30)
Hisham Muhammad's avatar
Hisham Muhammad committed
216
      delay(0.3)
Hisham Muhammad's avatar
Hisham Muhammad committed
217
218
219
220
      local line = find_selected_y()
      local pid = ("      "..tostring(unistd.getpid())):sub(-5)
      assert.equal(attr, attrs.black_on_cyan)
      local ourpid = check_string_at(1, line, pid)
Hisham Muhammad's avatar
Hisham Muhammad committed
221
      send("\n")
Hisham Muhammad's avatar
Hisham Muhammad committed
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
      send(curses.KEY_HOME)
      assert.equal(check(ourpid))
   end)

   running_it("kills a process", function()
      send(curses.KEY_HOME)
      send("\\")
      send("sleep 12345")
      local attr = rt:cellAttr(rt:rows() - 1, 30)
      assert.equal(attr, attrs.black_on_cyan)
      send("\n")
      delay(0.3)
      rt:update()
      local col = find_command_x()
      local procname = check_string_at(col, y_panelhdr + 1, "sleep 12345")
      send("k")
      send("\n")
      send("\\")
      send(ESC)
      delay(0.3)
      assert.equal(check(procname))
      assert.not_equal((os.execute("ps aux | grep -q '[s]leep 12345'")), true)
   end)

   running_it("runs strace", function()
      send(curses.KEY_HOME)
      send("/")
      send("busted")
      send("\n")
      send("s")
      delay(1)
      send(ESC)
   end)

   running_it("runs lsof", function()
      send(curses.KEY_HOME)
      send("/")
      send("busted")
      send("\n")
      send("l")
      delay(1)
Hisham Muhammad's avatar
Hisham Muhammad committed
263
264
265
266
267
268
269
270
271
272
273
      send(ESC)
   end)

   running_it("performs filtering in lsof", function()
      send(curses.KEY_HOME)
      send("/")
      send("htop")
      send("\n")
      send("l")
      send(curses.KEY_F4)
      send("pipe")
Hisham Muhammad's avatar
Hisham Muhammad committed
274
      delay(1)
Hisham Muhammad's avatar
Hisham Muhammad committed
275
      local pipefd = check_string_at(1, 3, "    3")
Hisham Muhammad's avatar
Hisham Muhammad committed
276
      send(ESC)
Hisham Muhammad's avatar
Hisham Muhammad committed
277
      assert.equal(check(pipefd))
Hisham Muhammad's avatar
Hisham Muhammad committed
278
279
   end)

Hisham Muhammad's avatar
Hisham Muhammad committed
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
   running_it("performs search in lsof", function()
      send(curses.KEY_HOME)
      send("/")
      send("htop")
      send("\n")
      send("l")
      send(curses.KEY_F3)
      send("pipe")
      delay(1)
      local line = find_selected_y(3)
      local pipefd = check_string_at(1, line, "    3")
      send(ESC)
      assert.equal(check(pipefd))
   end)


   running_it("cycles through meter modes in the default meters", function()
Hisham Muhammad's avatar
Hisham Muhammad committed
297
      send("S")
Hisham Muhammad's avatar
Hisham Muhammad committed
298
299
300
301
302
303
304
      for _ = 1, 2 do
         send(curses.KEY_RIGHT)
         for _ = 1, 3 do
            send("\n", 4)
            send(curses.KEY_DOWN)
         end
      end
Hisham Muhammad's avatar
Hisham Muhammad committed
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
      send(ESC)
   end)

   running_it("show process of a user", function()
      send(curses.KEY_F5)
      send("u")
      send(curses.KEY_DOWN)
      delay(0.3)
      rt:update()
      local chosen = string_at(1, y_panelhdr + 2, 9)
      send("\n")
      send(curses.KEY_HOME)
      delay(0.3)
      rt:update()
      local shown = string_at(7, y_panelhdr + 1, 9)
      send("u")
      send("\n")
      send(curses.KEY_HOME)
      delay(0.3)
      rt:update()
      local inituser = string_at(7, y_panelhdr + 1, 9)
      send(curses.KEY_F5)
      assert.equal(shown, chosen)
      assert.equal(inituser, "root     ")
   end)

   running_it("performs failing search", function()
      send(curses.KEY_HOME)
      send("/")
      send("xxxxxxxxxx")
      delay(0.3)
      rt:update()
      local attr = rt:cellAttr(rt:rows() - 1, 30)
      assert.equal(attr, attrs.red_on_cyan)
      send("\n")
   end)

   running_it("cycles through search", function()
      send(curses.KEY_HOME)
      send("/")
      send("sh")
      local lastpid
      local pidpairs = {}
      for _ = 1, 3 do
         send(curses.KEY_F3)
         local line = find_selected_y()
         local pid = string_at(1, line, 5)
         if lastpid then
            pidpairs[#pidpairs + 1] = { lastpid, pid }
            lastpid = pid
         end
      end
      send(curses.KEY_HOME)
      for _, pair in pairs(pidpairs) do
         assert.not_equal(pair[1], pair[2])
      end
   end)
   
Hisham Muhammad's avatar
Hisham Muhammad committed
363
364
365
366
367
   running_it("visits each setup screen", function()
      send("S")
      send(curses.KEY_DOWN, 3)
      send(curses.KEY_F10)
   end)
Hisham Muhammad's avatar
Hisham Muhammad committed
368
   
369
370
371
372
373
374
375
376
   running_it("adds and removes PPID column", function()
      send("S")
      send(curses.KEY_DOWN, 3)
      send(curses.KEY_RIGHT, 2)
      send(curses.KEY_DOWN, 2)
      send("\n")
      send(curses.KEY_F10)
      delay(0.2)
Hisham Muhammad's avatar
Hisham Muhammad committed
377
      local ppid = check_string_at(2, y_panelhdr, "PPID")
378
379
380
381
382
383
      send("S")
      send(curses.KEY_DOWN, 3)
      send(curses.KEY_RIGHT, 1)
      send(curses.KEY_DC)
      send(curses.KEY_F10)
      delay(0.2)
Hisham Muhammad's avatar
Hisham Muhammad committed
384
      local not_ppid = check_string_at(2, y_panelhdr, "PPID")
385
386
387
      assert.equal(check(ppid))
      assert.not_equal(check(not_ppid))
   end)
Hisham Muhammad's avatar
Hisham Muhammad committed
388
   
Hisham Muhammad's avatar
Hisham Muhammad committed
389
390
391
392
393
   running_it("changes CPU affinity for a process", function()
      send("a")
      send(" \n")
      send(ESC)
   end)
Hisham Muhammad's avatar
Hisham Muhammad committed
394
395
396
397
398
399
400
401
402
403
404
405
406

   running_it("renices for a process", function()
      send("/")
      send("busted")
      send("\n")
      local line = find_selected_y()
      local before = check_string_at(22, line, " 0")
      send(curses.KEY_F8)
      delay(0.3)
      local after = check_string_at(22, line, " 1")
      assert.equal(check(before))
      assert.equal(check(after))
   end)
Hisham Muhammad's avatar
Hisham Muhammad committed
407
408
409
410
   
   running_it("changes IO priority for a process", function()
      send("/")
      send("htop")
Hisham Muhammad's avatar
Hisham Muhammad committed
411
      send("\n")
Hisham Muhammad's avatar
Hisham Muhammad committed
412
413
      send("i")
      send(curses.KEY_END)
Hisham Muhammad's avatar
Hisham Muhammad committed
414
415
416
      send("\n")
      send(ESC)
   end)
Hisham Muhammad's avatar
Hisham Muhammad committed
417
418
419
420
421
422
423
424
425

   running_it("shows help", function()
      send(curses.KEY_F1)
      send("\n")
      set_display_option(9)
      send(curses.KEY_F1)
      send("\n")
      set_display_option(9)
   end)
Hisham Muhammad's avatar
Hisham Muhammad committed
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
   
   local meters = {
      { name = "clock", down = 0, string = "Time" },
      { name = "load", down = 2, string = "Load" },
      { name = "battery", down = 7, string = "Battery" },
      { name = "hostname", down = 8, string = "Hostname" },
   }
   
   for _, item in ipairs(meters) do
      running_it("adds and removes a "..item.name.." widget", function()
         send("S")
         send(curses.KEY_RIGHT, 3)
         send(curses.KEY_DOWN, item.down)
         if branch == "wip" then
            send("\n")
            send(curses.KEY_UP, 4)
            send("\n")
         else
            send(curses.KEY_F6)
            send(curses.KEY_LEFT)
            send(curses.KEY_DOWN, 4)
            send(curses.KEY_F7, 4)
         end
Hisham Muhammad's avatar
Hisham Muhammad committed
449
         send(curses.KEY_F4, 4) -- cycle through meter modes
Hisham Muhammad's avatar
Hisham Muhammad committed
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
         delay(0.15)
         rt:update()
         local with = check_string_at(x_metercol2, 2, item.string)
         send(curses.KEY_DC)
         delay(0.15)
         local without = check_string_at(x_metercol2, 2, item.string)
         send(curses.KEY_F10)
         assert.equal(check(with))
         assert.not_equal(check(without))
      end)
   end

   running_it("goes through themes", function()
      send(curses.KEY_F2)
      send(curses.KEY_DOWN, 2)
      send(curses.KEY_RIGHT)
Hisham Muhammad's avatar
Hisham Muhammad committed
466
      for _ = 1, 6 do
Hisham Muhammad's avatar
Hisham Muhammad committed
467
468
469
470
         send("\n")
         send(curses.KEY_DOWN)
      end
      send(curses.KEY_UP, 6)
Hisham Muhammad's avatar
Hisham Muhammad committed
471
      send("\n")
Hisham Muhammad's avatar
Hisham Muhammad committed
472
      send(curses.KEY_F10)
Hisham Muhammad's avatar
Hisham Muhammad committed
473
   end)
Hisham Muhammad's avatar
Hisham Muhammad committed
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
   
   local display_options = {
      { name = "tree view", down = 0 },
      { name = "shadow other user's process", down = 1 },
      { name = "hide kernel threads", down = 2 },
      { name = "hide userland threads", down = 3 },
      { name = "display threads in different color", down = 4 },
      { name = "show custom thread names", down = 5 },
      { name = "highlight basename", down = 6 },
      { name = "highlight large numbers", down = 7 },
      { name = "leave margin around header", down = 8 },
      { name = "use detailed CPU time", down = 9 },
      { name = "count from zero", down = 10 },
      { name = "update process names", down = 11 },
      { name = "guest time in CPU%", down = 12 },
   }
   
   for _, item in ipairs(display_options) do
      running_it("checks display option to "..item.name, function()
         for _ = 1, 2 do
Hisham Muhammad's avatar
Hisham Muhammad committed
494
            set_display_option(item.down)
Hisham Muhammad's avatar
Hisham Muhammad committed
495
496
497
498
            delay(0.1)
         end
      end)
   end
Hisham Muhammad's avatar
Hisham Muhammad committed
499
500
501
502
503
504
505
506
507
508
509
510
511
512

   running_it("shows detailed CPU with guest time", function()
      for _ = 1, 2 do
         send("S")
         send(curses.KEY_DOWN)
         send(curses.KEY_RIGHT)
         send(curses.KEY_DOWN, 9)
         send("\n")
         send(curses.KEY_DOWN, 3)
         send("\n")
         send(curses.KEY_F10)
         delay(0.1)
      end
   end)
Hisham Muhammad's avatar
Hisham Muhammad committed
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552

   running_it("expands and collapses tree", function()
      send(curses.KEY_F5) -- tree view
      send(curses.KEY_HOME)
      send(curses.KEY_DOWN) -- second process in the tree
      send("-")
      send("+")
      send(curses.KEY_F5)
   end)

   running_it("sets sort key", function()
      send(".")
      send("\n")
   end)

   running_it("tags all children", function()
      send(curses.KEY_F5) -- tree view
      send(curses.KEY_HOME) -- ensure we're at init
      send("c")
      local taggedattrs = {}
      rt:update()
      for y = y_panelhdr + 2, 23 do
         table.insert(taggedattrs, rt:cellAttr(y-1, 4))
      end
      delay(0.2)
      send("U")
      local untaggedattrs = {}
      rt:update()
      for y = y_panelhdr + 2, 23 do
         table.insert(untaggedattrs, rt:cellAttr(y-1, 4))
      end
      send(curses.KEY_F5)

      for _, taggedattr in ipairs(taggedattrs) do
         assert.equal(attrs.yellow_on_black, taggedattr)
      end
      for _, untaggedattr in ipairs(untaggedattrs) do
         assert.equal(attrs.white_on_black, untaggedattr)
      end
   end)
Hisham Muhammad's avatar
Hisham Muhammad committed
553
   
Hisham Muhammad's avatar
Hisham Muhammad committed
554
   for i = 1, 62 do
Hisham Muhammad's avatar
Hisham Muhammad committed
555
556
557
      running_it("show column "..i, function()
         send("S")
         send(curses.KEY_END)
Hisham Muhammad's avatar
Hisham Muhammad committed
558
559
560
561
562
563
564
565
566
567
568
         send(curses.KEY_RIGHT, 1)
         if i > 1 then
            send(curses.KEY_DC)
         end
         send(curses.KEY_RIGHT, 1)
         local down = i
         while down > 13 do
            send(curses.KEY_NPAGE)
            down = down - 13
         end
         send(curses.KEY_DOWN, down, "quick")
Hisham Muhammad's avatar
Hisham Muhammad committed
569
570
         send("\n")
         send(curses.KEY_F10)
Hisham Muhammad's avatar
Hisham Muhammad committed
571
572
573
574
575
576
577
578
579
         if i == 62 then
            send("S")
            send(curses.KEY_END)
            send(curses.KEY_RIGHT, 1)
            if i > 1 then
               send(curses.KEY_DC)
            end
            send(curses.KEY_F10)
         end
Hisham Muhammad's avatar
Hisham Muhammad committed
580
581
582
      end)
   end
   
Hisham Muhammad's avatar
Hisham Muhammad committed
583
   it("finally quits", function()
Hisham Muhammad's avatar
Hisham Muhammad committed
584
      assert(not terminated())
Hisham Muhammad's avatar
Hisham Muhammad committed
585
      send("q")
Hisham Muhammad's avatar
Hisham Muhammad committed
586
      while not terminated() do
Hisham Muhammad's avatar
Hisham Muhammad committed
587
588
         unistd.sleep(1)
      end
Hisham Muhammad's avatar
Hisham Muhammad committed
589
      assert(terminated())
Hisham Muhammad's avatar
Hisham Muhammad committed
590
591
592
593
594
595
596
      if visual then
         curses.endwin()
      end
      os.execute("make lcov && xdg-open lcov/index.html")
   end)
end)