Debian Codegraph @

internal/shared/otlp/observ/target_test.go.tmpl

raw
1 // Code generated by gotmpl. DO NOT MODIFY.
2 // source: internal/shared/otlp/observ/target_test.go.tmpl
3
4 // Copyright The OpenTelemetry Authors
5 // SPDX-License-Identifier: Apache-2.0
6
7 package {{ .pkg }}
8
9 import "testing"
10
11 func TestParseTarget(t *testing.T) {
12 // gRPC target naming is defined here:
13 // https://github.com/grpc/grpc/blob/74232c6bd3c0f4bc35bad035dbeecf5cbc834a11/doc/naming.md
14 //
15 // The Go gRPC client only supports the "dns", "unix", "unix-abstract", and
16 // "passthrough" schemes natively with "dns" being the default:
17 // https://pkg.go.dev/google.golang.org/[email protected]/internal/resolver
18 //
19 // Other schemes (e.g., "consul", "zk") are supported via custom resolvers
20 // that can be registered with the gRPC resolver package. These custom
21 // resolvers are still expected to follow the general target string format
22 // when rendered with the CanonicalTarget method:
23 //
24 // <scheme>://<authority>/<endpoint>
25 //
26 // All target strings in these tests are rendered with the
27 // CanonicalTarget method. Therefore they all follow the above format.
28 tests := []struct {
29 target string
30 host string
31 port int
32 }{
33 // DNS scheme: hostname and port.
34 {target: "dns:///:8080", host: "", port: 8080},
35 {target: "dns:///example.com", host: "example.com", port: -1},
36 {target: "dns:///example.com%eth0", host: "example.com%eth0", port: -1},
37 {target: "dns:///example.com:42", host: "example.com", port: 42},
38 {target: "dns:///example.com%eth0:42", host: "example.com%eth0", port: 42},
39
40 // DNS scheme: hostname and port with authority.
41 {target: "dns://8.8.8.8/example.com", host: "example.com", port: -1},
42 {target: "dns://8.8.8.8/example.com%eth0", host: "example.com%eth0", port: -1},
43 {target: "dns://8.8.8.8/example.com:42", host: "example.com", port: 42},
44 {target: "dns://8.8.8.8/example.com%eth0:42", host: "example.com%eth0", port: 42},
45
46 // DNS scheme: IPv4 address and port.
47 {target: "dns:///192.168.1.1", host: "192.168.1.1", port: -1},
48 {target: "dns:///192.168.1.1%eth0", host: "192.168.1.1%eth0", port: -1},
49 {target: "dns:///192.168.1.1:8080", host: "192.168.1.1", port: 8080},
50 {target: "dns:///192.168.1.1%eth0:8080", host: "192.168.1.1%eth0", port: 8080},
51
52 // DNS scheme: IPv6 address and port.
53 {target: "dns:///2001:0db8:85a3:0000:0000:8a2e:0370:7334", host: "2001:db8:85a3::8a2e:370:7334", port: -1},
54 {target: "dns:///2001:db8:85a3:0:0:8a2e:370:7334", host: "2001:db8:85a3::8a2e:370:7334", port: -1},
55 {target: "dns:///2001:db8:85a3::8a2e:370:7334", host: "2001:db8:85a3::8a2e:370:7334", port: -1},
56 {target: "dns:///2001:db8:85a3::8a2e:370:7334%eth0", host: "2001:db8:85a3::8a2e:370:7334%eth0", port: -1},
57 {target: "dns:///[2001:db8:85a3::8a2e:370:7334]", host: "2001:db8:85a3::8a2e:370:7334", port: -1},
58 {target: "dns:///[2001:db8:85a3::8a2e:370:7334%eth0]", host: "2001:db8:85a3::8a2e:370:7334%eth0", port: -1},
59 {target: "dns:///[::1]:9090", host: "::1", port: 9090},
60 {target: "dns:///[::1%eth0]:9090", host: "::1%eth0", port: 9090},
61
62 // Unix domain sockets.
63 {target: "unix:///tmp/grpc.sock", host: "/tmp/grpc.sock", port: -1},
64 {target: "unix:///absolute_path", host: "/absolute_path", port: -1},
65
66 // Unix domain socket in abstract namespace.
67 {target: "unix-abstract:///abstract-socket-name", host: "/abstract-socket-name", port: -1},
68
69 // International domain names.
70 {target: "dns:///测试.example.com:8080", host: "测试.example.com", port: 8080},
71
72 // Port edge cases.
73 {target: "dns:///example.com:0", host: "example.com", port: 0},
74 {target: "dns:///example.com:65535", host: "example.com", port: 65535},
75
76 // Case sensitivity.
77 {target: "dns:///EXAMPLE.COM:8080", host: "EXAMPLE.COM", port: 8080},
78 {target: "dns:///Example.Com:8080", host: "Example.Com", port: 8080},
79
80 // Custom and passthrough resolvers scheme
81 {target: "passthrough:///localhost:50051", host: "localhost", port: 50051},
82 {target: "passthrough:///10.0.0.2:7777", host: "10.0.0.2", port: 7777},
83 {target: "consul:///my-service", host: "my-service", port: -1},
84 {target: "zk:///services/my-service", host: "services/my-service", port: -1},
85 }
86
87 for _, tt := range tests {
88 host, port, err := ParseCanonicalTarget(tt.target)
89 if err != nil {
90 t.Errorf("parseTarget(%q) unexpected error: %v", tt.target, err)
91 continue
92 }
93 if host != tt.host {
94 t.Errorf("parseTarget(%q) host = %q, want %q", tt.target, host, tt.host)
95 }
96 if port != tt.port {
97 t.Errorf("parseTarget(%q) port = %d, want %d", tt.target, port, tt.port)
98 }
99 }
100 }
101
102 func TestParseTargetErrors(t *testing.T) {
103 targets := []string{
104 "dns:///example.com:invalid", // Non-numeric port in URL.
105 "dns:///example.com:8080:9090", // Multiple colons in port.
106 "dns:///example.com:99999", // Port out of range.
107 "dns:///example.com:-1", // Port out of range.
108 "unix://localhost/sock", // Non-empty authority for unix scheme.
109 "unix:", // Empty unix scheme.
110 "unix-abstract://", // Empty unix-abstract scheme.
111 "unix-abstract://authority/sock", // Non-empty authority for unix-abstract scheme.
112 "contains-cont\roll-cha\rs", // Invalid URL.
113 }
114
115 for _, target := range targets {
116 host, port, err := ParseCanonicalTarget(target)
117 if err == nil {
118 t.Errorf("parseTarget(%q) expected error, got nil", target)
119 }
120
121 if host != "" {
122 t.Errorf("parseTarget(%q) host = %q, want empty", target, host)
123 }
124
125 if port != -1 {
126 t.Errorf("parseTarget(%q) port = %d, want -1", target, port)
127 }
128 }
129 }
130
131 func BenchmarkParseTarget(b *testing.B) {
132 benchmarks := []struct {
133 name string
134 target string
135 }{
136 {"HostName", "dns:///example.com"},
137 {"HostPort", "dns:///example.com:8080"},
138 {"IPv4WithoutPort", "dns:///192.168.1.1"},
139 {"IPv4WithPort", "dns:///192.168.1.1:8080"},
140 {"IPv6Bare", "dns:///2001:db8::1"},
141 {"IPv6Bracket", "dns:///[2001:db8::1]"},
142 {"IPv6WithPort", "dns:///[2001:db8::1]:8080"},
143 {"UnixSocket", "unix:///tmp/grpc.sock"},
144 {"UnixAbstractSocket", "unix-abstract:///abstract-socket-name"},
145 {"Passthrough", "passthrough:///localhost:50051"},
146 }
147
148 var (
149 host string
150 port int
151 err error
152 )
153 for _, bm := range benchmarks {
154 b.Run(bm.name, func(b *testing.B) {
155 b.ReportAllocs()
156 for b.Loop() {
157 host, port, err = ParseCanonicalTarget(bm.target)
158 }
159 })
160 }
161 _, _, _ = host, port, err
162 }