/
startosis_stop_service_test.go
126 lines (103 loc) · 4.68 KB
/
startosis_stop_service_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package startosis_stop_service_test
import (
"context"
"fmt"
"testing"
"github.com/kurtosis-tech/kurtosis-cli/golang_internal_testsuite/test_helpers"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/require"
)
const (
testName = "starlark-stop-service"
serviceName = "example-datastore-stop-service"
portId = "grpc"
starlarkScript = `
DATASTORE_IMAGE = "kurtosistech/example-datastore-server"
DATASTORE_SERVICE_NAME = "` + serviceName + `"
DATASTORE_PORT_ID = "` + portId + `"
DATASTORE_PORT_NUMBER = 1323
DATASTORE_PORT_PROTOCOL = "TCP"
def run(plan):
plan.print("Adding service " + DATASTORE_SERVICE_NAME + ".")
config = ServiceConfig(
image = DATASTORE_IMAGE,
ports = {
DATASTORE_PORT_ID: PortSpec(number = DATASTORE_PORT_NUMBER, transport_protocol = DATASTORE_PORT_PROTOCOL)
}
)
plan.add_service(name = DATASTORE_SERVICE_NAME, config = config)
plan.print("Service " + DATASTORE_SERVICE_NAME + " deployed successfully.")
`
// We stop the service we created through the script above with a different script
stopScript = `
DATASTORE_SERVICE_NAME = "` + serviceName + `"
def run(plan):
plan.stop_service(DATASTORE_SERVICE_NAME)
plan.print("Service stopped")
`
stopScript2 = `
DATASTORE_SERVICE_NAME = "` + serviceName + `"
def run(plan):
plan.stop_service(DATASTORE_SERVICE_NAME)
plan.print("Service stopped once again")
`
)
func TestStartosis(t *testing.T) {
ctx := context.Background()
// ------------------------------------- ENGINE SETUP ----------------------------------------------
enclaveCtx, _, destroyEnclaveFunc, err := test_helpers.CreateEnclave(t, ctx, testName)
require.NoError(t, err, "An error occurred creating an enclave")
defer func() {
destroyErr := destroyEnclaveFunc()
if destroyErr != nil {
logrus.Errorf("Error destroying enclave at the end of integration test '%s'",
testName)
}
}()
// ------------------------------------- TEST RUN ----------------------------------------------
logrus.Infof("Executing Starlark script to first add the datastore service...")
logrus.Debugf("Starlark script content: \n%v", starlarkScript)
runResult, err := test_helpers.RunScriptWithDefaultConfig(ctx, enclaveCtx, starlarkScript)
require.NoError(t, err, "Unexpected error executing Starlark script")
require.Nil(t, runResult.InterpretationError, "Unexpected interpretation error. This test requires you to be online for the read_file command to run")
require.Empty(t, runResult.ValidationErrors, "Unexpected validation error")
require.Nil(t, runResult.ExecutionError, "Unexpected execution error")
expectedScriptOutput := `Adding service ` + serviceName + `.
Service '` + serviceName + `' added with service UUID '[a-z-0-9]+'
Service ` + serviceName + ` deployed successfully.
`
require.Regexp(t, expectedScriptOutput, string(runResult.RunOutput))
logrus.Infof("Successfully ran Starlark script to add datastore service")
// Check that the service added by the script is functional
logrus.Infof("Checking that services are all healthy")
require.NoError(
t,
test_helpers.ValidateDatastoreServiceHealthy(context.Background(), enclaveCtx, serviceName, portId),
"Error validating datastore server '%s' is healthy",
serviceName,
)
logrus.Infof("Validated that all services are healthy")
// we run the stop script and validate that the service is unreachable.
runResult, err = test_helpers.RunScriptWithDefaultConfig(ctx, enclaveCtx, stopScript)
require.NoError(t, err, "Unexpected error executing stop script")
require.Nil(t, runResult.InterpretationError, "Unexpected interpretation error")
require.Empty(t, runResult.ValidationErrors, "Unexpected validation error")
require.Nil(t, runResult.ExecutionError, "Unexpected execution error")
expectedScriptOutput = `Service '` + serviceName + `' stopped
`
require.Regexp(t, expectedScriptOutput, string(runResult.RunOutput))
require.Error(
t,
test_helpers.ValidateDatastoreServiceHealthy(context.Background(), enclaveCtx, serviceName, portId),
"Error validating datastore server '%s' is not healthy",
serviceName,
)
logrus.Infof("Validated that the service is stopped")
// we run the stop script one more time and validate that an error is returned since the service is already stopped.
runResult, _ = test_helpers.RunScriptWithDefaultConfig(ctx, enclaveCtx, stopScript2)
require.Nil(t, runResult.InterpretationError, "Unexpected interpretation error")
require.Empty(t, runResult.ValidationErrors, "Unexpected validation error")
require.NotEmpty(t, runResult.ExecutionError, "Expected execution error coming from already stopped service")
expectedErrorStr := fmt.Sprintf("Service '%v' is already stopped", serviceName)
require.Contains(t, runResult.ExecutionError.ErrorMessage, expectedErrorStr)
}