//go:build hostedlive package awsjob import ( "context" "encoding/json" "errors" "fmt" "net/netip" "os" "testing" "time" "amadan.net/oficina/equipo/internal/isolatedjob" "amadan.net/oficina/equipo/internal/isolatedjob/isolatedjobtest" "github.com/aws/aws-sdk-go-v2/config" "github.com/aws/aws-sdk-go-v2/service/ec2" "github.com/aws/aws-sdk-go-v2/service/lambdacore" vms "github.com/aws/aws-sdk-go-v2/service/lambdamicrovms" ) // liveConfig is the JSON file EQUIPO_HOSTED_LIVE_CONFIG names. Credentials // come from the usual AWS environment (AWS_PROFILE and the rest). type liveConfig struct { Region string `json:"region"` ConnectorID string `json:"connector_id"` // version-qualified ARN VPCID string `json:"vpc_id"` VMSubnetID string `json:"vm_subnet_id"` VMSecurityGroupID string `json:"vm_security_group_id"` Image isolatedjob.ImageDigest `json:"image"` // the digest the contract launches ImageARN string `json:"image_arn"` Versions map[isolatedjob.Size]string `json:"versions"` // size to image version Egress []string `json:"egress"` // "host:port" } // TestLiveContract runs the Runner contract against real AWS Lambda // MicroVMs. It launches and stops real VMs, so it runs only by hand, with // -tags hostedlive and EQUIPO_HOSTED_LIVE_CONFIG set (Task 17). func TestLiveContract(t *testing.T) { path := os.Getenv("EQUIPO_HOSTED_LIVE_CONFIG") if path == "" { t.Skip("EQUIPO_HOSTED_LIVE_CONFIG is not set") } raw, err := os.ReadFile(path) if err != nil { t.Fatalf("live config: %v", err) } var lc liveConfig if err := json.Unmarshal(raw, &lc); err != nil { t.Fatalf("live config %s: %v", path, err) } cfg := Config{ Region: lc.Region, ConnectorID: lc.ConnectorID, VPCID: lc.VPCID, VMSubnetID: lc.VMSubnetID, VMSecurityGroupID: lc.VMSecurityGroupID, Images: map[isolatedjob.ImageDigest]Image{lc.Image: {ARN: lc.ImageARN, Versions: lc.Versions}}, } for _, e := range lc.Egress { ap, err := netip.ParseAddrPort(e) if err != nil { t.Fatalf("live config egress %q: %v", e, err) } cfg.Egress = append(cfg.Egress, ap) } ctx := context.Background() awsCfg, err := config.LoadDefaultConfig(ctx, config.WithRegion(lc.Region)) if err != nil { t.Fatalf("AWS config: %v", err) } r, err := New(cfg, NewAPI(vms.NewFromConfig(awsCfg), ec2.NewFromConfig(awsCfg), lambdacore.NewFromConfig(awsCfg))) if err != nil { t.Fatalf("New: %v", err) } isolatedjobtest.Contract(t, isolatedjobtest.Harness{ Runner: r, Image: lc.Image, Settle: func(ctx context.Context, ref isolatedjob.Ref, want func(isolatedjob.State) bool) (isolatedjob.Job, error) { return settle(ctx, r, ref, want, 5*time.Minute, liveVisibility) }, // GetMicrovm's state is eventually consistent (doc.go, States), and // ListMicrovms is a separate read that can trail it. 30 s and 60 s // are guesses for Task 17 to replace with what it measures. ListLag: 30 * time.Second, Visibility: liveVisibility, CallTimeout: liveCallTimeout, }) } const ( // liveVisibility is how long a VM RunMicrovm just created may read as // not found (final review A3). liveVisibility = 60 * time.Second // liveCallTimeout bounds each provider call of a live run. liveCallTimeout = 30 * time.Second ) // settle polls Get every 5 s until want holds or bound passes. Each Get is // bounded by liveCallTimeout. ErrNotFound is read again for the first // visible of the wait, as a new VM may not show yet, unless want accepts a // terminal state: to a caller waiting for the VM to end, gone is an answer. func settle(ctx context.Context, r *Runner, ref isolatedjob.Ref, want func(isolatedjob.State) bool, bound, visible time.Duration) (isolatedjob.Job, error) { start := time.Now() deadline := start.Add(bound) waitsForEnd := want(isolatedjob.StateStopped) || want(isolatedjob.StateFailed) for { callCtx, cancel := context.WithTimeout(ctx, liveCallTimeout) job, err := r.Get(callCtx, ref) cancel() notYet := errors.Is(err, isolatedjob.ErrNotFound) && !waitsForEnd && time.Since(start) < visible if err != nil && !errors.Is(err, isolatedjob.ErrThrottled) && !notYet { return job, err } if err == nil && want(job.State) { return job, nil } if time.Now().After(deadline) { return job, fmt.Errorf("%s still %s after %v (last error %v)", ref, job.State, bound, err) } select { case <-ctx.Done(): return job, ctx.Err() case <-time.After(5 * time.Second): } } }