From c88d0ef4b3b678a8cdbb035da79338dde30a9e8c Mon Sep 17 00:00:00 2001 From: Mark Ryan Date: Mon, 18 Jul 2016 14:48:49 +0100 Subject: [PATCH] ciao-launcher: Add TestExtractImageInfo Add a new unit test case to test the code that extracts information from qemu-img info. Partial fix for issue #1 Signed-off-by: Mark Ryan --- ciao-launcher/qemu.go | 50 ++++++++------- ciao-launcher/qemu_test.go | 128 +++++++++++++++++++++++++++++++++++++ 2 files changed, 156 insertions(+), 22 deletions(-) create mode 100644 ciao-launcher/qemu_test.go diff --git a/ciao-launcher/qemu.go b/ciao-launcher/qemu.go index 2eac37ecd..53000ebe7 100644 --- a/ciao-launcher/qemu.go +++ b/ciao-launcher/qemu.go @@ -20,6 +20,7 @@ import ( "bufio" "bytes" "fmt" + "io" "io/ioutil" "net" "os" @@ -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) @@ -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) diff --git a/ciao-launcher/qemu_test.go b/ciao-launcher/qemu_test.go new file mode 100644 index 000000000..a333119fc --- /dev/null +++ b/ciao-launcher/qemu_test.go @@ -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) + } + } +}