summaryrefslogtreecommitdiff
path: root/tests/integration/source-determinism.py
blob: d1760c26703979bf287b5fad8148e4deb2453930 (plain)
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
import os
import pytest

from buildstream import _yaml, utils
from tests.testutils import create_repo, ALL_REPO_KINDS
from tests.testutils import cli_integration as cli


DATA_DIR = os.path.join(
    os.path.dirname(os.path.realpath(__file__)),
    "project"
)


def create_test_file(*path, mode=0o644, content='content\n'):
    path = os.path.join(*path)
    os.makedirs(os.path.dirname(path), exist_ok=True)
    with open(path, 'w') as f:
        f.write(content)
        os.fchmod(f.fileno(), mode)


def create_test_directory(*path, mode=0o644):
    create_test_file(*path, '.keep', content='')
    path = os.path.join(*path)
    os.chmod(path, mode)


@pytest.mark.integration
@pytest.mark.datafiles(DATA_DIR)
@pytest.mark.parametrize("kind", [(kind) for kind in ALL_REPO_KINDS] + ['local'])
def test_deterministic_source_umask(cli, tmpdir, datafiles, kind, integration_cache):
    project = str(datafiles)
    element_name = 'list'
    element_path = os.path.join(project, 'elements', element_name)
    repodir = os.path.join(str(tmpdir), 'repo')
    sourcedir = os.path.join(project, 'source')

    create_test_file(sourcedir, 'a.txt', mode=0o700)
    create_test_file(sourcedir, 'b.txt', mode=0o755)
    create_test_file(sourcedir, 'c.txt', mode=0o600)
    create_test_file(sourcedir, 'd.txt', mode=0o400)
    create_test_file(sourcedir, 'e.txt', mode=0o644)
    create_test_file(sourcedir, 'f.txt', mode=0o4755)
    create_test_file(sourcedir, 'g.txt', mode=0o2755)
    create_test_file(sourcedir, 'h.txt', mode=0o1755)
    create_test_directory(sourcedir, 'dir-a', mode=0o0700)
    create_test_directory(sourcedir, 'dir-c', mode=0o0755)
    create_test_directory(sourcedir, 'dir-d', mode=0o4755)
    create_test_directory(sourcedir, 'dir-e', mode=0o2755)
    create_test_directory(sourcedir, 'dir-f', mode=0o1755)

    if kind == 'local':
        source = {'kind': 'local',
                  'path': 'source'}
    else:
        repo = create_repo(kind, repodir)
        ref = repo.create(sourcedir)
        source = repo.source_config(ref=ref)
    element = {
        'kind': 'manual',
        'depends': [
            {
                'filename': 'base.bst',
                'type': 'build'
            }
        ],
        'sources': [
            source
        ],
        'config': {
            'install-commands': [
                'ls -l >"%{install-root}/ls-l"'
            ]
        }
    }
    _yaml.dump(element, element_path)

    def get_value_for_umask(umask):
        checkoutdir = os.path.join(str(tmpdir), 'checkout-{}'.format(umask))

        old_umask = os.umask(umask)

        try:
            result = cli.run(project=project, args=['build', element_name])
            result.assert_success()

            result = cli.run(project=project, args=['checkout', element_name, checkoutdir])
            result.assert_success()

            with open(os.path.join(checkoutdir, 'ls-l'), 'r') as f:
                return f.read()
        finally:
            os.umask(old_umask)
            cache_dir = os.path.join(integration_cache, 'artifacts')
            cli.remove_artifact_from_cache(project, element_name,
                                           cache_dir=cache_dir)

    assert get_value_for_umask(0o022) == get_value_for_umask(0o077)


@pytest.mark.integration
@pytest.mark.datafiles(DATA_DIR)
def test_deterministic_source_local(cli, tmpdir, datafiles, integration_cache):
    """Only user rights should be considered for local source.
    """
    project = str(datafiles)
    element_name = 'test'
    element_path = os.path.join(project, 'elements', element_name)
    sourcedir = os.path.join(project, 'source')

    element = {
        'kind': 'manual',
        'depends': [
            {
                'filename': 'base.bst',
                'type': 'build'
            }
        ],
        'sources': [
            {
                'kind': 'local',
                'path': 'source'
            }
        ],
        'config': {
            'install-commands': [
                'ls -l >"%{install-root}/ls-l"'
            ]
        }
    }
    _yaml.dump(element, element_path)

    def get_value_for_mask(mask):
        checkoutdir = os.path.join(str(tmpdir), 'checkout-{}'.format(mask))

        create_test_file(sourcedir, 'a.txt', mode=0o644 & mask)
        create_test_file(sourcedir, 'b.txt', mode=0o755 & mask)
        create_test_file(sourcedir, 'c.txt', mode=0o4755 & mask)
        create_test_file(sourcedir, 'd.txt', mode=0o2755 & mask)
        create_test_file(sourcedir, 'e.txt', mode=0o1755 & mask)
        create_test_directory(sourcedir, 'dir-a', mode=0o0755 & mask)
        create_test_directory(sourcedir, 'dir-b', mode=0o4755 & mask)
        create_test_directory(sourcedir, 'dir-c', mode=0o2755 & mask)
        create_test_directory(sourcedir, 'dir-d', mode=0o1755 & mask)
        try:
            result = cli.run(project=project, args=['build', element_name])
            result.assert_success()

            result = cli.run(project=project, args=['checkout', element_name, checkoutdir])
            result.assert_success()

            with open(os.path.join(checkoutdir, 'ls-l'), 'r') as f:
                return f.read()
        finally:
            cache_dir = os.path.join(integration_cache, 'artifacts')
            cli.remove_artifact_from_cache(project, element_name,
                                           cache_dir=cache_dir)

    assert get_value_for_mask(0o7777) == get_value_for_mask(0o0700)