~townsend/libertine-testing/trunk

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
#!/usr/bin/python3

import lxc
import os
import shlex

from testtools import TestCase
from testtools.matchers import Equals, NotEquals


CONTAINER_NAME = "libertine-smoke-test"


def add_libertine_devel_ppa(container):
    rootfs_path = os.path.join(lxc.default_config_path, CONTAINER_NAME, 'rootfs')

    with open(os.path.join(rootfs_path, "etc", "apt", "sources.list"), "a") as fd:
        fd.write("deb http://ppa.launchpad.net/libertine-team/devel/ubuntu xenial main\n")

    
def start_smoke_test_lxc(container):
    if not container.running:
        if not container.start():
            raise RuntimeError("Container failed to start")
        if not container.wait("RUNNING", 10):
            raise RuntimeError("Container failed to enter the RUNNING state")

    if not container.get_ips(timeout=30):
        raise RuntimeError("Not able to connect to the network.")


def destroy_smoke_test_lxc(container):
    if container.defined:
        if container.running:
            container.stop()
        container.destroy()


def create_smoke_test_lxc(container):
    container.create("download", 0,
                     {"dist": "ubuntu",
                      "release": "xenial",
                      "arch": "amd64"})


def update_smoke_test_lxc(container):
    cmd = "bash -c \"echo \"ubuntu:ubuntu\" | chpasswd\"" 
    cmd_args = shlex.split(cmd)
    container.attach_wait(lxc.attach_run_command, cmd_args)

    add_libertine_devel_ppa(container)

    cmd = "apt-key adv --keyserver keyserver.ubuntu.com --recv-keys B8951985"
    cmd_args = shlex.split(cmd)
    container.attach_wait(lxc.attach_run_command, cmd_args)

    cmd = "apt-get update"
    cmd_args = shlex.split(cmd)
    container.attach_wait(lxc.attach_run_command, cmd_args)

    cmd = "apt-get dist-upgrade -y"
    cmd_args = shlex.split(cmd)
    container.attach_wait(lxc.attach_run_command, cmd_args)

    cmd = "apt-get install -y libertine-tools python3-libertine-chroot"
    cmd_args = shlex.split(cmd)
    container.attach_wait(lxc.attach_run_command, cmd_args)


def create_host_container():
    container = lxc.Container(CONTAINER_NAME)

    destroy_smoke_test_lxc(container)

    if not container.defined:
        container = lxc.Container(CONTAINER_NAME)

    create_smoke_test_lxc(container)

    start_smoke_test_lxc(container)

    update_smoke_test_lxc(container)

    return container


class TestLibertineChroot(TestCase):
    def setUp(self):
        super(TestLibertineChroot, self).setUp()
        self.container = create_host_container()

    def test_01_create_libertine_chroot(self):
        cmd = "bash -c \"sudo -u ubuntu -H libertine-container-manager create -i xenial-chroot -n Chroot -t chroot -d xenial\""
        cmd_args = shlex.split(cmd)
        ret = self.container.attach_wait(lxc.attach_run_command, cmd_args)

        self.assertThat(ret, Equals(0))


class TestLibertineLxc(TestCase):
    def setUp(self):
        super(TestLibertineLxc, self).setUp()
        self.container = create_host_container()

    def test_01_create_libertine_lxc(self):
        cmd = "bash -c \"sudo -u ubuntu -H libertine-container-manager create -i xenial-lxc -n LXC -t lxc -d xenial\""
        cmd_args = shlex.split(cmd)
        ret = self.container.attach_wait(lxc.attach_run_command, cmd_args)

        self.assertThat(ret, Equals(0))