|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require_relative "clickhouse/version" |
| 4 | +require "testcontainers" |
| 5 | + |
| 6 | +module Testcontainers |
| 7 | + # ClickhouseContainer class is used to manage containers that runs a Clickhouse server |
| 8 | + # |
| 9 | + # @attr_reader [String] username used by the container |
| 10 | + # @attr_reader [String] password used by the container |
| 11 | + # @attr_reader [String] database used by the container |
| 12 | + class ClickhouseContainer < ::Testcontainers::DockerContainer |
| 13 | + # Default ports used by the container |
| 14 | + CLICKHOUSE_DEFAULT_PORT = 9000 |
| 15 | + CLICKHOUSE_DEFAULT_HTTP_PORT = 8123 |
| 16 | + |
| 17 | + # Default image used by the container |
| 18 | + CLICKHOUSE_DEFAULT_IMAGE = "clickhouse/clickhouse-server:latest" |
| 19 | + |
| 20 | + # Default credentials used by the container |
| 21 | + CLICKHOUSE_DEFAULT_USER = "default" |
| 22 | + CLICKHOUSE_DEFAULT_PASS = "password" |
| 23 | + CLICKHOUSE_DB = "default" |
| 24 | + |
| 25 | + attr_reader :username, :password, :database |
| 26 | + |
| 27 | + # Default "wait for" strategy |
| 28 | + WAIT_FOR_PROC = ->(container) { |
| 29 | + container.wait_for_http(container_port: 8123, timeout: 30, interval: 1.0, path: "/", status: 200) |
| 30 | + } |
| 31 | + |
| 32 | + # Initializes a new instance of ClickhouseContainer |
| 33 | + # |
| 34 | + # @param image [String] the image to use |
| 35 | + # @param username [String] the username to use |
| 36 | + # @param password [String] the password to use |
| 37 | + # @param database [String] |
| 38 | + # @param kwargs [Hash] the options to pass to the container. See {DockerContainer#initialize} |
| 39 | + # @return [ClickhouseContainer] a new instance of ClickhouseContainer |
| 40 | + def initialize(image = CLICKHOUSE_DEFAULT_IMAGE, username: nil, password: nil, database: nil, **kwargs) |
| 41 | + super(image, wait_for: WAIT_FOR_PROC, exposed_ports: [CLICKHOUSE_DEFAULT_PORT, CLICKHOUSE_DEFAULT_HTTP_PORT], **kwargs) |
| 42 | + @username = username || ENV.fetch("CLICKHOUSE_USER", CLICKHOUSE_DEFAULT_USER) |
| 43 | + @password = password || ENV.fetch("CLICKHOUSE_PASSWORD", CLICKHOUSE_DEFAULT_PASS) |
| 44 | + @database = database || ENV.fetch("CLICKHOUSE_DB", CLICKHOUSE_DB) |
| 45 | + end |
| 46 | + |
| 47 | + # Starts the container |
| 48 | + # |
| 49 | + # @return [ClickhouseContainer] self |
| 50 | + def start |
| 51 | + _configure |
| 52 | + super |
| 53 | + end |
| 54 | + |
| 55 | + # Returns the native TCP port used to connect to the container |
| 56 | + # |
| 57 | + # @return [Integer] the port used by the container |
| 58 | + def port |
| 59 | + CLICKHOUSE_DEFAULT_PORT |
| 60 | + end |
| 61 | + alias_method :tcp_port, :port |
| 62 | + |
| 63 | + # Returns the HTTP port used to connect to the container via HTTP/HTTPS |
| 64 | + # |
| 65 | + # @return [Integer] the HTTP/HTTPS port used by the container |
| 66 | + def http_port |
| 67 | + CLICKHOUSE_DEFAULT_HTTP_PORT |
| 68 | + end |
| 69 | + |
| 70 | + # Returns the clickhouse connection url (e.g. clickhouse://user:password@host:port/database) |
| 71 | + # |
| 72 | + # @param protocol [String] the protocol to use in the string (default: "clickhouse://") |
| 73 | + # @param username [String] the username to use in the string (default: @username) |
| 74 | + # @param password [String] the password to use in the string (default: @password) |
| 75 | + # @param database [String] the database to use in the string (default: @database) |
| 76 | + # @return [String] the clickhouse url |
| 77 | + # @raise [ConnectionError] If the connection to the Docker daemon fails. |
| 78 | + # @raise [ContainerNotStartedError] If the container has not been started. |
| 79 | + def clickhouse_url(protocol: "clickhouse://", username: nil, password: nil, database: nil) |
| 80 | + username ||= @username |
| 81 | + password ||= @password |
| 82 | + database ||= @database |
| 83 | + database = "/#{database}" unless database.start_with?("/") |
| 84 | + |
| 85 | + # clickhouse://user:pass@host:9000/database |
| 86 | + "#{protocol}#{username}:#{password}@#{host}:#{mapped_port(port)}#{database}" |
| 87 | + end |
| 88 | + |
| 89 | + alias_method :connection_url, :clickhouse_url |
| 90 | + |
| 91 | + # Returns the clickhouse connection url (e.g. http://user:password@host:port) |
| 92 | + # |
| 93 | + # @param protocol [String] the protocol to use in the string (default: "http") |
| 94 | + # @return [String] the url for the management UI. Returns nil if the management UI is not available. |
| 95 | + # @raise [ConnectionError] If the connection to the Docker daemon fails. |
| 96 | + # @raise [ContainerNotStartedError] If the container has not been started. |
| 97 | + def clickhouse_http_url(protocol: "http") |
| 98 | + port = mapped_port(http_port) |
| 99 | + port.nil? ? nil : "#{protocol}://#{username}:#{password}@#{host}:#{port}" |
| 100 | + end |
| 101 | + |
| 102 | + # Sets the database to use |
| 103 | + # |
| 104 | + # @param database [String] the database to use |
| 105 | + # @return [ClickhouseContainer] self |
| 106 | + def with_database(database) |
| 107 | + @database = database |
| 108 | + self |
| 109 | + end |
| 110 | + |
| 111 | + # Sets the username to use |
| 112 | + # |
| 113 | + # @param username [String] the username to use |
| 114 | + # @return [ClickhouseContainer] self |
| 115 | + def with_username(username) |
| 116 | + @username = username |
| 117 | + self |
| 118 | + end |
| 119 | + |
| 120 | + # Sets the password to use |
| 121 | + # |
| 122 | + # @param password [String] the password to use |
| 123 | + # @return [ClickhouseContainer] self |
| 124 | + def with_password(password) |
| 125 | + @password = password |
| 126 | + self |
| 127 | + end |
| 128 | + |
| 129 | + # Returns the mapped TCP port |
| 130 | + # |
| 131 | + # @return [Integer] The container's mapped TCP port. |
| 132 | + # @raise [ConnectionError] If the connection to the Docker daemon fails. |
| 133 | + def first_mapped_port |
| 134 | + raise ContainerNotStartedError unless @_container |
| 135 | + mapped_port(port) |
| 136 | + end |
| 137 | + |
| 138 | + private |
| 139 | + |
| 140 | + def _configure |
| 141 | + add_env("CLICKHOUSE_USER", @username) |
| 142 | + add_env("CLICKHOUSE_PASSWORD", @password) |
| 143 | + add_env("CLICKHOUSE_DB", @database) |
| 144 | + end |
| 145 | + end |
| 146 | +end |
0 commit comments