From 103ee6aa9539bc8135b352cd028484be5296caf1 Mon Sep 17 00:00:00 2001 From: tanner0101 Date: Mon, 14 May 2018 12:14:34 -0400 Subject: [PATCH 1/4] remove executing code from asserts --- .gitignore | 1 + Sources/PostgreSQL/Data/PostgreSQLData+Point.swift | 6 ++++-- Sources/PostgreSQL/Utilities.swift | 3 ++- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 2acce1b1..ea8fec49 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ /Packages /*.xcodeproj Package.resolved +DerivedData diff --git a/Sources/PostgreSQL/Data/PostgreSQLData+Point.swift b/Sources/PostgreSQL/Data/PostgreSQLData+Point.swift index cf6415cd..bb78039a 100644 --- a/Sources/PostgreSQL/Data/PostgreSQLData+Point.swift +++ b/Sources/PostgreSQL/Data/PostgreSQLData+Point.swift @@ -49,8 +49,10 @@ extension PostgreSQLPoint: PostgreSQLDataConvertible { let parts = string.split(separator: ",") var x = parts[0] var y = parts[1] - assert(x.popFirst()! == "(") - assert(y.popLast()! == ")") + let leftParen = x.popFirst() + assert(leftParen == "(") + let rightParen = y.popLast() + assert(rightParen == ")") return .init(x: Double(x)!, y: Double(y)!) case .binary: let x = value[0..<8] diff --git a/Sources/PostgreSQL/Utilities.swift b/Sources/PostgreSQL/Utilities.swift index 15b8db9b..4953c993 100644 --- a/Sources/PostgreSQL/Utilities.swift +++ b/Sources/PostgreSQL/Utilities.swift @@ -37,7 +37,8 @@ extension Data { return } for _ in 0.. Date: Mon, 14 May 2018 13:59:35 -0400 Subject: [PATCH 2/4] add docker compose --- .../PostgreSQLConnectionTests.swift | 8 ++++---- circle.yml | 3 ++- docker-compose.yml | 19 +++++++++++++++++++ 3 files changed, 25 insertions(+), 5 deletions(-) create mode 100644 docker-compose.yml diff --git a/Tests/PostgreSQLTests/PostgreSQLConnectionTests.swift b/Tests/PostgreSQLTests/PostgreSQLConnectionTests.swift index ed0fbb7b..e0cc231b 100644 --- a/Tests/PostgreSQLTests/PostgreSQLConnectionTests.swift +++ b/Tests/PostgreSQLTests/PostgreSQLConnectionTests.swift @@ -489,18 +489,18 @@ extension PostgreSQLConnection { /// Creates a test event loop and psql client. static func makeTest() throws -> PostgreSQLConnection { #if Xcode - return try _makeTest(hostname: self.dockerMachineHostname) + return try _makeTest(hostname: self.dockerMachineHostname, password: "vapor_password") #else - return try _makeTest(hostname: "localhost") + return try _makeTest(hostname: "cleartext") #endif } /// Creates a test event loop and psql client over ssl. static func makeTest(transport: PostgreSQLTransportConfig) throws -> PostgreSQLConnection { #if Xcode - return try _makeTest(hostname: self.dockerMachineHostname, port: 5433, transport: transport) + return try _makeTest(hostname: self.dockerMachineHostname, password: "vapor_password", port: 5433, transport: transport) #else - return try _makeTest(hostname: "localhost-ssl", password: "vapor_password", transport: transport) + return try _makeTest(hostname: "tls", password: "vapor_password", transport: transport) #endif } diff --git a/circle.yml b/circle.yml index 67bede28..5173c677 100644 --- a/circle.yml +++ b/circle.yml @@ -14,12 +14,13 @@ jobs: docker: - image: codevapor/swift:4.1 - image: circleci/postgres:latest + name: cleartext environment: POSTGRES_USER: vapor_username POSTGRES_DB: vapor_database POSTGRES_PASSWORD: vapor_password - image: scenecheck/postgres-ssl:latest - name: localhost-ssl + name: tls environment: POSTGRES_USER: vapor_username POSTGRES_DB: vapor_database diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 00000000..df139c4f --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,19 @@ +version: '2' + +services: + cleartext: + image: postgres:latest + environment: + POSTGRES_USER: vapor_username + POSTGRES_DB: vapor_database + POSTGRES_PASSWORD: vapor_password + ports: + - 5432:5432 + tls: + image: scenecheck/postgres-ssl:latest + environment: + POSTGRES_USER: vapor_username + POSTGRES_DB: vapor_database + POSTGRES_PASSWORD: vapor_password + ports: + - 5433:5432 From cf3794e01d534db8d06addd1e7ee12ce1faceb50 Mon Sep 17 00:00:00 2001 From: tanner0101 Date: Mon, 14 May 2018 14:04:55 -0400 Subject: [PATCH 3/4] cleanup maketest --- .../Database/PostgreSQLTransportConfig.swift | 8 +++++ .../PostgreSQLConnectionTests.swift | 36 ++++++++----------- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/Sources/PostgreSQL/Database/PostgreSQLTransportConfig.swift b/Sources/PostgreSQL/Database/PostgreSQLTransportConfig.swift index 764ba9a2..32996d34 100644 --- a/Sources/PostgreSQL/Database/PostgreSQLTransportConfig.swift +++ b/Sources/PostgreSQL/Database/PostgreSQLTransportConfig.swift @@ -36,6 +36,14 @@ public struct PostgreSQLTransportConfig { public static func customTLS(_ tlsConfiguration: TLSConfiguration)-> PostgreSQLTransportConfig { return .init(method: .tls(tlsConfiguration)) } + + /// Returns `true` if this configuration uses TLS. + public var isTLS: Bool { + switch method { + case .cleartext: return false + case .tls: return true + } + } internal enum Method { case cleartext diff --git a/Tests/PostgreSQLTests/PostgreSQLConnectionTests.swift b/Tests/PostgreSQLTests/PostgreSQLConnectionTests.swift index e0cc231b..07ced057 100644 --- a/Tests/PostgreSQLTests/PostgreSQLConnectionTests.swift +++ b/Tests/PostgreSQLTests/PostgreSQLConnectionTests.swift @@ -8,7 +8,7 @@ import Core class PostgreSQLConnectionTests: XCTestCase { let defaultTimeout = 5.0 func testVersion() throws { - let client = try PostgreSQLConnection.makeTest() + let client = try PostgreSQLConnection.makeTest(transport: .cleartext) let results = try client.simpleQuery("SELECT version();").wait() try XCTAssert(results[0].firstValue(forColumn: "version")?.decode(String.self).contains("10.") == true) } @@ -20,7 +20,7 @@ class PostgreSQLConnectionTests: XCTestCase { } func testSelectTypes() throws { - let client = try PostgreSQLConnection.makeTest() + let client = try PostgreSQLConnection.makeTest(transport: .cleartext) let results = try client.query("select * from pg_type;").wait() if results.count > 350 { let name = try results[128].firstValue(forColumn: "typname")?.decode(String.self) @@ -31,7 +31,7 @@ class PostgreSQLConnectionTests: XCTestCase { } func testParse() throws { - let client = try PostgreSQLConnection.makeTest() + let client = try PostgreSQLConnection.makeTest(transport: .cleartext) let query = """ select * from "pg_type" where "typlen" = $1 or "typlen" = $2 """ @@ -46,7 +46,7 @@ class PostgreSQLConnectionTests: XCTestCase { } func testTypes() throws { - let client = try PostgreSQLConnection.makeTest() + let client = try PostgreSQLConnection.makeTest(transport: .cleartext) let createQuery = """ create table kitchen_sink ( "smallint" smallint, @@ -139,7 +139,7 @@ class PostgreSQLConnectionTests: XCTestCase { } func testParameterizedTypes() throws { - let client = try PostgreSQLConnection.makeTest() + let client = try PostgreSQLConnection.makeTest(transport: .cleartext) let createQuery = """ create table kitchen_sink ( "smallint" smallint, @@ -262,7 +262,7 @@ class PostgreSQLConnectionTests: XCTestCase { var message: String } - let client = try PostgreSQLConnection.makeTest() + let client = try PostgreSQLConnection.makeTest(transport: .cleartext) _ = try client.query("drop table if exists foo;").wait() let createResult = try client.query("create table foo (id integer, dict jsonb);").wait() XCTAssertEqual(createResult.count, 0) @@ -282,7 +282,7 @@ class PostgreSQLConnectionTests: XCTestCase { } func testNull() throws { - let client = try PostgreSQLConnection.makeTest() + let client = try PostgreSQLConnection.makeTest(transport: .cleartext) _ = try client.query("drop table if exists nulltest;").wait() let createResult = try client.query("create table nulltest (i integer not null, d timestamp);").wait() XCTAssertEqual(createResult.count, 0) @@ -297,7 +297,7 @@ class PostgreSQLConnectionTests: XCTestCase { func testGH24() throws { /// PREPARE - let client = try PostgreSQLConnection.makeTest() + let client = try PostgreSQLConnection.makeTest(transport: .cleartext) _ = try client.query(""" DROP TABLE IF EXISTS "acronym+category" """).wait() @@ -448,7 +448,7 @@ class PostgreSQLConnectionTests: XCTestCase { var count: Int } - let connection = try PostgreSQLConnection.makeTest() + let connection = try PostgreSQLConnection.makeTest(transport: .cleartext) _ = try connection.simpleQuery("DROP TABLE IF EXISTS apps").wait() _ = try connection.simpleQuery("CREATE TABLE apps (id INT, platform TEXT, identifier TEXT)").wait() _ = try connection.simpleQuery("INSERT INTO apps VALUES (1, 'a', 'b')").wait() @@ -486,24 +486,16 @@ class PostgreSQLConnectionTests: XCTestCase { } extension PostgreSQLConnection { - /// Creates a test event loop and psql client. - static func makeTest() throws -> PostgreSQLConnection { - #if Xcode - return try _makeTest(hostname: self.dockerMachineHostname, password: "vapor_password") - #else - return try _makeTest(hostname: "cleartext") - #endif - } - /// Creates a test event loop and psql client over ssl. static func makeTest(transport: PostgreSQLTransportConfig) throws -> PostgreSQLConnection { - #if Xcode - return try _makeTest(hostname: self.dockerMachineHostname, password: "vapor_password", port: 5433, transport: transport) + #if os(macOS) + return try _makeTest(hostname: self.dockerMachineHostname, password: "vapor_password", port: transport.isTLS ? 5433 : 5432, transport: transport) #else - return try _makeTest(hostname: "tls", password: "vapor_password", transport: transport) + return try _makeTest(hostname: transport.isTLS ? "tls" : "cleartext", password: "vapor_password", transport: transport) #endif } - + + /// Creates a test connection. private static func _makeTest(hostname: String, password: String? = nil, port: Int = 5432, transport: PostgreSQLTransportConfig = .cleartext) throws -> PostgreSQLConnection { let group = MultiThreadedEventLoopGroup(numThreads: 1) let client = try PostgreSQLConnection.connect(hostname: hostname, port: port, transport: transport, on: group) { error in From f446e140202a4ee1f7ebe9d6837b788de2004ed8 Mon Sep 17 00:00:00 2001 From: tanner0101 Date: Mon, 14 May 2018 14:05:36 -0400 Subject: [PATCH 4/4] cleanup docker --- Tests/PostgreSQLTests/PostgreSQLConnectionTests.swift | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/Tests/PostgreSQLTests/PostgreSQLConnectionTests.swift b/Tests/PostgreSQLTests/PostgreSQLConnectionTests.swift index 07ced057..4e4e978e 100644 --- a/Tests/PostgreSQLTests/PostgreSQLConnectionTests.swift +++ b/Tests/PostgreSQLTests/PostgreSQLConnectionTests.swift @@ -489,7 +489,7 @@ extension PostgreSQLConnection { /// Creates a test event loop and psql client over ssl. static func makeTest(transport: PostgreSQLTransportConfig) throws -> PostgreSQLConnection { #if os(macOS) - return try _makeTest(hostname: self.dockerMachineHostname, password: "vapor_password", port: transport.isTLS ? 5433 : 5432, transport: transport) + return try _makeTest(hostname: "192.168.99.100", password: "vapor_password", port: transport.isTLS ? 5433 : 5432, transport: transport) #else return try _makeTest(hostname: transport.isTLS ? "tls" : "cleartext", password: "vapor_password", transport: transport) #endif @@ -504,10 +504,6 @@ extension PostgreSQLConnection { _ = try client.authenticate(username: "vapor_username", database: "vapor_database", password: password).wait() return client } - - private static var dockerMachineHostname: String { - return (try? Process.execute("docker-machine", "ip")) ?? "192.168.99.100" - } } func +=(lhs: inout [T], rhs: T) {