Skip to content

Commit 60181aa

Browse files
xibzpowellbe
authored andcommitted
Adding more verbose error messages and testdata folder
1 parent 451f195 commit 60181aa

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
@@ -24,22 +24,37 @@ import (
2424
)
2525

2626
const (
27-
firecrackerBinaryPath = "./firecracker"
27+
firecrackerBinaryPath = "firecracker"
2828
firecrackerBinaryOverrideEnv = "FC_TEST_BIN"
2929

3030
defaultTuntapName = "fc-test-tap0"
3131
tuntapOverrideEnv = "FC_TEST_TAP"
32+
33+
testDataPathEnv = "FC_TEST_DATA_PATH"
3234
)
3335

36+
var testDataPath = "./testdata"
37+
3438
var skipTuntap bool
3539

3640
func init() {
3741
flag.BoolVar(&skipTuntap, "test.skip-tuntap", false, "Disables tests that require a tuntap device")
42+
43+
if val := os.Getenv(testDataPathEnv); val != "" {
44+
testDataPath = val
45+
}
3846
}
3947

4048
// Ensure that we can create a new machine
4149
func TestNewMachine(t *testing.T) {
42-
m := NewMachine(Config{Debug: true})
50+
m, err := NewMachine(Config{
51+
Debug: true,
52+
disableValidation: true,
53+
})
54+
if err != nil {
55+
t.Fatalf("unexpected error: %v", err)
56+
}
57+
4358
if m == nil {
4459
t.Errorf("NewMachine did not create a Machine")
4560
}
@@ -63,8 +78,13 @@ func TestMicroVMExecution(t *testing.T) {
6378
CPUTemplate: cpuTemplate,
6479
MemInMiB: memSz,
6580
Debug: true,
81+
disableValidation: true,
6682
}
67-
m := NewMachine(cfg)
83+
m, err := NewMachine(cfg)
84+
if err != nil {
85+
t.Fatalf("unexpectd error: %v", err)
86+
}
87+
6888
ctx := context.Background()
6989
vmmCtx, vmmCancel := context.WithTimeout(ctx, 30*time.Second)
7090
defer vmmCancel()
@@ -97,8 +117,13 @@ func TestStartVMM(t *testing.T) {
97117
cfg := Config{
98118
SocketPath: filepath.Join("fc-start-vmm-test.sock"),
99119
BinPath: getFirecrackerBinaryPath(),
120+
disableValidation: true,
100121
}
101-
m := NewMachine(cfg)
122+
m, err := NewMachine(cfg)
123+
if err != nil {
124+
t.Fatalf("unexpected error: %v", err)
125+
}
126+
102127
timeout, cancel := context.WithTimeout(context.Background(), 250*time.Millisecond)
103128
defer cancel()
104129
errchan, err := m.startVMM(timeout)
@@ -120,7 +145,7 @@ func getFirecrackerBinaryPath() string {
120145
if val := os.Getenv(firecrackerBinaryOverrideEnv); val != "" {
121146
return val
122147
}
123-
return firecrackerBinaryPath
148+
return filepath.Join(testDataPath, firecrackerBinaryPath)
124149
}
125150

126151
func testCreateMachine(ctx context.Context, t *testing.T, m *Machine) {
@@ -144,7 +169,7 @@ func testMachineConfigApplication(ctx context.Context, t *testing.T, m *Machine,
144169
}
145170

146171
func testCreateBootSource(ctx context.Context, t *testing.T, m *Machine) {
147-
err := m.createBootSource(ctx, "./vmlinux", "ro console=ttyS0 noapic reboot=k panic=1 pci=off nomodules")
172+
err := m.createBootSource(ctx, filepath.Join(testDataPath, "./vmlinux"), "ro console=ttyS0 noapic reboot=k panic=1 pci=off nomodules")
148173
if err != nil {
149174
t.Errorf("failed to create boot source: %s", err)
150175
}
@@ -175,15 +200,15 @@ func getTapName() string {
175200
}
176201

177202
func testAttachRootDrive(ctx context.Context, t *testing.T, m *Machine) {
178-
drive := BlockDevice{HostPath: "root-drive.img", Mode: "ro"}
203+
drive := BlockDevice{HostPath: filepath.Join(testDataPath, "root-drive.img"), Mode: "ro"}
179204
err := m.attachRootDrive(ctx, drive)
180205
if err != nil {
181206
t.Errorf("attaching root drive failed: %s", err)
182207
}
183208
}
184209

185210
func testAttachSecondaryDrive(ctx context.Context, t *testing.T, m *Machine) {
186-
drive := BlockDevice{HostPath: "drive-2.img", Mode: "ro"}
211+
drive := BlockDevice{HostPath: filepath.Join(testDataPath, "drive-2.img"), Mode: "ro"}
187212
err := m.attachDrive(ctx, drive, 2, false)
188213
if err != nil {
189214
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)