github.com/mirantis/virtlet@v1.5.2-0.20191204181327-1659b8a48e9b/pkg/libvirttools/qcow2_flexvolume_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 libvirttools
    18  
    19  import (
    20  	"io/ioutil"
    21  	"os"
    22  	"testing"
    23  
    24  	libvirtxml "github.com/libvirt/libvirt-go-xml"
    25  
    26  	"github.com/Mirantis/virtlet/pkg/metadata/types"
    27  	fakeutils "github.com/Mirantis/virtlet/pkg/utils/fake"
    28  	testutils "github.com/Mirantis/virtlet/pkg/utils/testing"
    29  	"github.com/Mirantis/virtlet/pkg/virt/fake"
    30  	"github.com/Mirantis/virtlet/tests/gm"
    31  )
    32  
    33  const (
    34  	TestVolumeName = "test-volume"
    35  )
    36  
    37  func prepareOptsFileForQcow2Volume() (string, error) {
    38  	tempfile, err := ioutil.TempFile("", "qcow2-flexvol-test-")
    39  	if err != nil {
    40  		return "", err
    41  	}
    42  	defer tempfile.Close()
    43  
    44  	content := []byte("{\"capacity\": \"424242\",\n\"uuid\":\"123\"}")
    45  	if _, err := tempfile.Write(content); err != nil {
    46  		return "", err
    47  	}
    48  
    49  	return tempfile.Name(), nil
    50  }
    51  
    52  func TestQCOW2VolumeNaming(t *testing.T) {
    53  	v := qcow2Volume{
    54  		volumeBase: volumeBase{
    55  			&types.VMConfig{DomainUUID: testUUID},
    56  			nil,
    57  		},
    58  		name: TestVolumeName,
    59  	}
    60  	expected := "virtlet-" + testUUID + "-" + TestVolumeName
    61  	volumeName := v.volumeName()
    62  	if volumeName != expected {
    63  		t.Errorf("Incorrect root volume image name. Expected %s, received %s", expected, volumeName)
    64  	}
    65  }
    66  
    67  func TestQCOW2VolumeLifeCycle(t *testing.T) {
    68  	rec := testutils.NewToplevelRecorder()
    69  
    70  	volumesPoolPath := "/fake/volumes/pool"
    71  	expectedVolumePath := volumesPoolPath + "/virtlet-" + testUUID + "-" + TestVolumeName
    72  	sc := fake.NewFakeStorageConnection(rec)
    73  	spool, err := sc.CreateStoragePool(&libvirtxml.StoragePool{
    74  		Name:   "volumes",
    75  		Target: &libvirtxml.StoragePoolTarget{Path: volumesPoolPath},
    76  	})
    77  
    78  	im := newFakeImageManager(rec.Child("image"))
    79  
    80  	optsFilePath, err := prepareOptsFileForQcow2Volume()
    81  	if err != nil {
    82  		t.Fatalf("prepareOptsFileForQcow2Volume returned an error: %v", err)
    83  	}
    84  	defer os.Remove(optsFilePath)
    85  
    86  	volume, err := newQCOW2Volume(
    87  		TestVolumeName,
    88  		optsFilePath,
    89  		&types.VMConfig{DomainUUID: testUUID, Image: "rootfs image name"},
    90  		newFakeVolumeOwner(sc, spool.(*fake.FakeStoragePool), im, fakeutils.NewCommander(nil, nil)),
    91  	)
    92  	if err != nil {
    93  		t.Fatalf("newQCOW2Volume returned an error: %v", err)
    94  	}
    95  
    96  	vol, _, err := volume.Setup()
    97  	if err != nil {
    98  		t.Errorf("Setup returned an error: %v", err)
    99  	}
   100  
   101  	if vol.Source.File == nil {
   102  		t.Errorf("Expected 'file' volume type")
   103  	}
   104  
   105  	if vol.Device != "disk" {
   106  		t.Errorf("Expected 'disk' as volume device, received: %s", vol.Device)
   107  	}
   108  
   109  	if vol.Source.File.File != expectedVolumePath {
   110  		t.Errorf("Expected '%s' as volume path, received: %s", expectedVolumePath, vol.Source.File.File)
   111  	}
   112  
   113  	out, err := vol.Marshal()
   114  	if err != nil {
   115  		t.Fatalf("error marshalling the volume: %v", err)
   116  	}
   117  	rec.Rec("volume retuned by qcow2_flexvolume", out)
   118  
   119  	if err := volume.Teardown(); err != nil {
   120  		t.Errorf("Teardown returned an error: %v", err)
   121  	}
   122  
   123  	gm.Verify(t, gm.NewYamlVerifier(rec.Content()))
   124  }