gopkg.in/libvirt/libvirt-go-xml.v7@v7.4.0/domain_snapshot_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 domainSnapshotTestData = []struct {
    34  	Object   *DomainSnapshot
    35  	Expected []string
    36  }{
    37  	{
    38  		Object: &DomainSnapshot{
    39  			Description: "Snapshot",
    40  			Disks: &DomainSnapshotDisks{
    41  				[]DomainSnapshotDisk{
    42  					DomainSnapshotDisk{
    43  						Name: "/old",
    44  						Source: &DomainDiskSource{
    45  							File: &DomainDiskSourceFile{
    46  								File: "/new",
    47  							},
    48  						},
    49  					},
    50  					DomainSnapshotDisk{
    51  						Name:     "vdb",
    52  						Snapshot: "no",
    53  					},
    54  				},
    55  			},
    56  		},
    57  		Expected: []string{
    58  			`<domainsnapshot>`,
    59  			`  <description>Snapshot</description>`,
    60  			`  <disks>`,
    61  			`    <disk type="file" name="/old">`,
    62  			`      <source file="/new"></source>`,
    63  			`    </disk>`,
    64  			`    <disk name="vdb" snapshot="no"></disk>`,
    65  			`  </disks>`,
    66  			`</domainsnapshot>`,
    67  		},
    68  	},
    69  	{
    70  		Object: &DomainSnapshot{
    71  			Name:         "1270477159",
    72  			Description:  "Snapshot of OS install and updates",
    73  			State:        "running",
    74  			CreationTime: "1270477159",
    75  			Parent: &DomainSnapshotParent{
    76  				Name: "bare-os-install",
    77  			},
    78  			Memory: &DomainSnapshotMemory{
    79  				Snapshot: "no",
    80  			},
    81  			Disks: &DomainSnapshotDisks{
    82  				Disks: []DomainSnapshotDisk{
    83  					DomainSnapshotDisk{
    84  						Name:     "vda",
    85  						Snapshot: "external",
    86  						Driver: &DomainDiskDriver{
    87  							Type: "qcow2",
    88  						},
    89  						Source: &DomainDiskSource{
    90  							File: &DomainDiskSourceFile{
    91  								File: "/path/to/new",
    92  							},
    93  						},
    94  					},
    95  					DomainSnapshotDisk{
    96  						Name:     "vdb",
    97  						Snapshot: "no",
    98  					},
    99  				},
   100  			},
   101  			Domain: &Domain{
   102  				Name: "fedora",
   103  				Memory: &DomainMemory{
   104  					Value: 1048576,
   105  				},
   106  				Devices: &DomainDeviceList{
   107  					Disks: []DomainDisk{
   108  						DomainDisk{
   109  							Device: "disk",
   110  							Driver: &DomainDiskDriver{
   111  								Name: "qemu",
   112  								Type: "raw",
   113  							},
   114  							Source: &DomainDiskSource{
   115  								File: &DomainDiskSourceFile{
   116  									File: "/path/to/old",
   117  								},
   118  							},
   119  							Target: &DomainDiskTarget{
   120  								Dev: "vda",
   121  								Bus: "virtio",
   122  							},
   123  						},
   124  						DomainDisk{
   125  							Device:   "disk",
   126  							Snapshot: "external",
   127  							Driver: &DomainDiskDriver{
   128  								Name: "qemu",
   129  								Type: "raw",
   130  							},
   131  							Source: &DomainDiskSource{
   132  								File: &DomainDiskSourceFile{
   133  									File: "/path/to/old2",
   134  								},
   135  							},
   136  							Target: &DomainDiskTarget{
   137  								Dev: "vdb",
   138  								Bus: "virtio",
   139  							},
   140  						},
   141  					},
   142  				},
   143  			},
   144  		},
   145  		Expected: []string{
   146  			`<domainsnapshot>`,
   147  			`  <name>1270477159</name>`,
   148  			`  <description>Snapshot of OS install and updates</description>`,
   149  			`  <state>running</state>`,
   150  			`  <creationTime>1270477159</creationTime>`,
   151  			`  <parent>`,
   152  			`    <name>bare-os-install</name>`,
   153  			`  </parent>`,
   154  			`  <memory snapshot="no"></memory>`,
   155  			`  <disks>`,
   156  			`    <disk type="file" name="vda" snapshot="external">`,
   157  			`      <driver type="qcow2"></driver>`,
   158  			`      <source file="/path/to/new"></source>`,
   159  			`    </disk>`,
   160  			`    <disk name="vdb" snapshot="no"></disk>`,
   161  			`  </disks>`,
   162  			`  <domain>`,
   163  			`    <name>fedora</name>`,
   164  			`    <memory>1048576</memory>`,
   165  			`    <devices>`,
   166  			`      <disk type="file" device="disk">`,
   167  			`        <driver name="qemu" type="raw"></driver>`,
   168  			`        <source file="/path/to/old"></source>`,
   169  			`        <target dev="vda" bus="virtio"></target>`,
   170  			`      </disk>`,
   171  			`      <disk type="file" device="disk" snapshot="external">`,
   172  			`        <driver name="qemu" type="raw"></driver>`,
   173  			`        <source file="/path/to/old2"></source>`,
   174  			`        <target dev="vdb" bus="virtio"></target>`,
   175  			`      </disk>`,
   176  			`    </devices>`,
   177  			`  </domain>`,
   178  			`</domainsnapshot>`,
   179  		},
   180  	},
   181  }
   182  
   183  func TestDomainSnapshot(t *testing.T) {
   184  	for _, test := range domainSnapshotTestData {
   185  		doc, err := test.Object.Marshal()
   186  		if err != nil {
   187  			t.Fatal(err)
   188  		}
   189  
   190  		expect := strings.Join(test.Expected, "\n")
   191  
   192  		if doc != expect {
   193  			t.Fatal("Bad xml:\n", string(doc), "\n does not match\n", expect, "\n")
   194  		}
   195  	}
   196  }