Skip to content

Commit

Permalink
Provide support for tests to access a VCAP Services JSON object to
Browse files Browse the repository at this point in the history
retrieve service credential information.  Make access available via the
VCAP_SERVICES environment variable or thru a JSON file.
  • Loading branch information
jasonpet authored and rabbah committed May 10, 2016
1 parent c56d4fc commit f9b4f48
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
3 changes: 3 additions & 0 deletions config/writePropertyFile.sh
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ echo "whisk.ssl.cert=$WHISK_SSL_CERTIFICATE" >> "$WHISK_HOME/whisk.properties"
echo "whisk.ssl.key=$WHISK_SSL_KEY" >> "$WHISK_HOME/whisk.properties"
echo "whisk.ssl.challenge=$WHISK_SSL_CHALLENGE" >> "$WHISK_HOME/whisk.properties"

# VCAP Services file to use for tests
echo "vcap.services.file=$VCAP_SERVICES_FILE" >> "$WHISK_HOME/whisk.properties"

#Hosts
echo "activator.host="$ACTIVATOR_HOST >> "$WHISK_HOME/whisk.properties"
echo "consulserver.host="$CONSULSERVER_HOST >> "$WHISK_HOME/whisk.properties"
Expand Down
23 changes: 23 additions & 0 deletions tests/src/common/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
Expand All @@ -27,6 +28,8 @@
import java.util.logging.Level;
import java.util.logging.Logger;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;

Expand Down Expand Up @@ -54,6 +57,9 @@ public class TestUtils {

private static final File catalogDir = WhiskProperties.getFileRelativeToWhiskHome("catalog");
private static final File testActionsDir = WhiskProperties.getFileRelativeToWhiskHome("tests/dat/actions");
private static final File vcapFile = WhiskProperties.getVCAPServicesFile();
private static final String envServices = System.getenv("VCAP_SERVICES");


static {
logger.setLevel(Level.WARNING);
Expand All @@ -79,6 +85,23 @@ public static String getTestActionFilename(String name) {
return new File(testActionsDir, name).toString();
}

/**
* Gets the value of VCAP_SERVICES
* @return VCAP_SERVICES as a JSON object
*/
public static JsonObject getVCAPServices() {
try {
if (envServices != null) {
return new JsonParser().parse(envServices).getAsJsonObject();
} else {
return new JsonParser().parse(new FileReader(vcapFile)).getAsJsonObject();
}
} catch (Throwable t) {
System.out.println("faild to parse VCAP" + t);
return new JsonObject();
}
}

/**
* @return a junit {@link TestWatcher} that prints a message when each test starts and ends
*/
Expand Down
12 changes: 12 additions & 0 deletions tests/src/common/WhiskProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,18 @@ public static String readAuthKey(File filename) {
}
}

/**
* @return the path to a file holding the VCAP_SERVICES used during junit testing
*/
public static File getVCAPServicesFile() {
String vcapServices = whiskProperties.getProperty("vcap.services.file");
if (vcapServices.startsWith(File.separator)) {
return new File(vcapServices);
} else {
return WhiskProperties.getFileRelativeToWhiskHome(vcapServices);
}
}

/**
* are we running on Mac OS X?
*/
Expand Down

0 comments on commit f9b4f48

Please sign in to comment.