Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace runner gRPC by LCD in e2e tests #1757

Merged
merged 2 commits into from
Mar 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions e2e/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,23 @@ import (
"context"
"os"
"path/filepath"
"strconv"
"testing"

"github.com/cosmos/cosmos-sdk/crypto/keys"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/mesg-foundation/engine/app"
"github.com/mesg-foundation/engine/config"
"github.com/mesg-foundation/engine/container"
"github.com/mesg-foundation/engine/cosmos"
"github.com/mesg-foundation/engine/ext/xnet"
pb "github.com/mesg-foundation/engine/protobuf/api"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
)

type apiclient struct {
pb.EventClient
pb.RunnerClient
}

var (
Expand All @@ -29,6 +31,10 @@ var (
kb *cosmos.Keybase
cfg *config.Config
engineAddress sdk.AccAddress
cont container.Container
ipfsEndpoint string
engineName string
enginePort string
)

func TestAPI(t *testing.T) {
Expand Down Expand Up @@ -58,13 +64,20 @@ func TestAPI(t *testing.T) {
engineAddress = acc.GetAddress()
}

// init runner builder
cont, err = container.New(cfg.Name)
require.NoError(t, err)
_, port, _ := xnet.SplitHostPort(cfg.Server.Address)
enginePort = strconv.Itoa(port)
engineName = cfg.Name
ipfsEndpoint = cfg.IpfsEndpoint

// init gRPC client
conn, err := grpc.DialContext(context.Background(), "localhost:50052", grpc.WithInsecure())
require.NoError(t, err)

client = apiclient{
pb.NewEventClient(conn),
pb.NewRunnerClient(conn),
}

// run tests
Expand Down
81 changes: 60 additions & 21 deletions e2e/complex_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,81 @@ import (
"context"
"testing"

"github.com/mesg-foundation/engine/ext/xos"
"github.com/mesg-foundation/engine/hash"
"github.com/mesg-foundation/engine/protobuf/acknowledgement"
pb "github.com/mesg-foundation/engine/protobuf/api"
"github.com/mesg-foundation/engine/runner/builder"
"github.com/mesg-foundation/engine/service"
runnermodule "github.com/mesg-foundation/engine/x/runner"
runnerrest "github.com/mesg-foundation/engine/x/runner/client/rest"
"github.com/stretchr/testify/require"
)

func testComplexService(t *testing.T) {
var (
testServiceHash hash.Hash
testRunnerHashC hash.Hash
testInstanceHash hash.Hash
testServiceComplexHash hash.Hash
testRunnerComplexHash hash.Hash
testInstanceComplexHash hash.Hash
testInstanceComplexEnvHash hash.Hash
testServiceComplexStruct *service.Service
testServiceComplexImageHash string
testInstanceComplexEnv []string
)

t.Run("create", func(t *testing.T) {
t.Run("create service", func(t *testing.T) {
testComplexCreateServiceMsg.Owner = engineAddress
testServiceHash = lcdBroadcastMsg(testComplexCreateServiceMsg)
testServiceComplexHash = lcdBroadcastMsg(testComplexCreateServiceMsg)
})

stream, err := client.EventClient.Stream(context.Background(), &pb.StreamEventRequest{})
require.NoError(t, err)
acknowledgement.WaitForStreamToBeReady(stream)
t.Run("get service", func(t *testing.T) {
lcdGet("service/get/"+testServiceComplexHash.String(), &testServiceComplexStruct)
require.Equal(t, testServiceComplexHash, testServiceComplexStruct.Hash)
})
testInstanceComplexEnv = xos.EnvMergeSlices(testServiceComplexStruct.Configuration.Env, []string{"ENVB=is_override"})

t.Run("run", func(t *testing.T) {
resp, err := client.RunnerClient.Create(context.Background(), &pb.CreateRunnerRequest{
ServiceHash: testServiceHash,
Env: []string{"ENVB=is_override"},
})
require.NoError(t, err)
testRunnerHashC = resp.Hash
t.Run("get runner hashes", func(t *testing.T) {
var res runnerrest.HashResponse
lcdPost("runner/hash", &runnerrest.HashRequest{
ServiceHash: testServiceComplexHash,
Address: engineAddress.String(),
Env: testInstanceComplexEnv,
}, &res)
testRunnerComplexHash = res.RunnerHash
testInstanceComplexHash = res.InstanceHash
testInstanceComplexEnvHash = res.EnvHash
})

resp1, err := client.RunnerClient.Get(context.Background(), &pb.GetRunnerRequest{Hash: testRunnerHashC})
t.Run("build service image", func(t *testing.T) {
var err error
testServiceComplexImageHash, err = builder.Build(cont, testServiceComplexStruct, ipfsEndpoint)
require.NoError(t, err)
testInstanceHash = resp1.InstanceHash
})

t.Run("start runner", func(t *testing.T) {
require.NoError(t, builder.Start(cont, testServiceComplexStruct, testInstanceComplexHash, testRunnerComplexHash, testServiceComplexImageHash, testInstanceComplexEnv, engineName, enginePort))
})

t.Run("register runner", func(t *testing.T) {
msg := runnermodule.MsgCreate{
Owner: engineAddress,
ServiceHash: testServiceComplexHash,
EnvHash: testInstanceComplexEnvHash,
}
require.True(t, testRunnerComplexHash.Equal(lcdBroadcastMsg(msg)))
})

stream, err := client.EventClient.Stream(context.Background(), &pb.StreamEventRequest{})
require.NoError(t, err)
acknowledgement.WaitForStreamToBeReady(stream)

t.Run("check events", func(t *testing.T) {
okEventsNo := 6
for i := 0; i < okEventsNo; {
ev, err := stream.Recv()
require.NoError(t, err)

if !ev.InstanceHash.Equal(testInstanceHash) {
if !ev.InstanceHash.Equal(testInstanceComplexHash) {
continue
}
i++
Expand All @@ -60,8 +93,14 @@ func testComplexService(t *testing.T) {
})

t.Run("delete", func(t *testing.T) {
t.Skip("FIXME: this call never get trough. some issue with the service's dependency")
_, err := client.RunnerClient.Delete(context.Background(), &pb.DeleteRunnerRequest{Hash: testRunnerHashC})
require.NoError(t, err)
t.Skip("FIXME: this test timeout on CIRCLE CI. works well on local computer")
msg := runnermodule.MsgDelete{
Owner: engineAddress,
Hash: testRunnerComplexHash,
}

lcdBroadcastMsg(msg)

require.NoError(t, builder.Stop(cont, testRunnerComplexHash, testServiceComplexStruct.Dependencies))
})
}
6 changes: 3 additions & 3 deletions e2e/definition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package main

import (
"github.com/mesg-foundation/engine/service"
serviceModule "github.com/mesg-foundation/engine/x/service"
servicemodule "github.com/mesg-foundation/engine/x/service"
)

var testComplexCreateServiceMsg = &serviceModule.MsgCreate{
var testComplexCreateServiceMsg = &servicemodule.MsgCreate{
Sid: "test-complex-service",
Name: "test-complex-service",
Dependencies: []*service.Service_Dependency{
Expand Down Expand Up @@ -39,7 +39,7 @@ var testComplexCreateServiceMsg = &serviceModule.MsgCreate{
Source: "QmSuVcdic2dhS5QKQGWp66SJQUkDRqAqCHpU6Sx9uXJcdc",
}

var testCreateServiceMsg = &serviceModule.MsgCreate{
var testCreateServiceMsg = &servicemodule.MsgCreate{
Sid: "test-service",
Name: "test-service",
Configuration: service.Service_Configuration{
Expand Down
123 changes: 58 additions & 65 deletions e2e/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,51 @@ import (
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/mesg-foundation/engine/ext/xos"
"github.com/mesg-foundation/engine/hash"
"github.com/mesg-foundation/engine/protobuf/acknowledgement"
pb "github.com/mesg-foundation/engine/protobuf/api"
"github.com/mesg-foundation/engine/runner"
"github.com/mesg-foundation/engine/runner/builder"
runnermodule "github.com/mesg-foundation/engine/x/runner"
runnerrest "github.com/mesg-foundation/engine/x/runner/client/rest"
"github.com/stretchr/testify/require"
)

var testRunnerHash hash.Hash
var testRunnerAddress sdk.AccAddress
var (
testRunnerHash hash.Hash
testInstanceEnvHash hash.Hash
testRunnerAddress sdk.AccAddress
testServiceImageHash string
)

func testRunner(t *testing.T) {
t.Run("create", func(t *testing.T) {
var (
testInstanceEnv = xos.EnvMergeSlices(testServiceStruct.Configuration.Env, []string{"BAR=3", "REQUIRED=4"})
)
t.Run("hash", func(t *testing.T) {
var res runnerrest.HashResponse
lcdPost("runner/hash", &runnerrest.HashRequest{
ServiceHash: testServiceHash,
Address: engineAddress.String(),
Env: testInstanceEnv,
}, &res)
testRunnerHash = res.RunnerHash
testInstanceHash = res.InstanceHash
testInstanceEnvHash = res.EnvHash
})

t.Run("build service image", func(t *testing.T) {
var err error
testServiceImageHash, err = builder.Build(cont, testServiceStruct, ipfsEndpoint)
require.NoError(t, err)
})

t.Run("start", func(t *testing.T) {
require.NoError(t, builder.Start(cont, testServiceStruct, testInstanceHash, testRunnerHash, testServiceImageHash, testInstanceEnv, engineName, enginePort))
})

t.Run("register", func(t *testing.T) {
stream, err := client.EventClient.Stream(context.Background(), &pb.StreamEventRequest{
Filter: &pb.StreamEventRequest_Filter{
Key: "test_service_ready",
Expand All @@ -26,88 +58,49 @@ func testRunner(t *testing.T) {
require.NoError(t, err)
acknowledgement.WaitForStreamToBeReady(stream)

resp, err := client.RunnerClient.Create(context.Background(), &pb.CreateRunnerRequest{
msg := runnermodule.MsgCreate{
Owner: engineAddress,
ServiceHash: testServiceHash,
Env: []string{"BAR=3", "REQUIRED=4"},
})
require.NoError(t, err)
testRunnerHash = resp.Hash
EnvHash: testInstanceEnvHash,
}
require.True(t, testRunnerHash.Equal(lcdBroadcastMsg(msg)))

// wait for service to be ready
_, err = stream.Recv()
require.NoError(t, err)
})

t.Run("recreate", func(t *testing.T) {
_, err := client.RunnerClient.Delete(context.Background(), &pb.DeleteRunnerRequest{Hash: testRunnerHash})
require.NoError(t, err)
resp, err := client.RunnerClient.Create(context.Background(), &pb.CreateRunnerRequest{
ServiceHash: testServiceHash,
Env: []string{"BAR=3", "REQUIRED=4"},
})
require.NoError(t, err)
testRunnerHash = resp.Hash
})

t.Run("get", func(t *testing.T) {
t.Run("grpc", func(t *testing.T) {
resp, err := client.RunnerClient.Get(context.Background(), &pb.GetRunnerRequest{Hash: testRunnerHash})
require.NoError(t, err)
require.Equal(t, testRunnerHash, resp.Hash)
testInstanceHash = resp.InstanceHash
})
t.Run("lcd", func(t *testing.T) {
var r *runner.Runner
lcdGet("runner/get/"+testRunnerHash.String(), &r)
require.Equal(t, testRunnerHash, r.Hash)
testRunnerAddress = r.Address
})
var run *runner.Runner
lcdGet("runner/get/"+testRunnerHash.String(), &run)
require.Equal(t, testRunnerHash, run.Hash)
testRunnerAddress = run.Address
})

// TODO: need to test the filters
t.Run("list", func(t *testing.T) {
t.Run("grpc", func(t *testing.T) {
resp, err := client.RunnerClient.List(context.Background(), &pb.ListRunnerRequest{})
require.NoError(t, err)
require.Len(t, resp.Runners, 1)
require.Equal(t, testInstanceHash, resp.Runners[0].InstanceHash)
require.Equal(t, testRunnerHash, resp.Runners[0].Hash)
})
t.Run("lcd", func(t *testing.T) {
rs := make([]*runner.Runner, 0)
lcdGet("runner/list", &rs)
require.Len(t, rs, 1)
require.Equal(t, testInstanceHash, rs[0].InstanceHash)
require.Equal(t, testRunnerHash, rs[0].Hash)
})
})

t.Run("hash", func(t *testing.T) {
var res runnerrest.HashResponse
lcdPost("runner/hash", &runnerrest.HashRequest{
ServiceHash: testServiceHash,
Address: engineAddress.String(),
Env: []string{"BAR=3", "REQUIRED=4"},
}, &res)
require.Equal(t, testRunnerHash, res.RunnerHash)
require.Equal(t, testInstanceHash, res.InstanceHash)
rs := make([]*runner.Runner, 0)
lcdGet("runner/list", &rs)
require.Len(t, rs, 1)
require.Equal(t, testInstanceHash, rs[0].InstanceHash)
require.Equal(t, testRunnerHash, rs[0].Hash)
})
}

func testDeleteRunner(t *testing.T) {
_, err := client.RunnerClient.Delete(context.Background(), &pb.DeleteRunnerRequest{Hash: testRunnerHash})
require.NoError(t, err)
msg := runnermodule.MsgDelete{
Owner: engineAddress,
Hash: testRunnerHash,
}
lcdBroadcastMsg(msg)

t.Run("grpc", func(t *testing.T) {
resp, err := client.RunnerClient.List(context.Background(), &pb.ListRunnerRequest{})
require.NoError(t, err)
require.Len(t, resp.Runners, 0)
})
t.Run("lcd", func(t *testing.T) {
require.NoError(t, builder.Stop(cont, testRunnerHash, testServiceStruct.Dependencies))

t.Run("check deletion", func(t *testing.T) {
rs := make([]*runner.Runner, 0)
lcdGet("runner/list", &rs)
require.Len(t, rs, 0)
})

t.Run("check coins on runner", func(t *testing.T) {
var coins sdk.Coins
lcdGet("bank/balances/"+testRunnerAddress.String(), &coins)
Expand Down
8 changes: 4 additions & 4 deletions e2e/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
var (
testServiceHash hash.Hash
testServiceAddress sdk.AccAddress
testServiceStruct *service.Service
)

func testService(t *testing.T) {
Expand All @@ -23,10 +24,9 @@ func testService(t *testing.T) {
})

t.Run("get", func(t *testing.T) {
var s *service.Service
lcdGet("service/get/"+testServiceHash.String(), &s)
require.Equal(t, testServiceHash, s.Hash)
testServiceAddress = s.Address
lcdGet("service/get/"+testServiceHash.String(), &testServiceStruct)
require.Equal(t, testServiceHash, testServiceStruct.Hash)
testServiceAddress = testServiceStruct.Address
})

t.Run("list", func(t *testing.T) {
Expand Down
Loading