censor_test.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. package detailed_test
  2. import (
  3. "reflect"
  4. "testing"
  5. "github.com/weaveworks/common/test"
  6. "github.com/weaveworks/scope/render/detailed"
  7. "github.com/weaveworks/scope/report"
  8. )
  9. func TestCensorNode(t *testing.T) {
  10. node := detailed.Node{
  11. NodeSummary: detailed.NodeSummary{
  12. Metadata: []report.MetadataRow{
  13. {ID: "cmdline", Label: "Command", Value: "prog -a --b=c"},
  14. },
  15. Tables: []report.Table{
  16. {ID: "blibli", Rows: []report.Row{{ID: "bli"}}},
  17. {ID: "docker_env_", Rows: []report.Row{{ID: "env_var"}}},
  18. },
  19. },
  20. }
  21. for _, c := range []struct {
  22. label string
  23. have, want detailed.Node
  24. }{
  25. {
  26. label: "no censoring",
  27. have: detailed.CensorNode(node, report.CensorConfig{
  28. HideCommandLineArguments: false,
  29. HideEnvironmentVariables: false,
  30. }),
  31. want: detailed.Node{
  32. NodeSummary: detailed.NodeSummary{
  33. Metadata: []report.MetadataRow{
  34. {ID: "cmdline", Label: "Command", Value: "prog -a --b=c"},
  35. },
  36. Tables: []report.Table{
  37. {ID: "blibli", Rows: []report.Row{{ID: "bli"}}},
  38. {ID: "docker_env_", Rows: []report.Row{{ID: "env_var"}}},
  39. },
  40. },
  41. },
  42. },
  43. {
  44. label: "censor only command line args",
  45. have: detailed.CensorNode(node, report.CensorConfig{
  46. HideCommandLineArguments: true,
  47. HideEnvironmentVariables: false,
  48. }),
  49. want: detailed.Node{
  50. NodeSummary: detailed.NodeSummary{
  51. Metadata: []report.MetadataRow{
  52. {ID: "cmdline", Label: "Command", Value: "prog"},
  53. },
  54. Tables: []report.Table{
  55. {ID: "blibli", Rows: []report.Row{{ID: "bli"}}},
  56. {ID: "docker_env_", Rows: []report.Row{{ID: "env_var"}}},
  57. },
  58. },
  59. },
  60. },
  61. {
  62. label: "censor only env variables",
  63. have: detailed.CensorNode(node, report.CensorConfig{
  64. HideCommandLineArguments: false,
  65. HideEnvironmentVariables: true,
  66. }),
  67. want: detailed.Node{
  68. NodeSummary: detailed.NodeSummary{
  69. Metadata: []report.MetadataRow{
  70. {ID: "cmdline", Label: "Command", Value: "prog -a --b=c"},
  71. },
  72. Tables: []report.Table{
  73. {ID: "blibli", Rows: []report.Row{{ID: "bli"}}},
  74. },
  75. },
  76. },
  77. },
  78. {
  79. label: "censor both command line args and env vars",
  80. have: detailed.CensorNode(node, report.CensorConfig{
  81. HideCommandLineArguments: true,
  82. HideEnvironmentVariables: true,
  83. }),
  84. want: detailed.Node{
  85. NodeSummary: detailed.NodeSummary{
  86. Metadata: []report.MetadataRow{
  87. {ID: "cmdline", Label: "Command", Value: "prog"},
  88. },
  89. Tables: []report.Table{
  90. {ID: "blibli", Rows: []report.Row{{ID: "bli"}}},
  91. },
  92. },
  93. },
  94. },
  95. } {
  96. if !reflect.DeepEqual(c.want, c.have) {
  97. t.Errorf("%s - %s", c.label, test.Diff(c.want, c.have))
  98. }
  99. }
  100. }
  101. func TestCensorNodeSummaries(t *testing.T) {
  102. summaries := detailed.NodeSummaries{
  103. "a": detailed.NodeSummary{
  104. Metadata: []report.MetadataRow{
  105. {ID: "blublu", Label: "blabla", Value: "blu blu"},
  106. {ID: "docker_container_command", Label: "Command", Value: "scope --token=blibli"},
  107. },
  108. },
  109. "b": detailed.NodeSummary{
  110. Metadata: []report.MetadataRow{
  111. {ID: "cmdline", Label: "Command", Value: "prog -a --b=c"},
  112. },
  113. Tables: []report.Table{
  114. {ID: "blibli", Rows: []report.Row{{ID: "bli"}}},
  115. {ID: "docker_env_", Rows: []report.Row{{ID: "env_var"}}},
  116. },
  117. },
  118. }
  119. for _, c := range []struct {
  120. label string
  121. have, want detailed.NodeSummaries
  122. }{
  123. {
  124. label: "no censoring",
  125. have: detailed.CensorNodeSummaries(summaries, report.CensorConfig{
  126. HideCommandLineArguments: false,
  127. HideEnvironmentVariables: false,
  128. }),
  129. want: detailed.NodeSummaries{
  130. "a": detailed.NodeSummary{
  131. Metadata: []report.MetadataRow{
  132. {ID: "blublu", Label: "blabla", Value: "blu blu"},
  133. {ID: "docker_container_command", Label: "Command", Value: "scope --token=blibli"},
  134. },
  135. },
  136. "b": detailed.NodeSummary{
  137. Metadata: []report.MetadataRow{
  138. {ID: "cmdline", Label: "Command", Value: "prog -a --b=c"},
  139. },
  140. Tables: []report.Table{
  141. {ID: "blibli", Rows: []report.Row{{ID: "bli"}}},
  142. {ID: "docker_env_", Rows: []report.Row{{ID: "env_var"}}},
  143. },
  144. },
  145. },
  146. },
  147. {
  148. label: "censor only command line args",
  149. have: detailed.CensorNodeSummaries(summaries, report.CensorConfig{
  150. HideCommandLineArguments: true,
  151. HideEnvironmentVariables: false,
  152. }),
  153. want: detailed.NodeSummaries{
  154. "a": detailed.NodeSummary{
  155. Metadata: []report.MetadataRow{
  156. {ID: "blublu", Label: "blabla", Value: "blu blu"},
  157. {ID: "docker_container_command", Label: "Command", Value: "scope"},
  158. },
  159. },
  160. "b": detailed.NodeSummary{
  161. Metadata: []report.MetadataRow{
  162. {ID: "cmdline", Label: "Command", Value: "prog"},
  163. },
  164. Tables: []report.Table{
  165. {ID: "blibli", Rows: []report.Row{{ID: "bli"}}},
  166. {ID: "docker_env_", Rows: []report.Row{{ID: "env_var"}}},
  167. },
  168. },
  169. },
  170. },
  171. {
  172. label: "censor only env variables",
  173. have: detailed.CensorNodeSummaries(summaries, report.CensorConfig{
  174. HideCommandLineArguments: false,
  175. HideEnvironmentVariables: true,
  176. }),
  177. want: detailed.NodeSummaries{
  178. "a": detailed.NodeSummary{
  179. Metadata: []report.MetadataRow{
  180. {ID: "blublu", Label: "blabla", Value: "blu blu"},
  181. {ID: "docker_container_command", Label: "Command", Value: "scope --token=blibli"},
  182. },
  183. },
  184. "b": detailed.NodeSummary{
  185. Metadata: []report.MetadataRow{
  186. {ID: "cmdline", Label: "Command", Value: "prog -a --b=c"},
  187. },
  188. Tables: []report.Table{
  189. {ID: "blibli", Rows: []report.Row{{ID: "bli"}}},
  190. },
  191. },
  192. },
  193. },
  194. {
  195. label: "censor both command line args and env vars",
  196. have: detailed.CensorNodeSummaries(summaries, report.CensorConfig{
  197. HideCommandLineArguments: true,
  198. HideEnvironmentVariables: true,
  199. }),
  200. want: detailed.NodeSummaries{
  201. "a": detailed.NodeSummary{
  202. Metadata: []report.MetadataRow{
  203. {ID: "blublu", Label: "blabla", Value: "blu blu"},
  204. {ID: "docker_container_command", Label: "Command", Value: "scope"},
  205. },
  206. },
  207. "b": detailed.NodeSummary{
  208. Metadata: []report.MetadataRow{
  209. {ID: "cmdline", Label: "Command", Value: "prog"},
  210. },
  211. Tables: []report.Table{
  212. {ID: "blibli", Rows: []report.Row{{ID: "bli"}}},
  213. },
  214. },
  215. },
  216. },
  217. } {
  218. if !reflect.DeepEqual(c.want, c.have) {
  219. t.Errorf("%s - %s", c.label, test.Diff(c.want, c.have))
  220. }
  221. }
  222. }