12
12
// limitations under the License.
13
13
// ----------------------------------------------------------------------------------
14
14
15
+ using System ;
16
+ using System . Collections ;
17
+ using System . Collections . Concurrent ;
18
+ using System . Collections . Generic ;
19
+ using System . IO ;
20
+ using System . Linq ;
21
+ using System . Net ;
22
+ using System . Runtime . Serialization . Formatters ;
23
+ using System . Text . RegularExpressions ;
24
+ using System . Threading ;
25
+ using System . Threading . Tasks ;
15
26
using Microsoft . Azure . Commands . ResourceManager . Cmdlets . SdkClient ;
16
27
using Microsoft . Azure . Commands . ResourceManager . Cmdlets . SdkModels ;
17
28
using Microsoft . Azure . Commands . Resources . Models ;
23
34
using Microsoft . WindowsAzure . Commands . Test . Utilities . Common ;
24
35
using Moq ;
25
36
using Newtonsoft . Json ;
26
- using System ;
27
- using System . Collections ;
28
- using System . Collections . Generic ;
29
- using System . IO ;
30
- using System . Linq ;
31
- using System . Net ;
32
- using System . Runtime . Serialization . Formatters ;
33
- using System . Text . RegularExpressions ;
34
- using System . Threading ;
35
- using System . Threading . Tasks ;
36
37
using Xunit ;
37
38
38
39
namespace Microsoft . Azure . Commands . Resources . Test . Models
@@ -392,7 +393,7 @@ public void NewResourceGroupUsesDeploymentNameForDeploymentName()
392
393
} ) )
393
394
. Callback ( ( string rg , string dn , Deployment d , Dictionary < string , List < string > > customHeaders , CancellationToken c ) => { deploymentFromValidate = d ; } ) ;
394
395
395
- deploymentsMock . Setup ( f => f . CreateOrUpdateWithHttpMessagesAsync (
396
+ deploymentsMock . Setup ( f => f . BeginCreateOrUpdateWithHttpMessagesAsync (
396
397
It . IsAny < string > ( ) ,
397
398
It . IsAny < string > ( ) ,
398
399
It . IsAny < Deployment > ( ) ,
@@ -408,6 +409,16 @@ public void NewResourceGroupUsesDeploymentNameForDeploymentName()
408
409
. Callback ( ( string name , string dName , Deployment bDeploy , Dictionary < string , List < string > > customHeaders , CancellationToken token ) =>
409
410
{ deploymentFromGet = bDeploy ; deploymentName = dName ; } ) ;
410
411
412
+ deploymentsMock . Setup ( f => f . CheckExistenceWithHttpMessagesAsync (
413
+ It . IsAny < string > ( ) ,
414
+ It . IsAny < string > ( ) ,
415
+ null ,
416
+ new CancellationToken ( ) ) )
417
+ . Returns ( Task . Factory . StartNew ( ( ) => new AzureOperationResponse < bool ? > ( )
418
+ {
419
+ Body = true
420
+ } ) ) ;
421
+
411
422
SetupListForResourceGroupAsync ( parameters . ResourceGroupName , new List < GenericResource >
412
423
{
413
424
CreateGenericResource ( null , null , "website" )
@@ -537,6 +548,176 @@ public void NewResourceGroupUsesDeploymentNameForDeploymentName()
537
548
Times . Once ( ) ) ;
538
549
}
539
550
551
+ [ Fact ]
552
+ public void NewResourceGroupDeploymentWithDelay ( )
553
+ {
554
+ string deploymentName = "abc123" ;
555
+ ConcurrentBag < string > deploymentNames = new ConcurrentBag < string > ( ) ;
556
+
557
+ PSCreateResourceGroupParameters parameters = new PSCreateResourceGroupParameters ( )
558
+ {
559
+ ResourceGroupName = resourceGroupName ,
560
+ Location = resourceGroupLocation ,
561
+ DeploymentName = deploymentName ,
562
+ ConfirmAction = ConfirmAction ,
563
+ TemplateFile = "http://path/file.html"
564
+ } ;
565
+
566
+ deploymentsMock . Setup ( f => f . ValidateWithHttpMessagesAsync (
567
+ parameters . ResourceGroupName ,
568
+ parameters . DeploymentName ,
569
+ It . IsAny < Deployment > ( ) ,
570
+ null ,
571
+ new CancellationToken ( ) ) )
572
+ . Returns ( Task . Factory . StartNew ( ( ) =>
573
+ new AzureOperationResponse < DeploymentValidateResult > ( )
574
+ {
575
+ Body = new DeploymentValidateResult
576
+ {
577
+ }
578
+ } ) ) ;
579
+
580
+ deploymentsMock . Setup ( f => f . GetWithHttpMessagesAsync (
581
+ parameters . ResourceGroupName ,
582
+ parameters . DeploymentName ,
583
+ null ,
584
+ new CancellationToken ( ) ) )
585
+ . Returns < string , string , Dictionary < string , List < string > > , CancellationToken > (
586
+ async ( getResourceGroupName , getDeploymentName , customHeaders , cancellationToken ) =>
587
+ {
588
+ await Task . Delay ( 100 , cancellationToken ) ;
589
+
590
+ if ( deploymentNames . Contains ( getDeploymentName ) )
591
+ {
592
+ return new AzureOperationResponse < DeploymentExtended > ( )
593
+ {
594
+ Body = new DeploymentExtended ( )
595
+ {
596
+ Name = getDeploymentName ,
597
+ Id = requestId ,
598
+ Properties = new DeploymentPropertiesExtended ( )
599
+ {
600
+ Mode = DeploymentMode . Incremental ,
601
+ CorrelationId = "123" ,
602
+ ProvisioningState = "Succeeded"
603
+ } ,
604
+ }
605
+ } ;
606
+ }
607
+
608
+ throw new CloudException ( String . Format ( "Deployment '{0}' could not be found." , getDeploymentName ) ) ;
609
+ } ) ;
610
+
611
+ deploymentsMock . Setup ( f => f . BeginCreateOrUpdateWithHttpMessagesAsync (
612
+ parameters . ResourceGroupName ,
613
+ parameters . DeploymentName ,
614
+ It . IsAny < Deployment > ( ) ,
615
+ null ,
616
+ new CancellationToken ( ) ) )
617
+ . Returns < string , string , Deployment , Dictionary < string , List < string > > , CancellationToken > (
618
+ async ( craeteResourceGroupName , createDeploymentName , createDeployment , customHeaders , cancellationToken ) =>
619
+ {
620
+ await Task . Delay ( 500 , cancellationToken ) ;
621
+
622
+ deploymentNames . Add ( createDeploymentName ) ;
623
+
624
+ return new AzureOperationResponse < DeploymentExtended > ( )
625
+ {
626
+ Body = new DeploymentExtended
627
+ {
628
+ Id = requestId
629
+ }
630
+ } ;
631
+ } ) ;
632
+
633
+ deploymentsMock . Setup ( f => f . CreateOrUpdateWithHttpMessagesAsync (
634
+ parameters . ResourceGroupName ,
635
+ parameters . DeploymentName ,
636
+ It . IsAny < Deployment > ( ) ,
637
+ null ,
638
+ new CancellationToken ( ) ) )
639
+ . Returns < string , string , Deployment , Dictionary < string , List < string > > , CancellationToken > (
640
+ async ( craeteResourceGroupName , createDeploymentName , createDeployment , customHeaders , cancellationToken ) =>
641
+ {
642
+ await Task . Delay ( 10000 , cancellationToken ) ;
643
+
644
+ deploymentNames . Add ( createDeploymentName ) ;
645
+
646
+ return new AzureOperationResponse < DeploymentExtended > ( )
647
+ {
648
+ Body = new DeploymentExtended
649
+ {
650
+ Id = requestId
651
+ }
652
+ } ;
653
+ } ) ;
654
+
655
+ deploymentsMock . Setup ( f => f . CheckExistenceWithHttpMessagesAsync (
656
+ parameters . ResourceGroupName ,
657
+ parameters . DeploymentName ,
658
+ null ,
659
+ new CancellationToken ( ) ) )
660
+ . Returns ( Task . Factory . StartNew ( ( ) => new AzureOperationResponse < bool ? > ( )
661
+ {
662
+ Body = true
663
+ } ) ) ;
664
+
665
+ SetupListForResourceGroupAsync ( parameters . ResourceGroupName , new List < GenericResource >
666
+ {
667
+ CreateGenericResource ( null , null , "website" )
668
+ } ) ;
669
+
670
+ var operationId = Guid . NewGuid ( ) . ToString ( ) ;
671
+ var operationQueue = new Queue < DeploymentOperation > ( ) ;
672
+ operationQueue . Enqueue (
673
+ new DeploymentOperation ( )
674
+ {
675
+ OperationId = operationId ,
676
+ Properties = new DeploymentOperationProperties ( )
677
+ {
678
+ ProvisioningState = "Succeeded" ,
679
+ TargetResource = new TargetResource ( )
680
+ {
681
+ ResourceType = "Microsoft.Website" ,
682
+ ResourceName = resourceName
683
+ }
684
+ }
685
+ }
686
+ ) ;
687
+ deploymentOperationsMock . Setup ( f => f . ListWithHttpMessagesAsync (
688
+ parameters . ResourceGroupName ,
689
+ parameters . DeploymentName ,
690
+ null ,
691
+ null ,
692
+ new CancellationToken ( ) ) )
693
+ . Returns < string , string , int ? , Dictionary < string , List < string > > , CancellationToken > (
694
+ async ( getResourceGroupName , getDeploymentName , top , customHeaders , cancellationToken ) =>
695
+ {
696
+ await Task . Delay ( 100 , cancellationToken ) ;
697
+
698
+ if ( deploymentNames . Contains ( getDeploymentName ) )
699
+ {
700
+ return new AzureOperationResponse < IPage < DeploymentOperation > > ( )
701
+ {
702
+ Body = GetPagableType (
703
+ new List < DeploymentOperation > ( )
704
+ {
705
+ operationQueue . Dequeue ( )
706
+ } )
707
+ } ;
708
+ }
709
+
710
+ throw new CloudException ( String . Format ( "Deployment '{0}' could not be found." , getDeploymentName ) ) ;
711
+ } ) ;
712
+
713
+ Microsoft . Azure . Commands . ResourceManager . Cmdlets . SdkModels . PSResourceGroupDeployment result = resourcesClient . ExecuteDeployment ( parameters ) ;
714
+ Assert . Equal ( deploymentName , result . DeploymentName ) ;
715
+ Assert . Equal ( "Succeeded" , result . ProvisioningState ) ;
716
+ progressLoggerMock . Verify (
717
+ f => f ( string . Format ( "Resource {0} '{1}' provisioning status is {2}" , "Microsoft.Website" , resourceName , "Succeeded" . ToLower ( ) ) ) ,
718
+ Times . Once ( ) ) ;
719
+ }
720
+
540
721
[ Fact ]
541
722
[ Trait ( Category . AcceptanceType , Category . CheckIn ) ]
542
723
public void NewResourceGroupWithDeploymentSucceeds ( )
@@ -573,7 +754,7 @@ public void NewResourceGroupWithDeploymentSucceeds()
573
754
Body = new ResourceGroup ( ) { Location = resourceGroupLocation }
574
755
} ) ) ;
575
756
576
- deploymentsMock . Setup ( f => f . CreateOrUpdateWithHttpMessagesAsync ( resourceGroupName , deploymentName , It . IsAny < Deployment > ( ) , null , new CancellationToken ( ) ) )
757
+ deploymentsMock . Setup ( f => f . BeginCreateOrUpdateWithHttpMessagesAsync ( resourceGroupName , deploymentName , It . IsAny < Deployment > ( ) , null , new CancellationToken ( ) ) )
577
758
. Returns ( Task . Factory . StartNew ( ( ) => new AzureOperationResponse < DeploymentExtended > ( )
578
759
{
579
760
Body = new DeploymentExtended
@@ -604,6 +785,15 @@ public void NewResourceGroupWithDeploymentSucceeds()
604
785
{
605
786
} ) ) )
606
787
. Callback ( ( string rg , string dn , Deployment d , Dictionary < string , List < string > > customHeaders , CancellationToken c ) => { deploymentFromValidate = d ; } ) ;
788
+ deploymentsMock . Setup ( f => f . CheckExistenceWithHttpMessagesAsync (
789
+ It . IsAny < string > ( ) ,
790
+ It . IsAny < string > ( ) ,
791
+ null ,
792
+ new CancellationToken ( ) ) )
793
+ . Returns ( Task . Factory . StartNew ( ( ) => new AzureOperationResponse < bool ? > ( )
794
+ {
795
+ Body = true
796
+ } ) ) ;
607
797
608
798
SetupListForResourceGroupAsync ( parameters . ResourceGroupName , new List < GenericResource > ( ) { CreateGenericResource ( null , null , "website" ) } ) ;
609
799
deploymentOperationsMock . Setup ( f => f . ListWithHttpMessagesAsync ( resourceGroupName , deploymentName , null , null , new CancellationToken ( ) ) )
@@ -626,7 +816,7 @@ public void NewResourceGroupWithDeploymentSucceeds()
626
816
627
817
PSResourceGroup result = resourcesClient . CreatePSResourceGroup ( parameters ) ;
628
818
Microsoft . Azure . Commands . ResourceManager . Cmdlets . SdkModels . PSResourceGroupDeployment deploymentResult = resourcesClient . ExecuteDeployment ( parameters ) ;
629
- deploymentsMock . Verify ( ( f => f . CreateOrUpdateWithHttpMessagesAsync ( resourceGroupName , deploymentName , deploymentFromGet , null , new CancellationToken ( ) ) ) , Times . Once ( ) ) ;
819
+ deploymentsMock . Verify ( ( f => f . BeginCreateOrUpdateWithHttpMessagesAsync ( resourceGroupName , deploymentName , deploymentFromGet , null , new CancellationToken ( ) ) ) , Times . Once ( ) ) ;
630
820
Assert . Equal ( parameters . ResourceGroupName , deploymentResult . ResourceGroupName ) ;
631
821
632
822
Assert . Equal ( DeploymentMode . Incremental , deploymentFromGet . Properties . Mode ) ;
0 commit comments