github.com/libvirt/libvirt-go-xml@v7.4.0+incompatible/secret_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) 2016 Red Hat, Inc. 23 * 24 */ 25 26 package libvirtxml 27 28 import ( 29 "strings" 30 "testing" 31 ) 32 33 var secretTestData = []struct { 34 Object *Secret 35 Expected []string 36 }{ 37 { 38 Object: &Secret{ 39 Description: "Demo", 40 }, 41 Expected: []string{ 42 `<secret>`, 43 ` <description>Demo</description>`, 44 `</secret>`, 45 }, 46 }, 47 { 48 Object: &Secret{ 49 Description: "Demo", 50 Private: "yes", 51 Ephemeral: "yes", 52 UUID: "55806c7d-8e93-456f-829b-607d8c198367", 53 }, 54 Expected: []string{ 55 `<secret ephemeral="yes" private="yes">`, 56 ` <description>Demo</description>`, 57 ` <uuid>55806c7d-8e93-456f-829b-607d8c198367</uuid>`, 58 `</secret>`, 59 }, 60 }, 61 { 62 Object: &Secret{ 63 Description: "Demo", 64 Private: "yes", 65 Ephemeral: "yes", 66 UUID: "55806c7d-8e93-456f-829b-607d8c198367", 67 Usage: &SecretUsage{ 68 Type: "volume", 69 Volume: "/var/lib/libvirt/images/puppyname.img", 70 }, 71 }, 72 Expected: []string{ 73 `<secret ephemeral="yes" private="yes">`, 74 ` <description>Demo</description>`, 75 ` <uuid>55806c7d-8e93-456f-829b-607d8c198367</uuid>`, 76 ` <usage type="volume">`, 77 ` <volume>/var/lib/libvirt/images/puppyname.img</volume>`, 78 ` </usage>`, 79 `</secret>`, 80 }, 81 }, 82 { 83 Object: &Secret{ 84 Description: "Demo", 85 Private: "yes", 86 Ephemeral: "yes", 87 UUID: "55806c7d-8e93-456f-829b-607d8c198367", 88 Usage: &SecretUsage{ 89 Type: "ceph", 90 Name: "mycluster", 91 }, 92 }, 93 Expected: []string{ 94 `<secret ephemeral="yes" private="yes">`, 95 ` <description>Demo</description>`, 96 ` <uuid>55806c7d-8e93-456f-829b-607d8c198367</uuid>`, 97 ` <usage type="ceph">`, 98 ` <name>mycluster</name>`, 99 ` </usage>`, 100 `</secret>`, 101 }, 102 }, 103 { 104 Object: &Secret{ 105 Description: "Demo", 106 Private: "yes", 107 Ephemeral: "yes", 108 UUID: "55806c7d-8e93-456f-829b-607d8c198367", 109 Usage: &SecretUsage{ 110 Type: "iscsi", 111 Target: "libvirtiscsi", 112 }, 113 }, 114 Expected: []string{ 115 `<secret ephemeral="yes" private="yes">`, 116 ` <description>Demo</description>`, 117 ` <uuid>55806c7d-8e93-456f-829b-607d8c198367</uuid>`, 118 ` <usage type="iscsi">`, 119 ` <target>libvirtiscsi</target>`, 120 ` </usage>`, 121 `</secret>`, 122 }, 123 }, 124 { 125 Object: &Secret{ 126 Description: "Demo", 127 Private: "yes", 128 Ephemeral: "yes", 129 UUID: "55806c7d-8e93-456f-829b-607d8c198367", 130 Usage: &SecretUsage{ 131 Type: "tls", 132 Name: "Demo cert", 133 }, 134 }, 135 Expected: []string{ 136 `<secret ephemeral="yes" private="yes">`, 137 ` <description>Demo</description>`, 138 ` <uuid>55806c7d-8e93-456f-829b-607d8c198367</uuid>`, 139 ` <usage type="tls">`, 140 ` <name>Demo cert</name>`, 141 ` </usage>`, 142 `</secret>`, 143 }, 144 }, 145 } 146 147 func TestSecret(t *testing.T) { 148 for _, test := range secretTestData { 149 doc, err := test.Object.Marshal() 150 if err != nil { 151 t.Fatal(err) 152 } 153 154 expect := strings.Join(test.Expected, "\n") 155 156 if doc != expect { 157 t.Fatal("Bad xml:\n", string(doc), "\n does not match\n", expect, "\n") 158 } 159 } 160 }