Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mgmt, iothub, add live tests #35371

Merged
merged 8 commits into from
Jun 9, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.resourcemanager.iothub.generated;

import com.azure.core.credential.TokenCredential;
import com.azure.core.http.policy.HttpLogDetailLevel;
import com.azure.core.http.policy.HttpLogOptions;
import com.azure.core.management.AzureEnvironment;
import com.azure.core.management.Region;
import com.azure.core.management.profile.AzureProfile;
import com.azure.core.test.TestBase;
import com.azure.core.test.annotation.DoNotRecord;
import com.azure.core.util.Configuration;
import com.azure.core.util.CoreUtils;
import com.azure.identity.DefaultAzureCredentialBuilder;
import com.azure.resourcemanager.iothub.IotHubManager;
import com.azure.resourcemanager.iothub.models.ArmIdentity;
import com.azure.resourcemanager.iothub.models.Capabilities;
import com.azure.resourcemanager.iothub.models.CloudToDeviceProperties;
import com.azure.resourcemanager.iothub.models.EventHubProperties;
import com.azure.resourcemanager.iothub.models.FallbackRouteProperties;
import com.azure.resourcemanager.iothub.models.FeedbackProperties;
import com.azure.resourcemanager.iothub.models.IotHubDescription;
import com.azure.resourcemanager.iothub.models.IotHubProperties;
import com.azure.resourcemanager.iothub.models.IotHubSku;
import com.azure.resourcemanager.iothub.models.IotHubSkuInfo;
import com.azure.resourcemanager.iothub.models.MessagingEndpointProperties;
import com.azure.resourcemanager.iothub.models.RoutingProperties;
import com.azure.resourcemanager.iothub.models.RoutingSource;
import com.azure.resourcemanager.iothub.models.ResourceIdentityType;
import com.azure.resourcemanager.iothub.models.StorageEndpointProperties;
import com.azure.resourcemanager.resources.ResourceManager;
import io.netty.util.internal.StringUtil;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.time.Duration;
import java.util.Arrays;
import java.util.Map;
import java.util.HashMap;
import java.util.Random;

public class IotHubManagerTests extends TestBase {

private static final Random RANDOM = new Random();
private static final Region REGION = Region.US_WEST2;
private String resourceGroupName = "rg" + randomPadding();
private IotHubManager iotHubManager;
private ResourceManager resourceManager;
private boolean testEnv;

@Override
public void beforeTest() {
final TokenCredential credential = new DefaultAzureCredentialBuilder().build();
final AzureProfile profile = new AzureProfile(AzureEnvironment.AZURE);

iotHubManager = IotHubManager
.configure()
.withLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BASIC))
.authenticate(credential, profile);

resourceManager = ResourceManager
.configure()
.withLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BASIC))
.authenticate(credential, profile)
.withDefaultSubscription();

// use AZURE_RESOURCE_GROUP_NAME if run in LIVE CI
String testResourceGroup = Configuration.getGlobalConfiguration().get("AZURE_RESOURCE_GROUP_NAME");
testEnv = !CoreUtils.isNullOrEmpty(testResourceGroup);
if (testEnv) {
resourceGroupName = testResourceGroup;
} else {
resourceManager.resourceGroups()
.define(resourceGroupName)
.withRegion(REGION)
.create();
}
}

@Override
protected void afterTest() {
if (!testEnv) {
resourceManager.resourceGroups().beginDeleteByName(resourceGroupName);
}
}

