|
| 1 | +package agent |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + |
| 10 | + builderv0 "github.com/codefly-dev/core/generated/go/codefly/services/builder/v0" |
| 11 | + "github.com/codefly-dev/core/resources" |
| 12 | + "google.golang.org/grpc/codes" |
| 13 | + "google.golang.org/grpc/status" |
| 14 | +) |
| 15 | + |
| 16 | +// TestLoadRefusesWhatTheHarnessCannotHonour holds both refusals at the first |
| 17 | +// call that sees a declaration. Core admits each value, and a package built |
| 18 | +// for either would claim what its harness never does when called: a submit |
| 19 | +// package would answer a bare output where an acceptance is owed, and a signal |
| 20 | +// package would promise an interrupted outcome no Python thread can report. |
| 21 | +func TestLoadRefusesWhatTheHarnessCannotHonour(t *testing.T) { |
| 22 | + for _, test := range []struct { |
| 23 | + name string |
| 24 | + declare func(*resources.RunnableExecution) |
| 25 | + refused string |
| 26 | + }{ |
| 27 | + {"call completion and no cancellation", func(*resources.RunnableExecution) {}, ""}, |
| 28 | + {"submit completion", func(e *resources.RunnableExecution) { |
| 29 | + e.Recovery, e.Completion = resources.RunnableRecoveryReceipt, resources.RunnableCompletionSubmit |
| 30 | + }, `completion "submit"`}, |
| 31 | + {"signal cancellation", func(e *resources.RunnableExecution) { |
| 32 | + e.Cancellation = resources.RunnableCancellationSignal |
| 33 | + }, `cancellation "signal"`}, |
| 34 | + } { |
| 35 | + t.Run(test.name, func(t *testing.T) { |
| 36 | + ctx := context.Background() |
| 37 | + builder, request := declared(t, test.declare) |
| 38 | + |
| 39 | + loaded, err := builder.Load(ctx, request) |
| 40 | + |
| 41 | + if test.refused == "" { |
| 42 | + if err != nil { |
| 43 | + t.Fatalf("a declaration the harness honours was refused: %v", err) |
| 44 | + } |
| 45 | + if loaded.GetState().GetState() != builderv0.LoadStatus_READY { |
| 46 | + t.Fatalf("state = %v", loaded.GetState().GetState()) |
| 47 | + } |
| 48 | + return |
| 49 | + } |
| 50 | + if status.Code(err) != codes.InvalidArgument || !strings.Contains(err.Error(), test.refused) { |
| 51 | + t.Fatalf("Load = %v, want InvalidArgument naming %s", err, test.refused) |
| 52 | + } |
| 53 | + if builder.runnable != nil { |
| 54 | + t.Fatal("a refused declaration stayed loaded") |
| 55 | + } |
| 56 | + }) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func declared(t *testing.T, declare func(*resources.RunnableExecution)) (*Builder, *builderv0.LoadRequest) { |
| 61 | + t.Helper() |
| 62 | + ctx := context.Background() |
| 63 | + manifest, err := resources.LoadFromPath[resources.Agent](ctx, filepath.Join("..", "..", "agent.codefly.yaml")) |
| 64 | + if err != nil { |
| 65 | + t.Fatal(err) |
| 66 | + } |
| 67 | + dir := t.TempDir() |
| 68 | + if err := os.WriteFile(filepath.Join(dir, "workspace.codefly.yaml"), |
| 69 | + []byte("kind: workspace\nname: proof\nlayout: flat\nservices: []\n"), 0o600); err != nil { |
| 70 | + t.Fatal(err) |
| 71 | + } |
| 72 | + workspace, err := resources.LoadWorkspaceFromDir(ctx, dir) |
| 73 | + if err != nil { |
| 74 | + t.Fatal(err) |
| 75 | + } |
| 76 | + module, err := workspace.LoadModuleFromName(ctx, "proof") |
| 77 | + if err != nil { |
| 78 | + t.Fatal(err) |
| 79 | + } |
| 80 | + r, err := module.NewRunnable(ctx, "word-count", manifest, "handler.py") |
| 81 | + if err != nil { |
| 82 | + t.Fatal(err) |
| 83 | + } |
| 84 | + declare(r.Execution) |
| 85 | + if err := r.Save(ctx); err != nil { |
| 86 | + t.Fatal(err) |
| 87 | + } |
| 88 | + location, err := workspace.RunnableLocationOf(r) |
| 89 | + if err != nil { |
| 90 | + t.Fatal(err) |
| 91 | + } |
| 92 | + wire, err := location.Proto() |
| 93 | + if err != nil { |
| 94 | + t.Fatal(err) |
| 95 | + } |
| 96 | + return NewBuilder(manifest), &builderv0.LoadRequest{Runnable: wire} |
| 97 | +} |
0 commit comments