forked from ciao-project/ciao
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 ciao-project#1 Signed-off-by: Mark Ryan <[email protected]>
- Loading branch information
Mark Ryan
committed
Jul 18, 2016
1 parent
57f6d19
commit c88d0ef
Showing
2 changed files
with
156 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |