Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rds example test #219

Merged
merged 10 commits into from
Mar 19, 2025
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,16 @@ bin
.envrc

tests/testdata/**/package-lock.json
tests/testdata/**/yarn.lock
tests/testdata/**/build/lib
tests/testdata/**/*.egg-info
tests/testdata/programs/**/node_modules
tests/testdata/programs/**/sdks
tests/testdata/examples/**/sdks
examples/**/node_modules
examples/**/sdks
examples/**/yarn.lock
examples**/package-lock.json

# TODO[pulumi/pulumi-terraform-module-provider#79] unwanted file generated
schema-terraform-module.json
Expand Down
5 changes: 5 additions & 0 deletions examples/aws-rds-example/Pulumi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name: rds-example
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding a quick README and a script how to initialize this (pulumi package add). Since parameterized providers didn't make it into Pulumi.yaml yet this won't work OOTB.

runtime:
name: nodejs
options:
packagemanager: npm
77 changes: 77 additions & 0 deletions examples/aws-rds-example/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import * as vpcmod from '@pulumi/vpcmod';
import * as pulumi from '@pulumi/pulumi';
import * as rdsmod from '@pulumi/rdsmod';
import * as aws from '@pulumi/aws';
import * as std from '@pulumi/std';

const azs = aws.getAvailabilityZonesOutput({
filters: [{
name: "opt-in-status",
values: ["opt-in-not-required"],
}]
}).names.apply(names => names.slice(0, 3));

const cidr = "10.0.0.0/16";

const cfg = new pulumi.Config();
const prefix = cfg.get("prefix") ?? pulumi.getStack();

const vpc = new vpcmod.Module("test-vpc", {
azs: azs,
name: `test-vpc-${prefix}`,
cidr,
public_subnets: azs.apply(azs => azs.map((_, i) => {
return getCidrSubnet(cidr, i+1);
})),
private_subnets: azs.apply(azs => azs.map((_, i) => {
return getCidrSubnet(cidr, i+1+4);
})),
database_subnets: azs.apply(azs => azs.map((_, i) => {
return getCidrSubnet(cidr, i+1 + 8);
})),

create_database_subnet_group: true,
});

const rdsSecurityGroup = new aws.ec2.SecurityGroup('test-rds-sg', {
vpcId: vpc.vpc_id.apply(id => id!),
});

new aws.vpc.SecurityGroupIngressRule('test-rds-sg-ingress', {
ipProtocol: 'tcp',
securityGroupId: rdsSecurityGroup.id,
cidrIpv4: vpc.vpc_cidr_block.apply(cidr => cidr!),
fromPort: 3306,
toPort: 3306,
});

new rdsmod.Module("test-rds", {
engine: "mysql",
identifier: `test-rds-${prefix}`,
manage_master_user_password: true,
publicly_accessible: false,
allocated_storage: 20,
max_allocated_storage: 100,
instance_class: "db.t4g.large",
engine_version: "8.0",
family: "mysql8.0",
db_name: "completeMysql",
username: "complete_mysql",
port: '3306',
multi_az: true,
db_subnet_group_name: vpc.database_subnet_group_name.apply(name => name!),
vpc_security_group_ids: [rdsSecurityGroup.id],
skip_final_snapshot: true,
deletion_protection: false,
create_db_option_group: false,
create_db_parameter_group: false,

})

function getCidrSubnet(cidr: string, netnum: number): pulumi.Output<string> {
return std.cidrsubnetOutput({
input: cidr,
newbits: 8,
netnum,
}).result
}
13 changes: 13 additions & 0 deletions examples/aws-rds-example/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "rds-example",
"main": "index.ts",
"devDependencies": {
"@types/node": "^18",
"typescript": "^5.0.0"
},
"dependencies": {
"@pulumi/aws": "^6.72.0",
"@pulumi/pulumi": "^3.113.0",
"@pulumi/std": "^2.2.0"
}
}
58 changes: 58 additions & 0 deletions tests/examples_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package tests

import (
"context"
"fmt"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/pulumi/providertest/pulumitest"
"github.com/pulumi/providertest/pulumitest/opttest"
"github.com/pulumi/pulumi/sdk/v3/go/auto/optdestroy"
"github.com/pulumi/pulumi/sdk/v3/go/auto/optpreview"
"github.com/pulumi/pulumi/sdk/v3/go/auto/optup"
)

func Test_RdsExample(t *testing.T) {
localProviderBinPath := ensureCompiledProvider(t)
skipLocalRunsWithoutCreds(t)
// Module written to support the test.
testProgram, err := filepath.Abs(filepath.Join("../", "examples", "aws-rds-example"))
require.NoError(t, err)
localPath := opttest.LocalProviderPath("terraform-module", filepath.Dir(localProviderBinPath))
integrationTest := pulumitest.NewPulumiTest(t, testProgram, localPath)

// Get a prefix for resource names
prefix := generateTestResourcePrefix()

// Set prefix via config
integrationTest.SetConfig(t, "prefix", prefix)

// Generate package
pulumiPackageAdd(t, integrationTest, localProviderBinPath, "terraform-aws-modules/vpc/aws", "5.19.0", "vpcmod")
pulumiPackageAdd(t, integrationTest, localProviderBinPath, "terraform-aws-modules/rds/aws", "6.10.0", "rdsmod")

integrationTest.Up(t, optup.Diff(),
optup.ErrorProgressStreams(os.Stderr),
optup.ProgressStreams(os.Stdout),
)

stackName := integrationTest.CurrentStack().Name()
projectSettings, err := integrationTest.CurrentStack().Workspace().ProjectSettings(context.Background())
assert.NoError(t, err)
rdsUrn := fmt.Sprintf("urn:pulumi:%s::%s::rdsmod:index:Module::test-rds", stackName, projectSettings.Name.String())

integrationTest.Preview(t, optpreview.Diff(), optpreview.ExpectNoChanges(),
optpreview.ErrorProgressStreams(os.Stderr),
optpreview.ProgressStreams(os.Stdout),
)

// TODO [pulumi/pulumi-terraform-module#151] Property dependencies aren't flowing through
integrationTest.Destroy(t, optdestroy.TargetDependents(), optdestroy.Target([]string{
rdsUrn,
}))
}
Loading