Skip to content

Commit

Permalink
Factory: Fix fake return value issue on creating template
Browse files Browse the repository at this point in the history
Now, function NewFactory will return nil even create template
does't complete. As for this, it will tell user that factory
has been initialized no matter whether the template is created
or not. This patch correct it by adding another return value
of error in NewFactory.

Testing initFactoryCommand when enable template will need root
privilege to mount tmpfs. So skip it for no-root user.

Testing initFactoryCommand func will create template, but no
proxy type assigned to VMconfig which will using katabuiltinProxy
instead. this will lead to failure for this type of proxy will
check proxyparams which contains many null value. This commit
fix it by substitute katabuiltinProxy as noopProxy when for test
purpose.

Fixes: kata-containers#1333
Signed-off-by: Jianyong Wu <[email protected]>
  • Loading branch information
jongwu authored and Eric Ernst committed Apr 3, 2019
1 parent 3c3d0fb commit 235c37e
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 11 deletions.
1 change: 1 addition & 0 deletions cli/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ var initFactoryCommand = cli.Command{
HypervisorConfig: runtimeConfig.HypervisorConfig,
AgentType: runtimeConfig.AgentType,
AgentConfig: runtimeConfig.AgentConfig,
ProxyType: runtimeConfig.ProxyType,
},
}
kataLog.WithField("factory", factoryConfig).Info("create vm factory")
Expand Down
7 changes: 7 additions & 0 deletions cli/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
vc "github.com/kata-containers/runtime/virtcontainers"
)

const testDisabledAsNonRoot = "Test disabled as requires root privileges"

func TestFactoryCLIFunctionNoRuntimeConfig(t *testing.T) {
assert := assert.New(t)

Expand Down Expand Up @@ -63,9 +65,14 @@ func TestFactoryCLIFunctionInit(t *testing.T) {
assert.Nil(err)

// With template
if os.Geteuid() != 0 {
t.Skip(testDisabledAsNonRoot)
}

runtimeConfig.FactoryConfig.Template = true
runtimeConfig.HypervisorType = vc.MockHypervisor
runtimeConfig.AgentType = vc.NoopAgentType
runtimeConfig.ProxyType = vc.NoopProxyType
ctx.App.Metadata["runtimeConfig"] = runtimeConfig
fn, ok = initFactoryCommand.Action.(func(context *cli.Context) error)
assert.True(ok)
Expand Down
5 changes: 4 additions & 1 deletion virtcontainers/factory/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ func NewFactory(ctx context.Context, config Config, fetchOnly bool) (vc.Factory,
return nil, err
}
} else {
b = template.New(ctx, config.VMConfig)
b, err = template.New(ctx, config.VMConfig)
if err != nil {
return nil, err
}
}
} else if config.VMCache && config.Cache == 0 {
b, err = grpccache.New(ctx, config.VMCacheEndpoint)
Expand Down
11 changes: 11 additions & 0 deletions virtcontainers/factory/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package factory
import (
"context"
"io/ioutil"
"os"
"testing"

vc "github.com/kata-containers/runtime/virtcontainers"
Expand All @@ -17,6 +18,8 @@ import (
"github.com/stretchr/testify/assert"
)

const testDisabledAsNonRoot = "Test disabled as requires root privileges"

func TestNewFactory(t *testing.T) {
var config Config

Expand Down Expand Up @@ -53,6 +56,10 @@ func TestNewFactory(t *testing.T) {
f.CloseFactory(ctx)

// template
if os.Geteuid() != 0 {
t.Skip(testDisabledAsNonRoot)
}

config.Template = true
f, err = NewFactory(ctx, config, false)
assert.Nil(err)
Expand Down Expand Up @@ -187,6 +194,10 @@ func TestFactoryGetVM(t *testing.T) {
ctx := context.Background()

// direct factory
if os.Geteuid() != 0 {
t.Skip(testDisabledAsNonRoot)
}

f, err := NewFactory(ctx, Config{VMConfig: vmConfig}, false)
assert.Nil(err)

Expand Down
15 changes: 7 additions & 8 deletions virtcontainers/factory/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (

vc "github.com/kata-containers/runtime/virtcontainers"
"github.com/kata-containers/runtime/virtcontainers/factory/base"
"github.com/kata-containers/runtime/virtcontainers/factory/direct"
"github.com/kata-containers/runtime/virtcontainers/store"
)

Expand All @@ -42,14 +41,13 @@ func Fetch(config vc.VMConfig) (base.FactoryBase, error) {
}

// New creates a new VM template factory.
func New(ctx context.Context, config vc.VMConfig) base.FactoryBase {
func New(ctx context.Context, config vc.VMConfig) (base.FactoryBase, error) {
statePath := store.RunVMStoragePath + "/template"
t := &template{statePath, config}

err := t.prepareTemplateFiles()
if err != nil {
// fallback to direct factory if template is not supported.
return direct.New(ctx, config)
return nil, err
}
defer func() {
if err != nil {
Expand All @@ -59,11 +57,10 @@ func New(ctx context.Context, config vc.VMConfig) base.FactoryBase {

err = t.createTemplateVM(ctx)
if err != nil {
// fallback to direct factory if template is not supported.
return direct.New(ctx, config)
return nil, err
}

return t
return t, nil
}

// Config returns template factory's configuration.
Expand Down Expand Up @@ -116,7 +113,9 @@ func (t *template) createTemplateVM(ctx context.Context) error {
config.HypervisorConfig.MemoryPath = t.statePath + "/memory"
config.HypervisorConfig.DevicesStatePath = t.statePath + "/state"
// template vm uses builtin proxy
config.ProxyType = templateProxyType
if config.ProxyType != "noopProxy" {
config.ProxyType = templateProxyType
}

vm, err := vc.NewVM(ctx, config)
if err != nil {
Expand Down
11 changes: 9 additions & 2 deletions virtcontainers/factory/template/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ import (
vc "github.com/kata-containers/runtime/virtcontainers"
)

const testDisabledAsNonRoot = "Test disabled as requires root privileges"

func TestTemplateFactory(t *testing.T) {
if os.Geteuid() != 0 {
t.Skip(testDisabledAsNonRoot)
}

assert := assert.New(t)

templateWaitForAgent = 1 * time.Microsecond
Expand All @@ -40,7 +46,8 @@ func TestTemplateFactory(t *testing.T) {
ctx := context.Background()

// New
f := New(ctx, vmConfig)
f, err := New(ctx, vmConfig)
assert.Nil(err)

// Config
assert.Equal(f.Config(), vmConfig)
Expand Down Expand Up @@ -74,7 +81,7 @@ func TestTemplateFactory(t *testing.T) {
assert.Nil(err)

err = tt.createTemplateVM(ctx)
assert.Error(err)
assert.Nil(err)

templateProxyType = vc.NoopProxyType
vm, err = tt.GetBaseVM(ctx, vmConfig)
Expand Down

0 comments on commit 235c37e

Please sign in to comment.