|
17 | 17 | using ResourceNotFoundException = Amazon.CloudControlApi.Model.ResourceNotFoundException; |
18 | 18 | using Task = System.Threading.Tasks.Task; |
19 | 19 | using System.Collections.Generic; |
| 20 | +using Amazon.EC2.Model; |
20 | 21 |
|
21 | 22 | namespace AWS.Deploy.CLI.Common.UnitTests.Recipes.Validation |
22 | 23 | { |
@@ -292,6 +293,48 @@ public async Task DockerExecutionDirectory_AbsoluteDoesNotExist() |
292 | 293 | await Validate(optionSettingItem, Path.Join("C:", "other_project"), false); |
293 | 294 | } |
294 | 295 |
|
| 296 | + /// <summary> |
| 297 | + /// Tests the relationship between an explicit VPC ID, whether "Default VPC" is checked, |
| 298 | + /// and any security groups that are specified. |
| 299 | + /// </summary> |
| 300 | + /// <param name="vpcId">selected VPC Id</param> |
| 301 | + /// <param name="isDefaultVpcSelected">whether the "Default VPC" radio is selected</param> |
| 302 | + /// <param name="selectedSecurityGroups">selected security groups</param> |
| 303 | + /// <param name="isValid">Whether or not the test case is expected to be valid</param> |
| 304 | + [Theory] |
| 305 | + // The Console Service recipe uses a comma-seperated string of security groups |
| 306 | + [InlineData("vpc1", true, "", true)] // Valid because the security groups are optional |
| 307 | + [InlineData("vpc1", true, "sg-1a,sg-1b", true)] // Valid because the security group does belong to the default VPC |
| 308 | + [InlineData("vpc1", true, "sg-1a,sg-2a", false)] // Invalid because the security group does not belong to the default VPC |
| 309 | + [InlineData("vpc2", false, "sg-2a", true)] // Valid because the security group does belong to the non-default VPC |
| 310 | + [InlineData("vpc2", false, "sg-1a", false)] // Invalid because the security group does not belong to the non-default VPC |
| 311 | + [InlineData("vpc2", true, "sg-1a", true)] // Valid because "true" for IsDefaultVPC overrides the "vpc2", so the security group matches |
| 312 | + [InlineData("vpc2", true, "sg-2a", false)] // Invalid because "true" for IsDefaultVPC overrides the "vpc2", so the security group does not match |
| 313 | + // |
| 314 | + // The ASP.NET on Fargate recipe uses a JSON list of security groups (these are same cases from above) |
| 315 | + // |
| 316 | + [InlineData("vpc1", true, "[]", true)] |
| 317 | + [InlineData("vpc1", true, "[\"sg-1a\",\"sg-1b\"]", true)] |
| 318 | + [InlineData("vpc1", true, "[\"sg-1a\",\"sg-2a\"]", false)] |
| 319 | + [InlineData("vpc2", false, "[\"sg-2a\"]", true)] |
| 320 | + [InlineData("vpc2", false, "[\"sg-1a\"]", false)] |
| 321 | + [InlineData("vpc2", true, "[\"sg-1a\"]", true)] |
| 322 | + [InlineData("vpc2", true, "[\"sg-2a\"]", false)] |
| 323 | + |
| 324 | + public async Task VpcId_DefaultVpc_SecurityGroups_Relationship(string vpcId, bool isDefaultVpcSelected, object selectedSecurityGroups, bool isValid) |
| 325 | + { |
| 326 | + PrepareMockVPCsAndSecurityGroups(_awsResourceQueryer); |
| 327 | + |
| 328 | + var (vpcIdOption, vpcDefaultOption, securityGroupsOption) = PrepareECSVpcOptions(); |
| 329 | + |
| 330 | + securityGroupsOption.Validators.Add(GetSecurityGroupsInVpcValidatorConfig(_awsResourceQueryer, _optionSettingHandler)); |
| 331 | + |
| 332 | + await _optionSettingHandler.SetOptionSettingValue(_recommendation, vpcIdOption, vpcId); |
| 333 | + await _optionSettingHandler.SetOptionSettingValue(_recommendation, vpcDefaultOption, isDefaultVpcSelected); |
| 334 | + |
| 335 | + await Validate(securityGroupsOption, selectedSecurityGroups, isValid); |
| 336 | + } |
| 337 | + |
295 | 338 | private OptionSettingItemValidatorConfig GetRegexValidatorConfig(string regex) |
296 | 339 | { |
297 | 340 | var regexValidatorConfig = new OptionSettingItemValidatorConfig |
@@ -387,5 +430,69 @@ private async Task Validate<T>(OptionSettingItem optionSettingItem, T value, boo |
387 | 430 | else |
388 | 431 | exception.ShouldNotBeNull(); |
389 | 432 | } |
| 433 | + |
| 434 | + /// <summary> |
| 435 | + /// Prepares a <see cref="SecurityGroupsInVpcValidator"/> for testing |
| 436 | + /// </summary> |
| 437 | + private OptionSettingItemValidatorConfig GetSecurityGroupsInVpcValidatorConfig(Mock<IAWSResourceQueryer> awsResourceQueryer, IOptionSettingHandler optionSettingHandler) |
| 438 | + { |
| 439 | + var validator = new SecurityGroupsInVpcValidator(awsResourceQueryer.Object, optionSettingHandler); |
| 440 | + validator.VpcId = "Vpc.VpcId"; |
| 441 | + validator.IsDefaultVpcOptionSettingId = "Vpc.IsDefault"; |
| 442 | + |
| 443 | + return new OptionSettingItemValidatorConfig |
| 444 | + { |
| 445 | + ValidatorType = OptionSettingItemValidatorList.SecurityGroupsInVpc, |
| 446 | + Configuration = validator |
| 447 | + }; |
| 448 | + } |
| 449 | + |
| 450 | + /// <summary> |
| 451 | + /// Mocks the provided <see cref="IAWSResourceQueryer"> to return the following |
| 452 | + /// 1. Default vpc1 with security groups sg-1a and sg-1b |
| 453 | + /// 2. Non-default vpc2 with security groups sg-2a and sg-2b |
| 454 | + /// </summary> |
| 455 | + /// <param name="awsResourceQueryer">Mocked AWS Resource Queryer</param> |
| 456 | + private void PrepareMockVPCsAndSecurityGroups(Mock<IAWSResourceQueryer> awsResourceQueryer) |
| 457 | + { |
| 458 | + awsResourceQueryer.Setup(x => x.GetListOfVpcs()).ReturnsAsync( |
| 459 | + new List<Vpc> { |
| 460 | + new Vpc { VpcId = "vpc1", IsDefault = true }, |
| 461 | + new Vpc { VpcId = "vpc2"} |
| 462 | + }); |
| 463 | + |
| 464 | + awsResourceQueryer.Setup(x => x.DescribeSecurityGroups("vpc1")).ReturnsAsync( |
| 465 | + new List<SecurityGroup> { |
| 466 | + new SecurityGroup { GroupId = "sg-1a", VpcId = "vpc1" }, |
| 467 | + new SecurityGroup { GroupId = "sg-1b", VpcId = "vpc1" } |
| 468 | + }); |
| 469 | + |
| 470 | + awsResourceQueryer.Setup(x => x.DescribeSecurityGroups("vpc2")).ReturnsAsync( |
| 471 | + new List<SecurityGroup> { |
| 472 | + new SecurityGroup { GroupId = "sg-2a", VpcId = "vpc2" }, |
| 473 | + new SecurityGroup { GroupId = "sg-2a", VpcId = "vpc2" } |
| 474 | + }); |
| 475 | + |
| 476 | + awsResourceQueryer.Setup(x => x.GetDefaultVpc()).ReturnsAsync(new Vpc { VpcId = "vpc1", IsDefault = true }); |
| 477 | + } |
| 478 | + |
| 479 | + /// <summary> |
| 480 | + /// Prepares VPC-related options that match the ECS Fargate recipes for testing |
| 481 | + /// </summary> |
| 482 | + /// <returns>The "Vpc.VpcId" option, the "Vpc.IsDefault" option, and the "ECSServiceSecurityGroups" option</returns> |
| 483 | + private (OptionSettingItem, OptionSettingItem, OptionSettingItem) PrepareECSVpcOptions() |
| 484 | + { |
| 485 | + var vpcIdOption = new OptionSettingItem("VpcId", "Vpc.VpcId", "name", "description"); |
| 486 | + var vpcDefaultOption = new OptionSettingItem("IsDefault", "Vpc.IsDefault", "name", "description"); |
| 487 | + var ecsServiceSecurityGroupsOption = new OptionSettingItem("ECSServiceSecurityGroups", "ECSServiceSecurityGroups", "name", ""); |
| 488 | + |
| 489 | + var vpc = new OptionSettingItem("Vpc", "Vpc", "", ""); |
| 490 | + vpc.ChildOptionSettings.Add(vpcIdOption); |
| 491 | + vpc.ChildOptionSettings.Add(vpcDefaultOption); |
| 492 | + |
| 493 | + _recipe.OptionSettings.Add(vpc); |
| 494 | + |
| 495 | + return (vpcIdOption, vpcDefaultOption, ecsServiceSecurityGroupsOption); |
| 496 | + } |
390 | 497 | } |
391 | 498 | } |
0 commit comments