-
Notifications
You must be signed in to change notification settings - Fork 41
/
client_test.go
103 lines (84 loc) · 1.86 KB
/
client_test.go
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright 2017-2023 Block, Inc.
package rce_test
import (
"testing"
"time"
"github.com/go-test/deep"
"github.com/square/rce-agent"
"github.com/square/rce-agent/pb"
)
func TestClientExitZero(t *testing.T) {
s := rce.NewServer(LADDR, nil, whitelist)
go s.StartServer()
defer s.StopServer()
time.Sleep(200 * time.Millisecond)
c := rce.NewClient(nil)
err := c.Open(HOST, PORT)
if err != nil {
t.Fatal(err)
}
id, err := c.Start("exit.zero", []string{})
if err != nil {
t.Error(err)
}
status, err := c.Wait(id)
if err != nil {
t.Error(err)
}
if status.ExitCode != 0 {
t.Errorf("got exit %d, expected 0", status.ExitCode)
}
}
func TestClientLongRunningCommand(t *testing.T) {
s := rce.NewServer(LADDR, nil, whitelist)
go s.StartServer()
defer s.StopServer()
time.Sleep(200 * time.Millisecond)
c := rce.NewClient(nil)
err := c.Open(HOST, PORT)
if err != nil {
t.Fatal(err)
}
id, err := c.Start("sleep60", []string{})
if err != nil {
t.Error(err)
}
doneChan := make(chan struct{})
var finalStatus *pb.Status
var waitErr error
go func() {
defer close(doneChan)
finalStatus, waitErr = c.Wait(id)
}()
time.Sleep(1 * time.Second)
gotRunning, err := c.Running()
if err != nil {
t.Error(err)
}
expectRunning := []string{id}
if diff := deep.Equal(gotRunning, expectRunning); diff != nil {
t.Error(diff)
}
runningStatus, err := c.GetStatus(id)
if err != nil {
t.Error(err)
}
if runningStatus.State != pb.STATE_RUNNING {
t.Errorf("Status.State = %d, expected %d (RUNNING)", runningStatus.State, pb.STATE_RUNNING)
}
err = c.Stop(id)
if err != nil {
t.Error(err)
}
select {
case <-doneChan:
case <-time.After(2 * time.Second):
t.Fatal("timeout waiting for command to stop")
}
if waitErr != nil {
t.Error(waitErr)
}
if finalStatus.ExitCode != -1 {
t.Errorf("got exit %d, expected -1", finalStatus.ExitCode)
}
}