gopkg.in/libvirt/libvirt-go-xml.v7@v7.4.0/storage_vol_test.go (about)

     1  /*
     2   * This file is part of the libvirt-go-xml project
     3   *
     4   * Permission is hereby granted, free of charge, to any person obtaining a copy
     5   * of this software and associated documentation files (the "Software"), to deal
     6   * in the Software without restriction, including without limitation the rights
     7   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     8   * copies of the Software, and to permit persons to whom the Software is
     9   * furnished to do so, subject to the following conditions:
    10   *
    11   * The above copyright notice and this permission notice shall be included in
    12   * all copies or substantial portions of the Software.
    13   *
    14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    20   * THE SOFTWARE.
    21   *
    22   * Copyright (C) 2017 Red Hat, Inc.
    23   *
    24   */
    25  
    26  package libvirtxml
    27  
    28  import (
    29  	"strings"
    30  	"testing"
    31  )
    32  
    33  var storageVolumeTestData = []struct {
    34  	Object   *StorageVolume
    35  	Expected []string
    36  }{
    37  	{
    38  		Object: &StorageVolume{
    39  			Type: "file",
    40  			Name: "file.img",
    41  			Key:  "/file.img",
    42  			Allocation: &StorageVolumeSize{
    43  				Value: 0,
    44  			},
    45  
    46  			Capacity: &StorageVolumeSize{
    47  				Unit:  "T",
    48  				Value: 1,
    49  			},
    50  		},
    51  		Expected: []string{
    52  			`<volume type="file">`,
    53  			`  <name>file.img</name>`,
    54  			`  <key>/file.img</key>`,
    55  			`  <allocation>0</allocation>`,
    56  			`  <capacity unit="T">1</capacity>`,
    57  			`</volume>`,
    58  		},
    59  	},
    60  	{
    61  		Object: &StorageVolume{
    62  			Type: "file",
    63  			Name: "file.img",
    64  			Target: &StorageVolumeTarget{
    65  				Path: "/file.img",
    66  				Format: &StorageVolumeTargetFormat{
    67  					Type: "qcow2",
    68  				},
    69  				Permissions: &StorageVolumeTargetPermissions{
    70  					Owner: "107",
    71  					Group: "107",
    72  					Mode:  "0744",
    73  					Label: "image",
    74  				},
    75  				Timestamps: &StorageVolumeTargetTimestamps{
    76  					Atime: "1341933637.273190990",
    77  					Mtime: "1341930622.047245868",
    78  					Ctime: "1341930622.047245868",
    79  				},
    80  				Compat: "1.1",
    81  				NoCOW:  &struct{}{},
    82  				Features: []StorageVolumeTargetFeature{
    83  					StorageVolumeTargetFeature{
    84  						LazyRefcounts: &struct{}{},
    85  					},
    86  				},
    87  			},
    88  		},
    89  		Expected: []string{
    90  			`<volume type="file">`,
    91  			`  <name>file.img</name>`,
    92  			`  <target>`,
    93  			`    <path>/file.img</path>`,
    94  			`    <format type="qcow2"></format>`,
    95  			`    <permissions>`,
    96  			`      <owner>107</owner>`,
    97  			`      <group>107</group>`,
    98  			`      <mode>0744</mode>`,
    99  			`      <label>image</label>`,
   100  			`    </permissions>`,
   101  			`    <timestamps>`,
   102  			`      <atime>1341933637.273190990</atime>`,
   103  			`      <mtime>1341930622.047245868</mtime>`,
   104  			`      <ctime>1341930622.047245868</ctime>`,
   105  			`    </timestamps>`,
   106  			`    <compat>1.1</compat>`,
   107  			`    <nocow></nocow>`,
   108  			`    <features>`,
   109  			`      <lazy_refcounts></lazy_refcounts>`,
   110  			`    </features>`,
   111  			`  </target>`,
   112  			`</volume>`,
   113  		},
   114  	},
   115  	{
   116  		Object: &StorageVolume{
   117  			Type: "file",
   118  			Name: "file.img",
   119  			BackingStore: &StorageVolumeBackingStore{
   120  				Path: "/master.img",
   121  				Format: &StorageVolumeTargetFormat{
   122  					Type: "raw",
   123  				},
   124  				Permissions: &StorageVolumeTargetPermissions{
   125  					Owner: "107",
   126  					Group: "107",
   127  					Mode:  "0744",
   128  					Label: "label",
   129  				},
   130  			},
   131  		},
   132  		Expected: []string{
   133  			`<volume type="file">`,
   134  			`  <name>file.img</name>`,
   135  			`  <backingStore>`,
   136  			`    <path>/master.img</path>`,
   137  			`    <format type="raw"></format>`,
   138  			`    <permissions>`,
   139  			`      <owner>107</owner>`,
   140  			`      <group>107</group>`,
   141  			`      <mode>0744</mode>`,
   142  			`      <label>label</label>`,
   143  			`    </permissions>`,
   144  			`  </backingStore>`,
   145  			`</volume>`,
   146  		},
   147  	},
   148  	{
   149  		Object: &StorageVolume{
   150  			Name: "luks.img",
   151  			Capacity: &StorageVolumeSize{
   152  				Unit:  "G",
   153  				Value: 5,
   154  			},
   155  			Target: &StorageVolumeTarget{
   156  				Path: "/luks.img",
   157  				Format: &StorageVolumeTargetFormat{
   158  					Type: "raw",
   159  				},
   160  				Encryption: &StorageEncryption{
   161  					Format: "luks",
   162  					Secret: &StorageEncryptionSecret{
   163  						Type: "passphrase",
   164  						UUID: "f52a81b2-424e-490c-823d-6bd4235bc572",
   165  					},
   166  				},
   167  			},
   168  		},
   169  		Expected: []string{
   170  			`<volume>`,
   171  			`  <name>luks.img</name>`,
   172  			`  <capacity unit="G">5</capacity>`,
   173  			`  <target>`,
   174  			`    <path>/luks.img</path>`,
   175  			`    <format type="raw"></format>`,
   176  			`    <encryption format="luks">`,
   177  			`      <secret type="passphrase" uuid="f52a81b2-424e-490c-823d-6bd4235bc572"></secret>`,
   178  			`    </encryption>`,
   179  			`  </target>`,
   180  			`</volume>`,
   181  		},
   182  	},
   183  	{
   184  		Object: &StorageVolume{
   185  			Name: "twofish",
   186  			Capacity: &StorageVolumeSize{
   187  				Unit:  "G",
   188  				Value: 5,
   189  			},
   190  			Target: &StorageVolumeTarget{
   191  				Path: "/twofish.luks",
   192  				Format: &StorageVolumeTargetFormat{
   193  					Type: "raw",
   194  				},
   195  				Encryption: &StorageEncryption{
   196  					Format: "luks",
   197  					Secret: &StorageEncryptionSecret{
   198  						Type: "passphrase",
   199  						UUID: "f52a81b2-424e-490c-823d-6bd4235bc572",
   200  					},
   201  					Cipher: &StorageEncryptionCipher{
   202  						Name: "twofish",
   203  						Size: 256,
   204  						Mode: "cbc",
   205  						Hash: "sha256",
   206  					},
   207  					Ivgen: &StorageEncryptionIvgen{
   208  						Name: "plain64",
   209  						Hash: "sha256",
   210  					},
   211  				},
   212  			},
   213  		},
   214  		Expected: []string{
   215  			`<volume>`,
   216  			`  <name>twofish</name>`,
   217  			`  <capacity unit="G">5</capacity>`,
   218  			`  <target>`,
   219  			`    <path>/twofish.luks</path>`,
   220  			`    <format type="raw"></format>`,
   221  			`    <encryption format="luks">`,
   222  			`      <secret type="passphrase" uuid="f52a81b2-424e-490c-823d-6bd4235bc572"></secret>`,
   223  			`      <cipher name="twofish" size="256" mode="cbc" hash="sha256"></cipher>`,
   224  			`      <ivgen name="plain64" hash="sha256"></ivgen>`,
   225  			`    </encryption>`,
   226  			`  </target>`,
   227  			`</volume>`,
   228  		},
   229  	},
   230  }
   231  
   232  func TestStorageVolume(t *testing.T) {
   233  	for _, test := range storageVolumeTestData {
   234  		doc, err := test.Object.Marshal()
   235  		if err != nil {
   236  			t.Fatal(err)
   237  		}
   238  
   239  		expect := strings.Join(test.Expected, "\n")
   240  
   241  		if doc != expect {
   242  			t.Fatal("Bad xml:\n", string(doc), "\n does not match\n", expect, "\n")
   243  		}
   244  	}
   245  }