container_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package render_test
  2. import (
  3. "context"
  4. "fmt"
  5. "testing"
  6. "github.com/weaveworks/common/test"
  7. "github.com/weaveworks/scope/render"
  8. "github.com/weaveworks/scope/render/expected"
  9. "github.com/weaveworks/scope/report"
  10. "github.com/weaveworks/scope/test/fixture"
  11. "github.com/weaveworks/scope/test/reflect"
  12. "github.com/weaveworks/scope/test/utils"
  13. )
  14. var (
  15. filterApplication = render.Transformers([]render.Transformer{
  16. render.AnyFilterFunc(render.IsPseudoTopology, render.IsApplication),
  17. render.FilterUnconnectedPseudo,
  18. })
  19. filterSystem = render.Transformers([]render.Transformer{
  20. render.AnyFilterFunc(render.IsPseudoTopology, render.IsSystem),
  21. render.FilterUnconnectedPseudo,
  22. })
  23. )
  24. func TestMapProcess2Container(t *testing.T) {
  25. for _, input := range []testcase{
  26. {"empty", report.MakeNode("empty"), true},
  27. {"basic process", report.MakeNodeWith("basic", map[string]string{report.PID: "201", report.DockerContainerID: "a1b2c3"}), true},
  28. {"uncontained", report.MakeNodeWith("uncontained", map[string]string{report.PID: "201", report.HostNodeID: report.MakeHostNodeID("foo")}), true},
  29. } {
  30. testMap(t, render.MapProcess2Container, input)
  31. }
  32. }
  33. type testcase struct {
  34. name string
  35. n report.Node
  36. ok bool
  37. }
  38. func testMap(t *testing.T, f render.MapFunc, input testcase) {
  39. if have := f(input.n); input.ok != (have.ID != "") {
  40. name := input.name
  41. if name == "" {
  42. name = fmt.Sprintf("%v", input.n)
  43. }
  44. t.Errorf("%s: want %v, have %v", name, input.ok, have)
  45. }
  46. }
  47. func TestContainerRenderer(t *testing.T) {
  48. have := utils.Prune(render.ContainerWithImageNameRenderer.Render(context.Background(), fixture.Report).Nodes)
  49. want := utils.Prune(expected.RenderedContainers)
  50. if !reflect.DeepEqual(want, have) {
  51. t.Error(test.Diff(want, have))
  52. }
  53. }
  54. func TestContainerFilterRenderer(t *testing.T) {
  55. // tag on of the containers in the topology and ensure
  56. // it is filtered out correctly.
  57. input := fixture.Report.Copy()
  58. input.Container.Nodes[fixture.ClientContainerNodeID] = input.Container.Nodes[fixture.ClientContainerNodeID].WithLatests(map[string]string{
  59. report.DockerLabelPrefix + "works.weave.role": "system",
  60. })
  61. have := utils.Prune(render.Render(context.Background(), input, render.ContainerWithImageNameRenderer, filterApplication).Nodes)
  62. want := utils.Prune(expected.RenderedContainers.Copy())
  63. delete(want, fixture.ClientContainerNodeID)
  64. if !reflect.DeepEqual(want, have) {
  65. t.Error(test.Diff(want, have))
  66. }
  67. }
  68. func TestContainerHostnameRenderer(t *testing.T) {
  69. have := utils.Prune(render.Render(context.Background(), fixture.Report, render.ContainerHostnameRenderer, render.Transformers(nil)).Nodes)
  70. want := utils.Prune(expected.RenderedContainerHostnames)
  71. if !reflect.DeepEqual(want, have) {
  72. t.Error(test.Diff(want, have))
  73. }
  74. }
  75. func TestContainerHostnameFilterRenderer(t *testing.T) {
  76. have := utils.Prune(render.Render(context.Background(), fixture.Report, render.ContainerHostnameRenderer, filterSystem).Nodes)
  77. want := utils.Prune(expected.RenderedContainerHostnames.Copy())
  78. delete(want, fixture.ClientContainerHostname)
  79. delete(want, fixture.ServerContainerHostname)
  80. delete(want, render.IncomingInternetID)
  81. if !reflect.DeepEqual(want, have) {
  82. t.Error(test.Diff(want, have))
  83. }
  84. }
  85. func TestContainerImageRenderer(t *testing.T) {
  86. have := utils.Prune(render.Render(context.Background(), fixture.Report, render.ContainerImageRenderer, render.Transformers(nil)).Nodes)
  87. want := utils.Prune(expected.RenderedContainerImages)
  88. if !reflect.DeepEqual(want, have) {
  89. t.Error(test.Diff(want, have))
  90. }
  91. }
  92. func TestContainerImageFilterRenderer(t *testing.T) {
  93. have := utils.Prune(render.Render(context.Background(), fixture.Report, render.ContainerImageRenderer, filterSystem).Nodes)
  94. want := utils.Prune(expected.RenderedContainerHostnames.Copy())
  95. delete(want, fixture.ClientContainerHostname)
  96. delete(want, fixture.ServerContainerHostname)
  97. delete(want, render.IncomingInternetID)
  98. if !reflect.DeepEqual(want, have) {
  99. t.Error(test.Diff(want, have))
  100. }
  101. }