|
| 1 | +package config |
| 2 | + |
| 3 | +import "testing" |
| 4 | + |
| 5 | +func TestHostFromEndpoint(t *testing.T) { |
| 6 | + cases := []struct { |
| 7 | + in string |
| 8 | + want string |
| 9 | + }{ |
| 10 | + {"localhost:4317", "localhost"}, |
| 11 | + {"127.0.0.1:4317", "127.0.0.1"}, |
| 12 | + {"[::1]:4317", "::1"}, |
| 13 | + {"http://localhost:4317", "localhost"}, |
| 14 | + {"https://collector.example.com:4317", "collector.example.com"}, |
| 15 | + {"otelcollector:4317", "otelcollector"}, |
| 16 | + {"localhost", "localhost"}, |
| 17 | + {" ", ""}, |
| 18 | + {"", ""}, |
| 19 | + {"http://localhost:4317/v1/traces", "localhost"}, |
| 20 | + } |
| 21 | + for _, tc := range cases { |
| 22 | + if got := hostFromEndpoint(tc.in); got != tc.want { |
| 23 | + t.Errorf("hostFromEndpoint(%q) = %q, want %q", tc.in, got, tc.want) |
| 24 | + } |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +func TestIsLoopbackHost(t *testing.T) { |
| 29 | + loopback := []string{"", "localhost", "127.0.0.1", "127.1.2.3", "::1"} |
| 30 | + for _, h := range loopback { |
| 31 | + if !isLoopbackHost(h) { |
| 32 | + t.Errorf("isLoopbackHost(%q) = false, want true", h) |
| 33 | + } |
| 34 | + } |
| 35 | + notLoopback := []string{"otelcollector", "10.0.0.1", "collector.example.com", "192.168.1.1"} |
| 36 | + for _, h := range notLoopback { |
| 37 | + if isLoopbackHost(h) { |
| 38 | + t.Errorf("isLoopbackHost(%q) = true, want false", h) |
| 39 | + } |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +func TestHasService(t *testing.T) { |
| 44 | + t.Parallel() |
| 45 | + cases := []struct { |
| 46 | + list, service string |
| 47 | + want bool |
| 48 | + }{ |
| 49 | + {"", "otelcontext", false}, |
| 50 | + {"otelcontext", "otelcontext", true}, |
| 51 | + {"a,b,otelcontext,c", "otelcontext", true}, |
| 52 | + {"a, otelcontext , c", "otelcontext", true}, |
| 53 | + {"a,b,c", "otelcontext", false}, |
| 54 | + {"otelcontextual", "otelcontext", false}, |
| 55 | + } |
| 56 | + for _, tc := range cases { |
| 57 | + if got := hasService(tc.list, tc.service); got != tc.want { |
| 58 | + t.Errorf("hasService(%q, %q) = %v, want %v", tc.list, tc.service, got, tc.want) |
| 59 | + } |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +func TestGuardSelfInstrumentation(t *testing.T) { |
| 64 | + t.Run("NoOpWhenEndpointEmpty", func(t *testing.T) { |
| 65 | + c := &Config{IngestExcludedServices: "foo"} |
| 66 | + c.GuardSelfInstrumentation() |
| 67 | + if c.IngestExcludedServices != "foo" { |
| 68 | + t.Fatalf("modified excluded list when endpoint empty: %q", c.IngestExcludedServices) |
| 69 | + } |
| 70 | + }) |
| 71 | + |
| 72 | + t.Run("AutoAddsWhenLoopback", func(t *testing.T) { |
| 73 | + c := &Config{OTelExporterEndpoint: "localhost:4317"} |
| 74 | + c.GuardSelfInstrumentation() |
| 75 | + if !hasService(c.IngestExcludedServices, SelfServiceName) { |
| 76 | + t.Fatalf("self service not auto-added: %q", c.IngestExcludedServices) |
| 77 | + } |
| 78 | + }) |
| 79 | + |
| 80 | + t.Run("PrependsToExistingList", func(t *testing.T) { |
| 81 | + c := &Config{ |
| 82 | + OTelExporterEndpoint: "127.0.0.1:4317", |
| 83 | + IngestExcludedServices: "noisy-svc", |
| 84 | + } |
| 85 | + c.GuardSelfInstrumentation() |
| 86 | + want := SelfServiceName + ",noisy-svc" |
| 87 | + if c.IngestExcludedServices != want { |
| 88 | + t.Fatalf("got %q, want %q", c.IngestExcludedServices, want) |
| 89 | + } |
| 90 | + }) |
| 91 | + |
| 92 | + t.Run("IdempotentWhenAlreadyExcluded", func(t *testing.T) { |
| 93 | + c := &Config{ |
| 94 | + OTelExporterEndpoint: "[::1]:4317", |
| 95 | + IngestExcludedServices: "a," + SelfServiceName + ",b", |
| 96 | + } |
| 97 | + before := c.IngestExcludedServices |
| 98 | + c.GuardSelfInstrumentation() |
| 99 | + if c.IngestExcludedServices != before { |
| 100 | + t.Fatalf("guard mutated already-excluded list: %q -> %q", before, c.IngestExcludedServices) |
| 101 | + } |
| 102 | + }) |
| 103 | + |
| 104 | + t.Run("NoOpForRemoteEndpoint", func(t *testing.T) { |
| 105 | + c := &Config{ |
| 106 | + OTelExporterEndpoint: "collector.example.com:4317", |
| 107 | + IngestExcludedServices: "foo", |
| 108 | + } |
| 109 | + c.GuardSelfInstrumentation() |
| 110 | + if hasService(c.IngestExcludedServices, SelfServiceName) { |
| 111 | + t.Fatalf("guard fired on remote endpoint: %q", c.IngestExcludedServices) |
| 112 | + } |
| 113 | + }) |
| 114 | + |
| 115 | + t.Run("NilSafe", func(t *testing.T) { |
| 116 | + var c *Config |
| 117 | + c.GuardSelfInstrumentation() |
| 118 | + }) |
| 119 | +} |
0 commit comments