summaryrefslogtreecommitdiff
path: root/tests/remoteexecution/workspace.py
blob: b525cefcd708a0b818a20017c5f9eca5436e4a24 (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
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
# Pylint doesn't play well with fixtures and dependency injection from pytest
# pylint: disable=redefined-outer-name

import os
import re
import shutil
import pytest

from buildstream.testing import cli_remote_execution as cli  # pylint: disable=unused-import
from buildstream.testing.integration import assert_contains

pytestmark = pytest.mark.remoteexecution


# subdirectories of the buildtree
SRC = "src"
DEPS = os.path.join(SRC, ".deps")
AUTO = "autom4te.cache"
DIRS = [os.sep + SRC, os.sep + DEPS, os.sep + AUTO]

DATA_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), "project")
MAIN = os.path.join(SRC, "main.c")
MAINO = os.path.join(SRC, "main.o")
CFGMARK = "config-time"
BLDMARK = "build-time"


def files():
    _input_files = [
        ".bstproject.yaml",
        "aclocal.m4",
        "missing",
        "README",
        "install-sh",
        "depcomp",
        "configure.ac",
        "compile",
        SRC,
        MAIN,
        os.path.join(SRC, "Makefile.am"),
        "Makefile.am",
    ]
    input_files = [os.sep + fname for fname in _input_files]

    _generated_files = [
        "Makefile",
        "Makefile.in",
        AUTO,
        os.path.join(AUTO, "traces.1"),
        os.path.join(AUTO, "traces.0"),
        os.path.join(AUTO, "requests"),
        os.path.join(AUTO, "output.0"),
        os.path.join(AUTO, "output.1"),
        "config.h",
        "config.h.in",
        "config.log",
        "config.status",
        "configure",
        "configure.lineno",
        os.path.join(SRC, "hello"),
        DEPS,
        os.path.join(DEPS, "main.Po"),
        os.path.join(SRC, "Makefile"),
        MAINO,
        CFGMARK,
        BLDMARK,
        os.path.join(SRC, "Makefile.in"),
        "stamp-h1",
    ]
    generated_files = [os.sep + fname for fname in _generated_files]

    _artifacts = [
        "usr",
        os.path.join("usr", "lib"),
        os.path.join("usr", "bin"),
        os.path.join("usr", "share"),
        os.path.join("usr", "bin", "hello"),
        os.path.join("usr", "share", "doc"),
        os.path.join("usr", "share", "doc", "amhello"),
        os.path.join("usr", "share", "doc", "amhello", "README"),
    ]
    artifacts = [os.sep + fname for fname in _artifacts]
    return input_files, generated_files, artifacts


def _get_mtimes(root):
    assert os.path.exists(root)
    # timestamps on subdirs are not currently semantically meaningful
    for dirname, _, filenames in os.walk(root):
        filenames.sort()
        for filename in filenames:
            fname = os.path.join(dirname, filename)
            yield fname[len(root) :], os.stat(fname).st_mtime


def get_mtimes(root):
    return dict(set(_get_mtimes(root)))


def check_buildtree(
    cli, project, element_name, input_files, generated_files, incremental=False,
):
    # check modified workspace dir was cached
    #   - generated files are present
    #   - generated files are newer than inputs
    #   - check the date recorded in the marker file
    #   - check that the touched file mtime is preserved from before

    assert cli and project and element_name and input_files and generated_files

    result = cli.run(
        project=project,
        args=[
            "shell",
            "--build",
            element_name,
            "--use-buildtree",
            "always",
            "--",
            "find",
            ".",
            "-mindepth",
            "1",
            "-exec",
            "stat",
            "-c",
            "%n::%Y",
            "{}",
            ";",
        ],
    )
    result.assert_success()

    buildtree = {}
    output = result.output.splitlines()

    typ_inptime = None
    typ_gentime = None

    for line in output:
        assert "::" in line
        fname, mtime = line.split("::")
        # remove the symbolic dir
        fname = fname[1:]
        mtime = int(mtime)
        buildtree[fname] = mtime

        if incremental:
            # directory timestamps are not meaningful
            if fname in DIRS:
                continue
            if fname in input_files:
                if fname != os.sep + MAIN and not typ_inptime:
                    typ_inptime = mtime
            if fname in generated_files:
                if fname != os.sep + MAINO and not typ_gentime:
                    typ_gentime = mtime

    # all expected files should have been found
    for filename in input_files + generated_files:
        assert filename in buildtree

    if incremental:
        # the source file was changed so should be more recent than other input files
        # it should be older than the main object.
        # The main object should be more recent than generated files.
        assert buildtree[os.sep + MAIN] > typ_inptime
        assert buildtree[os.sep + MAINO] > buildtree[os.sep + MAIN]
        assert buildtree[os.sep + MAINO] > typ_gentime

    for fname in DIRS:
        del buildtree[fname]

    return buildtree


