reflist_test.go 11.4 KB
Newer Older
1 2 3 4
package deb

import (
	"errors"
5

6
	"github.com/aptly-dev/aptly/database"
7 8

	. "gopkg.in/check.v1"
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
)

type PackageRefListSuite struct {
	// Simple list with "real" packages from stanzas
	list                   *PackageList
	p1, p2, p3, p4, p5, p6 *Package
}

var _ = Suite(&PackageRefListSuite{})

func toStrSlice(reflist *PackageRefList) (result []string) {
	result = make([]string, reflist.Len())
	for i, r := range reflist.Refs {
		result[i] = string(r)
	}
	return
}

func (s *PackageRefListSuite) SetUpTest(c *C) {
	s.list = NewPackageList()

	s.p1 = NewPackageFromControlFile(packageStanza.Copy())
	s.p2 = NewPackageFromControlFile(packageStanza.Copy())
	stanza := packageStanza.Copy()
	stanza["Package"] = "mars-invaders"
	s.p3 = NewPackageFromControlFile(stanza)
	stanza = packageStanza.Copy()
36
	stanza["Source"] = "unknown-planet"
37 38 39 40 41 42 43 44 45 46
	s.p4 = NewPackageFromControlFile(stanza)
	stanza = packageStanza.Copy()
	stanza["Package"] = "lonely-strangers"
	s.p5 = NewPackageFromControlFile(stanza)
	stanza = packageStanza.Copy()
	stanza["Version"] = "99.1"
	s.p6 = NewPackageFromControlFile(stanza)
}

func (s *PackageRefListSuite) TestNewPackageListFromRefList(c *C) {
47
	db, _ := database.NewOpenDB(c.MkDir())
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
	coll := NewPackageCollection(db)
	coll.Update(s.p1)
	coll.Update(s.p3)

	s.list.Add(s.p1)
	s.list.Add(s.p3)
	s.list.Add(s.p5)
	s.list.Add(s.p6)

	reflist := NewPackageRefListFromPackageList(s.list)

	_, err := NewPackageListFromRefList(reflist, coll, nil)
	c.Assert(err, ErrorMatches, "unable to load package with key.*")

	coll.Update(s.p5)
	coll.Update(s.p6)

	list, err := NewPackageListFromRefList(reflist, coll, nil)
	c.Assert(err, IsNil)
	c.Check(list.Len(), Equals, 4)
	c.Check(list.Add(s.p4), ErrorMatches, "conflict in package.*")

	list, err = NewPackageListFromRefList(nil, coll, nil)
	c.Assert(err, IsNil)
	c.Check(list.Len(), Equals, 0)
}

func (s *PackageRefListSuite) TestNewPackageRefList(c *C) {
	s.list.Add(s.p1)
	s.list.Add(s.p3)
	s.list.Add(s.p5)
	s.list.Add(s.p6)

	reflist := NewPackageRefListFromPackageList(s.list)
	c.Assert(reflist.Len(), Equals, 4)
	c.Check(reflist.Refs[0], DeepEquals, []byte(s.p1.Key("")))
	c.Check(reflist.Refs[1], DeepEquals, []byte(s.p6.Key("")))
	c.Check(reflist.Refs[2], DeepEquals, []byte(s.p5.Key("")))
	c.Check(reflist.Refs[3], DeepEquals, []byte(s.p3.Key("")))

	reflist = NewPackageRefList()
	c.Check(reflist.Len(), Equals, 0)
}

func (s *PackageRefListSuite) TestPackageRefListEncodeDecode(c *C) {
	s.list.Add(s.p1)
	s.list.Add(s.p3)
	s.list.Add(s.p5)
	s.list.Add(s.p6)

	reflist := NewPackageRefListFromPackageList(s.list)

	reflist2 := &PackageRefList{}
	err := reflist2.Decode(reflist.Encode())
	c.Assert(err, IsNil)
	c.Check(reflist2.Len(), Equals, reflist.Len())
	c.Check(reflist2.Refs, DeepEquals, reflist.Refs)
}

func (s *PackageRefListSuite) TestPackageRefListForeach(c *C) {
	s.list.Add(s.p1)
	s.list.Add(s.p3)
	s.list.Add(s.p5)
	s.list.Add(s.p6)

	reflist := NewPackageRefListFromPackageList(s.list)

	Len := 0
	err := reflist.ForEach(func([]byte) error {
		Len++
		return nil
	})

	c.Check(Len, Equals, 4)
	c.Check(err, IsNil)

	e := errors.New("b")

	err = reflist.ForEach(func([]byte) error {
		return e
	})

	c.Check(err, Equals, e)
}

