1
0

commands_test.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package nebula
  2. import (
  3. "bytes"
  4. "io"
  5. "net/netip"
  6. "testing"
  7. "github.com/slackhq/nebula/commands"
  8. "github.com/slackhq/nebula/logging"
  9. "github.com/slackhq/nebula/test"
  10. "github.com/stretchr/testify/assert"
  11. "github.com/stretchr/testify/require"
  12. )
  13. // TestLogFormatRefusalIsShown: a format the logger does not know is reported to the user
  14. // as a failure, rather than returned bare, which over ssh was silence.
  15. func TestLogFormatRefusalIsShown(t *testing.T) {
  16. l := logging.NewLogger(io.Discard)
  17. out := &bytes.Buffer{}
  18. w := commands.NewStringWriter(out)
  19. var f *commands.Failure
  20. require.ErrorAs(t, logFormatCommand(l, nil, []string{"bogus"}, w), &f)
  21. assert.Equal(t, "Unable to set log format: unknown log format `bogus`. possible formats: [text json]\n", out.String())
  22. out.Reset()
  23. require.NoError(t, logFormatCommand(l, nil, []string{"JSON"}, w))
  24. assert.Equal(t, "Log format is: json\n", out.String())
  25. }
  26. // TestPrintRelaysReportsErrorsAsText: the Error field was an error value, which encodes
  27. // as {} and lost the message. It is the message now, and absent when there is none.
  28. func TestPrintRelaysReportsErrorsAsText(t *testing.T) {
  29. l := test.NewLogger()
  30. hm := newHostMap(l)
  31. relay := netip.MustParseAddr("10.0.0.1")
  32. peer := netip.MustParseAddr("10.0.0.2")
  33. // A relay host that is in Relays but not in Hosts, so its hostinfo cannot be found.
  34. hm.Relays[1] = &HostInfo{vpnAddrs: []netip.Addr{relay}}
  35. ifce := &Interface{hostMap: hm}
  36. out := &bytes.Buffer{}
  37. require.NoError(t, printRelaysCommand(ifce, &printTunnelFlags{}, nil, commands.NewStringWriter(out)))
  38. assert.JSONEq(t, `{"Relays":[{"NebulaAddr":"10.0.0.1","RelayForAddrs":[{"Error":"could not find hostinfo","Type":"","State":"","PeerAddr":"","LocalIndex":0,"RemoteIndex":0,"RelayedThrough":null}]}]}`, out.String())
  39. // The same relay host, found, relaying for a peer whose index matches: no Error key.
  40. hi := hm.Relays[1]
  41. r := &Relay{Type: ForwardingType, State: Established, PeerAddr: peer, LocalIndex: 1, RemoteIndex: 2}
  42. hi.relayState = RelayState{
  43. relayForByAddr: map[netip.Addr]*Relay{peer: r},
  44. relayForByIdx: map[uint32]*Relay{r.LocalIndex: r},
  45. }
  46. hm.Hosts[relay] = hi
  47. out.Reset()
  48. require.NoError(t, printRelaysCommand(ifce, &printTunnelFlags{}, nil, commands.NewStringWriter(out)))
  49. 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}]}]}`, out.String())
  50. // And an index mismatch names it.
  51. hm.Relays[7] = hi
  52. delete(hm.Relays, 1)
  53. out.Reset()
  54. require.NoError(t, printRelaysCommand(ifce, &printTunnelFlags{}, nil, commands.NewStringWriter(out)))
  55. assert.Contains(t, out.String(), `"Error":"hostmap LocalIndex '7' does not match RelayState LocalIndex"`)
  56. }