|
| 1 | +// Copyright (c) 2023-2026, Nubificus LTD |
| 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 | +// http://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 | +package hypervisors |
| 16 | + |
| 17 | +import ( |
| 18 | + "strings" |
| 19 | + "testing" |
| 20 | + |
| 21 | + "github.com/stretchr/testify/assert" |
| 22 | + "github.com/urunc-dev/urunc/pkg/unikontainers/types" |
| 23 | +) |
| 24 | + |
| 25 | +const testFCBinary = "/usr/bin/firecracker" |
| 26 | + |
| 27 | +// TestFirecrackerBuildExecCmdSocket verifies Firecracker enables --api-sock |
| 28 | +// only when socket_path is set, and restores --no-api otherwise. |
| 29 | +func TestFirecrackerBuildExecCmdSocket(t *testing.T) { |
| 30 | + t.Parallel() |
| 31 | + |
| 32 | + tests := []struct { |
| 33 | + name string |
| 34 | + args types.ExecArgs |
| 35 | + mustContain []string |
| 36 | + mustNotContain []string |
| 37 | + }{ |
| 38 | + { |
| 39 | + name: "configured SocketPath renders --api-sock and keeps --config-file", |
| 40 | + args: types.ExecArgs{ |
| 41 | + UnikernelPath: testKernelPath, |
| 42 | + Command: testCommand, |
| 43 | + SocketPath: "/run/urunc/fc.sock", |
| 44 | + }, |
| 45 | + mustContain: []string{"--api-sock /run/urunc/fc.sock", "--config-file"}, |
| 46 | + mustNotContain: []string{"--no-api"}, |
| 47 | + }, |
| 48 | + { |
| 49 | + name: "unset SocketPath restores --no-api and omits --api-sock", |
| 50 | + args: types.ExecArgs{ |
| 51 | + UnikernelPath: testKernelPath, |
| 52 | + Command: testCommand, |
| 53 | + ContainerID: "abc123", |
| 54 | + }, |
| 55 | + mustContain: []string{"--no-api", "--config-file"}, |
| 56 | + mustNotContain: []string{"--api-sock"}, |
| 57 | + }, |
| 58 | + } |
| 59 | + |
| 60 | + for _, tt := range tests { |
| 61 | + t.Run(tt.name, func(t *testing.T) { |
| 62 | + t.Parallel() |
| 63 | + |
| 64 | + fc := &Firecracker{binary: FirecrackerBinary, binaryPath: testFCBinary} |
| 65 | + out, err := fc.BuildExecCmd(tt.args, &fakeUnikernel{}) |
| 66 | + assert.NoError(t, err) |
| 67 | + assert.NotEmpty(t, out) |
| 68 | + |
| 69 | + assert.Equal(t, testFCBinary, out[0], "binary path must be the first element") |
| 70 | + joined := strings.Join(out, " ") |
| 71 | + |
| 72 | + for _, want := range tt.mustContain { |
| 73 | + assert.Contains(t, joined, want, "expected %q to be present", want) |
| 74 | + } |
| 75 | + for _, notWant := range tt.mustNotContain { |
| 76 | + assert.NotContains(t, joined, notWant, "expected %q to be absent", notWant) |
| 77 | + } |
| 78 | + }) |
| 79 | + } |
| 80 | +} |
0 commit comments