planner.lua 26.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
--[[
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'

22
local planner = require 'planner'
23
local requests = require 'requests'
24
local utils = require "utils"
25

26
module("planner-tests", package.seeall, lunit.testcase)
27

28 29
local def_repo = {priority = 50, serial = 1}

30 31 32 33 34 35 36
--[[
Test installation plan generation when there are no
dependencies.
]]
function test_no_deps()
	local pkgs = {
		pkg1 = {
37
			candidates = {{Package = 'pkg1', repo = def_repo}},
38
			modifier = {}
39 40
		},
		pkg2 = {
41
			candidates = {{Package = 'pkg2', repo = def_repo}},
42
			modifier = {}
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
		}
	}
	local requests = {
		{
			tp = 'install',
			package = {
				tp = 'package',
				name = 'pkg1',
			}
		},
		{
			tp = 'install',
			package = {
				tp = 'package',
				name = 'pkg2'
			}
		}
	}
61
	local result = planner.required_pkgs(pkgs, requests)
62
	local expected = {
63
		pkg1 = {
64
			action = "require",
65
			package = {Package = 'pkg1', repo = def_repo},
66
			modifier = {},
67
			name = "pkg1"
68
		},
69
		pkg2 = {
70
			action = "require",
71
			package = {Package = 'pkg2', repo = def_repo},
72
			modifier = {},
73
			name = "pkg2"
74 75
		}
	}
76
	assert_plan_dep_order(expected, result)
77 78
end

79 80 81
function test_reinstall()
	local pkgs = {
		pkg1 = {
82
			candidates = {{Package = 'pkg1', repo = def_repo}},
83
			modifier = {}
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
		}
	}
	local requests = {
		{
			tp = 'install',
			package = {
				tp = 'package',
				name = 'pkg1'
			},
			reinstall = true
		}
	}
	local result = planner.required_pkgs(pkgs, requests)
	local expected = {
		{
			action = "reinstall",
100
			package = {Package = 'pkg1', repo = def_repo},
101
			modifier = {},
102 103 104 105 106 107
			name = 'pkg1'
		}
	}
	assert_table_equal(expected, result)
end

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
-- Test the reinstall flag works even when we „require“ the package first (this was broken before)
function test_reinstall_upgrade()
	local pkgs = {
		pkg1 = {
			candidates = {{Package = 'pkg1', repo = def_repo}},
			modifier = {}
		},
		pkg2 = {
			candidates = {{Package = 'pkg2', repo = def_repo}},
			modifier = {}
		}
	}
	local requests = {
		{
			-- First ask for it to get installed. That one schedules it as „require"
			tp = 'install',
			package = {
				tp = 'package',
				name = 'pkg1'
			}
		},
		{
			-- Just a package in the middle, so we are sure the following reschedule doesn't reorder things.
			tp = 'install',
			package = {
				tp = 'package',
				name = 'pkg2'
			}
		},
		{
			-- Second instance with reinstall. That one should reschedule it as „reinstall“
			tp = 'install',
			package = {
				tp = 'package',
				name = 'pkg1'
			},
			reinstall = true
		}
	}
	local result = planner.required_pkgs(pkgs, requests)
	local expected = {
149
		pkg1 = {
150 151 152 153 154
			action = "reinstall",
			package = {Package = 'pkg1', repo = def_repo},
			modifier = {},
			name = 'pkg1'
		},
155
		pkg2 = {
156 157 158 159 160 161
			action = "require",
			package = {Package = 'pkg2', repo = def_repo},
			modifier = {},
			name = 'pkg2'
		}
	}
162 163 164 165 166
	-- We don't care about order, just compare that expected packages are there in expected form
	assert_table_equal(expected, utils.map(result, function(_, v)
				return v.name, v
			end
		))
167 168
end

169 170 171 172 173 174 175 176 177
--[[
Find some deps. Some are from the modifier, some from the candidate.
There may be multiple candidates. Also, check each dep is brought in
just once.
]]
function test_deps()
	local pkgs = {
		dep1 = {
			candidates = {
178 179
				{Package = 'dep1', Version = "2", repo = def_repo},
				{Package = 'dep1', Version = "1", repo = def_repo}
180
			},
181
			modifier = {}
182 183
		},
		dep2 = {
184
			candidates = {{Package = 'dep2', repo = def_repo}},
185
			modifier = {}
186 187
		},
		dep3 = {
188
			candidates = {{Package = 'dep3', repo = def_repo}},
189
			modifier = {
190
				deps = "dep1"
191 192 193
			}
		},
		unused = {
194
			candidates = {{Package = 'unused', repo = def_repo}},
195
			modifier = {
196
				deps = "dep1"
197 198 199
			}
		},
		pkg1 = {
200
			candidates = {{Package = 'pkg1', repo = def_repo}},
201
			modifier = {
202
				deps = "dep1"
203 204 205
			}
		},
		pkg2 = {
206
			candidates = {{Package = 'pkg2', deps = {tp = 'dep-and', sub = {'dep2', 'dep3'}}, repo = def_repo}},
207
			modifier = {}
208 209 210 211 212 213 214 215 216 217 218 219 220 221
		}
	}
	local requests = {
		{
			tp = 'install',
			package = {
				tp = 'package',
				name = 'pkg1',
			}
		},
		{
			tp = 'install',
			package = {
				tp = 'package',
222
				name = 'pkg2',
223 224 225
			}
		}
	}
226
	local result = planner.required_pkgs(pkgs, requests)
227
	local expected = {
228 229
		dep1 = {
			action = 'require',
230
			package = {Package = 'dep1', Version = "2", repo = def_repo},
231
			modifier = {},
232
			name = 'dep1'
233
		},
234 235
		dep2 = {
			action = 'require',
236
			package = {Package = 'dep2', repo = def_repo},
237
			modifier = {},
238
			name = 'dep2'
239
		},
240 241
		dep3 = {
			action = 'require',
242
			package = {Package = 'dep3', repo = def_repo},
243
			modifier = {
244
				deps = "dep1"
245
			},
246
			name = 'dep3'
247
		},
248 249
		pkg1 = {
			action = 'require',
250
			package = {Package = 'pkg1', repo = def_repo},
251 252 253 254 255 256 257
			modifier = {
				deps = "dep1"
			},
			name = 'pkg1'
		},
		pkg2 = {
			action = 'require',
258
			package = {Package = 'pkg2', deps = {tp = 'dep-and', sub = {'dep2', 'dep3'}}, repo = def_repo},
259
			modifier = {},
260
			name = 'pkg2'
261 262
		}
	}
263
	assert_plan_dep_order(expected, result)
264
end
265 266 267 268 269 270 271

--[[
A dependency doesn't exist. It should fail.
]]
function test_missing_dep()
	local pkgs = {
		pkg = {
272
			candidates = {{Package = 'pkg', deps = 'nothere', repo = def_repo}},
273
			modifier = {}
274 275 276 277 278 279 280 281 282 283 284
		}
	}
	local requests = {
		{
			tp = 'install',
			package = {
				tp = 'package',
				name = 'pkg',
			}
		}
	}
285
	assert_exception(function () planner.required_pkgs(pkgs, requests) end, 'inconsistent')
286 287
end

288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
function test_virtual()
	local pkgs = {
		virt1 = {
			modifier = {
				virtual = true,
				deps = "pkg"
			}
		},
		virt2 = {
			modifier = {
				virtual = true
			}
		},
		pkg = {
			candidates = {{Package = 'pkg', Version = "1", deps = 'virt2', repo = def_repo}},
			modifier = {}
		}
	}
	local requests = {
		{
			tp = 'install',
			package = {
				tp = 'package',
				name = 'virt1',
			}
		}
	}
	local result = planner.required_pkgs(pkgs, requests)
	local expected = {
		pkg = {
			action = 'require',
			package = {Package = 'pkg', Version = "1", deps = "virt2", repo = def_repo},
			modifier = {},
			name = 'pkg'
		}
	}
	assert_plan_dep_order(expected, result)
end

function test_virtual_version()
	local pkgs = {
		virt = {
			modifier = {
				virtual = true,
				deps = "pkg"
			}
		}
	}
	local requests = {
		{
			tp = 'install',
			package = {
				tp = 'package',
				name = 'virt',
				version = '1'
			}
		}
	}
	assert_exception(function () planner.required_pkgs(pkgs, requests) end, 'inconsistent')
end

349
-- It is able to solve a circular dependency and doesn't stack overflow
350 351 352
function test_circular_deps()
	local pkgs = {
		pkg1 = {
353
			candidates = {{Package = 'pkg1', deps = 'pkg2', repo = def_repo}},
354
			modifier = {}
355 356
		},
		pkg2 = {
357
			candidates = {{Package = 'pkg2', repo = def_repo}},
358
			modifier = {
359
				deps = "pkg1"
360 361 362 363 364 365 366 367 368 369 370 371
			}
		}
	}
	local requests = {
		{
			tp = 'install',
			package = {
				tp = 'package',
				name = 'pkg1'
			}
		}
	}
372 373 374 375
	local result = planner.required_pkgs(pkgs, requests)
	local expected = {
		pkg1 = {
			action = 'require',
376
			package = {Package = 'pkg1', deps = 'pkg2', repo = def_repo},
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
			modifier = {},
			name = 'pkg1'
		},
		pkg2 = {
			action = 'require',
			package = {Package = 'pkg2', repo = def_repo},
			modifier = {
				deps = "pkg1"
			},
			name = 'pkg2'
		}
	}
	-- We don't care about order, just compare that expected packages are there in expected form
	assert_table_equal(expected, utils.map(result, function(_, v)
				return v.name, v
			end
		))
end

-- It is able to detect a circular dependency for critival package
function test_circular_deps_critical()
	local pkgs = {
		critic = {
400
			candidates = {{Package = 'critic', deps = 'pkg', repo = def_repo}},
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
			modifier = { }
		},
		pkg = {
			candidates = {{Package = 'pkg', repo = def_repo}},
			modifier = {
				deps = "critic"
			}
		}
	}
	local requests = {
		{
			tp = 'install',
			package = {
				tp = 'package',
				name = 'critic'
			},
			critical = true
		}
	}
420
	assert_exception(function () planner.required_pkgs(pkgs, requests) end, 'inconsistent', nil, { critical = true })
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
end

function test_priority()
	local pkgs = {
		pkg1 = {
			candidates = {{Package = 'pkg1', deps = {}, repo = def_repo}},
			modifier = {}
		},
		pkg2 = {
			candidates = {{Package = 'pkg2', repo = def_repo}},
			modifier = {}
		}
	}
	local requests = {
		{
			tp = 'install',
			package = {
				tp = 'package',
				name = 'pkg1',
			},
			priority = 70
		},
		{
			tp = 'install',
			package = {
				tp = 'package',
				name = 'pkg1',
			},
			reinstall = true
		},
		{
			tp = 'uninstall',
			package = {
				tp = 'package',
				name = 'pkg1',
			},
			priority = 40
		},
		{
			tp = 'uninstall',
			package = {
				tp = 'package',
				name = 'pkg2'
			}
		},
		{
			tp = 'install',
			package = {
				tp = 'package',
				name = 'pkg2'
			},
			priority = 20
		}
	}
	local result = planner.required_pkgs(pkgs, requests)
	local expected = {
		pkg1 = {
			action = "reinstall",
			package = {Package = 'pkg1', deps = {}, repo = def_repo},
			modifier = {},
			name = "pkg1"
		}
	}
	assert_plan_dep_order(expected, result)
end

function test_request_collision()
	local pkgs = {
		pkg1 = {
			candidates = {{Package = 'pkg1', deps = {}, repo = def_repo}},
			modifier = {}
		}
	}
	local requests = {
		{
			tp = 'install',
			package = {
				tp = 'package',
				name = 'pkg1',
			}
		},
		{
			tp = 'uninstall',
			package = {
				tp = 'package',
				name = 'pkg1',
			}
		}
	}
	assert_exception(function() planner.required_pkgs(pkgs, requests) end, 'invalid-request')
511
end
512

Karel Koci's avatar
Karel Koci committed
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
function test_penalty()
	local pkgs = {
		pkg = {
			candidates = {{Package = 'pkg', deps = {}, repo = def_repo}},
			modifier = {
				deps = {
					tp = "dep-or",
					sub = {"dep1", "dep2", "dep3", "dep4"}
				}
			}
		},
		dep1 = {
			candidates = {{Package = 'dep1', deps = {}, repo = def_repo}},
			modifier = {}
		},
		dep2 = {
			candidates = {{Package = 'dep2', deps = {}, repo = def_repo}},
			modifier = {}
		},
		dep3 = {
			candidates = {{Package = 'dep3', deps = {}, repo = def_repo}},
			modifier = {}
		},
		dep4 = {
			candidates = {{Package = 'dep4', deps = {}, repo = def_repo}},
			modifier = {}
		}
	}
	local requests = {
		{
			tp = 'install',
			package = {
				tp = 'package',
				name = 'pkg',
			}
		}
	}
	local expected = {
		pkg = {
			action = "require",
			package = {Package = 'pkg', deps = {}, repo = def_repo},
			modifier = {
				deps = {
					tp = "dep-or",
					sub = {"dep1", "dep2", "dep3", "dep4"}
				}
			},
			name = "pkg"
		},
		dep1 = {
			action = "require",
			package = {Package = 'dep1', deps = {}, repo = def_repo},
			modifier = {},
			name = "dep1"
		}
	}
	local result = planner.required_pkgs(pkgs, requests)
	assert_plan_dep_order(expected, result)

	table.insert(requests, {
		tp = 'install',
		package = {
			tp = 'package',
			name = 'dep3'
		}
	})
	expected['dep1'] = nil
	expected['dep3'] = {
		action = "require",
		package = {Package = 'dep3', deps = {}, repo = def_repo},
		modifier = {},
		name = 'dep3'
	}
	result = planner.required_pkgs(pkgs, requests)
	assert_plan_dep_order(expected, result)
end

590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
-- Check that we chose common preference
function test_penalty_most_common()
	local pkgs = {
		pkg1 = {
			candidates = {{Package = 'pkg1', deps = {}, repo = def_repo}},
			modifier = {
				deps = {
					tp = "dep-or",
					sub = {"dep1", "dep2"}
				}
			}
		},
		pkg2 = {
			candidates = {{Package = 'pkg2', deps = {}, repo = def_repo}},
			modifier = {
				deps = {
					tp = "dep-or",
					sub = {"dep2", "dep1"}
				}
			}
		},
		pkg3 = {
			candidates = {{Package = 'pkg3', deps = {}, repo = def_repo}},
			modifier = {
				deps = {
					tp = "dep-or",
					sub = {"dep1", "dep2"}
				}
			}
		},
		dep1 = {
			candidates = {{Package = 'dep1', deps = {}, repo = def_repo}},
			modifier = {}
		},
		dep2 = {
			candidates = {{Package = 'dep2', deps = {}, repo = def_repo}},
			modifier = {}
		}
	}
	local requests = {
		{
			tp = 'install',
			package = {
				tp = 'package',
				name = 'pkg1',
			}
		},
		{
			tp = 'install',
			package = {
				tp = 'package',
				name = 'pkg2',
			}
		},
		{
			tp = 'install',
			package = {
				tp = 'package',
				name = 'pkg3',
			}
		}
	}
	local expected = {
		pkg1 = {
			action = "require",
			package = {Package = 'pkg1', deps = {}, repo = def_repo},
			modifier = {
				deps = {
					tp = "dep-or",
					sub = {"dep1", "dep2"}
				}
			},
			name = "pkg1"
		},
		pkg2 = {
			action = "require",
			package = {Package = 'pkg2', deps = {}, repo = def_repo},
			modifier = {
				deps = {
					tp = "dep-or",
					sub = {"dep2", "dep1"}
				}
			},
			name = "pkg2"
		},
		pkg3 = {
			action = "require",
			package = {Package = 'pkg3', deps = {}, repo = def_repo},
			modifier = {
				deps = {
					tp = "dep-or",
					sub = {"dep1", "dep2"}
				}
			},
			name = "pkg3"
		},
		dep1 = {
			action = "require",
			package = {Package = 'dep1', deps = {}, repo = def_repo},
			modifier = {},
			name = "dep1"
		}
	}
	local result = planner.required_pkgs(pkgs, requests)
	assert_plan_dep_order(expected, result)
end

Karel Koci's avatar
Karel Koci committed
697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767
function test_penalty_and_missing()
	local pkgs = {
		pkg = {
			candidates = {{Package = 'pkg', deps = {}, repo = def_repo}},
			modifier = {
				deps = {
					tp = "dep-or",
					sub = {"dep1", "dep2", "dep3", "dep4"}
				}
			}
		},
		dep1 = {
			candidates = {},
			modifier = {}
		},
		dep2 = {
			candidates = {{Package = 'dep2', deps = {}, repo = def_repo}},
			modifier = {}
		},
		dep3 = {
			candidates = {{Package = 'dep3', deps = {}, repo = def_repo}},
			modifier = {}
		},
		dep4 = {
			candidates = {},
			modifier = {}
		}
	}
	local requests = {
		{
			tp = 'install',
			package = {
				tp = 'package',
				name = 'pkg',
			}
		}
	}
	local expected = {
		pkg = {
			action = "require",
			package = {Package = 'pkg', deps = {}, repo = def_repo},
			modifier = {
				deps = {
					tp = "dep-or",
					sub = {"dep1", "dep2", "dep3", "dep4"}
				}
			},
			name = "pkg"
		},
		dep2 = {
			action = "require",
			package = {Package = 'dep2', deps = {}, repo = def_repo},
			modifier = {},
			name = "dep2"
		}
	}
	local result = planner.required_pkgs(pkgs, requests)
	assert_plan_dep_order(expected, result)

	pkgs.dep2.candidates = {}
	expected.dep2 = nil
	expected.dep3 = {
		action = 'require',
		package = {Package = 'dep3', deps = {}, repo = def_repo},
		modifier = {},
		name = "dep3"
	}
	result = planner.required_pkgs(pkgs, requests)
	assert_plan_dep_order(expected, result)
end

768 769 770 771 772 773 774 775 776 777 778 779
function test_filter_required()
	local status = {
		pkg1 = {
			Version = "1"
		},
		pkg2 = {
			Version = "2"
		},
		pkg3 = {
			Version = "3"
		},
		pkg4 = {
780
			Depends = "pkg5 nonexist",
781
			Version = "4"
782 783 784
		},
		pkg5 = {
			Version = "5"
785 786 787 788 789 790 791 792
		}
	}
	local requests = {
		{
			-- Installed, but requires an upgrade
			action = "require",
			name = "pkg1",
			package = {
793 794
				Version = "2",
				repo = def_repo
795 796
			},
			modifier = {}
797 798 799 800 801 802
		},
		{
			-- Installed in the right version
			action = "require",
			name = "pkg2",
			package = {
803 804
				Version = "2",
				repo = def_repo
805 806
			},
			modifier = {}
807 808 809 810 811 812
		},
		{
			-- Installed, but we explicitly want to reinstall
			action = "reinstall",
			name = "pkg3",
			package = {
813 814
				Version = "3",
				repo = def_repo
815 816
			},
			modifier = {}
817
		},
818
		-- The pkg4 and pkg5 are not mentioned, they shall be uninstalled at the end
819 820 821
		{
			-- Not installed and we want it
			action = "require",
822
			name = "pkg6",
823
			package = {
824
				Version = "6",
825
				repo = def_repo
826 827
			},
			modifier = {}
828 829
		}
	}
830
	local result = planner.filter_required(status, requests)
831 832 833 834 835 836
	local expected = {
		requests[1],
		{
			action = "require",
			name = "pkg3",
			package = {
837 838
				Version = "3",
				repo = def_repo
839 840
			},
			modifier = {}
841 842 843 844
		},
		requests[4],
		{
			action = "remove",
845
			name = "pkg4",
846
			package = {
847
				Depends = "pkg5 nonexist",
848
				Version = "4"
849
				-- No repo field here, this comes from the status, there are no repositories
850
			}
851 852 853 854 855 856 857 858
		},
		{
			action = "remove",
			name = "pkg5",
			package = {
				Version = "5"
				-- No repo field here, this comes from the status, there are no repositories
			}
859 860 861 862
		}
	}
	assert_table_equal(expected, result)
end
863 864 865 866 867 868 869 870

-- Test we don't schedule anything after a replan package
function test_replan()
	local requests = {
		{
			action = "require",
			name = "pkg1",
			package = {
871 872
				Version = "1",
				repo = def_repo
873 874 875 876 877 878 879 880 881 882
			},
			modifier = {
				replan = true
			}
		},
		{
			action = "require",
			name = "pkg2",
			package = {
				Version = "13",
883
				repo = def_repo
884 885 886 887 888 889 890 891 892
			},
			modifier = {}
		}
	}
	local result = planner.filter_required({}, requests)
	assert_table_equal({
		requests[1]
	}, result)
end
893 894

function test_candidate_choose()
895 896 897 898 899
	-- Create dummy repositories in requests module
	requests.known_repositories = {
		repo1 = {priority = 50, serial = 1},
		repo2 = {priority = 50, serial = 2}
	}
900
	local candidates = {
901
		{
902
			Version = "1",
903 904 905 906 907
			repo = requests.known_repositories.repo1
		},
		{
			Version = "1",
			repo = requests.known_repositories.repo2
908 909
		},
		{
910
			Version = "3",
911
			repo = requests.known_repositories.repo1
912 913
		},
		{
914
			Version = "2",
915
			repo = requests.known_repositories.repo1
916 917
		},
		{
918
			Version = "4",
919
			repo = requests.known_repositories.repo2
920 921
		}
	}
922 923
	assert_table_equal({
		candidates[1],
924 925
		candidates[2],
		candidates[4]
926 927 928
	}, planner.candidates_choose(candidates, "<3"))
	assert_table_equal({
		candidates[1],
929 930
		candidates[2],
		candidates[4]
931 932
	}, planner.candidates_choose(candidates, " < 3 "))
	assert_table_equal({
933
		candidates[1],
934
		candidates[4]
935
	}, planner.candidates_choose(candidates, "<3", {'repo1'}))
936
	assert_table_equal({
937
		candidates[1],
938 939
		candidates[2],
		candidates[4]
940 941 942
	}, planner.candidates_choose(candidates, "<3", {'repo1', requests.known_repositories.repo2}))
	assert_table_equal({
		candidates[5]
943
	}, planner.candidates_choose(candidates, ">3"))
944 945 946
	assert_table_equal({
		candidates[3],
		candidates[5]
947 948
	}, planner.candidates_choose(candidates, ">=3"))
	assert_table_equal({
949 950 951
		candidates[5]
	}, planner.candidates_choose(candidates, ">=3", {'repo2'}))
	assert_table_equal({
952
		candidates[3],
953 954 955 956 957 958
		candidates[5]
	}, planner.candidates_choose(candidates, ">=3", {'repo2', 'repo1'}))
	assert_table_equal({
		candidates[3],
		candidates[4],
		candidates[5]
959 960
	}, planner.candidates_choose(candidates, "=>2"))
	assert_table_equal({
961 962
		candidates[3],
		candidates[4]
963 964
	}, planner.candidates_choose(candidates, "~[23]"))
	assert_table_equal({
965 966
		candidates[3],
		candidates[4]
967
	}, planner.candidates_choose(candidates, "~[2 3]"))
968 969
	assert_table_equal({
		candidates[2],
970 971 972 973
		candidates[5]
	}, planner.candidates_choose(candidates, nil, {'repo2'}))
	assert_table_equal({
		candidates[1],
974 975
		candidates[3],
		candidates[4]
976
	}, planner.candidates_choose(candidates, nil, {requests.known_repositories.repo1}))
977 978 979
	-- Both of these should match nothing, because second character should be handled as part of version not compare specification.
	assert_table_equal({}, planner.candidates_choose(candidates, "~=1"))
	assert_table_equal({}, planner.candidates_choose(candidates, "=~1"))
980
end
981 982 983 984

function test_missing_install()
	local pkgs = {
		pkg1 = {
985
			candidates = {{Package = 'pkg1', deps = {}, repo = def_repo}},
986
			modifier = {}
987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006
		}
	}
	local requests = {
		{
			tp = 'install',
			package = {
				tp = 'package',
				name = 'pkg1',
			}
		},
		{
			tp = 'install',
			package = {
				tp = 'package',
				name = 'pkg2'
			},
			ignore = {'missing'}
		}
	}
	local result = planner.required_pkgs(pkgs, requests)
1007 1008 1009
	local expected = {
		{
			action = "require",
1010
			package = {Package = 'pkg1', deps = {}, repo = def_repo},
1011
			modifier = {},
1012 1013 1014 1015 1016 1017
			name = "pkg1"
		}
	}
	assert_table_equal(expected, result)
end

1018
function test_missing_dep_ignore()
1019 1020
	local pkgs = {
		pkg1 = {
1021
			candidates = {{Package = 'pkg1', deps = 'pkg2', repo = def_repo}},
1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
			modifier = {
				ignore = {"deps"}
			},
			name = "pkg1"
		}
	}
	local requests = {
		{
			tp = 'install',
			package = {
				tp = 'package',
				name = 'pkg1',
			}
		}
	}
	local result = planner.required_pkgs(pkgs, requests)
	local expected = {
		{
			action = "require",
1041
			package = {Package = 'pkg1', deps = 'pkg2', repo = def_repo},
1042 1043 1044 1045 1046 1047 1048
			modifier = {
				ignore = {"deps"}
			},
			name = "pkg1"
		}
	}
	assert_table_equal(expected, result)
1049
end
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107

function test_complex_deps()
	local pkgs = {}
	for i = 1, 7 do
		local n = "pkg" .. tostring(i)
		pkgs[n] = {
			candidates = {{Package = n, repo = def_repo}},
			modifier = {},
			name = n
		}
	end
	local pkg2 = {
		tp = "package",
		name = "pkg2",
	}
	pkgs.meta = {
		candidates = {{Package = "meta", repo = def_repo}},
		modifier = {
			deps = {
				tp = "dep-and",
				sub = {
					"pkg1",
					pkg2,
					{
						tp = "dep-or",
						sub = {
							{
								tp = "dep-and",
								sub = {
									"pkg3",
									"pkg4"
								}
							},
							"pkg5",
						}
					},
					{
						tp = "dep-not",
						sub = {
							"pkg6"
						}
					},
					"pkg7"
				}
			}
		},
		name = "meta"
	}
	local requests = {
		{
			tp = 'install',
			package = {
				tp = 'package',
				name = 'meta',
			}
		}
	}
	local result = planner.required_pkgs(pkgs, requests)
1108
	local expected = utils.map({"pkg1", "pkg2", "pkg3", "pkg4", "pkg7", "meta"}, function (_, name)
1109
		local p = pkgs[name]
1110
		return name, {
1111 1112 1113 1114 1115 1116
			action = "require",
			package = p.candidates[1],
			modifier = p.modifier,
			name = name
		}
	end)
1117 1118 1119
	assert_plan_dep_order(expected, result)
end

1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318
function test_version_request()
	local pkgs = {
		pkg1 = {
			candidates = {
				{Package = 'pkg1', deps = {}, Version = "2", repo = def_repo},
				{Package = 'pkg1', deps = {}, Version = "1", repo = def_repo}
			},
			modifier = {}
		},
		pkg2 = {
			candidates = {
				{Package = 'pkg2', Version = "2", deps = {}, repo = def_repo},
				{Package = 'pkg2', Version = "1", deps = {}, repo = def_repo}
			},
			modifier = {}
		}
	}
	local requests = {
		{
			tp = 'install',
			package = {
				tp = 'package',
				name = 'pkg1',
			},
			version = '>1'
		},
		{
			tp = 'install',
			package = {
				tp = 'package',
				name = 'pkg2',
			},
			version = '=1'
		}
	}
	local result = planner.required_pkgs(pkgs, requests)
	local expected = {
		pkg1 = {
			action = 'require',
			package = {Package = 'pkg1', deps = {}, Version = "2", repo = def_repo},
			modifier = {},
			name = 'pkg1'
		},
		pkg2 = {
			action = 'require',
			package = {Package = 'pkg2', deps = {}, Version = "1", repo = def_repo},
			modifier = {},
			name = 'pkg2'
		}
	}
	assert_plan_dep_order(expected, result)
end

function test_version_deps()
	local pkg_dep = {
		tp = 'dep-and',
		sub = {
			{
				tp = 'dep-package',
				name = 'dep1',
				version = '>1'
			},
			{
				tp = 'dep-package',
				name = 'dep2',
				version = '<2'
			},
			{
				tp = 'dep-or',
				sub = {
					{
						tp = 'dep-package',
						name = 'dep3',
						version = '=1'
					},
					{
						tp = 'dep-package',
						name = 'dep3',
						version = '=2'
					}
				}
			}
		}
	}
	local pkgs = {
		pkg = {
			candidates = {
				{Package = 'pkg', deps = pkg_dep, Version = "2", repo = def_repo},
				{Package = 'pkg', deps = {}, Version = "1", repo = def_repo}
			},
			modifier = {}
		},
		dep1 = {
			candidates = {
				{Package = 'dep1', Version = "2", deps = {}, repo = def_repo},
				{Package = 'dep1', Version = "1", deps = {}, repo = def_repo}
			},
			modifier = {}
		},
		dep2 = {
			candidates = {
				{Package = 'dep2', Version = "2", deps = {}, repo = def_repo},
				{Package = 'dep2', Version = "1", deps = {}, repo = def_repo}
			},
			modifier = {}
		},
		dep3 = {
			candidates = {
				{Package = 'dep3', Version = "2", deps = {}, repo = def_repo},
				{Package = 'dep3', Version = "1", deps = {}, repo = def_repo}
			},
			modifier = {}
		}
	}
	local requests = {
		{
			tp = 'install',
			package = {
				tp = 'package',
				name = 'pkg',
			}
		}
	}
	local result = planner.required_pkgs(pkgs, requests)
	local expected = {
		pkg = {
			action = 'require',
			package = {Package = 'pkg', deps = pkg_dep, Version = "2", repo = def_repo},
			modifier = {},
			name = 'pkg'
		},
		dep1 = {
			action = 'require',
			package = {Package = 'dep1', deps = {}, Version = "2", repo = def_repo},
			modifier = {},
			name = 'dep1'
		},
		dep2 = {
			action = 'require',
			package = {Package = 'dep2', deps = {}, Version = "1", repo = def_repo},
			modifier = {},
			name = 'dep2'
		},
		dep3 = {
			action = 'require',
			package = {Package = 'dep3', deps = {}, Version = "2", repo = def_repo},
			modifier = {},
			name = 'dep3'
		}
	}
	assert_plan_dep_order(expected, result)
end

-- Package depends on version we don't have
function test_version_missing_dep()
	local pkgs = {
		pkg = {
			candidates = {{Package = 'pkg', deps = {
				tp = 'dep-package', name = 'dep', version = '>=2'
			}, Version = "1", repo = def_repo}},
			modifier = {}
		},
		dep = {
			candidates = {{Package = 'dep', deps = {}, Version = "1", repo = def_repo}},
			modifier = {}
		}
	}
	local requests = {
		{
			tp = 'install',
			package = {
				tp = 'package',
				name = 'pkg',
			}
		}
	}
	assert_exception(function () planner.required_pkgs(pkgs, requests) end, 'inconsistent')
end

function test_version_missing_request()
	local pkgs = {
		pkg = {
			candidates = {{Package = 'pkg', Version = "1", repo = def_repo}},
			modifier = {}
		},
	}
	local requests = {
		{
			tp = 'install',
			package = {
				tp = 'package',
				name = 'pkg',
			},
			version = '>1'
		}
	}
	assert_exception(function () planner.required_pkgs(pkgs, requests) end, 'inconsistent')
end

1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350
function test_pkg_dep_iterate()
	local dep = {
		tp = "dep-and",
		sub = {
			{
				tp = 'dep-or',
				sub = { 'pkg1', 'pkg2' }
			},
			{
				tp = 'dep-and',
				sub = { {tp = 'dep-package'}, 'pkg3' }
			},
			'pkg4'
		}
	}
	local expected = {
		pkg1 = true,
		pkg2 = true,
		pkg3 = true,
		pkg4 = true,
	}
	for _, pkg in planner.pkg_dep_iterate(dep) do
		if type(pkg) == 'table' then
			assert_table_equal({tp = 'dep-package'}, pkg)
		else
			assert_true(expected[pkg])
			expected[pkg] = nil
		end
	end
	assert_nil(next(expected))
end

Karel Koci's avatar
Karel Koci committed
1351
-- This checks plan order by checking dependencies and tables in plan are checked against expected ones by name of package
1352
function assert_plan_dep_order(expected, plan)
Karel Koci's avatar
Karel Koci committed
1353 1354 1355 1356
	-- Check that plan contains all and only expected entries
	assert_table_equal(expected, utils.map(plan, function(_, p) return p.name, p end))
	-- Check that objects are in correct order
	local p2i = utils.map(plan, function(k, v) return v.name, k end)
1357
	for _, p in ipairs(plan) do
1358
		local alldeps = utils.arr_prune({p.modifier.deps, p.package.deps})
1359 1360 1361 1362 1363 1364 1365 1366 1367 1368
		local pi = p2i[p.name]
		for _, dep in ipairs(alldeps) do
			for _, pkg in planner.pkg_dep_iterate(dep) do
				local name = pkg.name or pkg
				if p2i[name] then -- We ignore packages thats are not in plan
					assert_true(pi > p2i[name])
				end
			end
		end
	end
1369
end