summaryrefslogtreecommitdiff
path: root/test-suite
blob: aca851d9962298f7d37b4724f701b023afa1501c (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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
#!/usr/bin/python3

import argparse
import json
import logging
import os
import shutil
import subprocess
import sys
import tempfile
import yaml


class Suite:
    def __init__(self, tmp):
        self.tmp = tmp
        self.rad = Rad(os.path.join(tmp, "dot-radicle"))
        self.rad.auth()

        self.config = Config(tmp)
        self.config.write()

    def assert_triggered(self, msg):
        assert msg["response"] == "triggered"

    def assert_success(self, msg):
        assert msg == {"response": "finished", "result": "success"}

    def assert_failure(self, msg):
        assert msg["response"] == "finished"
        assert msg["result"] == "failure"

    def assert_error(self, msg):
        assert msg["response"] == "finished"
        res = msg["result"]
        assert isinstance(res, dict)
        assert "error" in res

    def run_all_test_cases(self, tests):
        methods = self._test_methods()
        chosen = self._chosen(methods, tests)
        for meth in methods:
            if meth in chosen:
                info(f"test case {meth}")
                getattr(self, meth)()
            else:
                info(f"skipping test {meth}")

    def _test_methods(self):
        return [meth for meth in dir(self) if meth.startswith("test_")]

    def _chosen(self, methods, tests):
        if tests:
            chosen = set()
            for test in tests:
                for meth in methods:
                    if test in meth:
                        chosen.add(meth)
        else:
            chosen = set(methods)
        return chosen

    def _create_git_repo(self, repo_name):
        git = Git(os.path.join(self.tmp, repo_name))
        git.init()
        git.write("README.md", "README")
        git.commit("first commit")
        return git

    def _create_valid_native_yaml(self, git, shell):
        native = NativeYaml(shell)
        git.write(".radicle/native.yaml", native.yaml())
        git.commit("add native.yaml")

    def _get_repo_info(self, git):
        commit = git.head()
        self.rad.init(git)
        rid = self.rad.rid(git)
        return rid, commit

    def _setup(self, repo_name, shell):
        git = self._create_git_repo(repo_name)
        self._create_valid_native_yaml(git, shell)
        return self._get_repo_info(git)

    def _create_ci(self):
        return NativeCI(self.rad, self.config)

    def _create_valid_trigger(self, rid, commit):
        return Trigger(rid, commit)

    def _run_ci(self, trigger, ci, rid, commit):
        exit, resps, stderr = ci.run(trigger)
        debug(f"exit: {exit}")
        debug(f"responses: {resps}")
        return exit, resps, stderr

    def _test_case(self, repo_name, shell):
        rid, commit = self._setup(repo_name, shell)
        ci = self._create_ci()
        trigger = self._create_valid_trigger(rid, commit)
        return self._run_ci(trigger, ci, rid, commit)

    def test_happy_path(self):
        exit, resps, stderr = self._test_case("happy-path", "echo hello, world")
        assert exit == 0
        assert len(resps) == 2
        self.assert_triggered(resps[0])
        self.assert_success(resps[1])

    def test_without_config_env_var(self):
        git = self._create_git_repo("no-config")
        self._create_valid_native_yaml(git, "echo hello world")
        rid, commit = self._get_repo_info(git)
        trigger = Trigger(rid, commit)
        ci = self._create_ci()
        ci.without_config()
        exit, resps, stderr = ci.run(trigger)
        debug(f"exit: {exit}")
        debug(f"responses: {resps}")
        assert exit != 0
        assert len(resps) == 0
        assert "RADICLE_NATIVE_CI" in stderr

    def test_config_missing(self):
        git = self._create_git_repo("config-missing")
        self._create_valid_native_yaml(git, "echo hello world")
        rid, commit = self._get_repo_info(git)
        trigger = Trigger(rid, commit)
        cfg2 = Config("no-config-file")
        ci = NativeCI(self.rad, cfg2)
        exit, resps, stderr = ci.run(trigger)
        debug(f"exit: {exit}")
        debug(f"responses: {resps}")
        assert exit != 0
        assert len(resps) == 0
        assert "no-config-file" in stderr

    def test_command_fails(self):
        exit, resps, stderr = self._test_case("cmd-fails", "false")
        assert exit == 1
        assert len(resps) == 2
        self.assert_triggered(resps[0])
        self.assert_failure(resps[1])

    def test_repository_does_not_exist(self):
        rid = "rad:z3aaaaaaaaaaaaaaaaaaaaaaaaaaa"
        commit = "8d947e182b096ec009e1c9eda9e6a67f5eef83d9"

        ci = self._create_ci()
        trigger = self._create_valid_trigger(rid, commit)
        exit, resps, stderr = self._run_ci(trigger, ci, rid, commit)

        assert exit == 2

    def test_commit_does_not_exist(self):
        git = self._create_git_repo("commit-missing")
        self._create_valid_native_yaml(git, "echo hello world")
        rid, _commit = self._get_repo_info(git)
        commit = "0000000000000000000000000000000000000000"
        trigger = Trigger(rid, commit)
        ci = self._create_ci()
        exit, resps, stderr = ci.run(trigger)

        assert exit == 1
        assert len(resps) == 2
        self.assert_triggered(resps[0])
        self.assert_failure(resps[1])

    def test_no_message(self):
        git = self._create_git_repo("no-message")
        self._create_valid_native_yaml(git, "echo hello world")
        ci = self._create_ci()
        exit, resps, stderr = ci.run_without_request()

        assert exit != 0
        assert len(resps) == 0

    def test_malformed_trigger(self):
        git = self._create_git_repo("malformed-trigger")
        self._create_valid_native_yaml(git, "echo hello world")
        rid, _commit = self._get_repo_info(git)
        trigger = MalformedTrigger()
        ci = self._create_ci()
        exit, resps, stderr = ci.run(trigger)

        assert exit != 0
        assert len(resps) == 0

    def test_native_yaml_has_no_shell(self):
        exit, resps, stderr = self._test_case("no-shell", None)
        assert exit == 1
        assert len(resps) == 2
        self.assert_triggered(resps[0])
        self.assert_failure(resps[1])

    def test_native_yaml_shell_is_not_string(self):
        exit, resps, stderr = self._test_case("shell-not-string", {"foo": "bar"})
        assert exit == 1
        assert len(resps) == 2
        self.assert_triggered(resps[0])
        self.assert_failure(resps[1])

    def test_command_takes_too_long(self):
        git = self._create_git_repo("command-takes-too-long")
        self._create_valid_native_yaml(git, "sleep 5")
        rid, commit = self._get_repo_info(git)
        trigger = Trigger(rid, commit)
        ci = self._create_ci()
        exit, resps, stderr = ci.run(trigger)
        assert exit == 1
        assert len(resps) == 2
        self.assert_triggered(resps[0])
        self.assert_failure(resps[1])
        assert "124" in stderr


class Git:
    def __init__(self, path):
        self.path = path

    def filename(self, relative):
        return os.path.join(self.path, "./" + relative)

    def write(self, relative, data):
        write(self.filename(relative), data)

    def _git(self, argv):
        return run(argv, cwd=self.path).stdout

    def init(self):
        assert not os.path.exists(self.path)
        run(["git", "init", self.path])
        debug(f"created git repository at {self.path}")

    def commit(self, msg):
        run(["git", "add", "."], cwd=self.path)
        run(["git", "commit", "-m", msg], cwd=self.path)

    def head(self):
        return self._git(["git", "rev-parse", "HEAD"]).strip()

    def ls_files(self):
        out = self._git(["git", "ls-files"])
        debug(f"ls-files:\n{out}")


class Rad:
    PASSPHRASE = "xyzzy"

    def __init__(self, rad_home):
        assert rad_home is not None
        self.rad_home = rad_home

    def _rad(self, argv, env=None, cwd=None):
        if env is None:
            env = {}
        env["RAD_HOME"] = self.rad_home
        env["RAD_PASSPHRASE"] = self.PASSPHRASE
        return run(argv, env=env, cwd=cwd).stdout

    def auth(self):
        self._rad(["rad", "auth", "--alias=test-node"])
        debug(f"created Radicle node at {self.rad_home}")

    def init(self, git):
        name = os.path.basename(git.path)
        self._rad(
            [
                "rad",
                "init",
                f"--name={name}",
                f"--description=test-repo",
                f"--public",
                f"--no-confirm",
            ],
            cwd=git.path,
        )
        debug(f"added git repository {git.path} to node")

    def rid(self, git):
        return self._rad(["rad", "."], cwd=git.path).strip()


class Config:
    def __init__(self, tmp):
        self.path = os.path.join(tmp, "config.yaml")
        self.dict = {
            "state": os.path.join(tmp, "state"),
            "log": os.path.join(tmp, "node-log.txt"),
            "timeout": 2,
        }

    def write(self):
        write(self.path, self.yaml())

    def yaml(self):
        return yaml.safe_dump(self.dict, indent=4)


class NativeYaml:
    def __init__(self, shell):
        self.dict = {}
        if shell is not None:
            self.dict["shell"] = shell

    def yaml(self):
        return yaml.safe_dump(self.dict, indent=4)


class Trigger:
    def __init__(self, rid, commit):
        self.rid = rid
        self.commit = commit

    def json(self):
        return json.dumps(
            {"request": "trigger", "repo": self.rid, "commit": self.commit}
        )


class MalformedTrigger:
    def json(self):
        return json.dumps({"request": "trigger"})


class NativeCI:
    def __init__(self, rad, config):
        self.rad_home = rad.rad_home
        self.config = config.path
        self.env = {
            "RAD_HOME": self.rad_home,
            "RADICLE_NATIVE_CI": self.config,
            "RADICLE_NATIVE_CI_LOG": "debug",
        }
        self.timeout = 1

    def without_config(self):
        del self.env["RADICLE_NATIVE_CI"]

    def run(self, request):
        p = run(
            [
                "cargo",
                "run",
                "-q",
            ],
            input=request.json(),
            env=dict(self.env),
            may_fail=True,
        )
        resps = [json.loads(line.strip()) for line in p.stdout.splitlines()]
        return p.returncode, resps, p.stderr

    def run_without_request(self):
        p = run(
            [
                "cargo",
                "run",
                "-q",
            ],
            stdin=subprocess.DEVNULL,
            env=dict(self.env),
            may_fail=True,
        )
        resps = [json.loads(line.strip()) for line in p.stdout.splitlines()]
        return p.returncode, resps, p.stderr


def debug(msg):
    logging.debug(msg)


def info(msg):
    logging.info(msg)


def die(msg, exc_info=None):
    logging.critical(msg, exc_info=exc_info)
    sys.exit(1)


def run(argv, env=None, cwd=None, stdin=None, input=None, may_fail=False):
    debug(f"run {argv} with env={env}, cwd={cwd} input={input!r}")
    if env is not None:
        for name, value in os.environ.items():
            if name not in env:
                env[name] = value
    p = subprocess.run(
        argv, capture_output=True, env=env, cwd=cwd, stdin=stdin, input=input, text=True
    )

    debug(f"stdout:\n{indent(p.stdout)}")
    debug(f"stderr:\n{indent(p.stderr)}")
    if p.returncode != 0 and not may_fail:
        die(f"command failed: {argv}")

    return p


def indent(s):
    return "".join([f"    {line}\n" for line in s.splitlines()])


def write(filename, data):
    dirname = os.path.dirname(filename)
    if not os.path.exists(dirname):
        os.makedirs(dirname)
    with open(filename, "w") as f:
        f.write(data)


def main(tmp, tests):
    suite = Suite(tmp)
    suite.run_all_test_cases(tests)


p = argparse.ArgumentParser()
p.add_argument("--verbose", action="store_true")
p.add_argument("--test", action="append", nargs="?", dest="tests")
args = p.parse_args()

if args.verbose:
    level = logging.DEBUG
    print(args)
else:
    level = logging.INFO


logging.basicConfig(
    level=level,
    stream=sys.stderr,
    format="%(levelname)s %(message)s",
    datefmt="%Y-%m-%d %H:%M:%S",
)

tmp = tempfile.mkdtemp()
debug(f"created temporary directory {tmp}")

try:
    main(tmp, args.tests)
except AssertionError as e:
    debug(f"removing temporary directory {tmp}")
    shutil.rmtree(tmp)
    die(f"ERROR: assertion failed: {e}", exc_info=True)
except Exception as e:
    debug(f"removing temporary directory {tmp}")
    shutil.rmtree(tmp)
    die(f"ERROR: uncaught exception {e}", exc_info=True)
else:
    debug(f"removing temporary directory {tmp}")
    shutil.rmtree(tmp)