Skip to content

Commit

Permalink
added the get_service instruction
Browse files Browse the repository at this point in the history
  • Loading branch information
h4ck3rk3y committed Feb 28, 2024
1 parent e5ac92a commit 5e73caf
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 5e73caf

Please sign in to comment.