This repository has been archived by the owner on Sep 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement the shipper gRPC server (#76)
All the endpoints are actually functioning now. Following use-cases are implemented: * Publish a full batch of events * Publish a batch partially based on the queue capacity * Return an error if the UUID of server does not match the client request * Request or subscribe to persisted index change
- Loading branch information
Showing
8 changed files
with
585 additions
and
160 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
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
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,112 @@ | ||
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
// or more contributor license agreements. Licensed under the Elastic License; | ||
// you may not use this file except in compliance with the Elastic License. | ||
|
||
package server | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"sync" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/credentials/insecure" | ||
|
||
"github.com/elastic/elastic-agent-client/v7/pkg/client" | ||
"github.com/elastic/elastic-agent-client/v7/pkg/client/mock" | ||
"github.com/elastic/elastic-agent-client/v7/pkg/proto" | ||
) | ||
|
||
func TestAgentControl(t *testing.T) { | ||
unitOneID := mock.NewID() | ||
|
||
token := mock.NewID() | ||
var gotConfig, gotHealthy, gotStopped bool | ||
|
||
var mut sync.Mutex | ||
|
||
t.Logf("Creating mock server") | ||
srv := mock.StubServerV2{ | ||
CheckinV2Impl: func(observed *proto.CheckinObserved) *proto.CheckinExpected { | ||
mut.Lock() | ||
defer mut.Unlock() | ||
if observed.Token == token { | ||
if len(observed.Units) > 0 { | ||
t.Logf("Current unit state is: %v", observed.Units[0].State) | ||
} | ||
|
||
// initial checkin | ||
if len(observed.Units) == 0 || observed.Units[0].State == proto.State_STARTING { | ||
gotConfig = true | ||
return &proto.CheckinExpected{ | ||
Units: []*proto.UnitExpected{ | ||
{ | ||
Id: unitOneID, | ||
Type: proto.UnitType_OUTPUT, | ||
ConfigStateIdx: 1, | ||
Config: `{"logging": {"level": "debug"}}`, // hack to make my life easier | ||
State: proto.State_HEALTHY, | ||
}, | ||
}, | ||
} | ||
} else if observed.Units[0].State == proto.State_HEALTHY { | ||
gotHealthy = true | ||
//shutdown | ||
return &proto.CheckinExpected{ | ||
Units: []*proto.UnitExpected{ | ||
{ | ||
Id: unitOneID, | ||
Type: proto.UnitType_OUTPUT, | ||
ConfigStateIdx: 1, | ||
Config: "{}", | ||
State: proto.State_STOPPED, | ||
}, | ||
}, | ||
} | ||
} else if observed.Units[0].State == proto.State_STOPPED { | ||
gotStopped = true | ||
// remove the unit? I think? | ||
return &proto.CheckinExpected{ | ||
Units: nil, | ||
} | ||
} | ||
|
||
} | ||
|
||
//gotInvalid = true | ||
return nil | ||
}, | ||
ActionImpl: func(response *proto.ActionResponse) error { | ||
|
||
return nil | ||
}, | ||
ActionsChan: make(chan *mock.PerformAction, 100), | ||
} // end of srv declaration | ||
|
||
require.NoError(t, srv.Start()) | ||
defer srv.Stop() | ||
|
||
t.Logf("creating client") | ||
// connect with client | ||
validClient := client.NewV2(fmt.Sprintf(":%d", srv.Port), token, client.VersionInfo{ | ||
Name: "program", | ||
Version: "v1.0.0", | ||
Meta: map[string]string{ | ||
"key": "value", | ||
}, | ||
}, grpc.WithTransportCredentials(insecure.NewCredentials())) | ||
|
||
t.Logf("starting shipper controller") | ||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10) | ||
defer cancel() | ||
err := runController(ctx, validClient) | ||
assert.NoError(t, err) | ||
|
||
assert.True(t, gotConfig, "config state") | ||
assert.True(t, gotHealthy, "healthy state") | ||
assert.True(t, gotStopped, "stopped state") | ||
} |
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
Oops, something went wrong.