Skip to content
This repository was archived by the owner on Mar 11, 2022. It is now read-only.

Commit 4aef065

Browse files
authored
Merge pull request #523 from cloudant/new-test-server
Update test server config
2 parents 6a9b62c + 497d788 commit 4aef065

9 files changed

Lines changed: 90 additions & 56 deletions

Jenkinsfile

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,15 @@ def runTests(testEnv, isServiceTests) {
2727

2828
//Set up the environment and run the tests
2929
withEnv(testEnv) {
30-
withCredentials([(env.CREDS_ID.contains('iam')) ? string(credentialsId: env.CREDS_ID, variable: 'IAM_API_KEY') : usernamePassword(credentialsId: env.CREDS_ID, usernameVariable: 'DB_USER', passwordVariable: 'DB_PASSWORD')]) {
30+
withCredentials([(env.CREDS_ID.contains('Iam')) ? string(credentialsId: env.CREDS_ID, variable: 'IAM_API_KEY') : usernamePassword(credentialsId: env.CREDS_ID, usernameVariable: 'DB_USER', passwordVariable: 'DB_PASSWORD')]) {
3131
try {
32-
sh "./gradlew ${(env.DB_USER?.trim()) ? '-Dtest.server.user=$DB_USER -Dtest.server.password=$DB_PASSWORD' : ''} -Dtest.server.host=\$DB_HOST -Dtest.server.port=\$DB_PORT -Dtest.server.protocol=\$DB_HTTP \$GRADLE_TARGET"
32+
sh "./gradlew \
33+
${(env.DB_USER?.trim()) ? '-Dtest.server.user=$DB_USER -Dtest.server.password=$DB_PASSWORD' : ''} \
34+
-Dtest.server.host=\$DB_HOST \
35+
-Dtest.server.port=\$DB_PORT \
36+
-Dtest.server.protocol=\$DB_HTTP \
37+
-Dtest.replication.source.url=\$SDKS_TEST_SERVER_URL \
38+
\$GRADLE_TARGET"
3339
} finally {
3440
junit '**/build/test-results/**/*.xml'
3541
}
@@ -49,8 +55,8 @@ stage('Build') {
4955

5056
stage('QA') {
5157
// Define the matrix environments
52-
def CLOUDANT_ENV = ['DB_HTTP=https', 'DB_HOST=clientlibs-test.cloudant.com', 'DB_PORT=443', 'DB_IGNORE_COMPACTION=true', 'CREDS_ID=clientlibs-test']
53-
def CLOUDANT_IAM_ENV = ['DB_HTTP=https', 'DB_HOST=clientlibs-test.cloudant.com', 'DB_PORT=443', 'DB_IGNORE_COMPACTION=true', 'CREDS_ID=clientlibs-test-iam']
58+
def CLOUDANT_ENV = ['DB_HTTP=https', "DB_HOST=${SDKS_TEST_SERVER_HOST}", 'DB_PORT=443', 'DB_IGNORE_COMPACTION=true', 'CREDS_ID=testServerLegacy']
59+
def CLOUDANT_IAM_ENV = ['DB_HTTP=https', "DB_HOST=${SDKS_TEST_SERVER_HOST}", 'DB_PORT=443', 'DB_IGNORE_COMPACTION=true', 'CREDS_ID=testServerIamApiKey', "TEST_IAM_TOKEN_URL=${SDKS_TEST_IAM_URL}"]
5460
def COUCH1_6_ENV = ['DB_HTTP=http', 'DB_HOST=cloudantsync002.bristol-victoria.uk.ibm.com', 'DB_PORT=5984', 'DB_IGNORE_COMPACTION=false', 'CREDS_ID=couchdb']
5561
def COUCH2_0_ENV = ['DB_HTTP=http', 'DB_HOST=cloudantsync002.bristol-victoria.uk.ibm.com', 'DB_PORT=5985', 'DB_IGNORE_COMPACTION=true', 'CREDS_ID=couchdb']
5662
def CLOUDANT_LOCAL_ENV = ['DB_HTTP=http', 'DB_HOST=cloudantsync002.bristol-victoria.uk.ibm.com', 'DB_PORT=8081', 'DB_IGNORE_COMPACTION=true', 'CREDS_ID=couchdb']

cloudant-client/build.gradle

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,17 @@ tasks.withType(Test) {
5353
}
5454
jvmArgs "-javaagent:${jMockit}"
5555
}
56+
// Special environment variables for integration tests
57+
[SERVER_URL:'test.server.url',
58+
SERVER_USER:'test.server.user',
59+
SERVER_PASSWORD:'test.server.password',
60+
TEST_REPLICATION_SOURCE_URL:'test.replication.source.url',
61+
TEST_IAM_TOKEN_URL:'com.cloudant.client.iamserver'].each { k,v ->
62+
def e = System.getenv(k)
63+
if (e) {
64+
systemProperty v, e
65+
}
66+
}
5667
}
5768

5869
// Run tests which are compatible with all versions of Couch or
@@ -91,16 +102,6 @@ task cloudantServiceTest(type: Test) {
91102
}
92103

93104
task integrationTest(type: Test) {
94-
// Special environment variables for integration tests
95-
[SERVER_URL:'test.server.url',
96-
SERVER_USER:'test.server.user',
97-
SERVER_PASSWORD:'test.server.password',
98-
TEST_REPLICATION_SOURCE_URL:'test.replication.source.url'].each { k,v ->
99-
def e = System.getenv(k)
100-
if (e) {
101-
systemProperty v, e
102-
}
103-
}
104105
// Run all tests that need a running DB and those for Cloudant, but not unit tests
105106
useJUnitPlatform {
106107
includeEngines 'junit-jupiter'

cloudant-client/src/test/java/com/cloudant/tests/CloudFoundryServiceTest.java

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright © 2016, 2020 IBM Corp. All rights reserved.
2+
* Copyright © 2016, 2021 IBM Corp. All rights reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
55
* except in compliance with the License. You may obtain a copy of the License at
@@ -14,7 +14,6 @@
1414

1515
package com.cloudant.tests;
1616

17-
import static com.cloudant.http.internal.interceptors.IamCookieInterceptor.IAM_TOKEN_SERVER_URL_PROPERTY_KEY;
1817
import static com.cloudant.tests.util.MockWebServerResources.IAM_API_KEY;
1918
import static com.cloudant.tests.util.MockWebServerResources.IAM_TOKEN;
2019
import static com.cloudant.tests.util.MockWebServerResources.OK_IAM_COOKIE;
@@ -23,6 +22,7 @@
2322

2423
import com.cloudant.client.api.ClientBuilder;
2524
import com.cloudant.client.api.CloudantClient;
25+
import com.cloudant.tests.extensions.MockIamWebServerExtension;
2626
import com.cloudant.tests.extensions.MockWebServerExtension;
2727
import com.cloudant.tests.util.HttpFactoryParameterizedTest;
2828
import com.cloudant.tests.util.MockWebServerResources;
@@ -57,7 +57,7 @@ public class CloudFoundryServiceTest {
5757
public MockWebServerExtension mockWebServerExt = new MockWebServerExtension();
5858

5959
@RegisterExtension
60-
public MockWebServerExtension mockIamServerExt = new MockWebServerExtension();
60+
public MockWebServerExtension mockIamServerExt = new MockIamWebServerExtension();
6161

6262
public MockWebServer server;
6363
public MockWebServer mockIamServer;
@@ -74,14 +74,6 @@ public void beforeEach() {
7474
mockServerHostPort = String.format("%s:%s/", server.getHostName(), server.getPort());
7575
//setup mock IAM server
7676
mockIamServer = mockIamServerExt.get();
77-
// Override the default IAM token server with our test mock server
78-
System.setProperty(IAM_TOKEN_SERVER_URL_PROPERTY_KEY, mockIamServer.url(iamTokenEndpoint)
79-
.toString());
80-
}
81-
82-
@AfterEach
83-
public void clearIAMMock() {
84-
System.clearProperty(IAM_TOKEN_SERVER_URL_PROPERTY_KEY);
8577
}
8678

8779
private static class VCAPGenerator {
@@ -98,7 +90,7 @@ private static class VCAPGenerator {
9890
cloudantServices = new ArrayList<HashMap<String, Object>>();
9991
vcap.put(serviceName, cloudantServices);
10092
}
101-
93+
10294
private void addService(String name, String username, String password,
10395
String host, String apikey) {
10496
HashMap<String, Object> cloudantService = new HashMap<String, Object>();

cloudant-client/src/test/java/com/cloudant/tests/CloudantClientHelper.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright © 2015, 2019 IBM Corp. All rights reserved.
2+
* Copyright © 2015, 2021 IBM Corp. All rights reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
55
* except in compliance with the License. You may obtain a copy of the License at
@@ -36,12 +36,13 @@ public abstract class CloudantClientHelper {
3636
static final String SERVER_USER;
3737
static final String SERVER_PASSWORD;
3838
static final String SERVER_HOST;
39+
//some tests need access to the URL
40+
static final URL SERVER_URL;
3941

4042
private static final CloudantClient CLIENT_INSTANCE;
4143

4244
private static final String SERVER_PORT;
4345
private static final String SERVER_PROTOCOL;
44-
private static final URL SERVER_URL;
4546

4647
static {
4748

cloudant-client/src/test/java/com/cloudant/tests/CloudantClientTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright © 2015, 2019 IBM Corp. All rights reserved.
2+
* Copyright © 2015, 2021 IBM Corp. All rights reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
55
* except in compliance with the License. You may obtain a copy of the License at
@@ -500,7 +500,7 @@ public void testBasicAuth() throws IOException {
500500
new BasicAuthInterceptor(CloudantClientHelper.SERVER_USER
501501
+ ":" + CloudantClientHelper.SERVER_PASSWORD);
502502

503-
CloudantClient client = ClientBuilder.account(CloudantClientHelper.SERVER_USER)
503+
CloudantClient client = ClientBuilder.url(CloudantClientHelper.SERVER_URL)
504504
.interceptors(interceptor).build();
505505

506506
// Test passes if there are no exceptions

cloudant-client/src/test/java/com/cloudant/tests/HttpIamTest.java

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright © 2017, 2019 IBM Corp. All rights reserved.
2+
* Copyright © 2017, 2021 IBM Corp. All rights reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
55
* except in compliance with the License. You may obtain a copy of the License at
@@ -14,7 +14,6 @@
1414

1515
package com.cloudant.tests;
1616

17-
import static com.cloudant.http.internal.interceptors.IamCookieInterceptor.IAM_TOKEN_SERVER_URL_PROPERTY_KEY;
1817
import static com.cloudant.tests.HttpTest.takeN;
1918
import static com.cloudant.tests.util.MockWebServerResources.EXPECTED_OK_COOKIE;
2019
import static com.cloudant.tests.util.MockWebServerResources.EXPECTED_OK_COOKIE_2;
@@ -40,6 +39,7 @@
4039
import com.cloudant.client.org.lightcouch.CouchDbException;
4140
import com.cloudant.http.Http;
4241
import com.cloudant.http.interceptors.Replay429Interceptor;
42+
import com.cloudant.tests.extensions.MockIamWebServerExtension;
4343
import com.cloudant.tests.extensions.MockWebServerExtension;
4444
import com.cloudant.tests.util.MockWebServerResources;
4545

@@ -64,7 +64,7 @@ public class HttpIamTest {
6464
public MockWebServerExtension mockWebServerExt = new MockWebServerExtension();
6565

6666
@RegisterExtension
67-
public MockWebServerExtension mockIamServerExt = new MockWebServerExtension();
67+
public MockWebServerExtension mockIamServerExt = new MockIamWebServerExtension();
6868

6969
public MockWebServer mockWebServer;
7070
public MockWebServer mockIamServer;
@@ -76,14 +76,6 @@ public class HttpIamTest {
7676
public void setIAMMockEndpoint() {
7777
mockWebServer = mockWebServerExt.get();
7878
mockIamServer = mockIamServerExt.get();
79-
// Override the default IAM token server with our test mock server
80-
System.setProperty(IAM_TOKEN_SERVER_URL_PROPERTY_KEY, mockIamServer.url(iamTokenEndpoint)
81-
.toString());
82-
}
83-
84-
@AfterEach
85-
public void clearIAMMock() {
86-
System.clearProperty(IAM_TOKEN_SERVER_URL_PROPERTY_KEY);
8779
}
8880

8981
/**

cloudant-client/src/test/java/com/cloudant/tests/SessionInterceptorExpiryTests.java

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright © 2017, 2018 IBM Corp. All rights reserved.
2+
* Copyright © 2017, 2021 IBM Corp. All rights reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
55
* except in compliance with the License. You may obtain a copy of the License at
@@ -14,7 +14,6 @@
1414

1515
package com.cloudant.tests;
1616

17-
import static com.cloudant.http.internal.interceptors.IamCookieInterceptor.IAM_TOKEN_SERVER_URL_PROPERTY_KEY;
1817
import static com.cloudant.tests.util.MockWebServerResources.iamTokenEndpoint;
1918
import static org.junit.jupiter.api.Assertions.assertEquals;
2019
import static org.junit.jupiter.api.Assertions.assertNotNull;
@@ -30,6 +29,7 @@
3029
import com.cloudant.http.internal.interceptors.CookieInterceptor;
3130
import com.cloudant.http.internal.interceptors.IamCookieInterceptor;
3231
import com.cloudant.http.internal.ok.OkHttpClientHttpUrlConnectionFactory;
32+
import com.cloudant.tests.extensions.MockIamWebServerExtension;
3333
import com.cloudant.tests.extensions.MockWebServerExtension;
3434
import com.cloudant.tests.util.HttpFactoryParameterizedTest;
3535
import com.cloudant.tests.util.MockWebServerResources;
@@ -120,7 +120,7 @@ public Object resolveParameter(ParameterContext parameterContext,
120120
public MockWebServerExtension mockWebServerExt = new MockWebServerExtension();
121121
public MockWebServer mockWebServer;
122122
@RegisterExtension
123-
public MockWebServerExtension mockIamServerExt = new MockWebServerExtension();
123+
public MockWebServerExtension mockIamServerExt = new MockIamWebServerExtension();
124124
public MockWebServer mockIamServer;
125125

126126
private HttpConnectionRequestInterceptor rqInterceptor;
@@ -139,9 +139,6 @@ public void setupSessionInterceptor(boolean okUsable, String sessionPath) {
139139
rpInterceptor = ci;
140140
} else if (sessionPath.equals("/_iam_session")) {
141141
// Set the endpoint value before each test
142-
// Override the default IAM token server with our test mock server
143-
System.setProperty(IAM_TOKEN_SERVER_URL_PROPERTY_KEY, mockIamServer.url(iamTokenEndpoint)
144-
.toString());
145142
IamCookieInterceptor ici = new IamCookieInterceptor("apikey", baseUrl);
146143
rqInterceptor = ici;
147144
rpInterceptor = ici;
@@ -150,11 +147,6 @@ public void setupSessionInterceptor(boolean okUsable, String sessionPath) {
150147
}
151148
}
152149

153-
@AfterEach
154-
public void clearIAMMock() {
155-
System.clearProperty(IAM_TOKEN_SERVER_URL_PROPERTY_KEY);
156-
}
157-
158150
private void queueResponses(boolean okUsable,
159151
String sessionPath,
160152
Long cookieLifetimeSeconds,
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright © 2021 IBM Corp. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5+
* except in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the
10+
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
11+
* either express or implied. See the License for the specific language governing permissions
12+
* and limitations under the License.
13+
*/
14+
package com.cloudant.tests.extensions;
15+
16+
import static com.cloudant.http.internal.interceptors.IamCookieInterceptor.IAM_TOKEN_SERVER_URL_PROPERTY_KEY;
17+
import static com.cloudant.tests.util.MockWebServerResources.iamTokenEndpoint;
18+
19+
import org.junit.jupiter.api.extension.AfterEachCallback;
20+
import org.junit.jupiter.api.extension.BeforeEachCallback;
21+
import org.junit.jupiter.api.extension.ExtensionContext;
22+
23+
import okhttp3.mockwebserver.MockWebServer;
24+
25+
public class MockIamWebServerExtension extends MockWebServerExtension {
26+
27+
// Immutably store the real token server
28+
private final String tokenServer = System.getProperty(IAM_TOKEN_SERVER_URL_PROPERTY_KEY);
29+
30+
// NB beforeEach/afterEach emulate before and after in the actual mock, because we can't call
31+
// these directly as they are marked as protected
32+
33+
@Override
34+
public synchronized void beforeEach(ExtensionContext context) throws Exception {
35+
super.beforeEach(context);
36+
// Override the token server property
37+
System.setProperty(IAM_TOKEN_SERVER_URL_PROPERTY_KEY, this.get().url(iamTokenEndpoint)
38+
.toString());
39+
}
40+
41+
@Override
42+
public synchronized void afterEach(ExtensionContext context) throws Exception {
43+
if (tokenServer != null) {
44+
// Restore the original token server
45+
System.setProperty(IAM_TOKEN_SERVER_URL_PROPERTY_KEY, tokenServer);
46+
} else {
47+
// Clear the temporary token server
48+
System.clearProperty(IAM_TOKEN_SERVER_URL_PROPERTY_KEY);
49+
}
50+
super.afterEach(context);
51+
}
52+
53+
}

cloudant-client/src/test/java/com/cloudant/tests/extensions/MockWebServerExtension.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright © 2018 IBM Corp. All rights reserved.
2+
* Copyright © 2018, 2021 IBM Corp. All rights reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
55
* except in compliance with the License. You may obtain a copy of the License at
@@ -24,9 +24,6 @@ public class MockWebServerExtension implements BeforeEachCallback, AfterEachCall
2424
private MockWebServer mockWebServer;
2525
private boolean started = false;
2626

27-
public MockWebServerExtension() {
28-
}
29-
3027
public MockWebServer get() {
3128
return this.mockWebServer;
3229
}

0 commit comments

Comments
 (0)