summaryrefslogtreecommitdiffstats
path: root/nixos/tests/clickhouse/ui.nix
blob: 73050910d25b267a060f7dbfbb278eb35ec55e62 (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
{
  lib,
  pkgs,
  package,
  ...
}:

{
  name = "clickhouse-ui";

  nodes = {
    browser =
      {
        config,
        pkgs,
        ...
      }:
      {
        virtualisation.memorySize = 1024 * 2;

        environment.systemPackages =
          let
            clickhouseSeleniumScript =
              pkgs.writers.writePython3Bin "clickhouse-selenium-script"
                {
                  libraries = with pkgs.python3Packages; [ selenium ];
                }
                ''
                  from selenium import webdriver
                  from selenium.webdriver.common.by import By
                  from selenium.webdriver.firefox.options import Options
                  from selenium.webdriver.support import expected_conditions as EC
                  from selenium.webdriver.support.ui import WebDriverWait

                  options = Options()
                  options.add_argument("--headless")
                  service = webdriver.FirefoxService(executable_path="${lib.getExe pkgs.geckodriver}")  # noqa: E501

                  driver = webdriver.Firefox(options=options, service=service)
                  driver.implicitly_wait(10)
                  driver.get("http://clickhouse:8123/play")

                  wait = WebDriverWait(driver, 60)

                  wait.until(EC.presence_of_element_located(
                    (By.ID, "query_div")))

                  version = "${lib.strings.replaceStrings [ "-stable" "-lts" ] [ "" "" ] package.version}"
                  wait.until(EC.text_to_be_present_in_element(
                    (By.XPATH, "//span[@id='server_info']"), version))

                  # Determine if a shadow DOM is used for query-result and
                  # query-progress
                  result_els = driver.find_elements(By.ID, "query-result")
                  uses_shadow = len(result_els) > 0
                  if uses_shadow:
                      result_shadow = result_els[0].shadow_root
                      progress_shadow = driver.find_element(
                        By.ID, "query-progress").shadow_root


                  def find_rows():
                      root = result_shadow if uses_shadow else driver
                      return root.find_elements(
                        By.CSS_SELECTOR, ".row-number")


                  def find_check_mark():
                      root = progress_shadow if uses_shadow else driver
                      return root.find_elements(
                        By.CSS_SELECTOR, "#check-mark")


                  # Shouldn't show before query done
                  assert len(find_rows()) == 0

                  query_box = driver.find_element(
                    By.XPATH, "//textarea[@id='query']")
                  query_box.click()
                  query_box.send_keys("SELECT 1")

                  driver.find_element(
                    By.XPATH, "//button[@id='run']").click()

                  # Wait for query to complete
                  WebDriverWait(driver, 60).until(
                    lambda d: len(find_check_mark()) > 0)

                  # Wait for results to render
                  WebDriverWait(driver, 60).until(
                    lambda d: len(find_rows()) > 0)

                  driver.close()
                '';
          in
          with pkgs;
          [
            curl
            firefox-unwrapped
            geckodriver
            clickhouseSeleniumScript
          ];
      };

    clickhouse =
      { config, pkgs, ... }:
      {
        networking.firewall.allowedTCPPorts = [
          8123
          9000
        ];

        environment.etc = {
          "clickhouse-server/config.d/listen.xml".text = ''
            <clickhouse>
              <listen_host>::</listen_host>
            </clickhouse>
          '';
        };

        services.clickhouse = {
          enable = true;
          inherit package;
        };
      };
  };

  testScript = ''
    clickhouse.wait_for_unit("clickhouse")
    clickhouse.wait_for_open_port(8123)
    clickhouse.wait_for_open_port(9000)

    browser.systemctl("start network-online.target")
    browser.wait_for_unit("network-online.target")

    browser.succeed("curl -kLs http://clickhouse:8123/play | grep 'ClickHouse Query'")

    # Ensure the application is actually rendered by the Javascript
    browser.succeed("PYTHONUNBUFFERED=1 clickhouse-selenium-script")
  '';
}