commands_output_test.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. //go:build e2e_testing
  2. package e2e
  3. import (
  4. "bytes"
  5. "fmt"
  6. "path/filepath"
  7. "regexp"
  8. "strings"
  9. "testing"
  10. "time"
  11. "github.com/slackhq/nebula/cert"
  12. "github.com/slackhq/nebula/cert_test"
  13. "github.com/slackhq/nebula/commands"
  14. "github.com/slackhq/nebula/sshd"
  15. "github.com/slackhq/nebula/test"
  16. "github.com/stretchr/testify/require"
  17. )
  18. // volatile lists everything in command output that legitimately differs between runs.
  19. // Each run mints fresh certificates and indexes, so these are replaced with a marker
  20. // before comparison. Keep the list short: anything added here is something the golden
  21. // stops guarding.
  22. var volatile = []struct {
  23. re *regexp.Regexp
  24. with string
  25. }{
  26. {regexp.MustCompile(`[0-9a-f]{40,}`), "<hex>"}, // fingerprints, keys, signatures
  27. {regexp.MustCompile(`\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})`), "<time>"}, // RFC 3339
  28. {regexp.MustCompile(`\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}(?:\.\d+)? [+-]\d{4} \w+`), "<time>"}, // time.Time.String()
  29. {regexp.MustCompile(`(?s)(-----BEGIN [A-Z ]+-----\n).*?(\n-----END [A-Z ]+-----)`), "${1}<pem>${2}"}, // print-cert -raw
  30. {regexp.MustCompile(`(-----BEGIN [A-Z ]+-----\\n).*?(\\n-----END [A-Z ]+-----)`), "${1}<pem>${2}"}, // the same, inside a JSON string
  31. {regexp.MustCompile(`"(localIndex|remoteIndex|messageCounter)":\d+`), `"$1":<n>`},
  32. {regexp.MustCompile(`(?m)^(\s*"?(?:LocalIndex|RemoteIndex|localIndex|remoteIndex|messageCounter)"?:?\s*)\d+`), "${1}<n>"}, // and the -pretty form
  33. }
  34. func normalise(s string) string {
  35. for _, v := range volatile {
  36. s = v.re.ReplaceAllString(s, v.with)
  37. }
  38. return s
  39. }
  40. // outputCase is one command line to run through the ssh dispatch path.
  41. type outputCase struct {
  42. name string
  43. args []string
  44. }
  45. // TestCommandOutput pins the bytes every command produces, in every format, through the
  46. // ssh dispatch path. It is the safety net for moving rendering out of the commands: a
  47. // change here is a change someone at an ssh prompt would see. Regenerate with:
  48. //
  49. // go test -tags e2e_testing ./e2e -run TestCommandOutput -update
  50. func TestCommandOutput(t *testing.T) {
  51. ca, _, caKey, _ := cert_test.NewTestCaCert(
  52. cert.Version1, cert.Curve_CURVE25519,
  53. time.Now(), time.Now().Add(10*time.Minute),
  54. nil, nil, []string{},
  55. )
  56. control, _, _, _ := newSimpleServer(cert.Version1, ca, caKey, "output-golden", "10.222.0.1/24", nil)
  57. control.Start()
  58. t.Cleanup(func() { control.Stop() })
  59. // As an ssh session does: a clone of the daemon's registry with logout added, which
  60. // help on the clone lists and describes. There is no session to end here.
  61. reg := control.Commands().Clone()
  62. reg.Register(sshd.LogoutCommand(func() {}))
  63. // Dispatch with ssh's default-format policy, so the no-flag column records what an
  64. // operator at an ssh prompt sees.
  65. reg.DefaultFormat = sshd.DefaultFormat
  66. // run dispatches as the ssh exec path does and returns the output and the error, which
  67. // over ssh is exit status 1.
  68. run := func(args ...string) (string, error) {
  69. buf := &bytes.Buffer{}
  70. err := reg.DispatchArgs(args, commands.MergedOutput(buf))
  71. return buf.String(), err
  72. }
  73. // Seed a pending tunnel so the listing and per-address commands have something to show.
  74. seed, err := run("create-tunnel", "10.222.0.9")
  75. require.NoError(t, err)
  76. require.Equal(t, "Created\n", seed, "seeding a tunnel should succeed before anything else is recorded")
  77. cases := []outputCase{
  78. {"version", nil},
  79. {"device-info", nil},
  80. {"list-hostmap", nil},
  81. {"list-hostmap", []string{"-by-index"}},
  82. {"list-pending-hostmap", nil},
  83. {"list-pending-hostmap", []string{"-by-index"}},
  84. {"list-lighthouse-addrmap", nil},
  85. {"print-cert", nil},
  86. {"print-cert", []string{"-raw"}},
  87. {"print-cert", []string{"10.222.0.9"}},
  88. // A flag after the operand is still a flag, on both surfaces.
  89. {"print-cert", []string{"10.222.0.9", "-json"}},
  90. {"print-cert", []string{"10.222.0.250"}},
  91. {"print-cert", []string{"not-an-address"}},
  92. // No address is this node; so is our own address, which the hostmap never holds.
  93. {"identity", nil},
  94. {"identity", []string{"-pem"}},
  95. {"identity", []string{"-pem", "10.222.0.1"}},
  96. {"identity", []string{"-pem", "10.222.0.9"}},
  97. {"identity", []string{"10.222.0.1"}},
  98. {"identity", []string{"10.222.0.1:4242"}},
  99. // A pending tunnel has no certificate yet.
  100. {"identity", []string{"10.222.0.9"}},
  101. {"identity", []string{"not-an-address"}},
  102. // An empty address is a caller's mistake, never this node.
  103. {"identity", []string{""}},
  104. {"identity", []string{"10.222.0.1", "10.222.0.9"}},
  105. {"print-tunnel", nil},
  106. {"print-tunnel", []string{"10.222.0.9"}},
  107. {"print-tunnel", []string{"10.222.0.250"}},
  108. {"print-relays", nil},
  109. {"query-lighthouse", nil},
  110. {"query-lighthouse", []string{"10.222.0.9"}},
  111. {"query-lighthouse", []string{"not-an-address"}},
  112. // The test logger cannot change level or format, so these cases record the
  113. // refusal each command prints on it, not a successful change.
  114. {"log-level", nil},
  115. {"log-level", []string{"debug"}},
  116. {"log-level", []string{"bogus"}},
  117. {"log-level", []string{"info"}},
  118. {"log-format", nil},
  119. {"log-format", []string{"json"}},
  120. {"log-format", []string{"text"}},
  121. {"change-remote", nil},
  122. {"change-remote", []string{"10.222.0.9"}},
  123. {"change-remote", []string{"-address", "10.94.0.10:4242", "10.222.0.9"}},
  124. {"change-remote", []string{"-address", "10.94.0.10:4242", "10.222.0.250"}},
  125. {"create-tunnel", nil},
  126. {"create-tunnel", []string{"10.222.0.9"}},
  127. // A fresh address, so this exercises the handshake start with a remote attached
  128. // rather than the "already handshaking" path above.
  129. {"create-tunnel", []string{"-address", "10.94.0.10:4242", "10.222.0.10"}},
  130. {"create-tunnel", []string{"not-an-address"}},
  131. {"close-tunnel", nil},
  132. {"close-tunnel", []string{"10.222.0.250"}},
  133. {"close-tunnel", []string{"not-an-address"}},
  134. {"help", nil},
  135. {"help", []string{"print-cert"}},
  136. {"help", []string{"logout"}},
  137. {"logout", []string{"-h"}},
  138. {"not-a-command", []string{"10.222.0.9"}},
  139. // reload is last so that whatever a reload does to the daemon cannot leak into an
  140. // earlier case. In this harness it fails: the config was loaded from a string and
  141. // has no path to re-read, which is what the recorded result says.
  142. {"reload", nil},
  143. }
  144. got := &strings.Builder{}
  145. for _, c := range cases {
  146. for _, fmtFlags := range [][]string{nil, {"-json"}, {"-pretty"}} {
  147. args := append([]string{c.name}, fmtFlags...)
  148. args = append(args, c.args...)
  149. fmt.Fprintf(got, "$ %s\n", strings.Join(args, " "))
  150. out, err := run(args...)
  151. out = normalise(out)
  152. got.WriteString(out)
  153. // Output that does not end in a newline is recorded as such, the way diff marks
  154. // it, so the golden sees that byte too and the next prompt still starts a line.
  155. if out != "" && !strings.HasSuffix(out, "\n") {
  156. got.WriteString("\\ No newline at end of output\n")
  157. }
  158. // A failed command is recorded as the exit status an ssh caller sees, so the
  159. // golden pins which outcomes count as failures.
  160. if err != nil {
  161. got.WriteString("exit status 1\n")
  162. }
  163. got.WriteString("\n")
  164. }
  165. }
  166. test.Golden(t, filepath.Join("testdata", "commands_output.golden"), got.String())
  167. }