Skip to content

Commit

Permalink
Fixed failing junit related to proxy change. As of 1.6.0, proxy changes
Browse files Browse the repository at this point in the history
requires a client reset (e.g., disconnect and reconnect) via the CF
server behaviour. Added additional junits to test CF app module creation
and presence after connection/disconnect/reconnect.
  • Loading branch information
nierajsingh committed Mar 17, 2014
1 parent e496f1d commit 0dee580
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,14 @@ protected void findNonChangedResources(IModuleResourceDelta[] deltas, List<IModu
}
}

/**
* Disconnects the local server from the remote CF server, and terminate the
* session. Note that this will stop any refresh operations, or console
* streaming, but will NOT stop any apps that are currently running. It may
* also clear any application module caches associated with the session.
* @param monitor
* @throws CoreException
*/
public void disconnect(IProgressMonitor monitor) throws CoreException {
CloudFoundryPlugin.getCallback().disconnecting(getCloudFoundryServer());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ protected void connectClient(String username, String password) throws CoreExcept

/**
* Resets the client in the server behaviour based on the given credentials.
* If passing null, the stored server credentials will be used instead
* This will disconnect any existing session. If passing null, the stored
* server credentials will be used instead
* @param credentials
* @return
* @throws CoreException
Expand All @@ -129,7 +130,8 @@ protected void assertApplicationIsRunning(IModule module, String prefix) throws
}

protected void assertApplicationIsRunning(CloudFoundryApplicationModule appModule) throws Exception {
int attempts = 5;
int total = 10;
int attempts = total;
long wait = 10000;
boolean running = false;

Expand All @@ -149,7 +151,7 @@ protected void assertApplicationIsRunning(CloudFoundryApplicationModule appModul
}

// Verify separately that the app did indeed start
assertTrue("Application has not started after waiting for (ms): " + (wait * attempts), running);
assertTrue("Application has not started after waiting for (ms): " + (wait * total), running);
assertEquals(IServer.STATE_STARTED, appModule.getState());
assertEquals(AppState.STARTED, appModule.getApplication().getState());

Expand Down Expand Up @@ -313,12 +315,13 @@ protected CloudApplication getUpdatedApplication(String appName) throws CoreExce
}

/**
* Deploys application and starts it in the CF server.
* Deploys application and starts it in the CF server. Verifies the
* application is running.
* @param appPrefix
* @return
* @return deployed app.
* @throws Exception
*/
protected CloudFoundryApplicationModule deployApplicationStartMode(String appPrefix) throws Exception {
protected CloudFoundryApplicationModule assertDeployApplicationStartMode(String appPrefix) throws Exception {
CloudFoundryApplicationModule appModule = deployApplication(appPrefix, false);
assertApplicationIsRunning(appModule.getLocalModule(), appPrefix);
return appModule;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public void testNonCaldecottServiceUnbinding_STS_2767() throws Exception {
String prefix = "nonCaldecottServiceUnbinding";
createWebApplicationProject();

CloudFoundryApplicationModule appModule = deployApplicationStartMode(prefix);
CloudFoundryApplicationModule appModule = assertDeployApplicationStartMode(prefix);

CloudApplication nonCaldecottApp = appModule.getApplication();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,19 @@ protected void handleProxyChange() {

public void testInvalidProxyThroughServerInstance() throws Exception {

// NOTE: As of CF 1.6.0 (Cloud Foundry Java client 1.0.2), proxy changes
// REQUIRE a client change. They can no longer be changed during the
// same
// client session used by the CF server instance. Therefore, proxy
// changes
// require a new connection.

// Verify that connection and operations can be performed without the
// proxy change
String prefix = "InvalidProxyServerInstance";
createWebApplicationProject();

CloudFoundryApplicationModule appModule = deployApplicationStartMode(prefix);
CloudFoundryApplicationModule appModule = assertDeployApplicationStartMode(prefix);
final String appName = appModule.getDeployedApplicationName();

final boolean[] ran = { false };
Expand All @@ -90,13 +97,12 @@ public void testInvalidProxyThroughServerInstance() throws Exception {
@Override
protected void handleProxyChange() throws CoreException {

IModule[] modules = server.getModules();

IProxyService proxyService = getProxyService();
try {
// Reset the client to use the new proxy settings
connectClient();

IModule[] modules = server.getModules();
// Should fail, as its now going through invalid proxy
serverBehavior.stopModule(modules, null);

Expand All @@ -113,6 +119,9 @@ protected void handleProxyChange() throws CoreException {
proxyService.setProxiesEnabled(getOriginalProxiesEnabled());
proxyService.setProxyData(getOriginalProxyData());

connectClient();
IModule[] modules = server.getModules();

serverBehavior.stopModule(modules, null);

int moduleState = server.getModuleState(modules);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import java.net.URL;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

Expand Down Expand Up @@ -46,6 +47,37 @@ public void testDisconnect() throws Exception {
assertEquals(Collections.emptyList(), Arrays.asList(server.getModules()));
serverBehavior.disconnect(new NullProgressMonitor());
assertEquals(IServer.STATE_STOPPED, serverBehavior.getServer().getServerState());
assertEquals(Collections.emptyList(), cloudServer.getExistingCloudModules());
}

public void testCloudFoundryModuleCreation() throws Exception {
// Test that a cloud foundry module is created when an application is
// pushed
// using framework API rather than test harness API.

String appPrefix = "testCloudFoundryModuleCreation";
createWebApplicationProject();

// Configure the test fixture for deployment.
// This step is a substitute for the Application deployment wizard
String projectName = harness.getDefaultWebAppProjectName();
String expectedAppName = harness.getDefaultWebAppName(appPrefix);
getTestFixture().configureForApplicationDeployment(expectedAppName, false);

IModule module = getModule(projectName);

assertNotNull("Expected non-null IModule when deploying application", module);

serverBehavior.startModuleWaitForDeployment(new IModule[] { module }, null);

Collection<CloudFoundryApplicationModule> appModules = cloudServer.getExistingCloudModules();
assertNotNull("Expected list of cloud modules after deploying: " + appPrefix, appModules);
assertTrue("Expected one application module for " + appPrefix + " but got: " + appModules.size(),
appModules.size() == 1);

CloudFoundryApplicationModule applicationModule = appModules.iterator().next();
assertEquals(expectedAppName, applicationModule.getDeployedApplicationName());

}

public void testCreateDeployAppHelpersStartMode() throws Exception {
Expand All @@ -56,7 +88,7 @@ public void testCreateDeployAppHelpersStartMode() throws Exception {
createWebApplicationProject();

// Invoke the helper method
deployApplicationStartMode(prefix);
assertDeployApplicationStartMode(prefix);

// Verify it is deployed
CloudFoundryApplicationModule appModule = assertApplicationIsDeployed(prefix, IServer.STATE_STARTED);
Expand Down Expand Up @@ -113,22 +145,15 @@ public void testCreateDeployAppHelpersStopMode() throws Exception {
int moduleState = server.getModuleState(new IModule[] { module });
assertEquals(IServer.STATE_STOPPED, moduleState);

Exception error = null;
try {
assertApplicationIsRunning(module, prefix);
}
catch (Exception e) {
error = e;
}

assertNotNull("Expected error when checking if application is running", error);
assertFalse("Expected application to be stopped, but server behaviour indicated it is running",
serverBehavior.isApplicationRunning(appModule, new NullProgressMonitor()));

}

public void testStopApplication() throws Exception {
String prefix = "stopApplication";
createWebApplicationProject();
CloudFoundryApplicationModule appModule = deployApplicationStartMode(prefix);
CloudFoundryApplicationModule appModule = assertDeployApplicationStartMode(prefix);

assertStopModule(appModule);

Expand All @@ -142,12 +167,12 @@ public void testStartStopAfterDeployment() throws Exception {
// stopped and restarted, the application starts without problems
String prefix = "startStopApplication";
createWebApplicationProject();
CloudFoundryApplicationModule appModule = deployApplicationStartMode(prefix);
CloudFoundryApplicationModule appModule = assertDeployApplicationStartMode(prefix);

assertStopModule(appModule);

assertStartModule(appModule);

assertEquals(IServer.STATE_STARTED, appModule.getState());
assertEquals(AppState.STARTED, appModule.getApplication().getState());
}
Expand All @@ -170,7 +195,7 @@ public void testServerBehaviourIsApplicationRunning() throws Exception {
String prefix = "isApplicationRunning";
createWebApplicationProject();

CloudFoundryApplicationModule appModule = deployApplicationStartMode(prefix);
CloudFoundryApplicationModule appModule = assertDeployApplicationStartMode(prefix);

// Verify that the server behaviour API to determine that an app is
// running tests correctly
Expand All @@ -196,7 +221,7 @@ public void testStartModuleInvalidToken() throws Exception {
}

try {
deployApplicationStartMode(prefix);
assertDeployApplicationStartMode(prefix);
}
catch (CoreException ce) {
assertNotNull(ce);
Expand All @@ -212,7 +237,7 @@ public void testStartModuleInvalidToken() throws Exception {
connectClient();

// Starting the app should now pass without errors
deployApplicationStartMode(prefix);
assertDeployApplicationStartMode(prefix);

}

Expand All @@ -229,7 +254,7 @@ public void testStartModuleInvalidPassword() throws Exception {
CloudCredentials credentials = new CloudCredentials(userName, "invalid-password");
connectClient(credentials);

deployApplicationStartMode(prefix);
assertDeployApplicationStartMode(prefix);

fail("Expected CoreException due to invalid password");
}
Expand All @@ -240,9 +265,7 @@ public void testStartModuleInvalidPassword() throws Exception {
connectClient();

// Should now deploy without errors
deployApplicationStartMode(prefix);

assertApplicationIsDeployed(prefix, IServer.STATE_STARTED);
assertDeployApplicationStartMode(prefix);

}

Expand All @@ -258,7 +281,7 @@ public void testDeleteModuleExternally() throws Exception {
String appName = harness.getDefaultWebAppName(prefix);
createWebApplicationProject();

deployApplicationStartMode(prefix);
assertDeployApplicationStartMode(prefix);

List<CloudApplication> applications = serverBehavior.getApplications(new NullProgressMonitor());
boolean found = false;
Expand Down Expand Up @@ -295,6 +318,107 @@ public void testDeleteModuleExternally() throws Exception {
assertFalse(found);
}

public void testCloudModulesClearedOnDisconnect() throws Exception {
// Test the following:
// Create an application and deploy it.
// Then disconnect and verify that the app module cache is cleared.

String appPrefix = "testCloudModulesClearedOnDisconnect";
createWebApplicationProject();
assertDeployApplicationStartMode(appPrefix);

// Cloud module should have been created.
Collection<CloudFoundryApplicationModule> appModules = cloudServer.getExistingCloudModules();
assertEquals(harness.getDefaultWebAppName(appPrefix), appModules.iterator().next().getDeployedApplicationName());

serverBehavior.disconnect(new NullProgressMonitor());

appModules = cloudServer.getExistingCloudModules();

assertTrue("Expected empty list of cloud application modules after server disconnect", appModules.isEmpty());
}

public void testCloudModulesCreatedForExistingApps() throws Exception {
// Test the following:
// Create an application and deploy it.
// Disconnect (which clears cloud module cache).
// Re-connect again and verify that cloud modules are created for the
// deployed
// app.

String appPrefix = "testCloudModulesCreatedForExistingApps";
createWebApplicationProject();
assertDeployApplicationStartMode(appPrefix);

// Cloud module should have been created.
Collection<CloudFoundryApplicationModule> appModules = cloudServer.getExistingCloudModules();
assertEquals(harness.getDefaultWebAppName(appPrefix), appModules.iterator().next().getDeployedApplicationName());

serverBehavior.disconnect(new NullProgressMonitor());

appModules = cloudServer.getExistingCloudModules();

assertTrue("Expected empty list of cloud application modules after server disconnect", appModules.isEmpty());

serverBehavior.connect(new NullProgressMonitor());

appModules = cloudServer.getExistingCloudModules();
assertEquals(harness.getDefaultWebAppName(appPrefix), appModules.iterator().next().getDeployedApplicationName());

}

public void testApplicationRemainsStartedAfterDisconnectedBehaviour() throws Exception {
// Deploy and start an application.
// Disconnect through the server behaviour. Verify through an external
// client that the app
// remains deployed and in started mode.
// Reconnect, and verify that the application is still running (i.e.
// disconnecting
// the server should not stop the application).

String appPrefix = "testApplicationRemainsStartedAfterDisconnected";
String expectedAppName = harness.getDefaultWebAppName(appPrefix);

createWebApplicationProject();
assertDeployApplicationStartMode(appPrefix);

// Cloud module should have been created.
Collection<CloudFoundryApplicationModule> appModules = cloudServer.getExistingCloudModules();
assertEquals(harness.getDefaultWebAppName(appPrefix), appModules.iterator().next().getDeployedApplicationName());

// Disconnect and verify that there are no cloud foundry application
// modules
serverBehavior.disconnect(new NullProgressMonitor());
appModules = cloudServer.getExistingCloudModules();
assertTrue("Expected empty list of cloud application modules after server disconnect but got list with size: "
+ appModules.size(), appModules.isEmpty());

// Now create an external client to independently check that the
// application remains deployed and in started mode
URL url = new URL(getTestFixture().getUrl());
CloudFoundryOperations client = CloudFoundryPlugin.getCloudFoundryClientFactory().getCloudFoundryOperations(
new CloudCredentials(getTestFixture().getCredentials().userEmail,
getTestFixture().getCredentials().password), url, false);
client.login();
List<CloudApplication> deployedApplications = client.getApplications();
assertTrue("Expected one cloud application for " + appPrefix + " but got: " + deployedApplications.size(),
deployedApplications.size() == 1);
assertEquals(expectedAppName, deployedApplications.get(0).getName());
assertTrue(deployedApplications.get(0).getState() == AppState.STARTED);

// Re-connect through the server behaviour. It should re-create a cloud
// application module for the deployed
// application
serverBehavior.connect(new NullProgressMonitor());

appModules = cloudServer.getExistingCloudModules();
CloudFoundryApplicationModule appModule = appModules.iterator().next();

assertEquals(expectedAppName, appModule.getDeployedApplicationName());

assertApplicationIsRunning(appModule);
}

@Override
protected CloudFoundryTestFixture getTestFixture() throws CoreException {
return CloudFoundryTestFixture.getTestFixture();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void testServiceBinding() throws Exception {
CloudService service = createService();
String prefix = "testServiceBinding";
createWebApplicationProject();
CloudFoundryApplicationModule appModule = deployApplicationStartMode(prefix);
CloudFoundryApplicationModule appModule = assertDeployApplicationStartMode(prefix);

CloudApplication app = appModule.getApplication();
assertStopModule(appModule);
Expand All @@ -48,7 +48,7 @@ public void testServiceUnBinding() throws Exception {
String prefix = "testServiceUnbinding";
createWebApplicationProject();

CloudFoundryApplicationModule appModule = deployApplicationStartMode(prefix);
CloudFoundryApplicationModule appModule = assertDeployApplicationStartMode(prefix);

CloudApplication app = appModule.getApplication();

Expand Down

0 comments on commit 0dee580

Please sign in to comment.