|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "net" |
| 7 | + "os" |
| 8 | + "sync" |
| 9 | + |
| 10 | + "tailscale.com/client/tailscale" |
| 11 | + "tailscale.com/ipn" |
| 12 | + "tailscale.com/ipn/ipnstate" |
| 13 | +) |
| 14 | + |
| 15 | +// static check for local client interface implementation |
| 16 | +var _ localClient = (*tailscale.LocalClient)(nil) |
| 17 | + |
| 18 | +// localClient is an abstraction of tailscale.LocalClient |
| 19 | +type localClient interface { |
| 20 | + Status(ctx context.Context) (*ipnstate.Status, error) |
| 21 | + GetServeConfig(ctx context.Context) (*ipn.ServeConfig, error) |
| 22 | + StatusWithoutPeers(ctx context.Context) (*ipnstate.Status, error) |
| 23 | + SetServeConfig(ctx context.Context, config *ipn.ServeConfig) error |
| 24 | +} |
| 25 | + |
| 26 | +type profile struct { |
| 27 | + Status *ipnstate.Status |
| 28 | + ServeConfig *ipn.ServeConfig |
| 29 | + MockOffline bool |
| 30 | + MockAccessDenied bool |
| 31 | +} |
| 32 | + |
| 33 | +// NewMockClient returns a mock localClient |
| 34 | +// based on the given json file. The format of the file |
| 35 | +// is described in the profile struct. Note that SET |
| 36 | +// operations update the given input in memory. |
| 37 | +func NewMockClient(file string) (localClient, error) { |
| 38 | + bts, err := os.ReadFile(file) |
| 39 | + if err != nil { |
| 40 | + return nil, err |
| 41 | + } |
| 42 | + var p profile |
| 43 | + return &mockClient{p: &p}, json.Unmarshal(bts, &p) |
| 44 | +} |
| 45 | + |
| 46 | +type mockClient struct { |
| 47 | + sync.Mutex |
| 48 | + p *profile |
| 49 | +} |
| 50 | + |
| 51 | +// GetServeConfig implements localClient. |
| 52 | +func (m *mockClient) GetServeConfig(ctx context.Context) (*ipn.ServeConfig, error) { |
| 53 | + if m.p.MockOffline { |
| 54 | + return nil, &net.OpError{Op: "dial"} |
| 55 | + } |
| 56 | + return m.p.ServeConfig, nil |
| 57 | +} |
| 58 | + |
| 59 | +// SetServeConfig implements localClient. |
| 60 | +func (m *mockClient) SetServeConfig(ctx context.Context, config *ipn.ServeConfig) error { |
| 61 | + if m.p.MockAccessDenied { |
| 62 | + return &tailscale.AccessDeniedError{} |
| 63 | + } |
| 64 | + m.Lock() |
| 65 | + defer m.Unlock() |
| 66 | + m.p.ServeConfig = config |
| 67 | + return nil |
| 68 | +} |
| 69 | + |
| 70 | +// Status implements localClient. |
| 71 | +func (m *mockClient) Status(ctx context.Context) (*ipnstate.Status, error) { |
| 72 | + if m.p.MockOffline || m.p.Status == nil { |
| 73 | + return nil, &net.OpError{Op: "dial"} |
| 74 | + } |
| 75 | + return m.p.Status, nil |
| 76 | +} |
| 77 | + |
| 78 | +// StatusWithoutPeers implements localClient. |
| 79 | +func (m *mockClient) StatusWithoutPeers(ctx context.Context) (*ipnstate.Status, error) { |
| 80 | + if m.p.MockOffline || m.p.Status == nil { |
| 81 | + return nil, &net.OpError{Op: "dial"} |
| 82 | + } |
| 83 | + copy := *(m.p.Status) |
| 84 | + copy.Peer = nil |
| 85 | + return ©, nil |
| 86 | +} |
0 commit comments