~seb128/click/initctl-not-there

« back to all changes in this revision

Viewing changes to tests/integration/test_integration.py

  • Committer: CI bot
  • Author(s): Colin Watson
  • Date: 2014-06-23 11:31:40 UTC
  • mfrom: (425.1.47 devel)
  • Revision ID: ps-jenkins@lists.canonical.com-20140623113140-e129f85zscm2uqrs
Click 0.4.27: Integration test improvements; handle renaming of qtsensors5-dev to libqt5sensors5-dev in Qt 5.3. Fixes: 1328486

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
 
16
16
"""Integration tests for the click CLI interface."""
17
17
 
18
 
import copy
19
 
import contextlib
20
 
import glob
21
18
import json
22
19
import os
23
 
import random
24
20
import re
25
21
import shutil
26
 
import string
27
22
import subprocess
28
 
import sys
 
23
import tarfile
29
24
import tempfile
30
25
import unittest
31
26
 
32
 
 
33
 
@contextlib.contextmanager
34
 
def chdir(target):
35
 
    curdir = os.getcwd()
36
 
    os.chdir(target)
37
 
    yield
38
 
    os.chdir(curdir)
39
 
 
40
 
 
41
 
class TestCase(unittest.TestCase):
42
 
 
43
 
    @classmethod
44
 
    def setUpClass(cls):
45
 
        cls.click_binary = os.path.abspath(
46
 
            os.path.join(sys.argv[0], "..", "bin", "click"))
47
 
 
48
 
    def setUp(self):
49
 
        self.saved_env = copy.copy(os.environ)
50
 
        os.environ["PYTHONPATH"] = os.path.abspath(
51
 
            os.path.join(sys.argv[0], ".."))
52
 
 
53
 
    def tearDown(self):
54
 
        os.environ = self.saved_env
55
 
 
56
 
    def _make_click(self, name=None, version=1.0):
57
 
        if name is None:
58
 
            name = "com.ubuntu.%s" % "".join(
59
 
                random.choice(string.ascii_lowercase) for i in range(10))
60
 
        tmpdir = tempfile.mkdtemp()
61
 
        self.addCleanup(lambda: shutil.rmtree(tmpdir))
62
 
        clickdir = os.path.join(tmpdir, name)
63
 
        os.makedirs(clickdir)
64
 
        with open(os.path.join(clickdir, "manifest.json"), "w") as f:
65
 
            f.write("""{
66
 
            "name": "%s",
67
 
            "version": "%s",
68
 
            "maintainer": "Foo Bar <foo@example.org>",
69
 
            "title": "test title",
70
 
            "framework": "ubuntu-sdk-13.10"
71
 
            }""" % (name, version))
72
 
        with open(os.path.join(clickdir, "README"), "w") as f:
73
 
            f.write("hello world!")
74
 
        with chdir(tmpdir), open(os.devnull, "w") as devnull:
75
 
            subprocess.call(["click", "build", clickdir], stdout=devnull)
76
 
        generated_clicks = glob.glob(os.path.join(tmpdir, "*.click"))
77
 
        self.assertEqual(len(generated_clicks), 1)
78
 
        return generated_clicks[0]
 
27
from .helpers import (
 
28
    chdir,
 
29
    TestCase,
 
30
)
79
31
 
80
32
 
81
33
class TestBuild(TestCase):
113
65
        self.assertTrue(re.search(
114
66
            r'-rw-r[-w]-r-- root/root\s+[0-9]+\s+[0-9-]+ [0-9:]+ ./README', output))
115
67
 
116
 
 
117
 
@unittest.skipIf(
118
 
    os.getuid() != 0, "This tests needs to run as root")
119
 
@unittest.skipIf(
120
 
    subprocess.call(
121
 
        ["ping", "-c1", "archive.ubuntu.com"]) != 0, "Need network")
122
 
class TestChroot(TestCase):
123
 
 
124
 
    @classmethod
125
 
    def setUpClass(cls):
126
 
        super(TestChroot, cls).setUpClass()
127
 
        cls.arch = subprocess.check_output(
128
 
            ["dpkg", "--print-architecture"], universal_newlines=True).strip()
129
 
        subprocess.check_call([
130
 
            cls.click_binary,
131
 
            "chroot", "-a", cls.arch,
132
 
            "create"])
133
 
 
134
 
    @classmethod
135
 
    def tearDownClass(cls):
136
 
        subprocess.check_call([
137
 
            cls.click_binary,
138
 
            "chroot", "-a", cls.arch,
139
 
            "destroy"])
140
 
 
141
 
    def test_upgrade(self):
142
 
        subprocess.check_call([
143
 
            self.click_binary, "chroot", "-a", self.arch,
144
 
            "upgrade"])
145
 
 
146
 
    def test_install(self):
147
 
        subprocess.check_call([
148
 
            self.click_binary, "chroot", "-a", self.arch,
149
 
            "install", "apt-utils"])
150
 
 
151
 
    def test_run(self):
152
 
        output = subprocess.check_output([
153
 
            self.click_binary, "chroot", "-a", self.arch,
154
 
            "run", "echo", "hello world"], universal_newlines=True)
155
 
        self.assertEqual(output, "hello world\n")
156
 
 
157
 
    def test_maint(self):
158
 
        output = subprocess.check_output([
159
 
            self.click_binary, "chroot", "-a", self.arch,
160
 
            "maint", "id"], universal_newlines=True)
161
 
        self.assertEqual(output, "uid=0(root) gid=0(root) groups=0(root)\n")
 
68
@unittest.skipIf(
 
69
    (not os.path.exists("/usr/share/click/frameworks") or 
 
70
     not os.listdir("/usr/share/click/frameworks")),
 
71
    "Please install ubuntu-sdk-libs")
 
72
class TestFrameworks(TestCase):
 
73
    def test_framework_list(self):
 
74
        output = subprocess.check_output([
 
75
            self.click_binary, "framework", "list"], universal_newlines=True)
 
76
        self.assertTrue("ubuntu-sdk-" in output)
 
77
 
 
78
 
 
79
class TestBuildSource(TestCase):
 
80
    def test_buildsource(self):
 
81
        temp_dir = tempfile.mkdtemp()
 
82
        self.addCleanup(shutil.rmtree, temp_dir)
 
83
        with chdir(temp_dir):
 
84
            with open(os.path.join(temp_dir, "README"), "w") as f:
 
85
                f.write("I'm a source package")
 
86
            os.mkdir(os.path.join(temp_dir, ".git"))
 
87
            os.mkdir(os.path.join(temp_dir, ".bzr"))
 
88
            os.mkdir(os.path.join(temp_dir, ".normal"))
 
89
            self._create_manifest(os.path.join(temp_dir, "manifest.json"),
 
90
                                  "srcfoo", "1.2", "ubuntu-sdk-13.10")
 
91
            subprocess.check_call(
 
92
                [self.click_binary, "buildsource", temp_dir],
 
93
                universal_newlines=True)
 
94
            # ensure we have the content we expect
 
95
            source_file = "srcfoo_1.2.tar.gz"
 
96
            tar = tarfile.open(source_file)
 
97
            self.assertEqual(
 
98
                sorted(tar.getnames()),
 
99
                sorted([".", "./.normal", "./manifest.json", "./README"]))
 
100