mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-10-21 20:08:54 +00:00
Add enable_template option to the config file. When it is set, enable the vm template factory. cache factory cannot be used by kata cli directly because it requires a running daemon to maintain the cache VMs. `kata-runtime factory init` would initialize the vm factory and `kata-runtime factory destroy` would destroy the vm factory. When configured, a vm factory is loaded before creating new sandboxes. Signed-off-by: Peng Tao <bergwolf@gmail.com>
118 lines
2.7 KiB
Go
118 lines
2.7 KiB
Go
// Copyright (c) 2018 HyperHQ Inc.
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
|
|
package main
|
|
|
|
import (
|
|
"flag"
|
|
"io/ioutil"
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/urfave/cli"
|
|
|
|
vc "github.com/kata-containers/runtime/virtcontainers"
|
|
)
|
|
|
|
func TestFactoryCLIFunctionNoRuntimeConfig(t *testing.T) {
|
|
assert := assert.New(t)
|
|
|
|
app := cli.NewApp()
|
|
ctx := cli.NewContext(app, nil, nil)
|
|
app.Name = "foo"
|
|
ctx.App.Metadata = map[string]interface{}{
|
|
"foo": "bar",
|
|
}
|
|
|
|
fn, ok := initFactoryCommand.Action.(func(context *cli.Context) error)
|
|
assert.True(ok)
|
|
err := fn(ctx)
|
|
// no runtime config in the Metadata
|
|
assert.Error(err)
|
|
|
|
fn, ok = destroyFactoryCommand.Action.(func(context *cli.Context) error)
|
|
assert.True(ok)
|
|
err = fn(ctx)
|
|
// no runtime config in the Metadata
|
|
assert.Error(err)
|
|
}
|
|
|
|
func TestFactoryCLIFunctionInit(t *testing.T) {
|
|
assert := assert.New(t)
|
|
|
|
tmpdir, err := ioutil.TempDir("", "")
|
|
assert.NoError(err)
|
|
defer os.RemoveAll(tmpdir)
|
|
|
|
runtimeConfig, err := newTestRuntimeConfig(tmpdir, testConsole, true)
|
|
assert.NoError(err)
|
|
|
|
set := flag.NewFlagSet("", 0)
|
|
|
|
set.String("console-socket", "", "")
|
|
|
|
app := cli.NewApp()
|
|
ctx := cli.NewContext(app, set, nil)
|
|
app.Name = "foo"
|
|
|
|
// No template
|
|
ctx.App.Metadata = map[string]interface{}{
|
|
"runtimeConfig": runtimeConfig,
|
|
}
|
|
fn, ok := initFactoryCommand.Action.(func(context *cli.Context) error)
|
|
assert.True(ok)
|
|
err = fn(ctx)
|
|
assert.Nil(err)
|
|
|
|
// With template
|
|
runtimeConfig.FactoryConfig.Template = true
|
|
runtimeConfig.HypervisorType = vc.MockHypervisor
|
|
runtimeConfig.AgentType = vc.NoopAgentType
|
|
ctx.App.Metadata["runtimeConfig"] = runtimeConfig
|
|
fn, ok = initFactoryCommand.Action.(func(context *cli.Context) error)
|
|
assert.True(ok)
|
|
err = fn(ctx)
|
|
assert.Nil(err)
|
|
}
|
|
|
|
func TestFactoryCLIFunctionDestroy(t *testing.T) {
|
|
assert := assert.New(t)
|
|
|
|
tmpdir, err := ioutil.TempDir("", "")
|
|
assert.NoError(err)
|
|
defer os.RemoveAll(tmpdir)
|
|
|
|
runtimeConfig, err := newTestRuntimeConfig(tmpdir, testConsole, true)
|
|
assert.NoError(err)
|
|
|
|
set := flag.NewFlagSet("", 0)
|
|
|
|
set.String("console-socket", "", "")
|
|
|
|
app := cli.NewApp()
|
|
ctx := cli.NewContext(app, set, nil)
|
|
app.Name = "foo"
|
|
|
|
// No template
|
|
ctx.App.Metadata = map[string]interface{}{
|
|
"runtimeConfig": runtimeConfig,
|
|
}
|
|
fn, ok := destroyFactoryCommand.Action.(func(context *cli.Context) error)
|
|
assert.True(ok)
|
|
err = fn(ctx)
|
|
assert.Nil(err)
|
|
|
|
// With template
|
|
runtimeConfig.FactoryConfig.Template = true
|
|
runtimeConfig.HypervisorType = vc.MockHypervisor
|
|
runtimeConfig.AgentType = vc.NoopAgentType
|
|
ctx.App.Metadata["runtimeConfig"] = runtimeConfig
|
|
fn, ok = destroyFactoryCommand.Action.(func(context *cli.Context) error)
|
|
assert.True(ok)
|
|
err = fn(ctx)
|
|
assert.Nil(err)
|
|
}
|