|
1 | 1 | # frozen_string_literal: true
|
2 | 2 | require 'uri'
|
3 | 3 | require_relative 'spec_helper'
|
| 4 | +require_relative 'helpers/auth_query_helper' |
4 | 5 |
|
5 | 6 | describe "Query Mirroing" do
|
6 | 7 | let(:processes) { Helpers::Pgcat.single_instance_setup("sharded_db", 10) }
|
|
89 | 90 | end
|
90 | 91 | end
|
91 | 92 | end
|
| 93 | + |
| 94 | +describe "Query Mirroring with Auth Query" do |
| 95 | + let(:pg_user) { { 'username' => 'sharding_user', 'password' => 'sharding_user' } } |
| 96 | + let(:config_user) { {'username' => 'md5_auth_user'} } |
| 97 | + let(:auth_query_user) { { 'username' => 'md5_auth_user', 'password' => 'hash' } } |
| 98 | + let(:mirror_pg) { PgInstance.new(8432, "md5_auth_user", "hash", "shard0") } |
| 99 | + let(:mirror_host) { "localhost" } |
| 100 | + let(:config) { |
| 101 | + { |
| 102 | + 'general' => { |
| 103 | + 'auth_query' => "SELECT * FROM public.user_lookup('$1');", |
| 104 | + 'auth_query_user' => auth_query_user['username'], |
| 105 | + 'auth_query_password' => auth_query_user['password'] |
| 106 | + }, |
| 107 | + } |
| 108 | + } |
| 109 | + let(:processes) { Helpers::AuthQuery.single_shard_auth_query( |
| 110 | + pool_name: "sharded_db", |
| 111 | + pg_user: pg_user, |
| 112 | + config_user: config_user, |
| 113 | + extra_conf: config, |
| 114 | + wait_until_ready: false, |
| 115 | + )} |
| 116 | + |
| 117 | + before do |
| 118 | + pgcat = processes.pgcat |
| 119 | + |
| 120 | + new_configs = processes.pgcat.current_config |
| 121 | + new_configs["pools"]["sharded_db"]["shards"]["0"]["mirrors"] = [ |
| 122 | + [mirror_host, mirror_pg.port.to_i, 0], |
| 123 | + [mirror_host, mirror_pg.port.to_i, 1], |
| 124 | + ] |
| 125 | + pgcat.update_config(new_configs) |
| 126 | + pgcat.reload_config |
| 127 | + |
| 128 | + Helpers::AuthQuery.set_up_auth_query_for_user( |
| 129 | + user: auth_query_user['username'], |
| 130 | + password: auth_query_user['password'], |
| 131 | + instance_ports: [processes.primary.port, processes.replicas[0].port, mirror_pg.port], |
| 132 | + ) |
| 133 | + |
| 134 | + pgcat.wait_until_ready( |
| 135 | + pgcat.connection_string("sharded_db", auth_query_user['username'], auth_query_user['password']) |
| 136 | + ) |
| 137 | + |
| 138 | + mirror_pg.reset |
| 139 | + end |
| 140 | + |
| 141 | + after do |
| 142 | + Helpers::AuthQuery.tear_down_auth_query_for_user( |
| 143 | + user: auth_query_user['username'], |
| 144 | + password: auth_query_user['password'], |
| 145 | + instance_ports: [processes.primary.port, processes.replicas[0].port, mirror_pg.port], |
| 146 | + ) |
| 147 | + end |
| 148 | + |
| 149 | + context "when auth_query is configured" do |
| 150 | + it "can mirror a query" do |
| 151 | + conn = PG.connect(processes.pgcat.connection_string("sharded_db", auth_query_user['username'], auth_query_user['password'])) |
| 152 | + runs=5 |
| 153 | + runs.times { conn.sync_exec("SELECT 1 + 2") } |
| 154 | + conn.close |
| 155 | + |
| 156 | + # I'd like to check verify the primary and replica received the queries too, but getting the permissions correct is annoying |
| 157 | + # expect((processes.all_databases + processes.replicas).map(&:count_select_1_plus_2).sum).to eq(0) |
| 158 | + expect(mirror_pg.count_select_1_plus_2).to eq(runs) |
| 159 | + end |
| 160 | + end |
| 161 | +end |
| 162 | + |
0 commit comments