Skip to content
This repository was archived by the owner on May 12, 2021. It is now read-only.
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
16 changes: 11 additions & 5 deletions virtcontainers/factory/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func TestNewFactory(t *testing.T) {
config.VMConfig = vc.VMConfig{
HypervisorType: vc.MockHypervisor,
AgentType: vc.NoopAgentType,
ProxyType: vc.NoopProxyType,
}

_, err = NewFactory(ctx, config, false)
Expand All @@ -43,28 +44,33 @@ func TestNewFactory(t *testing.T) {
}

// direct
_, err = NewFactory(ctx, config, false)
f, err := NewFactory(ctx, config, false)
assert.Nil(err)
_, err = NewFactory(ctx, config, true)
f.CloseFactory(ctx)
f, err = NewFactory(ctx, config, true)
assert.Nil(err)
f.CloseFactory(ctx)

// template
config.Template = true
_, err = NewFactory(ctx, config, false)
f, err = NewFactory(ctx, config, false)
assert.Nil(err)
f.CloseFactory(ctx)
_, err = NewFactory(ctx, config, true)
assert.Error(err)

// Cache
config.Cache = 10
_, err = NewFactory(ctx, config, false)
f, err = NewFactory(ctx, config, false)
assert.Nil(err)
f.CloseFactory(ctx)
_, err = NewFactory(ctx, config, true)
assert.Error(err)

config.Template = false
_, err = NewFactory(ctx, config, false)
f, err = NewFactory(ctx, config, false)
assert.Nil(err)
f.CloseFactory(ctx)
_, err = NewFactory(ctx, config, true)
assert.Error(err)
}
Expand Down
11 changes: 11 additions & 0 deletions virtcontainers/factory/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ func New(ctx context.Context, config vc.VMConfig) base.FactoryBase {
// fallback to direct factory if template is not supported.
return direct.New(ctx, config)
}
defer func() {
if err != nil {
t.close()
}
}()

err = t.createTemplateVM(ctx)
if err != nil {
Expand All @@ -73,6 +78,10 @@ func (t *template) GetBaseVM(ctx context.Context, config vc.VMConfig) (*vc.VM, e

// CloseFactory cleans up the template VM.
func (t *template) CloseFactory(ctx context.Context) {
t.close()
}

func (t *template) close() {
syscall.Unmount(t.statePath, 0)
os.RemoveAll(t.statePath)
}
Expand All @@ -86,10 +95,12 @@ func (t *template) prepareTemplateFiles() error {
flags := uintptr(syscall.MS_NOSUID | syscall.MS_NODEV)
opts := fmt.Sprintf("size=%dM", t.config.HypervisorConfig.MemorySize+8)
if err = syscall.Mount("tmpfs", t.statePath, "tmpfs", flags, opts); err != nil {
t.close()
return err
}
f, err := os.Create(t.statePath + "/memory")
if err != nil {
t.close()
return err
}
f.Close()
Expand Down