diff --git a/core/server/api_container/server/startosis_engine/interpretation_time_value_store/service_value_repository_test.go b/core/server/api_container/server/startosis_engine/interpretation_time_value_store/service_value_repository_test.go new file mode 100644 index 0000000000..8b59d4d118 --- /dev/null +++ b/core/server/api_container/server/startosis_engine/interpretation_time_value_store/service_value_repository_test.go @@ -0,0 +1,93 @@ +package interpretation_time_value_store + +import ( + port_spec_core "github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/port_spec" + "github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/service" + "github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/database_accessors/enclave_db" + "github.com/kurtosis-tech/kurtosis/core/server/api_container/server/startosis_engine/kurtosis_types" + "github.com/kurtosis-tech/kurtosis/core/server/api_container/server/startosis_engine/kurtosis_types/port_spec" + "github.com/stretchr/testify/require" + bolt "go.etcd.io/bbolt" + "go.starlark.net/starlark" + "go.starlark.net/starlarkstruct" + "os" + "testing" +) + +const ( + starlarkThreadName = "thread-for-db-test" + serviceName = service.ServiceName("datastore-1") + serviceNameStarlarkStr = starlark.String(serviceName) + hostName = serviceNameStarlarkStr + ipAddress = starlark.String("172.23.34.44") +) + +func TestAddAndGetTest(t *testing.T) { + repository := getRecipeResultRepositoryForTest(t) + require.NotNil(t, repository) + + applicationProtocol := "" + maybeUrl := "" + + port, interpretationErr := port_spec.CreatePortSpecUsingGoValues( + "my-test-service", + uint16(443), + port_spec_core.TransportProtocol_TCP, + &applicationProtocol, + "10s", + &maybeUrl, + ) + require.Nil(t, interpretationErr) + ports := starlark.NewDict(1) + require.NoError(t, ports.SetKey(starlark.String("http"), port)) + + expectedService, interpretationErr := kurtosis_types.CreateService(serviceNameStarlarkStr, hostName, ipAddress, ports) + require.Nil(t, interpretationErr) + + err := repository.AddService(serviceName, expectedService) + require.Nil(t, err) + + actualService, err := repository.GetService(serviceName) + require.Nil(t, err) + require.Equal(t, expectedService.AttrNames(), actualService.AttrNames()) + require.Equal(t, expectedService.String(), actualService.String()) +} + +func getRecipeResultRepositoryForTest(t *testing.T) *serviceInterpretationValueRepository { + file, err := os.CreateTemp("/tmp", "*.db") + defer func() { + err = os.Remove(file.Name()) + require.NoError(t, err) + }() + + require.NoError(t, err) + db, err := bolt.Open(file.Name(), 0666, nil) + require.NoError(t, err) + enclaveDb := &enclave_db.EnclaveDB{ + DB: db, + } + + dummySerde := newDummyStarlarkValueSerDeForTest() + + repository, err := getOrCreateNewServiceInterpretationTimeValueRepository(enclaveDb, dummySerde) + require.NoError(t, err) + + return repository +} + +func newDummyStarlarkValueSerDeForTest() *kurtosis_types.StarlarkValueSerde { + thread := &starlark.Thread{ + Name: starlarkThreadName, + Print: nil, + Load: nil, + OnMaxSteps: nil, + Steps: 0, + } + starlarkEnv := starlark.StringDict{ + starlarkstruct.Default.GoString(): starlark.NewBuiltin(starlarkstruct.Default.GoString(), starlarkstruct.Make), + + kurtosis_types.ServiceTypeName: starlark.NewBuiltin(kurtosis_types.ServiceTypeName, kurtosis_types.NewServiceType().CreateBuiltin()), + port_spec.PortSpecTypeName: starlark.NewBuiltin(port_spec.PortSpecTypeName, port_spec.NewPortSpecType().CreateBuiltin()), + } + return kurtosis_types.NewStarlarkValueSerde(thread, starlarkEnv) +} diff --git a/core/server/api_container/server/startosis_engine/kurtosis_instruction/get_service/get_service.go b/core/server/api_container/server/startosis_engine/kurtosis_instruction/get_service/get_service.go index 0894b91d60..9b3048c0a0 100644 --- a/core/server/api_container/server/startosis_engine/kurtosis_instruction/get_service/get_service.go +++ b/core/server/api_container/server/startosis_engine/kurtosis_instruction/get_service/get_service.go @@ -55,6 +55,8 @@ func (builtin *GetServiceCapabilities) Interpret(_ string, arguments *builtin_ar } serviceName := service.ServiceName(serviceNameArgumentValue.GoString()) + builtin.serviceName = serviceName + serviceStarlarkValue, err := builtin.interpretationTimeStore.GetService(serviceName) if err != nil { return nil, startosis_errors.WrapWithInterpretationError(err, "an error occurred while fetching service '%v' from the store", serviceName) diff --git a/internal_testsuites/golang/testsuite/startosis_add_service_test/startosis_add_service_test.go b/internal_testsuites/golang/testsuite/startosis_add_service_test/startosis_add_service_test.go index 809a8a6c20..3ea4f6a6e7 100644 --- a/internal_testsuites/golang/testsuite/startosis_add_service_test/startosis_add_service_test.go +++ b/internal_testsuites/golang/testsuite/startosis_add_service_test/startosis_add_service_test.go @@ -36,6 +36,9 @@ def run(plan): datastore_1 = plan.add_service(name = SERVICE_NAME, config = config) datastore_2 = plan.add_service(name = SERVICE_NAME_2, config = config) + ds1_through_get = plan.get_service(SERVICE_NAME) + plan.print(ds1_through_get) + test_hostname_cmd = "nc -zv {0} {1}".format(datastore_1.hostname, GRPC_PORT) connection_result = plan.exec( recipe = ExecRecipe( @@ -67,6 +70,8 @@ func (suite *StartosisAddServiceTestSuite) TestAddTwoServicesAndTestConnection() expectedScriptOutput := `Adding services ` + serviceName + ` and ` + serviceName2 + ` Service '` + serviceName + `' added with service UUID '[a-z-0-9]+' Service '` + serviceName2 + `' added with service UUID '[a-z-0-9]+' +Fetched service '` + "" + `' +Service\(name="datastore-1", hostname="datastore-1", ip_address="[0-9\.]+", ports=\{"grpc": PortSpec\(number=1323, transport_protocol="TCP", wait="2m0s"\)\}\) Command returned with exit code '0' and the following output: -------------------- [a-z-0-9]+ \([0-9\.]+:1323\) open