Skip to content

Commit

Permalink
refactor!: rename assert to verify (#1295)
Browse files Browse the repository at this point in the history
## Description:
<!-- Describe this change, how it works, and the motivation behind it.
-->

## Is this change user facing?
YES/NO
<!-- If yes, please add the "user facing" label to the PR -->
<!-- If yes, don't forget to include docs changes where relevant -->

## References (if applicable):
<!-- Add relevant Github Issues, Discord threads, or other helpful
information. -->
  • Loading branch information
h4ck3rk3y committed Sep 18, 2023
1 parent b822870 commit 651df40
Show file tree
Hide file tree
Showing 19 changed files with 114 additions and 114 deletions.
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/startosis_engine/builtins/print_builtin"
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/startosis_engine/builtins/read_file"
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/startosis_engine/kurtosis_instruction/add_service"
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/startosis_engine/kurtosis_instruction/assert"
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/startosis_engine/kurtosis_instruction/exec"
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/startosis_engine/kurtosis_instruction/kurtosis_print"
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/startosis_engine/kurtosis_instruction/remove_service"
Expand All @@ -18,6 +17,7 @@ import (
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/startosis_engine/kurtosis_instruction/store_service_files"
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/startosis_engine/kurtosis_instruction/tasks"
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/startosis_engine/kurtosis_instruction/upload_files"
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/startosis_engine/kurtosis_instruction/verify"
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/startosis_engine/kurtosis_instruction/wait"
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/startosis_engine/kurtosis_starlark_framework/kurtosis_plan_instruction"
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/startosis_engine/kurtosis_types"
Expand Down Expand Up @@ -56,7 +56,7 @@ func KurtosisPlanInstructions(serviceNetwork service_network.ServiceNetwork, run
return []*kurtosis_plan_instruction.KurtosisPlanInstruction{
add_service.NewAddService(serviceNetwork, runtimeValueStore),
add_service.NewAddServices(serviceNetwork, runtimeValueStore),
assert.NewAssert(runtimeValueStore),
verify.NewVerify(runtimeValueStore),
exec.NewExec(serviceNetwork, runtimeValueStore),
kurtosis_print.NewPrint(serviceNetwork, runtimeValueStore),
remove_service.NewRemoveService(serviceNetwork),
Expand Down
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/service"
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/service_network"
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/startosis_engine/kurtosis_instruction/assert"
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/startosis_engine/kurtosis_instruction/verify"
"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/recipe"
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/startosis_engine/runtime_value_store"
Expand Down Expand Up @@ -75,7 +75,7 @@ func ExecuteServiceAssertionWithRecipe(
}

func assertResult(currentResult starlark.Comparable, assertion string, target starlark.Comparable) error {
err := assert.Assert(currentResult, assertion, target)
err := verify.Verify(currentResult, assertion, target)
if err != nil {
return err
}
Expand Down
@@ -1,4 +1,4 @@
package assert
package verify

import (
"context"
Expand All @@ -20,7 +20,7 @@ import (
)

const (
AssertBuiltinName = "assert"
VerifyBuiltinName = "verify"

RuntimeValueArgName = "value"
AssertionArgName = "assertion"
Expand All @@ -41,10 +41,10 @@ var StringTokenToComparisonStarlarkToken = map[string]syntax.Token{
"<": syntax.LT,
}

func NewAssert(runtimeValueStore *runtime_value_store.RuntimeValueStore) *kurtosis_plan_instruction.KurtosisPlanInstruction {
func NewVerify(runtimeValueStore *runtime_value_store.RuntimeValueStore) *kurtosis_plan_instruction.KurtosisPlanInstruction {
return &kurtosis_plan_instruction.KurtosisPlanInstruction{
KurtosisBaseBuiltin: &kurtosis_starlark_framework.KurtosisBaseBuiltin{
Name: AssertBuiltinName,
Name: VerifyBuiltinName,

Arguments: []*builtin_argument.BuiltinArgument{
{
Expand All @@ -57,7 +57,7 @@ func NewAssert(runtimeValueStore *runtime_value_store.RuntimeValueStore) *kurtos
Name: AssertionArgName,
IsOptional: false,
ZeroValueProvider: builtin_argument.ZeroValueProvider[starlark.String],
Validator: ValidateAssertionToken,
Validator: ValidateVerificationToken,
},
{
Name: TargetArgName,
Expand All @@ -69,7 +69,7 @@ func NewAssert(runtimeValueStore *runtime_value_store.RuntimeValueStore) *kurtos
},

Capabilities: func() kurtosis_plan_instruction.KurtosisPlanInstructionCapabilities {
return &AssertCapabilities{
return &VerifyCapabilities{
runtimeValueStore: runtimeValueStore,

runtimeValue: "", // populated at interpretation time
Expand All @@ -86,20 +86,20 @@ func NewAssert(runtimeValueStore *runtime_value_store.RuntimeValueStore) *kurtos
}
}

type AssertCapabilities struct {
type VerifyCapabilities struct {
runtimeValueStore *runtime_value_store.RuntimeValueStore

runtimeValue string
assertion string
target starlark.Comparable
}

func (builtin *AssertCapabilities) Interpret(_ string, arguments *builtin_argument.ArgumentValuesSet) (starlark.Value, *startosis_errors.InterpretationError) {
func (builtin *VerifyCapabilities) Interpret(_ string, arguments *builtin_argument.ArgumentValuesSet) (starlark.Value, *startosis_errors.InterpretationError) {
runtimeValue, err := builtin_argument.ExtractArgumentValue[starlark.String](arguments, RuntimeValueArgName)
if err != nil {
return nil, startosis_errors.WrapWithInterpretationError(err, "Unable to extract value for '%s' argument", RuntimeValueArgName)
}
assertion, err := builtin_argument.ExtractArgumentValue[starlark.String](arguments, AssertionArgName)
verification, err := builtin_argument.ExtractArgumentValue[starlark.String](arguments, AssertionArgName)
if err != nil {
return nil, startosis_errors.WrapWithInterpretationError(err, "Unable to extract value for '%s' argument", AssertionArgName)
}
Expand All @@ -108,7 +108,7 @@ func (builtin *AssertCapabilities) Interpret(_ string, arguments *builtin_argume
return nil, startosis_errors.WrapWithInterpretationError(err, "Unable to extract value for '%s' argument", TargetArgName)
}

builtin.assertion = assertion.GoString()
builtin.assertion = verification.GoString()
builtin.runtimeValue = runtimeValue.GoString()
builtin.target = target

Expand All @@ -118,11 +118,11 @@ func (builtin *AssertCapabilities) Interpret(_ string, arguments *builtin_argume
return starlark.None, nil
}

func (builtin *AssertCapabilities) Validate(_ *builtin_argument.ArgumentValuesSet, _ *startosis_validator.ValidatorEnvironment) *startosis_errors.ValidationError {
func (builtin *VerifyCapabilities) Validate(_ *builtin_argument.ArgumentValuesSet, _ *startosis_validator.ValidatorEnvironment) *startosis_errors.ValidationError {
return nil
}

func (builtin *AssertCapabilities) Execute(_ context.Context, _ *builtin_argument.ArgumentValuesSet) (string, error) {
func (builtin *VerifyCapabilities) Execute(_ context.Context, _ *builtin_argument.ArgumentValuesSet) (string, error) {
currentValue, err := magic_string_helper.GetOrReplaceRuntimeValueFromString(builtin.runtimeValue, builtin.runtimeValueStore)
if err != nil {
return "", err
Expand All @@ -136,44 +136,44 @@ func (builtin *AssertCapabilities) Execute(_ context.Context, _ *builtin_argumen
return "", err
}
}
err = Assert(currentValue, builtin.assertion, targetWithReplacedRuntimeValuesMaybe)
err = Verify(currentValue, builtin.assertion, targetWithReplacedRuntimeValuesMaybe)
if err != nil {
return "", err
}
instructionResult := fmt.Sprintf("Assertion succeeded. Value is '%s'.", currentValue.String())
instructionResult := fmt.Sprintf("Verification succeeded. Value is '%s'.", currentValue.String())
return instructionResult, nil
}

func (builtin *AssertCapabilities) TryResolveWith(instructionsAreEqual bool, _ *enclave_plan_persistence.EnclavePlanInstruction, _ *enclave_structure.EnclaveComponents) enclave_structure.InstructionResolutionStatus {
func (builtin *VerifyCapabilities) TryResolveWith(instructionsAreEqual bool, _ *enclave_plan_persistence.EnclavePlanInstruction, _ *enclave_structure.EnclaveComponents) enclave_structure.InstructionResolutionStatus {
if instructionsAreEqual {
return enclave_structure.InstructionIsEqual
}
return enclave_structure.InstructionIsUnknown
}

func (builtin *AssertCapabilities) FillPersistableAttributes(builder *enclave_plan_persistence.EnclavePlanInstructionBuilder) {
builder.SetType(AssertBuiltinName)
func (builtin *VerifyCapabilities) FillPersistableAttributes(builder *enclave_plan_persistence.EnclavePlanInstructionBuilder) {
builder.SetType(VerifyBuiltinName)
}

// Assert verifies whether the currentValue matches the targetValue w.r.t. the assertion operator
// TODO: This and ValidateAssertionToken below are used by both assert and wait. Refactor it to a better place
func Assert(currentValue starlark.Comparable, assertion string, targetValue starlark.Comparable) error {
// Verify verifies whether the currentValue matches the targetValue w.r.t. the assertion operator
// TODO: This and ValidateVerificationToken below are used by both verify and wait. Refactor it to a better place
func Verify(currentValue starlark.Comparable, assertion string, targetValue starlark.Comparable) error {
if comparisonToken, found := StringTokenToComparisonStarlarkToken[assertion]; found {
if currentValue.Type() != targetValue.Type() {
return stacktrace.NewError("Assert failed because '%v' is type '%v' and '%v' is type '%v'", currentValue, currentValue.Type(), targetValue, targetValue.Type())
return stacktrace.NewError("Verify failed because '%v' is type '%v' and '%v' is type '%v'", currentValue, currentValue.Type(), targetValue, targetValue.Type())
}
result, err := currentValue.CompareSameType(comparisonToken, targetValue, 1)
if err != nil {
return stacktrace.Propagate(err, "Assert comparison failed '%v' '%v' '%v'", currentValue, assertion, targetValue)
return stacktrace.Propagate(err, "Verify comparison failed '%v' '%v' '%v'", currentValue, assertion, targetValue)
}
if !result {
return stacktrace.NewError("Assertion failed '%v' '%v' '%v'", currentValue, assertion, targetValue)
return stacktrace.NewError("Verification failed '%v' '%v' '%v'", currentValue, assertion, targetValue)
}
return nil
} else if assertion == InCollectionAssertionToken || assertion == NotInCollectionAssertionToken {
iterableTarget, ok := targetValue.(starlark.Iterable)
if !ok {
return stacktrace.NewError("Assertion failed, expected an iterable object but got '%v'", targetValue.Type())
return stacktrace.NewError("Verification failed, expected an iterable object but got '%v'", targetValue.Type())
}

iterator := iterableTarget.Iterate()
Expand All @@ -192,12 +192,12 @@ func Assert(currentValue starlark.Comparable, assertion string, targetValue star
if assertion == NotInCollectionAssertionToken && !currentValuePresentInIterable {
return nil
}
return stacktrace.NewError("Assertion failed '%v' '%v' '%v'", currentValue, assertion, targetValue)
return stacktrace.NewError("Verification failed '%v' '%v' '%v'", currentValue, assertion, targetValue)
}
return stacktrace.NewError("The '%s' token '%s' seems invalid. This is a Kurtosis bug as it should have been validated earlier", AssertionArgName, assertion)
}

func ValidateAssertionToken(value starlark.Value) *startosis_errors.InterpretationError {
func ValidateVerificationToken(value starlark.Value) *startosis_errors.InterpretationError {
strValue, ok := value.(starlark.String)
if !ok {
return startosis_errors.NewInterpretationError("'%s' argument should be a 'starlark.String', got '%s'", AssertionArgName, reflect.TypeOf(value))
Expand Down
@@ -1,4 +1,4 @@
package assert
package verify

import (
"github.com/stretchr/testify/require"
Expand All @@ -10,28 +10,28 @@ func TestAssert_StringsEqual(t *testing.T) {
currentValue := starlark.String("Hello")
assertion := "=="
targetValue := starlark.String("Hello")
require.Nil(t, Assert(currentValue, assertion, targetValue))
require.Nil(t, Verify(currentValue, assertion, targetValue))
}

func TestAssert_StringsNonEqual(t *testing.T) {
currentValue := starlark.String("Hello")
assertion := "=="
targetValue := starlark.String("World")
require.NotNil(t, Assert(currentValue, assertion, targetValue))
require.NotNil(t, Verify(currentValue, assertion, targetValue))
}

func TestAssert_IntsLt(t *testing.T) {
currentValue := starlark.MakeInt(1)
assertion := "<"
targetValue := starlark.MakeInt(5)
require.Nil(t, Assert(currentValue, assertion, targetValue))
require.Nil(t, Verify(currentValue, assertion, targetValue))
}

func TestAssert_IntsGtFalse(t *testing.T) {
currentValue := starlark.MakeInt(1)
assertion := ">"
targetValue := starlark.MakeInt(5)
require.NotNil(t, Assert(currentValue, assertion, targetValue))
require.NotNil(t, Verify(currentValue, assertion, targetValue))
}

func TestAssert_ListIn(t *testing.T) {
Expand All @@ -41,7 +41,7 @@ func TestAssert_ListIn(t *testing.T) {
starlark.String("Hello"),
starlark.String("World"),
})
require.Nil(t, Assert(currentValue, assertion, targetValue))
require.Nil(t, Verify(currentValue, assertion, targetValue))
}

func TestAssert_ListInFalse(t *testing.T) {
Expand All @@ -51,7 +51,7 @@ func TestAssert_ListInFalse(t *testing.T) {
starlark.String("Hello"),
starlark.String("World"),
})
require.NotNil(t, Assert(currentValue, assertion, targetValue))
require.NotNil(t, Verify(currentValue, assertion, targetValue))
}

func TestAssert_ListNotIn(t *testing.T) {
Expand All @@ -61,7 +61,7 @@ func TestAssert_ListNotIn(t *testing.T) {
starlark.String("Hello"),
starlark.String("World"),
})
require.Nil(t, Assert(currentValue, assertion, targetValue))
require.Nil(t, Verify(currentValue, assertion, targetValue))
}

func TestAssert_ListNotInFalse(t *testing.T) {
Expand All @@ -71,7 +71,7 @@ func TestAssert_ListNotInFalse(t *testing.T) {
starlark.String("Hello"),
starlark.String("World"),
})
require.NotNil(t, Assert(currentValue, assertion, targetValue))
require.NotNil(t, Verify(currentValue, assertion, targetValue))
}

func TestAssert_InvalidToken(t *testing.T) {
Expand All @@ -81,5 +81,5 @@ func TestAssert_InvalidToken(t *testing.T) {
starlark.String("Hello"),
starlark.String("World"),
})
require.NotNil(t, Assert(currentValue, assertion, targetValue))
require.NotNil(t, Verify(currentValue, assertion, targetValue))
}
Expand Up @@ -7,8 +7,8 @@ import (
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/service_network"
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/startosis_engine/enclave_plan_persistence"
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/startosis_engine/enclave_structure"
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/startosis_engine/kurtosis_instruction/assert"
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/startosis_engine/kurtosis_instruction/shared_helpers"
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/startosis_engine/kurtosis_instruction/verify"
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/startosis_engine/kurtosis_starlark_framework"
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/startosis_engine/kurtosis_starlark_framework/builtin_argument"
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/startosis_engine/kurtosis_starlark_framework/kurtosis_plan_instruction"
Expand Down Expand Up @@ -66,7 +66,7 @@ func NewWait(serviceNetwork service_network.ServiceNetwork, runtimeValueStore *r
Name: AssertionArgName,
IsOptional: false,
ZeroValueProvider: builtin_argument.ZeroValueProvider[starlark.String],
Validator: assert.ValidateAssertionToken,
Validator: verify.ValidateVerificationToken,
},
{
Name: TargetArgName,
Expand Down Expand Up @@ -206,7 +206,7 @@ func (builtin *WaitCapabilities) Interpret(_ string, arguments *builtin_argument
return nil, startosis_errors.NewInterpretationError("An error occurred while creating return value for %v instruction", WaitBuiltinName)
}

if _, ok := builtin.target.(starlark.Iterable); (builtin.assertion == assert.InCollectionAssertionToken || builtin.assertion == assert.NotInCollectionAssertionToken) && !ok {
if _, ok := builtin.target.(starlark.Iterable); (builtin.assertion == verify.InCollectionAssertionToken || builtin.assertion == verify.NotInCollectionAssertionToken) && !ok {
return nil, startosis_errors.NewInterpretationError("'%v' assertion requires an iterable for target values, got '%v'", builtin.assertion, builtin.target.Type())
}

Expand Down
Expand Up @@ -3,7 +3,7 @@ package test_engine
import (
"fmt"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/uuid_generator"
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/startosis_engine/kurtosis_instruction/assert"
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/startosis_engine/kurtosis_instruction/verify"
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/startosis_engine/kurtosis_starlark_framework/kurtosis_plan_instruction"
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/startosis_engine/runtime_value_store"
"github.com/stretchr/testify/require"
Expand All @@ -15,45 +15,45 @@ const (
runtimeValueValue = "Hello World!"
)

type assertTestCase struct {
type verificationTestcase struct {
*testing.T

runtimeValueStore *runtime_value_store.RuntimeValueStore
runtimeValueUuid string
}

func (suite *KurtosisPlanInstructionTestSuite) TestAssert() {
func (suite *KurtosisPlanInstructionTestSuite) TestVerify() {
runtimeValueUuid, err := uuid_generator.GenerateUUIDString()
suite.Require().Nil(err)
err = suite.runtimeValueStore.SetValue(runtimeValueUuid, map[string]starlark.Comparable{
"value": starlark.String(runtimeValueValue),
})
suite.Require().NoError(err)

suite.run(&assertTestCase{
suite.run(&verificationTestcase{
T: suite.T(),
runtimeValueUuid: runtimeValueUuid,
runtimeValueStore: suite.runtimeValueStore,
})
}

func (t *assertTestCase) GetInstruction() *kurtosis_plan_instruction.KurtosisPlanInstruction {
return assert.NewAssert(t.runtimeValueStore)
func (t *verificationTestcase) GetInstruction() *kurtosis_plan_instruction.KurtosisPlanInstruction {
return verify.NewVerify(t.runtimeValueStore)
}

func (t *assertTestCase) GetStarlarkCode() string {
func (t *verificationTestcase) GetStarlarkCode() string {
runtimeValue := fmt.Sprintf("{{kurtosis:%s:value.runtime_value}}", t.runtimeValueUuid)
assertion := "=="
targetValue := runtimeValueValue
return fmt.Sprintf("%s(%s=%q, %s=%q, %s=%q)", assert.AssertBuiltinName, assert.RuntimeValueArgName, runtimeValue, assert.AssertionArgName, assertion, assert.TargetArgName, targetValue)
return fmt.Sprintf("%s(%s=%q, %s=%q, %s=%q)", verify.VerifyBuiltinName, verify.RuntimeValueArgName, runtimeValue, verify.AssertionArgName, assertion, verify.TargetArgName, targetValue)
}

func (t *assertTestCase) GetStarlarkCodeForAssertion() string {
func (t *verificationTestcase) GetStarlarkCodeForAssertion() string {
return ""
}

func (t *assertTestCase) Assert(interpretationResult starlark.Value, executionResult *string) {
func (t *verificationTestcase) Assert(interpretationResult starlark.Value, executionResult *string) {
require.Equal(t, starlark.None, interpretationResult)
expectedExecutionResult := fmt.Sprintf(`Assertion succeeded. Value is '%q'.`, runtimeValueValue)
expectedExecutionResult := fmt.Sprintf(`Verification succeeded. Value is '%q'.`, runtimeValueValue)
require.Equal(t, expectedExecutionResult, *executionResult)
}

0 comments on commit 651df40

Please sign in to comment.