gopkg.in/libvirt/libvirt-go-xml.v7@v7.4.0/storage_pool_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 adapterDomain uint = 0
    34  var adapterBus uint = 0
    35  var adapterSlot uint = 31
    36  var adapterFunction uint = 2
    37  
    38  var storagePoolTestData = []struct {
    39  	Object   *StoragePool
    40  	Expected []string
    41  }{
    42  	{
    43  		Object: &StoragePool{
    44  			Type:       "dir",
    45  			Name:       "pool",
    46  			UUID:       "3e3fce45-4f53-4fa7-bb32-11f34168b82b",
    47  			Allocation: &StoragePoolSize{Value: 1000000},
    48  			Capacity:   &StoragePoolSize{Value: 5000000},
    49  			Available:  &StoragePoolSize{Value: 3000000},
    50  		},
    51  		Expected: []string{
    52  			`<pool type="dir">`,
    53  			`  <name>pool</name>`,
    54  			`  <uuid>3e3fce45-4f53-4fa7-bb32-11f34168b82b</uuid>`,
    55  			`  <allocation>1000000</allocation>`,
    56  			`  <capacity>5000000</capacity>`,
    57  			`  <available>3000000</available>`,
    58  			`</pool>`,
    59  		},
    60  	},
    61  	{
    62  		Object: &StoragePool{
    63  			Type: "iscsi",
    64  			Name: "pool",
    65  			Source: &StoragePoolSource{
    66  				Host: []StoragePoolSourceHost{
    67  					StoragePoolSourceHost{
    68  						Name: "host.example.com",
    69  					},
    70  				},
    71  				Device: []StoragePoolSourceDevice{
    72  					StoragePoolSourceDevice{
    73  						Path: "pool.example.com:iscsi-pool",
    74  					},
    75  				},
    76  				Auth: &StoragePoolSourceAuth{
    77  					Type:     "chap",
    78  					Username: "username",
    79  					Secret: &StoragePoolSourceAuthSecret{
    80  						Usage: "cluster",
    81  					},
    82  				},
    83  				Vendor: &StoragePoolSourceVendor{
    84  					Name: "vendor",
    85  				},
    86  				Product: &StoragePoolSourceProduct{
    87  					Name: "product",
    88  				},
    89  			},
    90  		},
    91  		Expected: []string{
    92  			`<pool type="iscsi">`,
    93  			`  <name>pool</name>`,
    94  			`  <source>`,
    95  			`    <host name="host.example.com"></host>`,
    96  			`    <device path="pool.example.com:iscsi-pool"></device>`,
    97  			`    <auth type="chap" username="username">`,
    98  			`      <secret usage="cluster"></secret>`,
    99  			`    </auth>`,
   100  			`    <vendor name="vendor"></vendor>`,
   101  			`    <product name="product"></product>`,
   102  			`  </source>`,
   103  			`</pool>`,
   104  		},
   105  	},
   106  	{
   107  		Object: &StoragePool{
   108  			Type: "disk",
   109  			Name: "pool",
   110  			Source: &StoragePoolSource{
   111  				Device: []StoragePoolSourceDevice{
   112  					StoragePoolSourceDevice{
   113  						Path:          "/dev/mapper/pool",
   114  						PartSeparator: "no",
   115  					},
   116  				},
   117  				Format: &StoragePoolSourceFormat{
   118  					Type: "gpt",
   119  				},
   120  			},
   121  		},
   122  		Expected: []string{
   123  			`<pool type="disk">`,
   124  			`  <name>pool</name>`,
   125  			`  <source>`,
   126  			`    <device path="/dev/mapper/pool" part_separator="no"></device>`,
   127  			`    <format type="gpt"></format>`,
   128  			`  </source>`,
   129  			`</pool>`,
   130  		},
   131  	},
   132  	{
   133  		Object: &StoragePool{
   134  			Type: "scsi",
   135  			Name: "pool",
   136  			Source: &StoragePoolSource{
   137  				Adapter: &StoragePoolSourceAdapter{
   138  					Type: "scsi_host",
   139  					Name: "scsi_host",
   140  				},
   141  			},
   142  		},
   143  		Expected: []string{
   144  			`<pool type="scsi">`,
   145  			`  <name>pool</name>`,
   146  			`  <source>`,
   147  			`    <adapter type="scsi_host" name="scsi_host"></adapter>`,
   148  			`  </source>`,
   149  			`</pool>`,
   150  		},
   151  	},
   152  	{
   153  		Object: &StoragePool{
   154  			Type: "scsi",
   155  			Name: "pool",
   156  			Source: &StoragePoolSource{
   157  				Adapter: &StoragePoolSourceAdapter{
   158  					Type: "scsi_host",
   159  					ParentAddr: &StoragePoolSourceAdapterParentAddr{
   160  						UniqueID: 1,
   161  						Address: &StoragePoolPCIAddress{
   162  							Domain:   &adapterDomain,
   163  							Bus:      &adapterBus,
   164  							Slot:     &adapterSlot,
   165  							Function: &adapterFunction,
   166  						},
   167  					},
   168  				},
   169  			},
   170  		},
   171  		Expected: []string{
   172  			`<pool type="scsi">`,
   173  			`  <name>pool</name>`,
   174  			`  <source>`,
   175  			`    <adapter type="scsi_host">`,
   176  			`      <parentaddr unique_id="1">`,
   177  			`        <address domain="0x0000" bus="0x00" slot="0x1f" function="0x2"></address>`,
   178  			`      </parentaddr>`,
   179  			`    </adapter>`,
   180  			`  </source>`,
   181  			`</pool>`,
   182  		},
   183  	},
   184  	{
   185  		Object: &StoragePool{
   186  			Type: "fs",
   187  			Name: "pool",
   188  			Source: &StoragePoolSource{
   189  				Adapter: &StoragePoolSourceAdapter{
   190  					Type:   "fc_host",
   191  					Parent: "scsi_parent",
   192  					WWNN:   "20000000c9831b4b",
   193  					WWPN:   "10000000c9831b4b",
   194  				},
   195  			},
   196  		},
   197  		Expected: []string{
   198  			`<pool type="fs">`,
   199  			`  <name>pool</name>`,
   200  			`  <source>`,
   201  			`    <adapter type="fc_host" parent="scsi_parent" wwnn="20000000c9831b4b" wwpn="10000000c9831b4b"></adapter>`,
   202  			`  </source>`,
   203  			`</pool>`,
   204  		},
   205  	},
   206  	{
   207  		Object: &StoragePool{
   208  			Type: "dir",
   209  			Name: "pool",
   210  			Target: &StoragePoolTarget{
   211  				Path: "/pool",
   212  				Permissions: &StoragePoolTargetPermissions{
   213  					Owner: "1",
   214  					Group: "1",
   215  					Mode:  "0744",
   216  					Label: "pool",
   217  				},
   218  			},
   219  		},
   220  		Expected: []string{
   221  			`<pool type="dir">`,
   222  			`  <name>pool</name>`,
   223  			`  <target>`,
   224  			`    <path>/pool</path>`,
   225  			`    <permissions>`,
   226  			`      <owner>1</owner>`,
   227  			`      <group>1</group>`,
   228  			`      <mode>0744</mode>`,
   229  			`      <label>pool</label>`,
   230  			`    </permissions>`,
   231  			`  </target>`,
   232  			`</pool>`,
   233  		},
   234  	},
   235  }
   236  
   237  func TestStoragePool(t *testing.T) {
   238  	for _, test := range storagePoolTestData {
   239  		doc, err := test.Object.Marshal()
   240  		if err != nil {
   241  			t.Fatal(err)
   242  		}
   243  
   244  		expect := strings.Join(test.Expected, "\n")
   245  
   246  		if doc != expect {
   247  			t.Fatal("Bad xml:\n", string(doc), "\n does not match\n", expect, "\n")
   248  		}
   249  	}
   250  }