| 1 | package hostedwire |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/json" |
| 6 | "errors" |
| 7 | "math" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | "time" |
| 11 | |
| 12 | "amadan.net/oficina/equipo/internal/isolatedjob" |
| 13 | ) |
| 14 | |
| 15 | func TestDecodeStrict(t *testing.T) { |
| 16 | pad := func(n int) string { |
| 17 | // {"reason":"aaa..."} padded to exactly n bytes. |
| 18 | const frame = `{"reason":""}` |
| 19 | return `{"reason":"` + strings.Repeat("a", n-len(frame)) + `"}` |
| 20 | } |
| 21 | cases := []struct { |
| 22 | name string |
| 23 | in string |
| 24 | want error |
| 25 | }{ |
| 26 | {"valid", `{"reason":"done"}`, nil}, |
| 27 | {"valid with space", " \n{\"reason\":\"done\"}\n ", nil}, |
| 28 | {"exactly MaxBody", pad(MaxBody), nil}, |
| 29 | {"over MaxBody", pad(MaxBody + 1), ErrTooLarge}, |
| 30 | {"unknown field", `{"reason":"done","extra":1}`, ErrBadJSON}, |
| 31 | {"field in another case", `{"Reason":"done"}`, ErrBadJSON}, |
| 32 | {"duplicate field", `{"reason":"a","reason":"b"}`, ErrBadJSON}, |
| 33 | {"trailing value", `{"reason":"a"} {"reason":"b"}`, ErrBadJSON}, |
| 34 | {"trailing garbage", `{"reason":"a"}x`, ErrBadJSON}, |
| 35 | {"empty", ``, ErrBadJSON}, |
| 36 | {"null", `null`, ErrBadJSON}, |
| 37 | {"array", `[]`, ErrBadJSON}, |
| 38 | {"truncated", `{"reason":"a"`, ErrBadJSON}, |
| 39 | {"wrong type", `{"reason":5}`, ErrBadJSON}, |
| 40 | } |
| 41 | for _, c := range cases { |
| 42 | var v StopRequest |
| 43 | err := DecodeStrict(strings.NewReader(c.in), &v) |
| 44 | if !errors.Is(err, c.want) || (c.want == nil) != (err == nil) { |
| 45 | name := c.name |
| 46 | t.Fatalf("DecodeStrict(%s) = %v, want %v", name, err, c.want) |
| 47 | } |
| 48 | if c.want == nil && v.Reason == "" { |
| 49 | t.Fatalf("DecodeStrict(%s) left Reason empty", c.name) |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | // Nested objects are held to the same rules. |
| 54 | nested := []struct { |
| 55 | name string |
| 56 | in string |
| 57 | want error |
| 58 | }{ |
| 59 | {"valid", `{"vms":[{"vm_ref":"vm-1","state":"running","attempt_id":"a1","age_known":true,"age_ms":5}]}`, nil}, |
| 60 | {"nested duplicate", `{"vms":[{"vm_ref":"vm-1","vm_ref":"vm-2"}]}`, ErrBadJSON}, |
| 61 | {"nested unknown", `{"vms":[{"vm_ref":"vm-1","owner":"x"}]}`, ErrBadJSON}, |
| 62 | {"nested other case", `{"vms":[{"VM_REF":"vm-1"}]}`, ErrBadJSON}, |
| 63 | {"same key in sibling objects", `{"vms":[{"vm_ref":"vm-1"},{"vm_ref":"vm-2"}]}`, nil}, |
| 64 | } |
| 65 | for _, c := range nested { |
| 66 | var v VMListResponse |
| 67 | err := DecodeStrict(strings.NewReader(c.in), &v) |
| 68 | if !errors.Is(err, c.want) || (c.want == nil) != (err == nil) { |
| 69 | t.Fatalf("DecodeStrict(%s) = %v, want %v", c.name, err, c.want) |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | // TestWireNames pins the JSON names, so the controller and the instance cannot |
| 75 | // drift apart silently. |
| 76 | func TestWireNames(t *testing.T) { |
| 77 | // Controller time, a duration since the controller's epoch, kept to the |
| 78 | // nanosecond and above 2^53 so a float on the way would show. |
| 79 | at := time.Duration(1<<62 + 1) |
| 80 | cases := []struct { |
| 81 | v any |
| 82 | into func() any |
| 83 | want string |
| 84 | }{ |
| 85 | {LaunchRequest{AttemptID: "a1", TaskID: "t1", Account: "acct", Replaces: "a0", Image: "sha256:ab", Size: isolatedjob.SizeSmall, MaxLifetimeMS: 1000, InstanceCode: "code"}, |
| 86 | func() any { return &LaunchRequest{} }, |
| 87 | `{"attempt_id":"a1","task_id":"t1","account":"acct","replaces":"a0","image":"sha256:ab","size":"small","max_lifetime_ms":1000,"instance_code":"code"}`}, |
| 88 | {JobView{AttemptID: "a1", LaunchID: "l1", State: "running", VMRef: "vm-1", Epoch: 2, DeadlineInMS: 3, DrainInMS: 4, StopInMS: 5}, |
| 89 | func() any { return &JobView{} }, |
| 90 | `{"attempt_id":"a1","launch_id":"l1","state":"running","vm_ref":"vm-1","epoch":2,"deadline_in_ms":3,"drain_in_ms":4,"stop_in_ms":5}`}, |
| 91 | {StopRequest{Reason: "done"}, func() any { return &StopRequest{} }, `{"reason":"done"}`}, |
| 92 | {StopProof{AttemptID: "a1", VMRef: "vm-1", State: isolatedjob.StateStopped, ObservedAt: at}, |
| 93 | func() any { return &StopProof{} }, |
| 94 | `{"attempt_id":"a1","vm_ref":"vm-1","state":"stopped","observed_at_ns":4611686018427387905}`}, |
| 95 | {DrainRequest{}, func() any { return &DrainRequest{} }, `{}`}, |
| 96 | {DrainResponse{Generation: 4}, func() any { return &DrainResponse{} }, `{"generation":4}`}, |
| 97 | {QuiescentRequest{Generation: 4}, func() any { return &QuiescentRequest{} }, `{"generation":4}`}, |
| 98 | {QuiescentResponse{Generation: 4}, func() any { return &QuiescentResponse{} }, `{"generation":4}`}, |
| 99 | {SuspendRequest{Generation: 4}, func() any { return &SuspendRequest{} }, `{"generation":4}`}, |
| 100 | {SuspendResponse{Generation: 4}, func() any { return &SuspendResponse{} }, `{"generation":4}`}, |
| 101 | {ReopenRequest{Generation: 4}, func() any { return &ReopenRequest{} }, `{"generation":4}`}, |
| 102 | {ReopenResponse{Generation: 4}, func() any { return &ReopenResponse{} }, `{"generation":4}`}, |
| 103 | {ResumeRequest{OperationID: "op1"}, func() any { return &ResumeRequest{} }, `{"operation_id":"op1"}`}, |
| 104 | {ResumeResponse{Epoch: 3}, func() any { return &ResumeResponse{} }, `{"epoch":3}`}, |
| 105 | {VMListResponse{VMs: []VMView{{VMRef: "vm-1", State: isolatedjob.StateRunning, AttemptID: "a1", AgeKnown: true, AgeMS: 9}}}, |
| 106 | func() any { return &VMListResponse{} }, |
| 107 | `{"vms":[{"vm_ref":"vm-1","state":"running","attempt_id":"a1","age_known":true,"age_ms":9}]}`}, |
| 108 | {ErrorResponse{Code: "forbidden", Message: "not yours"}, func() any { return &ErrorResponse{} }, `{"code":"forbidden","message":"not yours"}`}, |
| 109 | {Bootstrap{Version: BootstrapVersion, LaunchID: "l1", InstanceCode: "code"}, |
| 110 | func() any { return &Bootstrap{} }, |
| 111 | `{"version":1,"launch_id":"l1","instance_code":"code"}`}, |
| 112 | } |
| 113 | for _, c := range cases { |
| 114 | b, err := json.Marshal(c.v) |
| 115 | if err != nil { |
| 116 | t.Fatal(err) |
| 117 | } |
| 118 | if string(b) != c.want { |
| 119 | t.Fatalf("json of %T = %s, want %s", c.v, b, c.want) |
| 120 | } |
| 121 | back := c.into() |
| 122 | if err := DecodeStrict(bytes.NewReader(b), back); err != nil { |
| 123 | t.Fatalf("DecodeStrict(%s) into %T = %v", b, back, err) |
| 124 | } |
| 125 | again, _ := json.Marshal(back) |
| 126 | if string(again) != c.want { |
| 127 | t.Fatalf("round trip of %T = %s, want %s", c.v, again, c.want) |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | func TestLaunchLifetimeBounds(t *testing.T) { |
| 133 | const most = math.MaxInt64 / int64(time.Millisecond) |
| 134 | cases := []struct { |
| 135 | ms int64 |
| 136 | want time.Duration |
| 137 | ok bool |
| 138 | }{ |
| 139 | {0, 0, true}, |
| 140 | {1000, time.Second, true}, |
| 141 | {28_800_000, 8 * time.Hour, true}, |
| 142 | {most, time.Duration(most) * time.Millisecond, true}, |
| 143 | {most + 1, 0, false}, |
| 144 | // Each of these wraps to exactly one hour if multiplied unchecked. |
| 145 | {288230376155311744, 0, false}, |
| 146 | {-288230376148111744, 0, false}, |
| 147 | {-1, 0, false}, |
| 148 | {math.MaxInt64, 0, false}, |
| 149 | {math.MinInt64, 0, false}, |
| 150 | } |
| 151 | for _, c := range cases { |
| 152 | got, err := LaunchRequest{MaxLifetimeMS: c.ms}.MaxLifetime() |
| 153 | if c.ok && (err != nil || got != c.want) { |
| 154 | t.Fatalf("MaxLifetime(%d ms) = (%v, %v), want (%v, nil)", c.ms, got, err, c.want) |
| 155 | } |
| 156 | if !c.ok && (!errors.Is(err, ErrDuration) || got != 0) { |
| 157 | t.Fatalf("MaxLifetime(%d ms) = (%v, %v), want (0, ErrDuration)", c.ms, got, err) |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | |