github.com/zhaoxuat/libvirt-go-xml@v6.3.1-0.20200612053919-a025f1d30c41+incompatible/node_device_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 pciDomain uint = 1
    34  var pciBus uint = 21
    35  var pciSlot uint = 10
    36  var pciFunc uint = 50
    37  
    38  var NodeDeviceTestData = []struct {
    39  	Object *NodeDevice
    40  	XML    []string
    41  }{
    42  	{
    43  		Object: &NodeDevice{
    44  			Name:   "pci_0000_81_00_0",
    45  			Parent: "pci_0000_80_01_0",
    46  			Driver: &NodeDeviceDriver{
    47  				Name: "ixgbe",
    48  			},
    49  			Capability: NodeDeviceCapability{
    50  				PCI: &NodeDevicePCICapability{
    51  					Domain:   &pciDomain,
    52  					Bus:      &pciBus,
    53  					Slot:     &pciSlot,
    54  					Function: &pciFunc,
    55  					Product: NodeDeviceIDName{
    56  						ID:   "0x1528",
    57  						Name: "Ethernet Controller 10-Gigabit X540-AT2",
    58  					},
    59  					Vendor: NodeDeviceIDName{
    60  						ID:   "0x8086",
    61  						Name: "Intel Corporation",
    62  					},
    63  					IOMMUGroup: &NodeDeviceIOMMUGroup{
    64  						Number: 3,
    65  					},
    66  					NUMA: &NodeDeviceNUMA{
    67  						Node: 1,
    68  					},
    69  					Capabilities: []NodeDevicePCISubCapability{
    70  						NodeDevicePCISubCapability{
    71  							VirtFunctions: &NodeDevicePCIVirtFunctionsCapability{
    72  								MaxCount: 63,
    73  							},
    74  						},
    75  					},
    76  				},
    77  			},
    78  		},
    79  		XML: []string{
    80  			`<device>`,
    81  			`  <name>pci_0000_81_00_0</name>`,
    82  			`  <parent>pci_0000_80_01_0</parent>`,
    83  			`  <driver>`,
    84  			`    <name>ixgbe</name>`,
    85  			`  </driver>`,
    86  			`  <capability type="pci">`,
    87  			`    <domain>1</domain>`,
    88  			`    <bus>21</bus>`,
    89  			`    <slot>10</slot>`,
    90  			`    <function>50</function>`,
    91  			`    <product id="0x1528">Ethernet Controller 10-Gigabit X540-AT2</product>`,
    92  			`    <vendor id="0x8086">Intel Corporation</vendor>`,
    93  			`    <iommuGroup number="3"></iommuGroup>`,
    94  			`    <numa node="1"></numa>`,
    95  			`    <capability type="virt_functions" maxCount="63"></capability>`,
    96  			`  </capability>`,
    97  			`</device>`,
    98  		},
    99  	},
   100  }
   101  
   102  func TestNodeDevice(t *testing.T) {
   103  	for _, test := range NodeDeviceTestData {
   104  		expect := strings.Join(test.XML, "\n")
   105  
   106  		nodeDevice := NodeDevice{}
   107  		err := nodeDevice.Unmarshal(expect)
   108  		if err != nil {
   109  			t.Fatal(err)
   110  		}
   111  
   112  		doc, err := nodeDevice.Marshal()
   113  		if err != nil {
   114  			t.Fatal(err)
   115  		}
   116  
   117  		if doc != expect {
   118  			t.Fatal("Bad xml:\n", string(doc), "\n does not match\n", expect, "\n")
   119  		}
   120  	}
   121  }