ssh_commands_test.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package nebula
  2. import (
  3. "bytes"
  4. "fmt"
  5. "path/filepath"
  6. "strings"
  7. "testing"
  8. "github.com/slackhq/nebula/commands"
  9. "github.com/slackhq/nebula/config"
  10. "github.com/slackhq/nebula/test"
  11. )
  12. // TestSSHCommandSurface is a characterization test for the ssh command surface.
  13. // It pins the registered command set, their descriptions, help text and flag definitions
  14. // so that moving the registry to a transport neutral package is provably a relocation
  15. // rather than a rewrite. Regenerate with `go test . -run TestSSHCommandSurface -update`.
  16. func TestSSHCommandSurface(t *testing.T) {
  17. got := renderCommandSurface(registeredCommands(t).All())
  18. test.Golden(t, filepath.Join("testdata", "ssh_commands.golden"), got)
  19. }
  20. // registeredCommands returns a registry holding every command nebula serves. The
  21. // Interface is never dereferenced: attachCommands only closes over it.
  22. func registeredCommands(t *testing.T) *commands.Registry {
  23. t.Helper()
  24. l := test.NewLogger()
  25. reg := commands.New()
  26. attachCommands(l, config.NewC(l), reg, &Interface{})
  27. return reg
  28. }
  29. func renderCommandSurface(cmds []*commands.Command) string {
  30. lines := make([]string, 0, len(cmds))
  31. for _, cmd := range cmds {
  32. b := &bytes.Buffer{}
  33. fmt.Fprintf(b, "%s\n", cmd.Name)
  34. fmt.Fprintf(b, " short: %s\n", cmd.ShortDescription)
  35. fmt.Fprintf(b, " help: %s\n", cmd.Help)
  36. if cmd.Flags != nil {
  37. fl, _ := cmd.Flags()
  38. if fl != nil {
  39. fb := &bytes.Buffer{}
  40. fl.SetOutput(fb)
  41. fl.PrintDefaults()
  42. fmt.Fprintf(b, " flags:\n%s", fb.String())
  43. }
  44. }
  45. lines = append(lines, b.String())
  46. }
  47. // In the order All gives, so the golden pins display order too.
  48. return strings.Join(lines, "")
  49. }