|
1 | 1 | package adaptation |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "github.com/docker/secrets-engine/pkg/api" |
| 7 | + "github.com/docker/secrets-engine/pkg/secrets" |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | + "net" |
| 11 | + "os" |
| 12 | + "path/filepath" |
| 13 | + "testing" |
| 14 | + "time" |
| 15 | + |
| 16 | + p "github.com/docker/secrets-engine/plugin" |
| 17 | +) |
| 18 | + |
| 19 | +type mockStub struct { |
| 20 | +} |
| 21 | + |
| 22 | +func (m mockStub) GetSecret(ctx context.Context, request secrets.Request) (secrets.Envelope, error) { |
| 23 | + return secrets.Envelope{}, nil |
| 24 | +} |
| 25 | + |
| 26 | +func (m mockStub) Config() p.Config { |
| 27 | + return p.Config{ |
| 28 | + Version: "v1", |
| 29 | + Pattern: "*", |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +func (m mockStub) Configure(ctx context.Context, config p.RuntimeConfig) error { |
| 34 | + return nil |
| 35 | +} |
| 36 | + |
| 37 | +func (m mockStub) Shutdown(context.Context) { |
| 38 | +} |
| 39 | + |
| 40 | +func Test_newExternalPlugin(t *testing.T) { |
| 41 | + tests := []struct { |
| 42 | + name string |
| 43 | + test func(t *testing.T) |
| 44 | + }{ |
| 45 | + { |
| 46 | + name: "create external plugin", |
| 47 | + test: func(t *testing.T) { |
| 48 | + t.Setenv("XDG_RUNTIME_DIR", os.TempDir()) |
| 49 | + socketPath := api.DefaultSocketPath() |
| 50 | + os.Remove(socketPath) |
| 51 | + require.NoError(t, os.MkdirAll(filepath.Dir(socketPath), 0755)) |
| 52 | + l, err := net.ListenUnix("unix", &net.UnixAddr{ |
| 53 | + Name: socketPath, |
| 54 | + Net: "unix", |
| 55 | + }) |
| 56 | + go func() { |
| 57 | + conn, err := l.Accept() |
| 58 | + require.NoError(t, err) |
| 59 | + |
| 60 | + p, err := newExternalPlugin(conn, setupValidator{ |
| 61 | + out: pluginCfgOut{engineName: "test-engine", engineVersion: "1.0.0", requestTimeout: 30 * time.Second}, |
| 62 | + acceptPattern: func(pattern secrets.Pattern) error { return nil }, |
| 63 | + }) |
| 64 | + assert.NoError(t, err) |
| 65 | + defer p.close() |
| 66 | + e, err := p.GetSecret(t.Context(), secrets.Request{ID: "foo"}) |
| 67 | + assert.NoError(t, err) |
| 68 | + fmt.Printf("Received envelope: %+v\n", e) |
| 69 | + }() |
| 70 | + |
| 71 | + conn, err := net.DialUnix("unix", nil, &net.UnixAddr{Name: socketPath, Net: "unix"}) |
| 72 | + require.NoError(t, err) |
| 73 | + |
| 74 | + s, err := p.New(&mockStub{}, p.WithPluginName("my-plugin"), p.WithConnection(conn)) |
| 75 | + require.NoError(t, err) |
| 76 | + assert.NoError(t, s.Run(context.Background())) |
| 77 | + }, |
| 78 | + }, |
| 79 | + } |
| 80 | + for _, tt := range tests { |
| 81 | + t.Run(tt.name, func(t *testing.T) { |
| 82 | + tt.test(t) |
| 83 | + }) |
| 84 | + } |
| 85 | +} |
0 commit comments