github.com/mirantis/virtlet@v1.5.2-0.20191204181327-1659b8a48e9b/pkg/image/virtualsize_test.go (about) 1 /* 2 Copyright 2018 Mirantis 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package image 18 19 import ( 20 "io/ioutil" 21 "os" 22 "os/exec" 23 "path/filepath" 24 "runtime" 25 "testing" 26 ) 27 28 const ( 29 expectedImageSize = 10485760 30 ) 31 32 // Here's how this test was made: 33 // $ qemu-img create -f qcow2 /tmp/foobar.qcow2 10M 34 // Formatting '/tmp/foobar.qcow2', fmt=qcow2 size=10485760 encryption=off cluster_size=65536 lazy_refcounts=off refcount_bits=16 35 // $ qemu-img info --output json /tmp/foobar.qcow2 36 // { 37 // "virtual-size": 10485760, 38 // "filename": "/tmp/foobar.qcow2", 39 // "cluster-size": 65536, 40 // "format": "qcow2", 41 // "actual-size": 200704, 42 // "format-specific": { 43 // "type": "qcow2", 44 // "data": { 45 // "compat": "1.1", 46 // "lazy-refcounts": false, 47 // "refcount-bits": 16, 48 // "corrupt": false 49 // } 50 // }, 51 // "dirty-flag": false 52 // } 53 54 func TestImageSize(t *testing.T) { 55 // it may be possible to run it on non-Linux systems but 56 // that would require installing qemu-img tools 57 if runtime.GOOS != "linux" { 58 t.Skip("ImageSize only works on Linux") 59 } 60 61 tmpDir, err := ioutil.TempDir("", "images") 62 if err != nil { 63 t.Fatalf("TempDir(): %v", err) 64 } 65 defer os.RemoveAll(tmpDir) 66 67 imagePath := filepath.Join(tmpDir, "image.qcow2") 68 if out, err := exec.Command("qemu-img", "create", "-f", "qcow2", imagePath, "10M").CombinedOutput(); err != nil { 69 t.Fatalf("qemu-img create: %q: %v\noutput:\n%v", imagePath, err, out) 70 } 71 72 imageSize, err := GetImageVirtualSize(imagePath) 73 if err != nil { 74 t.Fatalf("GetImageSize(): %q: %v", imagePath, err) 75 } 76 77 if imageSize != expectedImageSize { 78 t.Errorf("bad image size: %d instead of %d", imageSize, expectedImageSize) 79 } 80 }