1 2 3 4 5 6 7 8 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 36 37 38 39 40 41 42 43 44 45 46 47 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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 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 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 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 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 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 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 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 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 511 512 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 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 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 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 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 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 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 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 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804
|
// Copyright (c) 2019-2023 Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE.md file distributed with the sources of this project regarding your
// rights to use or distribute this software.
// The DOCKER E2E group tests functionality of actions, pulls / builds of
// Docker/OCI source images. These tests are separated from direct SIF build /
// pull / actions because they examine OCI specific image behavior. They are run
// ordered, rather than in parallel to avoid any concurrency issues with
// containers/image. Also, we can then maximally benefit from caching to avoid
// Docker Hub rate limiting.
package docker
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"testing"
dockerclient "github.com/docker/docker/client"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/pkg/errors"
ocisif "github.com/sylabs/oci-tools/pkg/sif"
"github.com/sylabs/sif/v2/pkg/sif"
"github.com/sylabs/singularity/v4/e2e/internal/e2e"
"github.com/sylabs/singularity/v4/e2e/internal/testhelper"
"github.com/sylabs/singularity/v4/internal/pkg/test/tool/require"
"github.com/sylabs/singularity/v4/internal/pkg/test/tool/tmpl"
"github.com/sylabs/singularity/v4/internal/pkg/util/fs"
"golang.org/x/sys/unix"
"gotest.tools/assert"
)
type ctx struct {
env e2e.TestEnv
}
func (c ctx) testDockerPulls(t *testing.T) {
const tmpContainerFile = "test_container.sif"
tmpPath, err := fs.MakeTmpDir(c.env.TestDir, "docker-", 0o755)
err = errors.Wrapf(err, "creating temporary directory in %q for docker pull test", c.env.TestDir)
if err != nil {
t.Fatalf("failed to create temporary directory: %+v", err)
}
t.Cleanup(func() {
if !t.Failed() {
os.RemoveAll(tmpPath)
}
})
tmpImage := filepath.Join(tmpPath, tmpContainerFile)
tests := []struct {
name string
options []string
image string
uri string
exit int
}{
{
name: "BusyboxLatestPull",
image: tmpImage,
uri: "docker://busybox:latest",
exit: 0,
},
{
name: "BusyboxLatestPullFail",
image: tmpImage,
uri: "docker://busybox:latest",
exit: 255,
},
{
name: "BusyboxLatestPullForce",
options: []string{"--force"},
image: tmpImage,
uri: "docker://busybox:latest",
exit: 0,
},
{
name: "Busybox1.28Pull",
options: []string{"--force", "--dir", tmpPath},
image: tmpContainerFile,
uri: "docker://busybox:1.28",
exit: 0,
},
{
name: "Busybox1.28PullFail",
image: tmpImage,
uri: "docker://busybox:1.28",
exit: 255,
},
{
name: "Busybox1.28PullDirFail",
image: "/foo/sif.sif",
uri: "docker://busybox:1.28",
exit: 255,
},
}
for _, tt := range tests {
c.env.RunSingularity(
t,
e2e.AsSubtest(tt.name),
e2e.WithProfile(e2e.UserProfile),
e2e.WithCommand("pull"),
e2e.WithArgs(append(tt.options, tt.image, tt.uri)...),
e2e.PostRun(func(t *testing.T) {
if !t.Failed() && tt.exit == 0 {
path := tt.image
// handle the --dir case
if path == tmpContainerFile {
path = filepath.Join(tmpPath, tmpContainerFile)
}
c.env.ImageVerify(t, path)
}
}),
e2e.ExpectExit(tt.exit),
)
}
}
// Testing DOCKER_ host support (only if docker available)
func (c ctx) testDockerHost(t *testing.T) {
require.Command(t, "docker")
// Temporary homedir for docker commands, so invoking docker doesn't create
// a ~/.docker that may interfere elsewhere.
tmpHome, cleanupHome := e2e.MakeTempDir(t, c.env.TestDir, "docker-", "")
t.Cleanup(func() { e2e.Privileged(cleanupHome)(t) })
// Create a Dockerfile for a small image we can build locally
tmpPath, cleanup := e2e.MakeTempDir(t, c.env.TestDir, "docker-", "")
t.Cleanup(func() { cleanup(t) })
dockerfile := filepath.Join(tmpPath, "Dockerfile")
dockerfileContent := []byte("FROM alpine:latest\n")
err := os.WriteFile(dockerfile, dockerfileContent, 0o644)
if err != nil {
t.Fatalf("failed to create temporary Dockerfile: %+v", err)
}
dockerRef := "dinosaur/test-image:latest"
dockerURI := "docker-daemon:" + dockerRef
// Invoke docker build to build image in the docker daemon.
// Use os/exec because easier to generate a command with a working directory
e2e.Privileged(func(t *testing.T) {
cmd := exec.Command("docker", "build", "-t", dockerRef, tmpPath)
cmd.Dir = tmpPath
cmd.Env = append(cmd.Env, "HOME="+tmpHome)
out, err := cmd.CombinedOutput()
t.Log(cmd.Args)
if err != nil {
t.Fatalf("Unexpected error while running command.\n%s: %s", err, string(out))
}
})(t)
tests := []struct {
name string
envarName string
envarValue string
exit int
}{
// Unset docker host should use default and succeed
{
name: "singularityDockerHostEmpty",
envarName: "SINGULARITY_DOCKER_HOST",
envarValue: "",
exit: 0,
},
{
name: "dockerHostEmpty",
envarName: "DOCKER_HOST",
envarValue: "",
exit: 0,
},
// bad Docker host should fail
{
name: "singularityDockerHostInvalid",
envarName: "SINGULARITY_DOCKER_HOST",
envarValue: "tcp://192.168.59.103:oops",
exit: 255,
},
{
name: "dockerHostInvalid",
envarName: "DOCKER_HOST",
envarValue: "tcp://192.168.59.103:oops",
exit: 255,
},
// Set to default should succeed
// The default host varies based on OS, so we use dockerclient default
{
name: "singularityDockerHostValid",
envarName: "SINGULARITY_DOCKER_HOST",
envarValue: dockerclient.DefaultDockerHost,
exit: 0,
},
{
name: "dockerHostValid",
envarName: "DOCKER_HOST",
envarValue: dockerclient.DefaultDockerHost,
exit: 0,
},
}
for _, profile := range []e2e.Profile{e2e.RootProfile, e2e.OCIRootProfile} {
t.Run(profile.String(), func(t *testing.T) {
t.Run("exec", func(t *testing.T) {
for _, tt := range tests {
cmdOps := []e2e.SingularityCmdOp{
e2e.WithProfile(profile),
e2e.AsSubtest(profile.String() + "/" + tt.name),
e2e.WithCommand("exec"),
e2e.WithArgs("--disable-cache", dockerURI, "/bin/true"),
e2e.WithEnv(append(os.Environ(), tt.envarName+"="+tt.envarValue)),
e2e.ExpectExit(tt.exit),
}
c.env.RunSingularity(t, cmdOps...)
}
})
t.Run("pull", func(t *testing.T) {
for _, tt := range tests {
cmdOps := []e2e.SingularityCmdOp{
e2e.WithProfile(profile),
e2e.AsSubtest(tt.name),
e2e.WithCommand("pull"),
e2e.WithArgs("--force", "--disable-cache", dockerURI),
e2e.WithEnv(append(os.Environ(), tt.envarName+"="+tt.envarValue)),
e2e.WithDir(tmpPath),
e2e.ExpectExit(tt.exit),
}
c.env.RunSingularity(t, cmdOps...)
}
})
})
t.Run("build", func(t *testing.T) {
for _, tt := range tests {
cmdOps := []e2e.SingularityCmdOp{
e2e.WithProfile(e2e.RootProfile),
e2e.AsSubtest(tt.name),
e2e.WithCommand("build"),
e2e.WithArgs("--force", "--disable-cache", "test.sif", dockerURI),
e2e.WithEnv(append(os.Environ(), tt.envarName+"="+tt.envarValue)),
e2e.WithDir(tmpPath),
e2e.ExpectExit(tt.exit),
}
c.env.RunSingularity(t, cmdOps...)
}
})
}
// Clean up docker image
e2e.Privileged(func(t *testing.T) {
cmd := exec.Command("docker", "rmi", dockerRef)
cmd.Env = append(cmd.Env, "HOME="+tmpHome)
_, err = cmd.Output()
if err != nil {
t.Fatalf("Unexpected error while cleaning up docker image.\n%s", err)
}
})(t)
}
// Test that DOCKER_xyz env vars take priority over other means of
// authenticating with Docker - in particular, the --authfile flag.
func (c ctx) testDockerCredsPriority(t *testing.T) {
e2e.EnsureImage(t, c.env)
privImgNoPrefix := strings.TrimPrefix(c.env.TestRegistryPrivImage, "docker://")
simpleDef := e2e.PrepareDefFile(e2e.DefFileDetails{
Bootstrap: "docker",
From: privImgNoPrefix,
})
t.Cleanup(func() {
if !t.Failed() {
os.Remove(simpleDef)
}
})
tmpdir, tmpdirCleanup := e2e.MakeTempDir(t, c.env.TestDir, "build-auth", "")
t.Cleanup(func() {
if !t.Failed() {
tmpdirCleanup(t)
}
})
dockerfileContent := fmt.Sprintf(
`
FROM %s
CMD /bin/true
`,
privImgNoPrefix,
)
dockerfile, err := e2e.WriteTempFile(tmpdir, "Dockerfile", dockerfileContent)
if err != nil {
t.Fatalf("while trying to generate test dockerfile: %v", err)
}
ocisifPath := dockerfile + ".oci.sif"
profiles := []e2e.Profile{
e2e.UserProfile,
e2e.RootProfile,
}
for _, p := range profiles {
t.Run(p.String(), func(t *testing.T) {
t.Run("def pull", func(t *testing.T) {
c.dockerCredsPriorityTester(t, false, p, "pull", "--disable-cache", "--no-https", "-F", c.env.TestRegistryPrivImage)
})
t.Run("def exec", func(t *testing.T) {
c.dockerCredsPriorityTester(t, false, p, "exec", "--disable-cache", "--no-https", c.env.TestRegistryPrivImage, "true")
})
t.Run("cstm pull", func(t *testing.T) {
c.dockerCredsPriorityTester(t, true, p, "pull", "--disable-cache", "--no-https", "-F", c.env.TestRegistryPrivImage)
})
t.Run("cstm exec", func(t *testing.T) {
c.dockerCredsPriorityTester(t, true, p, "exec", "--disable-cache", "--no-https", c.env.TestRegistryPrivImage, "true")
})
})
}
profiles = []e2e.Profile{
e2e.OCIUserProfile,
e2e.OCIRootProfile,
}
for _, p := range profiles {
t.Run(p.String(), func(t *testing.T) {
t.Run("def df build", func(t *testing.T) {
c.dockerCredsPriorityTester(t, false, p, "build", "-F", ocisifPath, dockerfile)
})
t.Run("cstm df build", func(t *testing.T) {
c.dockerCredsPriorityTester(t, true, p, "build", "-F", ocisifPath, dockerfile)
})
})
}
}
func (c ctx) dockerCredsPriorityTester(t *testing.T, withCustomAuthFile bool, profile e2e.Profile, cmd string, args ...string) {
tmpdir, tmpdirCleanup := e2e.MakeTempDir(t, c.env.TestDir, "docker-auth", "")
t.Cleanup(func() {
if !t.Failed() {
tmpdirCleanup(t)
}
})
prevCwd, err := os.Getwd()
if err != nil {
t.Fatalf("could not get current working directory: %s", err)
}
defer os.Chdir(prevCwd)
if err = os.Chdir(tmpdir); err != nil {
t.Fatalf("could not change cwd to %q: %s", tmpdir, err)
}
localAuthFileName := ""
if withCustomAuthFile {
localAuthFileName = "./my_local_authfile"
}
authFileArgs := []string{}
if withCustomAuthFile {
authFileArgs = []string{"--authfile", localAuthFileName}
}
// Store the previous values of relevant env vars, and set up facilities for
// wiping and restoring them.
envVarSet := []string{
"SINGULARITY_DOCKER_USERNAME",
"SINGULARITY_DOCKER_PASSWORD",
"DOCKER_USERNAME",
"DOCKER_PASSWORD",
}
prevEnvVals := make(map[string]string)
for _, varName := range envVarSet {
if varVal, ok := os.LookupEnv(varName); ok {
prevEnvVals[varName] = varVal
}
}
wipeVars := func() {
for _, varName := range envVarSet {
os.Unsetenv(varName)
}
}
restoreVars := func() {
wipeVars()
for _, varName := range envVarSet {
if varVal, ok := prevEnvVals[varName]; ok {
os.Setenv(varName, varVal)
}
}
}
t.Cleanup(func() {
e2e.PrivateRepoLogout(t, c.env, profile, localAuthFileName)
restoreVars()
})
tests := []struct {
name string
pfxDockerUser string
pfxDockerPass string
nopfxDockerUser string
nopfxDockerPass string
authLoggedIn bool
expectExit int
}{
{
name: "pfx denv wrong, no auth",
pfxDockerUser: "wrong",
pfxDockerPass: "wrong",
authLoggedIn: false,
expectExit: 255,
},
{
name: "pfx denv wrong, auth",
pfxDockerUser: "wrong",
pfxDockerPass: "wrong",
authLoggedIn: true,
expectExit: 255,
},
{
name: "pfx denv right, no auth",
pfxDockerUser: e2e.DefaultUsername,
pfxDockerPass: e2e.DefaultPassword,
authLoggedIn: false,
expectExit: 0,
},
{
name: "pfx denv right, auth",
pfxDockerUser: e2e.DefaultUsername,
pfxDockerPass: e2e.DefaultPassword,
authLoggedIn: true,
expectExit: 0,
},
{
name: "nopfx denv wrong, no auth",
nopfxDockerUser: "wrong",
nopfxDockerPass: "wrong",
authLoggedIn: false,
expectExit: 255,
},
{
name: "nopfx denv wrong, auth",
nopfxDockerUser: "wrong",
nopfxDockerPass: "wrong",
authLoggedIn: true,
expectExit: 255,
},
{
name: "nopfx denv right, no auth",
nopfxDockerUser: e2e.DefaultUsername,
nopfxDockerPass: e2e.DefaultPassword,
authLoggedIn: false,
expectExit: 0,
},
{
name: "nopfx denv right, auth",
nopfxDockerUser: e2e.DefaultUsername,
nopfxDockerPass: e2e.DefaultPassword,
authLoggedIn: true,
expectExit: 0,
},
{
name: "both denv (pfx right), auth",
pfxDockerUser: e2e.DefaultUsername,
pfxDockerPass: e2e.DefaultPassword,
nopfxDockerUser: "wrong",
nopfxDockerPass: "wrong",
authLoggedIn: true,
expectExit: 0,
},
{
name: "both denv (pfx right), noauth",
pfxDockerUser: e2e.DefaultUsername,
pfxDockerPass: e2e.DefaultPassword,
nopfxDockerUser: "wrong",
nopfxDockerPass: "wrong",
authLoggedIn: false,
expectExit: 0,
},
{
name: "both denv (nopfx right), auth",
pfxDockerUser: "wrong",
pfxDockerPass: "wrong",
nopfxDockerUser: e2e.DefaultUsername,
nopfxDockerPass: e2e.DefaultPassword,
authLoggedIn: true,
expectExit: 255,
},
{
name: "both denv (nopfx right), noauth",
pfxDockerUser: "wrong",
pfxDockerPass: "wrong",
nopfxDockerUser: e2e.DefaultUsername,
nopfxDockerPass: e2e.DefaultPassword,
authLoggedIn: false,
expectExit: 255,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
wipeVars()
if tt.pfxDockerUser != "" {
os.Setenv("SINGULARITY_DOCKER_USERNAME", tt.pfxDockerUser)
}
if tt.pfxDockerPass != "" {
os.Setenv("SINGULARITY_DOCKER_PASSWORD", tt.pfxDockerPass)
}
if tt.nopfxDockerUser != "" {
os.Setenv("DOCKER_USERNAME", tt.nopfxDockerUser)
}
if tt.nopfxDockerPass != "" {
os.Setenv("DOCKER_PASSWORD", tt.nopfxDockerPass)
}
if tt.authLoggedIn {
e2e.PrivateRepoLogin(t, c.env, profile, localAuthFileName)
} else {
e2e.PrivateRepoLogout(t, c.env, profile, localAuthFileName)
}
c.env.RunSingularity(
t,
e2e.WithProfile(profile),
e2e.WithCommand(cmd),
e2e.WithArgs(append(authFileArgs, args...)...),
e2e.ExpectExit(tt.expectExit),
)
})
}
}
// AUFS whiteout tests
func (c ctx) testDockerAUFS(t *testing.T) {
tests := []struct {
name string
profile e2e.Profile
keepLayers bool
}{
// Native SIF - whiteouts applied to squashed image via umoci rootfs
// extraction at creation.
{
name: "NativeSIF",
profile: e2e.UserProfile,
keepLayers: false,
},
// Single layer OCI-SIF - whiteouts applied to squashed image via
// oci-tools.Squash at creation.
{
name: "OCISIF",
profile: e2e.OCIUserProfile,
keepLayers: false,
},
// Multi layer OCI-SIF - whiteouts translated AUFS -> OverlayFS by
// oci-tools at creation and applied at runtime.
{
name: "OCISIFKeepLayers",
profile: e2e.OCIUserProfile,
keepLayers: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c.aufsTest(t, tt.profile, tt.keepLayers)
})
}
}
func (c ctx) aufsTest(t *testing.T, profile e2e.Profile, keepLayers bool) {
imageDir, cleanup := e2e.MakeTempDir(t, c.env.TestDir, "aufs-", "")
t.Cleanup(func() {
if !t.Failed() {
cleanup(t)
}
})
imagePath := filepath.Join(imageDir, "container")
args := []string{}
if keepLayers {
args = []string{"--keep-layers"}
}
args = append(args, imagePath, "docker://sylabsio/aufs-sanity")
c.env.RunSingularity(
t,
e2e.WithProfile(profile),
e2e.WithCommand("pull"),
e2e.WithArgs(args...),
e2e.ExpectExit(0),
)
if t.Failed() {
return
}
fileTests := []struct {
name string
argv []string
exit int
}{
// 'file2' should be present in three locations
{
name: "File 2",
argv: []string{imagePath, "ls", "/test/whiteout-dir/file2", "/test/whiteout-file/file2", "/test/normal-dir/file2"},
exit: 0,
},
// '/test/whiteout-file/file1' should be absent (via whiteout of the file)
{
name: "WhiteoutFileFile1",
argv: []string{imagePath, "ls", "/test/whiteout-file/file1"},
exit: 1,
},
// '/test/whiteout-dir/file1' should be absent (via whiteout of the dir)
{
name: "WhiteoutDirFile1",
argv: []string{imagePath, "ls", "/test/whiteout-dir/file1"},
exit: 1,
},
}
for _, tt := range fileTests {
c.env.RunSingularity(
t,
e2e.AsSubtest(tt.name),
e2e.WithProfile(profile),
e2e.WithCommand("exec"),
e2e.WithArgs(tt.argv...),
e2e.ExpectExit(tt.exit),
)
}
}
// Check force permissions for user builds #977
func (c ctx) testDockerPermissions(t *testing.T) {
imageDir, cleanup := e2e.MakeTempDir(t, c.env.TestDir, "perm-", "")
t.Cleanup(func() {
if !t.Failed() {
cleanup(t)
}
})
imagePath := filepath.Join(imageDir, "container")
c.env.RunSingularity(
t,
e2e.WithProfile(e2e.UserProfile),
e2e.WithCommand("build"),
e2e.WithArgs([]string{imagePath, "docker://sylabsio/userperms"}...),
e2e.ExpectExit(0),
)
if t.Failed() {
return
}
fileTests := []struct {
name string
argv []string
exit int
}{
{
name: "TestDir",
argv: []string{imagePath, "ls", "/testdir/"},
exit: 0,
},
{
name: "TestDirFile",
argv: []string{imagePath, "ls", "/testdir/testfile"},
exit: 1,
},
}
for _, tt := range fileTests {
c.env.RunSingularity(
t,
e2e.AsSubtest(tt.name),
e2e.WithProfile(e2e.UserProfile),
e2e.WithCommand("exec"),
e2e.WithArgs(tt.argv...),
e2e.ExpectExit(tt.exit),
)
}
}
// Check whiteout of symbolic links #1592 #1576
func (c ctx) testDockerWhiteoutSymlink(t *testing.T) {
imageDir, cleanup := e2e.MakeTempDir(t, c.env.TestDir, "whiteout-", "")
t.Cleanup(func() {
if !t.Failed() {
cleanup(t)
}
})
imagePath := filepath.Join(imageDir, "container")
c.env.RunSingularity(
t,
e2e.WithProfile(e2e.UserProfile),
e2e.WithCommand("build"),
e2e.WithArgs([]string{imagePath, "docker://sylabsio/linkwh"}...),
e2e.PostRun(func(t *testing.T) {
if t.Failed() {
return
}
c.env.ImageVerify(t, imagePath)
}),
e2e.ExpectExit(0),
)
}
func (c ctx) testDockerDefFile(t *testing.T) {
imageDir, cleanup := e2e.MakeTempDir(t, c.env.TestDir, "def-", "")
t.Cleanup(func() {
if !t.Failed() {
cleanup(t)
}
})
imagePath := filepath.Join(imageDir, "container")
getKernelMajor := func(t *testing.T) (major int) {
var buf unix.Utsname
if err := unix.Uname(&buf); err != nil {
err = errors.Wrap(err, "getting current kernel information")
t.Fatalf("uname failed: %+v", err)
}
n, err := fmt.Sscanf(string(buf.Release[:]), "%d.", &major)
err = errors.Wrap(err, "getting current kernel release")
if err != nil {
t.Fatalf("Sscanf failed, n=%d: %+v", n, err)
}
if n != 1 {
t.Fatalf("Unexpected result while getting major release number: n=%d", n)
}
return
}
tests := []struct {
name string
kernelMajorRequired int
archRequired string
from string
}{
{
name: "Alpine",
kernelMajorRequired: 0,
from: "alpine:latest",
},
{
name: "AlmaLinux_9",
kernelMajorRequired: 3,
from: "almalinux:9",
},
{
name: "Ubuntu_2204",
kernelMajorRequired: 3,
from: "ubuntu:22.04",
},
}
for _, tt := range tests {
defFile := e2e.PrepareDefFile(e2e.DefFileDetails{
Bootstrap: "docker",
From: tt.from,
})
c.env.RunSingularity(
t,
e2e.AsSubtest(tt.name),
e2e.WithProfile(e2e.RootProfile),
e2e.WithCommand("build"),
e2e.WithArgs([]string{imagePath, defFile}...),
e2e.PreRun(func(t *testing.T) {
require.Arch(t, tt.archRequired)
if getKernelMajor(t) < tt.kernelMajorRequired {
t.Skipf("kernel >=%v.x required", tt.kernelMajorRequired)
}
}),
e2e.PostRun(func(t *testing.T) {
if t.Failed() {
return
}
c.env.ImageVerify(t, imagePath)
if !t.Failed() {
os.Remove(imagePath)
os.Remove(defFile)
}
}),
e2e.ExpectExit(0),
)
}
}
func (c ctx) testDockerRegistry(t *testing.T) {
imageDir, cleanup := e2e.MakeTempDir(t, c.env.TestDir, "registry-", "")
t.Cleanup(func() {
if !t.Failed() {
cleanup(t)
}
})
imagePath := filepath.Join(imageDir, "container")
tests := []struct {
name string
exit int
dfd e2e.DefFileDetails
}{
{
name: "Alpine",
exit: 0,
dfd: e2e.DefFileDetails{
Bootstrap: "docker",
From: c.env.TestRegistry + "/my-alpine:3.18",
},
},
{
name: "AlpineRegistry",
exit: 0,
dfd: e2e.DefFileDetails{
Bootstrap: "docker",
From: "my-alpine:3.18",
Registry: c.env.TestRegistry,
},
},
{
name: "AlpineNamespace",
exit: 255,
dfd: e2e.DefFileDetails{
Bootstrap: "docker",
From: "my-alpine:3.18",
Registry: c.env.TestRegistry,
Namespace: "not-a-namespace",
},
},
}
for _, tt := range tests {
defFile := e2e.PrepareDefFile(tt.dfd)
c.env.RunSingularity(
t,
e2e.WithProfile(e2e.RootProfile),
e2e.WithCommand("build"),
e2e.WithArgs("--disable-cache", "--no-https", imagePath, defFile),
e2e.PostRun(func(t *testing.T) {
if t.Failed() || tt.exit != 0 {
return
}
c.env.ImageVerify(t, imagePath)
if !t.Failed() {
os.Remove(imagePath)
os.Remove(defFile)
}
}),
e2e.ExpectExit(tt.exit),
)
}
}
func (c ctx) testDockerLabels(t *testing.T) {
imageDir, cleanup := e2e.MakeTempDir(t, c.env.TestDir, "labels-", "")
t.Cleanup(func() {
if !t.Failed() {
cleanup(t)
}
})
imagePath := filepath.Join(imageDir, "container")
// Test container & set labels
// See: https://github.com/sylabs/singularity-test-containers/pull/1
imgSrc := "docker://sylabsio/labels"
label1 := "LABEL1: 1"
label2 := "LABEL2: TWO"
c.env.RunSingularity(
t,
e2e.AsSubtest("build"),
e2e.WithProfile(e2e.RootProfile),
e2e.WithCommand("build"),
e2e.WithArgs(imagePath, imgSrc),
e2e.ExpectExit(0),
)
verifyOutput := func(t *testing.T, r *e2e.SingularityCmdResult) {
output := string(r.Stdout)
for _, l := range []string{label1, label2} {
if !strings.Contains(output, l) {
t.Errorf("Did not find expected label %s in inspect output", l)
}
}
}
c.env.RunSingularity(
t,
e2e.AsSubtest("inspect"),
e2e.WithProfile(e2e.UserProfile),
e2e.WithCommand("inspect"),
e2e.WithArgs([]string{"--labels", imagePath}...),
e2e.ExpectExit(0, verifyOutput),
)
}
func (c ctx) testDockerCMD(t *testing.T) {
imageDir, cleanup := e2e.MakeTempDir(t, c.env.TestDir, "docker-", "")
t.Cleanup(func() {
if !t.Failed() {
cleanup(t)
}
})
imagePath := filepath.Join(imageDir, "container")
home, err := os.UserHomeDir()
if err != nil {
t.Fatalf("while getting $HOME: %s", err)
}
c.env.RunSingularity(
t,
e2e.WithProfile(e2e.UserProfile),
e2e.WithCommand("pull"),
e2e.WithArgs(imagePath, "docker://sylabsio/docker-cmd"),
e2e.ExpectExit(0),
)
tests := []struct {
name string
args []string
noeval bool
expectOutput string
}{
// Singularity historic behavior (without --no-eval)
// These do not all match Docker, due to evaluation, consumption of quoting.
{
name: "default",
args: []string{},
noeval: false,
expectOutput: `CMD 'quotes' "quotes" $DOLLAR s p a c e s`,
},
{
name: "override",
args: []string{"echo", "test"},
noeval: false,
expectOutput: `test`,
},
{
name: "override env var",
args: []string{"echo", "$HOME"},
noeval: false,
expectOutput: home,
},
// This looks very wrong, but is historic behavior
{
name: "override sh echo",
args: []string{"sh", "-c", `echo "hello there"`},
noeval: false,
expectOutput: "hello",
},
// Docker/OCI behavior (with --no-eval)
{
name: "no-eval/default",
args: []string{},
noeval: true,
expectOutput: `CMD 'quotes' "quotes" $DOLLAR s p a c e s`,
},
{
name: "no-eval/override",
args: []string{"echo", "test"},
noeval: true,
expectOutput: `test`,
},
{
name: "no-eval/override env var",
noeval: true,
args: []string{"echo", "$HOME"},
expectOutput: "$HOME",
},
{
name: "no-eval/override sh echo",
noeval: true,
args: []string{"sh", "-c", `echo "hello there"`},
expectOutput: "hello there",
},
}
for _, tt := range tests {
cmdArgs := []string{}
if tt.noeval {
cmdArgs = append(cmdArgs, "--no-eval")
}
cmdArgs = append(cmdArgs, imagePath)
cmdArgs = append(cmdArgs, tt.args...)
c.env.RunSingularity(
t,
e2e.AsSubtest(tt.name),
e2e.WithProfile(e2e.UserProfile),
e2e.WithCommand("run"),
e2e.WithArgs(cmdArgs...),
e2e.ExpectExit(0,
e2e.ExpectOutput(e2e.ExactMatch, tt.expectOutput),
),
)
}
}
//nolint:dupl
func (c ctx) testDockerENTRYPOINT(t *testing.T) {
imageDir, cleanup := e2e.MakeTempDir(t, c.env.TestDir, "docker-", "")
t.Cleanup(func() {
if !t.Failed() {
cleanup(t)
}
})
imagePath := filepath.Join(imageDir, "container")
home, err := os.UserHomeDir()
if err != nil {
t.Fatalf("while getting $HOME: %s", err)
}
c.env.RunSingularity(
t,
e2e.WithProfile(e2e.UserProfile),
e2e.WithCommand("pull"),
e2e.WithArgs(imagePath, "docker://sylabsio/docker-entrypoint"),
e2e.ExpectExit(0),
)
tests := []struct {
name string
args []string
noeval bool
expectOutput string
}{
// Singularity historic behavior (without --no-eval)
// These do not all match Docker, due to evaluation, consumption of quoting.
{
name: "default",
args: []string{},
noeval: false,
expectOutput: `ENTRYPOINT 'quotes' "quotes" $DOLLAR s p a c e s`,
},
{
name: "override",
args: []string{"echo", "test"},
noeval: false,
expectOutput: `ENTRYPOINT 'quotes' "quotes" $DOLLAR s p a c e s echo test`,
},
{
name: "override env var",
args: []string{"echo", "$HOME"},
noeval: false,
expectOutput: `ENTRYPOINT 'quotes' "quotes" $DOLLAR s p a c e s echo ` + home,
},
// Docker/OCI behavior (with --no-eval)
{
name: "no-eval/default",
args: []string{},
noeval: true,
expectOutput: `ENTRYPOINT 'quotes' "quotes" $DOLLAR s p a c e s`,
},
{
name: "no-eval/override",
args: []string{"echo", "test"},
noeval: true,
expectOutput: `ENTRYPOINT 'quotes' "quotes" $DOLLAR s p a c e s echo test`,
},
{
name: "no-eval/override env var",
noeval: true,
args: []string{"echo", "$HOME"},
expectOutput: `ENTRYPOINT 'quotes' "quotes" $DOLLAR s p a c e s echo $HOME`,
},
}
for _, tt := range tests {
cmdArgs := []string{}
if tt.noeval {
cmdArgs = append(cmdArgs, "--no-eval")
}
cmdArgs = append(cmdArgs, imagePath)
cmdArgs = append(cmdArgs, tt.args...)
c.env.RunSingularity(
t,
e2e.AsSubtest(tt.name),
e2e.WithProfile(e2e.UserProfile),
e2e.WithCommand("run"),
e2e.WithArgs(cmdArgs...),
e2e.ExpectExit(0,
e2e.ExpectOutput(e2e.ExactMatch, tt.expectOutput),
),
)
}
}
//nolint:dupl
func (c ctx) testDockerCMDENTRYPOINT(t *testing.T) {
imageDir, cleanup := e2e.MakeTempDir(t, c.env.TestDir, "docker-", "")
t.Cleanup(func() {
if !t.Failed() {
cleanup(t)
}
})
imagePath := filepath.Join(imageDir, "container")
home, err := os.UserHomeDir()
if err != nil {
t.Fatalf("while getting $HOME: %s", err)
}
c.env.RunSingularity(
t,
e2e.WithProfile(e2e.UserProfile),
e2e.WithCommand("pull"),
e2e.WithArgs(imagePath, "docker://sylabsio/docker-cmd-entrypoint"),
e2e.ExpectExit(0),
)
tests := []struct {
name string
args []string
noeval bool
expectOutput string
}{
// Singularity historic behavior (without --no-eval)
// These do not all match Docker, due to evaluation, consumption of quoting.
{
name: "default",
args: []string{},
noeval: false,
expectOutput: `ENTRYPOINT 'quotes' "quotes" $DOLLAR s p a c e s CMD 'quotes' "quotes" $DOLLAR s p a c e s`,
},
{
name: "override",
args: []string{"echo", "test"},
noeval: false,
expectOutput: `ENTRYPOINT 'quotes' "quotes" $DOLLAR s p a c e s echo test`,
},
{
name: "override env var",
args: []string{"echo", "$HOME"},
noeval: false,
expectOutput: `ENTRYPOINT 'quotes' "quotes" $DOLLAR s p a c e s echo ` + home,
},
// Docker/OCI behavior (with --no-eval)
{
name: "no-eval/default",
args: []string{},
noeval: true,
expectOutput: `ENTRYPOINT 'quotes' "quotes" $DOLLAR s p a c e s CMD 'quotes' "quotes" $DOLLAR s p a c e s`,
},
{
name: "no-eval/override",
args: []string{"echo", "test"},
noeval: true,
expectOutput: `ENTRYPOINT 'quotes' "quotes" $DOLLAR s p a c e s echo test`,
},
{
name: "no-eval/override env var",
noeval: true,
args: []string{"echo", "$HOME"},
expectOutput: `ENTRYPOINT 'quotes' "quotes" $DOLLAR s p a c e s echo $HOME`,
},
}
for _, tt := range tests {
cmdArgs := []string{}
if tt.noeval {
cmdArgs = append(cmdArgs, "--no-eval")
}
cmdArgs = append(cmdArgs, imagePath)
cmdArgs = append(cmdArgs, tt.args...)
c.env.RunSingularity(
t,
e2e.AsSubtest(tt.name),
e2e.WithProfile(e2e.UserProfile),
e2e.WithCommand("run"),
e2e.WithArgs(cmdArgs...),
e2e.ExpectExit(0,
e2e.ExpectOutput(e2e.ExactMatch, tt.expectOutput),
),
)
}
}
// https://github.com/sylabs/singularity/issues/233
// This tests quotes in the CMD shell form, not the [ .. ] exec form.
func (c ctx) testDockerCMDQuotes(t *testing.T) {
c.env.RunSingularity(
t,
e2e.WithProfile(e2e.UserProfile),
e2e.WithCommand("run"),
e2e.WithArgs("docker://sylabsio/issue233"),
e2e.ExpectExit(0,
e2e.ExpectOutput(e2e.ContainMatch, "Test run"),
),
)
}
// Check that the USER & WORKDIR in a docker container are honored under --oci mode
func (c ctx) testDockerUSERWORKDIR(t *testing.T) {
dockerURI := "docker://sylabsio/docker-user"
dockerfile := filepath.Join("..", "test", "defs", "Dockerfile.customuser")
tmpdir, tmpdirCleanup := e2e.MakeTempDir(t, "", "dockerfile-build-USER-", "temp dir for OCI-SIF images")
t.Cleanup(func() {
if !t.Failed() {
tmpdirCleanup(t)
}
})
userBuiltOCISIF := filepath.Join(tmpdir, "docker-user.oci.sif")
c.env.RunSingularity(
t,
e2e.AsSubtest("user df build"),
e2e.WithProfile(e2e.OCIUserProfile),
e2e.WithCommand("build"),
e2e.WithArgs(userBuiltOCISIF, dockerfile),
e2e.ExpectExit(0),
)
rootBuiltOCISIF := filepath.Join(tmpdir, "rootbuilt-docker-user.oci.sif")
c.env.RunSingularity(
t,
e2e.AsSubtest("root df build"),
e2e.WithProfile(e2e.OCIRootProfile),
e2e.WithCommand("build"),
e2e.WithArgs(rootBuiltOCISIF, dockerfile),
e2e.ExpectExit(0),
)
// Sanity check singularity native engine... no support for USER
c.env.RunSingularity(
t,
e2e.AsSubtest("default"),
e2e.WithProfile(e2e.UserProfile),
e2e.WithCommand("run"),
e2e.WithArgs(dockerURI),
e2e.ExpectExit(0, e2e.ExpectOutput(e2e.ContainMatch, fmt.Sprintf("uid=%d(%s) gid=%d",
e2e.UserProfile.ContainerUser(t).UID,
e2e.UserProfile.ContainerUser(t).Name,
e2e.UserProfile.ContainerUser(t).GID,
))),
)
metaTests := map[string]string{
"uri": dockerURI,
"user oci-sif": userBuiltOCISIF,
"root oci-sif": rootBuiltOCISIF,
}
for subtestName, container := range metaTests {
t.Run(subtestName, func(t *testing.T) {
c.testDockerUSERWorker(t, container)
})
}
}
func (c ctx) testDockerUSERWorker(t *testing.T, container string) {
tests := []struct {
name string
cmd string
args []string
wd string
expectOutputs []e2e.SingularityCmdResultOp
profiles []e2e.Profile
expectExit int
}{
// `--oci` should honor container USER by default
{
name: "OCIImageUser",
cmd: "run",
profiles: []e2e.Profile{e2e.OCIUserProfile, e2e.OCIRootProfile},
args: []string{container},
expectOutputs: []e2e.SingularityCmdResultOp{
e2e.ExpectOutput(e2e.ContainMatch, `uid=2000(testuser) gid=2000(testgroup)`),
},
},
// `--fakeroot` is an explicit request for root in the container
{
name: "OCIFakerootUser",
profiles: []e2e.Profile{e2e.OCIFakerootProfile},
args: []string{container},
expectOutputs: []e2e.SingularityCmdResultOp{
e2e.ExpectOutput(e2e.ContainMatch, `uid=0(root) gid=0(root)`),
},
},
// At present, we don't support specifying `--home` when container declares a USER.
{
name: "WithHomeOCIUser",
cmd: "run",
profiles: []e2e.Profile{e2e.OCIUserProfile, e2e.OCIRootProfile, e2e.OCIFakerootProfile},
args: []string{"--home", "/tmp", container},
expectExit: 255,
},
// $HOME env var should match the container USER's home dir, by default.
{
name: "OCIImageHomeEnv",
cmd: "exec",
profiles: []e2e.Profile{e2e.OCIUserProfile, e2e.OCIRootProfile},
args: []string{container, "env"},
expectOutputs: []e2e.SingularityCmdResultOp{
e2e.ExpectOutput(e2e.RegexMatch, `\bHOME=/home/testuser\b`),
},
expectExit: 0,
},
// `--fakeroot` is an explicit request for root in the container, so verify home dir.
{
name: "OCIFakerootHomeEnv",
cmd: "exec",
profiles: []e2e.Profile{e2e.OCIFakerootProfile},
args: []string{container, "env"},
expectOutputs: []e2e.SingularityCmdResultOp{
e2e.ExpectOutput(e2e.RegexMatch, `\bHOME=/root\b`),
},
expectExit: 0,
},
// USER's home directory should always be owned by USER
{
name: "OCIImageHomePerms",
cmd: "exec",
profiles: []e2e.Profile{e2e.OCIUserProfile, e2e.OCIRootProfile, e2e.OCIFakerootProfile},
args: []string{container, "stat", "-c", "%U(%u):%G(%g)", "/home/testuser"},
expectOutputs: []e2e.SingularityCmdResultOp{
e2e.ExpectOutput(e2e.ExactMatch, "testuser(2000):testgroup(2000)"),
},
expectExit: 0,
},
// WORKDIR should be honored, by default.
{
name: "OCIImageWorkdir",
cmd: "exec",
profiles: []e2e.Profile{e2e.OCIUserProfile, e2e.OCIRootProfile, e2e.OCIFakerootProfile},
args: []string{container, "pwd"},
expectOutputs: []e2e.SingularityCmdResultOp{
e2e.ExpectOutput(e2e.ExactMatch, "/home/testuser"),
},
expectExit: 0,
},
// --no-compat emulates native mode, so WORKDIR is ignored and container is entered at host CWD.
{
name: "OCINoCompatWorkdir",
cmd: "exec",
profiles: []e2e.Profile{e2e.OCIUserProfile, e2e.OCIRootProfile, e2e.OCIFakerootProfile},
args: []string{"--no-compat", container, "pwd"},
wd: "/tmp",
expectOutputs: []e2e.SingularityCmdResultOp{
e2e.ExpectOutput(e2e.ExactMatch, "/tmp"),
},
expectExit: 0,
},
}
for _, tt := range tests {
for _, profile := range tt.profiles {
cmd := "run"
if tt.cmd != "" {
cmd = tt.cmd
}
cmdOps := []e2e.SingularityCmdOp{
e2e.AsSubtest(tt.name + "/" + profile.String()),
e2e.WithProfile(profile),
e2e.WithCommand(cmd),
e2e.WithArgs(tt.args...),
e2e.ExpectExit(tt.expectExit, tt.expectOutputs...),
}
if tt.wd != "" {
cmdOps = append(cmdOps, e2e.WithDir(tt.wd))
}
c.env.RunSingularity(
t,
cmdOps...,
)
}
}
}
// Test that we can pull for different --platforms
func (c ctx) testDockerPlatform(t *testing.T) {
tmpPath, err := fs.MakeTmpDir(c.env.TestDir, "docker-platform-", 0o755)
if err != nil {
t.Fatalf("failed to create temporary directory: %v", err)
}
t.Cleanup(func() {
if !t.Failed() {
os.RemoveAll(tmpPath)
}
})
tmpSIF := filepath.Join(tmpPath, "test.sif")
tests := []struct {
name string
platform string
uri string
exit int
}{
{
name: "MultiArchArm64",
platform: "linux/arm64/v8",
uri: "docker://alpine:latest",
exit: 0,
},
{
name: "MultiArchPpc64le",
platform: "linux/ppc64le",
uri: "docker://alpine:latest",
exit: 0,
},
{
name: "MultiArchInvalidPlatform",
platform: "windows/m68k",
uri: "docker://alpine:latest",
exit: 255,
},
{
name: "SingleArchArm64",
platform: "linux/arm64/v8",
uri: "docker://arm64v8/alpine:latest",
exit: 0,
},
{
name: "SingleArchPpc64le",
platform: "linux/ppc64le",
uri: "docker://ppc64le/alpine:latest",
exit: 0,
},
{
name: "SingleArchInvalidPlatform",
platform: "windows/m68k",
uri: "docker://ppc64le/alpine:latest",
exit: 255,
},
{
name: "SingleArchMissingPlatform",
platform: "linux/arm64",
uri: "docker://ppc64le/alpine:latest",
exit: 255,
},
}
for _, p := range []e2e.Profile{e2e.UserProfile, e2e.OCIUserProfile} {
for _, tt := range tests {
c.env.RunSingularity(
t,
e2e.AsSubtest(p.String()+"/"+tt.name),
e2e.WithProfile(p),
e2e.WithCommand("pull"),
e2e.WithArgs("--force", "--platform", tt.platform, tmpSIF, tt.uri),
e2e.ExpectExit(tt.exit),
e2e.PostRun(func(t *testing.T) {
if t.Failed() || tt.exit != 0 {
return
}
if p.OCI() {
checkOCISIFPlatform(t, tmpSIF, tt.platform)
} else {
checkNativeSIFPlatform(t, tmpSIF, tt.platform)
}
}),
)
}
}
}
func checkOCISIFPlatform(t *testing.T, imgPath, platform string) {
wantPlatform, err := v1.ParsePlatform(platform)
if err != nil {
t.Errorf("while parsing platform %v", err)
}
fi, err := sif.LoadContainerFromPath(imgPath, sif.OptLoadWithFlag(os.O_RDONLY))
defer fi.UnloadContainer()
if err != nil {
t.Errorf("while loading SIF: %v", err)
}
ix, err := ocisif.ImageIndexFromFileImage(fi)
if err != nil {
t.Errorf("while obtaining image index: %v", err)
}
idxManifest, err := ix.IndexManifest()
if err != nil {
t.Errorf("while obtaining index manifest: %v", err)
}
if len(idxManifest.Manifests) != 1 {
t.Errorf("image has multiple manifests")
}
imageDigest := idxManifest.Manifests[0].Digest
img, err := ix.Image(imageDigest)
if err != nil {
t.Errorf("while initializing image: %v", err)
}
cfg, err := img.ConfigFile()
if err != nil {
t.Errorf("while fetching image config: %v", err)
}
if !cfg.Platform().Equals(*wantPlatform) {
t.Errorf("wrong platform - wanted %q, got %q", wantPlatform.String(), cfg.Platform().String())
}
}
func checkNativeSIFPlatform(t *testing.T, imgPath, platform string) {
wantPlatform, err := v1.ParsePlatform(platform)
if err != nil {
t.Errorf("while parsing platform %v", err)
}
fi, err := sif.LoadContainerFromPath(imgPath, sif.OptLoadWithFlag(os.O_RDONLY))
defer fi.UnloadContainer()
if err != nil {
t.Errorf("while loading SIF: %v", err)
}
d, err := fi.GetDescriptor(sif.WithPartitionType(sif.PartPrimSys))
if err != nil {
t.Errorf("while getting primary partition: %v", err)
}
_, _, arch, _ := d.PartitionMetadata() //nolint:dogsled
if arch != wantPlatform.Architecture {
t.Errorf("wrong architecture - wanted %q, got %q", wantPlatform.Architecture, arch)
}
}
// Test that we can perform cross-architecture builds from Dockerfile using buildkit
func (c ctx) testDockerCrossArchBk(t *testing.T) {
tmpdir, tmpdirCleanup := e2e.MakeTempDir(t, "", "dockerfile_crossarch_", "dir")
t.Cleanup(func() {
if !t.Failed() {
tmpdirCleanup(t)
}
})
dockerfile, err := e2e.WriteTempFile(tmpdir, "Dockerfile", `
FROM alpine
CMD /bin/true
`)
if err != nil {
t.Fatalf("While trying to create temporary Dockerfile: %v", err)
}
arch := getNonNativeArch()
profiles := []e2e.Profile{e2e.OCIUserProfile, e2e.OCIRootProfile}
for _, profile := range profiles {
imgPath := filepath.Join(tmpdir, "image."+profile.String()+".oci.sif")
c.env.RunSingularity(
t,
e2e.AsSubtest(profile.String()),
e2e.WithProfile(profile),
e2e.WithCommand("build"),
e2e.WithArgs("--arch", arch, imgPath, dockerfile),
e2e.ExpectExit(0),
e2e.PostRun(func(t *testing.T) {
verifyImgArch(t, imgPath, arch)
}),
)
}
}
func getNonNativeArch() string {
nativeArch := runtime.GOARCH
switch nativeArch {
case "amd64":
return "arm64"
default:
return "amd64"
}
}
func verifyImgArch(t *testing.T, imgPath, arch string) {
fi, err := sif.LoadContainerFromPath(imgPath, sif.OptLoadWithFlag(os.O_RDONLY))
if err != nil {
t.Fatalf("while loading SIF (%s): %v", imgPath, err)
}
defer fi.UnloadContainer()
ix, err := ocisif.ImageIndexFromFileImage(fi)
if err != nil {
t.Fatalf("while obtaining image index from %s: %v", imgPath, err)
}
idxManifest, err := ix.IndexManifest()
if err != nil {
t.Fatalf("while obtaining index manifest from %s: %v", imgPath, err)
}
if len(idxManifest.Manifests) != 1 {
t.Fatalf("while reading %s: single manifest expected, found %d manifests", imgPath, len(idxManifest.Manifests))
}
imageDigest := idxManifest.Manifests[0].Digest
img, err := ix.Image(imageDigest)
if err != nil {
t.Fatalf("while initializing image from %s: %v", imgPath, err)
}
cg, err := img.ConfigFile()
if err != nil {
t.Fatalf("while accessing config for %s: %v", imgPath, err)
}
assert.Equal(t, arch, cg.Architecture)
}
// Test support for SCIF containers in OCI mode
func (c ctx) testDockerSCIF(t *testing.T) {
tmpdir, tmpdirCleanup := e2e.MakeTempDir(t, "", "docker-scif-", "dir")
t.Cleanup(func() {
if !t.Failed() {
tmpdirCleanup(t)
}
})
scifRecipeFilename := "local_scif_recipe"
scifRecipeFullpath := filepath.Join(tmpdir, scifRecipeFilename)
scifRecipeSource := filepath.Join("..", "test", "defs", "scif_recipe")
if err := fs.CopyFile(scifRecipeSource, scifRecipeFullpath, 0o755); err != nil {
t.Fatalf("While trying to copy %q to %q: %v", scifRecipeSource, scifRecipeFullpath, err)
}
tmplValues := struct{ SCIFRecipeFilename string }{SCIFRecipeFilename: scifRecipeFilename}
scifDockerfile := tmpl.Execute(t, tmpdir, "Dockerfile-", filepath.Join("..", "test", "defs", "Dockerfile.scif.tmpl"), tmplValues)
scifImageFilename := "scif-image.oci.sif"
scifImageFullpath := filepath.Join(tmpdir, scifImageFilename)
// Uncomment when `singularity inspect --oci` for Docker-style SCIF
// containers is enabled.
// See: https://github.com/sylabs/singularity/pull/2360
// scifInspectOutAllPath := filepath.Join("..", "test", "defs", "scif_recipe.inspect_output.all")
// scifInspectOutAllBytes, err := os.ReadFile(scifInspectOutAllPath)
// if err != nil {
// t.Fatalf("While trying to read contents of %s: %v", scifInspectOutAllPath, err)
// }
// scifInspectOutOnePath := filepath.Join("..", "test", "defs", "scif_recipe.inspect_output.one")
// scifInspectOutOneBytes, err := os.ReadFile(scifInspectOutOnePath)
// if err != nil {
// t.Fatalf("While trying to read contents of %s: %v", scifInspectOutOnePath, err)
// }
// testInspectOutput := func(bytes []byte) func(t *testing.T, r *e2e.SingularityCmdResult) {
// return func(t *testing.T, r *e2e.SingularityCmdResult) {
// got := string(r.Stdout)
// assert.Equal(t, got, string(bytes))
// }
// }
c.env.RunSingularity(
t,
e2e.AsSubtest("build"),
e2e.WithProfile(e2e.OCIUserProfile),
e2e.WithCommand("build"),
e2e.WithDir(tmpdir),
e2e.WithArgs(scifImageFilename, scifDockerfile),
e2e.ExpectExit(0),
)
tests := []struct {
name string
cmd string
app string
preArgs []string
args []string
expects []e2e.SingularityCmdResultOp
expectExit int
}{
{
name: "run echo",
cmd: "run",
app: "hello-world-echo",
expects: []e2e.SingularityCmdResultOp{
e2e.ExpectOutput(e2e.ContainMatch, "The best app is hello-world-echo"),
},
expectExit: 0,
},
{
name: "exec echo",
cmd: "exec",
app: "hello-world-echo",
args: []string{"echo", "This is different text that should still include [e]SCIF_APPNAME"},
expects: []e2e.SingularityCmdResultOp{
e2e.ExpectOutput(e2e.ContainMatch, "This is different text that should still include hello-world-echo"),
},
expectExit: 0,
},
{
name: "run script",
cmd: "run",
app: "hello-world-script",
expects: []e2e.SingularityCmdResultOp{
e2e.ExpectOutput(e2e.ContainMatch, "Hello World!"),
},
expectExit: 0,
},
{
name: "exec script",
cmd: "exec",
app: "hello-world-script",
args: []string{"echo", "This is different text that should still include [e]SCIF_APPNAME"},
expects: []e2e.SingularityCmdResultOp{
e2e.ExpectOutput(e2e.ContainMatch, "This is different text that should still include hello-world-script"),
},
expectExit: 0,
},
{
name: "exec script2",
cmd: "exec",
app: "hello-world-script",
args: []string{"/bin/bash hello-world.sh"},
expects: []e2e.SingularityCmdResultOp{
e2e.ExpectOutput(e2e.ContainMatch, "Hello World!"),
},
expectExit: 0,
},
// Uncomment when `singularity inspect --oci` for Docker-style SCIF
// containers is enabled.
// See: https://github.com/sylabs/singularity/pull/2360
// {
// name: "insp all",
// cmd: "inspect",
// preArgs: []string{"--oci", "--list-apps"},
// expects: []e2e.SingularityCmdResultOp{
// testInspectOutput(scifInspectOutAllBytes),
// },
// expectExit: 0,
// }, {
// name: "insp one",
// cmd: "inspect",
// app: "hello-world-script",
// preArgs: []string{"--oci"},
// expects: []e2e.SingularityCmdResultOp{
// testInspectOutput(scifInspectOutOneBytes),
// },
// expectExit: 0,
// },
}
for _, tt := range tests {
args := tt.preArgs[:]
if tt.app != "" {
args = append(args, "--app", tt.app)
}
args = append(args, scifImageFullpath)
if len(tt.args) > 0 {
args = append(args, tt.args...)
}
c.env.RunSingularity(
t,
e2e.AsSubtest(tt.name),
e2e.WithProfile(e2e.OCIUserProfile),
e2e.WithCommand(tt.cmd),
e2e.WithArgs(args...),
e2e.ExpectExit(0, tt.expects...),
)
}
}
// E2ETests is the main func to trigger the test suite
func E2ETests(env e2e.TestEnv) testhelper.Tests {
c := ctx{
env: env,
}
np := testhelper.NoParallel
return testhelper.Tests{
// Run most docker:// source tests sequentially amongst themselves, so we
// don't hit DockerHub massively in parallel, and we benefit from
// caching as the same images are used frequently.
"ordered": func(t *testing.T) {
t.Run("AUFS", c.testDockerAUFS)
t.Run("def file", c.testDockerDefFile)
t.Run("permissions", c.testDockerPermissions)
t.Run("pulls", c.testDockerPulls)
t.Run("whiteout symlink", c.testDockerWhiteoutSymlink)
t.Run("labels", c.testDockerLabels)
t.Run("cmd", c.testDockerCMD)
t.Run("entrypoint", c.testDockerENTRYPOINT)
t.Run("cmdentrypoint", c.testDockerCMDENTRYPOINT)
t.Run("cmd quotes", c.testDockerCMDQuotes)
t.Run("user workdir", c.testDockerUSERWORKDIR)
t.Run("platform", c.testDockerPlatform)
t.Run("crossarch buildkit", c.testDockerCrossArchBk)
t.Run("scif", c.testDockerSCIF)
// Regressions
t.Run("issue 4524", c.issue4524)
t.Run("issue 1286", c.issue1286)
t.Run("issue 1528", c.issue1528)
t.Run("issue 1586", c.issue1586)
t.Run("issue 1670", c.issue1670)
},
// Tests that are especially slow, or run against a local docker
// registry, can be run in parallel, with `--disable-cache` used within
// them to avoid docker caching concurrency issues.
"docker host": c.testDockerHost,
"cred prio": np(c.testDockerCredsPriority),
"registry": c.testDockerRegistry,
// Regressions
"issue 4943": c.issue4943,
"issue 5172": c.issue5172,
"issue 274": c.issue274, // https://github.com/sylabs/singularity/issues/274
"issue 1704": c.issue1704, // https://github.com/sylabs/singularity/issues/1704
}
}
|