Skip to content

Commit af7ea1e

Browse files
authored
Pubsub in ci (#175)
* run pubsub sample in codebuild CI for both mqtt direct and websockets
1 parent 22b3875 commit af7ea1e

File tree

4 files changed

+87
-1
lines changed

4 files changed

+87
-1
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
version: 0.2
2+
#this build spec assumes the ubuntu aws/codebuild/java:openjdk-8 image
3+
phases:
4+
install:
5+
commands:
6+
- sudo add-apt-repository ppa:openjdk-r/ppa
7+
- sudo add-apt-repository ppa:ubuntu-toolchain-r/test
8+
- sudo apt-get update -y
9+
- curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "/tmp/awscliv2.zip"
10+
- unzip -q -d /tmp /tmp/awscliv2.zip
11+
- sudo /tmp/aws/install
12+
build:
13+
commands:
14+
- echo Build started on `date`
15+
- $CODEBUILD_SRC_DIR/codebuild/samples/setup-linux.sh
16+
- $CODEBUILD_SRC_DIR/codebuild/samples/pubsub-linux.sh
17+
post_build:
18+
commands:
19+
- echo Build completed on `date`
20+
21+
artifacts:
22+
discard-paths: yes
23+
files:
24+
- "target/surefire-reports/**"
25+
- "hs_err_pid*"
26+
- "core*"

codebuild/samples/pubsub-linux.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
env
6+
7+
pushd $CODEBUILD_SRC_DIR/samples/BasicPubSub
8+
9+
ENDPOINT=$(aws secretsmanager get-secret-value --secret-id "unit-test/endpoint" --query "SecretString" | cut -f2 -d":" | sed -e 's/[\\\"\}]//g')
10+
11+
mvn compile
12+
13+
echo "Mqtt Direct test"
14+
mvn exec:java -Dexec.mainClass="pubsub.PubSub" -Daws.crt.ci="True" -Dexec.arguments="--endpoint,$ENDPOINT,--key,/tmp/privatekey.pem,--cert,/tmp/certificate.pem"
15+
16+
echo "Websocket test"
17+
mvn exec:java -Dexec.mainClass="pubsub.PubSub" -Daws.crt.ci="True" -Dexec.arguments="--endpoint,$ENDPOINT,--websockets,--region,us-east-1,--port,443"
18+
19+
popd

codebuild/samples/setup-linux.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
env
6+
7+
# build java package
8+
cd $CODEBUILD_SRC_DIR
9+
10+
ulimit -c unlimited
11+
mvn compile
12+
mvn install -DskipTests=true
13+
14+
cert=$(aws secretsmanager get-secret-value --secret-id "unit-test/certificate" --query "SecretString" | cut -f2 -d":" | cut -f2 -d\") && echo -e "$cert" > /tmp/certificate.pem
15+
key=$(aws secretsmanager get-secret-value --secret-id "unit-test/privatekey" --query "SecretString" | cut -f2 -d":" | cut -f2 -d\") && echo -e "$key" > /tmp/privatekey.pem
16+
17+

samples/BasicPubSub/src/main/java/pubsub/PubSub.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@
2929
import java.util.concurrent.ExecutionException;
3030

3131
public class PubSub {
32+
33+
// When run normally, we want to exit nicely even if something goes wrong
34+
// When run from CI, we want to let an exception escape which in turn causes the
35+
// exec:java task to return a non-zero exit code
36+
static String ciPropValue = System.getProperty("aws.crt.ci");
37+
static boolean isCI = ciPropValue != null && Boolean.valueOf(ciPropValue);
38+
3239
static String clientId = "test-" + UUID.randomUUID().toString();
3340
static String rootCaPath;
3441
static String certPath;
@@ -139,6 +146,7 @@ static void parseCommandLine(String[] args) {
139146
}
140147
break;
141148
case "-w":
149+
case "--websockets":
142150
useWebsockets = true;
143151
break;
144152
case "--x509":
@@ -200,21 +208,37 @@ static void onRejectedError(RejectedError error) {
200208
System.out.println("Request rejected: " + error.code.toString() + ": " + error.message);
201209
}
202210

211+
/*
212+
* When called during a CI run, throw an exception that will escape and fail the exec:java task
213+
* When called otherwise, print what went wrong (if anything) and just continue (return from main)
214+
*/
215+
static void onApplicationFailure(Throwable cause) {
216+
if (isCI) {
217+
throw new RuntimeException("BasicPubSub execution failure", cause);
218+
} else if (cause != null) {
219+
System.out.println("Exception encountered: " + cause.toString());
220+
}
221+
}
222+
203223
public static void main(String[] args) {
224+
204225
parseCommandLine(args);
205226
if (showHelp || endpoint == null) {
206227
printUsage();
228+
onApplicationFailure(null);
207229
return;
208230
}
209231

210232
if (!useWebsockets) {
211233
if (certPath == null || keyPath == null) {
212234
printUsage();
235+
onApplicationFailure(null);
213236
return;
214237
}
215238
} else if (useX509Credentials) {
216239
if (x509RoleAlias == null || x509Endpoint == null || x509Thing == null || x509CertPath == null || x509KeyPath == null) {
217240
printUsage();
241+
onApplicationFailure(null);
218242
return;
219243
}
220244
}
@@ -318,7 +342,7 @@ public void onConnectionResumed(boolean sessionPresent) {
318342
disconnected.get();
319343
}
320344
} catch (CrtRuntimeException | InterruptedException | ExecutionException ex) {
321-
System.out.println("Exception encountered: " + ex.toString());
345+
onApplicationFailure(ex);
322346
}
323347

324348
CrtResource.waitForNoResources();

0 commit comments

Comments
 (0)