github.com/kubri/kubri@v0.5.1-0.20240317001612-bda2aaef967e/integrations/yum/types_test.go (about) 1 package yum_test 2 3 import ( 4 "encoding/xml" 5 "testing" 6 7 "github.com/google/go-cmp/cmp" 8 "github.com/google/go-cmp/cmp/cmpopts" 9 10 "github.com/kubri/kubri/integrations/yum" 11 ) 12 13 func TestRepoMDMarshalUnmarshal(t *testing.T) { 14 in := &yum.RepoMD{ 15 Data: []yum.Data{ 16 { 17 Type: "primary", 18 Location: yum.Location{ 19 HREF: "repodata/primary.xml.gz", 20 }, 21 Checksum: yum.Checksum{ 22 Type: "sha256", 23 Value: "abc", 24 }, 25 OpenChecksum: yum.Checksum{ 26 Type: "sha256", 27 Value: "abc", 28 }, 29 Timestamp: 1192033773, 30 }, 31 { 32 Type: "filelists", 33 Location: yum.Location{ 34 HREF: "repodata/filelists.xml.gz", 35 }, 36 Checksum: yum.Checksum{ 37 Type: "sha256", 38 Value: "abc", 39 }, 40 OpenChecksum: yum.Checksum{ 41 Type: "sha256", 42 Value: "abc", 43 }, 44 Timestamp: 1192033773, 45 }, 46 { 47 Type: "other", 48 Location: yum.Location{ 49 HREF: "repodata/other.xml.gz", 50 }, 51 Checksum: yum.Checksum{ 52 Type: "sha256", 53 Value: "abc", 54 }, 55 OpenChecksum: yum.Checksum{ 56 Type: "sha256", 57 Value: "abc", 58 }, 59 Timestamp: 1192033773, 60 }, 61 }, 62 } 63 64 want := `<repomd xmlns="http://linux.duke.edu/metadata/repo"> 65 <data type="primary"> 66 <checksum type="sha256">abc</checksum> 67 <open-checksum type="sha256">abc</open-checksum> 68 <location href="repodata/primary.xml.gz"></location> 69 <timestamp>1192033773</timestamp> 70 </data> 71 <data type="filelists"> 72 <checksum type="sha256">abc</checksum> 73 <open-checksum type="sha256">abc</open-checksum> 74 <location href="repodata/filelists.xml.gz"></location> 75 <timestamp>1192033773</timestamp> 76 </data> 77 <data type="other"> 78 <checksum type="sha256">abc</checksum> 79 <open-checksum type="sha256">abc</open-checksum> 80 <location href="repodata/other.xml.gz"></location> 81 <timestamp>1192033773</timestamp> 82 </data> 83 </repomd>` 84 85 testMarshalUnmarshal(t, in, want) 86 } 87 88 func TestMetaDataMarshalUnmarshal(t *testing.T) { 89 in := &yum.MetaData{ 90 Package: []yum.Package{{ 91 Type: "rpm", 92 Name: "Title", 93 Arch: "x86_64", 94 Version: yum.Version{ 95 Ver: "1.0", 96 Rel: "1", 97 Epoch: "0", 98 }, 99 Checksum: yum.Checksum{ 100 Type: "sha256", 101 Value: "abc", 102 }, 103 Summary: "Summary", 104 Description: "Description", 105 Time: yum.Time{ 106 File: 1192033774, 107 Build: 1192033773, 108 }, 109 Size: yum.Size{ 110 Package: 1000, 111 Installed: 2000, 112 Archive: 2000, 113 }, 114 Location: yum.Location{ 115 HREF: "Packages/test.x86_64.rpm", 116 }, 117 Format: yum.Format{ 118 License: "MIT", 119 HeaderRange: yum.HeaderRange{ 120 Start: 5, 121 End: 10, 122 }, 123 }, 124 }}, 125 } 126 127 want := `<metadata xmlns="http://linux.duke.edu/metadata/common" xmlns:rpm="http://linux.duke.edu/metadata/rpm" packages="1"> 128 <package type="rpm"> 129 <name>Title</name> 130 <arch>x86_64</arch> 131 <version ver="1.0" rel="1" epoch="0"></version> 132 <checksum type="sha256">abc</checksum> 133 <summary>Summary</summary> 134 <description>Description</description> 135 <time file="1192033774" build="1192033773"></time> 136 <size package="1000" installed="2000" archive="2000"></size> 137 <location href="Packages/test.x86_64.rpm"></location> 138 <format> 139 <rpm:license>MIT</rpm:license> 140 <rpm:header-range start="5" end="10"></rpm:header-range> 141 </format> 142 </package> 143 </metadata>` 144 145 testMarshalUnmarshal(t, in, want) 146 } 147 148 func testMarshalUnmarshal[T any](t *testing.T, in *T, want string) { 149 t.Helper() 150 151 b, err := xml.MarshalIndent(in, "", "\t") 152 if err != nil { 153 t.Fatal(err) 154 } 155 156 if diff := cmp.Diff(want, string(b)); diff != "" { 157 t.Fatal(diff) 158 } 159 160 got := new(T) 161 if err = xml.Unmarshal(b, got); err != nil { 162 t.Fatal(err) 163 } 164 165 if diff := cmp.Diff(in, got, cmpopts.IgnoreTypes(xml.Name{})); diff != "" { 166 t.Fatal(diff) 167 } 168 }