@Test
@DoNotRecord(skipInPlayback = true)
public void testIotHubDescription() {
IotHubDescription iotHubDescription = null;
try {
String descriptionName = "iotHub" + randomPadding();
v-hongli1 marked this conversation as resolved.
Show resolved Hide resolved

Map<String, EventHubProperties> eventHubEndpointsMap = new HashMap<>();
eventHubEndpointsMap.put("events", new EventHubProperties()
.withRetentionTimeInDays(1L).withPartitionCount(2));

Map<String, StorageEndpointProperties> storageEndpointsMap = new HashMap<>();
storageEndpointsMap.put("$default", new StorageEndpointProperties()
.withSasTtlAsIso8601(Duration.ofHours(1L))
.withConnectionString(StringUtil.EMPTY_STRING)
.withContainerName(StringUtil.EMPTY_STRING));

Map<String, MessagingEndpointProperties> messagingEndpointsMap = new HashMap<>();
messagingEndpointsMap.put("fileNotifications", new MessagingEndpointProperties()
.withLockDurationAsIso8601(Duration.ofMinutes(1L))
.withTtlAsIso8601(Duration.ofHours(1L))
.withMaxDeliveryCount(10));

// embedmeStart
v-hongli1 marked this conversation as resolved.
Show resolved Hide resolved
iotHubDescription = iotHubManager.iotHubResources()
.define(descriptionName)
.withRegion(REGION)
.withExistingResourceGroup(resourceGroupName)
.withSku(new IotHubSkuInfo().withName(IotHubSku.F1).withCapacity(1L))
.withIdentity(new ArmIdentity().withType(ResourceIdentityType.NONE))
.withProperties(
new IotHubProperties()
.withEventHubEndpoints(eventHubEndpointsMap)
.withRouting(new RoutingProperties()
.withFallbackRoute(
new FallbackRouteProperties()
.withName("$fallback")
.withSource(RoutingSource.DEVICE_MESSAGES)
.withCondition("true")
.withIsEnabled(true)
.withEndpointNames(Arrays.asList("events"))))
.withStorageEndpoints(storageEndpointsMap)
.withMessagingEndpoints(messagingEndpointsMap)
.withEnableFileUploadNotifications(false)
.withCloudToDevice(new CloudToDeviceProperties()
.withMaxDeliveryCount(10)
.withDefaultTtlAsIso8601(Duration.ofHours(1L))
.withFeedback(new FeedbackProperties()
.withLockDurationAsIso8601(Duration.ofMinutes(1L))
.withTtlAsIso8601(Duration.ofHours(1L))
.withMaxDeliveryCount(10)))
.withFeatures(Capabilities.NONE)
.withDisableLocalAuth(false)
.withEnableDataResidency(false)
)
.create();
// embedmeEnd
v-hongli1 marked this conversation as resolved.
Show resolved Hide resolved
iotHubDescription.refresh();

Assertions.assertEquals(iotHubDescription.name(), descriptionName);
Assertions.assertEquals(iotHubDescription.name(), iotHubManager.iotHubResources().getById(iotHubDescription.id()).name());
Assertions.assertTrue(iotHubManager.iotHubResources().list().stream().count() > 0);
} finally {
if (iotHubDescription != null) {
iotHubManager.iotHubResources().deleteById(iotHubDescription.id());
}
}
}

private static String randomPadding() {
return String.format("%05d", Math.abs(RANDOM.nextInt() % 100000));
}
}
27 changes: 27 additions & 0 deletions sdk/iothub/test-resources.bicep
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
@description('The tenant id to which the application and resources belong.')
param tenantId string = '72f988bf-86f1-41af-91ab-2d7cd011db47'

@description('The client id of the service principal used to run tests.')
param testApplicationId string

@description('This is the object id of the service principal used to run tests.')
param testApplicationOid string

@description('The application client secret used to run tests.')
param testApplicationSecret string

var contributorRoleId = '/subscriptions/${subscription().subscriptionId}/providers/Microsoft.Authorization/roleDefinitions/b24988ac-6180-42a0-ab88-20f7382dd24c'

resource contributorRoleId_name 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
name: guid('contributorRoleId${resourceGroup().name}')
properties: {
roleDefinitionId: contributorRoleId
principalId: testApplicationOid
}
}

output AZURE_TENANT_ID string = tenantId
output AZURE_CLIENT_ID string = testApplicationId
output AZURE_CLIENT_SECRET string = testApplicationSecret
output AZURE_SUBSCRIPTION_ID string = subscription().subscriptionId
output AZURE_RESOURCE_GROUP_NAME string = resourceGroup().name
16 changes: 16 additions & 0 deletions sdk/iothub/tests.mgmt.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
trigger: none

pr: none

stages:
- template: /eng/pipelines/templates/stages/archetype-sdk-tests.yml
parameters:
ServiceDirectory: iothub
Artifacts:
- name: azure-resourcemanager-iothub
groupId: com.azure.resourcemanager
safeName: azureresourcemanageriothub
Clouds: 'Public'
# Only run tests on Windows to save cost.
MatrixFilters:
- pool=.*(win).*