Skip to content

Commit

Permalink
ciao-launcher: Add TestExtractImageInfo
Browse files Browse the repository at this point in the history
Add a new unit test case to test the code that extracts information from
qemu-img info.

Partial fix for issue ciao-project#1

Signed-off-by: Mark Ryan <[email protected]>
  • Loading branch information
Mark Ryan committed Jul 18, 2016
1 parent 57f6d19 commit c88d0ef
Show file tree
Hide file tree
Showing 2 changed files with 156 additions and 22 deletions.
50 changes: 28 additions & 22 deletions ciao-launcher/qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"bufio"
"bytes"
"fmt"
"io"
"io/ioutil"
"net"
"os"
Expand Down Expand Up @@ -65,28 +66,9 @@ func (q *qemu) init(cfg *vmConfig, instanceDir string) {
q.isoPath = path.Join(instanceDir, seedImage)
}

func (q *qemu) imageInfo(imagePath string) (imageSizeMB int, err error) {
imageSizeMB = -1

params := make([]string, 0, 8)
params = append(params, "info")
params = append(params, imagePath)

cmd := exec.Command("qemu-img", params...)
stdout, err := cmd.StdoutPipe()
if err != nil {
glog.Errorf("Unable to read output from qemu-img: %v", err)
return -1, err
}

err = cmd.Start()
if err != nil {
_ = stdout.Close()
glog.Errorf("Unable start qemu-img: %v", err)
return -1, err
}

scanner := bufio.NewScanner(stdout)
func extractImageInfo(r io.Reader) int {
imageSizeMB := -1
scanner := bufio.NewScanner(r)
for scanner.Scan() && imageSizeMB == -1 {
line := scanner.Text()
matches := virtualSizeRegexp.FindStringSubmatch(line)
Expand Down Expand Up @@ -120,6 +102,30 @@ func (q *qemu) imageInfo(imagePath string) (imageSizeMB int, err error) {
}
}

return imageSizeMB
}

func (q *qemu) imageInfo(imagePath string) (int, error) {
params := make([]string, 0, 8)
params = append(params, "info")
params = append(params, imagePath)

cmd := exec.Command("qemu-img", params...)
stdout, err := cmd.StdoutPipe()
if err != nil {
glog.Errorf("Unable to read output from qemu-img: %v", err)
return -1, err
}

err = cmd.Start()
if err != nil {
_ = stdout.Close()
glog.Errorf("Unable start qemu-img: %v", err)
return -1, err
}

imageSizeMB := extractImageInfo(stdout)

err = cmd.Wait()
if err != nil {
glog.Warningf("qemu-img returned an error: %v", err)
Expand Down
128 changes: 128 additions & 0 deletions ciao-launcher/qemu_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
// Copyright (c) 2016 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 (
"bytes"
"testing"
)

var imageInfoTestGood = `
image: /var/lib/ciao/images/clear-8780-cloud.img
file format: qcow2
virtual size: 865M (907018240 bytes)
disk size: 113M
cluster_size: 65536
Format specific information:
compat: 1.1
lazy refcounts: false
refcount bits: 16
corrupt: false
`

var imageInfoTestMissingBytes = `
image: /var/lib/ciao/images/clear-8780-cloud.img
file format: qcow2
virtual size: 865M
disk size: 113M
cluster_size: 65536
Format specific information:
compat: 1.1
lazy refcounts: false
refcount bits: 16
corrupt: false
`

var imageInfoTestMissingLine = `
image: /var/lib/ciao/images/clear-8780-cloud.img
file format: qcow2
disk size: 113M
cluster_size: 65536
Format specific information:
compat: 1.1
lazy refcounts: false
refcount bits: 16
corrupt: false
`

var imageInfoTooBig = `
image: /var/lib/ciao/images/clear-8780-cloud.img
file format: qcow2
virtual size: 18,446,744,073,710M (18446744073709551615 bytes)
disk size: 113M
cluster_size: 65536
Format specific information:
compat: 1.1
lazy refcounts: false
refcount bits: 16
corrupt: false
`

var imageInfoBadBytes = `
image: /var/lib/ciao/images/clear-8780-cloud.img
file format: qcow2
virtual size: 865M (9aaaa07018240 bytes)
disk size: 113M
cluster_size: 65536
Format specific information:
compat: 1.1
lazy refcounts: false
refcount bits: 16
corrupt: false
`

func TestExtractImageInfo(t *testing.T) {
tests := []struct {
name string
result int
data string
}{
{
"imageInfoTestGood",
908,
imageInfoTestGood,
},
{
"imageInfoTestMissingBytes",
-1,
imageInfoTestMissingBytes,
},
{
"imageInfoTestMissingLine",
-1,
imageInfoTestMissingLine,
},
{
"imageInfoTooBig",
-1,
imageInfoTooBig,
},
{
"imageInfoBadBytes",
-1,
imageInfoBadBytes,
},
}

for _, ti := range tests {
b := bytes.NewBuffer([]byte(ti.data))
result := extractImageInfo(b)
if result != ti.result {
t.Fatalf("%s failed. %d != %d", ti.name, result, ti.result)
}
}
}

0 comments on commit c88d0ef

Please sign in to comment.