1import os
2
3import infra.basetest
4
5
6class TestDockerCompose(infra.basetest.BRTest):
7    scripts = ["conf/docker-compose.yml",
8               "tests/package/sample_python_docker.py"]
9    config = \
10        """
11        BR2_x86_64=y
12        BR2_x86_corei7=y
13        BR2_TOOLCHAIN_EXTERNAL=y
14        BR2_TOOLCHAIN_EXTERNAL_BOOTLIN_X86_64_CORE_I7_GLIBC_STABLE=y
15        BR2_SYSTEM_DHCP="eth0"
16        BR2_ROOTFS_POST_BUILD_SCRIPT="{}"
17        BR2_ROOTFS_POST_SCRIPT_ARGS="{}"
18        BR2_LINUX_KERNEL=y
19        BR2_LINUX_KERNEL_CUSTOM_VERSION=y
20        BR2_LINUX_KERNEL_CUSTOM_VERSION_VALUE="4.19.262"
21        BR2_LINUX_KERNEL_USE_CUSTOM_CONFIG=y
22        BR2_LINUX_KERNEL_CUSTOM_CONFIG_FILE="{}"
23        BR2_PACKAGE_PYTHON3=y
24        BR2_PACKAGE_PYTHON_DOCKER=y
25        BR2_PACKAGE_CA_CERTIFICATES=y
26        BR2_PACKAGE_DOCKER_CLI=y
27        BR2_PACKAGE_DOCKER_COMPOSE=y
28        BR2_PACKAGE_DOCKER_ENGINE=y
29        BR2_TARGET_ROOTFS_EXT2=y
30        BR2_TARGET_ROOTFS_EXT2_SIZE="512M"
31        # BR2_TARGET_ROOTFS_TAR is not set
32        """.format(
33            infra.filepath("tests/package/copy-sample-script-to-target.sh"),
34            " ".join([infra.filepath(i) for i in scripts]),
35            infra.filepath("conf/docker-compose-kernel.config"))
36
37    def wait_for_dockerd(self):
38        # dockerd takes a while to start up
39        _, _ = self.emulator.run('while [ ! -e /var/run/docker.sock ]; do sleep 1; done', 120)
40
41    def docker_test(self):
42        # will download container if not available, which may take some time
43        self.assertRunOk('docker run --rm -p 8888:8888 busybox:latest /bin/true', 120)
44
45    def docker_compose_test(self):
46        # will download container if not available, which may take some time
47        self.assertRunOk('docker compose up -d --quiet-pull', 120)
48        # container may take some time to start
49        self.assertRunOk('while ! docker inspect root-busybox-1 2>&1 >/dev/null; do sleep 1; done', 120)
50        self.assertRunOk('wget -q -O /tmp/busybox http://127.0.0.1/busybox', 120)
51        self.assertRunOk('cmp /bin/busybox /tmp/busybox', 120)
52
53    def python_docker_test(self):
54        self.assertRunOk('python3 ./sample_python_docker.py', 120)
55
56    def test_run(self):
57        kernel = os.path.join(self.builddir, "images", "bzImage")
58        rootfs = os.path.join(self.builddir, "images", "rootfs.ext2")
59        self.emulator.boot(arch="x86_64",
60                           kernel=kernel,
61                           kernel_cmdline=["root=/dev/vda", "console=ttyS0"],
62                           options=["-cpu", "Nehalem",
63                                    "-m", "512M",
64                                    "-device", "virtio-rng-pci",
65                                    "-drive", "file={},format=raw,if=virtio".format(rootfs),
66                                    "-net", "nic,model=virtio",
67                                    "-net", "user"])
68        self.emulator.login()
69        self.wait_for_dockerd()
70        self.docker_test()
71        self.docker_compose_test()
72        self.python_docker_test()
73