-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1d353fa
commit 081bec0
Showing
2 changed files
with
142 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package integration_tests | ||
|
||
import ( | ||
"github.com/datastax/go-cassandra-native-protocol/client" | ||
"github.com/datastax/go-cassandra-native-protocol/frame" | ||
"github.com/datastax/go-cassandra-native-protocol/message" | ||
"github.com/datastax/go-cassandra-native-protocol/primitive" | ||
"github.com/datastax/zdm-proxy/integration-tests/setup" | ||
"github.com/stretchr/testify/require" | ||
"net" | ||
"testing" | ||
) | ||
|
||
func TestProtocolV2Basic(t *testing.T) { | ||
originAddress := "127.0.0.2" | ||
targetAddress := "127.0.0.3" | ||
|
||
serverConf := setup.NewTestConfig(originAddress, targetAddress) | ||
proxyConf := setup.NewTestConfig(originAddress, targetAddress) | ||
proxyConf.ControlConnMaxProtocolVersion = "2" | ||
|
||
testSetup, err := setup.NewCqlServerTestSetup(t, serverConf, false, false, false) | ||
require.Nil(t, err) | ||
defer testSetup.Cleanup() | ||
|
||
originRequestHandler := NewProtocolV2RequestHandler("origin", "dc1", "127.0.0.4") | ||
targetRequestHandler := NewProtocolV2RequestHandler("target", "dc1", "127.0.0.5") | ||
|
||
testSetup.Origin.CqlServer.RequestHandlers = []client.RequestHandler{ | ||
originRequestHandler.HandleRequest, | ||
client.NewDriverConnectionInitializationHandler("origin", "dc1", func(_ string) {}), | ||
} | ||
testSetup.Target.CqlServer.RequestHandlers = []client.RequestHandler{ | ||
targetRequestHandler.HandleRequest, | ||
client.NewDriverConnectionInitializationHandler("target", "dc1", func(_ string) {}), | ||
} | ||
|
||
err = testSetup.Start(nil, false, primitive.ProtocolVersion2) | ||
require.Nil(t, err) | ||
|
||
proxy, err := setup.NewProxyInstanceWithConfig(proxyConf) // starts the proxy | ||
if proxy != nil { | ||
defer proxy.Shutdown() | ||
} | ||
|
||
require.Nil(t, err) | ||
} | ||
|
||
type ProtocolV2RequestHandler struct { | ||
cluster string | ||
datacenter string | ||
peerIP string | ||
} | ||
|
||
func NewProtocolV2RequestHandler(cluster string, datacenter string, peerIP string) *ProtocolV2RequestHandler { | ||
return &ProtocolV2RequestHandler{ | ||
cluster: cluster, | ||
datacenter: datacenter, | ||
peerIP: peerIP, | ||
} | ||
} | ||
|
||
func (recv *ProtocolV2RequestHandler) HandleRequest( | ||
request *frame.Frame, | ||
conn *client.CqlServerConnection, | ||
ctx client.RequestHandlerContext) (response *frame.Frame) { | ||
switch request.Body.Message.GetOpCode() { | ||
case primitive.OpCodeStartup: | ||
case primitive.OpCodeRegister: | ||
return frame.NewFrame(request.Header.Version, request.Header.StreamId, &message.Ready{}) | ||
case primitive.OpCodeQuery: | ||
query := request.Body.Message.(*message.Query) | ||
switch query.Query { | ||
case "SELECT * FROM system.local": | ||
// C* 2.0.0 does not store local endpoint details in system.local table | ||
sysLocRow := systemLocalRow(recv.cluster, recv.datacenter, "Murmur3Partitioner", nil, request.Header.Version) | ||
sysLocMsg := &message.RowsResult{ | ||
Metadata: &message.RowsMetadata{ | ||
ColumnCount: int32(len(systemLocalColumnsV2)), | ||
Columns: systemLocalColumnsV2, | ||
}, | ||
Data: message.RowSet{sysLocRow}, | ||
} | ||
return frame.NewFrame(request.Header.Version, request.Header.StreamId, sysLocMsg) | ||
case "SELECT * FROM system.peers": | ||
sysPeerRow := systemPeersRow( | ||
recv.datacenter, | ||
&net.TCPAddr{IP: net.ParseIP(recv.peerIP), Port: 9042}, | ||
primitive.ProtocolVersion2, | ||
) | ||
sysPeeMsg := &message.RowsResult{ | ||
Metadata: &message.RowsMetadata{ | ||
ColumnCount: int32(len(systemPeersColumns)), | ||
Columns: systemPeersColumns, | ||
}, | ||
Data: message.RowSet{sysPeerRow}, | ||
} | ||
return frame.NewFrame(request.Header.Version, request.Header.StreamId, sysPeeMsg) | ||
} | ||
} | ||
return nil | ||
} |