Skip to content

Commit

Permalink
New upstream version 0.0.7
Browse files Browse the repository at this point in the history
  • Loading branch information
obbardc committed Nov 12, 2023
1 parent 5d60c8f commit 05b59cd
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 20 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
linters:
enable:
- errorlint
- gofmt
- stylecheck
- whitespace
10 changes: 9 additions & 1 deletion backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,15 @@ func newBackend(name string, m *Machine) (backend, error) {

b, backendErr := newBackend(backendName, m)
if backendErr != nil {
err = fmt.Errorf("%v, %v", err, backendErr)
/* Append the error to any existing backend creation error(s).
* Since we cannot join errors together in golang <1.20, instead
* join the error messages strings and return that as a new error.
*/
if err != nil {
err = fmt.Errorf("%v, %v", err.Error(), backendErr.Error())
} else {
err = backendErr
}
continue
}
return b, nil
Expand Down
1 change: 1 addition & 0 deletions backend_qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ func (b qemuBackend) StartQemu(kvm bool) (bool, error) {
qemuargs = append(qemuargs, "-machine", qemuMachine.machine)
console := fmt.Sprintf("console=%s", qemuMachine.console)
kernelargs := []string{console, "panic=-1",
"plymouth.enable=0",
"systemd.unit=fakemachine.service"}

if m.showBoot {
Expand Down
2 changes: 1 addition & 1 deletion backend_uml.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func (b umlBackend) Start() (bool, error) {
"mem=" + memory + "M",
"initrd=" + m.initrdpath,
"panic=-1",
"nosplash",
"plymouth.enable=0",
"systemd.unit=fakemachine.service",
"console=tty0",
}
Expand Down
8 changes: 5 additions & 3 deletions cmd/fakemachine/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package main

import (
"errors"
"fmt"
"github.com/alessio/shellescape"
"github.com/docker/go-units"
"github.com/go-debos/fakemachine"
"github.com/jessevdk/go-flags"
Expand Down Expand Up @@ -141,8 +143,8 @@ func main() {

args, err := parser.Parse()
if err != nil {
flagsErr, ok := err.(*flags.Error)
if ok && flagsErr.Type == flags.ErrHelp {
var flagsErr *flags.Error
if errors.As(err, &flagsErr) && flagsErr.Type == flags.ErrHelp {
os.Exit(0)
} else {
os.Exit(1)
Expand Down Expand Up @@ -180,7 +182,7 @@ func main() {

command := "/bin/bash"
if len(args) > 0 {
command = strings.Join(args, " ")
command = shellescape.QuoteCommand(args)
}

ret, err := m.Run(command)
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/go-debos/fakemachine
go 1.15

require (
github.com/alessio/shellescape v1.4.2
github.com/docker/go-units v0.5.0
github.com/jessevdk/go-flags v1.5.0
github.com/klauspost/compress v1.15.3
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/alessio/shellescape v1.4.2 h1:MHPfaU+ddJ0/bYWpgIeUnQUqKrlJ1S7BfEYPM4uEoM0=
github.com/alessio/shellescape v1.4.2/go.mod h1:PZAiSCk0LJaZkiCSkPv8qIobYglO3FPpyFjDCtHLS30=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
12 changes: 4 additions & 8 deletions machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"bytes"
"errors"
"fmt"
"github.com/alessio/shellescape"
"io/ioutil"
"os"
"os/exec"
Expand Down Expand Up @@ -894,8 +895,8 @@ func (m *Machine) Run(command string) (int, error) {
func (m *Machine) RunInMachineWithArgs(args []string) (int, error) {
name := path.Join("/", path.Base(os.Args[0]))

// FIXME: shell escaping?
command := strings.Join(append([]string{name}, args...), " ")
quotedArgs := shellescape.QuoteCommand(args)
command := strings.Join([]string{name, quotedArgs}, " ")

executable, err := exec.LookPath(os.Args[0])

Expand All @@ -909,10 +910,5 @@ func (m *Machine) RunInMachineWithArgs(args []string) (int, error) {
// RunInMachine runs the caller binary inside the fakemachine with the same
// commandline arguments as the parent
func (m *Machine) RunInMachine() (int, error) {
name := path.Join("/", path.Base(os.Args[0]))

// FIXME: shell escaping?
command := strings.Join(append([]string{name}, os.Args[1:]...), " ")

return m.startup(command, [][2]string{{os.Args[0], name}})
return m.RunInMachineWithArgs(os.Args[1:])
}
35 changes: 28 additions & 7 deletions machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ import (
)

var backendName string
var testArg string

func init() {
flag.StringVar(&backendName, "backend", "auto", "Fakemachine backend to use")
flag.StringVar(&testArg, "testarg", "", "Test specific argument")
}

func CreateMachine(t *testing.T) *Machine {
Expand Down Expand Up @@ -90,7 +92,7 @@ func TestScratchTmp(t *testing.T) {

m := CreateMachine(t)

exitcode, _ := m.RunInMachineWithArgs([]string{"-test.run TestScratchTmp"})
exitcode, _ := m.RunInMachineWithArgs([]string{"-test.run", "TestScratchTmp"})

if exitcode != 0 {
t.Fatalf("Test for tmpfs mount on scratch failed with %d", exitcode)
Expand All @@ -107,7 +109,7 @@ func TestScratchDisk(t *testing.T) {
m := CreateMachine(t)
m.SetScratch(1024*1024*1024, "")

exitcode, _ := m.RunInMachineWithArgs([]string{"-test.run TestScratchDisk"})
exitcode, _ := m.RunInMachineWithArgs([]string{"-test.run", "TestScratchDisk"})

if exitcode != 0 {
t.Fatalf("Test for device mount on scratch failed with %d", exitcode)
Expand Down Expand Up @@ -145,7 +147,7 @@ func TestSpawnMachine(t *testing.T) {

m := CreateMachine(t)

exitcode, _ := m.RunInMachineWithArgs([]string{"-test.run TestSpawnMachine"})
exitcode, _ := m.RunInMachineWithArgs([]string{"-test.run", "TestSpawnMachine"})

if exitcode != 0 {
t.Fatalf("Test for respawning in the machine failed failed with %d", exitcode)
Expand Down Expand Up @@ -180,7 +182,7 @@ func TestImageLabel(t *testing.T) {
labeled, err := m.CreateImageWithLabel("test-labeled.img", 1024*1024, "test-labeled")
require.Nil(t, err)

exitcode, _ := m.RunInMachineWithArgs([]string{"-test.run TestImageLabel", autolabel, labeled})
exitcode, _ := m.RunInMachineWithArgs([]string{"-test.run", "TestImageLabel", autolabel, labeled})
if exitcode != 0 {
t.Fatalf("Test for images in the machine failed failed with %d", exitcode)
}
Expand All @@ -197,23 +199,42 @@ func TestVolumes(t *testing.T) {
m := CreateMachine(t)
m.AddVolume("random_directory_never_exists")

exitcode, err := m.RunInMachineWithArgs([]string{"-test.run TestVolumes"})
exitcode, err := m.RunInMachineWithArgs([]string{"-test.run", "TestVolumes"})
require.Equal(t, exitcode, -1)
require.Error(t, err)

/* Try to mount a device file into the machine */
m = CreateMachine(t)
m.AddVolume("/dev/zero")

exitcode, err = m.RunInMachineWithArgs([]string{"-test.run TestVolumes"})
exitcode, err = m.RunInMachineWithArgs([]string{"-test.run", "TestVolumes"})
require.Equal(t, exitcode, -1)
require.Error(t, err)

/* Try to mount a volume with whitespace into the machine */
m = CreateMachine(t)
m.AddVolumeAt("/dev", "/dev ices")

exitcode, err = m.RunInMachineWithArgs([]string{"-test.run TestVolumes"})
exitcode, err = m.RunInMachineWithArgs([]string{"-test.run", "TestVolumes"})
require.Equal(t, exitcode, -1)
require.Error(t, err)
}

func TestCommandEscaping(t *testing.T) {
t.Parallel()
if InMachine() {
t.Log("Running in the machine")
require.Equal(t, testArg, "$s'n\\akes")
t.Log(testArg)
return
}

m := CreateMachine(t)
exitcode, _ := m.RunInMachineWithArgs([]string{
"-test.v", "-test.run",
"TestCommandEscaping", "-testarg", "$s'n\\akes"})

if exitcode != 0 {
t.Fatalf("Expected 0 but got %d", exitcode)
}
}

0 comments on commit 05b59cd

Please sign in to comment.