-
Notifications
You must be signed in to change notification settings - Fork 127
/
agent_test.go
128 lines (102 loc) · 2.74 KB
/
agent_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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package agent_test
import (
"context"
"fmt"
"net"
"sync/atomic"
"testing"
"time"
"github.com/kubeshop/testkube/pkg/executor/output"
"github.com/kubeshop/testkube/pkg/log"
"github.com/kubeshop/testkube/pkg/ui"
"github.com/stretchr/testify/assert"
"github.com/valyala/fasthttp"
"go.uber.org/zap"
"golang.org/x/sync/errgroup"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
"github.com/kubeshop/testkube/pkg/agent"
"github.com/kubeshop/testkube/pkg/cloud"
)
func TestCommandExecution(t *testing.T) {
url := "localhost:9999"
ctx, cancel := context.WithCancel(context.Background())
go func() {
lis, err := net.Listen("tcp", url)
if err != nil {
panic(err)
}
var opts []grpc.ServerOption
grpcServer := grpc.NewServer(opts...)
cloud.RegisterTestKubeCloudAPIServer(grpcServer, newServer(ctx))
grpcServer.Serve(lis)
}()
var msgCnt int32
m := func(ctx *fasthttp.RequestCtx) {
h := &ctx.Response.Header
h.Add("Content-type", "application/json")
fmt.Fprintf(ctx, "Hi there! RequestURI is %q", ctx.RequestURI())
atomic.AddInt32(&msgCnt, 1)
}
grpcConn, err := agent.NewGRPCConnection(context.Background(), true, url, log.DefaultLogger)
ui.ExitOnError("error creating gRPC connection", err)
defer grpcConn.Close()
grpcClient := cloud.NewTestKubeCloudAPIClient(grpcConn)
var logStreamFunc func(ctx context.Context, executionID string) (chan output.Output, error)
logger, _ := zap.NewDevelopment()
agent, err := agent.NewAgent(logger.Sugar(), m, "api-key", grpcClient, 5, 5, logStreamFunc, "")
if err != nil {
t.Fatal(err)
}
g, groupCtx := errgroup.WithContext(ctx)
g.Go(func() error {
return agent.Run(groupCtx)
})
time.Sleep(100 * time.Millisecond)
cancel()
g.Wait()
assert.True(t, msgCnt > 0)
}
type CloudServer struct {
cloud.UnimplementedTestKubeCloudAPIServer
ctx context.Context
}
func (cs *CloudServer) GetLogsStream(srv cloud.TestKubeCloudAPI_GetLogsStreamServer) error {
<-cs.ctx.Done()
return nil
}
func (cs *CloudServer) ExecuteAsync(srv cloud.TestKubeCloudAPI_ExecuteAsyncServer) error {
md, ok := metadata.FromIncomingContext(srv.Context())
if !ok {
panic("no metadata")
}
apiKey := md.Get("api-key")
if apiKey[0] != "api-key" {
panic("error bad api-key")
}
req := &cloud.ExecuteRequest{Method: "url", Url: "localhost/v1/tests", Body: nil}
err := srv.Send(req)
if err != nil {
return err
}
resp, err := srv.Recv()
if err != nil {
return err
}
fmt.Println(resp)
return nil
}
func (cs *CloudServer) Send(srv cloud.TestKubeCloudAPI_SendServer) error {
for {
if srv.Context().Err() != nil {
return srv.Context().Err()
}
_, err := srv.Recv()
if err != nil {
return err
}
}
}
func newServer(ctx context.Context) *CloudServer {
return &CloudServer{ctx: ctx}
}