@@ -3,8 +3,7 @@ package tailscalesd
33import (
44 "context"
55 "errors"
6- "io"
7- "log"
6+ "log/slog"
87 "net/http"
98 "net/http/httptest"
109 "os"
@@ -15,7 +14,7 @@ import (
1514
1615func TestMain (m * testing.M ) {
1716 // No log output during test runs.
18- log . SetOutput ( io . Discard )
17+ slog . SetDefault ( slog . New ( slog . DiscardHandler ) )
1918 os .Exit (m .Run ())
2019}
2120
@@ -111,6 +110,29 @@ func TestFilterIPv6Addresses(t *testing.T) {
111110 }
112111}
113112
113+ func TestTagToLabelKey (t * testing.T ) {
114+ for _ , tc := range []struct {
115+ tag string
116+ want string
117+ }{
118+ {"" , "__meta_tailscale_device_tag_EMPTY" },
119+ {"tag:" , "__meta_tailscale_device_tag_EMPTY" },
120+ {"tag:foo" , "__meta_tailscale_device_tag_foo" },
121+ {"tag:foo-bar" , "__meta_tailscale_device_tag_foo_bar" },
122+ {"tag:foo_bar" , "__meta_tailscale_device_tag_foo_bar" },
123+ {"tag:ж" , "__meta_tailscale_device_tag_ж" },
124+ {"tag:foo:1234" , "__meta_tailscale_device_tag_foo_1234" },
125+ {"tag:camelCase" , "__meta_tailscale_device_tag_camelcase" },
126+ } {
127+ t .Run (tc .tag , func (t * testing.T ) {
128+ got := tagToLabelKey (tc .tag )
129+ if got != tc .want {
130+ t .Errorf ("tagToLabelKey(%q): got: %v want: %v" , tc .tag , got , tc .want )
131+ }
132+ })
133+ }
134+ }
135+
114136func TestTranslate (t * testing.T ) {
115137 for tn , tc := range map [string ]struct {
116138 devices []Device
@@ -150,7 +172,7 @@ func TestTranslate(t *testing.T) {
150172 },
151173 },
152174 },
153- "single device with two tags expands to two descriptors " : {
175+ "single device with single tag expands to single descriptor " : {
154176 devices : []Device {
155177 {
156178 Addresses : []string {
@@ -166,7 +188,6 @@ func TestTranslate(t *testing.T) {
166188167189 Tags : []string {
168190 "tag:foo" ,
169- "tag:bar" ,
170191 },
171192 },
172193 },
@@ -181,10 +202,33 @@ func TestTranslate(t *testing.T) {
181202 "__meta_tailscale_device_id" : "id" ,
182203 "__meta_tailscale_device_name" : "somethingclever" ,
183204 "__meta_tailscale_device_os" : "beos" ,
184- "__meta_tailscale_device_tag " : "tag:foo " ,
205+ "__meta_tailscale_device_tag_foo " : "1 " ,
185206 "__meta_tailscale_tailnet" :
"[email protected] " ,
186207 },
187208 },
209+ },
210+ },
211+ "single device with two tags expands to single descriptor" : {
212+ devices : []Device {
213+ {
214+ Addresses : []string {
215+ "100.2.3.4" ,
216+ "fd7a::1234" ,
217+ },
218+ API : "foo.example.com" ,
219+ ClientVersion : "420.69" ,
220+ Hostname : "somethingclever" ,
221+ ID : "id" ,
222+ Name : "somethingclever" ,
223+ OS : "beos" ,
224+ 225+ Tags : []string {
226+ "tag:foo" ,
227+ "tag:bar" ,
228+ },
229+ },
230+ },
231+ want : []TargetDescriptor {
188232 {
189233 Targets : []string {"100.2.3.4" , "fd7a::1234" },
190234 Labels : map [string ]string {
@@ -195,7 +239,8 @@ func TestTranslate(t *testing.T) {
195239 "__meta_tailscale_device_id" : "id" ,
196240 "__meta_tailscale_device_name" : "somethingclever" ,
197241 "__meta_tailscale_device_os" : "beos" ,
198- "__meta_tailscale_device_tag" : "tag:bar" ,
242+ "__meta_tailscale_device_tag_foo" : "1" ,
243+ "__meta_tailscale_device_tag_bar" : "1" ,
199244 "__meta_tailscale_tailnet" :
"[email protected] " ,
200245 },
201246 },
@@ -238,22 +283,8 @@ func TestTranslate(t *testing.T) {
238283 "__meta_tailscale_device_id" : "id" ,
239284 "__meta_tailscale_device_name" : "somethingclever" ,
240285 "__meta_tailscale_device_os" : "beos" ,
241- "__meta_tailscale_device_tag" : "tag:foo" ,
242- "__meta_tailscale_tailnet" :
"[email protected] " ,
243- "test_label" : "IT WORKED" ,
244- },
245- },
246- {
247- Targets : []string {"100.2.3.4" , "fd7a::1234" },
248- Labels : map [string ]string {
249- "__meta_tailscale_api" : "foo.example.com" ,
250- "__meta_tailscale_device_authorized" : "false" ,
251- "__meta_tailscale_device_client_version" : "420.69" ,
252- "__meta_tailscale_device_hostname" : "somethingclever" ,
253- "__meta_tailscale_device_id" : "id" ,
254- "__meta_tailscale_device_name" : "somethingclever" ,
255- "__meta_tailscale_device_os" : "beos" ,
256- "__meta_tailscale_device_tag" : "tag:bar" ,
286+ "__meta_tailscale_device_tag_foo" : "1" ,
287+ "__meta_tailscale_device_tag_bar" : "1" ,
257288 "__meta_tailscale_tailnet" :
"[email protected] " ,
258289 "test_label" : "IT WORKED" ,
259290 },
@@ -333,7 +364,7 @@ func TestDiscoveryHandler(t *testing.T) {
333364 want : httpWant {
334365 code : http .StatusOK ,
335366 contentType : "application/json; charset=utf-8" ,
336- body :
`[{"targets":["100.2.3.4","fd7a::1234"],"labels":{"__meta_tailscale_api":"foo.example.com","__meta_tailscale_device_authorized":"false","__meta_tailscale_device_client_version":"420.69","__meta_tailscale_device_hostname":"somethingclever","__meta_tailscale_device_id":"id","__meta_tailscale_device_name":"somethingclever","__meta_tailscale_device_os":"beos","__meta_tailscale_device_tag ":"tag:foo ","__meta_tailscale_tailnet ":"[email protected] "}},{"targets":["100.2.3.4","fd7a::1234"],"labels":{"__meta_tailscale_api":"foo.example.com","__meta_tailscale_device_authorized":"false","__meta_tailscale_device_client_version":"420.69","__meta_tailscale_device_hostname":"somethingclever","__meta_tailscale_device_id":"id","__meta_tailscale_device_name":"somethingclever","__meta_tailscale_device_os":"beos","__meta_tailscale_device_tag":"tag:bar ","__meta_tailscale_tailnet":"[email protected] "}}]` + "\n " ,
367+ body :
`[{"targets":["100.2.3.4","fd7a::1234"],"labels":{"__meta_tailscale_api":"foo.example.com","__meta_tailscale_device_authorized":"false","__meta_tailscale_device_client_version":"420.69","__meta_tailscale_device_hostname":"somethingclever","__meta_tailscale_device_id":"id","__meta_tailscale_device_name":"somethingclever","__meta_tailscale_device_os":"beos","__meta_tailscale_device_tag_bar ":"1 ","__meta_tailscale_device_tag_foo ":"1 ","__meta_tailscale_tailnet":"[email protected] "}}]` + "\n " ,
337368 },
338369 },
339370 "results with no errors are served" : {
@@ -361,7 +392,7 @@ func TestDiscoveryHandler(t *testing.T) {
361392 want : httpWant {
362393 code : http .StatusOK ,
363394 contentType : "application/json; charset=utf-8" ,
364- body :
`[{"targets":["100.2.3.4","fd7a::1234"],"labels":{"__meta_tailscale_api":"foo.example.com","__meta_tailscale_device_authorized":"false","__meta_tailscale_device_client_version":"420.69","__meta_tailscale_device_hostname":"somethingclever","__meta_tailscale_device_id":"id","__meta_tailscale_device_name":"somethingclever","__meta_tailscale_device_os":"beos","__meta_tailscale_device_tag ":"tag:foo ","__meta_tailscale_tailnet ":"[email protected] "}},{"targets":["100.2.3.4","fd7a::1234"],"labels":{"__meta_tailscale_api":"foo.example.com","__meta_tailscale_device_authorized":"false","__meta_tailscale_device_client_version":"420.69","__meta_tailscale_device_hostname":"somethingclever","__meta_tailscale_device_id":"id","__meta_tailscale_device_name":"somethingclever","__meta_tailscale_device_os":"beos","__meta_tailscale_device_tag":"tag:bar ","__meta_tailscale_tailnet":"[email protected] "}}]` + "\n " ,
395+ body :
`[{"targets":["100.2.3.4","fd7a::1234"],"labels":{"__meta_tailscale_api":"foo.example.com","__meta_tailscale_device_authorized":"false","__meta_tailscale_device_client_version":"420.69","__meta_tailscale_device_hostname":"somethingclever","__meta_tailscale_device_id":"id","__meta_tailscale_device_name":"somethingclever","__meta_tailscale_device_os":"beos","__meta_tailscale_device_tag_bar ":"1 ","__meta_tailscale_device_tag_foo ":"1 ","__meta_tailscale_tailnet":"[email protected] "}}]` + "\n " ,
365396 },
366397 },
367398 } {
0 commit comments