Skip to content

Commit 54b9b4d

Browse files
authored
Add rds example test (#219)
Add an example test for the rds module re #211
1 parent ddcb2a6 commit 54b9b4d

File tree

6 files changed

+180
-0
lines changed

6 files changed

+180
-0
lines changed

.gitignore

+8
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,16 @@ bin
2828
.envrc
2929

3030
tests/testdata/**/package-lock.json
31+
tests/testdata/**/yarn.lock
3132
tests/testdata/**/build/lib
3233
tests/testdata/**/*.egg-info
34+
tests/testdata/programs/**/node_modules
35+
tests/testdata/programs/**/sdks
36+
tests/testdata/examples/**/sdks
37+
examples/**/node_modules
38+
examples/**/sdks
39+
examples/**/yarn.lock
40+
examples**/package-lock.json
3341

3442
# TODO[pulumi/pulumi-terraform-module-provider#79] unwanted file generated
3543
schema-terraform-module.json

examples/aws-rds-example/Pulumi.yaml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
name: rds-example
2+
runtime:
3+
name: nodejs
4+
options:
5+
packagemanager: npm

examples/aws-rds-example/README.md

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# AWS RDS Module Example
2+
3+
## Setup Steps
4+
5+
Install the Terraform modules using `pulumi package add`.
6+
7+
```console
8+
$ yarn install
9+
$ pulumi package add terraform-module terraform-aws-modules/vpc/aws 5.19.0 vpcmod
10+
$ pulumi package add terraform-module terraform-aws-modules/rds/aws 6.10.0 rdsmod
11+
```
12+
13+
## Deploy
14+
15+
To deploy the example run
16+
17+
```console
18+
$ pulumi up
19+
```

examples/aws-rds-example/index.ts

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import * as vpcmod from '@pulumi/vpcmod';
2+
import * as pulumi from '@pulumi/pulumi';
3+
import * as rdsmod from '@pulumi/rdsmod';
4+
import * as aws from '@pulumi/aws';
5+
import * as std from '@pulumi/std';
6+
7+
const azs = aws.getAvailabilityZonesOutput({
8+
filters: [{
9+
name: "opt-in-status",
10+
values: ["opt-in-not-required"],
11+
}]
12+
}).names.apply(names => names.slice(0, 3));
13+
14+
const cidr = "10.0.0.0/16";
15+
16+
const cfg = new pulumi.Config();
17+
const prefix = cfg.get("prefix") ?? pulumi.getStack();
18+
19+
const vpc = new vpcmod.Module("test-vpc", {
20+
azs: azs,
21+
name: `test-vpc-${prefix}`,
22+
cidr,
23+
public_subnets: azs.apply(azs => azs.map((_, i) => {
24+
return getCidrSubnet(cidr, i+1);
25+
})),
26+
private_subnets: azs.apply(azs => azs.map((_, i) => {
27+
return getCidrSubnet(cidr, i+1+4);
28+
})),
29+
database_subnets: azs.apply(azs => azs.map((_, i) => {
30+
return getCidrSubnet(cidr, i+1 + 8);
31+
})),
32+
33+
create_database_subnet_group: true,
34+
});
35+
36+
const rdsSecurityGroup = new aws.ec2.SecurityGroup('test-rds-sg', {
37+
vpcId: vpc.vpc_id.apply(id => id!),
38+
});
39+
40+
new aws.vpc.SecurityGroupIngressRule('test-rds-sg-ingress', {
41+
ipProtocol: 'tcp',
42+
securityGroupId: rdsSecurityGroup.id,
43+
cidrIpv4: vpc.vpc_cidr_block.apply(cidr => cidr!),
44+
fromPort: 3306,
45+
toPort: 3306,
46+
});
47+
48+
new rdsmod.Module("test-rds", {
49+
engine: "mysql",
50+
identifier: `test-rds-${prefix}`,
51+
manage_master_user_password: true,
52+
publicly_accessible: false,
53+
allocated_storage: 20,
54+
max_allocated_storage: 100,
55+
instance_class: "db.t4g.large",
56+
engine_version: "8.0",
57+
family: "mysql8.0",
58+
db_name: "completeMysql",
59+
username: "complete_mysql",
60+
port: '3306',
61+
multi_az: true,
62+
db_subnet_group_name: vpc.database_subnet_group_name.apply(name => name!),
63+
vpc_security_group_ids: [rdsSecurityGroup.id],
64+
skip_final_snapshot: true,
65+
deletion_protection: false,
66+
create_db_option_group: false,
67+
create_db_parameter_group: false,
68+
69+
})
70+
71+
function getCidrSubnet(cidr: string, netnum: number): pulumi.Output<string> {
72+
return std.cidrsubnetOutput({
73+
input: cidr,
74+
newbits: 8,
75+
netnum,
76+
}).result
77+
}

examples/aws-rds-example/package.json

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "rds-example",
3+
"main": "index.ts",
4+
"devDependencies": {
5+
"@types/node": "^18",
6+
"typescript": "^5.0.0"
7+
},
8+
"dependencies": {
9+
"@pulumi/aws": "^6.72.0",
10+
"@pulumi/pulumi": "^3.113.0",
11+
"@pulumi/std": "^2.2.0"
12+
}
13+
}

tests/examples_test.go

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package tests
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"os"
7+
"path/filepath"
8+
"testing"
9+
10+
"github.com/stretchr/testify/assert"
11+
"github.com/stretchr/testify/require"
12+
13+
"github.com/pulumi/providertest/pulumitest"
14+
"github.com/pulumi/providertest/pulumitest/opttest"
15+
"github.com/pulumi/pulumi/sdk/v3/go/auto/optdestroy"
16+
"github.com/pulumi/pulumi/sdk/v3/go/auto/optpreview"
17+
"github.com/pulumi/pulumi/sdk/v3/go/auto/optup"
18+
)
19+
20+
func Test_RdsExample(t *testing.T) {
21+
localProviderBinPath := ensureCompiledProvider(t)
22+
skipLocalRunsWithoutCreds(t)
23+
// Module written to support the test.
24+
testProgram, err := filepath.Abs(filepath.Join("../", "examples", "aws-rds-example"))
25+
require.NoError(t, err)
26+
localPath := opttest.LocalProviderPath("terraform-module", filepath.Dir(localProviderBinPath))
27+
integrationTest := pulumitest.NewPulumiTest(t, testProgram, localPath)
28+
29+
// Get a prefix for resource names
30+
prefix := generateTestResourcePrefix()
31+
32+
// Set prefix via config
33+
integrationTest.SetConfig(t, "prefix", prefix)
34+
35+
// Generate package
36+
pulumiPackageAdd(t, integrationTest, localProviderBinPath, "terraform-aws-modules/vpc/aws", "5.19.0", "vpcmod")
37+
pulumiPackageAdd(t, integrationTest, localProviderBinPath, "terraform-aws-modules/rds/aws", "6.10.0", "rdsmod")
38+
39+
integrationTest.Up(t, optup.Diff(),
40+
optup.ErrorProgressStreams(os.Stderr),
41+
optup.ProgressStreams(os.Stdout),
42+
)
43+
44+
stackName := integrationTest.CurrentStack().Name()
45+
projectSettings, err := integrationTest.CurrentStack().Workspace().ProjectSettings(context.Background())
46+
assert.NoError(t, err)
47+
rdsUrn := fmt.Sprintf("urn:pulumi:%s::%s::rdsmod:index:Module::test-rds", stackName, projectSettings.Name.String())
48+
49+
integrationTest.Preview(t, optpreview.Diff(), optpreview.ExpectNoChanges(),
50+
optpreview.ErrorProgressStreams(os.Stderr),
51+
optpreview.ProgressStreams(os.Stdout),
52+
)
53+
54+
// TODO [pulumi/pulumi-terraform-module#151] Property dependencies aren't flowing through
55+
integrationTest.Destroy(t, optdestroy.TargetDependents(), optdestroy.Target([]string{
56+
rdsUrn,
57+
}))
58+
}

0 commit comments

Comments
 (0)