-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathconnect_test.go
More file actions
75 lines (62 loc) · 1.88 KB
/
connect_test.go
File metadata and controls
75 lines (62 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package cli_test
import (
"bytes"
"context"
"net"
"testing"
"github.com/stretchr/testify/require"
"tailscale.com/net/tsaddr"
"github.com/coder/coder/v2/cli"
"github.com/coder/coder/v2/codersdk/workspacesdk"
"github.com/coder/coder/v2/testutil"
"github.com/coder/serpent"
)
func TestConnectExists_Running(t *testing.T) {
t.Parallel()
ctx := testutil.Context(t, testutil.WaitShort)
var root cli.RootCmd
cmd, err := root.Command(root.AGPL())
require.NoError(t, err)
inv := (&serpent.Invocation{
Command: cmd,
Args: []string{"connect", "exists", "test.example"},
}).WithContext(withCoderConnectRunning(ctx))
stdout := new(bytes.Buffer)
stderr := new(bytes.Buffer)
inv.Stdout = stdout
inv.Stderr = stderr
err = inv.Run()
require.NoError(t, err)
}
func TestConnectExists_NotRunning(t *testing.T) {
t.Parallel()
ctx := testutil.Context(t, testutil.WaitShort)
var root cli.RootCmd
cmd, err := root.Command(root.AGPL())
require.NoError(t, err)
inv := (&serpent.Invocation{
Command: cmd,
Args: []string{"connect", "exists", "test.example"},
}).WithContext(withCoderConnectNotRunning(ctx))
stdout := new(bytes.Buffer)
stderr := new(bytes.Buffer)
inv.Stdout = stdout
inv.Stderr = stderr
err = inv.Run()
require.ErrorIs(t, err, cli.ErrSilent)
}
type fakeResolver struct {
shouldReturnSuccess bool
}
func (f *fakeResolver) LookupIP(_ context.Context, _, _ string) ([]net.IP, error) {
if f.shouldReturnSuccess {
return []net.IP{net.ParseIP(tsaddr.CoderServiceIPv6().String())}, nil
}
return nil, &net.DNSError{IsNotFound: true}
}
func withCoderConnectRunning(ctx context.Context) context.Context {
return workspacesdk.WithTestOnlyCoderContextResolver(ctx, &fakeResolver{shouldReturnSuccess: true})
}
func withCoderConnectNotRunning(ctx context.Context) context.Context {
return workspacesdk.WithTestOnlyCoderContextResolver(ctx, &fakeResolver{shouldReturnSuccess: false})
}