| 1 | package awsjob |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | |
| 9 | "amadan.net/oficina/equipo/internal/isolatedjob" |
| 10 | "github.com/aws/aws-sdk-go-v2/aws" |
| 11 | vms "github.com/aws/aws-sdk-go-v2/service/lambdamicrovms" |
| 12 | "github.com/aws/aws-sdk-go-v2/service/lambdamicrovms/types" |
| 13 | ) |
| 14 | |
| 15 | // sentinelID is a provider "ID" carrying text that must never reach an |
| 16 | // error or a log. |
| 17 | const sentinelID = "vm-1 SENTINEL-secret=hunter2" |
| 18 | |
| 19 | // TestMalformedIDsRefused (ruling R36): an ID that is not 1 to 128 letters, |
| 20 | // digits and '-' is refused on every path that reads one, with text that |
| 21 | // never quotes it. |
| 22 | func TestMalformedIDsRefused(t *testing.T) { |
| 23 | for _, id := range []string{sentinelID, strings.Repeat("a", isolatedjob.MaxRefLen+1), "vm/1", "vm-1\n"} { |
| 24 | quoted := func(t *testing.T, err error) { |
| 25 | t.Helper() |
| 26 | if err != nil && (strings.Contains(err.Error(), "SENTINEL") || strings.Contains(err.Error(), id)) { |
| 27 | t.Errorf("error %q quotes the malformed id", err) |
| 28 | } |
| 29 | } |
| 30 | t.Run("run "+id[:min(len(id), 12)], func(t *testing.T) { |
| 31 | s := &stub{run: func(*vms.RunMicrovmInput) (*vms.RunMicrovmOutput, error) { |
| 32 | return &vms.RunMicrovmOutput{MicrovmId: aws.String(id), State: types.MicrovmStatePending}, nil |
| 33 | }} |
| 34 | ref, err := newTestRunner(t, s).Launch(context.Background(), testSpec()) |
| 35 | if err == nil || ref != "" { |
| 36 | t.Fatalf("Launch with id %q = (%q, %v), want an error and no ref", id, ref, err) |
| 37 | } |
| 38 | quoted(t, err) |
| 39 | }) |
| 40 | t.Run("get "+id[:min(len(id), 12)], func(t *testing.T) { |
| 41 | s := &stub{get: func(*vms.GetMicrovmInput) (*vms.GetMicrovmOutput, error) { |
| 42 | return &vms.GetMicrovmOutput{MicrovmId: aws.String(id), State: types.MicrovmStateRunning}, nil |
| 43 | }} |
| 44 | job, err := newTestRunner(t, s).Get(context.Background(), "vm-1") |
| 45 | if err == nil || job.Ref != "" { |
| 46 | t.Fatalf("Get answered with id %q = (%+v, %v), want an error", id, job, err) |
| 47 | } |
| 48 | quoted(t, err) |
| 49 | }) |
| 50 | t.Run("list "+id[:min(len(id), 12)], func(t *testing.T) { |
| 51 | s := &stub{list: func(*vms.ListMicrovmsInput) (*vms.ListMicrovmsOutput, error) { |
| 52 | return &vms.ListMicrovmsOutput{Items: []types.MicrovmItem{ |
| 53 | {MicrovmId: aws.String("vm-ok"), ImageArn: aws.String(testImageARN), State: types.MicrovmStateRunning}, |
| 54 | {MicrovmId: aws.String(id), ImageArn: aws.String(testImageARN), State: types.MicrovmStateRunning}, |
| 55 | }}, nil |
| 56 | }} |
| 57 | jobs, err := newTestRunner(t, s).List(context.Background()) |
| 58 | if !errors.Is(err, isolatedjob.ErrIncomplete) || jobs != nil { |
| 59 | t.Fatalf("List holding id %q = (%d jobs, %v), want no jobs and ErrIncomplete", id, len(jobs), err) |
| 60 | } |
| 61 | quoted(t, err) |
| 62 | }) |
| 63 | } |
| 64 | // R38: a Get reply with no ID, empty or absent, is an error, never the |
| 65 | // asked-for ref (a stopped state in it must not become stop proof). |
| 66 | for name, id := range map[string]*string{"empty": aws.String(""), "nil": nil} { |
| 67 | t.Run("get "+name+" id", func(t *testing.T) { |
| 68 | s := &stub{get: func(*vms.GetMicrovmInput) (*vms.GetMicrovmOutput, error) { |
| 69 | return &vms.GetMicrovmOutput{MicrovmId: id, State: types.MicrovmStateTerminated}, nil |
| 70 | }} |
| 71 | job, err := newTestRunner(t, s).Get(context.Background(), "vm-1") |
| 72 | if err == nil || job.Ref != "" || job.State != "" { |
| 73 | t.Fatalf("Get answered with a %s id = (%+v, %v), want an error and no job", name, job, err) |
| 74 | } |
| 75 | }) |
| 76 | } |
| 77 | // A malformed ref handed in is refused before any provider call. |
| 78 | s := &stub{} |
| 79 | r := newTestRunner(t, s) |
| 80 | if _, err := r.Get(context.Background(), sentinelID); !errors.Is(err, isolatedjob.ErrInvalid) || strings.Contains(err.Error(), "SENTINEL") { |
| 81 | t.Errorf("Get(malformed ref) = %v, want ErrInvalid without the ref", err) |
| 82 | } |
| 83 | if err := r.Stop(context.Background(), sentinelID, "x"); !errors.Is(err, isolatedjob.ErrInvalid) { |
| 84 | t.Errorf("Stop(malformed ref) = %v, want ErrInvalid", err) |
| 85 | } |
| 86 | if n := s.count("GetMicrovm") + s.count("TerminateMicrovm"); n != 0 { |
| 87 | t.Errorf("%d provider calls for malformed refs, want none", n) |
| 88 | } |
| 89 | } |
| 90 | |