Skip to content

Commit

Permalink
Merge pull request ciao-project#1377 from dlespiau/20170720-2-documen…
Browse files Browse the repository at this point in the history
…ts-state-file

ciao-down: Make workload and state files be only 2 YAML documents
  • Loading branch information
Mark Ryan authored Jul 21, 2017
2 parents d3a5fa5 + 2b8bca4 commit 3b6ebc4
Show file tree
Hide file tree
Showing 11 changed files with 251 additions and 77 deletions.
24 changes: 12 additions & 12 deletions testutil/ciao-down/ciao_down.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func createFlags(ctx context.Context, ws *workspace) (*workload, bool, error) {
return nil, false, err
}

in := &wkl.insData
in := &wkl.spec.VM
if memGiB != 0 {
in.MemGiB = memGiB
}
Expand All @@ -196,7 +196,7 @@ func createFlags(ctx context.Context, ws *workspace) (*workload, bool, error) {
in.mergePorts(p)

ws.Mounts = in.Mounts
ws.Hostname = wkl.insSpec.Hostname
ws.Hostname = wkl.spec.Hostname
if ws.NoProxy != "" {
ws.NoProxy = fmt.Sprintf("%s,%s", ws.Hostname, ws.NoProxy)
} else if ws.HTTPProxy != "" || ws.HTTPSProxy != "" {
Expand All @@ -207,7 +207,7 @@ func createFlags(ctx context.Context, ws *workspace) (*workload, bool, error) {
return wkl, debug, nil
}

func startFlags(in *instance) error {
func startFlags(in *VMSpec) error {
var m mounts
var p ports

Expand Down Expand Up @@ -251,12 +251,12 @@ func create(ctx context.Context, errCh chan error) {
return
}

if wkld.insSpec.NeedsNestedVM && !hostSupportsNestedKVM() {
if wkld.spec.NeedsNestedVM && !hostSupportsNestedKVM() {
err = fmt.Errorf("nested KVM is not enabled. Please enable and try again")
return
}

in := &wkld.insData
in := &wkld.spec.VM
_, err = os.Stat(ws.instanceDir)
if err == nil {
err = fmt.Errorf("instance already exists")
Expand Down Expand Up @@ -289,8 +289,8 @@ func create(ctx context.Context, errCh chan error) {
return
}

fmt.Printf("Downloading %s\n", wkld.insSpec.BaseImageName)
qcowPath, err := downloadFile(ctx, wkld.insSpec.BaseImageURL, ws.ciaoDir, downloadProgress)
fmt.Printf("Downloading %s\n", wkld.spec.BaseImageName)
qcowPath, err := downloadFile(ctx, wkld.spec.BaseImageURL, ws.ciaoDir, downloadProgress)
if err != nil {
return
}
Expand Down Expand Up @@ -332,7 +332,7 @@ func start(ctx context.Context, errCh chan error) {
errCh <- err
return
}
in := &wkld.insData
in := &wkld.spec.VM

memGiB, CPUs := getMemAndCpus()
if in.MemGiB == 0 {
Expand All @@ -348,7 +348,7 @@ func start(ctx context.Context, errCh chan error) {
return
}

if wkld.insSpec.NeedsNestedVM && !hostSupportsNestedKVM() {
if wkld.spec.NeedsNestedVM && !hostSupportsNestedKVM() {
errCh <- fmt.Errorf("nested KVM is not enabled. Please enable and try again")
return
}
Expand Down Expand Up @@ -418,15 +418,15 @@ func status(ctx context.Context, errCh chan error) {
errCh <- fmt.Errorf("Unable to load instance state: %v", err)
return
}
in := &wkld.insData
in := &wkld.spec.VM

sshPort, err := in.sshPort()
if err != nil {
errCh <- fmt.Errorf("Instance does not have SSH port open. Unable to determine status")
return
}

statusVM(ctx, ws.instanceDir, ws.keyPath, wkld.insSpec.WorkloadName,
statusVM(ctx, ws.instanceDir, ws.keyPath, wkld.spec.WorkloadName,
sshPort)
errCh <- err
}
Expand All @@ -443,7 +443,7 @@ func connect(ctx context.Context, errCh chan error) {
errCh <- fmt.Errorf("Unable to load instance state: %v", err)
return
}
in := &wkld.insData
in := &wkld.spec.VM

path, err := exec.LookPath("ssh")
if err != nil {
Expand Down
22 changes: 12 additions & 10 deletions testutil/ciao-down/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,27 +56,29 @@ func (m mount) String() string {
return fmt.Sprintf("%s,%s,%s", m.Tag, m.SecurityModel, m.Path)
}

type instance struct {
// VMSpec holds the per-VM state.
type VMSpec struct {
MemGiB int `yaml:"mem_gib"`
CPUs int `yaml:"cpus"`
PortMappings []portMapping `yaml:"ports"`
Mounts []mount `yaml:"mounts"`
}

type instanceSpec struct {
type workloadSpec struct {
BaseImageURL string `yaml:"base_image_url"`
BaseImageName string `yaml:"base_image_name"`
Hostname string `yaml:"hostname"`
WorkloadName string `yaml:"workload"`
NeedsNestedVM bool `yaml:"needs_nested_vm"`
VM VMSpec `yaml:"vm"`
}

// This function creates a default instanceData object for legacy ciao-down
// ciao VMs. These old VMs did not store information about mounts and
// mapped ports as this information was hard-coded into ciao-down itself.
// Consequently, when migrating one of these old VMs we need to fill in
// the missing information.
func (in *instance) loadLegacyInstance(ws *workspace) error {
func (in *VMSpec) loadLegacyInstance(ws *workspace) error {
// Check for legacy state files.

vmType := CIAO
Expand Down Expand Up @@ -128,7 +130,7 @@ func (in *instance) loadLegacyInstance(ws *workspace) error {
return nil
}

func (in *instance) unmarshal(data []byte) error {
func (in *VMSpec) unmarshal(data []byte) error {
err := yaml.Unmarshal(data, in)
if err != nil {
return fmt.Errorf("Unable to unmarshal instance state : %v", err)
Expand Down Expand Up @@ -169,7 +171,7 @@ func (in *instance) unmarshal(data []byte) error {
return nil
}

func (in *instance) unmarshalWithTemplate(ws *workspace, data string) error {
func (in *VMSpec) unmarshalWithTemplate(ws *workspace, data string) error {
tmpl, err := template.New("instance-data").Parse(string(data))
if err != nil {
return fmt.Errorf("Unable to parse instance data template: %v", err)
Expand All @@ -182,7 +184,7 @@ func (in *instance) unmarshalWithTemplate(ws *workspace, data string) error {
return in.unmarshal(buf.Bytes())
}

func (in *instance) mergeMounts(m mounts) {
func (in *VMSpec) mergeMounts(m mounts) {
mountCount := len(in.Mounts)
for _, mount := range m {
var i int
Expand All @@ -200,7 +202,7 @@ func (in *instance) mergeMounts(m mounts) {
}
}

func (in *instance) mergePorts(p ports) {
func (in *VMSpec) mergePorts(p ports) {
portCount := len(in.PortMappings)
for _, port := range p {
var i int
Expand All @@ -218,7 +220,7 @@ func (in *instance) mergePorts(p ports) {
}
}

func (in *instance) sshPort() (int, error) {
func (in *VMSpec) sshPort() (int, error) {
for _, p := range in.PortMappings {
if p.Guest == 22 {
return p.Host, nil
Expand All @@ -227,7 +229,7 @@ func (in *instance) sshPort() (int, error) {
return 0, fmt.Errorf("No SSH port configured")
}

func (ins *instanceSpec) unmarshal(data []byte) error {
func (ins *workloadSpec) unmarshal(data []byte) error {
err := yaml.Unmarshal(data, ins)
if err != nil {
return fmt.Errorf("Unable to unmarshal instance specification : %v", err)
Expand Down Expand Up @@ -258,7 +260,7 @@ func (ins *instanceSpec) unmarshal(data []byte) error {
return nil
}

func (ins *instanceSpec) unmarshalWithTemplate(ws *workspace, data string) error {
func (ins *workloadSpec) unmarshalWithTemplate(ws *workspace, data string) error {
tmpl, err := template.New("instance-spec").Parse(string(data))
if err != nil {
return fmt.Errorf("Unable to parse instance data template: %v", err)
Expand Down
95 changes: 95 additions & 0 deletions testutil/ciao-down/mock_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
//
// Copyright (c) 2017 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

package main

import (
"io/ioutil"
"path"
"testing"

"os"

"github.com/stretchr/testify/assert"
)

const xenialWorkloadSpecNoVM = `
base_image_url: ` + guestDownloadURL + `
base_image_name: ` + guestImageFriendlyName + `
`

const sampleVMSpec = `
mem_gib: 3
cpus: 2
ports:
- host: 10022
guest: 22
mounts: []
`

const xenialWorkloadSpec = `
base_image_url: ` + guestDownloadURL + `
base_image_name: ` + guestImageFriendlyName + `
vm:
mem_gib: 3
cpus: 2
ports:
- host: 10022
guest: 22
mounts: []
`

var mockVMSpec = VMSpec{
MemGiB: 3,
CPUs: 2,
PortMappings: []portMapping{{Host: 10022, Guest: 22}},
Mounts: []mount{},
}

const sampleCloudInit = `
`

const sampleWorkload3Docs = "---\n" + xenialWorkloadSpecNoVM + "...\n---\n" + sampleVMSpec + "...\n---\n" + sampleCloudInit + "...\n"
const sampleWorkload = "---\n" + xenialWorkloadSpec + "...\n---\n" + sampleCloudInit + "...\n"

func createMockWorkSpaceWithWorkload(t *testing.T, workload string) *workspace {
ciaoDir, err := ioutil.TempDir("", "ciao-down-tests-")
assert.Nil(t, err)

instanceDir := path.Join(ciaoDir, "foo")
err = os.Mkdir(instanceDir, 0750)
assert.Nil(t, err)

ws := &workspace{
ciaoDir: ciaoDir,
instanceDir: instanceDir,
}

workloadFile := path.Join(ws.instanceDir, "state.yaml")
err = ioutil.WriteFile(workloadFile, []byte(workload), 0640)
assert.Nil(t, err)

return ws
}

func createMockWorkspace(t *testing.T) *workspace {
return createMockWorkSpaceWithWorkload(t, sampleWorkload)
}

func cleanupMockWorkspace(t *testing.T, ws *workspace) {
err := os.RemoveAll(ws.ciaoDir)
assert.Nil(t, err)
}
2 changes: 1 addition & 1 deletion testutil/ciao-down/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const (
urlParam = "url"
)

func bootVM(ctx context.Context, ws *workspace, in *instance) error {
func bootVM(ctx context.Context, ws *workspace, in *VMSpec) error {
disconnectedCh := make(chan struct{})
socket := path.Join(ws.instanceDir, "socket")
qmp, _, err := qemu.QMPStart(ctx, socket, qemu.QMPConfig{}, disconnectedCh)
Expand Down
Loading

0 comments on commit 3b6ebc4

Please sign in to comment.