For #6493 : Add postgres_session support for custom port with a socket connection (#6494)

* Add support for custom port with a socket connection

Signed-off-by: Pg <pg.developper.fr@gmail.com>

* Add tests for the postgres_session with custom port

Signed-off-by: Pg <pg.developper.fr@gmail.com>

---------

Signed-off-by: Pg <pg.developper.fr@gmail.com>
This commit is contained in:
Pg 2023-05-16 03:33:47 +02:00 committed by GitHub
parent 30fa76a5af
commit 869e1902a1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 2 deletions

View file

@ -81,7 +81,8 @@ module Inspec::Resources
# Socket path and empty host in the connection string establishes socket connection
# Socket connection only enabled for non-windows platforms
# Windows does not support unix domain sockets
"psql -d postgresql://#{@user}:#{@pass}@/#{dbs}?host=#{@socket_path} -A -t -w -c #{escaped_query(query)}"
option_port = @port.nil? ? "" : "-p #{@port}" # add explicit port if specified
"psql -d postgresql://#{@user}:#{@pass}@/#{dbs}?host=#{@socket_path} #{option_port} -A -t -w -c #{escaped_query(query)}"
else
# Host in connection string establishes tcp/ip connection
if inspec.os.windows?

View file

@ -39,7 +39,11 @@ describe "Inspec::Resources::PostgresSession" do
end
it "verify postgres_session create_psql_cmd in socket connection" do
resource = load_resource("postgres_session", "myuser", "mypass", "127.0.0.1", 5432, "/var/run/postgresql")
_(resource.send(:create_psql_cmd, "SELECT * FROM STUDENTS;", ["testdb"])).must_equal "psql -d postgresql://myuser:mypass@/testdb?host=/var/run/postgresql -A -t -w -c SELECT\\ \\*\\ FROM\\ STUDENTS\\;"
_(resource.send(:create_psql_cmd, "SELECT * FROM STUDENTS;", ["testdb"])).must_equal "psql -d postgresql://myuser:mypass@/testdb?host=/var/run/postgresql -p 5432 -A -t -w -c SELECT\\ \\*\\ FROM\\ STUDENTS\\;"
end
it "verify postgres_session create_psql_cmd in socket connection" do
resource = load_resource("postgres_session", "myuser", "mypass", "127.0.0.1", 1234, "/var/run/postgresql")
_(resource.send(:create_psql_cmd, "SELECT * FROM STUDENTS;", ["testdb"])).must_equal "psql -d postgresql://myuser:mypass@/testdb?host=/var/run/postgresql -p 1234 -A -t -w -c SELECT\\ \\*\\ FROM\\ STUDENTS\\;"
end
it "fails when no connection established in linux" do