Created
February 1, 2020 09:34
-
-
Save mikhailshilkov/e7f44bf13c8a217df23e0642078ee837 to your computer and use it in GitHub Desktop.
Pulumi TS tests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import * as assert from "assert"; | |
| import "mocha"; | |
| import * as pulumi from "@pulumi/pulumi"; | |
| pulumi.runtime.setMocks({ | |
| newResource: function(type: string, name: string, inputs: any) : {id: string, state: any} { | |
| switch (type) { | |
| case "aws:ec2/securityGroup:SecurityGroup": | |
| return { | |
| id: "sg-12345678", | |
| state: { | |
| ...inputs, | |
| arn: "arn:aws:ec2:us-west-2:123456789012:security-group/sg-12345678", | |
| name: inputs.name || name + "-sg", | |
| }, | |
| }; | |
| case "aws:ec2/instance:Instance": | |
| return { | |
| id: "i-1234567890abcdef0", | |
| state: { | |
| ...inputs, | |
| arn: "arn:aws:ec2:us-west-2:123456789012:instance/i-1234567890abcdef0", | |
| instanceState: "running", | |
| primaryNetworkInterfaceId: "eni-12345678", | |
| privateDns: "ip-10-0-1-17.ec2.internal", | |
| publicDns: "ec2-203-0-113-12.compute-1.amazonaws.com", | |
| publicIp: "203.0.113.12", | |
| }, | |
| }; | |
| default: | |
| return { | |
| id: inputs.name + "_id", | |
| state: { | |
| ...inputs, | |
| }, | |
| } | |
| } | |
| }, | |
| call: function(token: string, args: any, provider?: string) { | |
| }, | |
| }); | |
| import * as infra from "./index"; | |
| describe("Infrastructure", function() { | |
| let server = infra.server; | |
| describe("#server", function() { | |
| // check 1: Instances have a Name tag. | |
| it("must have a name tag", function(done) { | |
| pulumi.all([server.urn, server.tags]).apply(([urn, tags]) => { | |
| if (!tags || !tags["Name"]) { | |
| done(new Error(`Missing a name tag on server ${urn}`)); | |
| } else { | |
| done(); | |
| } | |
| }); | |
| }); | |
| // check 2: Instances must not use an inline userData script. | |
| it("must not use userData (use an AMI instead)", function(done) { | |
| pulumi.all([server.urn, server.userData]).apply(([urn, userData]) => { | |
| if (userData) { | |
| done(new Error(`Illegal use of userData on server ${urn}`)); | |
| } else { | |
| done(); | |
| } | |
| }); | |
| }); | |
| // check 3: Instances must name at least one security group. | |
| it("must name a security group", function(done) { | |
| pulumi.all([server.urn, server.securityGroups]).apply(([urn, securityGroups]) => { | |
| if (!securityGroups || securityGroups.length === 0 || typeof securityGroups[0] !== "string") { | |
| done(new Error(`${securityGroups[0]}illegal security group spec on server ${urn}`)); | |
| } else { | |
| done(); | |
| } | |
| }); | |
| }); | |
| }); | |
| let group = infra.group; | |
| describe("#group", function() { | |
| // check 4: Instances must not have SSH open to the Internet. | |
| it("must not open port 22 (SSH) to the Internet", function(done) { | |
| pulumi.all([ group.urn, group.ingress ]).apply(([ urn, ingress ]) => { | |
| if (ingress.find(rule => | |
| rule.fromPort == 22 && (rule.cidrBlocks || []).find(block => block === "0.0.0.0/0"))) { | |
| done(new Error(`Illegal SSH port 22 open to the Internet (CIDR 0.0.0.0/0) on group ${urn}`)); | |
| } else { | |
| done(); | |
| } | |
| }); | |
| }); | |
| }); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import * as pulumi from "@pulumi/pulumi"; | |
| import * as aws from "@pulumi/aws"; | |
| import * as awsx from "@pulumi/awsx"; | |
| import { Provider } from "@pulumi/aws"; | |
| const secretParameter = new aws.ssm.Parameter("my-secret-value", { | |
| type: "SecureString", | |
| value: pulumi.secret("123") | |
| //value: "123" | |
| }); | |
| export const group = new aws.ec2.SecurityGroup("web-secgrp", { | |
| ingress: [ | |
| //{ protocol: "tcp", fromPort: 22, toPort: 22, cidrBlocks: ["0.0.0.0/0"] }, | |
| { protocol: "tcp", fromPort: 80, toPort: 80, cidrBlocks: ["0.0.0.0/0"] }, | |
| ], | |
| }); | |
| let userData = `#!/bin/bash echo "Hello, World!" > index.html nohup python -m SimpleHTTPServer 80 &`; | |
| export const server = new aws.ec2.Instance("web-server-www", { | |
| instanceType: "t2.micro", | |
| tags: { Name: "Bobby"}, | |
| securityGroups: [ group.name ], // reference the group object above | |
| ami: "ami-c55673a0", // AMI for us-east-2 (Ohio), | |
| //userData: userData, // start a simple web server | |
| }); | |
| export const publicIp = server.publicIp; | |
| export const publicHostName = server.publicDns; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment