transaction.lua 13.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
--[[
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'
20
local B = require 'backend'
21
local T = require 'transaction'
22
local utils = require 'utils'
23
local journal = require 'journal'
24
local syscnf = require "syscnf"
25 26

syscnf.set_root_dir()
27 28 29

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

30 31
local test_status = {
	["pkg-rem"] = {
32 33 34
		Package = "pkg-rem",
		Conffiles = {
			remconf = "12345678901234567890123456789012"
35 36
		},
		Status = {"install", "user", "installed"}
37 38 39
	},
	["pkg-name"] = {
		Package = "pkg-name",
40
		Canary = true,
41
		Version = "0",
42 43
		Conffiles = {
			c = "12345678901234567890123456789012"
44 45
		},
		Status = {"install", "user", "installed"}
46 47
	}
}
48

49
local intro = {
50
	{
51
		f = "backend.run_state",
52 53
		p = {}
	},
54
	{
55
		f = "journal.fresh",
56 57
		p = {}
	},
58
	{
59
		f = "utils.mkdirp",
60
		p = {"/usr/share/updater/unpacked/"}
61
	},
62 63
}

64 65 66 67 68 69 70 71 72 73 74
local function outro(cleanup_dirs, status)
	return {
		{
			f = "utils.cleanup_dirs",
			p = {cleanup_dirs}
		},
		{
			f = "backend.control_cleanup",
			p = {status}
		},
		{
75
			f = "backend.status_dump",
76
			p = {status}
77 78 79 80 81 82 83 84
		},
		{
			f = "journal.write",
			p = {journal.CLEANED}
		},
		{
			f = "journal.finish",
			p = {}
85 86
		},
		{
87 88
			f = "backend.run_state.release",
			p = {}
89 90 91 92
		}
	}
end

93 94 95 96 97 98 99 100 101 102 103 104
local function tables_join(...)
	local idx = 0
	local result = {}
	for _, param in ipairs({...}) do
		for _, val in ipairs(param) do
			idx = idx + 1
			result[idx] = val
		end
	end
	return result
end

105
local function mocks_install()
106 107 108
	mock_gen("os.remove")
	mock_gen("utils.dir_ensure")
	mock_gen("utils.mkdirp")
109 110 111 112 113 114 115 116 117 118 119 120
	mock_gen("backend.status_parse", function () fail("Forbidden function backend.status_parse") end)
	mock_gen("backend.run_state", function()
		return {
			release = function ()
				table.insert(mocks_called, {
					f = "backend.run_state.release",
					p = {}
				})
			end,
			status = utils.clone(test_status)
		}
	end)
121
	mock_gen("backend.pkg_unpack", function () return "pkg_dir" end)
122
	mock_gen("backend.pkg_examine", function () return {f = true}, {d = true}, {c = "1234567890123456"}, {Package = "pkg-name", files = {f = true}, Conffiles = {c = "1234567890123456"}, Version = "1", Status = {"install", "user", "installed"}} end)
123
	mock_gen("backend.collision_check", function () return {}, {}, {}  end)
124
	mock_gen("backend.pkg_merge_files")
125
	mock_gen("backend.pkg_cleanup_files")
126 127
	mock_gen("backend.control_cleanup")
	mock_gen("backend.pkg_merge_control")
128
	mock_gen("backend.status_dump")
129 130 131 132 133 134 135
	mock_gen("backend.script_run", function (pkgname, suffix)
		if suffix == "postinst" then
			return false, "Fake failed postinst"
		else
			return true, ""
		end
	end)
136 137 138
	mock_gen("backend.pkg_config_info", function (f, configs)
		return f, false
	end)
139
	mock_gen("utils.cleanup_dirs")
140 141 142
	mock_gen("journal.fresh")
	mock_gen("journal.finish")
	mock_gen("journal.write")
143
	mock_gen("locks.acquire", function () fail("Forbidden function locks.acquire") end)
144 145 146 147 148
end

-- Test calling empty transaction
function test_perform_empty()
	mocks_install()
149 150 151
	-- Run empty set of operations
	T.perform({})
	local expected = tables_join(intro, {
152 153
		{
			f = "journal.write",
154
			p = {journal.UNPACKED, {}, {}, {}, {}, {}}
155
		},
156 157 158 159
		{
			f="utils.cleanup_dirs",
			p={ { "/usr/share/updater/download/" } }
		},
160 161 162 163
		{
			f = "backend.collision_check",
			p = {test_status, {}, {}}
		},
164 165
		{
			f = "journal.write",
166
			p = {journal.CHECKED, {}, {}}
167 168 169
		},
		{
			f = "journal.write",
170
			p = {journal.MOVED, test_status, {}, {}}
171
		},
172 173
		{
			f = "backend.pkg_cleanup_files",
174
			p = {{}, {}}
175 176 177 178
		},
		{
			f = "journal.write",
			p = {journal.SCRIPTS, test_status, {}}
179
		}
180
	}, outro({}, test_status))
181 182 183
	assert_table_equal(expected, mocks_called)
end

184 185 186
-- Test a transaction when it goes well
function test_perform_ok()
	mocks_install()
187
	mock_gen("backend.collision_check", function () return {}, {}, {d2 = true}  end)
188
	local result = T.perform({
189 190
		{
			op = "install",
191
			file = "<package>"
192 193 194 195 196
		}, {
			op = "remove",
			name = "pkg-rem"
		}
	})
197 198 199 200 201 202
	-- No collected errors
	assert_table_equal({
		["pkg-name"] = {
			["postinst"] = "Fake failed postinst"
		}
	}, result)
203 204 205 206
	local status_mod = utils.clone(test_status)
	status_mod["pkg-name"] = {
		Package = "pkg-name",
		Conffiles = { c = "1234567890123456" },
207
		Version = "1",
208 209
		files = { f = true },
		Status = {"install", "user", "installed"}
210 211
	}
	status_mod["pkg-rem"] = nil
212 213 214
	local expected = tables_join(intro, {
		{
			f = "backend.pkg_unpack",
215
			p = {"<package>"}
216 217 218 219 220
		},
		{
			f = "backend.pkg_examine",
			p = {"pkg_dir"}
		},
221 222 223 224 225 226 227 228 229 230 231 232 233
		{
			f = "journal.write",
			p = {
				journal.UNPACKED,
				{["pkg-name"] = true, ["pkg-rem"] = true},
				{["pkg-name"] = { f = true }},
				{
					{
						configs = { c = "1234567890123456" },
						control = {
							Conffiles = { c = "1234567890123456" },
							Package = "pkg-name",
							Version = "1",
234 235
							files = { f = true },
							Status = {"install", "user", "installed"}
236 237 238 239
						},
						dir = "pkg_dir",
						dirs = { d = true },
						files = { f = true },
240
						op = "install",
241 242
						old_configs = { c = "12345678901234567890123456789012" },
						reboot_immediate = false
243 244 245
					},
					{ name = "pkg-rem", op = "remove" }
				},
246 247
				{"pkg_dir"},
				{}
248 249
			}
		},
250 251 252 253
		{
			f = "utils.cleanup_dirs",
			p = {{syscnf.pkg_download_dir}}
		},
254 255 256 257 258 259 260 261 262 263 264
		{
			f = "backend.collision_check",
			p = {
				test_status,
				{
					["pkg-rem"] = true,
					["pkg-name"] = true
				},
				{["pkg-name"] = {f = true}}
			}
		},
265 266
		{
			f = "journal.write",
267
			p = {journal.CHECKED, {["d2"] = true}, { }}
268
		},
269 270 271 272 273 274 275 276
		{
			f = "backend.pkg_merge_control",
			p = {"pkg_dir/control", "pkg-name", {f = true}}
		},
		{
			f = "backend.script_run",
			p = {"pkg-name", "preinst", "upgrade", "0"}
		},
277 278
		{
			f = "backend.pkg_merge_files",
279
			p = {"pkg_dir/data", {d = true}, {f = true}, {c = "12345678901234567890123456789012"}}
280
		},
281 282 283 284 285 286 287 288 289
		{
			f = "journal.write",
			p = {
				journal.MOVED,
				{
					["pkg-name"] = {
						Conffiles = { c = "1234567890123456" },
						Package = "pkg-name",
						Version = "1",
290 291
						files = { f = true },
						Status = {"install", "user", "installed"}
292 293
					},
					["pkg-rem"] = {
294
						Package = "pkg-rem",
295 296
						Conffiles = { remconf = "12345678901234567890123456789012" },
						Status = {"install", "user", "installed"}
297 298
					}
				},
299 300
				{},
				{c = "12345678901234567890123456789012"}
301 302
			}
		},
303
		{
304 305 306
			f = "backend.script_run",
			p = {"pkg-name", "postinst", "configure"}
		},
307 308 309 310
		{
			f = "backend.pkg_config_info",
			p = {"remconf", { remconf = "12345678901234567890123456789012" } }
		},
311 312 313
		{
			f = "backend.script_run",
			p = {"pkg-rem", "prerm", "remove"}
314 315
		},
		{
316
			f = "backend.pkg_cleanup_files",
317
			p = {{d2 = true}, {c = "12345678901234567890123456789012", remconf = "12345678901234567890123456789012"}}
318 319 320 321
		},
		{
			f = "backend.script_run",
			p = {"pkg-rem", "postrm", "remove"}
322 323 324 325 326 327 328 329 330 331
		},
		{
			f = "journal.write",
			p = {
				journal.SCRIPTS,
				{
					["pkg-name"] = {
						Conffiles = { c = "1234567890123456" },
						Package = "pkg-name",
						Version = "1",
332 333
						files = { f = true },
						Status = {"install", "user", "installed"}
334 335 336 337
					}
				},
				{ ["pkg-name"] = { ["postinst"] = "Fake failed postinst" } }
			}
338
		}
339
	}, outro({"pkg_dir"}, status_mod))
340 341 342
	assert_table_equal(expected, mocks_called)
end

343
-- Test if it stops when it finds collisions
344 345 346
function test_perform_collision()
	mocks_install()
	mock_gen("backend.collision_check", function () return {f = {["<pkg1name>"] = "new", ["<pkg2name>"] = "new", ["other"] = "existing"}}, {} end)
347
	mock_gen("backend.pkg_unpack", function (data) return data:gsub("file", "dir") end)
348
	mock_gen("backend.pkg_examine", function (dir) return {f = true}, {d = true}, {c = "1234567890123456"}, {Package = dir:gsub("dir", "name")} end)
349
	local ok, err = pcall(T.perform, {
350 351
		{
			op = "install",
352
			file = "<pkg1file>"
353 354 355
		},
		{
			op = "install",
356
			file = "<pkg2file>"
357
		}
358 359 360 361 362
	})
	assert_false(ok)
	-- We can't really check for equality, because the order is not guaranteed. So we check for some snippets
	assert(err:match("Collisions"))
	assert(err:match("f: .*other %(existing%)"))
363 364 365
	local expected = tables_join(intro, {
		{
			f = "backend.pkg_unpack",
366
			p = {"<pkg1file>"}
367 368 369 370 371 372 373
		},
		{
			f = "backend.pkg_examine",
			p = {"<pkg1dir>"}
		},
		{
			f = "backend.pkg_unpack",
374
			p = {"<pkg2file>"}
375 376 377 378 379
		},
		{
			f = "backend.pkg_examine",
			p = {"<pkg2dir>"}
		},
380 381 382 383 384 385 386 387 388 389 390 391 392
		{
			f = "journal.write",
			p = {
				journal.UNPACKED,
				{["<pkg1name>"] = true, ["<pkg2name>"] = true},
				{["<pkg1name>"] = { f = true }, ["<pkg2name>"] = { f = true }},
				{
					{
						configs = { c = "1234567890123456" },
						control = { Package = "<pkg1name>" },
						dir = "<pkg1dir>",
						dirs = { d = true },
						files = { f = true },
393
						op = "install",
394 395
						old_configs = { c = "1234567890123456" },
						reboot_immediate = false
396 397 398 399 400 401 402
					},
					{
						configs = { c = "1234567890123456" },
						control = { Package = "<pkg2name>" },
						dir = "<pkg2dir>",
						dirs = { d = true },
						files = { f = true },
403
						op = "install",
404 405
						old_configs = { c = "1234567890123456" },
						reboot_immediate = false
406 407
					}
				},
408 409
				{"<pkg1dir>", "<pkg2dir>"},
				{}
410 411
			}
		},
412 413 414 415
		{
			f = "utils.cleanup_dirs",
			p = {{syscnf.pkg_download_dir}}
		},
416 417 418 419 420 421 422 423 424 425 426 427 428
		{
			f = "backend.collision_check",
			p = {
				test_status,
				{
					["<pkg1name>"] = true,
					["<pkg2name>"] = true
				},
				{
					["<pkg1name>"] = {f = true},
					["<pkg2name>"] = {f = true}
				}
			}
429 430 431 432
		},
		{
			f = "utils.cleanup_dirs",
			p = {{"<pkg1dir>", "<pkg2dir>"}}
433 434 435 436
		},
		{
			f = "journal.finish",
			p = {}
437 438 439 440 441
		}
	})
	assert_table_equal(expected, mocks_called)
end

442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
--[[
Test the journal recovery.

In this case, the journal contains nothing useful, because it's been
interrupted before even unpacking the packages.

Therefore, the recovery actioun would actually only close the journal
properly, since there's been nothing to recover.
]]
function test_recover_early()
	mocks_install()
	-- The journal contains only the start, not even unpack, therefore there's nothing to restore
	mock_gen("journal.recover", function ()
		return {
			{ type = journal.START, params = {} }
		}
	end)
	assert_table_equal({
		["*"] = {
			transaction = "Transaction in the journal hasn't started yet, nothing to resume"
		}
	}, transaction.recover())
	assert_table_equal({
465
		{ f = "backend.run_state", p = {} },
466
		{ f = "journal.recover", p = {} },
467
		{ f = "journal.finish", p = {} },
468
		{ f = "backend.run_state.release", p = {} }
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
	}, mocks_called)
end

--[[
Test the journal recovery.

The transaction is almost complete, so nothing from the middle shall be called. Only
the missing cleanup is run.
]]
function test_recover_late()
	mocks_install()
	mock_gen("journal.recover", function ()
		return {
			{ type = journal.START, params = {} },
			{ type = journal.UNPACKED, params = {
				{["pkg-name"] = true, ["pkg-rem"] = true},
				{["pkg-name"] = { f = true } },
				{
					{
						configs = { c = "1234567890123456" },
						control = {
							Conffiles = { c = "1234567890123456" },
							Package = "pkg-name",
							Version = "1",
493 494
							files = { f = true },
							Status = {"install", "user", "instatalled"}
495 496 497 498 499 500 501 502
						},
						dir = "pkg_dir",
						dirs = { d = true },
						files = { f = true },
						op = "install"
					},
					{ name = "pkg-rem", op = "remove" }
				},
503 504
				{"pkg_dir"},
				{}
505 506 507 508 509 510 511 512
			} },
			{ type = journal.CHECKED, params = { {["d2"] = true} } },
			{ type = journal.MOVED, params = {
				{
					["pkg-name"] = {
						Conffiles = { c = "1234567890123456" },
						Package = "pkg-name",
						Version = "1",
513 514
						files = { f = true },
						Status = {"install", "user", "installed"}
515 516 517 518 519 520 521 522 523 524 525 526 527
					},
					["pkg-rem"] = {
						Package = "pkg-rem"
					}
				},
				{}
			} },
			{ type = journal.SCRIPTS, params = {
				{
					["pkg-name"] = {
						Conffiles = { c = "1234567890123456" },
						Package = "pkg-name",
						Version = "1",
528 529
						files = { f = true },
						Status = {"install", "user", "installed"}
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
					}
				},
				{ ["pkg-name"] = { ["postinst"] = "Fake failed postinst" } }
			} }
		}
	end)
	assert_table_equal({
		["pkg-name"] = {
			["postinst"] = "Fake failed postinst"
		}
	}, transaction.recover())
	local status_mod = utils.clone(test_status)
	status_mod["pkg-name"] = {
		Package = "pkg-name",
		Conffiles = { c = "1234567890123456" },
		Version = "1",
546 547
		files = { f = true },
		Status = {"install", "user", "installed"}
548 549 550
	}
	status_mod["pkg-rem"] = nil
	local intro_mod = utils.clone(intro)
551
	intro_mod[2].f = "journal.recover"
552
	intro_mod[4] = {f = "utils.cleanup_dirs", p = {{syscnf.pkg_download_dir}}}
553 554 555 556
	local expected = tables_join(intro_mod, outro({"pkg_dir"}, status_mod))
	assert_table_equal(expected, mocks_called)
end

557
function teardown()
558
	-- A trick to clean up the queue
559
	mock_gen('transaction.perform', function () return {} end)
560
	transaction.perform_queue()
561 562
	mocks_reset()
end