tailscaled_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. package main // import "tailscale.com/cmd/tailscaled"
  4. import (
  5. "os"
  6. "strings"
  7. "testing"
  8. "tailscale.com/envknob"
  9. "tailscale.com/ipn"
  10. "tailscale.com/net/netmon"
  11. "tailscale.com/tsd"
  12. "tailscale.com/tstest/deptest"
  13. "tailscale.com/types/logid"
  14. "tailscale.com/util/must"
  15. )
  16. func TestNothing(t *testing.T) {
  17. // This test does nothing on purpose, so we can run
  18. // GODEBUG=memprofilerate=1 go test -v -run=Nothing -memprofile=prof.mem
  19. // without any errors about no matching tests.
  20. }
  21. func TestDeps(t *testing.T) {
  22. deptest.DepChecker{
  23. GOOS: "darwin",
  24. GOARCH: "arm64",
  25. BadDeps: map[string]string{
  26. "testing": "do not use testing package in production code",
  27. "gvisor.dev/gvisor/pkg/hostarch": "will crash on non-4K page sizes; see https://github.com/tailscale/tailscale/issues/8658",
  28. "net/http/httptest": "do not use httptest in production code",
  29. "net/http/internal/testcert": "do not use httptest in production code",
  30. },
  31. }.Check(t)
  32. deptest.DepChecker{
  33. GOOS: "linux",
  34. GOARCH: "arm64",
  35. BadDeps: map[string]string{
  36. "testing": "do not use testing package in production code",
  37. "gvisor.dev/gvisor/pkg/hostarch": "will crash on non-4K page sizes; see https://github.com/tailscale/tailscale/issues/8658",
  38. "google.golang.org/protobuf/proto": "unexpected",
  39. "github.com/prometheus/client_golang/prometheus": "use tailscale.com/metrics in tailscaled",
  40. },
  41. }.Check(t)
  42. }
  43. func TestStateStoreError(t *testing.T) {
  44. logID, err := logid.NewPrivateID()
  45. if err != nil {
  46. t.Fatal(err)
  47. }
  48. // Don't upload any logs from tests.
  49. envknob.SetNoLogsNoSupport()
  50. args.statedir = t.TempDir()
  51. args.tunname = "userspace-networking"
  52. t.Run("new state", func(t *testing.T) {
  53. sys := tsd.NewSystem()
  54. sys.NetMon.Set(must.Get(netmon.New(sys.Bus.Get(), t.Logf)))
  55. lb, err := getLocalBackend(t.Context(), t.Logf, logID.Public(), sys)
  56. if err != nil {
  57. t.Fatal(err)
  58. }
  59. defer lb.Shutdown()
  60. if lb.HealthTracker().IsUnhealthy(ipn.StateStoreHealth) {
  61. t.Errorf("StateStoreHealth is unhealthy on fresh LocalBackend:\n%s", strings.Join(lb.HealthTracker().Strings(), "\n"))
  62. }
  63. })
  64. t.Run("corrupt state", func(t *testing.T) {
  65. sys := tsd.NewSystem()
  66. sys.NetMon.Set(must.Get(netmon.New(sys.Bus.Get(), t.Logf)))
  67. // Populate the state file with something that will fail to parse to
  68. // trigger an error from store.New.
  69. if err := os.WriteFile(statePathOrDefault(), []byte("bad json"), 0644); err != nil {
  70. t.Fatal(err)
  71. }
  72. lb, err := getLocalBackend(t.Context(), t.Logf, logID.Public(), sys)
  73. if err != nil {
  74. t.Fatal(err)
  75. }
  76. defer lb.Shutdown()
  77. if !lb.HealthTracker().IsUnhealthy(ipn.StateStoreHealth) {
  78. t.Errorf("StateStoreHealth is healthy when state file is corrupt")
  79. }
  80. })
  81. }
  82. func TestIsPortableStore(t *testing.T) {
  83. tests := []struct {
  84. name string
  85. path string
  86. want bool
  87. }{
  88. {
  89. name: "kube_store",
  90. path: "kube:my-secret",
  91. want: true,
  92. },
  93. {
  94. name: "aws_arn_store",
  95. path: "arn:aws:ssm:us-east-1:123456789012:parameter/tailscale/state",
  96. want: true,
  97. },
  98. {
  99. name: "tpm_store",
  100. path: "tpmseal:/var/lib/tailscale/tailscaled.state",
  101. want: false,
  102. },
  103. {
  104. name: "local_file_store",
  105. path: "/var/lib/tailscale/tailscaled.state",
  106. want: false,
  107. },
  108. {
  109. name: "empty_path",
  110. path: "",
  111. want: false,
  112. },
  113. {
  114. name: "mem_store",
  115. path: "mem:",
  116. want: true,
  117. },
  118. {
  119. name: "windows_file_store",
  120. path: `C:\ProgramData\Tailscale\server-state.conf`,
  121. want: false,
  122. },
  123. }
  124. for _, tt := range tests {
  125. t.Run(tt.name, func(t *testing.T) {
  126. got := isPortableStore(tt.path)
  127. if got != tt.want {
  128. t.Errorf("isPortableStore(%q) = %v, want %v", tt.path, got, tt.want)
  129. }
  130. })
  131. }
  132. }