oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1//go:build hostedlive
2
3package awsjob
4
5import (
6 "context"
7 "encoding/json"
8 "errors"
9 "fmt"
10 "net/netip"
11 "os"
12 "testing"
13 "time"
14
15 "amadan.net/oficina/equipo/internal/isolatedjob"
16 "amadan.net/oficina/equipo/internal/isolatedjob/isolatedjobtest"
17 "github.com/aws/aws-sdk-go-v2/config"
18 "github.com/aws/aws-sdk-go-v2/service/ec2"
19 "github.com/aws/aws-sdk-go-v2/service/lambdacore"
20 vms "github.com/aws/aws-sdk-go-v2/service/lambdamicrovms"
21)
22
23// liveConfig is the JSON file EQUIPO_HOSTED_LIVE_CONFIG names. Credentials
24// come from the usual AWS environment (AWS_PROFILE and the rest).
25type liveConfig struct {
26 Region string `json:"region"`
27 ConnectorID string `json:"connector_id"` // version-qualified ARN
28 VPCID string `json:"vpc_id"`
29 VMSubnetID string `json:"vm_subnet_id"`
30 VMSecurityGroupID string `json:"vm_security_group_id"`
31 Image isolatedjob.ImageDigest `json:"image"` // the digest the contract launches
32 ImageARN string `json:"image_arn"`
33 Versions map[isolatedjob.Size]string `json:"versions"` // size to image version
34 Egress []string `json:"egress"` // "host:port"
35}
36
37// TestLiveContract runs the Runner contract against real AWS Lambda
38// MicroVMs. It launches and stops real VMs, so it runs only by hand, with
39// -tags hostedlive and EQUIPO_HOSTED_LIVE_CONFIG set (Task 17).
40func TestLiveContract(t *testing.T) {
41 path := os.Getenv("EQUIPO_HOSTED_LIVE_CONFIG")
42 if path == "" {
43 t.Skip("EQUIPO_HOSTED_LIVE_CONFIG is not set")
44 }
45 raw, err := os.ReadFile(path)
46 if err != nil {
47 t.Fatalf("live config: %v", err)
48 }
49 var lc liveConfig
50 if err := json.Unmarshal(raw, &lc); err != nil {
51 t.Fatalf("live config %s: %v", path, err)
52 }
53 cfg := Config{
54 Region: lc.Region,
55 ConnectorID: lc.ConnectorID,
56 VPCID: lc.VPCID,
57 VMSubnetID: lc.VMSubnetID,
58 VMSecurityGroupID: lc.VMSecurityGroupID,
59 Images: map[isolatedjob.ImageDigest]Image{lc.Image: {ARN: lc.ImageARN, Versions: lc.Versions}},
60 }
61 for _, e := range lc.Egress {
62 ap, err := netip.ParseAddrPort(e)
63 if err != nil {
64 t.Fatalf("live config egress %q: %v", e, err)
65 }
66 cfg.Egress = append(cfg.Egress, ap)
67 }
68
69 ctx := context.Background()
70 awsCfg, err := config.LoadDefaultConfig(ctx, config.WithRegion(lc.Region))
71 if err != nil {
72 t.Fatalf("AWS config: %v", err)
73 }
74 r, err := New(cfg, NewAPI(vms.NewFromConfig(awsCfg), ec2.NewFromConfig(awsCfg), lambdacore.NewFromConfig(awsCfg)))
75 if err != nil {
76 t.Fatalf("New: %v", err)
77 }
78
79 isolatedjobtest.Contract(t, isolatedjobtest.Harness{
80 Runner: r,
81 Image: lc.Image,
82 Settle: func(ctx context.Context, ref isolatedjob.Ref, want func(isolatedjob.State) bool) (isolatedjob.Job, error) {
83 return settle(ctx, r, ref, want, 5*time.Minute, liveVisibility)
84 },
85 // GetMicrovm's state is eventually consistent (doc.go, States), and
86 // ListMicrovms is a separate read that can trail it. 30 s and 60 s
87 // are guesses for Task 17 to replace with what it measures.
88 ListLag: 30 * time.Second,
89 Visibility: liveVisibility,
90 CallTimeout: liveCallTimeout,
91 })
92}
93
94const (
95 // liveVisibility is how long a VM RunMicrovm just created may read as
96 // not found (final review A3).
97 liveVisibility = 60 * time.Second
98 // liveCallTimeout bounds each provider call of a live run.
99 liveCallTimeout = 30 * time.Second
100)
101
102// settle polls Get every 5 s until want holds or bound passes. Each Get is
103// bounded by liveCallTimeout. ErrNotFound is read again for the first
104// visible of the wait, as a new VM may not show yet, unless want accepts a
105// terminal state: to a caller waiting for the VM to end, gone is an answer.
106func settle(ctx context.Context, r *Runner, ref isolatedjob.Ref, want func(isolatedjob.State) bool, bound, visible time.Duration) (isolatedjob.Job, error) {
107 start := time.Now()
108 deadline := start.Add(bound)
109 waitsForEnd := want(isolatedjob.StateStopped) || want(isolatedjob.StateFailed)
110 for {
111 callCtx, cancel := context.WithTimeout(ctx, liveCallTimeout)
112 job, err := r.Get(callCtx, ref)
113 cancel()
114 notYet := errors.Is(err, isolatedjob.ErrNotFound) && !waitsForEnd && time.Since(start) < visible
115 if err != nil && !errors.Is(err, isolatedjob.ErrThrottled) && !notYet {
116 return job, err
117 }
118 if err == nil && want(job.State) {
119 return job, nil
120 }
121 if time.Now().After(deadline) {
122 return job, fmt.Errorf("%s still %s after %v (last error %v)", ref, job.State, bound, err)
123 }
124 select {
125 case <-ctx.Done():
126 return job, ctx.Err()
127 case <-time.After(5 * time.Second):
128 }
129 }
130}
131