top_test.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. Copyright 2024 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package compose
  14. import (
  15. "bytes"
  16. "strings"
  17. "testing"
  18. "github.com/docker/compose/v2/pkg/api"
  19. "github.com/stretchr/testify/assert"
  20. "github.com/stretchr/testify/require"
  21. )
  22. var topTestCases = []struct {
  23. name string
  24. titles []string
  25. procs [][]string
  26. header topHeader
  27. entries []topEntries
  28. output string
  29. }{
  30. {
  31. name: "noprocs",
  32. titles: []string{"UID", "PID", "PPID", "C", "STIME", "TTY", "TIME", "CMD"},
  33. procs: [][]string{},
  34. header: topHeader{"SERVICE": 0},
  35. entries: []topEntries{},
  36. output: "",
  37. },
  38. {
  39. name: "simple",
  40. titles: []string{"UID", "PID", "PPID", "C", "STIME", "TTY", "TIME", "CMD"},
  41. procs: [][]string{{"root", "1", "1", "0", "12:00", "?", "00:00:01", "/entrypoint"}},
  42. header: topHeader{
  43. "SERVICE": 0,
  44. "UID": 1,
  45. "PID": 2,
  46. "PPID": 3,
  47. "C": 4,
  48. "STIME": 5,
  49. "TTY": 6,
  50. "TIME": 7,
  51. "CMD": 8,
  52. },
  53. entries: []topEntries{
  54. {
  55. "SERVICE": "simple",
  56. "UID": "root",
  57. "PID": "1",
  58. "PPID": "1",
  59. "C": "0",
  60. "STIME": "12:00",
  61. "TTY": "?",
  62. "TIME": "00:00:01",
  63. "CMD": "/entrypoint",
  64. },
  65. },
  66. output: trim(`
  67. SERVICE UID PID PPID C STIME TTY TIME CMD
  68. simple root 1 1 0 12:00 ? 00:00:01 /entrypoint
  69. `),
  70. },
  71. {
  72. name: "noppid",
  73. titles: []string{"UID", "PID", "C", "STIME", "TTY", "TIME", "CMD"},
  74. procs: [][]string{{"root", "1", "0", "12:00", "?", "00:00:02", "/entrypoint"}},
  75. header: topHeader{
  76. "SERVICE": 0,
  77. "UID": 1,
  78. "PID": 2,
  79. "C": 3,
  80. "STIME": 4,
  81. "TTY": 5,
  82. "TIME": 6,
  83. "CMD": 7,
  84. },
  85. entries: []topEntries{
  86. {
  87. "SERVICE": "noppid",
  88. "UID": "root",
  89. "PID": "1",
  90. "C": "0",
  91. "STIME": "12:00",
  92. "TTY": "?",
  93. "TIME": "00:00:02",
  94. "CMD": "/entrypoint",
  95. },
  96. },
  97. output: trim(`
  98. SERVICE UID PID C STIME TTY TIME CMD
  99. noppid root 1 0 12:00 ? 00:00:02 /entrypoint
  100. `),
  101. },
  102. {
  103. name: "extra-hdr",
  104. titles: []string{"UID", "GID", "PID", "PPID", "C", "STIME", "TTY", "TIME", "CMD"},
  105. procs: [][]string{{"root", "1", "1", "1", "0", "12:00", "?", "00:00:03", "/entrypoint"}},
  106. header: topHeader{
  107. "SERVICE": 0,
  108. "UID": 1,
  109. "GID": 2,
  110. "PID": 3,
  111. "PPID": 4,
  112. "C": 5,
  113. "STIME": 6,
  114. "TTY": 7,
  115. "TIME": 8,
  116. "CMD": 9,
  117. },
  118. entries: []topEntries{
  119. {
  120. "SERVICE": "extra-hdr",
  121. "UID": "root",
  122. "GID": "1",
  123. "PID": "1",
  124. "PPID": "1",
  125. "C": "0",
  126. "STIME": "12:00",
  127. "TTY": "?",
  128. "TIME": "00:00:03",
  129. "CMD": "/entrypoint",
  130. },
  131. },
  132. output: trim(`
  133. SERVICE UID GID PID PPID C STIME TTY TIME CMD
  134. extra-hdr root 1 1 1 0 12:00 ? 00:00:03 /entrypoint
  135. `),
  136. },
  137. {
  138. name: "multiple",
  139. titles: []string{"UID", "PID", "PPID", "C", "STIME", "TTY", "TIME", "CMD"},
  140. procs: [][]string{
  141. {"root", "1", "1", "0", "12:00", "?", "00:00:04", "/entrypoint"},
  142. {"root", "123", "1", "0", "12:00", "?", "00:00:42", "sleep infinity"},
  143. },
  144. header: topHeader{
  145. "SERVICE": 0,
  146. "UID": 1,
  147. "PID": 2,
  148. "PPID": 3,
  149. "C": 4,
  150. "STIME": 5,
  151. "TTY": 6,
  152. "TIME": 7,
  153. "CMD": 8,
  154. },
  155. entries: []topEntries{
  156. {
  157. "SERVICE": "multiple",
  158. "UID": "root",
  159. "PID": "1",
  160. "PPID": "1",
  161. "C": "0",
  162. "STIME": "12:00",
  163. "TTY": "?",
  164. "TIME": "00:00:04",
  165. "CMD": "/entrypoint",
  166. },
  167. {
  168. "SERVICE": "multiple",
  169. "UID": "root",
  170. "PID": "123",
  171. "PPID": "1",
  172. "C": "0",
  173. "STIME": "12:00",
  174. "TTY": "?",
  175. "TIME": "00:00:42",
  176. "CMD": "sleep infinity",
  177. },
  178. },
  179. output: trim(`
  180. SERVICE UID PID PPID C STIME TTY TIME CMD
  181. multiple root 1 1 0 12:00 ? 00:00:04 /entrypoint
  182. multiple root 123 1 0 12:00 ? 00:00:42 sleep infinity
  183. `),
  184. },
  185. }
  186. // TestRunTopCore only tests the core functionality of runTop: formatting
  187. // and printing of the output of (api.Service).Top().
  188. func TestRunTopCore(t *testing.T) {
  189. t.Parallel()
  190. all := []api.ContainerProcSummary{}
  191. for _, tc := range topTestCases {
  192. summary := api.ContainerProcSummary{
  193. Name: tc.name,
  194. Titles: tc.titles,
  195. Processes: tc.procs,
  196. }
  197. all = append(all, summary)
  198. t.Run(tc.name, func(t *testing.T) {
  199. header, entries := collectTop([]api.ContainerProcSummary{summary})
  200. assert.EqualValues(t, tc.header, header)
  201. assert.EqualValues(t, tc.entries, entries)
  202. var buf bytes.Buffer
  203. err := topPrint(&buf, header, entries)
  204. require.NoError(t, err)
  205. assert.Equal(t, tc.output, buf.String())
  206. })
  207. }
  208. t.Run("all", func(t *testing.T) {
  209. header, entries := collectTop(all)
  210. assert.EqualValues(t, topHeader{
  211. "SERVICE": 0,
  212. "UID": 1,
  213. "PID": 2,
  214. "PPID": 3,
  215. "C": 4,
  216. "STIME": 5,
  217. "TTY": 6,
  218. "TIME": 7,
  219. "CMD": 8,
  220. "GID": 9,
  221. }, header)
  222. assert.EqualValues(t, []topEntries{
  223. {
  224. "SERVICE": "simple",
  225. "UID": "root",
  226. "PID": "1",
  227. "PPID": "1",
  228. "C": "0",
  229. "STIME": "12:00",
  230. "TTY": "?",
  231. "TIME": "00:00:01",
  232. "CMD": "/entrypoint",
  233. }, {
  234. "SERVICE": "noppid",
  235. "UID": "root",
  236. "PID": "1",
  237. "C": "0",
  238. "STIME": "12:00",
  239. "TTY": "?",
  240. "TIME": "00:00:02",
  241. "CMD": "/entrypoint",
  242. }, {
  243. "SERVICE": "extra-hdr",
  244. "UID": "root",
  245. "GID": "1",
  246. "PID": "1",
  247. "PPID": "1",
  248. "C": "0",
  249. "STIME": "12:00",
  250. "TTY": "?",
  251. "TIME": "00:00:03",
  252. "CMD": "/entrypoint",
  253. }, {
  254. "SERVICE": "multiple",
  255. "UID": "root",
  256. "PID": "1",
  257. "PPID": "1",
  258. "C": "0",
  259. "STIME": "12:00",
  260. "TTY": "?",
  261. "TIME": "00:00:04",
  262. "CMD": "/entrypoint",
  263. }, {
  264. "SERVICE": "multiple",
  265. "UID": "root",
  266. "PID": "123",
  267. "PPID": "1",
  268. "C": "0",
  269. "STIME": "12:00",
  270. "TTY": "?",
  271. "TIME": "00:00:42",
  272. "CMD": "sleep infinity",
  273. },
  274. }, entries)
  275. var buf bytes.Buffer
  276. err := topPrint(&buf, header, entries)
  277. require.NoError(t, err)
  278. assert.Equal(t, trim(`
  279. SERVICE UID PID PPID C STIME TTY TIME CMD GID
  280. simple root 1 1 0 12:00 ? 00:00:01 /entrypoint -
  281. noppid root 1 - 0 12:00 ? 00:00:02 /entrypoint -
  282. extra-hdr root 1 1 0 12:00 ? 00:00:03 /entrypoint 1
  283. multiple root 1 1 0 12:00 ? 00:00:04 /entrypoint -
  284. multiple root 123 1 0 12:00 ? 00:00:42 sleep infinity -
  285. `), buf.String())
  286. })
  287. }
  288. func trim(s string) string {
  289. var out bytes.Buffer
  290. for _, line := range strings.Split(strings.TrimSpace(s), "\n") {
  291. out.WriteString(strings.TrimSpace(line))
  292. out.WriteRune('\n')
  293. }
  294. return out.String()
  295. }