commands_test.go 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package nebula
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "io"
  6. "net/netip"
  7. "testing"
  8. "github.com/slackhq/nebula/commands"
  9. "github.com/slackhq/nebula/logging"
  10. "github.com/slackhq/nebula/test"
  11. "github.com/stretchr/testify/assert"
  12. "github.com/stretchr/testify/require"
  13. )
  14. // TestLogFormatRefusalIsShown: a format the logger does not know is a refusal the user
  15. // sees, rather than a bare error, which over ssh was silence.
  16. func TestLogFormatRefusalIsShown(t *testing.T) {
  17. l := logging.NewLogger(io.Discard)
  18. res, err := runLogFormat(l, []string{"bogus"})
  19. require.NoError(t, err)
  20. assert.False(t, res.OK)
  21. assert.Equal(t, "Unable to set log format: unknown log format `bogus`. possible formats: [text json]", res.Reason)
  22. res, err = runLogFormat(l, []string{"JSON"})
  23. require.NoError(t, err)
  24. out := &bytes.Buffer{}
  25. require.NoError(t, commands.Typed(textLogFormat)(res.Value, out))
  26. assert.Equal(t, "Log format is: json\n", out.String())
  27. }
  28. // TestPrintRelaysReportsErrorsAsText: the Error field was an error value, which encodes
  29. // as {} and lost the message. It is the message now, and absent when there is none.
  30. func TestPrintRelaysReportsErrorsAsText(t *testing.T) {
  31. l := test.NewLogger()
  32. hm := newHostMap(l)
  33. relay := netip.MustParseAddr("10.0.0.1")
  34. peer := netip.MustParseAddr("10.0.0.2")
  35. // A relay host that is in Relays but not in Hosts, so its hostinfo cannot be found.
  36. hm.Relays[1] = &HostInfo{vpnAddrs: []netip.Addr{relay}}
  37. ifce := &Interface{hostMap: hm}
  38. marshal := func() string {
  39. b, err := json.Marshal(runPrintRelays(ifce))
  40. require.NoError(t, err)
  41. return string(b)
  42. }
  43. assert.JSONEq(t, `{"Relays":[{"NebulaAddr":"10.0.0.1","RelayForAddrs":[{"Error":"could not find hostinfo","Type":"","State":"","PeerAddr":"","LocalIndex":0,"RemoteIndex":0,"RelayedThrough":null}]}]}`, marshal())
  44. // The same relay host, found, relaying for a peer whose index matches: no Error key.
  45. hi := hm.Relays[1]
  46. r := &Relay{Type: ForwardingType, State: Established, PeerAddr: peer, LocalIndex: 1, RemoteIndex: 2}
  47. hi.relayState = RelayState{
  48. relayForByAddr: map[netip.Addr]*Relay{peer: r},
  49. relayForByIdx: map[uint32]*Relay{r.LocalIndex: r},
  50. }
  51. hm.Hosts[relay] = hi
  52. assert.JSONEq(t, `{"Relays":[{"NebulaAddr":"10.0.0.1","RelayForAddrs":[{"Type":"forwarding","State":"established","PeerAddr":"10.0.0.2","LocalIndex":1,"RemoteIndex":2,"RelayedThrough":null}]}]}`, marshal())
  53. // And an index mismatch names it.
  54. hm.Relays[7] = hi
  55. delete(hm.Relays, 1)
  56. assert.Contains(t, marshal(), `"Error":"hostmap LocalIndex '7' does not match RelayState LocalIndex"`)
  57. }
  58. // TestPrintCertOfATunnelWithoutACertificate: a tunnel still handshaking, or one being
  59. // torn down, has no certificate, and print-cert refuses rather than panicking.
  60. func TestPrintCertOfATunnelWithoutACertificate(t *testing.T) {
  61. f, _ := identityFixture(t)
  62. r, err := runPrintCert(f, &printCertFlags{}, []string{"10.0.0.3"})
  63. require.NoError(t, err)
  64. assert.False(t, r.OK)
  65. assert.Equal(t, "Could not find tunnel for vpn addr: 10.0.0.3", r.Reason)
  66. r, err = runPrintCert(f, &printCertFlags{}, []string{"10.0.0.2"})
  67. require.NoError(t, err)
  68. assert.True(t, r.OK, "a tunnel with a certificate is printed")
  69. }