-
Notifications
You must be signed in to change notification settings - Fork 43
/
server_client_test.go
103 lines (89 loc) · 2.6 KB
/
server_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
package opamp
import (
"context"
"os"
"sync/atomic"
"testing"
"time"
"github.com/SigNoz/signoz-otel-collector/signozcol"
"github.com/gorilla/websocket"
"github.com/open-telemetry/opamp-go/protobufs"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
)
func eventually(t *testing.T, f func() bool) {
assert.Eventually(t, f, 5*time.Second, 10*time.Millisecond)
}
func TestNewClient(t *testing.T) {
// FIXME(srikanthccv): this test interferes with other tests that use the same port.
// Remove the sleep and find a better way to fix this.
time.Sleep(5 * time.Second)
srv := StartMockServer(t)
var conn atomic.Value
srv.OnWSConnect = func(c *websocket.Conn) {
conn.Store(c)
}
var connected int64
srv.OnMessage = func(msg *protobufs.AgentToServer) *protobufs.ServerToAgent {
fileContents, err := os.ReadFile("testdata/coll-config-path-changed.yaml")
assert.NoError(t, err)
atomic.AddInt64(&connected, 1)
var resp *protobufs.ServerToAgent = &protobufs.ServerToAgent{}
if atomic.LoadInt64(&connected) == 1 {
resp = &protobufs.ServerToAgent{
RemoteConfig: &protobufs.AgentRemoteConfig{
Config: &protobufs.AgentConfigMap{
ConfigMap: map[string]*protobufs.AgentConfigFile{
"collector.yaml": {
Body: fileContents,
ContentType: "text/yaml",
},
},
},
},
}
}
return resp
}
logger := zap.NewNop()
cnt := 0
reloadFunc := func(contents []byte) error {
cnt++
return nil
}
_, err := NewDynamicConfig("./testdata/coll-config-path.yaml", reloadFunc, logger)
require.NoError(t, err)
// maintain a cop of the original config file and restore it after the test
fileContents, err := os.ReadFile("testdata/coll-config-path.yaml")
require.NoError(t, err)
defer func() {
err := os.WriteFile("testdata/coll-config-path.yaml", fileContents, 0644)
require.NoError(t, err)
}()
coll := signozcol.New(signozcol.WrappedCollectorSettings{
ConfigPaths: []string{"./testdata/coll-config-path.yaml"},
Version: "0.0.1-server-client-test",
})
svrClient, err := NewServerClient(&NewServerClientOpts{
Logger: logger,
Config: &AgentManagerConfig{
ServerEndpoint: "ws://" + srv.Endpoint,
},
CollectorConfgPath: "./testdata/coll-config-path.yaml",
WrappedCollector: coll,
})
require.NoError(t, err)
ctx := context.Background()
// Start the client.
err = svrClient.Start(ctx)
defer func() {
err := svrClient.Stop(ctx)
require.NoError(t, err)
}()
require.NoError(t, err)
// Wait for the client to connect.
eventually(t, func() bool {
return atomic.LoadInt64(&connected) == 1
})
}