|
| 1 | +package dataproxy |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "connectrpc.com/connect" |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + "github.com/stretchr/testify/mock" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + "google.golang.org/protobuf/proto" |
| 12 | + |
| 13 | + "github.com/flyteorg/flyte/v2/flytestdlib/storage" |
| 14 | + storageMocks "github.com/flyteorg/flyte/v2/flytestdlib/storage/mocks" |
| 15 | + "github.com/flyteorg/flyte/v2/gen/go/flyteidl2/common" |
| 16 | + "github.com/flyteorg/flyte/v2/gen/go/flyteidl2/core" |
| 17 | + "github.com/flyteorg/flyte/v2/gen/go/flyteidl2/task" |
| 18 | + "github.com/flyteorg/flyte/v2/gen/go/flyteidl2/workflow" |
| 19 | + workflowMocks "github.com/flyteorg/flyte/v2/gen/go/flyteidl2/workflow/workflowconnect/mocks" |
| 20 | +) |
| 21 | + |
| 22 | +func testActionID() *common.ActionIdentifier { |
| 23 | + return &common.ActionIdentifier{ |
| 24 | + Run: &common.RunIdentifier{ |
| 25 | + Org: "org", |
| 26 | + Project: "proj", |
| 27 | + Domain: "dev", |
| 28 | + Name: "run1", |
| 29 | + }, |
| 30 | + Name: "a0", |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +func testNamedLiterals() []*task.NamedLiteral { |
| 35 | + return []*task.NamedLiteral{ |
| 36 | + { |
| 37 | + Name: "test", |
| 38 | + Value: &core.Literal{ |
| 39 | + Value: &core.Literal_Scalar{ |
| 40 | + Scalar: &core.Scalar{ |
| 41 | + Value: &core.Scalar_Primitive{ |
| 42 | + Primitive: &core.Primitive{ |
| 43 | + Value: &core.Primitive_StringValue{StringValue: "hello world"}, |
| 44 | + }, |
| 45 | + }, |
| 46 | + }, |
| 47 | + }, |
| 48 | + }, |
| 49 | + }, |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +func testVariableMap() *core.VariableMap { |
| 54 | + return &core.VariableMap{ |
| 55 | + Variables: []*core.VariableEntry{ |
| 56 | + { |
| 57 | + Key: "test", |
| 58 | + Value: &core.Variable{ |
| 59 | + Type: &core.LiteralType{ |
| 60 | + Type: &core.LiteralType_Simple{Simple: core.SimpleType_STRING}, |
| 61 | + }, |
| 62 | + }, |
| 63 | + }, |
| 64 | + }, |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +func assertHelloWorldSchema(t *testing.T, resp *connect.Response[workflow.LiteralsToLaunchFormJsonResponse]) { |
| 69 | + t.Helper() |
| 70 | + schema := resp.Msg.GetJson().AsMap() |
| 71 | + properties, ok := schema["properties"].(map[string]any) |
| 72 | + require.True(t, ok) |
| 73 | + testField, ok := properties["test"].(map[string]any) |
| 74 | + require.True(t, ok) |
| 75 | + assert.Equal(t, "hello world", testField["default"]) |
| 76 | +} |
| 77 | + |
| 78 | +func TestLiteralsToLaunchFormJson_Inline(t *testing.T) { |
| 79 | + svc := NewTranslatorService(nil, nil) |
| 80 | + |
| 81 | + resp, err := svc.LiteralsToLaunchFormJson(context.Background(), connect.NewRequest(&workflow.LiteralsToLaunchFormJsonRequest{ |
| 82 | + Literals: testNamedLiterals(), |
| 83 | + Variables: testVariableMap(), |
| 84 | + })) |
| 85 | + |
| 86 | + require.NoError(t, err) |
| 87 | + assertHelloWorldSchema(t, resp) |
| 88 | +} |
| 89 | + |
| 90 | +func TestLiteralsToLaunchFormJson_OffloadedURI(t *testing.T) { |
| 91 | + inputsURI := "s3://test-bucket/metadata/proj/dev/run1/a0/inputs.pb" |
| 92 | + storedInputs := &task.Inputs{Literals: testNamedLiterals()} |
| 93 | + |
| 94 | + runClient := workflowMocks.NewRunServiceClient(t) |
| 95 | + runClient.EXPECT().GetActionDataURIs(mock.Anything, mock.Anything).Return( |
| 96 | + connect.NewResponse(&workflow.GetActionDataURIsResponse{ |
| 97 | + InputsUri: inputsURI, |
| 98 | + OutputsUri: "s3://test-bucket/metadata/proj/dev/run1/a0/outputs.pb", |
| 99 | + }), nil) |
| 100 | + |
| 101 | + mockComposedStore := storageMocks.NewComposedProtobufStore(t) |
| 102 | + mockComposedStore.On("ReadProtobuf", mock.Anything, storage.DataReference(inputsURI), mock.Anything). |
| 103 | + Run(func(args mock.Arguments) { |
| 104 | + msg := args.Get(2).(proto.Message) |
| 105 | + proto.Reset(msg) |
| 106 | + proto.Merge(msg, storedInputs) |
| 107 | + }).Return(nil) |
| 108 | + |
| 109 | + svc := NewTranslatorService(&storage.DataStore{ComposedProtobufStore: mockComposedStore}, runClient) |
| 110 | + |
| 111 | + resp, err := svc.LiteralsToLaunchFormJson(context.Background(), connect.NewRequest(&workflow.LiteralsToLaunchFormJsonRequest{ |
| 112 | + Variables: testVariableMap(), |
| 113 | + LiteralsUri: inputsURI, |
| 114 | + ActionId: testActionID(), |
| 115 | + })) |
| 116 | + |
| 117 | + require.NoError(t, err) |
| 118 | + assertHelloWorldSchema(t, resp) |
| 119 | +} |
| 120 | + |
| 121 | +func TestLiteralsToLaunchFormJson_OffloadedURI_MissingActionId(t *testing.T) { |
| 122 | + svc := NewTranslatorService(nil, nil) |
| 123 | + |
| 124 | + _, err := svc.LiteralsToLaunchFormJson(context.Background(), connect.NewRequest(&workflow.LiteralsToLaunchFormJsonRequest{ |
| 125 | + Variables: testVariableMap(), |
| 126 | + LiteralsUri: "s3://test-bucket/metadata/proj/dev/run1/a0/inputs.pb", |
| 127 | + })) |
| 128 | + |
| 129 | + require.Error(t, err) |
| 130 | + assert.Equal(t, connect.CodeInvalidArgument, connect.CodeOf(err)) |
| 131 | + assert.Contains(t, err.Error(), "action_id is required") |
| 132 | +} |
| 133 | + |
| 134 | +func TestLiteralsToLaunchFormJson_OffloadedURI_Mismatch(t *testing.T) { |
| 135 | + runClient := workflowMocks.NewRunServiceClient(t) |
| 136 | + runClient.EXPECT().GetActionDataURIs(mock.Anything, mock.Anything).Return( |
| 137 | + connect.NewResponse(&workflow.GetActionDataURIsResponse{ |
| 138 | + InputsUri: "s3://test-bucket/metadata/proj/dev/run1/a0/inputs.pb", |
| 139 | + OutputsUri: "s3://test-bucket/metadata/proj/dev/run1/a0/outputs.pb", |
| 140 | + }), nil) |
| 141 | + |
| 142 | + svc := NewTranslatorService(nil, runClient) |
| 143 | + |
| 144 | + _, err := svc.LiteralsToLaunchFormJson(context.Background(), connect.NewRequest(&workflow.LiteralsToLaunchFormJsonRequest{ |
| 145 | + Variables: testVariableMap(), |
| 146 | + LiteralsUri: "s3://test-bucket/some/other/object.pb", |
| 147 | + ActionId: testActionID(), |
| 148 | + })) |
| 149 | + |
| 150 | + require.Error(t, err) |
| 151 | + assert.Equal(t, connect.CodeInvalidArgument, connect.CodeOf(err)) |
| 152 | + assert.Contains(t, err.Error(), "does not match any data URI") |
| 153 | +} |
0 commit comments