File size: 4,780 Bytes
61d39e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#! /usr/bin/env python3

# test the client-replica feature
# - need browser environment (following features require browser environment: fs naive-cache, client-replica, wspush)
# - test multi-server setup
# - test change-propagation-time
# - test local read
# - test consistency

# first stage: test in the existing workspace, test single server + multiple sessions
# second stage: test from a fresh clone, test single server + multiple sessions
# third stage: test in the existing workspace, test multiple servers + multiple sessions
# fourth stage: test from a fresh clone, test multiple servers + multiple sessions

import time
import os
import json
import requests
import yaml

import cxc_toolkit

import common

ENABLE_FS_TREE_MANAGER = False
PUTER_ROOT = common.PUTER_ROOT


def init_backend_config():
    """

    TODO: replace with common.init_backend_config

    """
    # init config.json
    server_process = cxc_toolkit.exec.run_background("npm start")
    # wait 10s for the server to start
    time.sleep(10)
    server_process.terminate()

    example_config_path = f"{PUTER_ROOT}/volatile/config/config.json"
    config_path = f"{PUTER_ROOT}/volatile/config/config.json"

    # load
    with open(example_config_path, "r") as f:
        config = json.load(f)

    # update
    if ENABLE_FS_TREE_MANAGER:
        config["services"]["client-replica"] = {
            "enabled": True,
            "fs_tree_manager_url": "localhost:50052",
        }

    # write
    with open(config_path, "w") as f:
        json.dump(config, f, indent=2)


def init_fs_tree_manager_config():
    example_config_path = f"{PUTER_ROOT}/src/fs_tree_manager/example-config.yaml"
    config_path = f"{PUTER_ROOT}/src/fs_tree_manager/config.yaml"

    # load
    with open(example_config_path, "r") as f:
        config = yaml.safe_load(f)

    # update
    config["database"]["driver"] = "sqlite3"
    config["database"]["sqlite3"][
        "path"
    ] = f"{PUTER_ROOT}/volatile/runtime/puter-database.sqlite"

    # write
    with open(config_path, "w") as f:
        yaml.dump(config, f, default_flow_style=False, indent=2)

    print(f"fs-tree-manager config initialized at {config_path}")


def run():
    # =========================================================================
    # clean ports
    # =========================================================================

    # clean port 4100 for backend server
    cxc_toolkit.exec.run_command("fuser -k 4100/tcp", ignore_failure=True)

    # clean port 50052 for fs-tree-manager server
    cxc_toolkit.exec.run_command("fuser -k 50052/tcp", ignore_failure=True)

    # =========================================================================
    # config server
    # =========================================================================
    cxc_toolkit.exec.run_command("npm install")
    init_backend_config()
    admin_password = common.get_admin_password()

    # =========================================================================
    # start backend server
    # =========================================================================
    cxc_toolkit.exec.run_background(
        "npm start", work_dir=PUTER_ROOT, log_path="/tmp/backend.log"
    )
    # wait 10s for the server to start
    time.sleep(10)

    # =========================================================================
    # config client
    # =========================================================================
    token = common.get_token(admin_password)
    common.init_client_config(token)

    # =========================================================================
    # start fs-tree-manager server
    # =========================================================================
    if ENABLE_FS_TREE_MANAGER:
        init_fs_tree_manager_config()

        cxc_toolkit.exec.run_command(
            "go mod download",
            work_dir=f"{PUTER_ROOT}/src/fs_tree_manager",
        )

        cxc_toolkit.exec.run_background(
            "go run server.go",
            work_dir=f"{PUTER_ROOT}/src/fs_tree_manager",
            log_path="/tmp/fs-tree-manager.log",
        )

        # NB: "go mod download" and "go run server.go" may take a long time in github
        # action environment, I don't know why.
        time.sleep(60)

    # =========================================================================
    # run the test
    # =========================================================================
    cxc_toolkit.exec.run_command(
        "npx playwright test",
        work_dir=f"{PUTER_ROOT}/tests/playwright",
    )


if __name__ == "__main__":
    run()