| 1 | //go:build hostedlive |
| 2 | |
| 3 | package main |
| 4 | |
| 5 | import ( |
| 6 | "bytes" |
| 7 | "context" |
| 8 | "encoding/json" |
| 9 | "errors" |
| 10 | "net/http" |
| 11 | "os" |
| 12 | "path/filepath" |
| 13 | "reflect" |
| 14 | "strings" |
| 15 | "testing" |
| 16 | "time" |
| 17 | |
| 18 | "amadan.net/rastrillo/rastrillo/budget" |
| 19 | "github.com/aws/aws-sdk-go-v2/aws" |
| 20 | awsmiddleware "github.com/aws/aws-sdk-go-v2/aws/middleware" |
| 21 | awshttp "github.com/aws/aws-sdk-go-v2/aws/transport/http" |
| 22 | "github.com/aws/aws-sdk-go-v2/service/lambdacore" |
| 23 | coretypes "github.com/aws/aws-sdk-go-v2/service/lambdacore/types" |
| 24 | vms "github.com/aws/aws-sdk-go-v2/service/lambdamicrovms" |
| 25 | "github.com/aws/aws-sdk-go-v2/service/lambdamicrovms/types" |
| 26 | "github.com/aws/aws-sdk-go-v2/service/sts" |
| 27 | "github.com/aws/smithy-go" |
| 28 | "github.com/aws/smithy-go/middleware" |
| 29 | smithyhttp "github.com/aws/smithy-go/transport/http" |
| 30 | ) |
| 31 | |
| 32 | func TestMain(m *testing.M) { os.Exit(budget.Main(m)) } |
| 33 | |
| 34 | const ( |
| 35 | runID = "1128-abc123" |
| 36 | prefix = prefixBase + runID |
| 37 | ) |
| 38 | |
| 39 | // stub answers every SDK call from its fields and records the calls. |
| 40 | type stub struct { |
| 41 | calls []string |
| 42 | account string |
| 43 | |
| 44 | create func(*lambdacore.CreateNetworkConnectorInput) (*lambdacore.CreateNetworkConnectorOutput, error) |
| 45 | getConn func(*lambdacore.GetNetworkConnectorInput) (*lambdacore.GetNetworkConnectorOutput, error) |
| 46 | listVers func(*lambdacore.ListNetworkConnectorVersionsInput) (*lambdacore.ListNetworkConnectorVersionsOutput, error) |
| 47 | deleteConn func(*lambdacore.DeleteNetworkConnectorInput) (*lambdacore.DeleteNetworkConnectorOutput, error) |
| 48 | |
| 49 | baseVers func(*vms.ListManagedMicrovmImageVersionsInput) (*vms.ListManagedMicrovmImageVersionsOutput, error) |
| 50 | createImg func(*vms.CreateMicrovmImageInput) (*vms.CreateMicrovmImageOutput, error) |
| 51 | updateImg func(*vms.UpdateMicrovmImageInput) (*vms.UpdateMicrovmImageOutput, error) |
| 52 | getImg func(*vms.GetMicrovmImageInput) (*vms.GetMicrovmImageOutput, error) |
| 53 | getImgVer func(*vms.GetMicrovmImageVersionInput) (*vms.GetMicrovmImageVersionOutput, error) |
| 54 | runVM func(*vms.RunMicrovmInput) (*vms.RunMicrovmOutput, error) |
| 55 | getVM func(*vms.GetMicrovmInput) (*vms.GetMicrovmOutput, error) |
| 56 | listVMs func(*vms.ListMicrovmsInput) (*vms.ListMicrovmsOutput, error) |
| 57 | inputs []any |
| 58 | terminated []string |
| 59 | } |
| 60 | |
| 61 | func (s *stub) note(op string, in any) { |
| 62 | s.calls = append(s.calls, op) |
| 63 | s.inputs = append(s.inputs, in) |
| 64 | } |
| 65 | |
| 66 | func (s *stub) count(op string) int { |
| 67 | n := 0 |
| 68 | for _, c := range s.calls { |
| 69 | if c == op { |
| 70 | n++ |
| 71 | } |
| 72 | } |
| 73 | return n |
| 74 | } |
| 75 | |
| 76 | func (s *stub) CreateNetworkConnector(_ context.Context, in *lambdacore.CreateNetworkConnectorInput, _ ...func(*lambdacore.Options)) (*lambdacore.CreateNetworkConnectorOutput, error) { |
| 77 | s.note("CreateNetworkConnector", in) |
| 78 | if s.create != nil { |
| 79 | return s.create(in) |
| 80 | } |
| 81 | return &lambdacore.CreateNetworkConnectorOutput{Name: in.Name, State: coretypes.NetworkConnectorStatePending}, nil |
| 82 | } |
| 83 | |
| 84 | func (s *stub) GetNetworkConnector(_ context.Context, in *lambdacore.GetNetworkConnectorInput, _ ...func(*lambdacore.Options)) (*lambdacore.GetNetworkConnectorOutput, error) { |
| 85 | s.note("GetNetworkConnector", in) |
| 86 | if s.getConn != nil { |
| 87 | return s.getConn(in) |
| 88 | } |
| 89 | return &lambdacore.GetNetworkConnectorOutput{Name: aws.String(prefix + "-conn"), State: coretypes.NetworkConnectorStateActive}, nil |
| 90 | } |
| 91 | |
| 92 | func (s *stub) ListNetworkConnectorVersions(_ context.Context, in *lambdacore.ListNetworkConnectorVersionsInput, _ ...func(*lambdacore.Options)) (*lambdacore.ListNetworkConnectorVersionsOutput, error) { |
| 93 | s.note("ListNetworkConnectorVersions", in) |
| 94 | if s.listVers != nil { |
| 95 | return s.listVers(in) |
| 96 | } |
| 97 | return &lambdacore.ListNetworkConnectorVersionsOutput{}, nil |
| 98 | } |
| 99 | |
| 100 | func (s *stub) DeleteNetworkConnector(_ context.Context, in *lambdacore.DeleteNetworkConnectorInput, _ ...func(*lambdacore.Options)) (*lambdacore.DeleteNetworkConnectorOutput, error) { |
| 101 | s.note("DeleteNetworkConnector", in) |
| 102 | if s.deleteConn != nil { |
| 103 | return s.deleteConn(in) |
| 104 | } |
| 105 | return &lambdacore.DeleteNetworkConnectorOutput{State: coretypes.NetworkConnectorStateDeleting}, nil |
| 106 | } |
| 107 | |
| 108 | func (s *stub) ListManagedMicrovmImageVersions(_ context.Context, in *vms.ListManagedMicrovmImageVersionsInput, _ ...func(*vms.Options)) (*vms.ListManagedMicrovmImageVersionsOutput, error) { |
| 109 | s.note("ListManagedMicrovmImageVersions", in) |
| 110 | return s.baseVers(in) |
| 111 | } |
| 112 | |
| 113 | func (s *stub) CreateMicrovmImage(_ context.Context, in *vms.CreateMicrovmImageInput, _ ...func(*vms.Options)) (*vms.CreateMicrovmImageOutput, error) { |
| 114 | s.note("CreateMicrovmImage", in) |
| 115 | if s.createImg != nil { |
| 116 | return s.createImg(in) |
| 117 | } |
| 118 | return &vms.CreateMicrovmImageOutput{}, nil |
| 119 | } |
| 120 | |
| 121 | func (s *stub) UpdateMicrovmImage(_ context.Context, in *vms.UpdateMicrovmImageInput, _ ...func(*vms.Options)) (*vms.UpdateMicrovmImageOutput, error) { |
| 122 | s.note("UpdateMicrovmImage", in) |
| 123 | if s.updateImg != nil { |
| 124 | return s.updateImg(in) |
| 125 | } |
| 126 | return &vms.UpdateMicrovmImageOutput{}, nil |
| 127 | } |
| 128 | |
| 129 | func (s *stub) GetMicrovmImage(_ context.Context, in *vms.GetMicrovmImageInput, _ ...func(*vms.Options)) (*vms.GetMicrovmImageOutput, error) { |
| 130 | s.note("GetMicrovmImage", in) |
| 131 | if s.getImg != nil { |
| 132 | return s.getImg(in) |
| 133 | } |
| 134 | return &vms.GetMicrovmImageOutput{Name: aws.String(prefix + "-image")}, nil |
| 135 | } |
| 136 | |
| 137 | func (s *stub) GetMicrovmImageVersion(_ context.Context, in *vms.GetMicrovmImageVersionInput, _ ...func(*vms.Options)) (*vms.GetMicrovmImageVersionOutput, error) { |
| 138 | s.note("GetMicrovmImageVersion", in) |
| 139 | return s.getImgVer(in) |
| 140 | } |
| 141 | |
| 142 | func (s *stub) ListMicrovmImages(_ context.Context, in *vms.ListMicrovmImagesInput, _ ...func(*vms.Options)) (*vms.ListMicrovmImagesOutput, error) { |
| 143 | s.note("ListMicrovmImages", in) |
| 144 | return &vms.ListMicrovmImagesOutput{}, nil |
| 145 | } |
| 146 | |
| 147 | func (s *stub) DeleteMicrovmImage(_ context.Context, in *vms.DeleteMicrovmImageInput, _ ...func(*vms.Options)) (*vms.DeleteMicrovmImageOutput, error) { |
| 148 | s.note("DeleteMicrovmImage", in) |
| 149 | return &vms.DeleteMicrovmImageOutput{}, nil |
| 150 | } |
| 151 | |
| 152 | func (s *stub) RunMicrovm(_ context.Context, in *vms.RunMicrovmInput, _ ...func(*vms.Options)) (*vms.RunMicrovmOutput, error) { |
| 153 | s.note("RunMicrovm", in) |
| 154 | if s.runVM != nil { |
| 155 | return s.runVM(in) |
| 156 | } |
| 157 | return &vms.RunMicrovmOutput{MicrovmId: aws.String("vm-1")}, nil |
| 158 | } |
| 159 | |
| 160 | func (s *stub) GetMicrovm(_ context.Context, in *vms.GetMicrovmInput, _ ...func(*vms.Options)) (*vms.GetMicrovmOutput, error) { |
| 161 | s.note("GetMicrovm", in) |
| 162 | if s.getVM != nil { |
| 163 | return s.getVM(in) |
| 164 | } |
| 165 | return &vms.GetMicrovmOutput{MicrovmId: in.MicrovmIdentifier, State: types.MicrovmStateRunning, |
| 166 | ImageArn: aws.String("arn:aws:lambda:eu-west-1:640012953448:microvm-image:" + prefix + "-image")}, nil |
| 167 | } |
| 168 | |
| 169 | func (s *stub) ListMicrovms(_ context.Context, in *vms.ListMicrovmsInput, _ ...func(*vms.Options)) (*vms.ListMicrovmsOutput, error) { |
| 170 | s.note("ListMicrovms", in) |
| 171 | if s.listVMs != nil { |
| 172 | return s.listVMs(in) |
| 173 | } |
| 174 | return &vms.ListMicrovmsOutput{}, nil |
| 175 | } |
| 176 | |
| 177 | func (s *stub) SuspendMicrovm(_ context.Context, in *vms.SuspendMicrovmInput, _ ...func(*vms.Options)) (*vms.SuspendMicrovmOutput, error) { |
| 178 | s.note("SuspendMicrovm", in) |
| 179 | return &vms.SuspendMicrovmOutput{}, nil |
| 180 | } |
| 181 | |
| 182 | func (s *stub) ResumeMicrovm(_ context.Context, in *vms.ResumeMicrovmInput, _ ...func(*vms.Options)) (*vms.ResumeMicrovmOutput, error) { |
| 183 | s.note("ResumeMicrovm", in) |
| 184 | return &vms.ResumeMicrovmOutput{}, nil |
| 185 | } |
| 186 | |
| 187 | func (s *stub) TerminateMicrovm(_ context.Context, in *vms.TerminateMicrovmInput, _ ...func(*vms.Options)) (*vms.TerminateMicrovmOutput, error) { |
| 188 | s.note("TerminateMicrovm", in) |
| 189 | s.terminated = append(s.terminated, aws.ToString(in.MicrovmIdentifier)) |
| 190 | return &vms.TerminateMicrovmOutput{}, nil |
| 191 | } |
| 192 | |
| 193 | func (s *stub) GetCallerIdentity(_ context.Context, in *sts.GetCallerIdentityInput, _ ...func(*sts.Options)) (*sts.GetCallerIdentityOutput, error) { |
| 194 | s.note("GetCallerIdentity", in) |
| 195 | return &sts.GetCallerIdentityOutput{Account: aws.String(s.account), Arn: aws.String("arn:aws:iam::" + s.account + ":user/agent")}, nil |
| 196 | } |
| 197 | |
| 198 | // fakeClock moves only when the code sleeps. |
| 199 | type fakeClock struct { |
| 200 | now time.Time |
| 201 | sleeps int |
| 202 | } |
| 203 | |
| 204 | func (c *fakeClock) Now() time.Time { return c.now } |
| 205 | func (c *fakeClock) Sleep(d time.Duration) { c.now = c.now.Add(d); c.sleeps++ } |
| 206 | |
| 207 | type harness struct { |
| 208 | s *stub |
| 209 | clk *fakeClock |
| 210 | e *env |
| 211 | stdout *bytes.Buffer |
| 212 | stderr *bytes.Buffer |
| 213 | } |
| 214 | |
| 215 | func newHarness() *harness { |
| 216 | s := &stub{account: wantAccount} |
| 217 | h := &harness{s: s, clk: &fakeClock{now: time.Date(2026, 11, 28, 12, 0, 0, 0, time.UTC)}, |
| 218 | stdout: &bytes.Buffer{}, stderr: &bytes.Buffer{}} |
| 219 | h.e = &env{ctx: context.Background(), core: s, vms: s, sts: s, region: wantRegion, clock: h.clk, out: h.stdout, errw: h.stderr} |
| 220 | return h |
| 221 | } |
| 222 | |
| 223 | // do runs livectl with args and returns its exit code. |
| 224 | func (h *harness) do(args ...string) int { |
| 225 | h.stdout.Reset() |
| 226 | h.stderr.Reset() |
| 227 | h.e.prefix = "" |
| 228 | return run(h.e, args) |
| 229 | } |
| 230 | |
| 231 | // errLine decodes the one stderr line. |
| 232 | func (h *harness) errLine(t *testing.T) map[string]any { |
| 233 | t.Helper() |
| 234 | var m map[string]any |
| 235 | if err := json.Unmarshal(h.stderr.Bytes(), &m); err != nil || strings.Count(h.stderr.String(), "\n") != 1 { |
| 236 | t.Fatalf("stderr %q is not one JSON line", h.stderr) |
| 237 | } |
| 238 | return m |
| 239 | } |
| 240 | |
| 241 | func (h *harness) want(t *testing.T, code int, args ...string) { |
| 242 | t.Helper() |
| 243 | if got := h.do(args...); got != code { |
| 244 | t.Fatalf("livectl %q = exit %d, want %d; stderr %s", args, got, code, h.stderr) |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | func TestConnectorCreateInput(t *testing.T) { |
| 249 | h := newHarness() |
| 250 | h.want(t, exitOK, "-run", runID, "connector", "create", "-name", prefix+"-conn", "-role", "arn:aws:iam::640012953448:role/op", |
| 251 | "-subnet", "subnet-1", "-sg", "sg-1", "-client-token", prefix+"-conn-1", "-tag", "purpose=equipo-hosted-dev", "-tag", "run="+runID) |
| 252 | got := h.s.inputs[len(h.s.inputs)-1].(*lambdacore.CreateNetworkConnectorInput) |
| 253 | want := connectorCreateInput(prefix+"-conn", "arn:aws:iam::640012953448:role/op", "subnet-1", "sg-1", prefix+"-conn-1", |
| 254 | map[string]string{"purpose": "equipo-hosted-dev", "run": runID}) |
| 255 | if !reflect.DeepEqual(got, want) { |
| 256 | t.Fatalf("CreateNetworkConnector input = %+v, want %+v", got, want) |
| 257 | } |
| 258 | cfg := got.Configuration.(*coretypes.NetworkConnectorConfigurationMemberVpcEgressConfiguration).Value |
| 259 | if cfg.NetworkProtocol != "IPv4" || !reflect.DeepEqual(cfg.AssociatedComputeResourceTypes, []coretypes.ComputeResourceType{"MicroVm"}) || |
| 260 | cfg.ElasticIpConfiguration != nil || !reflect.DeepEqual(cfg.SubnetIds, []string{"subnet-1"}) || !reflect.DeepEqual(cfg.SecurityGroupIds, []string{"sg-1"}) { |
| 261 | t.Fatalf("connector configuration = %+v", cfg) |
| 262 | } |
| 263 | if h.s.calls[0] != "GetCallerIdentity" { |
| 264 | t.Fatalf("first call %s, want the caller check", h.s.calls[0]) |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | func hooksFile(t *testing.T) string { |
| 269 | t.Helper() |
| 270 | p := filepath.Join(t.TempDir(), "hooks.json") |
| 271 | body := `{"port":9000, |
| 272 | "microvmHooks":{"run":"ENABLED","runTimeoutInSeconds":10,"resume":"ENABLED","resumeTimeoutInSeconds":10, |
| 273 | "suspend":"ENABLED","suspendTimeoutInSeconds":10,"terminate":"ENABLED","terminateTimeoutInSeconds":10}, |
| 274 | "microvmImageHooks":{"ready":"ENABLED","readyTimeoutInSeconds":120,"validate":"DISABLED"}}` |
| 275 | if err := os.WriteFile(p, []byte(body), 0o600); err != nil { |
| 276 | t.Fatal(err) |
| 277 | } |
| 278 | return p |
| 279 | } |
| 280 | |
| 281 | func imageArgs(t *testing.T, memory string) []string { |
| 282 | return []string{"-description", "small", "-artifact", "s3://bucket/app.zip", "-base-version", "3", |
| 283 | "-build-role", "arn:aws:iam::640012953448:role/build", "-hooks", hooksFile(t), "-log-group", "/t17/lg", |
| 284 | "-client-token", prefix + "-image-create-1", "-memory", memory} |
| 285 | } |
| 286 | |
| 287 | func TestImageCreateAndUpdateInput(t *testing.T) { |
| 288 | h := newHarness() |
| 289 | h.want(t, exitOK, append([]string{"-run", runID, "image", "create", "-name", prefix + "-image", "-tag", "run=" + runID}, |
| 290 | imageArgs(t, "2048")...)...) |
| 291 | in := h.s.inputs[len(h.s.inputs)-1].(*vms.CreateMicrovmImageInput) |
| 292 | if aws.ToString(in.Name) != prefix+"-image" || aws.ToString(in.BaseImageArn) != defaultBase || aws.ToString(in.BaseImageVersion) != "3" || |
| 293 | aws.ToString(in.BuildRoleArn) != "arn:aws:iam::640012953448:role/build" || aws.ToString(in.ClientToken) != prefix+"-image-create-1" || |
| 294 | in.Tags["run"] != runID { |
| 295 | t.Fatalf("CreateMicrovmImage names = %+v", in) |
| 296 | } |
| 297 | if !reflect.DeepEqual(in.CpuConfigurations, []types.CpuConfiguration{{Architecture: "X86_64"}}) || |
| 298 | !reflect.DeepEqual(in.AdditionalOsCapabilities, []types.Capability{"ALL"}) || |
| 299 | aws.ToInt32(in.Resources[0].MinimumMemoryInMiB) != 2048 || len(in.Resources) != 1 { |
| 300 | t.Fatalf("CreateMicrovmImage shape = cpu %+v caps %+v res %+v", in.CpuConfigurations, in.AdditionalOsCapabilities, in.Resources) |
| 301 | } |
| 302 | if in.Hooks == nil || aws.ToInt32(in.Hooks.Port) != 9000 || in.Hooks.MicrovmHooks.Run != "ENABLED" || |
| 303 | aws.ToInt32(in.Hooks.MicrovmImageHooks.ReadyTimeoutInSeconds) != 120 || in.Hooks.MicrovmImageHooks.Validate != "DISABLED" { |
| 304 | t.Fatalf("hooks = %+v", in.Hooks) |
| 305 | } |
| 306 | if u, ok := in.CodeArtifact.(*types.CodeArtifactMemberUri); !ok || u.Value != "s3://bucket/app.zip" { |
| 307 | t.Fatalf("code artifact = %#v", in.CodeArtifact) |
| 308 | } |
| 309 | if l, ok := in.Logging.(*types.LoggingMemberCloudWatch); !ok || aws.ToString(l.Value.LogGroup) != "/t17/lg" { |
| 310 | t.Fatalf("logging = %#v", in.Logging) |
| 311 | } |
| 312 | |
| 313 | h.want(t, exitOK, append([]string{"-run", runID, "image", "update", "-id", "arn:img"}, imageArgs(t, "4096")...)...) |
| 314 | up := h.s.inputs[len(h.s.inputs)-1].(*vms.UpdateMicrovmImageInput) |
| 315 | if aws.ToString(up.ImageIdentifier) != "arn:img" || aws.ToInt32(up.Resources[0].MinimumMemoryInMiB) != 4096 || |
| 316 | !reflect.DeepEqual(up.CpuConfigurations, in.CpuConfigurations) { |
| 317 | t.Fatalf("UpdateMicrovmImage = %+v", up) |
| 318 | } |
| 319 | if h.s.count("GetMicrovmImage") != 1 { |
| 320 | t.Fatal("update did not read the image first") |
| 321 | } |
| 322 | |
| 323 | // A hooks file with an unknown key is a usage error, before any create. |
| 324 | bad := filepath.Join(t.TempDir(), "bad.json") |
| 325 | _ = os.WriteFile(bad, []byte(`{"port":9000,"prot":1}`), 0o600) |
| 326 | args := append([]string{"-run", runID, "image", "create", "-name", prefix + "-image"}, imageArgs(t, "2048")...) |
| 327 | for i := range args { |
| 328 | if args[i] == "-hooks" { |
| 329 | args[i+1] = bad |
| 330 | } |
| 331 | } |
| 332 | before := h.s.count("CreateMicrovmImage") |
| 333 | h.want(t, exitUsage, args...) |
| 334 | if h.s.count("CreateMicrovmImage") != before { |
| 335 | t.Fatal("a bad hooks file still reached CreateMicrovmImage") |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | func TestVMRunInput(t *testing.T) { |
| 340 | h := newHarness() |
| 341 | payload := filepath.Join(t.TempDir(), "payload") |
| 342 | _ = os.WriteFile(payload, []byte(`{"code":"x"}`+"\n"), 0o600) |
| 343 | image := "arn:aws:lambda:eu-west-1:640012953448:microvm-image:" + prefix + "-image" |
| 344 | h.want(t, exitOK, "-run", runID, "vm", "run", "-image", image, "-version", "1", |
| 345 | "-connector", "arn:aws:lambda:eu-west-1:640012953448:network-connector:nc-1:1", "-max-seconds", "900", |
| 346 | "-client-token", prefix+"-launch-1", "-payload-file", payload) |
| 347 | in := h.s.inputs[len(h.s.inputs)-1].(*vms.RunMicrovmInput) |
| 348 | want := &vms.RunMicrovmInput{ImageIdentifier: aws.String(image), ImageVersion: aws.String("1"), |
| 349 | EgressNetworkConnectors: []string{"arn:aws:lambda:eu-west-1:640012953448:network-connector:nc-1:1"}, |
| 350 | IngressNetworkConnectors: []string{"arn:aws:lambda:eu-west-1:aws:network-connector:aws-network-connector:NO_INGRESS"}, |
| 351 | MaximumDurationInSeconds: aws.Int32(900), ClientToken: aws.String(prefix + "-launch-1"), RunHookPayload: aws.String(`{"code":"x"}` + "\n")} |
| 352 | if !reflect.DeepEqual(in, want) { |
| 353 | t.Fatalf("RunMicrovm input = %+v, want %+v (no role, no idle policy)", in, want) |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | // TestOutputKeepsAllFields: every exported field of each output the tool |
| 358 | // prints is a key, null or empty when unset, and ResultMetadata shows the |
| 359 | // server time and request ID. |
| 360 | func TestOutputKeepsAllFields(t *testing.T) { |
| 361 | for _, v := range []any{ |
| 362 | &lambdacore.CreateNetworkConnectorOutput{}, &lambdacore.GetNetworkConnectorOutput{}, &lambdacore.DeleteNetworkConnectorOutput{}, |
| 363 | &vms.CreateMicrovmImageOutput{}, &vms.UpdateMicrovmImageOutput{}, &vms.GetMicrovmImageOutput{}, &vms.GetMicrovmImageVersionOutput{}, |
| 364 | &vms.DeleteMicrovmImageOutput{}, &vms.RunMicrovmOutput{}, &vms.GetMicrovmOutput{}, &vms.SuspendMicrovmOutput{}, |
| 365 | &vms.ResumeMicrovmOutput{}, &vms.TerminateMicrovmOutput{}, |
| 366 | } { |
| 367 | var buf bytes.Buffer |
| 368 | if err := printJSON(&buf, v); err != nil { |
| 369 | t.Fatal(err) |
| 370 | } |
| 371 | var m map[string]json.RawMessage |
| 372 | if err := json.Unmarshal(buf.Bytes(), &m); err != nil { |
| 373 | t.Fatalf("%T printed %s", v, buf.String()) |
| 374 | } |
| 375 | rt := reflect.TypeOf(v).Elem() |
| 376 | for i := range rt.NumField() { |
| 377 | if f := rt.Field(i); f.IsExported() { |
| 378 | if _, ok := m[f.Name]; !ok { |
| 379 | t.Errorf("%T output lacks %s: %s", v, f.Name, buf.String()) |
| 380 | } |
| 381 | } |
| 382 | } |
| 383 | if string(m["ResultMetadata"]) != `{"ServerTime":null,"RequestID":""}` { |
| 384 | t.Errorf("%T ResultMetadata = %s", v, m["ResultMetadata"]) |
| 385 | } |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | func TestConnectorGetGolden(t *testing.T) { |
| 390 | at := time.Date(2026, 11, 26, 11, 31, 23, 0, time.UTC) |
| 391 | out := &lambdacore.GetNetworkConnectorOutput{ |
| 392 | Arn: aws.String("arn:aws:lambda:eu-west-1:640012953448:network-connector:nc-1"), Id: aws.String("nc-1"), Name: aws.String(prefix + "-conn"), |
| 393 | Configuration: &coretypes.NetworkConnectorConfigurationMemberVpcEgressConfiguration{Value: coretypes.NetworkConnectorVpcEgressConfiguration{ |
| 394 | AssociatedComputeResourceTypes: []coretypes.ComputeResourceType{"MicroVm"}, |
| 395 | ElasticIpConfiguration: &coretypes.ElasticIpConfiguration{Type: "None"}, |
| 396 | NetworkProtocol: "IPv4", SecurityGroupIds: []string{"sg-1"}, SubnetIds: []string{"subnet-1"}}}, |
| 397 | LastModified: &at, LastUpdateStatus: "Successful", LastUpdateStatusReason: aws.String("done"), LastUpdateStatusReasonCode: "Ok", |
| 398 | LatestVersion: aws.Int64(1), LatestVersionArn: aws.String("arn:aws:lambda:eu-west-1:640012953448:network-connector:nc-1:1"), |
| 399 | OperatorRole: aws.String("arn:aws:iam::640012953448:role/op"), State: "ACTIVE", StateReason: aws.String("Initial creation"), |
| 400 | StateReasonCode: "Creating", Version: aws.Int64(1), VersionArn: aws.String("arn:aws:lambda:eu-west-1:640012953448:network-connector:nc-1:1"), |
| 401 | } |
| 402 | awsmiddleware.SetRequestIDMetadata(&out.ResultMetadata, "req-7") |
| 403 | var buf bytes.Buffer |
| 404 | if err := printJSON(&buf, out); err != nil { |
| 405 | t.Fatal(err) |
| 406 | } |
| 407 | const golden = `{"Arn":"arn:aws:lambda:eu-west-1:640012953448:network-connector:nc-1",` + |
| 408 | `"Configuration":{"Value":{"AssociatedComputeResourceTypes":["MicroVm"],"ElasticIpConfiguration":{"Type":"None"},"NetworkProtocol":"IPv4","SecurityGroupIds":["sg-1"],"SubnetIds":["subnet-1"]}},` + |
| 409 | `"Id":"nc-1","LastModified":"2026-11-26T11:31:23Z","LastUpdateStatus":"Successful","LastUpdateStatusReason":"done","LastUpdateStatusReasonCode":"Ok",` + |
| 410 | `"LatestVersion":1,"LatestVersionArn":"arn:aws:lambda:eu-west-1:640012953448:network-connector:nc-1:1",` + |
| 411 | `"Name":"equipo-hosted-dev-t17-1128-abc123-conn","OperatorRole":"arn:aws:iam::640012953448:role/op",` + |
| 412 | `"ResultMetadata":{"ServerTime":null,"RequestID":"req-7"},"State":"ACTIVE","StateReason":"Initial creation","StateReasonCode":"Creating",` + |
| 413 | `"Version":1,"VersionArn":"arn:aws:lambda:eu-west-1:640012953448:network-connector:nc-1:1"}` + "\n" |
| 414 | if buf.String() != golden { |
| 415 | t.Fatalf("printed\n%s\nwant\n%s", buf.String(), golden) |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | // apiErr is an SDK-shaped error: an API error inside the HTTP response |
| 420 | // error the SDK wraps it in. |
| 421 | func apiErr(code string, status int) error { |
| 422 | return &smithy.OperationError{ServiceID: "Lambda", OperationName: "Op", Err: &awshttp.ResponseError{ |
| 423 | ResponseError: &smithyhttp.ResponseError{Response: &smithyhttp.Response{Response: &http.Response{StatusCode: status}}, |
| 424 | Err: &smithy.GenericAPIError{Code: code, Message: "service text"}}, |
| 425 | RequestID: "req-1"}} |
| 426 | } |
| 427 | |
| 428 | func TestErrorShapeAndExitCodes(t *testing.T) { |
| 429 | h := newHarness() |
| 430 | h.s.getConn = func(*lambdacore.GetNetworkConnectorInput) (*lambdacore.GetNetworkConnectorOutput, error) { |
| 431 | return nil, apiErr("ResourceNotFoundException", 404) |
| 432 | } |
| 433 | h.want(t, exitAPI, "connector", "get", "-id", "nc-1") |
| 434 | got := h.errLine(t) |
| 435 | want := map[string]any{"error": "api", "op": "GetNetworkConnector", "code": "ResourceNotFoundException", "http_status": 404.0, |
| 436 | "message": "GetNetworkConnector failed", "aws_message": "service text", "request_id": "req-1"} |
| 437 | if !reflect.DeepEqual(got, want) { |
| 438 | t.Fatalf("error line = %v, want %v", got, want) |
| 439 | } |
| 440 | if h.stdout.Len() != 0 { |
| 441 | t.Fatalf("stdout %q on an API error, want nothing", h.stdout) |
| 442 | } |
| 443 | |
| 444 | h.s.getConn = func(*lambdacore.GetNetworkConnectorInput) (*lambdacore.GetNetworkConnectorOutput, error) { |
| 445 | return nil, errors.New("dial tcp: connection refused") |
| 446 | } |
| 447 | h.want(t, exitAPI, "connector", "get", "-id", "nc-1") |
| 448 | if l := h.errLine(t); l["error"] != "transport" || l["code"] != nil { |
| 449 | t.Fatalf("transport error line = %v", l) |
| 450 | } |
| 451 | |
| 452 | for _, args := range [][]string{{}, {"connector"}, {"connector", "nope"}, {"connector", "get"}, {"connector", "get", "-id"}, |
| 453 | {"-run", "UPPER", "caller"}, {"vm", "wait-state", "-id", "vm-1", "-state", "PENDING"}, {"caller", "extra"}} { |
| 454 | if got := h.do(args...); got != exitUsage || h.errLine(t)["error"] != "usage" { |
| 455 | t.Errorf("livectl %q = exit %d %s, want usage exit %d", args, got, h.stderr, exitUsage) |
| 456 | } |
| 457 | } |
| 458 | h.want(t, exitOK, "caller") |
| 459 | if !strings.Contains(h.stdout.String(), `"Account":"640012953448"`) || !strings.Contains(h.stdout.String(), `"Region":"eu-west-1"`) { |
| 460 | t.Fatalf("caller printed %s", h.stdout) |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | // TestAccountGuard: another account is refused (exit 4) before any |
| 465 | // Lambda call. |
| 466 | func TestAccountGuard(t *testing.T) { |
| 467 | h := newHarness() |
| 468 | h.s.account = "149757027610" |
| 469 | h.want(t, exitGuard, "connector", "get", "-id", "nc-1") |
| 470 | if l := h.errLine(t); l["error"] != "guard" || !strings.Contains(l["message"].(string), "149757027610") { |
| 471 | t.Fatalf("guard line = %v", l) |
| 472 | } |
| 473 | if !reflect.DeepEqual(h.s.calls, []string{"GetCallerIdentity"}) { |
| 474 | t.Fatalf("calls by a refused command = %v, want only the caller check", h.s.calls) |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | // TestRegionGuard: another region is refused (exit 4) before any call. |
| 479 | func TestRegionGuard(t *testing.T) { |
| 480 | h := newHarness() |
| 481 | h.e.region = "us-east-1" |
| 482 | h.want(t, exitGuard, "-run", runID, "vm", "terminate", "-id", "vm-1") |
| 483 | if l := h.errLine(t); l["error"] != "guard" || !strings.Contains(l["message"].(string), "us-east-1") { |
| 484 | t.Fatalf("guard line = %v", l) |
| 485 | } |
| 486 | if len(h.s.calls) != 0 { |
| 487 | t.Fatalf("calls by a command refused for its region = %v, want none", h.s.calls) |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | const outsideImage = "arn:aws:lambda:eu-west-1:640012953448:microvm-image:runner" |
| 492 | |
| 493 | func connectorCreateArgs(run, name string) []string { |
| 494 | args := []string{"connector", "create", "-name", name, "-role", "r", "-subnet", "s", "-sg", "g", "-client-token", name + "-1"} |
| 495 | if run != "" { |
| 496 | args = append([]string{"-run", run}, args...) |
| 497 | } |
| 498 | return args |
| 499 | } |
| 500 | |
| 501 | func vmRunArgs(t *testing.T, image, token string) []string { |
| 502 | payload := filepath.Join(t.TempDir(), "payload") |
| 503 | if err := os.WriteFile(payload, []byte("p"), 0o600); err != nil { |
| 504 | t.Fatal(err) |
| 505 | } |
| 506 | return []string{"-run", runID, "vm", "run", "-image", image, "-version", "1", "-connector", "c", "-max-seconds", "60", |
| 507 | "-client-token", token, "-payload-file", payload} |
| 508 | } |
| 509 | |
| 510 | // TestRunPrefixGuard: each create, delete, update and VM change is refused |
| 511 | // (exit 4) for a resource outside the run, one case per guard, so each |
| 512 | // guard is shown on its own; the refused call never reaches the SDK. |
| 513 | func TestRunPrefixGuard(t *testing.T) { |
| 514 | outsideConn := func(h *harness) { |
| 515 | h.s.getConn = func(*lambdacore.GetNetworkConnectorInput) (*lambdacore.GetNetworkConnectorOutput, error) { |
| 516 | return &lambdacore.GetNetworkConnectorOutput{Name: aws.String("production-egress")}, nil |
| 517 | } |
| 518 | } |
| 519 | outsideImg := func(h *harness) { |
| 520 | h.s.getImg = func(*vms.GetMicrovmImageInput) (*vms.GetMicrovmImageOutput, error) { |
| 521 | return &vms.GetMicrovmImageOutput{Name: aws.String("runner")}, nil |
| 522 | } |
| 523 | } |
| 524 | outsideVM := func(h *harness) { |
| 525 | h.s.getVM = func(in *vms.GetMicrovmInput) (*vms.GetMicrovmOutput, error) { |
| 526 | return &vms.GetMicrovmOutput{MicrovmId: in.MicrovmIdentifier, ImageArn: aws.String(outsideImage)}, nil |
| 527 | } |
| 528 | } |
| 529 | inRunImage := "arn:aws:lambda:eu-west-1:640012953448:microvm-image:" + prefix + "-image" |
| 530 | for _, c := range []struct { |
| 531 | name string |
| 532 | setup func(*harness) |
| 533 | args func(*testing.T) []string |
| 534 | op string |
| 535 | }{ |
| 536 | {"connector create, another run", nil, func(*testing.T) []string { |
| 537 | return connectorCreateArgs(runID, "equipo-hosted-dev-t17-other-conn") |
| 538 | }, "CreateNetworkConnector"}, |
| 539 | {"connector create, prefix without boundary", nil, func(*testing.T) []string { |
| 540 | return connectorCreateArgs(runID, prefix+"x-conn") |
| 541 | }, "CreateNetworkConnector"}, |
| 542 | {"connector create, no -run", nil, func(*testing.T) []string { |
| 543 | return connectorCreateArgs("", prefix+"-conn") |
| 544 | }, "CreateNetworkConnector"}, |
| 545 | {"image create", nil, func(t *testing.T) []string { |
| 546 | return append([]string{"-run", runID, "image", "create", "-name", "someone-else"}, imageArgs(t, "2048")...) |
| 547 | }, "CreateMicrovmImage"}, |
| 548 | {"vm run, image outside", nil, func(t *testing.T) []string { return vmRunArgs(t, outsideImage, prefix+"-launch-1") }, "RunMicrovm"}, |
| 549 | {"vm run, token outside", nil, func(t *testing.T) []string { return vmRunArgs(t, inRunImage, "someone-launch-1") }, "RunMicrovm"}, |
| 550 | {"connector delete", outsideConn, func(*testing.T) []string { |
| 551 | return []string{"-run", runID, "connector", "delete", "-id", "nc-prod"} |
| 552 | }, "DeleteNetworkConnector"}, |
| 553 | {"image delete", outsideImg, func(*testing.T) []string { |
| 554 | return []string{"-run", runID, "image", "delete", "-id", "arn:runner"} |
| 555 | }, "DeleteMicrovmImage"}, |
| 556 | {"image update", outsideImg, func(t *testing.T) []string { |
| 557 | return append([]string{"-run", runID, "image", "update", "-id", "arn:runner"}, imageArgs(t, "4096")...) |
| 558 | }, "UpdateMicrovmImage"}, |
| 559 | {"vm terminate", outsideVM, func(*testing.T) []string { return []string{"-run", runID, "vm", "terminate", "-id", "vm-x"} }, "TerminateMicrovm"}, |
| 560 | {"vm suspend", outsideVM, func(*testing.T) []string { return []string{"-run", runID, "vm", "suspend", "-id", "vm-x"} }, "SuspendMicrovm"}, |
| 561 | {"vm resume", outsideVM, func(*testing.T) []string { return []string{"-run", runID, "vm", "resume", "-id", "vm-x"} }, "ResumeMicrovm"}, |
| 562 | } { |
| 563 | t.Run(c.name, func(t *testing.T) { |
| 564 | h := newHarness() |
| 565 | if c.setup != nil { |
| 566 | c.setup(h) |
| 567 | } |
| 568 | h.want(t, exitGuard, c.args(t)...) |
| 569 | if l := h.errLine(t); l["error"] != "guard" { |
| 570 | t.Fatalf("refusal line = %v", l) |
| 571 | } |
| 572 | if n := h.s.count(c.op); n != 0 { |
| 573 | t.Fatalf("%s reached the SDK %d times outside the run", c.op, n) |
| 574 | } |
| 575 | }) |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | // TestRunPrefixAllowsTheRun: the same commands go ahead inside the run. |
| 580 | func TestRunPrefixAllowsTheRun(t *testing.T) { |
| 581 | h := newHarness() |
| 582 | h.want(t, exitOK, connectorCreateArgs(runID, prefix+"-conn")...) |
| 583 | h.want(t, exitOK, vmRunArgs(t, "arn:aws:lambda:eu-west-1:640012953448:microvm-image:"+prefix+"-image", prefix+"-launch-1")...) |
| 584 | h.want(t, exitOK, "-run", runID, "connector", "delete", "-id", "nc-1") |
| 585 | h.want(t, exitOK, "-run", runID, "image", "delete", "-id", "arn:img") |
| 586 | h.want(t, exitOK, "-run", runID, "vm", "terminate", "-id", "vm-1") |
| 587 | if !reflect.DeepEqual(h.s.terminated, []string{"vm-1"}) { |
| 588 | t.Fatalf("terminated %v, want the run's VM", h.s.terminated) |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | // TestBoundedWaits: waits read at once, then every interval, and stop at |
| 593 | // the timeout with exit 5; the fake clock moves only when they sleep. |
| 594 | func TestBoundedWaits(t *testing.T) { |
| 595 | h := newHarness() |
| 596 | h.s.getConn = func(*lambdacore.GetNetworkConnectorInput) (*lambdacore.GetNetworkConnectorOutput, error) { |
| 597 | return &lambdacore.GetNetworkConnectorOutput{State: coretypes.NetworkConnectorStatePending}, nil |
| 598 | } |
| 599 | h.want(t, exitTimeout, "connector", "wait-active", "-id", "nc-1", "-timeout", "1m", "-interval", "15s") |
| 600 | if n := h.s.count("GetNetworkConnector"); n != 4 { |
| 601 | t.Fatalf("reads in a 1m wait every 15s = %d, want 4 (0, 15, 30, 45 s; none at the deadline)", n) |
| 602 | } |
| 603 | if !strings.Contains(h.stdout.String(), `"State":"PENDING"`) || h.errLine(t)["error"] != "timeout" { |
| 604 | t.Fatalf("timed-out wait printed %s / %s", h.stdout, h.stderr) |
| 605 | } |
| 606 | h.want(t, exitUsage, "connector", "wait-active", "-id", "nc-1", "-timeout", "61m") |
| 607 | h.want(t, exitUsage, "connector", "wait-active", "-id", "nc-1", "-interval", "10ms") |
| 608 | |
| 609 | reads := 0 |
| 610 | h.s.getConn = func(*lambdacore.GetNetworkConnectorInput) (*lambdacore.GetNetworkConnectorOutput, error) { |
| 611 | reads++ |
| 612 | if reads < 3 { |
| 613 | return &lambdacore.GetNetworkConnectorOutput{State: coretypes.NetworkConnectorStatePending}, nil |
| 614 | } |
| 615 | return &lambdacore.GetNetworkConnectorOutput{State: coretypes.NetworkConnectorStateActive, Version: aws.Int64(1)}, nil |
| 616 | } |
| 617 | start := h.clk.now |
| 618 | h.want(t, exitOK, "connector", "wait-active", "-id", "nc-1", "-interval", "15s") |
| 619 | if d := h.clk.now.Sub(start); d != 30*time.Second { |
| 620 | t.Fatalf("wait took %v of fake time, want 30s", d) |
| 621 | } |
| 622 | |
| 623 | h.s.getImgVer = func(*vms.GetMicrovmImageVersionInput) (*vms.GetMicrovmImageVersionOutput, error) { |
| 624 | return &vms.GetMicrovmImageVersionOutput{State: types.MicrovmImageVersionStateFailed}, nil |
| 625 | } |
| 626 | h.want(t, exitFailed, "image", "wait-version", "-id", "arn:img", "-version", "1") |
| 627 | h.s.getImgVer = func(*vms.GetMicrovmImageVersionInput) (*vms.GetMicrovmImageVersionOutput, error) { |
| 628 | return &vms.GetMicrovmImageVersionOutput{State: types.MicrovmImageVersionStateSuccessful, Status: types.MicrovmImageVersionStatusActive}, nil |
| 629 | } |
| 630 | h.want(t, exitOK, "image", "wait-version", "-id", "arn:img", "-version", "1") |
| 631 | |
| 632 | h.s.getVM = func(in *vms.GetMicrovmInput) (*vms.GetMicrovmOutput, error) { |
| 633 | return &vms.GetMicrovmOutput{MicrovmId: in.MicrovmIdentifier, State: types.MicrovmStateTerminated}, nil |
| 634 | } |
| 635 | h.want(t, exitFailed, "vm", "wait-state", "-id", "vm-1", "-state", "RUNNING") |
| 636 | h.want(t, exitOK, "vm", "wait-state", "-id", "vm-1", "-state", "TERMINATED") |
| 637 | } |
| 638 | |
| 639 | // TestWaitNeverSucceedsLate: an answer that comes back after the deadline |
| 640 | // is a timeout (exit 5), not a success. |
| 641 | func TestWaitNeverSucceedsLate(t *testing.T) { |
| 642 | h := newHarness() |
| 643 | h.s.getConn = func(*lambdacore.GetNetworkConnectorInput) (*lambdacore.GetNetworkConnectorOutput, error) { |
| 644 | h.clk.now = h.clk.now.Add(2 * time.Minute) // the read takes longer than the whole wait |
| 645 | return &lambdacore.GetNetworkConnectorOutput{State: coretypes.NetworkConnectorStateActive}, nil |
| 646 | } |
| 647 | h.want(t, exitTimeout, "connector", "wait-active", "-id", "nc-1", "-timeout", "1m") |
| 648 | } |
| 649 | |
| 650 | // TestWaitEndsStalledRead: a read that never answers ends with the wait |
| 651 | // (exit 5), through the deadline the wait puts on the read's context. |
| 652 | func TestWaitEndsStalledRead(t *testing.T) { |
| 653 | h := newHarness() |
| 654 | h.e.clock = realClock{} |
| 655 | h.s.getConn = func(*lambdacore.GetNetworkConnectorInput) (*lambdacore.GetNetworkConnectorOutput, error) { |
| 656 | select { |
| 657 | case <-h.e.ctx.Done(): |
| 658 | return nil, h.e.ctx.Err() |
| 659 | case <-time.After(5 * time.Second): |
| 660 | return nil, errors.New("the read's context had no deadline") |
| 661 | } |
| 662 | } |
| 663 | start := time.Now() |
| 664 | h.want(t, exitTimeout, "connector", "wait-active", "-id", "nc-1", "-timeout", "1s", "-interval", "1s") |
| 665 | if d := time.Since(start); d > 3*time.Second { |
| 666 | t.Fatalf("a 1s wait on a stalled read took %v", d) |
| 667 | } |
| 668 | } |
| 669 | |
| 670 | // TestWaitGoneReportsDeleteFailed: not found ends wait-gone well; a |
| 671 | // DELETE_FAILED connector ends it at once with exit 6, code DELETE_FAILED, |
| 672 | // and its last read on stdout. |
| 673 | func TestWaitGoneReportsDeleteFailed(t *testing.T) { |
| 674 | h := newHarness() |
| 675 | h.s.getConn = func(*lambdacore.GetNetworkConnectorInput) (*lambdacore.GetNetworkConnectorOutput, error) { |
| 676 | return &lambdacore.GetNetworkConnectorOutput{State: coretypes.NetworkConnectorStateDeleteFailed, StateReason: aws.String("ENI in use")}, nil |
| 677 | } |
| 678 | h.want(t, exitFailed, "connector", "wait-gone", "-id", "nc-1") |
| 679 | if l := h.errLine(t); l["error"] != "failed_state" || l["code"] != "DELETE_FAILED" { |
| 680 | t.Fatalf("DELETE_FAILED line = %v", l) |
| 681 | } |
| 682 | if !strings.Contains(h.stdout.String(), `"StateReason":"ENI in use"`) || h.s.count("GetNetworkConnector") != 1 { |
| 683 | t.Fatalf("DELETE_FAILED stdout %s after %d reads", h.stdout, h.s.count("GetNetworkConnector")) |
| 684 | } |
| 685 | reads := 0 |
| 686 | h.s.getConn = func(*lambdacore.GetNetworkConnectorInput) (*lambdacore.GetNetworkConnectorOutput, error) { |
| 687 | if reads++; reads == 1 { |
| 688 | return &lambdacore.GetNetworkConnectorOutput{State: coretypes.NetworkConnectorStateDeleting}, nil |
| 689 | } |
| 690 | return nil, apiErr("ResourceNotFoundException", 404) |
| 691 | } |
| 692 | h.want(t, exitOK, "connector", "wait-gone", "-id", "nc-1") |
| 693 | if !strings.Contains(h.stdout.String(), `"Gone":true`) { |
| 694 | t.Fatalf("wait-gone printed %s", h.stdout) |
| 695 | } |
| 696 | } |
| 697 | |
| 698 | func TestBaseVersionsSortedAllPages(t *testing.T) { |
| 699 | h := newHarness() |
| 700 | v := func(s string, st types.ManagedMicrovmImageVersionStatus) types.ManagedMicrovmImageVersion { |
| 701 | return types.ManagedMicrovmImageVersion{ImageVersion: aws.String(s), Status: st} |
| 702 | } |
| 703 | h.s.baseVers = func(in *vms.ListManagedMicrovmImageVersionsInput) (*vms.ListManagedMicrovmImageVersionsOutput, error) { |
| 704 | if aws.ToString(in.ImageIdentifier) != defaultBase { |
| 705 | t.Errorf("base = %s", aws.ToString(in.ImageIdentifier)) |
| 706 | } |
| 707 | if in.NextToken == nil { |
| 708 | return &vms.ListManagedMicrovmImageVersionsOutput{Items: []types.ManagedMicrovmImageVersion{v("3", "AVAILABLE"), v("10", "DEPRECATED")}, |
| 709 | NextToken: aws.String("p2")}, nil |
| 710 | } |
| 711 | return &vms.ListManagedMicrovmImageVersionsOutput{Items: []types.ManagedMicrovmImageVersion{v("9", "AVAILABLE"), v("3.1", "AVAILABLE")}}, nil |
| 712 | } |
| 713 | h.want(t, exitOK, "image", "base-versions") |
| 714 | var got baseVersions |
| 715 | if err := json.Unmarshal(h.stdout.Bytes(), &got); err != nil { |
| 716 | t.Fatal(err) |
| 717 | } |
| 718 | var order []string |
| 719 | for _, it := range got.Items { |
| 720 | order = append(order, aws.ToString(it.ImageVersion)+"/"+string(it.Status)) |
| 721 | } |
| 722 | if want := []string{"10/DEPRECATED", "9/AVAILABLE", "3.1/AVAILABLE", "3/AVAILABLE"}; !reflect.DeepEqual(order, want) || got.Pages != 2 { |
| 723 | t.Fatalf("base versions = %v over %d pages, want %v over 2", order, got.Pages, want) |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | // metadataOf must find the field the SDK outputs carry. |
| 728 | func TestMetadataOf(t *testing.T) { |
| 729 | out := &vms.GetMicrovmOutput{} |
| 730 | awsmiddleware.SetRequestIDMetadata(&out.ResultMetadata, "r") |
| 731 | if md, ok := metadataOf(out); !ok || md.Has(struct{}{}) { |
| 732 | t.Fatalf("metadataOf = %v, %v", md, ok) |
| 733 | } |
| 734 | if _, ok := metadataOf(map[string]any{}); ok { |
| 735 | t.Fatal("metadataOf found metadata in a map") |
| 736 | } |
| 737 | var _ middleware.Metadata |
| 738 | } |
| 739 | |