render_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package render_test
  2. import (
  3. "context"
  4. "reflect"
  5. "testing"
  6. "github.com/weaveworks/common/test"
  7. "github.com/weaveworks/scope/render"
  8. "github.com/weaveworks/scope/report"
  9. )
  10. type mockRenderer struct {
  11. report.Nodes
  12. }
  13. func (m mockRenderer) Render(_ context.Context, rpt report.Report) render.Nodes {
  14. return render.Nodes{Nodes: m.Nodes}
  15. }
  16. func TestReduceRender(t *testing.T) {
  17. renderer := render.Reduce([]render.Renderer{
  18. mockRenderer{Nodes: report.Nodes{"foo": report.MakeNode("foo")}},
  19. mockRenderer{Nodes: report.Nodes{"bar": report.MakeNode("bar")}},
  20. })
  21. want := report.Nodes{
  22. "foo": report.MakeNode("foo"),
  23. "bar": report.MakeNode("bar"),
  24. }
  25. have := renderer.Render(context.Background(), report.MakeReport()).Nodes
  26. if !reflect.DeepEqual(want, have) {
  27. t.Errorf("want %+v, have %+v", want, have)
  28. }
  29. }
  30. func TestMapRender1(t *testing.T) {
  31. // 1. Check when we return false, the node gets filtered out
  32. mapper := render.Map{
  33. MapFunc: func(nodes report.Node) report.Node {
  34. return report.Node{}
  35. },
  36. Renderer: mockRenderer{Nodes: report.Nodes{
  37. "foo": report.MakeNode("foo"),
  38. }},
  39. }
  40. want := report.Nodes{}
  41. have := mapper.Render(context.Background(), report.MakeReport()).Nodes
  42. if !reflect.DeepEqual(want, have) {
  43. t.Errorf("want %+v, have %+v", want, have)
  44. }
  45. }
  46. func TestMapRender2(t *testing.T) {
  47. // 2. Check we can remap two nodes into one
  48. mapper := render.Map{
  49. MapFunc: func(nodes report.Node) report.Node {
  50. return report.MakeNode("bar")
  51. },
  52. Renderer: mockRenderer{Nodes: report.Nodes{
  53. "foo": report.MakeNode("foo"),
  54. "baz": report.MakeNode("baz"),
  55. }},
  56. }
  57. want := report.Nodes{
  58. "bar": report.MakeNode("bar"),
  59. }
  60. have := mapper.Render(context.Background(), report.MakeReport()).Nodes
  61. if !reflect.DeepEqual(want, have) {
  62. t.Error(test.Diff(want, have))
  63. }
  64. }
  65. func TestMapRender3(t *testing.T) {
  66. // 3. Check we can remap adjacencies
  67. mapper := render.Map{
  68. MapFunc: func(nodes report.Node) report.Node {
  69. id := "_" + nodes.ID
  70. return report.MakeNode(id)
  71. },
  72. Renderer: mockRenderer{Nodes: report.Nodes{
  73. "foo": report.MakeNode("foo").WithAdjacent("baz"),
  74. "baz": report.MakeNode("baz").WithAdjacent("foo"),
  75. }},
  76. }
  77. want := report.Nodes{
  78. "_foo": report.MakeNode("_foo").WithAdjacent("_baz"),
  79. "_baz": report.MakeNode("_baz").WithAdjacent("_foo"),
  80. }
  81. have := mapper.Render(context.Background(), report.MakeReport()).Nodes
  82. if !reflect.DeepEqual(want, have) {
  83. t.Error(test.Diff(want, have))
  84. }
  85. }
  86. func newu64(value uint64) *uint64 { return &value }