133 134 135 136 137 138 139 140 141 142 143 144 145
func (s *PackageRefListSuite) TestHas(c *C) {
	s.list.Add(s.p1)
	s.list.Add(s.p3)
	s.list.Add(s.p5)
	reflist := NewPackageRefListFromPackageList(s.list)

	c.Check(reflist.Has(s.p1), Equals, true)
	c.Check(reflist.Has(s.p3), Equals, true)
	c.Check(reflist.Has(s.p5), Equals, true)
	c.Check(reflist.Has(s.p2), Equals, true)
	c.Check(reflist.Has(s.p6), Equals, false)
}

146 147 148 149 150 151 152 153 154 155 156 157 158 159
func (s *PackageRefListSuite) TestSubstract(c *C) {
	r1 := []byte("r1")
	r2 := []byte("r2")
	r3 := []byte("r3")
	r4 := []byte("r4")
	r5 := []byte("r5")

	empty := &PackageRefList{Refs: [][]byte{}}
	l1 := &PackageRefList{Refs: [][]byte{r1, r2, r3, r4}}
	l2 := &PackageRefList{Refs: [][]byte{r1, r3}}
	l3 := &PackageRefList{Refs: [][]byte{r2, r4}}
	l4 := &PackageRefList{Refs: [][]byte{r4, r5}}
	l5 := &PackageRefList{Refs: [][]byte{r1, r2, r3}}

160 161 162 163 164 165
	c.Check(l1.Subtract(empty), DeepEquals, l1)
	c.Check(l1.Subtract(l2), DeepEquals, l3)
	c.Check(l1.Subtract(l3), DeepEquals, l2)
	c.Check(l1.Subtract(l4), DeepEquals, l5)
	c.Check(empty.Subtract(l1), DeepEquals, empty)
	c.Check(l2.Subtract(l3), DeepEquals, l2)
166 167 168
}

func (s *PackageRefListSuite) TestDiff(c *C) {
169
	db, _ := database.NewOpenDB(c.MkDir())
170 171 172
	coll := NewPackageCollection(db)

	packages := []*Package{
173 174 175 176 177 178 179
		{Name: "lib", Version: "1.0", Architecture: "i386"},      //0
		{Name: "dpkg", Version: "1.7", Architecture: "i386"},     //1
		{Name: "data", Version: "1.1~bp1", Architecture: "all"},  //2
		{Name: "app", Version: "1.1~bp1", Architecture: "i386"},  //3
		{Name: "app", Version: "1.1~bp2", Architecture: "i386"},  //4
		{Name: "app", Version: "1.1~bp2", Architecture: "amd64"}, //5
		{Name: "xyz", Version: "3.0", Architecture: "sparc"},     //6
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
	}

	for _, p := range packages {
		coll.Update(p)
	}

	listA := NewPackageList()
	listA.Add(packages[0])
	listA.Add(packages[1])
	listA.Add(packages[2])
	listA.Add(packages[3])
	listA.Add(packages[6])

	listB := NewPackageList()
	listB.Add(packages[0])
	listB.Add(packages[2])
	listB.Add(packages[4])
	listB.Add(packages[5])

	reflistA := NewPackageRefListFromPackageList(listA)
	reflistB := NewPackageRefListFromPackageList(listB)

	diffAA, err := reflistA.Diff(reflistA, coll)
	c.Check(err, IsNil)
	c.Check(diffAA, HasLen, 0)

	diffAB, err := reflistA.Diff(reflistB, coll)
	c.Check(err, IsNil)
	c.Check(diffAB, HasLen, 4)

	c.Check(diffAB[0].Left, IsNil)
	c.Check(diffAB[0].Right.String(), Equals, "app_1.1~bp2_amd64")

	c.Check(diffAB[1].Left.String(), Equals, "app_1.1~bp1_i386")
	c.Check(diffAB[1].Right.String(), Equals, "app_1.1~bp2_i386")

	c.Check(diffAB[2].Left.String(), Equals, "dpkg_1.7_i386")
	c.Check(diffAB[2].Right, IsNil)

	c.Check(diffAB[3].Left.String(), Equals, "xyz_3.0_sparc")
	c.Check(diffAB[3].Right, IsNil)

	diffBA, err := reflistB.Diff(reflistA, coll)
	c.Check(err, IsNil)
	c.Check(diffBA, HasLen, 4)

	c.Check(diffBA[0].Right, IsNil)
	c.Check(diffBA[0].Left.String(), Equals, "app_1.1~bp2_amd64")

	c.Check(diffBA[1].Right.String(), Equals, "app_1.1~bp1_i386")
	c.Check(diffBA[1].Left.String(), Equals, "app_1.1~bp2_i386")

	c.Check(diffBA[2].Right.String(), Equals, "dpkg_1.7_i386")
	c.Check(diffBA[2].Left, IsNil)

	c.Check(diffBA[3].Right.String(), Equals, "xyz_3.0_sparc")
	c.Check(diffBA[3].Left, IsNil)

}

