DockerDiscoveryTests.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using RackPeek.Domain.Discovery;
  2. using RackPeek.Domain.Persistence;
  3. using RackPeek.Domain.Resources;
  4. using RackPeek.Domain.Resources.Services;
  5. namespace Tests.Discovery;
  6. /// <summary>
  7. /// A captured <c>GET /containers/json</c> response in, Service resources out.
  8. /// No Docker daemon is needed, so this runs anywhere.
  9. /// </summary>
  10. public class DockerDiscoveryTests {
  11. private const string _hostSeed = "7f3c9a1e5b2d4f6081a3c5e7b9d1f3a5";
  12. private static List<Service> Discover() =>
  13. DockerServiceMapper.ToResources(
  14. DockerContainerParser.Parse(Fixture.Read("docker-containers.json")),
  15. _hostSeed,
  16. "nas01",
  17. "192.168.1.20");
  18. [Fact]
  19. public void Only_containers_reachable_from_outside_the_host_become_services() {
  20. List<Service> services = Discover();
  21. // redis publishes nothing and pgadmin only binds loopback, so nothing outside
  22. // the host can reach either and neither is a service.
  23. Assert.Equal(["jellyfin", "paperless-ngx", "unifi", "wireguard"], services.Select(s => s.Name));
  24. }
  25. [Fact]
  26. public void A_binding_pinned_to_one_interface_is_reported_at_that_address() {
  27. Service unifi = Discover().Single(s => s.Name == "unifi");
  28. // The loopback 8843 binding is ignored; the 8443 binding is only reachable on
  29. // the address it is pinned to, so that address wins over the host's.
  30. Assert.Equal("192.168.1.21", unifi.Network!.Ip);
  31. Assert.Equal(8443, unifi.Network.Port);
  32. }
  33. [Fact]
  34. public void A_dual_stack_publish_is_one_binding_not_two() {
  35. List<DockerContainer> containers = DockerContainerParser.Parse(Fixture.Read("docker-containers.json"));
  36. // Docker reports 0.0.0.0 and :: separately for the same publish.
  37. DockerContainer jellyfin = containers.Single(c => c.Name == "jellyfin");
  38. Assert.Single(jellyfin.PublishedPorts);
  39. Assert.True(jellyfin.PublishedPorts[0].IsWildcard);
  40. }
  41. [Fact]
  42. public void A_service_carries_the_address_it_is_reachable_on() {
  43. Service jellyfin = Discover().Single(s => s.Name == "jellyfin");
  44. Assert.Equal("192.168.1.20", jellyfin.Network!.Ip);
  45. Assert.Equal(8096, jellyfin.Network.Port);
  46. Assert.Equal("TCP", jellyfin.Network.Protocol);
  47. Assert.Equal("jellyfin/jellyfin:10.9.6", jellyfin.Notes);
  48. Assert.Equal(["nas01"], jellyfin.RunsOn);
  49. }
  50. [Fact]
  51. public void Udp_bindings_keep_their_protocol() =>
  52. Assert.Equal("UDP", Discover().Single(s => s.Name == "wireguard").Network!.Protocol);
  53. [Fact]
  54. public void A_service_keeps_every_published_protocol_and_interface_address_in_order() {
  55. const string json = """[{"Names":["/app"],"Image":"app:latest","Ports":[{"IP":"127.0.0.1","PublicPort":8080,"Type":"tcp"},{"IP":"::","PublicPort":443,"Type":"tcp"},{"IP":"0.0.0.0","PublicPort":443,"Type":"tcp"},{"IP":"192.168.1.21","PublicPort":8443,"Type":"tcp"},{"IP":"192.168.1.22","PublicPort":8443,"Type":"udp"},{"IP":"::1","PublicPort":9090,"Type":"tcp"}]}]""";
  56. Service service = DockerServiceMapper.ToResources(
  57. DockerContainerParser.Parse(json), _hostSeed, "nas01", "192.168.1.20").Single();
  58. Assert.Equal(("192.168.1.20", 443, "TCP"),
  59. (service.Network!.Ip, service.Network.Port, service.Network.Protocol));
  60. Assert.Equal([
  61. ("192.168.1.20", 443, "TCP"),
  62. ("192.168.1.21", 8443, "TCP"),
  63. ("192.168.1.22", 8443, "UDP")
  64. ], service.Endpoints.Select(e => (e.Ip, e.Port, e.Protocol)));
  65. DockerContainer container = DockerContainerParser.Parse(json).Single();
  66. Service fromReverseOrder = DockerServiceMapper.ToResources(
  67. [container with { PublishedPorts = container.PublishedPorts.Reverse().ToArray() }],
  68. _hostSeed,
  69. "nas01",
  70. "192.168.1.20").Single();
  71. Assert.Equal(service.Endpoints.Select(e => (e.Ip, e.Port, e.Protocol)),
  72. fromReverseOrder.Endpoints.Select(e => (e.Ip, e.Port, e.Protocol)));
  73. Fixture.AssertConformsToSchema(DiscoveryDocument.ToYaml([service]));
  74. }
  75. [Fact]
  76. public void Rediscovery_replaces_the_endpoint_list_for_an_existing_service() {
  77. Service original = Discover().Single(s => s.Name == "jellyfin");
  78. Service incoming = new() {
  79. Kind = Service.KindLabel,
  80. Name = original.Name,
  81. Endpoints = [new Network { Ip = "192.168.1.20", Port = 8181, Protocol = "TCP" }]
  82. };
  83. Service merged = Assert.IsType<Service>(
  84. ResourceCollectionMerger.Merge([original], [incoming], MergeMode.Merge).Single());
  85. Assert.Equal(8181, Assert.Single(merged.Endpoints).Port);
  86. }
  87. [Fact]
  88. public void A_compose_project_becomes_a_tag_so_a_stack_stays_grouped() {
  89. Assert.Equal(["media"], Discover().Single(s => s.Name == "jellyfin").Tags);
  90. Assert.Equal(["paperless-stack"], Discover().Single(s => s.Name == "paperless-ngx").Tags);
  91. }
  92. [Fact]
  93. public void A_container_outside_compose_gets_no_tag() =>
  94. Assert.Empty(Discover().Single(s => s.Name == "wireguard").Tags);
  95. [Fact]
  96. public void The_same_container_name_on_two_hosts_is_two_different_resources() {
  97. List<DockerContainer> containers = DockerContainerParser.Parse(Fixture.Read("docker-containers.json"));
  98. List<Service> onNas = DockerServiceMapper.ToResources(containers, "host-a", "nas01", "192.168.1.20");
  99. List<Service> onPi = DockerServiceMapper.ToResources(containers, "host-b", "pi01", "192.168.1.34");
  100. Assert.NotEqual(onNas[0].DiscoveryId, onPi[0].DiscoveryId);
  101. }
  102. [Fact]
  103. public void Rediscovering_the_same_host_produces_the_same_ids() =>
  104. Assert.Equal(
  105. Discover().Select(s => s.DiscoveryId),
  106. Discover().Select(s => s.DiscoveryId));
  107. [Fact]
  108. public void Output_conforms_to_the_published_schema() =>
  109. Fixture.AssertConformsToSchema(DiscoveryDocument.ToYaml(Discover()));
  110. [Fact]
  111. public void An_empty_daemon_yields_nothing_rather_than_failing() =>
  112. Assert.Empty(DockerContainerParser.Parse("[]"));
  113. [Fact]
  114. public void A_socket_endpoint_counts_as_local_but_tcp_does_not() {
  115. // Local is what decides whether the host System rides along in the payload:
  116. // over TCP the locally probed facts describe this machine, not the engine's.
  117. // The no-argument default is not asserted here because it honours DOCKER_HOST,
  118. // which a developer machine may legitimately point anywhere.
  119. using var bySocket = new DockerApiClient("unix:///run/user/1000/podman/podman.sock");
  120. using var byTcp = new DockerApiClient("tcp://nas01:2375");
  121. Assert.True(bySocket.IsLocal);
  122. Assert.False(byTcp.IsLocal);
  123. }
  124. }