mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-09-19 15:58:25 +00:00
Merge pull request #1334 from jongwu/factory
Factory: Fix fake return value issue on creating template
This commit is contained in:
@@ -185,6 +185,7 @@ var initFactoryCommand = cli.Command{
|
|||||||
HypervisorConfig: runtimeConfig.HypervisorConfig,
|
HypervisorConfig: runtimeConfig.HypervisorConfig,
|
||||||
AgentType: runtimeConfig.AgentType,
|
AgentType: runtimeConfig.AgentType,
|
||||||
AgentConfig: runtimeConfig.AgentConfig,
|
AgentConfig: runtimeConfig.AgentConfig,
|
||||||
|
ProxyType: runtimeConfig.ProxyType,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
kataLog.WithField("factory", factoryConfig).Info("create vm factory")
|
kataLog.WithField("factory", factoryConfig).Info("create vm factory")
|
||||||
|
@@ -17,6 +17,8 @@ import (
|
|||||||
vc "github.com/kata-containers/runtime/virtcontainers"
|
vc "github.com/kata-containers/runtime/virtcontainers"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const testDisabledAsNonRoot = "Test disabled as requires root privileges"
|
||||||
|
|
||||||
func TestFactoryCLIFunctionNoRuntimeConfig(t *testing.T) {
|
func TestFactoryCLIFunctionNoRuntimeConfig(t *testing.T) {
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
|
|
||||||
@@ -63,9 +65,14 @@ func TestFactoryCLIFunctionInit(t *testing.T) {
|
|||||||
assert.Nil(err)
|
assert.Nil(err)
|
||||||
|
|
||||||
// With template
|
// With template
|
||||||
|
if os.Geteuid() != 0 {
|
||||||
|
t.Skip(testDisabledAsNonRoot)
|
||||||
|
}
|
||||||
|
|
||||||
runtimeConfig.FactoryConfig.Template = true
|
runtimeConfig.FactoryConfig.Template = true
|
||||||
runtimeConfig.HypervisorType = vc.MockHypervisor
|
runtimeConfig.HypervisorType = vc.MockHypervisor
|
||||||
runtimeConfig.AgentType = vc.NoopAgentType
|
runtimeConfig.AgentType = vc.NoopAgentType
|
||||||
|
runtimeConfig.ProxyType = vc.NoopProxyType
|
||||||
ctx.App.Metadata["runtimeConfig"] = runtimeConfig
|
ctx.App.Metadata["runtimeConfig"] = runtimeConfig
|
||||||
fn, ok = initFactoryCommand.Action.(func(context *cli.Context) error)
|
fn, ok = initFactoryCommand.Action.(func(context *cli.Context) error)
|
||||||
assert.True(ok)
|
assert.True(ok)
|
||||||
|
@@ -67,7 +67,10 @@ func NewFactory(ctx context.Context, config Config, fetchOnly bool) (vc.Factory,
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
} else {
|
} 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 {
|
} else if config.VMCache && config.Cache == 0 {
|
||||||
b, err = grpccache.New(ctx, config.VMCacheEndpoint)
|
b, err = grpccache.New(ctx, config.VMCacheEndpoint)
|
||||||
|
@@ -8,6 +8,7 @@ package factory
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
vc "github.com/kata-containers/runtime/virtcontainers"
|
vc "github.com/kata-containers/runtime/virtcontainers"
|
||||||
@@ -17,6 +18,8 @@ import (
|
|||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const testDisabledAsNonRoot = "Test disabled as requires root privileges"
|
||||||
|
|
||||||
func TestNewFactory(t *testing.T) {
|
func TestNewFactory(t *testing.T) {
|
||||||
var config Config
|
var config Config
|
||||||
|
|
||||||
@@ -53,6 +56,10 @@ func TestNewFactory(t *testing.T) {
|
|||||||
f.CloseFactory(ctx)
|
f.CloseFactory(ctx)
|
||||||
|
|
||||||
// template
|
// template
|
||||||
|
if os.Geteuid() != 0 {
|
||||||
|
t.Skip(testDisabledAsNonRoot)
|
||||||
|
}
|
||||||
|
|
||||||
config.Template = true
|
config.Template = true
|
||||||
f, err = NewFactory(ctx, config, false)
|
f, err = NewFactory(ctx, config, false)
|
||||||
assert.Nil(err)
|
assert.Nil(err)
|
||||||
@@ -187,6 +194,10 @@ func TestFactoryGetVM(t *testing.T) {
|
|||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
// direct factory
|
// direct factory
|
||||||
|
if os.Geteuid() != 0 {
|
||||||
|
t.Skip(testDisabledAsNonRoot)
|
||||||
|
}
|
||||||
|
|
||||||
f, err := NewFactory(ctx, Config{VMConfig: vmConfig}, false)
|
f, err := NewFactory(ctx, Config{VMConfig: vmConfig}, false)
|
||||||
assert.Nil(err)
|
assert.Nil(err)
|
||||||
|
|
||||||
|
@@ -15,7 +15,6 @@ import (
|
|||||||
|
|
||||||
vc "github.com/kata-containers/runtime/virtcontainers"
|
vc "github.com/kata-containers/runtime/virtcontainers"
|
||||||
"github.com/kata-containers/runtime/virtcontainers/factory/base"
|
"github.com/kata-containers/runtime/virtcontainers/factory/base"
|
||||||
"github.com/kata-containers/runtime/virtcontainers/factory/direct"
|
|
||||||
"github.com/kata-containers/runtime/virtcontainers/store"
|
"github.com/kata-containers/runtime/virtcontainers/store"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -42,14 +41,13 @@ func Fetch(config vc.VMConfig) (base.FactoryBase, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// New creates a new VM template factory.
|
// 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"
|
statePath := store.RunVMStoragePath + "/template"
|
||||||
t := &template{statePath, config}
|
t := &template{statePath, config}
|
||||||
|
|
||||||
err := t.prepareTemplateFiles()
|
err := t.prepareTemplateFiles()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// fallback to direct factory if template is not supported.
|
return nil, err
|
||||||
return direct.New(ctx, config)
|
|
||||||
}
|
}
|
||||||
defer func() {
|
defer func() {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -59,11 +57,10 @@ func New(ctx context.Context, config vc.VMConfig) base.FactoryBase {
|
|||||||
|
|
||||||
err = t.createTemplateVM(ctx)
|
err = t.createTemplateVM(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// fallback to direct factory if template is not supported.
|
return nil, err
|
||||||
return direct.New(ctx, config)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return t
|
return t, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Config returns template factory's configuration.
|
// Config returns template factory's configuration.
|
||||||
@@ -116,7 +113,9 @@ func (t *template) createTemplateVM(ctx context.Context) error {
|
|||||||
config.HypervisorConfig.MemoryPath = t.statePath + "/memory"
|
config.HypervisorConfig.MemoryPath = t.statePath + "/memory"
|
||||||
config.HypervisorConfig.DevicesStatePath = t.statePath + "/state"
|
config.HypervisorConfig.DevicesStatePath = t.statePath + "/state"
|
||||||
// template vm uses builtin proxy
|
// template vm uses builtin proxy
|
||||||
config.ProxyType = templateProxyType
|
if config.ProxyType != "noopProxy" {
|
||||||
|
config.ProxyType = templateProxyType
|
||||||
|
}
|
||||||
|
|
||||||
vm, err := vc.NewVM(ctx, config)
|
vm, err := vc.NewVM(ctx, config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@@ -17,7 +17,13 @@ import (
|
|||||||
vc "github.com/kata-containers/runtime/virtcontainers"
|
vc "github.com/kata-containers/runtime/virtcontainers"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const testDisabledAsNonRoot = "Test disabled as requires root privileges"
|
||||||
|
|
||||||
func TestTemplateFactory(t *testing.T) {
|
func TestTemplateFactory(t *testing.T) {
|
||||||
|
if os.Geteuid() != 0 {
|
||||||
|
t.Skip(testDisabledAsNonRoot)
|
||||||
|
}
|
||||||
|
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
|
|
||||||
templateWaitForAgent = 1 * time.Microsecond
|
templateWaitForAgent = 1 * time.Microsecond
|
||||||
@@ -40,7 +46,8 @@ func TestTemplateFactory(t *testing.T) {
|
|||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
// New
|
// New
|
||||||
f := New(ctx, vmConfig)
|
f, err := New(ctx, vmConfig)
|
||||||
|
assert.Nil(err)
|
||||||
|
|
||||||
// Config
|
// Config
|
||||||
assert.Equal(f.Config(), vmConfig)
|
assert.Equal(f.Config(), vmConfig)
|
||||||
@@ -74,7 +81,7 @@ func TestTemplateFactory(t *testing.T) {
|
|||||||
assert.Nil(err)
|
assert.Nil(err)
|
||||||
|
|
||||||
err = tt.createTemplateVM(ctx)
|
err = tt.createTemplateVM(ctx)
|
||||||
assert.Error(err)
|
assert.Nil(err)
|
||||||
|
|
||||||
templateProxyType = vc.NoopProxyType
|
templateProxyType = vc.NoopProxyType
|
||||||
vm, err = tt.GetBaseVM(ctx, vmConfig)
|
vm, err = tt.GetBaseVM(ctx, vmConfig)
|
||||||
|
Reference in New Issue
Block a user