func (s *PackageRefListSuite) TestMerge(c *C) {
241
	db, _ := database.NewOpenDB(c.MkDir())
242 243 244
	coll := NewPackageCollection(db)

	packages := []*Package{
245 246 247 248 249 250 251 252 253 254
		{Name: "lib", Version: "1.0", Architecture: "i386"},                      //0
		{Name: "dpkg", Version: "1.7", Architecture: "i386"},                     //1
		{Name: "data", Version: "1.1~bp1", Architecture: "all"},                  //2
		{Name: "app", Version: "1.1~bp1", Architecture: "i386"},                  //3
		{Name: "app", Version: "1.1~bp2", Architecture: "i386"},                  //4
		{Name: "app", Version: "1.1~bp2", Architecture: "amd64"},                 //5
		{Name: "dpkg", Version: "1.0", Architecture: "i386"},                     //6
		{Name: "xyz", Version: "1.0", Architecture: "sparc"},                     //7
		{Name: "dpkg", Version: "1.0", Architecture: "i386", FilesHash: 0x34445}, //8
		{Name: "app", Version: "1.1~bp2", Architecture: "i386", FilesHash: 0x44}, //9
255 256 257
	}

	for _, p := range packages {
258
		p.V06Plus = true
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
		coll.Update(p)
	}

	listA := NewPackageList()
	listA.Add(packages[0])
	listA.Add(packages[1])
	listA.Add(packages[2])
	listA.Add(packages[3])
	listA.Add(packages[7])

	listB := NewPackageList()
	listB.Add(packages[0])
	listB.Add(packages[2])
	listB.Add(packages[4])
	listB.Add(packages[5])
	listB.Add(packages[6])

276 277 278 279 280
	listC := NewPackageList()
	listC.Add(packages[0])
	listC.Add(packages[8])
	listC.Add(packages[9])

281 282
	reflistA := NewPackageRefListFromPackageList(listA)
	reflistB := NewPackageRefListFromPackageList(listB)
283
	reflistC := NewPackageRefListFromPackageList(listC)
284

285 286 287 288 289
	mergeAB := reflistA.Merge(reflistB, true, false)
	mergeBA := reflistB.Merge(reflistA, true, false)
	mergeAC := reflistA.Merge(reflistC, true, false)
	mergeBC := reflistB.Merge(reflistC, true, false)
	mergeCB := reflistC.Merge(reflistB, true, false)
290 291

	c.Check(toStrSlice(mergeAB), DeepEquals,
292
		[]string{"Pall data 1.1~bp1 00000000", "Pamd64 app 1.1~bp2 00000000", "Pi386 app 1.1~bp2 00000000", "Pi386 dpkg 1.0 00000000", "Pi386 lib 1.0 00000000", "Psparc xyz 1.0 00000000"})
293
	c.Check(toStrSlice(mergeBA), DeepEquals,
294 295 296 297 298 299 300 301 302 303 304 305 306
		[]string{"Pall data 1.1~bp1 00000000", "Pamd64 app 1.1~bp2 00000000", "Pi386 app 1.1~bp1 00000000", "Pi386 dpkg 1.7 00000000", "Pi386 lib 1.0 00000000", "Psparc xyz 1.0 00000000"})
	c.Check(toStrSlice(mergeAC), DeepEquals,
		[]string{"Pall data 1.1~bp1 00000000", "Pi386 app 1.1~bp2 00000044", "Pi386 dpkg 1.0 00034445", "Pi386 lib 1.0 00000000", "Psparc xyz 1.0 00000000"})
	c.Check(toStrSlice(mergeBC), DeepEquals,
		[]string{"Pall data 1.1~bp1 00000000", "Pamd64 app 1.1~bp2 00000000", "Pi386 app 1.1~bp2 00000044", "Pi386 dpkg 1.0 00034445", "Pi386 lib 1.0 00000000"})
	c.Check(toStrSlice(mergeCB), DeepEquals,
		[]string{"Pall data 1.1~bp1 00000000", "Pamd64 app 1.1~bp2 00000000", "Pi386 app 1.1~bp2 00000000", "Pi386 dpkg 1.0 00000000", "Pi386 lib 1.0 00000000"})

	mergeABall := reflistA.Merge(reflistB, false, false)
	mergeBAall := reflistB.Merge(reflistA, false, false)
	mergeACall := reflistA.Merge(reflistC, false, false)
	mergeBCall := reflistB.Merge(reflistC, false, false)
	mergeCBall := reflistC.Merge(reflistB, false, false)
307 308 309

	c.Check(mergeABall, DeepEquals, mergeBAall)
	c.Check(toStrSlice(mergeBAall), DeepEquals,
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
		[]string{"Pall data 1.1~bp1 00000000", "Pamd64 app 1.1~bp2 00000000", "Pi386 app 1.1~bp1 00000000", "Pi386 app 1.1~bp2 00000000",
			"Pi386 dpkg 1.0 00000000", "Pi386 dpkg 1.7 00000000", "Pi386 lib 1.0 00000000", "Psparc xyz 1.0 00000000"})

	c.Check(mergeBCall, Not(DeepEquals), mergeCBall)
	c.Check(toStrSlice(mergeACall), DeepEquals,
		[]string{"Pall data 1.1~bp1 00000000", "Pi386 app 1.1~bp1 00000000", "Pi386 app 1.1~bp2 00000044", "Pi386 dpkg 1.0 00034445",
			"Pi386 dpkg 1.7 00000000", "Pi386 lib 1.0 00000000", "Psparc xyz 1.0 00000000"})
	c.Check(toStrSlice(mergeBCall), DeepEquals,
		[]string{"Pall data 1.1~bp1 00000000", "Pamd64 app 1.1~bp2 00000000", "Pi386 app 1.1~bp2 00000044", "Pi386 dpkg 1.0 00034445",
			"Pi386 lib 1.0 00000000"})

	mergeBCwithConflicts := reflistB.Merge(reflistC, false, true)
	c.Check(toStrSlice(mergeBCwithConflicts), DeepEquals,
		[]string{"Pall data 1.1~bp1 00000000", "Pamd64 app 1.1~bp2 00000000", "Pi386 app 1.1~bp2 00000000", "Pi386 app 1.1~bp2 00000044",
			"Pi386 dpkg 1.0 00000000", "Pi386 dpkg 1.0 00034445", "Pi386 lib 1.0 00000000"})
325 326 327 328
}

