| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- //go:build !windows
- package localapi
- import (
- "context"
- "fmt"
- "os"
- "path/filepath"
- "strings"
- "testing"
- "github.com/slackhq/nebula/config"
- "github.com/slackhq/nebula/test"
- "github.com/stretchr/testify/assert"
- "github.com/stretchr/testify/require"
- )
- // TestWireRecoversFromBadPathAtStartup covers a socket path that is unusable when nebula
- // starts. The operator's fix is a config edit and a reload, so the reload has to be able
- // to bring up a socket that never existed, not only move one that does.
- func TestWireRecoversFromBadPathAtStartup(t *testing.T) {
- ctx, cancel := context.WithCancel(context.Background())
- t.Cleanup(cancel)
- c := config.NewC(test.NewLogger())
- require.NoError(t, c.LoadString(fmt.Sprintf("local_api:\n control:\n socket: /%s\n", strings.Repeat("x", sunPathMax))))
- srv := Wire(ctx, test.NewLogger(), c, basicRegistry())
- t.Cleanup(srv.Stop)
- assert.Empty(t, srv.Path(), "an over-long path must not produce a socket")
- good := testSocket(t)
- require.NoError(t, c.ReloadConfigString(fmt.Sprintf("local_api:\n control:\n socket: %s\n", good)).Err)
- assert.Equal(t, good, srv.Path())
- out, errText := run(t, good, "echo", "hi")
- assert.Empty(t, errText)
- assert.Equal(t, "[hi]", out)
- }
- // TestRebindAfterReloadBoundTheSocket covers a HUP that lands inside Wire, after the
- // reload callback is registered and before the first attempt runs. The callback does the
- // first bind; the attempt must then find the socket it was about to create already up
- // and leave it, not fail or rebind it, which would have Wire warn that commands are
- // unavailable while they are.
- func TestRebindAfterReloadBoundTheSocket(t *testing.T) {
- ctx, cancel := context.WithCancel(context.Background())
- t.Cleanup(cancel)
- path := testSocket(t)
- cfg := fmt.Sprintf("local_api:\n control:\n socket: %s\n", path)
- c := config.NewC(test.NewLogger())
- require.NoError(t, c.LoadString(cfg))
- srv := NewServer(test.NewLogger(), basicRegistry())
- t.Cleanup(srv.Stop)
- // Wire's first half, then the HUP.
- followControlConfig(ctx, test.NewLogger(), c, srv)
- require.NoError(t, c.ReloadConfigString(cfg).Err)
- require.Equal(t, path, srv.Path(), "the reload callback does the first bind")
- // Wire's second half.
- require.NoError(t, srv.Rebind(ctx, path, ownerOnly()))
- assert.Equal(t, path, srv.Path())
- out, errText := run(t, path, "echo", "hi")
- assert.Empty(t, errText)
- assert.Equal(t, "[hi]", out)
- }
- // TestWireRetriesAFailedMoveOnPlainReload covers a move whose bind fails for a reason
- // outside the config: the key names a path under a runtime directory that is not there
- // yet. The daemon keeps the old socket, but the client reads the new path from the same
- // file and cannot reach it, so the operator creates the directory and reloads. The key
- // has not changed since the move was asked for, and the reload still has to make it.
- func TestWireRetriesAFailedMoveOnPlainReload(t *testing.T) {
- ctx, cancel := context.WithCancel(context.Background())
- t.Cleanup(cancel)
- section := func(path string) string {
- return fmt.Sprintf("local_api:\n control:\n socket: %s\n", path)
- }
- first := testSocket(t)
- c := config.NewC(test.NewLogger())
- require.NoError(t, c.LoadString(section(first)))
- srv := Wire(ctx, test.NewLogger(), c, basicRegistry())
- t.Cleanup(srv.Stop)
- require.Equal(t, first, srv.Path())
- runtimeDir := filepath.Join(privateBase(t), "run")
- moved := filepath.Join(runtimeDir, DirName, "s.sock")
- require.NoError(t, c.ReloadConfigString(section(moved)).Err)
- assert.Equal(t, first, srv.Path(), "a move that cannot bind keeps the socket it had")
- require.NoError(t, os.Mkdir(runtimeDir, 0700))
- require.NoError(t, c.ReloadConfigString(section(moved)).Err)
- require.False(t, c.HasChanged(CtlSocketKey), "the config is the same; the fix was on disk")
- assert.Equal(t, moved, srv.Path())
- _, err := os.Stat(first)
- assert.True(t, os.IsNotExist(err), "the old socket is given up once the move binds")
- out, errText := run(t, moved, "echo", "hi")
- assert.Empty(t, errText)
- assert.Equal(t, "[hi]", out)
- }
- // TestWireRecoversOnPlainReloadAfterBadBind covers a path that is fine but a bind that
- // is not: the runtime directory is missing when nebula starts. The fix is made outside
- // the config, so the config does not change; a plain reload still has to bring the
- // socket up.
- func TestWireRecoversOnPlainReloadAfterBadBind(t *testing.T) {
- ctx, cancel := context.WithCancel(context.Background())
- t.Cleanup(cancel)
- base := privateBase(t)
- runtimeDir := filepath.Join(base, "run")
- path := filepath.Join(runtimeDir, DirName, "s.sock")
- cfg := fmt.Sprintf("local_api:\n control:\n socket: %s\n", path)
- c := config.NewC(test.NewLogger())
- require.NoError(t, c.LoadString(cfg))
- srv := Wire(ctx, test.NewLogger(), c, basicRegistry())
- t.Cleanup(srv.Stop)
- assert.Empty(t, srv.Path(), "listen creates one level only, so a missing runtime dir fails the bind")
- require.NoError(t, os.Mkdir(runtimeDir, 0700))
- require.NoError(t, c.ReloadConfigString(cfg).Err)
- require.False(t, c.HasChanged(CtlSocketKey), "the config is the same; the fix was on disk")
- assert.Equal(t, path, srv.Path())
- out, errText := run(t, path, "echo", "hi")
- assert.Empty(t, errText)
- assert.Equal(t, "[hi]", out)
- // And a reload with a socket up and no change in the key is still a no-op.
- require.NoError(t, c.ReloadConfigString(cfg).Err)
- assert.Equal(t, path, srv.Path())
- out, errText = run(t, path, "echo", "still")
- assert.Empty(t, errText)
- assert.Equal(t, "[still]", out)
- }
|