Skip to content

Commit

Permalink
More fixes and more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jianghaolu committed Dec 7, 2016
1 parent 599409b commit afe631b
Show file tree
Hide file tree
Showing 10 changed files with 241 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,7 @@ interface WithSourceControl<FluentT> {
*/
interface WithCreate<FluentT> extends
Creatable<FluentT>,
GroupableResource.DefinitionWithTags<WithCreate<FluentT>>,
WithSiteEnabled<FluentT>,
WithScmSiteAlsoStopped<FluentT>,
WithClientAffinityEnabled<FluentT>,
Expand Down Expand Up @@ -991,6 +992,7 @@ interface WithSourceControl<FluentT> {
*/
interface Update<FluentT> extends
Appliable<FluentT>,
GroupableResource.UpdateWithTags<Update<FluentT>>,
UpdateStages.WithHostNameBinding<FluentT>,
UpdateStages.WithHostNameSslBinding<FluentT>,
UpdateStages.WithClientAffinityEnabled<FluentT>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,6 @@ public String name() {
return name;
}

@Override
public DeploymentSlotImpl refresh() {
SiteInner inner = client.getSlot(resourceGroupName(), parent.name(), name());
inner.withSiteConfig(client.getConfigurationSlot(resourceGroupName(), parent.name(), name()));
setInner(inner);
return this;
}

@Override
public Map<String, HostNameBinding> getHostNameBindings() {
List<HostNameBindingInner> collectionInner = client.listHostNameBindingsSlot(resourceGroupName(), parent.name(), name());
Expand Down Expand Up @@ -143,6 +135,11 @@ Observable<SiteInner> getInner() {
return client.getSlotAsync(resourceGroupName(), parent.name(), name());
}

@Override
Observable<SiteConfigInner> getConfigInner() {
return client.getConfigurationSlotAsync(resourceGroupName(), parent().name(), name());
}

@Override
Observable<SiteConfigInner> createOrUpdateSiteConfig(SiteConfigInner siteConfig) {
return client.createOrUpdateConfigurationSlotAsync(resourceGroupName(), parent.name(), name(), siteConfig);
Expand Down Expand Up @@ -191,11 +188,13 @@ Observable<SiteSourceControlInner> createOrUpdateSourceControl(SiteSourceControl
@Override
public void swap(String slotName) {
client.swapSlotSlot(resourceGroupName(), parent().name(), name(), new CsmSlotEntityInner().withTargetSlot(slotName));
refresh();
}

@Override
public void applySlotConfigurations(String slotName) {
client.applySlotConfigurationSlot(resourceGroupName(), parent().name(), name(), new CsmSlotEntityInner().withTargetSlot(slotName));
refresh();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class DeploymentSlotsImpl
@Override
public DeploymentSlot typeConvert(SiteInner siteInner) {
siteInner.withSiteConfig(innerCollection.getConfiguration(siteInner.resourceGroup(), siteInner.name()));
return wrapModel(siteInner);
return wrapModel(siteInner).cacheAppSettingsAndConnectionStrings().toBlocking().single();
}
};
}
Expand All @@ -58,7 +58,7 @@ protected DeploymentSlotImpl wrapModel(SiteInner inner) {
if (inner == null) {
return null;
}
return new DeploymentSlotImpl(inner.name(), inner, null, parent, innerCollection, super.manager, serviceClient);
return new DeploymentSlotImpl(inner.name(), inner, inner.siteConfig(), parent, innerCollection, super.manager, serviceClient);
}

protected PagedList<DeploymentSlot> wrapList(PagedList<SiteInner> pagedList) {
Expand All @@ -77,7 +77,7 @@ public DeploymentSlot getByParent(String resourceGroup, String parentName, Strin
return null;
}
siteInner.withSiteConfig(innerCollection.getConfigurationSlot(resourceGroup, parentName, name));
return wrapModel(siteInner);
return wrapModel(siteInner).cacheAppSettingsAndConnectionStrings().toBlocking().single();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,8 @@ public ConnectionString apply(String input) {

abstract Observable<SiteInner> getInner();

abstract Observable<SiteConfigInner> getConfigInner();

abstract Observable<SiteConfigInner> createOrUpdateSiteConfig(SiteConfigInner siteConfig);

abstract Observable<Void> deleteHostNameBinding(String hostname);
Expand Down Expand Up @@ -1122,4 +1124,12 @@ public FluentImplT withoutSourceControl() {
sourceControlToDelete = true;
return (FluentImplT) this;
}

@Override
public FluentT refresh() {
SiteInner inner = getInner().toBlocking().single();
inner.withSiteConfig(getConfigInner().toBlocking().single());
setInner(inner);
return this.cacheAppSettingsAndConnectionStrings().toBlocking().single();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ Observable<SiteInner> getInner() {
return client.getAsync(resourceGroupName(), name());
}

@Override
Observable<SiteConfigInner> getConfigInner() {
return client.getConfigurationAsync(resourceGroupName(), name());
}

@Override
Observable<SiteConfigInner> createOrUpdateSiteConfig(SiteConfigInner siteConfig) {
return client.createOrUpdateConfigurationAsync(resourceGroupName(), name(), siteConfig);
Expand Down Expand Up @@ -180,24 +185,20 @@ public void restart() {
@Override
public void swap(String slotName) {
client.swapSlotWithProduction(resourceGroupName(), name(), new CsmSlotEntityInner().withTargetSlot(slotName));
refresh();
}

@Override
public void applySlotConfigurations(String slotName) {
client.applySlotConfigToProduction(resourceGroupName(), name(), new CsmSlotEntityInner().withTargetSlot(slotName));
refresh();
}

@Override
public void resetSlotConfigurations() {
client.resetProductionSlotConfig(resourceGroupName(), name());
}

@Override
public WebAppImpl refresh() {
this.setInner(client.get(resourceGroupName(), name()));
return this;
}

@Override
public WebAppImpl withNewAppServicePlan(String name) {
appServicePlan = (AppServicePlanImpl) myManager.appServicePlans().define(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static void cleanup() throws Exception {
}

@Test
public void canCRUDWebApp() throws Exception {
public void canCRUDSwapSlots() throws Exception {
// Create web app
WebApp webApp = appServiceManager.webApps().define(WEBAPP_NAME)
.withNewResourceGroup(RG_NAME)
Expand All @@ -55,9 +55,11 @@ public void canCRUDWebApp() throws Exception {
// Create a deployment slot with empty config
DeploymentSlot slot1 = webApp.deploymentSlots().define(SLOT_NAME_1)
.withBrandNewConfiguration()
.withPythonVersion(PythonVersion.PYTHON_27)
.create();
Assert.assertNotNull(slot1);
Assert.assertNotEquals(JavaVersion.JAVA_1_7_0_51, slot1.javaVersion());
Assert.assertEquals(PythonVersion.PYTHON_27, slot1.pythonVersion());
Map<String, AppSetting> appSettingMap = slot1.appSettings();
Assert.assertFalse(appSettingMap.containsKey("appkey"));
Assert.assertFalse(appSettingMap.containsKey("stickykey"));
Expand Down Expand Up @@ -86,7 +88,8 @@ public void canCRUDWebApp() throws Exception {
slot2.update()
.withoutJava()
.withPythonVersion(PythonVersion.PYTHON_34)
.withStickyAppSetting("slot2key", "slot2value")
.withAppSetting("slot2key", "slot2value")
.withStickyAppSetting("sticky2key", "sticky2value")
.apply();
Assert.assertNotNull(slot2);
Assert.assertEquals(JavaVersion.OFF, slot2.javaVersion());
Expand All @@ -111,5 +114,16 @@ public void canCRUDWebApp() throws Exception {
// List
List<DeploymentSlot> deploymentSlots = webApp.deploymentSlots().list();
Assert.assertEquals(3, deploymentSlots.size());

// Swap
slot3.swap(slot1.name());
slot1 = webApp.deploymentSlots().getByName(SLOT_NAME_1);
Assert.assertEquals(JavaVersion.OFF, slot1.javaVersion());
Assert.assertEquals(PythonVersion.PYTHON_34, slot1.pythonVersion());
Assert.assertEquals(PythonVersion.PYTHON_27, slot3.pythonVersion());
Assert.assertEquals("appvalue", slot1.appSettings().get("appkey").value());
Assert.assertEquals("slot2value", slot1.appSettings().get("slot2key").value());
Assert.assertEquals("sticky2value", slot3.appSettings().get("sticky2key").value());
Assert.assertEquals("stickyvalue", slot3.appSettings().get("stickykey").value());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for
* license information.
*/

package com.microsoft.azure.management.appservice;

import com.microsoft.azure.management.resources.fluentcore.arm.Region;
import com.microsoft.azure.management.resources.fluentcore.utils.ResourceNamer;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

import javax.net.ssl.SSLPeerUnverifiedException;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import static org.junit.Assert.fail;

public class HostnameSslTests extends AppServiceTestBase {
private static final String RG_NAME = ResourceNamer.randomResourceName("javacsmrg", 20);
private static final String WEBAPP_NAME = ResourceNamer.randomResourceName("java-webapp-", 20);
private static final String APP_SERVICE_PLAN_NAME = ResourceNamer.randomResourceName("java-asp-", 20);
private static OkHttpClient httpClient = new OkHttpClient.Builder().readTimeout(1, TimeUnit.MINUTES).build();
private String DOMAIN = domain.name();

@BeforeClass
public static void setup() throws Exception {
createClients();
}

@AfterClass
public static void cleanup() throws Exception {
resourceManager.resourceGroups().deleteByName(RG_NAME);
}

@Test
public void canBindHostnameAndSsl() throws Exception {
// hostname binding
appServiceManager.webApps().define(WEBAPP_NAME)
.withNewResourceGroup(RG_NAME)
.withNewAppServicePlan(APP_SERVICE_PLAN_NAME)
.withRegion(Region.US_WEST)
.withPricingTier(AppServicePricingTier.BASIC_B1)
.defineHostnameBinding()
.withAzureManagedDomain(domain)
.withSubDomain(WEBAPP_NAME)
.withDnsRecordType(CustomHostNameDnsRecordType.CNAME)
.attach()
.create();

WebApp webApp = appServiceManager.webApps().getByGroup(RG_NAME, WEBAPP_NAME);
Assert.assertNotNull(webApp);
Response response = curl("http://" + WEBAPP_NAME + "." + DOMAIN);
Assert.assertEquals(200, response.code());
Assert.assertNotNull(response.body().string());

// hostname binding shortcut
webApp.update()
.withManagedHostnameBindings(domain, WEBAPP_NAME + "-1", WEBAPP_NAME + "-2")
.apply();
response = curl("http://" + WEBAPP_NAME + "-1." + DOMAIN);
Assert.assertEquals(200, response.code());
Assert.assertNotNull(response.body().string());
response = curl("http://" + WEBAPP_NAME + "-2." + DOMAIN);
Assert.assertEquals(200, response.code());
Assert.assertNotNull(response.body().string());

// SSL binding
webApp.update()
.defineSslBinding()
.forHostname(WEBAPP_NAME + "." + DOMAIN)
.withExistingAppServiceCertificateOrder(certificateOrder)
.withSniBasedSsl()
.attach()
.apply();
response = null;
int retryCount = 3;
while (response == null && retryCount > 0) {
try {
response = curl("https://" + WEBAPP_NAME + "." + DOMAIN);
} catch (SSLPeerUnverifiedException e) {
retryCount--;
Thread.sleep(5000);
}
}
if (retryCount == 0) {
fail();
}
Assert.assertEquals(200, response.code());
Assert.assertNotNull(response.body().string());
}

private static Response curl(String url) throws IOException {
Request request = new Request.Builder().url(url).get().build();
return httpClient.newCall(request).execute();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for
* license information.
*/

package com.microsoft.azure.management.appservice;

import com.microsoft.azure.management.resources.fluentcore.arm.Region;
import com.microsoft.azure.management.resources.fluentcore.utils.ResourceNamer;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class SourceControlTests extends AppServiceTestBase {
private static final String RG_NAME = ResourceNamer.randomResourceName("javacsmrg", 20);
private static final String WEBAPP_NAME = ResourceNamer.randomResourceName("java-webapp-", 20);
private static final String SLOT_NAME = ResourceNamer.randomResourceName("java-slot-", 20);
private static final String APP_SERVICE_PLAN_NAME = ResourceNamer.randomResourceName("java-asp-", 20);
private static OkHttpClient httpClient = new OkHttpClient.Builder().readTimeout(1, TimeUnit.MINUTES).build();

@BeforeClass
public static void setup() throws Exception {
createClients();
}

@AfterClass
public static void cleanup() throws Exception {
resourceManager.resourceGroups().deleteByName(RG_NAME);
}

@Test
public void canDeploySourceControl() throws Exception {
// Create web app
WebApp webApp = appServiceManager.webApps().define(WEBAPP_NAME)
.withNewResourceGroup(RG_NAME)
.withNewAppServicePlan(APP_SERVICE_PLAN_NAME)
.withRegion(Region.US_WEST)
.withPricingTier(AppServicePricingTier.BASIC_B1)
.defineSourceControl()
.withPublicExternalRepository()
.withGit("https://github.com/Azure-Samples/app-service-web-nodejs-get-started")
.withBranch("master")
.attach()
.withConnectionString("StorageConnectionString",
"DefaultEndpointsProtocol=https;AccountName=azurejsdkdemo;AccountKey=Brtk1ZZIiSdPAYATXJwwg8xyeWkyqUmJf0m1dH5caQZJSqKpPksUhHXeW9vo+fSg9BE7I+7dYFEWkZy+VQmCcw==",
ConnectionStringType.CUSTOM)
.create();
Assert.assertNotNull(webApp);
Response response = curl("http://" + WEBAPP_NAME + "." + "azurewebsites.net");
Assert.assertEquals(200, response.code());
String body = response.body().string();
Assert.assertNotNull(body);
Assert.assertTrue(body.contains("Hello world from linux 4"));


// auto swap
DeploymentSlot slot = webApp.deploymentSlots().define(SLOT_NAME)
.withBrandNewConfiguration()
.withPythonVersion(PythonVersion.PYTHON_27)
.create();
Assert.assertNotNull(slot);
Assert.assertNotEquals(JavaVersion.JAVA_1_7_0_51, slot.javaVersion());
Assert.assertEquals(PythonVersion.PYTHON_27, slot.pythonVersion());
Map<String, AppSetting> appSettingMap = slot.appSettings();
Assert.assertFalse(appSettingMap.containsKey("appkey"));
Assert.assertFalse(appSettingMap.containsKey("stickykey"));
Map<String, ConnectionString> connectionStringMap = slot.connectionStrings();
Assert.assertFalse(connectionStringMap.containsKey("connectionName"));
Assert.assertFalse(connectionStringMap.containsKey("stickyName"));


}

private static Response curl(String url) throws IOException {
Request request = new Request.Builder().url(url).get().build();
return httpClient.newCall(request).execute();
}
}
Loading

0 comments on commit afe631b

Please sign in to comment.