Skip to content

Commit 5be5021

Browse files
committed
Add unit tests for config file
1 parent 08bd7fc commit 5be5021

File tree

2 files changed

+189
-2
lines changed

2 files changed

+189
-2
lines changed

proxy/proxy.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ const preparedIdSize = 16
5555

5656
type PeerConfig struct {
5757
RPCAddr string `yaml:"rpc-address"`
58-
DC string `yaml:"data-center"`
59-
Tokens []string `yaml:"tokens"`
58+
DC string `yaml:"data-center,omitempty"`
59+
Tokens []string `yaml:"tokens,omitempty"`
6060
}
6161

6262
type Config struct {

proxy/run_test.go

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,11 @@ import (
2626
"time"
2727

2828
"github.com/datastax/cql-proxy/proxycore"
29+
"github.com/datastax/go-cassandra-native-protocol/message"
30+
"github.com/datastax/go-cassandra-native-protocol/primitive"
2931
"github.com/stretchr/testify/assert"
3032
"github.com/stretchr/testify/require"
33+
"gopkg.in/yaml.v2"
3134
)
3235

3336
const (
@@ -86,6 +89,190 @@ func TestRun_HealthChecks(t *testing.T) {
8689
})
8790
}
8891

92+
func TestRun_ConfigFileWithPeers(t *testing.T) {
93+
ctx, cancel := context.WithCancel(context.Background())
94+
defer cancel()
95+
96+
cluster := proxycore.NewMockCluster(net.ParseIP(testClusterStartIP), testClusterPort)
97+
98+
err := cluster.Add(ctx, 1)
99+
require.NoError(t, err)
100+
101+
defer cluster.Shutdown()
102+
103+
configFileName, err := writeTempYaml(struct {
104+
Bind string
105+
Port int
106+
RPCAddr string `yaml:"rpc-address"`
107+
DataCenter string `yaml:"data-center"`
108+
ContactPoints []string `yaml:"contact-points"`
109+
HealthCheck bool `yaml:"health-check"`
110+
HttpBind string `yaml:"http-bind"`
111+
Peers []PeerConfig
112+
}{
113+
Bind: "127.0.0.1:9042",
114+
RPCAddr: "127.0.0.1",
115+
DataCenter: "dc-1",
116+
Port: testClusterPort,
117+
ContactPoints: []string{testClusterContactPoint},
118+
HealthCheck: true,
119+
HttpBind: testProxyHTTPBind,
120+
Peers: []PeerConfig{{
121+
RPCAddr: "127.0.0.1",
122+
DC: "dc-1",
123+
}, {
124+
RPCAddr: "127.0.0.2",
125+
DC: "dc-2",
126+
}},
127+
})
128+
129+
go func() {
130+
rc := Run(ctx, []string{
131+
"--config", configFileName,
132+
})
133+
require.Equal(t, 0, rc)
134+
}()
135+
136+
waitUntil(10*time.Second, func() bool {
137+
res, err := http.Get(fmt.Sprintf("http://%s%s", testProxyHTTPBind, livenessPath))
138+
return err == nil && res.StatusCode == http.StatusOK
139+
})
140+
141+
cl := connectTestClient(t, ctx)
142+
143+
rs, err := cl.Query(ctx, primitive.ProtocolVersion4, &message.Query{
144+
Query: "SELECT rpc_address, data_center, tokens FROM system.local",
145+
})
146+
require.Equal(t, rs.RowCount(), 1)
147+
148+
rpcAddress, err := rs.Row(0).InetByName("rpc_address")
149+
require.NoError(t, err)
150+
assert.Equal(t, "127.0.0.1", rpcAddress.String())
151+
152+
dataCenter, err := rs.Row(0).StringByName("data_center")
153+
require.NoError(t, err)
154+
assert.Equal(t, "dc-1", dataCenter)
155+
156+
val, err := rs.Row(0).ByName("tokens")
157+
require.NoError(t, err)
158+
tokens := val.([]*string)
159+
assert.NotEmpty(t, tokens)
160+
assert.Equal(t, "-9223372036854775808", *tokens[0])
161+
162+
rs, err = cl.Query(ctx, primitive.ProtocolVersion4, &message.Query{
163+
Query: "SELECT rpc_address, data_center, tokens FROM system.peers",
164+
})
165+
require.Equal(t, rs.RowCount(), 1)
166+
167+
rpcAddress, err = rs.Row(0).InetByName("rpc_address")
168+
require.NoError(t, err)
169+
assert.Equal(t, "127.0.0.2", rpcAddress.String())
170+
171+
dataCenter, err = rs.Row(0).StringByName("data_center")
172+
require.NoError(t, err)
173+
assert.Equal(t, "dc-2", dataCenter)
174+
175+
val, err = rs.Row(0).ByName("tokens")
176+
require.NoError(t, err)
177+
tokens = val.([]*string)
178+
assert.NotEmpty(t, tokens)
179+
assert.Equal(t, "-3074457345618258602", *tokens[0])
180+
}
181+
182+
func TestRun_ConfigFileWithPeersAndNoRPCAddr(t *testing.T) {
183+
ctx, cancel := context.WithCancel(context.Background())
184+
defer cancel()
185+
186+
cluster := proxycore.NewMockCluster(net.ParseIP(testClusterStartIP), testClusterPort)
187+
188+
err := cluster.Add(ctx, 1)
189+
require.NoError(t, err)
190+
191+
defer cluster.Shutdown()
192+
193+
configFileName, err := writeTempYaml(struct {
194+
Bind string
195+
Port int
196+
RPCAddr string `yaml:"rpc-address"`
197+
DataCenter string `yaml:"data-center"`
198+
ContactPoints []string `yaml:"contact-points"`
199+
Peers []PeerConfig
200+
}{
201+
ContactPoints: []string{testClusterContactPoint},
202+
Port: testClusterPort,
203+
Bind: "127.0.0.1:9042",
204+
// No RPC address, but using peers
205+
Peers: []PeerConfig{{
206+
RPCAddr: "127.0.0.2",
207+
DC: "dc-2",
208+
}},
209+
})
210+
require.NoError(t, err)
211+
212+
rc := Run(ctx, []string{
213+
"--config", configFileName,
214+
})
215+
require.Equal(t, 1, rc)
216+
}
217+
218+
func TestRun_ConfigFileWithInvalidPeer(t *testing.T) {
219+
ctx, cancel := context.WithCancel(context.Background())
220+
defer cancel()
221+
222+
cluster := proxycore.NewMockCluster(net.ParseIP(testClusterStartIP), testClusterPort)
223+
224+
err := cluster.Add(ctx, 1)
225+
require.NoError(t, err)
226+
227+
defer cluster.Shutdown()
228+
229+
configFileName, err := writeTempYaml(struct {
230+
Bind string
231+
Port int
232+
RPCAddr string `yaml:"rpc-address"`
233+
DataCenter string `yaml:"data-center"`
234+
ContactPoints []string `yaml:"contact-points"`
235+
HealthCheck bool `yaml:"health-check"`
236+
HttpBind string `yaml:"http-bind"`
237+
Peers []PeerConfig
238+
}{
239+
ContactPoints: []string{"127.0.0.1"},
240+
Bind: "127.0.0.1:9042",
241+
RPCAddr: "127.0.0.1",
242+
Port: testClusterPort,
243+
Peers: []PeerConfig{{
244+
RPCAddr: "", // Empty
245+
DC: "dc-2",
246+
}},
247+
})
248+
require.NoError(t, err)
249+
250+
rc := Run(ctx, []string{
251+
"--config", configFileName,
252+
})
253+
require.Equal(t, 1, rc)
254+
}
255+
256+
func writeTempYaml(o interface{}) (name string, err error) {
257+
bytes, err := yaml.Marshal(o)
258+
if err != nil {
259+
return "", err
260+
}
261+
262+
f, err := ioutil.TempFile("", "cql-proxy-yaml")
263+
if err != nil {
264+
return "", err
265+
}
266+
defer f.Close()
267+
268+
_, err = f.Write(bytes)
269+
if err != nil {
270+
return "", err
271+
}
272+
273+
return f.Name(), nil
274+
}
275+
89276
func checkReadiness(t *testing.T) (outage time.Duration, status int) {
90277
res, err := http.Get(fmt.Sprintf("http://%s%s", testProxyHTTPBind, readinessPath))
91278
require.NoError(t, err)

0 commit comments

Comments
 (0)