Skip to content

Commit 91e88e3

Browse files
authored
Merge pull request #16 from xibz/master
cmd/firectl: adding more verbose error messages and testdata folder
2 parents ab150cf + 60181aa commit 91e88e3

File tree

6 files changed

+68
-42
lines changed

6 files changed

+68
-42
lines changed

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
*.img
21
firecracker
32
firecracker-*
4-
vmlinux
3+
vmlinux

cmd/firectl/main.go

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -26,32 +26,6 @@ import (
2626
log "github.com/sirupsen/logrus"
2727
)
2828

29-
func checkConfig(cfg firecracker.Config) error {
30-
var err error
31-
32-
// Check for the existence of some required files:
33-
_, err = os.Stat(cfg.BinPath)
34-
if err != nil {
35-
return err
36-
}
37-
_, err = os.Stat(cfg.KernelImagePath)
38-
if err != nil {
39-
return err
40-
}
41-
_, err = os.Stat(cfg.RootDrive.HostPath)
42-
if err != nil {
43-
return err
44-
}
45-
46-
// Check the non-existence of some files:
47-
_, err = os.Stat(cfg.SocketPath)
48-
if err == nil {
49-
msg := fmt.Sprintf("Socket %s already exists.", cfg.SocketPath)
50-
return errors.New(msg)
51-
}
52-
return nil
53-
}
54-
5529
func parseBlockDevices(entries []string) ([]firecracker.BlockDevice, error) {
5630
var devices []firecracker.BlockDevice
5731
for _, entry := range entries {
@@ -207,13 +181,11 @@ func main() {
207181
Debug: opts.Debug,
208182
}
209183

210-
err = checkConfig(fcCfg)
184+
m, err := firecracker.NewMachine(fcCfg, firecracker.WithLogger(log.NewEntry(logger)))
211185
if err != nil {
212-
log.Fatalf("Configuration error: %s", err)
186+
log.Fatalf("Failed creating machine: %s", err)
213187
}
214188

215-
m := firecracker.NewMachine(fcCfg, firecracker.WithLogger(log.NewEntry(logger)))
216-
217189
ctx := context.Background()
218190
vmmCtx, vmmCancel := context.WithCancel(ctx)
219191
defer vmmCancel()

machine.go

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,31 @@ type Config struct {
8282
Console string
8383
Debug bool
8484
machineCfg models.MachineConfiguration
85+
86+
// Allows for easier mock testing
87+
disableValidation bool
88+
}
89+
90+
// Validate will ensure that the required fields are set and that
91+
// the fields are valid values.
92+
func (cfg *Config) Validate() error {
93+
if cfg.disableValidation {
94+
return nil
95+
}
96+
97+
if _, err := os.Stat(cfg.KernelImagePath); err != nil {
98+
return fmt.Errorf("Failed to stat kernal image path, %q: %v", cfg.KernelImagePath, err)
99+
}
100+
if _, err := os.Stat(cfg.RootDrive.HostPath); err != nil {
101+
return fmt.Errorf("Failed to stat host path, %q: %v", cfg.RootDrive.HostPath, err)
102+
}
103+
104+
// Check the non-existence of some files:
105+
if _, err := os.Stat(cfg.SocketPath); err == nil {
106+
return fmt.Errorf("Socket %s already exists", cfg.SocketPath)
107+
}
108+
109+
return nil
85110
}
86111

87112
// Machine is the main object for manipulating firecracker VMs
@@ -136,7 +161,11 @@ func (m Machine) LogLevel() string {
136161
}
137162

138163
// NewMachine initializes a new Machine instance
139-
func NewMachine(cfg Config, opts ...Opt) *Machine {
164+
func NewMachine(cfg Config, opts ...Opt) (*Machine, error) {
165+
if err := cfg.Validate(); err != nil {
166+
return nil, err
167+
}
168+
140169
m := &Machine{}
141170

142171
for _, opt := range opts {
@@ -173,7 +202,7 @@ func NewMachine(cfg Config, opts ...Opt) *Machine {
173202
m.binPath = fcExecutable
174203
}
175204

176-
return m
205+
return m, nil
177206
}
178207

179208
// Init starts the VMM and attaches drives and network interfaces.

machine_test.go

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,37 @@ import (
2828
)
2929

3030
const (
31-
firecrackerBinaryPath = "./firecracker"
31+
firecrackerBinaryPath = "firecracker"
3232
firecrackerBinaryOverrideEnv = "FC_TEST_BIN"
3333

3434
defaultTuntapName = "fc-test-tap0"
3535
tuntapOverrideEnv = "FC_TEST_TAP"
36+
37+
testDataPathEnv = "FC_TEST_DATA_PATH"
3638
)
3739

40+
var testDataPath = "./testdata"
41+
3842
var skipTuntap bool
3943

4044
func init() {
4145
flag.BoolVar(&skipTuntap, "test.skip-tuntap", false, "Disables tests that require a tuntap device")
46+
47+
if val := os.Getenv(testDataPathEnv); val != "" {
48+
testDataPath = val
49+
}
4250
}
4351

4452
// Ensure that we can create a new machine
4553
func TestNewMachine(t *testing.T) {
46-
m := NewMachine(Config{Debug: true})
54+
m, err := NewMachine(Config{
55+
Debug: true,
56+
disableValidation: true,
57+
})
58+
if err != nil {
59+
t.Fatalf("unexpected error: %v", err)
60+
}
61+
4762
if m == nil {
4863
t.Errorf("NewMachine did not create a Machine")
4964
}
@@ -67,8 +82,13 @@ func TestMicroVMExecution(t *testing.T) {
6782
CPUTemplate: cpuTemplate,
6883
MemInMiB: memSz,
6984
Debug: true,
85+
disableValidation: true,
7086
}
71-
m := NewMachine(cfg)
87+
m, err := NewMachine(cfg)
88+
if err != nil {
89+
t.Fatalf("unexpectd error: %v", err)
90+
}
91+
7292
ctx := context.Background()
7393
vmmCtx, vmmCancel := context.WithTimeout(ctx, 30*time.Second)
7494
defer vmmCancel()
@@ -101,8 +121,13 @@ func TestStartVMM(t *testing.T) {
101121
cfg := Config{
102122
SocketPath: filepath.Join("fc-start-vmm-test.sock"),
103123
BinPath: getFirecrackerBinaryPath(),
124+
disableValidation: true,
104125
}
105-
m := NewMachine(cfg)
126+
m, err := NewMachine(cfg)
127+
if err != nil {
128+
t.Fatalf("unexpected error: %v", err)
129+
}
130+
106131
timeout, cancel := context.WithTimeout(context.Background(), 250*time.Millisecond)
107132
defer cancel()
108133
errchan, err := m.startVMM(timeout)
@@ -124,7 +149,7 @@ func getFirecrackerBinaryPath() string {
124149
if val := os.Getenv(firecrackerBinaryOverrideEnv); val != "" {
125150
return val
126151
}
127-
return firecrackerBinaryPath
152+
return filepath.Join(testDataPath, firecrackerBinaryPath)
128153
}
129154

130155
func testCreateMachine(ctx context.Context, t *testing.T, m *Machine) {
@@ -148,7 +173,7 @@ func testMachineConfigApplication(ctx context.Context, t *testing.T, m *Machine,
148173
}
149174

150175
func testCreateBootSource(ctx context.Context, t *testing.T, m *Machine) {
151-
err := m.createBootSource(ctx, "./vmlinux", "ro console=ttyS0 noapic reboot=k panic=1 pci=off nomodules")
176+
err := m.createBootSource(ctx, filepath.Join(testDataPath, "./vmlinux"), "ro console=ttyS0 noapic reboot=k panic=1 pci=off nomodules")
152177
if err != nil {
153178
t.Errorf("failed to create boot source: %s", err)
154179
}
@@ -179,15 +204,15 @@ func getTapName() string {
179204
}
180205

181206
func testAttachRootDrive(ctx context.Context, t *testing.T, m *Machine) {
182-
drive := BlockDevice{HostPath: "root-drive.img", Mode: "ro"}
207+
drive := BlockDevice{HostPath: filepath.Join(testDataPath, "root-drive.img"), Mode: "ro"}
183208
err := m.attachRootDrive(ctx, drive)
184209
if err != nil {
185210
t.Errorf("attaching root drive failed: %s", err)
186211
}
187212
}
188213

189214
func testAttachSecondaryDrive(ctx context.Context, t *testing.T, m *Machine) {
190-
drive := BlockDevice{HostPath: "drive-2.img", Mode: "ro"}
215+
drive := BlockDevice{HostPath: filepath.Join(testDataPath, "drive-2.img"), Mode: "ro"}
191216
err := m.attachDrive(ctx, drive, 2, false)
192217
if err != nil {
193218
t.Errorf("attaching secondary drive failed: %s", err)

testdata/drive-2.img

Whitespace-only changes.

testdata/root-drive.img

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

0 commit comments

Comments
 (0)