Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions jailer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ func TestJail(t *testing.T) {
},
}
cfg := &Config{
VMID: "vmid",
JailerCfg: &c.jailerCfg,
NetNS: c.netns,
SocketPath: c.socketPath,
Expand Down
33 changes: 18 additions & 15 deletions machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,9 @@ type Config struct {
JailerCfg *JailerConfig

// (Optional) VMID is a unique identifier for this VM. It's set to a
// random uuid if not provided by the user. It's currently used to
// set the CNI ContainerID and create a network namespace path if
// CNI configuration is provided as part of NetworkInterfaces
// random uuid if not provided by the user. It's used to set Firecracker's instance ID.
// If CNI configuration is provided as part of NetworkInterfaces,
// the VMID is used to set CNI ContainerID and create a network namespace path.
VMID string

// NetNS represents the path to a network namespace handle. If present, the
Expand Down Expand Up @@ -303,13 +303,27 @@ func (m *Machine) LogLevel() string {
return m.Cfg.LogLevel
}

func configureBuilder(builder VMCommandBuilder, cfg Config) VMCommandBuilder {
return builder.
WithSocketPath(cfg.SocketPath).
AddArgs("--seccomp-level", cfg.SeccompLevel.String(), "--id", cfg.VMID)
}

// NewMachine initializes a new Machine instance and performs validation of the
// provided Config.
func NewMachine(ctx context.Context, cfg Config, opts ...Opt) (*Machine, error) {
m := &Machine{
exitCh: make(chan struct{}),
}

if cfg.VMID == "" {
randomID, err := uuid.NewV4()
if err != nil {
return nil, errors.Wrap(err, "failed to create random ID for VMID")
}
cfg.VMID = randomID.String()
}

m.Handlers = defaultHandlers

if cfg.JailerCfg != nil {
Expand All @@ -319,10 +333,7 @@ func NewMachine(ctx context.Context, cfg Config, opts ...Opt) (*Machine, error)
}
} else {
m.Handlers.Validation = m.Handlers.Validation.Append(ConfigValidationHandler)
m.cmd = defaultFirecrackerVMMCommandBuilder.
WithSocketPath(cfg.SocketPath).
AddArgs("--seccomp-level", cfg.SeccompLevel.String()).
Build(ctx)
m.cmd = configureBuilder(defaultFirecrackerVMMCommandBuilder, cfg).Build(ctx)
}

for _, opt := range opts {
Expand All @@ -339,14 +350,6 @@ func NewMachine(ctx context.Context, cfg Config, opts ...Opt) (*Machine, error)
m.client = NewClient(cfg.SocketPath, m.logger, false)
}

if cfg.VMID == "" {
randomID, err := uuid.NewV4()
if err != nil {
return nil, errors.Wrap(err, "failed to create random ID for VMID")
}
cfg.VMID = randomID.String()
}

if cfg.ForwardSignals == nil {
cfg.ForwardSignals = []os.Signal{
os.Interrupt,
Expand Down
15 changes: 10 additions & 5 deletions machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,13 +420,17 @@ func TestStartVMM(t *testing.T) {
}

func TestLogAndMetrics(t *testing.T) {
const logLevel = "DEBUG"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be Debug

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, cool!

const vmID = "UserSuppliedVMID"

dir, err := ioutil.TempDir("", t.Name())
require.NoError(t, err)
defer os.RemoveAll(dir)

socketPath := filepath.Join(dir, "fc.sock")

cfg := Config{
VMID: vmID,
SocketPath: socketPath,
DisableValidation: true,
KernelImagePath: getVmlinuxPath(t),
Expand All @@ -438,13 +442,10 @@ func TestLogAndMetrics(t *testing.T) {
},
MetricsPath: filepath.Join(dir, "fc-metrics.out"),
LogPath: filepath.Join(dir, "fc.log"),
LogLevel: "Debug",
LogLevel: logLevel,
}
ctx := context.Background()
cmd := VMCommandBuilder{}.
WithSocketPath(cfg.SocketPath).
WithBin(getFirecrackerBinaryPath()).
Build(ctx)
cmd := configureBuilder(VMCommandBuilder{}.WithBin(getFirecrackerBinaryPath()), cfg).Build(ctx)
m, err := NewMachine(ctx, cfg, WithProcessRunner(cmd), WithLogger(fctesting.NewLogEntry(t)))
require.NoError(t, err)

Expand Down Expand Up @@ -472,6 +473,10 @@ func TestLogAndMetrics(t *testing.T) {
log, err := os.Stat(cfg.LogPath)
require.NoError(t, err)
assert.NotEqual(t, 0, log.Size())

content, err := ioutil.ReadFile(cfg.LogPath)
require.NoError(t, err)
assert.Contains(t, string(content), "["+vmID+":"+logLevel+"]")
}

func TestStartVMMOnce(t *testing.T) {
Expand Down