summaryrefslogtreecommitdiffhomepage
path: root/logpolicy/logpolicy_test.go
blob: 64e54467c496b9b132bf290d01b15af0cc8bdb54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause

package logpolicy

import (
	"net/http"
	"os"
	"reflect"
	"testing"

	"tailscale.com/logtail"
)

func resetLogTarget() {
	os.Unsetenv("TS_LOG_TARGET")
	v := reflect.ValueOf(&getLogTargetOnce).Elem()
	v.Set(reflect.Zero(v.Type()))
}

func TestLogHost(t *testing.T) {
	defer resetLogTarget()

	tests := []struct {
		env  string
		want string
	}{
		{"", logtail.DefaultHost},
		{"http://foo.com", "foo.com"},
		{"https://foo.com", "foo.com"},
		{"https://foo.com/", "foo.com"},
		{"https://foo.com:123/", "foo.com"},
	}
	for _, tt := range tests {
		resetLogTarget()
		os.Setenv("TS_LOG_TARGET", tt.env)
		if got := LogHost(); got != tt.want {
			t.Errorf("for env %q, got %q, want %q", tt.env, got, tt.want)
		}
	}
}
func TestOptions(t *testing.T) {
	defer resetLogTarget()

	tests := []struct {
		name        string
		opts        func() Options
		wantBaseURL string
	}{
		{
			name:        "default",
			opts:        func() Options { return Options{} },
			wantBaseURL: "",
		},
		{
			name: "custom_baseurl",
			opts: func() Options {
				os.Setenv("TS_LOG_TARGET", "http://localhost:1234")
				return Options{}
			},
			wantBaseURL: "http://localhost:1234",
		},
		{
			name: "custom_httpc_and_baseurl",
			opts: func() Options {
				os.Setenv("TS_LOG_TARGET", "http://localhost:12345")
				return Options{HTTPC: &http.Client{Transport: noopPretendSuccessTransport{}}}
			},
			wantBaseURL: "http://localhost:12345",
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			resetLogTarget()
			config, policy := tt.opts().init(false)
			if policy == nil {
				t.Fatal("unexpected nil policy")
			}
			if config.BaseURL != tt.wantBaseURL {
				t.Errorf("got %q, want %q", config.BaseURL, tt.wantBaseURL)
			}
			policy.Close()
		})
	}
}

// TestInvalidLogTarget is a test for #17792
func TestInvalidLogTarget(t *testing.T) {
	defer resetLogTarget()

	tests := []struct {
		name      string
		logTarget string
	}{
		{
			name:      "invalid_url_no_scheme",
			logTarget: "not a url at all",
		},
		{
			name:      "malformed_url",
			logTarget: "ht!tp://invalid",
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			resetLogTarget()
			os.Setenv("TS_LOG_TARGET", tt.logTarget)

			opts := Options{
				Collection: "test.log.tailscale.io",
				Logf:       t.Logf,
			}

			// This should not panic even with invalid log target
			config, policy := opts.init(false)
			if policy == nil {
				t.Fatal("expected non-nil policy")
			}
			defer policy.Close()

			// When log target is invalid, it should fall back to the invalid value
			// but not crash. BaseURL should remain empty
			if config.BaseURL != "" {
				t.Errorf("got BaseURL=%q, want empty", config.BaseURL)
			}
		})
	}
}