forked from pulumi/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebserverStackTests.cs
57 lines (49 loc) · 1.52 KB
/
WebserverStackTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using System.Linq;
using System.Threading.Tasks;
using FluentAssertions;
using NUnit.Framework;
using Pulumi.Aws.Ec2;
namespace UnitTesting
{
/// <summary>
/// Unit testing examples.
/// </summary>
[TestFixture]
public class WebserverStackTests
{
[Test]
public async Task InstanceHasNameTag()
{
var resources = await Testing.RunAsync<WebserverStack>();
var instance = resources.OfType<Instance>().FirstOrDefault();
instance.Should().NotBeNull("EC2 Instance not found");
var tags = await instance.Tags.GetValueAsync();
tags.Should().NotBeNull("Tags are not defined");
tags.Should().ContainKey("Name");
}
[Test]
public async Task InstanceMustNotUseInlineUserData()
{
var resources = await Testing.RunAsync<WebserverStack>();
var instance = resources.OfType<Instance>().FirstOrDefault();
instance.Should().NotBeNull("EC2 Instance not found");
var tags = await instance.UserData.GetValueAsync();
tags.Should().BeNull();
}
[Test]
public async Task SecurityGroupMustNotHaveSshPortsOpenToInternet()
{
var resources = await Testing.RunAsync<WebserverStack>();
foreach (var securityGroup in resources.OfType<SecurityGroup>())
{
var urn = await securityGroup.Urn.GetValueAsync();
var ingress = await securityGroup.Ingress.GetValueAsync();
foreach (var rule in ingress)
{
(rule.FromPort == 22 && rule.CidrBlocks.Any(b => b == "0.0.0.0/0"))
.Should().BeFalse($"Illegal SSH port 22 open to the Internet (CIDR 0.0.0.0/0) on group {urn}");
}
}
}
}
}