Skip to content

Commit

Permalink
Added some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bshien committed Apr 5, 2024
1 parent 5ced6c6 commit a64b4a4
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 8 deletions.
2 changes: 1 addition & 1 deletion infrastructure/lib/stacks/metricsWorkflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class OpenSearchMetricsWorkflowStack extends Stack {
targets: [new SfnStateMachine(opensearchMetricsWorkflow)],
});

// This rule is to ensure OpenSearch Dashboards does not show 0 values in visualizations when used now/d-now-1/d
// This rule is to ensure OpenSearch Dashboards does not show 0 values in visualizations when used now/d to now+1d/d
new Rule(this, 'MetricsWorkflow-12AM-PDT', {
schedule: Schedule.expression('cron(0 7 * * ? *)'),
targets: [new SfnStateMachine(opensearchMetricsWorkflow)],
Expand Down
2 changes: 1 addition & 1 deletion infrastructure/lib/stacks/opensearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as iam from "aws-cdk-lib/aws-iam";
import { ArnPrincipal, CompositePrincipal, Effect, IRole, ManagedPolicy, PolicyDocument, PolicyStatement, Role, ServicePrincipal } from "aws-cdk-lib/aws-iam";
import { VpcStack } from "./vpc";
import {OpenSearchMetricsCognito} from "../constructs/opensearchCognito";
import {OpenSearchMetricsNginxCognito} from "../constructs/opensearchNginxProxyCognito";;
import {OpenSearchMetricsNginxCognito} from "../constructs/opensearchNginxProxyCognito";


export interface OpenSearchStackProps {
Expand Down
15 changes: 15 additions & 0 deletions infrastructure/test/hostedzone-stack.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {App} from "aws-cdk-lib";
import {VpcStack} from "../lib/stacks/vpc";
import {Template} from "aws-cdk-lib/assertions";
import {OpenSearchHealthRoute53} from "../lib/stacks/route53";
import Project from "../lib/enums/project";

test('HostedZone Stack Test', () => {
const app = new App();
const hostedZoneStack = new OpenSearchHealthRoute53(app, 'Test-OpenSearchMetrics-HostedZone', {
hostedZone: Project.METRICS_HOSTED_ZONE,
appName: "OpenSearchMetrics"
});
const hostedZoneStackTemplate = Template.fromStack(hostedZoneStack);
hostedZoneStackTemplate.resourceCountIs('AWS::Route53::HostedZone', 1);
});
54 changes: 54 additions & 0 deletions infrastructure/test/nginx.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import {App} from "aws-cdk-lib";
import {VpcStack} from "../lib/stacks/vpc";
import {Template} from "aws-cdk-lib/assertions";
import {OpenSearchMetricsNginxReadonly} from "../lib/stacks/opensearchNginxProxyReadonly";
import Project from "../lib/enums/project";
import {OpenSearchDomainStack} from "../lib/stacks/opensearch";
import {ArnPrincipal} from "aws-cdk-lib/aws-iam";
import {OpenSearchHealthRoute53} from "../lib/stacks/route53";

test('Nginx Stack Test', () => {
const app = new App();
const vpcStack = new VpcStack(app, "OpenSearchHealth-VPC", {});
const openSearchDomainStack = new OpenSearchDomainStack(app, "OpenSearchHealth-OpenSearch", {
region: Project.REGION,
account: Project.AWS_ACCOUNT,
vpcStack: vpcStack,
enableNginxCognito: true,
jenkinsAccess: {
jenkinsAccountRoles: [
new ArnPrincipal(Project.JENKINS_MASTER_ROLE),
new ArnPrincipal(Project.JENKINS_AGENT_ROLE)
]
}
});
const metricsHostedZone = new OpenSearchHealthRoute53(app, "OpenSearchMetrics-HostedZone", {
hostedZone: Project.METRICS_HOSTED_ZONE,
appName: "OpenSearchMetrics"
});
const stack = new OpenSearchMetricsNginxReadonly(app, 'Test-OpenSearchMetricsNginxReadonly', {
region: Project.REGION,
account: Project.AWS_ACCOUNT,
vpc: vpcStack.vpc,
securityGroup: vpcStack.securityGroup,
opensearchDashboardUrlProps: {
opensearchDashboardVpcUrl: openSearchDomainStack.domain.domainEndpoint,
openSearchDomainName: openSearchDomainStack.domain.domainName
},
albProps: {
hostedZone: metricsHostedZone,
certificateArn: metricsHostedZone.certificateArn,
},
});
const template = Template.fromStack(stack);
template.resourceCountIs('AWS::EC2::SecurityGroup', 2);
template.hasResourceProperties('AWS::EC2::SecurityGroup', {
"SecurityGroupEgress": [
{
"CidrIp": "0.0.0.0/0",
"Description": "Allow all outbound traffic by default",
"IpProtocol": "-1"
}
]
});
});
9 changes: 3 additions & 6 deletions infrastructure/test/opensearch-stack.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
// import * as cdk from 'aws-cdk-lib';
// import { Template } from 'aws-cdk-lib/assertions';
// import * as Infrastructure from '../lib/infrastructure-stack';

// example test. To run these tests, uncomment this file along with the
// example resource in lib/infrastructure-stack.ts
import {App} from "aws-cdk-lib";
import {Template} from "aws-cdk-lib/assertions";
import {OpenSearchDomainStack} from "../lib/stacks/opensearch";
Expand All @@ -27,4 +21,7 @@ test('OpenSearchDomain Stack Test', () => {
});
const openSearchDomainStackTemplate = Template.fromStack(openSearchDomainStack);
openSearchDomainStackTemplate.resourceCountIs('AWS::IAM::Role', 8);
openSearchDomainStackTemplate.resourceCountIs('AWS::Cognito::UserPool', 1);
openSearchDomainStackTemplate.resourceCountIs('AWS::Cognito::UserPoolGroup', 1);
openSearchDomainStackTemplate.resourceCountIs('AWS::IAM::Policy', 4);
});
20 changes: 20 additions & 0 deletions infrastructure/test/vpc-stack.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,24 @@ test('VPC Stack Test', () => {
const vpcStackTemplate = Template.fromStack(vpcStack);
vpcStackTemplate.resourceCountIs('AWS::EC2::VPC', 1);
vpcStackTemplate.resourceCountIs('AWS::EC2::Subnet', 4);
vpcStackTemplate.hasResourceProperties('AWS::EC2::SecurityGroup', {
"SecurityGroupEgress": [
{
"CidrIp": "0.0.0.0/0",
"Description": "Allow all outbound traffic by default",
"IpProtocol": "-1"
}
]
});
vpcStackTemplate.hasResourceProperties('AWS::EC2::SecurityGroup', {
"SecurityGroupIngress": [
{
"Description": "Allow inbound HTTPS traffic",
"FromPort": 443,
"IpProtocol": "tcp",
"ToPort": 443
}
]
});

});
31 changes: 31 additions & 0 deletions infrastructure/test/workflow-stack.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import {App} from "aws-cdk-lib";
import {Template} from "aws-cdk-lib/assertions";
import {OpenSearchMetricsWorkflowStack} from "../lib/stacks/metricsWorkflow";
import Project from "../lib/enums/project";
import {OpenSearchDomainStack} from "../lib/stacks/opensearch";
import {VpcStack} from "../lib/stacks/vpc";
import {ArnPrincipal} from "aws-cdk-lib/aws-iam";

test('Workflow Stack Test', () => {
const app = new App();
const vpcStack = new VpcStack(app, 'Test-OpenSearchHealth-VPC', {})
const OpenSearchMetricsWorkflow = new OpenSearchMetricsWorkflowStack(app, 'Test-OpenSearchMetrics-Workflow', {
opensearchDomainStack: new OpenSearchDomainStack(app, 'Test-OpenSearchHealth-OpenSearch', {
region: "us-east-1",
account: "test-account",
vpcStack: vpcStack,
enableNginxCognito: true,
jenkinsAccess: {
jenkinsAccountRoles: [
new ArnPrincipal(Project.JENKINS_MASTER_ROLE),
new ArnPrincipal(Project.JENKINS_AGENT_ROLE)
]
}
}),
vpcStack: vpcStack,
lambdaPackage: Project.LAMBDA_PACKAGE
});
const template = Template.fromStack(OpenSearchMetricsWorkflow);
template.resourceCountIs('AWS::Lambda::Function', 1);
template.resourceCountIs('AWS::IAM::Role', 2);
});

0 comments on commit a64b4a4

Please sign in to comment.