Update proto client naming

This commit is contained in:
katexochen 2022-04-20 15:17:33 +02:00 committed by Paul Meyer
parent ddf94c7373
commit c08787ce80
4 changed files with 42 additions and 44 deletions

View File

@ -14,12 +14,10 @@ import (
"google.golang.org/grpc/credentials"
)
// Client wraps a AVPNClient and the connection to it.
// The client offers a method to activate the connected
// AVPNServer as Coordinator.
// Client wraps a PubAPI client and the connection to it.
type Client struct {
conn *grpc.ClientConn
avpn pubproto.APIClient
conn *grpc.ClientConn
pubapi pubproto.APIClient
}
// Connect connects the client to a given server, using the handed
@ -43,7 +41,7 @@ func (c *Client) Connect(ip, port string, validators []atls.Validator) error {
c.conn.Close()
}
c.conn = conn
c.avpn = pubproto.NewAPIClient(conn)
c.pubapi = pubproto.NewAPIClient(conn)
return nil
}
@ -65,7 +63,7 @@ func (c *Client) Close() error {
// The handed IP addresses must be the private IP addresses of running AWS or GCP instances,
// and the userPublicKey is the VPN key of the users WireGuard interface.
func (c *Client) Activate(ctx context.Context, userPublicKey, masterSecret []byte, nodeIPs, coordinatorIPs, autoscalingNodeGroups []string, cloudServiceAccountURI string) (ActivationResponseClient, error) {
if c.avpn == nil {
if c.pubapi == nil {
return nil, errors.New("client is not connected")
}
if len(userPublicKey) == 0 {
@ -80,7 +78,7 @@ func (c *Client) Activate(ctx context.Context, userPublicKey, masterSecret []byt
return nil, err
}
avpnRequest := &pubproto.ActivateAsCoordinatorRequest{
req := &pubproto.ActivateAsCoordinatorRequest{
AdminVpnPubKey: pubKey[:],
NodePublicIps: nodeIPs,
CoordinatorPublicIps: coordinatorIPs,
@ -93,7 +91,7 @@ func (c *Client) Activate(ctx context.Context, userPublicKey, masterSecret []byt
CloudServiceAccountUri: cloudServiceAccountURI,
}
client, err := c.avpn.ActivateAsCoordinator(ctx, avpnRequest)
client, err := c.pubapi.ActivateAsCoordinator(ctx, req)
if err != nil {
return nil, err
}

View File

@ -70,42 +70,42 @@ func TestActivate(t *testing.T) {
someErr := errors.New("failed")
testCases := map[string]struct {
avpn *stubAVPNClient
pubAPIClient *stubPubAPIClient
userPublicKey string
ips []string
wantErr bool
}{
"normal activation": {
avpn: &stubAVPNClient{},
pubAPIClient: &stubPubAPIClient{},
userPublicKey: testKey,
ips: []string{"192.0.2.1", "192.0.2.1", "192.0.2.1"},
wantErr: false,
},
"client without avpn": {
"client without pubAPIClient": {
userPublicKey: testKey,
ips: []string{"192.0.2.1", "192.0.2.1", "192.0.2.1"},
wantErr: true,
},
"empty public key parameter": {
avpn: &stubAVPNClient{},
pubAPIClient: &stubPubAPIClient{},
userPublicKey: "",
ips: []string{"192.0.2.1", "192.0.2.1", "192.0.2.1"},
wantErr: true,
},
"invalid public key parameter": {
avpn: &stubAVPNClient{},
pubAPIClient: &stubPubAPIClient{},
userPublicKey: "invalid Key",
ips: []string{"192.0.2.1", "192.0.2.1", "192.0.2.1"},
wantErr: true,
},
"empty ips parameter": {
avpn: &stubAVPNClient{},
pubAPIClient: &stubPubAPIClient{},
userPublicKey: testKey,
ips: []string{},
wantErr: true,
},
"fail ActivateAsCoordinator": {
avpn: &stubAVPNClient{activateAsCoordinatorErr: someErr},
pubAPIClient: &stubPubAPIClient{activateAsCoordinatorErr: someErr},
userPublicKey: testKey,
ips: []string{"192.0.2.1", "192.0.2.1", "192.0.2.1"},
wantErr: true,
@ -117,24 +117,24 @@ func TestActivate(t *testing.T) {
assert := assert.New(t)
client := Client{}
if tc.avpn != nil {
client.avpn = tc.avpn
if tc.pubAPIClient != nil {
client.pubapi = tc.pubAPIClient
}
_, err := client.Activate(context.Background(), []byte(tc.userPublicKey), []byte("Constellation"), tc.ips, nil, nil, "serviceaccount://test")
if tc.wantErr {
assert.Error(err)
} else {
assert.NoError(err)
assert.Equal("32bytesWireGuardKeyForTheTesting", string(tc.avpn.activateAsCoordinatorReqKey))
assert.Equal(tc.ips, tc.avpn.activateAsCoordinatorReqIPs)
assert.Equal("Constellation", string(tc.avpn.activateAsCoordinatorMasterSecret))
assert.Equal("serviceaccount://test", tc.avpn.activateCloudServiceAccountURI)
assert.Equal("32bytesWireGuardKeyForTheTesting", string(tc.pubAPIClient.activateAsCoordinatorReqKey))
assert.Equal(tc.ips, tc.pubAPIClient.activateAsCoordinatorReqIPs)
assert.Equal("Constellation", string(tc.pubAPIClient.activateAsCoordinatorMasterSecret))
assert.Equal("serviceaccount://test", tc.pubAPIClient.activateCloudServiceAccountURI)
}
})
}
}
type stubAVPNClient struct {
type stubPubAPIClient struct {
activateAsCoordinatorErr error
activateAdditionalNodesErr error
activateAsCoordinatorReqKey []byte
@ -145,19 +145,19 @@ type stubAVPNClient struct {
pubproto.APIClient
}
func (s *stubAVPNClient) ActivateAsCoordinator(ctx context.Context, in *pubproto.ActivateAsCoordinatorRequest,
func (s *stubPubAPIClient) ActivateAsCoordinator(ctx context.Context, in *pubproto.ActivateAsCoordinatorRequest,
opts ...grpc.CallOption,
) (pubproto.API_ActivateAsCoordinatorClient, error) {
s.activateAsCoordinatorReqKey = in.AdminVpnPubKey
s.activateAsCoordinatorReqIPs = in.NodePublicIps
s.activateAsCoordinatorMasterSecret = in.MasterSecret
s.activateCloudServiceAccountURI = in.CloudServiceAccountUri
return dummyAVPNActivateAsCoordinatorClient{}, s.activateAsCoordinatorErr
return dummyActivateAsCoordinatorClient{}, s.activateAsCoordinatorErr
}
func (s *stubAVPNClient) ActivateAdditionalNodes(ctx context.Context, in *pubproto.ActivateAdditionalNodesRequest,
func (s *stubPubAPIClient) ActivateAdditionalNodes(ctx context.Context, in *pubproto.ActivateAdditionalNodesRequest,
opts ...grpc.CallOption,
) (pubproto.API_ActivateAdditionalNodesClient, error) {
s.activateAdditionalNodesReqIPs = in.NodePublicIps
return dummyAVPNActivateAdditionalNodesClient{}, s.activateAdditionalNodesErr
return dummyActivateAdditionalNodesClient{}, s.activateAdditionalNodesErr
}

View File

@ -11,7 +11,7 @@ import (
)
// ActivationRespClient has methods to read messages from a stream of
// ActivateAsCoordinatorResponses. It wraps an AVPN_ActivateAsCoordinatorClient.
// ActivateAsCoordinatorResponses. It wraps an API_ActivateAsCoordinatorClient.
type ActivationRespClient struct {
client pubproto.API_ActivateAsCoordinatorClient
kubeconfig string
@ -22,7 +22,7 @@ type ActivationRespClient struct {
}
// NewActivationRespClient creates a new ActivationRespClient with the handed
// AVPN_ActivateAsCoordinatorClient.
// API_ActivateAsCoordinatorClient.
func NewActivationRespClient(client pubproto.API_ActivateAsCoordinatorClient) *ActivationRespClient {
return &ActivationRespClient{
client: client,

View File

@ -11,34 +11,34 @@ import (
"google.golang.org/grpc"
)
// dummyAVPNActivateAsCoordinatorClient is a dummy and panics if Recv() is called.
type dummyAVPNActivateAsCoordinatorClient struct {
// dummyActivateAsCoordinatorClient is a dummy and panics if Recv() is called.
type dummyActivateAsCoordinatorClient struct {
grpc.ClientStream
}
func (c dummyAVPNActivateAsCoordinatorClient) Recv() (*pubproto.ActivateAsCoordinatorResponse, error) {
func (c dummyActivateAsCoordinatorClient) Recv() (*pubproto.ActivateAsCoordinatorResponse, error) {
panic("i'm a dummy, Recv() not implemented")
}
// dummyAVPNActivateAsCoordinatorClient is a dummy and panics if Recv() is called.
type dummyAVPNActivateAdditionalNodesClient struct {
// dummyActivateAsCoordinatorClient is a dummy and panics if Recv() is called.
type dummyActivateAdditionalNodesClient struct {
grpc.ClientStream
}
func (c dummyAVPNActivateAdditionalNodesClient) Recv() (*pubproto.ActivateAdditionalNodesResponse, error) {
func (c dummyActivateAdditionalNodesClient) Recv() (*pubproto.ActivateAdditionalNodesResponse, error) {
panic("i'm a dummy, Recv() not implemented")
}
// stubAVPNActivationAsCoordinatorClient recives responses from an predefined
// stubActivationAsCoordinatorClient recives responses from an predefined
// response stream iterator or a stub error.
type stubAVPNActivationAsCoordinatorClient struct {
type stubActivationAsCoordinatorClient struct {
grpc.ClientStream
stream *stubActivateAsCoordinatorResponseIter
recvErr error
}
func (c stubAVPNActivationAsCoordinatorClient) Recv() (*pubproto.ActivateAsCoordinatorResponse, error) {
func (c stubActivationAsCoordinatorClient) Recv() (*pubproto.ActivateAsCoordinatorResponse, error) {
if c.recvErr != nil {
return nil, c.recvErr
}
@ -122,7 +122,7 @@ func TestNextLog(t *testing.T) {
t.Run(name, func(t *testing.T) {
assert := assert.New(t)
respClient := stubAVPNActivationAsCoordinatorClient{
respClient := stubActivationAsCoordinatorClient{
stream: &stubActivateAsCoordinatorResponseIter{
msgs: tc.msgs,
},
@ -180,7 +180,7 @@ func TestPrintLogStream(t *testing.T) {
},
})
}
respClient := stubAVPNActivationAsCoordinatorClient{
respClient := stubActivationAsCoordinatorClient{
stream: &stubActivateAsCoordinatorResponseIter{
msgs: msgs,
},
@ -194,7 +194,7 @@ func TestPrintLogStream(t *testing.T) {
// Check error handling.
//
someErr := errors.New("failed")
respClient = stubAVPNActivationAsCoordinatorClient{
respClient = stubActivationAsCoordinatorClient{
recvErr: someErr,
}
client = NewActivationRespClient(respClient)
@ -204,7 +204,7 @@ func TestPrintLogStream(t *testing.T) {
func TestGetKubeconfig(t *testing.T) {
assert := assert.New(t)
client := NewActivationRespClient(dummyAVPNActivateAsCoordinatorClient{})
client := NewActivationRespClient(dummyActivateAsCoordinatorClient{})
_, err := client.GetKubeconfig()
assert.Error(err)
@ -217,7 +217,7 @@ func TestGetKubeconfig(t *testing.T) {
func TestGetCoordinatorVpnKey(t *testing.T) {
assert := assert.New(t)
client := NewActivationRespClient(dummyAVPNActivateAsCoordinatorClient{})
client := NewActivationRespClient(dummyActivateAsCoordinatorClient{})
_, err := client.GetCoordinatorVpnKey()
assert.Error(err)
@ -230,7 +230,7 @@ func TestGetCoordinatorVpnKey(t *testing.T) {
func TestGetClientVpnIp(t *testing.T) {
assert := assert.New(t)
client := NewActivationRespClient(dummyAVPNActivateAsCoordinatorClient{})
client := NewActivationRespClient(dummyActivateAsCoordinatorClient{})
_, err := client.GetClientVpnIp()
assert.Error(err)