summaryrefslogtreecommitdiff
path: root/tests/components/config.py
blob: c683a11faeee3821b2901c03c665c91b7c3bd0a7 (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
import logging
import os
from shutil import copyfile
from subprocess import run


class Config:

    def __init__(self, request, tmpdir, worker_id):
        self.tmpdir = tmpdir.strpath
        self.data_home = os.path.join(self.tmpdir, 'data')

        # workaround for https://github.com/pytest-dev/pytest-services/issues/37
        logger = logging.getLogger(
            '[{worker_id}] {name}'.format(name="pytest_services.log", worker_id=worker_id))
        logger.handlers.clear()

        # copy config file from template
        self.conf = tmpdir.join("test.conf").strpath
        template = os.path.join(os.path.dirname(__file__), 'template.ini')
        copyfile(template, self.conf)

        # set TALER_HOME base dir
        config_cmd = ["taler-config", "-c", self.conf]
        run(config_cmd + ["-s", "PATHS", "-o", "TALER_HOME", "-V", self.tmpdir], check=True)

        # get path of exchange private key file and create key pair
        config_cmd = ["taler-config", "-c", self.conf]
        r = run(config_cmd + ["-f", "-s", "EXCHANGE", "-o", "MASTER_PRIV_FILE"],
                capture_output=True, check=True, text=True)
        master_priv_file = r.stdout.rstrip()
        master_priv_dir = os.path.dirname(master_priv_file)
        os.makedirs(master_priv_dir)
        run(["gnunet-ecc", "-g1", master_priv_file], check=True, capture_output=True)
        r = run(["gnunet-ecc", "-p", master_priv_file], check=True, capture_output=True, text=True)
        self.master_pub = r.stdout.rstrip()

        # write exchange public key into config
        run(config_cmd + ["-s", "exchange",
                          "-o", "MASTER_PUBLIC_KEY",
                          "-V", self.master_pub], check=True)
        run(config_cmd + ["-s", "merchant-exchange-default",
                          "-o", "MASTER_KEY",
                          "-V", self.master_pub], check=True)

        # write DB name into config
        self.db = "test-db"
        db_uri = "postgres:///" + self.db
        run(config_cmd + ["-s", "exchangedb-postgres", "-o", "CONFIG", "-V", db_uri], check=True)
        run(config_cmd + ["-s", "auditordb-postgres", "-o", "CONFIG", "-V", db_uri], check=True)
        run(config_cmd + ["-s", "merchantdb-postgres", "-o", "CONFIG", "-V", db_uri], check=True)
        run(config_cmd + ["-s", "bank", "-o", "database", "-V", db_uri], check=True)

        # create new DB
        run(["dropdb", self.db], capture_output=True)
        run(["createdb", self.db], check=True)

        # drop DB when test ends
        def finalize():
            run(["dropdb", self.db], capture_output=True)

        request.addfinalizer(finalize)