Skip to content
This repository has been archived by the owner on Dec 12, 2024. It is now read-only.

Commit

Permalink
Merge branch 'master' of github.com:GoogleCloudPlatform/openjdk-runti…
Browse files Browse the repository at this point in the history
…me into common-test-application

* 'master' of github.com:GoogleCloudPlatform/openjdk-runtime:
  adding custom logging test + local runtimes-common integration test (#134)
  • Loading branch information
Alex Sloan committed Sep 8, 2017
2 parents 9ef3012 + ad4b0a8 commit 7dc5ed6
Show file tree
Hide file tree
Showing 17 changed files with 316 additions and 33 deletions.
8 changes: 7 additions & 1 deletion DEVELOPING.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,15 @@ $ ./scripts/integration_test.sh $RUNTIME_IMAGE

**Run ONLY Local Docker integration tests:**
```bash
$ ./scripts/local_integration_test.sh $RUNTIME_IMAGE
$ ./scripts/local_runtimes_common_integration_test.sh $RUNTIME_IMAGE
```

**Run ONLY Local shutdown tests:**
```bash
$ ./scripts/local_shutdown_test.sh $RUNTIME_IMAGE
```


**Run ONLY App Engine flexible environment integration tests:**
```bash
$ ./scripts/ae_integration_test.sh $RUNTIME_IMAGE
Expand Down
8 changes: 4 additions & 4 deletions java-runtimes-common/test-spring-application/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-monitoring</artifactId>
<version>0.20.1-alpha</version>
<version>0.22.0-alpha</version>
</dependency>
<dependency>
<groupId>com.google.auth</groupId>
<artifactId>google-auth-library-credentials</artifactId>
<version>0.7.0</version>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-logging</artifactId>
<version>1.4.0</version>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.google.cloud.runtimes;

import static org.springframework.web.bind.annotation.RequestMethod.POST;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static org.springframework.web.bind.annotation.RequestMethod.POST;

@RestController
public class ExceptionController {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.google.cloud.runtimes;

import static org.springframework.web.bind.annotation.RequestMethod.GET;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static org.springframework.web.bind.annotation.RequestMethod.GET;

@RestController
public class HelloController {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,28 @@

import com.google.cloud.runtimes.stackdriver.StackDriverMonitoringService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.logging.Logger;

import static com.google.cloud.ServiceOptions.getDefaultProjectId;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

@RestController
public class MonitoringTestController {

@Autowired
@Lazy
private StackDriverMonitoringService stackDriverMonitoringService;

@Autowired
@Qualifier("projectId")
private String projectId;

private static Logger LOG = Logger.getLogger(MonitoringTestController.class.getName());

public static class MonitoringTestRequest {
Expand Down Expand Up @@ -45,7 +51,7 @@ public String toString() {
public String handleMonitoringRequest(@RequestBody MonitoringTestRequest monitoringTestRequest) throws IOException, InterruptedException {
LOG.info(String.valueOf(monitoringTestRequest));

stackDriverMonitoringService.createMetricAndInsertTestToken(getDefaultProjectId(),
stackDriverMonitoringService.createMetricAndInsertTestToken(projectId,
monitoringTestRequest.name,
monitoringTestRequest.token);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@
import com.google.api.MetricDescriptor;
import com.google.api.MonitoredResource;
import com.google.cloud.monitoring.v3.MetricServiceClient;
import com.google.cloud.monitoring.v3.stub.MetricServiceStub;
import com.google.cloud.runtimes.config.GcpConfiguration;
import com.google.monitoring.v3.*;
import com.google.protobuf.util.Timestamps;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.io.IOException;
Expand All @@ -15,29 +22,41 @@
@Service
public class StackDriverMonitoringService {

@Autowired
private ApplicationContext applicationContext;

@Value("${monitoring.write.retries}")
private int maxRetries;
private static Logger LOG = Logger.getLogger(StackDriverMonitoringService.class.getName());


public void createMetricAndInsertTestToken(String projectId, String metricType, long metricValue) throws IOException {
MetricServiceClient metricServiceClient = MetricServiceClient.create();
int retries = maxRetries;
while (retries > 0) {
try {
CreateTimeSeriesRequest timeSeriesRequest = createTimeSeriesRequest(projectId, metricType, metricValue);
metricServiceClient.createTimeSeries(timeSeriesRequest);
getClient().createTimeSeries(timeSeriesRequest);
LOG.info("Metric created with timeseries.");
return;
} catch (Exception e) {
LOG.warning("error creating timeseries request, retrying..." + e.getClass() + ": " + e.getMessage());
retries--;
if (retries == 0) {
throw new IllegalStateException("Failed to store timeseries after "+ maxRetries +" attempts! Last error:", e);
throw new IllegalStateException("Failed to store timeseries after " + maxRetries + " attempts! Last error:", e);
}
}
}
}

/**
* This pairs up with the @Lazy annotation on {@link GcpConfiguration#getMetricServiceClient()}.
* As MetricServiceClient methods are all `final` and that breaks the "@Autowire @Lazy" combination,
* this is the only way to wire this bean lazily.
*/
private MetricServiceClient getClient() {
return (MetricServiceClient) applicationContext.getBean("metricServiceClient");
}

private CreateTimeSeriesRequest createTimeSeriesRequest(String projectId, String metricType, Long metricValue) {
LOG.info("Creating time series to insert token: " + metricValue);
ProjectName projectName = ProjectName.create(projectId);
Expand Down Expand Up @@ -66,4 +85,5 @@ private CreateTimeSeriesRequest createTimeSeriesRequest(String projectId, String
.build();
}


}
11 changes: 11 additions & 0 deletions openjdk-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@

<build>
<plugins>
<plugin>
<artifactId>maven-enforcer-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>enforce</goal>
</goals>
<phase>validate</phase>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
Expand Down
4 changes: 4 additions & 0 deletions openjdk8/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@
<id>common-structure-test</id>
<phase>integration-test</phase>
</execution>
<execution>
<id>local-shutdown-test</id>
<phase>integration-test</phase>
</execution>
<!-- Execute structure tests specific to this module -->
<execution>
<id>openjdk8-structure-test</id>
Expand Down
4 changes: 4 additions & 0 deletions openjdk9/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@
<id>common-structure-test</id>
<phase>integration-test</phase>
</execution>
<execution>
<id>local-shutdown-test</id>
<phase>integration-test</phase>
</execution>
<!-- Execute structure tests specific to this module -->
<execution>
<id>openjdk9-structure-test</id>
Expand Down
42 changes: 25 additions & 17 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,24 +67,16 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>1.4.1</version>
<executions>
<execution>
<id>enforce-maven</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<requireMavenVersion>
<version>[3.0,)</version>
</requireMavenVersion>
<requireJavaVersion>
<configuration>
<rules>
<requireMavenVersion>
<version>[3.3,)</version>
</requireMavenVersion>
<requireJavaVersion>
<version>[1.8,)</version>
</requireJavaVersion>
</rules>
</configuration>
</execution>
</executions>
</requireJavaVersion>
</rules>
</configuration>
</plugin>

<plugin>
Expand Down Expand Up @@ -191,6 +183,22 @@
</arguments>
</configuration>
</execution>
<execution>
<id>local-shutdown-test</id>
<!-- Do not bind to a phase, so that child modules can explicitly choose the phase for
this execution -->
<phase>none</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>${project.parent.basedir}/scripts/local_shutdown_test.sh</executable>
<workingDirectory>${project.build.outputDirectory}</workingDirectory>
<arguments>
<argument>${docker.image.name}</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>

Expand Down
1 change: 1 addition & 0 deletions scripts/gcloud-init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,4 @@ gcloud auth activate-service-account --key-file=$KEYFILE
gcloud config set project $GCP_PROJECT
gcloud config set compute/zone us-east1-b
gcloud components install beta kubectl -q
gcloud components install beta container-builder-local
2 changes: 1 addition & 1 deletion scripts/integration_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ fi
# is not recommended
readonly gaeDeploymentVersion=$2

${dir}/local_integration_test.sh ${imageUnderTest}
${dir}/local_shutdown_test.sh ${imageUnderTest}

${dir}/ae_integration_test.sh ${imageUnderTest} ${gaeDeploymentVersion}

Expand Down
1 change: 0 additions & 1 deletion scripts/integration_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ steps:
- name: 'gcr.io/java-runtime-test/integration-test'
args:
- '--url=${_DEPLOYED_APP_URL}'
- '--skip-custom-logging-tests' # blocked by b/33415496
- '--skip-standard-logging-tests' # blocked by b/33415496
- '--skip-custom-tests'

109 changes: 109 additions & 0 deletions scripts/local_runtimes_common_integration_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
#!/bin/bash

# Copyright 2017 Google Inc. All rights reserved.

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

# http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


# exit on command failure
set -e

readonly dir=$(dirname $0)
readonly projectRoot="$dir/.."
readonly testAppDir="$projectRoot/test-application"
readonly deployDir="$testAppDir/target/deploy"

APP_IMAGE='openjdk-local-integration'
CONTAINER=${APP_IMAGE}-container
OUTPUT_FILE=${CONTAINER}-output.txt
DEPLOYMENT_TOKEN=$(uuidgen)

readonly imageUnderTest=$1
if [[ -z "$imageUnderTest" ]]; then
echo "Usage: ${0} <image_under_test>"
exit 1
fi

if [[ ! -f $HOME/.config/gcloud/application_default_credentials.json ]]; then
# get default application credentials
gcloud auth application-default login
fi

# build the test app
pushd ${testAppDir}
mvn clean package -Ddeployment.token="${DEPLOYMENT_TOKEN}" -DskipTests --batch-mode
popd

# build app container locally
pushd $deployDir
export STAGING_IMAGE=$imageUnderTest
envsubst < Dockerfile.in > Dockerfile
echo "Building app container..."
docker build -t $APP_IMAGE . || docker build -t $APP_IMAGE .

docker rm -f $CONTAINER || echo "Integration-test-app container is not running, ready to start a new instance."

# run app container locally to test shutdown logging
echo "Starting app container..."
docker run --rm --name $CONTAINER -p 8080 \
-e "SHUTDOWN_LOGGING_THREAD_DUMP=true" \
-e "SHUTDOWN_LOGGING_HEAP_INFO=true" \
-v "$HOME/.config/gcloud/:/root/.config/gcloud" $APP_IMAGE &> $OUTPUT_FILE &

function waitForOutput() {
found_output='false'
for run in {1..10}
do
grep "$1" $OUTPUT_FILE && found_output='true' && break
sleep 1
done

if [ "$found_output" == "false" ]; then
cat $OUTPUT_FILE
echo "did not match '$1' in '$OUTPUT_FILE'"
exit 1
fi
}

waitForOutput 'Started Application'

getPort() {
docker inspect --format='{{range $p, $conf := .NetworkSettings.Ports}}{{(index $conf 0).HostPort}}{{end}}' ${CONTAINER}
}


PORT=`getPort`

nslookup `hostname` | grep Address | grep -v 127.0 | awk '{print $2}' > /tmp/myip
MYIP=`cat /tmp/myip`

DEPLOYED_APP_URL=http://$MYIP:$PORT

echo app is deployed to $DEPLOYED_APP_URL, making sure it accepts connections


until [[ $(curl --silent --fail "$DEPLOYED_APP_URL/deployment.token" | grep "$DEPLOYMENT_TOKEN") ]]; do
sleep 2
done
popd

docker rm -f metadata || echo "ready to run local cloud builder"

# run in cloud container builder
echo "Running integration tests on application that is deployed at $DEPLOYED_APP_URL"
echo `pwd`
container-builder-local \
--config ${dir}/integration_test.yaml \
--substitutions "_DEPLOYED_APP_URL=$DEPLOYED_APP_URL" \
--dryrun=false \
${dir}
File renamed without changes.
Loading

0 comments on commit 7dc5ed6

Please sign in to comment.