def get_timemark(cli, project, element_name, marker):
    result = cli.run(
        project=project, args=["shell", "--build", element_name, "--use-buildtree", "always", "--", "cat", marker[1:]],
    )
    result.assert_success()
    marker_time = int(result.output)
    return marker_time


@pytest.mark.datafiles(DATA_DIR)
@pytest.mark.parametrize(
    "modification", [pytest.param("content"), pytest.param("time"),],
)
def test_workspace_build(cli, tmpdir, datafiles, modification):
    project = str(datafiles)
    checkout = os.path.join(cli.directory, "checkout")
    workspace = os.path.join(cli.directory, "workspace")
    element_name = "autotools/amhello.bst"

    # cli args
    artifact_checkout = ["artifact", "checkout", element_name, "--directory", checkout]
    build = ["--cache-buildtrees", "always", "build", element_name]
    input_files, generated_files, artifacts = files()

    services = cli.ensure_services()
    assert set(services) == set(["action-cache", "execution", "storage"])

    # open a workspace for the element in the workspace directory
    result = cli.run(project=project, args=["workspace", "open", "--directory", workspace, element_name])
    result.assert_success()

    # check that the workspace path exists
    assert os.path.exists(workspace)

    # add a file (asserting later that this is in the buildtree)
    newfile = "newfile.cfg"
    newfile_path = os.path.join(workspace, newfile)
    with open(newfile_path, "w") as fdata:
        fdata.write("somestring")
    input_files.append(os.sep + newfile)

    # check that the workspace *only* contains the expected input files
    assert_contains(workspace, input_files, strict=True)
    # save the mtimes for later comparison
    ws_times = get_mtimes(workspace)

    # build the element and cache the buildtree
    result = cli.run(project=project, args=build)
    result.assert_success()
    assert cli.get_element_state(project, element_name) == "cached"
    build_key = cli.get_element_key(project, element_name)

    # check that the local workspace is unchanged
    assert_contains(workspace, input_files, strict=True)
    assert ws_times == get_mtimes(workspace)

    # check modified workspace dir was cached and save the time
    # build was run. Incremental build conditions do not apply since the workspace
    # was initially opened using magic timestamps.
    build_times = check_buildtree(cli, project, element_name, input_files, generated_files, incremental=False)
    build_timemark = get_timemark(cli, project, element_name, (os.sep + BLDMARK))

    # check that the artifacts are available
    result = cli.run(project=project, args=artifact_checkout)
    result.assert_success()
    assert_contains(checkout, artifacts)
    shutil.rmtree(checkout)

    # rebuild the element
    result = cli.run(project=project, args=build)
    result.assert_success()
    assert cli.get_element_state(project, element_name) == "cached"
    rebuild_key = cli.get_element_key(project, element_name)
    assert rebuild_key == build_key
    rebuild_times = check_buildtree(cli, project, element_name, input_files, generated_files, incremental=False)
    rebuild_timemark = get_timemark(cli, project, element_name, (os.sep + BLDMARK))

    # buildmark time should be the same
    assert build_timemark == rebuild_timemark
    assert all([rebuild_times[fname] == build_times[fname] for fname in rebuild_times]), "{}\n{}".format(
        rebuild_times, build_times
    )

    # modify the open workspace and rebuild
    main_path = os.path.join(workspace, MAIN)
    assert os.path.exists(main_path)

    if modification == "time":
        # touch a file in the workspace and save the mtime
        os.utime(main_path)
        touched_time = int(os.stat(main_path).st_mtime)

    elif modification == "content":
        # change a source file (there's a race here but it's not serious)
        with open(main_path, "r") as fdata:
            data = fdata.readlines()
        with open(main_path, "w") as fdata:
            for line in data:
                fdata.write(re.sub(r"Hello", "Goodbye", line))
        touched_time = int(os.stat(main_path).st_mtime)

    # refresh input times
    ws_times = get_mtimes(workspace)

    # rebuild the element
    result = cli.run(project=project, args=build)
    result.assert_success()

    rebuild_times = check_buildtree(cli, project, element_name, input_files, generated_files, incremental=True)
    rebuild_timemark = get_timemark(cli, project, element_name, (os.sep + BLDMARK))
    assert rebuild_timemark > build_timemark

    # check the times of the changed files
    assert rebuild_times[os.sep + MAIN] == touched_time
    del rebuild_times[os.sep + MAIN]
    del rebuild_times[os.sep + MAINO]
    del rebuild_times[os.sep + SRC + os.sep + "hello"]
    del rebuild_times[os.sep + DEPS + os.sep + "main.Po"]
    del rebuild_times[os.sep + BLDMARK]

    # check the times of the unmodified files
    assert all([rebuild_times[fname] == build_times[fname] for fname in rebuild_times]), "{}\n{}".format(
        rebuild_times, build_times
    )

    # Check workspace is unchanged
    assert_contains(workspace, input_files, strict=True)
    assert ws_times == get_mtimes(workspace)