Example #1
0
    public async Task AddTest(bool resourceGroupExists)
    {
        using (var scope = Fixture.Container.BeginLifetimeScope())
        {
            var cl    = scope.Resolve <HttpClient>();
            var azure = scope.ResolveKeyed <IAzure>(EswDevOpsSdk.GetEnvironment());
            await PrepareResourceGroup(resourceGroupExists, azure);

            try
            {
                var rg            = TestResourceGroupRequest();
                var actorResponse = await cl.PostJsonToActor(Fixture.TestMiddlewareUri, "ResourceGroup", "Add", rg);

                actorResponse.Should().NotBeNull();
                actorResponse.ResourceId.Should().NotBeNullOrEmpty();
                actorResponse.State.Should().Be(EntityStateEnum.Created);

                await Task.Delay(TimeSpan.FromSeconds(5));

                var isResourceGroupCreated = await azure.ResourceGroups.ContainAsync(TestResourceGroupName);

                isResourceGroupCreated.Should()
                .BeTrue($"the resource {TestResourceGroupName} group should be created in the {azure.SubscriptionId} subscription, but the actor reported that it created {actorResponse.ResourceId}.");
                var createdResourceGroup = await azure.ResourceGroups.GetByNameAsync(TestResourceGroupName);

                createdResourceGroup.Should().NotBeNull();
                createdResourceGroup.Region.Should().Be(Fixture.TestRegion);
                actorResponse.ResourceId.Should().Be(createdResourceGroup.Id);
            }
            finally
            {
                await DeleteResourceGroup(azure, TestResourceGroupName);
            }
        }
    }
Example #2
0
    public async Task RemoveTests(OperationPhase operationPhase)
    {
        using (var scope = Fixture.Container.BeginLifetimeScope())
        {
            var cl            = scope.Resolve <HttpClient>();
            var azure         = scope.ResolveKeyed <IAzure>(EswDevOpsSdk.GetEnvironment());
            var resourceGroup = await EnsureResourceGroupExists(azure, TestResourceGroupName, Region.EuropeNorth);

            var scaleSet = await GetScaleSet(azure);
            await Prepare(azure, operationPhase, resourceGroup, scaleSet, IdentityName);

            var identity = await FindIdentity(azure, resourceGroup, IdentityName);

            var msi = CreateMangedIdentityAssignment();
            await cl.PostJsonToActor(Fixture.TestMiddlewareUri, "ManagedIdentity", "Remove", msi);

            var deletedIdentity = await FindIdentity(azure, resourceGroup, IdentityName);

            deletedIdentity.Should().BeNull($"identity {IdentityName} should be deleted.");
            if (identity != null)
            {
                var updatedScaleSet = await GetScaleSet(azure);

                if (updatedScaleSet.ManagedServiceIdentityType == ResourceIdentityType.SystemAssignedUserAssigned ||
                    updatedScaleSet.ManagedServiceIdentityType == ResourceIdentityType.UserAssigned)
                {
                    updatedScaleSet.UserAssignedManagedServiceIdentityIds.Should().NotBeNull();
                    updatedScaleSet.UserAssignedManagedServiceIdentityIds.Should().NotContain(identity.Id);
                }

                // TODO: if possible check whether the managed identity had been unassigned from the scale set before it has been deleted.
            }
        }
    }
Example #3
0
    public void GeEnvironmentTest(string envValue, DeploymentEnvironment env)
    {
        var prevEnv = Environment.GetEnvironmentVariable(EswDevOpsSdk.EnvironmentEnvVariable);

        Environment.SetEnvironmentVariable(EswDevOpsSdk.EnvironmentEnvVariable, envValue, EnvironmentVariableTarget.Process);
        try
        {
            var currentEnvironment = EswDevOpsSdk.GetEnvironment();
            currentEnvironment.Should().Be(env);
        }
        finally
        {
            Environment.SetEnvironmentVariable(EswDevOpsSdk.EnvironmentEnvVariable, prevEnv, EnvironmentVariableTarget.Process);
        }
    }
Example #4
0
    public async Task AddTest(OperationPhase operationPhase)
    {
        using (var scope = Fixture.Container.BeginLifetimeScope())
        {
            var cl            = scope.Resolve <HttpClient>();
            var azure         = scope.ResolveKeyed <IAzure>(EswDevOpsSdk.GetEnvironment());
            var resourceGroup = await EnsureResourceGroupExists(azure, TestResourceGroupName, Region.EuropeNorth);

            var scaleSet = await GetScaleSet(azure);
            await Prepare(azure, operationPhase, resourceGroup, scaleSet, IdentityName);

            var msi           = CreateMangedIdentityAssignment();
            var actorResponse = await cl.PostJsonToActor(Fixture.TestMiddlewareUri, "ManagedIdentity", "Add", msi);

            actorResponse.Should().NotBeNull();
            actorResponse.IdentityId.Should().NotBeNullOrEmpty();
            actorResponse.State.Should().Be(EntityStateEnum.Created);

            var policy = Policy
                         .Handle <CloudException>()
                         .OrResult <IIdentity>(x => x == null)
                         .WaitAndRetryAsync(3, retryAttempt =>
                                            TimeSpan.FromSeconds(Math.Pow(2, retryAttempt))
                                            );

            var capturedIdentity =
                await policy.ExecuteAndCaptureAsync(() => azure.Identities.GetByIdAsync(actorResponse.IdentityId));

            capturedIdentity.Result.Should().NotBeNull(
                $"the identity {actorResponse.IdentityId} should be created in {azure.SubscriptionId} subscription");
            capturedIdentity.Result.Name.Should().Be(msi.IdentityName);
            capturedIdentity.Result.ResourceGroupName.Should().Be(msi.ResourceGroupName);

            var modifiedScaleSet = await GetScaleSet(azure);

            var isAssigned = IsIdentityAssigned(modifiedScaleSet, capturedIdentity.Result);
            isAssigned.Should()
            .BeTrue($"the identity {capturedIdentity.Result.Id} should be assigned to the scale set {modifiedScaleSet.Id}");
        }
    }
Example #5
0
    public async Task RemoveTests(bool resourceGroupExists)
    {
        using (var scope = Fixture.Container.BeginLifetimeScope())
        {
            var cl    = scope.Resolve <HttpClient>();
            var azure = scope.ResolveKeyed <IAzure>(EswDevOpsSdk.GetEnvironment());
            await PrepareResourceGroup(resourceGroupExists, azure);

            try
            {
                await cl.PostJsonToActor(Fixture.TestMiddlewareUri, "ResourceGroup", "Remove", TestResourceGroupRequest());

                await Task.Delay(TimeSpan.FromSeconds(5));

                var exists = await azure.ResourceGroups.ContainAsync(TestResourceGroupName);

                exists.Should().BeFalse();
            }
            finally
            {
                await DeleteResourceGroup(azure, TestResourceGroupName);
            }
        }
    }