func (s *PackageRefListSuite) TestFilterLatestRefs(c *C) {
	packages := []*Package{
329 330 331 332 333 334 335 336
		{Name: "lib", Version: "1.0", Architecture: "i386"},
		{Name: "lib", Version: "1.2~bp1", Architecture: "i386"},
		{Name: "lib", Version: "1.2", Architecture: "i386"},
		{Name: "dpkg", Version: "1.2", Architecture: "i386"},
		{Name: "dpkg", Version: "1.3", Architecture: "i386"},
		{Name: "dpkg", Version: "1.3~bp2", Architecture: "i386"},
		{Name: "dpkg", Version: "1.5", Architecture: "i386"},
		{Name: "dpkg", Version: "1.6", Architecture: "i386"},
337 338 339 340 341 342 343 344 345 346 347 348 349
	}

	rl := NewPackageList()
	rl.Add(packages[0])
	rl.Add(packages[1])
	rl.Add(packages[2])
	rl.Add(packages[3])
	rl.Add(packages[4])
	rl.Add(packages[5])
	rl.Add(packages[6])
	rl.Add(packages[7])

	result := NewPackageRefListFromPackageList(rl)
350
	result.FilterLatestRefs()
351 352 353 354

	c.Check(toStrSlice(result), DeepEquals,
		[]string{"Pi386 dpkg 1.6", "Pi386 lib 1.2"})
}