|
| 1 | +// Copyright 2026 The gVisor Authors. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +//go:build linux |
| 16 | + |
| 17 | +package runsccmd |
| 18 | + |
| 19 | +import ( |
| 20 | + "encoding/json" |
| 21 | + "fmt" |
| 22 | + "os" |
| 23 | + "path/filepath" |
| 24 | + "strings" |
| 25 | + "testing" |
| 26 | + |
| 27 | + specs "github.com/opencontainers/runtime-spec/specs-go" |
| 28 | +) |
| 29 | + |
| 30 | +// TestRunscUpdateCLI verifies Update runs the OCI update flow: JSON resources on |
| 31 | +// stdin and "update --resources - <id>" in argv (same contract as go-runc). |
| 32 | +func TestRunscUpdateCLI(t *testing.T) { |
| 33 | + ctx := t.Context() |
| 34 | + dir := t.TempDir() |
| 35 | + argsLog := filepath.Join(dir, "args.txt") |
| 36 | + stdinLog := filepath.Join(dir, "stdin.txt") |
| 37 | + script := filepath.Join(dir, "fake-runsc") |
| 38 | + scriptBody := fmt.Sprintf(`#!/bin/sh |
| 39 | +printf '%%s\n' "$*" >%q |
| 40 | +cat >%q |
| 41 | +exit 0 |
| 42 | +`, argsLog, stdinLog) |
| 43 | + if err := os.WriteFile(script, []byte(scriptBody), 0o755); err != nil { |
| 44 | + t.Fatal(err) |
| 45 | + } |
| 46 | + |
| 47 | + limit := int64(4096) |
| 48 | + wantRes := &specs.LinuxResources{ |
| 49 | + Memory: &specs.LinuxMemory{Limit: &limit}, |
| 50 | + } |
| 51 | + |
| 52 | + r := &Runsc{ |
| 53 | + Command: script, |
| 54 | + Root: filepath.Join(dir, "root"), |
| 55 | + } |
| 56 | + if err := r.Update(ctx, "cid-1", wantRes); err != nil { |
| 57 | + t.Fatalf("Update: %v", err) |
| 58 | + } |
| 59 | + |
| 60 | + rawArgs, err := os.ReadFile(argsLog) |
| 61 | + if err != nil { |
| 62 | + t.Fatal(err) |
| 63 | + } |
| 64 | + args := strings.Fields(string(rawArgs)) |
| 65 | + var joined strings.Builder |
| 66 | + for _, a := range args { |
| 67 | + joined.WriteString(a) |
| 68 | + joined.WriteByte(' ') |
| 69 | + } |
| 70 | + s := joined.String() |
| 71 | + // go-runc may pass "--resources -"; we use "--resources=-" (same stdin contract). |
| 72 | + if !strings.Contains(s, "update ") || !strings.Contains(s, "--resources") || !strings.Contains(s, "cid-1") { |
| 73 | + t.Fatalf("argv missing expected update flags: %q", string(rawArgs)) |
| 74 | + } |
| 75 | + |
| 76 | + rawStdin, err := os.ReadFile(stdinLog) |
| 77 | + if err != nil { |
| 78 | + t.Fatal(err) |
| 79 | + } |
| 80 | + var got specs.LinuxResources |
| 81 | + if err := json.Unmarshal(rawStdin, &got); err != nil { |
| 82 | + t.Fatalf("stdin JSON: %v", err) |
| 83 | + } |
| 84 | + if got.Memory == nil || got.Memory.Limit == nil || *got.Memory.Limit != limit { |
| 85 | + t.Fatalf("stdin resources: got %+v, want memory limit %d", got.Memory, limit) |
| 86 | + } |
| 87 | +} |
0 commit comments