postprocess.lua 16.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
--[[
Copyright 2016, CZ.NIC z.s.p.o. (http://www.nic.cz/)

This file is part of the turris updater.

Updater is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Updater is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Updater.  If not, see <http://www.gnu.org/licenses/>.
]]--

require 'lunit'

local requests = require "requests"
local postprocess = require "postprocess"
24
local utils = require "utils"
25
local uri = require "uri"
26
require "syscnf"
27

28
syscnf.set_root_dir()
29
local dir = (os.getenv("S") .. "/") or ''
30 31 32 33 34 35 36

module("postprocess-tests", package.seeall, lunit.testcase)

local function repo_fake(name, uri, ok, content)
	local result =  {
		tp = "repository",
		name = name,
37 38 39 40 41 42 43 44 45 46
		repo_uri = uri
	}
	utils.private(result).index_uri = {
		[""] = {
			tp = "uri",
			uri = uri .. "/Packages",
			cback = function(self, cback)
				cback(ok, content)
			end,
			events = {}
47 48 49 50 51 52 53 54 55 56 57 58
		}
	}
	return result
end

local example_output = {
	{
		content = {
			[""] = {
				list = {
					["6in4"] = {
						Architecture = "all",
59
						Depends = "libc, kmod-sit",
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
						Description = [[Provides support for 6in4 tunnels in /etc/config/network.
 Refer to http://wiki.openwrt.org/doc/uci/network for
 configuration details.]],
						Filename = "6in4_21-2_all.ipk",
						["Installed-Size"] = "1558",
						License = "GPL-2.0",
						MD5Sum = "a2a58a05c002cf7b45fbe364794d96a5",
						Maintainer = "Jo-Philipp Wich <xm@subsignal.org>",
						Package = "6in4",
						SHA256sum = "06c3e5630a54a6c2d95ff13945b76e4122ac1a9e533fe4665c501ae26d55933d",
						Section = "net",
						Size = "2534",
						Source = "package/network/ipv6/6in4",
						Version = "21-2",
						uri_raw = "http://example.org/test1/6in4_21-2_all.ipk"
					},
					["6rd"] = {
						Architecture = "all",
78
						Depends = "libc, kmod-sit",
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
						Description = [[Provides support for 6rd tunnels in /etc/config/network.
 Refer to http://wiki.openwrt.org/doc/uci/network for
 configuration details.]],
						Filename = "6rd_9-2_all.ipk",
						["Installed-Size"] = "3432",
						License = "GPL-2.0",
						MD5Sum = "2b46cba96c887754f879676be77615e5",
						Maintainer = "Steven Barth <cyrus@openwrt.org>",
						Package = "6rd",
						SHA256sum = "e1081e495d0055f962a0ea4710239447eabf596f7acb06ccf0bd6f06b125fda8",
						Section = "net",
						Size = "4416",
						Source = "package/network/ipv6/6rd",
						Version = "9-2",
						uri_raw = "http://example.org/test1/6rd_9-2_all.ipk"
					}
				},
				tp="pkg-list"
			}
		},
		name="test1",
		repo_uri="http://example.org/test1",
		tp="parsed-repository"
	}
}
104 105
example_output[1].content[""].list["6in4"].repo = example_output[1]
example_output[1].content[""].list["6rd"].repo = example_output[1]
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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150

function test_get_repos_plain()
	requests.known_repositories_all = {
		repo_fake("test1", "http://example.org/test1", true, [[
Package: 6in4
Version: 21-2
Depends: libc, kmod-sit
Source: package/network/ipv6/6in4
License: GPL-2.0
Section: net
Maintainer: Jo-Philipp Wich <xm@subsignal.org>
Architecture: all
Installed-Size: 1558
Filename: 6in4_21-2_all.ipk
Size: 2534
MD5Sum: a2a58a05c002cf7b45fbe364794d96a5
SHA256sum: 06c3e5630a54a6c2d95ff13945b76e4122ac1a9e533fe4665c501ae26d55933d
Description:  Provides support for 6in4 tunnels in /etc/config/network.
 Refer to http://wiki.openwrt.org/doc/uci/network for
 configuration details.

Package: 6rd
Version: 9-2
Depends: libc, kmod-sit
Source: package/network/ipv6/6rd
License: GPL-2.0
Section: net
Maintainer: Steven Barth <cyrus@openwrt.org>
Architecture: all
Installed-Size: 3432
Filename: 6rd_9-2_all.ipk
Size: 4416
MD5Sum: 2b46cba96c887754f879676be77615e5
SHA256sum: e1081e495d0055f962a0ea4710239447eabf596f7acb06ccf0bd6f06b125fda8
Description:  Provides support for 6rd tunnels in /etc/config/network.
 Refer to http://wiki.openwrt.org/doc/uci/network for
 configuration details.
]])
	}
	assert_nil(postprocess.get_repos())
	assert_table_equal(example_output, requests.known_repositories_all)
end

function test_get_repos_gzip()
	local datadir = (os.getenv("S") or ".") .. "/tests/data"
151
	local content = utils.read_file(datadir .. "/Packages.gz")
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
	requests.known_repositories_all = {repo_fake("test1", "http://example.org/test1", true, content)}
	assert_nil(postprocess.get_repos())
	assert_table_equal(example_output, requests.known_repositories_all)
end

local multierror = utils.exception("multiple", "Multiple exceptions (1)")
local sub_err = utils.exception("unreachable", "Fake network is down")
sub_err.why = "missing"
sub_err.repo = "test1/http://example.org/test1/Packages"
multierror.errors = {sub_err}

function test_get_repos_broken_fatal()
	-- When we can't download the thing, it throws
	requests.known_repositories_all = {repo_fake("test1", "http://example.org/test1", false, utils.exception("unreachable", "Fake network is down"))}
	local ok, err = pcall(postprocess.get_repos)
	assert_false(ok)
	assert_table_equal(multierror, err)
end

function test_get_repos_broken_nonfatal()
	requests.known_repositories_all = {repo_fake("test1", "http://example.org/test1", false, utils.exception("unreachable", "Fake network is down"))}
	requests.known_repositories_all[1].ignore = {"missing"}
	assert_table_equal(multierror, postprocess.get_repos())
	assert_table_equal({
		{
			content = {
				[""] = sub_err
			},
			ignore = {"missing"},
			name = "test1",
			repo_uri = "http://example.org/test1",
			tp = "parsed-repository"
		}
	}, requests.known_repositories_all)
end

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
-- Default values for package modifiers to be added if they are not mentioned
local modifier_def = {
	tp = "package",
	abi_change = {},
	abi_change_deep = {},
	order_after = {},
	order_before = {},
	post_install = {},
	post_remove = {},
	pre_install = {},
	pre_remove = {},
	reboot = false,
	replan = false
}

-- Common tasks used in pkg_merge tests for building data structures
local function common_pkg_merge(exp)
	-- Add repo field
	for _, repo in pairs(requests.known_repositories_all) do
		for _, cont in pairs(repo.content) do
			if cont.tp == 'pkg-list' then
				for _, pkg in pairs(cont.list) do
					pkg.repo = repo
				end
			end
		end
	end
	-- Fill in default values for the ones that are not mentioned above
	for _, pkg in pairs(exp) do
		for name, def in pairs(modifier_def) do
			if pkg.modifier[name] == nil then
				pkg.modifier[name] = def
			end
		end
	end
end

225 226 227 228 229 230 231
function test_pkg_merge()
	requests.known_repositories_all = {
		{
			content = {
				[""] = {
					tp = 'pkg-list',
					list = {
232
						xyz = {Package = "xyz", Version = "1"},
233
						abc = {Package = "abc", Version = "2", Depends = "cde", Conflicts = "xyz"},
234 235
						cde = {Package = "cde", Version = "1"},
						fgh = {Package = "fgh", Version = "1", Provides = "cde"}
236 237 238 239 240 241 242 243 244
					}
				}
			}
		},
		{
			content = {
				a = {
					tp = 'pkg-list',
					list = {
245
						abc = {Package = "abc", Version = "1"}
246 247 248 249 250
					}
				},
				b = {
					tp = 'pkg-list',
					list = {
251
						another = {Package = "another", Version = "4"}
252 253 254 255 256 257 258 259 260
					}
				},
				c = utils.exception("Just an exception", "Just an exception")
			}
		}
	}
	requests.known_packages = {
		{
			tp = 'package',
261
			order_after = "abc",
262 263
			name = 'xyz',
			reboot = 'finished',
264
			abi_change = {'another'},
265 266 267 268 269
			deps = "abc"
		},
		{
			tp = 'package',
			name = 'xyz',
270
			replan = true,
271
			abi_change = true,
272 273 274 275 276 277 278 279 280 281 282 283
			deps = {"another", "xyz"}
		},
		{
			tp = 'package',
			name = 'virt',
			virtual = true,
			deps = {"xyz", "abc"}
		}
	}
	-- Build the expected data structure
	local exp = {
		abc = {
284
			candidates = {
285 286 287 288
				{Package = "abc", Depends = "cde", Conflicts = "xyz", Version = "2", deps = {
						tp = "dep-and", sub = { "cde", { tp = "dep-not", sub = {
							{ tp = "dep-package", name = "xyz", version = "~.*" }
					}}}}, repo = requests.known_repositories_all[1]},
289
				{Package = "abc", Version = "1", repo = requests.known_repositories_all[2]}
290
			},
291 292
			modifier = {name = "abc"}
		},
293
		cde = {
294 295 296 297
			candidates = {
				{Package = "cde", Version = "1", repo = requests.known_repositories_all[1]},
				{Package = "fgh", Version = "1", Provides = "cde", repo = requests.known_repositories_all[1]}
			},
298 299
			modifier = {name = "cde"}
		},
300 301 302 303
		fgh = {
			candidates = {{Package = "fgh", Version = "1", Provides = "cde", repo = requests.known_repositories_all[1]}},
			modifier = {name = "fgh"}
		},
304
		another = {
305
			candidates = {{Package = "another", Version = "4", repo = requests.known_repositories_all[2]}},
306 307 308
			modifier = {name = "another"}
		},
		virt = {
309 310 311 312 313 314
			candidates = {},
			modifier = {
				name = "virt",
				deps = {tp = "dep-and", sub = {"xyz", "abc"}},
				virtual = true
			},
315 316
		},
		xyz = {
317
			candidates = {{Package = "xyz", Version = "1", repo = requests.known_repositories_all[1]}},
318 319
			modifier = {
				name = "xyz",
320
				order_after = {abc = true},
321 322 323 324
				deps = {
					tp = 'dep-and',
					sub = {"abc", "another", "xyz"}
				},
325
				reboot = "finished",
326
				abi_change = {[true] = true, ['another'] = true},
327
				replan = "immediate"
328 329 330
			}
		}
	}
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
	common_pkg_merge(exp)
	postprocess.pkg_aggregate()
	assert_table_equal(exp, postprocess.available_packages)
end

function test_pkg_merge_virtual()
	requests.known_repositories_all = {
		{
			content = {
				[""] = {
					tp = 'pkg-list',
					list = {
						pkg = {Package = "pkg", Version = "1", Provides="virt"}
					}
				}
			}
		}
348
	}
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
	requests.known_packages = {
		{
			tp = 'package',
			name = 'virt',
			virtual = true
		}
	}
	-- Build the expected data structure
	local exp = {
		pkg = {
			candidates = {{Package = "pkg", Version = "1", Provides="virt", repo = requests.known_repositories_all[1]}},
			modifier = {name = "pkg"}
		},
		virt = {
			candidates = {{Package = "pkg", Version = "1", Provides="virt", repo = requests.known_repositories_all[1]}},
			modifier = {
				name = "virt",
				virtual = true
			},
		}
	}
	common_pkg_merge(exp)
	postprocess.pkg_aggregate()
372 373 374
	assert_table_equal(exp, postprocess.available_packages)
end

375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
function test_pkg_merge_virtual_with_candidate()
	requests.known_repositories_all = {
		{
			content = {
				[""] = {
					tp = 'pkg-list',
					list = {
						pkg = {Package = "pkg", Version = "1", Provides="virt"},
						virt = {Package = "virt", Version = "2"}
					}
				}
			}
		}
	}
	requests.known_packages = {
		{
			tp = 'package',
			name = 'virt',
			virtual = true
		}
	}
	common_pkg_merge({})
	assert_exception(function() postprocess.pkg_aggregate() end, "inconsistent")
end

400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
--[[
Test we handle when a package has a candidate from a repository and from local content.
The local one should be preferred.
]]
function test_local_and_repo()
	requests.known_repositories_all = {
		{
			content = {
				[""] = {
					tp = 'pkg-list',
					list = {
						xyz = {Package = "xyz", Version = "2"},
					}
				}
			},
			priority = 50,
			name = 'repo1',
			sequence = 1
		}
	}
	requests.known_repositories_all[1].content[""].list.xyz.repo = requests.known_repositories_all[1]
	requests.known_packages = {
		xyz = {
			tp = 'package',
			name = 'xyz',
425
			priority = 60,
426 427 428 429
			candidate = {Package = "xyz", Version = "1", local_mark = true}, -- Mark the package so we recognize it got through unmodified
			content = "dummy-content"
		}
	}
430
	requests.known_packages.xyz.candidate.pkg = requests.known_packages.xyz
431 432 433 434
	postprocess.pkg_aggregate()
	local exp = {
		xyz = {
			candidates = {
435
				{Package = "xyz", Version = "1", local_mark = true, pkg = requests.known_packages.xyz},
436 437 438 439 440 441 442 443 444 445 446 447 448
				{Package = "xyz", Version = "2", repo = requests.known_repositories_all[1]}
			},
			modifier = {
				name = "xyz",
				abi_change = {},
				abi_change_deep = {},
				order_after = {},
				order_before = {},
				post_install = {},
				post_remove = {},
				pre_install = {},
				pre_remove = {},
				reboot = false,
449
				replan = false,
450 451 452 453 454
				tp = "package"
			}
		}
	}
	assert_table_equal(exp, postprocess.available_packages)
455 456 457 458 459 460
	-- Try again, but with the same priority ‒ versions should be used to sort them
	postprocess.available_packages = {}
	requests.known_packages.xyz.priority = nil
	exp.xyz.candidates = {exp.xyz.candidates[2], exp.xyz.candidates[1]}
	postprocess.pkg_aggregate()
	assert_table_equal(exp, postprocess.available_packages)
461 462
end

463 464 465 466 467 468 469
function test_deps_canon()
	assert_equal(nil, postprocess.deps_canon(nil))
	assert_equal(nil, postprocess.deps_canon({}))
	assert_equal(nil, postprocess.deps_canon(""))
	assert_equal("x", postprocess.deps_canon("x"))
	assert_equal("x", postprocess.deps_canon(" x "))
	assert_equal("x", postprocess.deps_canon({"x"}))
470
	assert_equal("x", postprocess.deps_canon({"x", ""}))
471 472
	assert_equal("x", postprocess.deps_canon({tp = 'dep-and', sub = {"x"}}))
	assert_equal("x", postprocess.deps_canon({tp = 'dep-or', sub = {"x"}}))
473 474 475
	assert_equal("x", postprocess.deps_canon("x ( )"))
	assert_table_equal({tp = "dep-package", name = "x", version = ">1.25"}, postprocess.deps_canon("x (>1.25)"))
	assert_table_equal({tp = "dep-package", name = "x", version = ">v_12"}, postprocess.deps_canon("x ( >v_12)"))
476
	assert_table_equal({tp = "dep-not", sub = {"x"}}, postprocess.deps_canon({tp = 'dep-not', sub = {"x"}}))
477 478
	assert_table_equal({tp = "dep-and", sub = {"x", "y"}}, postprocess.deps_canon("x, y"))
	assert_table_equal({tp = "dep-and", sub = {"x", "y"}}, postprocess.deps_canon({"x, y"}))
479 480 481 482 483 484 485
	assert_table_equal({tp = "dep-and", sub = {"x", "y"}}, postprocess.deps_canon({"x", "y"}))
	assert_table_equal({tp = "dep-and", sub = {"x", "y"}}, postprocess.deps_canon({"x", {"y "}}))
	assert_table_equal({tp = "dep-and", sub = {"x", "y"}}, postprocess.deps_canon({"x", {tp = 'dep-and', sub = {"y "}}}))
	assert_table_equal({tp = "dep-or", sub = {"x", "y"}}, postprocess.deps_canon({tp = "dep-or", sub = {"x", {tp = 'dep-or', sub = {"y"}}}}))
	assert_table_equal({tp = "dep-or", sub = {"x", {tp = "dep-or", sub = {"y", "z"}}}}, postprocess.deps_canon({tp = "dep-or", sub = {"x", {tp = 'dep-or', sub = {"y ", "z"}}}}))
	assert_table_equal({tp = "dep-or", sub = {"x", {tp = "dep-and", sub = {"y", "z"}}}}, postprocess.deps_canon({tp = "dep-or", sub = {"x", {"y", "z"}}}))
	assert_table_equal({tp = "package", a = "b"}, postprocess.deps_canon({tp = "package", a = "b"}))
486
	assert_table_equal({tp = "dep-package", a = "b"}, postprocess.deps_canon({tp = "dep-package", a = "b"}))
487 488
end

489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
function test_conflicts_canon()
	assert_equal(nil, postprocess.conflicts_canon(nil))
	assert_equal(nil, postprocess.conflicts_canon(""))
	assert_equal(nil, postprocess.conflicts_canon(" "))
	assert_table_equal({ tp = "dep-not", sub = {
			{ tp = "dep-package", name = "x", version = "~.*" }
		}}, postprocess.conflicts_canon("x"))
	assert_table_equal({ tp = "dep-not", sub = {
			{ tp = "dep-package", name = "x", version = "~.*" }
		}}, postprocess.conflicts_canon(" x "))
	assert_table_equal({ tp = "dep-not", sub = {{ tp = "dep-or", sub = {
			{ tp = "dep-package", name = "x", version = "~.*" },
			{ tp = "dep-package", name = "y", version = "~.*" }
		}}}}, postprocess.conflicts_canon("x, y"))
	assert_table_equal({ tp = "dep-not", sub = {{ tp = "dep-or", sub = {
			{ tp = "dep-package", name = "x", version = ">1" },
			{ tp = "dep-package", name = "y", version = "~.*" }
		}}}}, postprocess.conflicts_canon("x (>1), y"))
end

509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
function test_sort_candidates()
	local candidates = {
		{ Package = "pkg", Version = "3", repo = { name = "main", priority = 50 } },
		{ Package = "nopkg", Version = "1", repo = { name = "main", priority = 50 } },
		{ Package = "pkg", Version = "1", repo = { name = "main", priority = 50 } },
		{ Package = "nopkg", Version = "2", repo = { name = "main", priority = 50 } },
		{ Package = "pkg", Version = "2", repo = { name = "main", priority = 50 } },
		{ Package = "pkg", Version = "1", repo = { name = "other", priority = 60 } },
		{ Package = "nopkg", Version = "1", repo = { name = "other", priority = 60 } },
		{ Package = "pkg", Version = "2", repo = { name = "other", priority = 60 } },
	}
	local result = {
		candidates[8],
		candidates[6],
		candidates[7],
		candidates[1],
		candidates[5],
		candidates[3],
		candidates[4],
		candidates[2],
	}
	postprocess.sort_candidates("pkg", candidates)
	assert_table_equal(candidates, result)
end

534 535
function teardown()
	requests.known_repositories_all = {}
536 537
	requests.known_packages = {}
	postprocess.available_packages = {}
538
end