commands_output_test.go 5.6 KB

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