Skip to content

Commit

Permalink
Merge pull request clearcontainers#546 from bergwolf/status-factory
Browse files Browse the repository at this point in the history
cli: support factory status command
  • Loading branch information
amshinde authored Aug 2, 2018
2 parents 16600ef + 024a38b commit 244917c
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 3 deletions.
41 changes: 38 additions & 3 deletions cli/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
var factorySubCmds = []cli.Command{
initFactoryCommand,
destroyFactoryCommand,
statusFactoryCommand,
}

var factoryCLICommand = cli.Command{
Expand Down Expand Up @@ -54,10 +55,10 @@ var initFactoryCommand = cli.Command{
kataLog.WithError(err).Error("create vm factory failed")
return err
}
fmt.Println("vm factory initialized")
fmt.Fprintln(defaultOutputFile, "vm factory initialized")
} else {
kataLog.Error("vm factory is not enabled")
fmt.Println("vm factory is not enabled")
fmt.Fprintln(defaultOutputFile, "vm factory is not enabled")
}

return nil
Expand Down Expand Up @@ -92,7 +93,41 @@ var destroyFactoryCommand = cli.Command{
f.CloseFactory()
}
}
fmt.Println("vm factory destroyed")
fmt.Fprintln(defaultOutputFile, "vm factory destroyed")
return nil
},
}

var statusFactoryCommand = cli.Command{
Name: "status",
Usage: "query the status of VM factory",
Action: func(context *cli.Context) error {
runtimeConfig, ok := context.App.Metadata["runtimeConfig"].(oci.RuntimeConfig)
if !ok {
return errors.New("invalid runtime config")
}

if runtimeConfig.FactoryConfig.Template {
factoryConfig := vf.Config{
Template: true,
VMConfig: vc.VMConfig{
HypervisorType: runtimeConfig.HypervisorType,
HypervisorConfig: runtimeConfig.HypervisorConfig,
AgentType: runtimeConfig.AgentType,
AgentConfig: runtimeConfig.AgentConfig,
},
}
kataLog.WithField("factory", factoryConfig).Info("load vm factory")
f, err := vf.NewFactory(factoryConfig, true)
if err != nil {
fmt.Fprintln(defaultOutputFile, "vm factory is off")
} else {
f.CloseFactory()
fmt.Fprintln(defaultOutputFile, "vm factory is on")
}
} else {
fmt.Fprintln(defaultOutputFile, "vm factory not enabled")
}
return nil
},
}
36 changes: 36 additions & 0 deletions cli/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,39 @@ func TestFactoryCLIFunctionDestroy(t *testing.T) {
err = fn(ctx)
assert.Nil(err)
}

func TestFactoryCLIFunctionStatus(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 := statusFactoryCommand.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
err = fn(ctx)
assert.Nil(err)
}

0 comments on commit 244917c

Please sign in to comment.