Skip to content

Commit

Permalink
setup: test render terraform template
Browse files Browse the repository at this point in the history
  • Loading branch information
ianic committed Sep 27, 2021
1 parent 7897e43 commit a1f4f99
Show file tree
Hide file tree
Showing 3 changed files with 211 additions and 5 deletions.
12 changes: 7 additions & 5 deletions api/setup/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func New() *Setup {
}

func (s *Setup) Invoke(ctx context.Context, req *dto.SetupRequest) (*dto.SetupResponse, error) {
if err := s.init(req); err != nil {
if err := s.init(req, nil); err != nil {
return nil, err
}
defer s.cleanup()
Expand Down Expand Up @@ -61,14 +61,16 @@ func (s *Setup) create() error {
return nil
}

func (s *Setup) init(req *dto.SetupRequest) error {
func (s *Setup) init(req *dto.SetupRequest, awsClient *aws.AWS) error {
tf, err := terraform.New("mantil-setup")
if err != nil {
return err
}
awsClient, err := aws.New()
if err != nil {
return fmt.Errorf("error initializing AWS client - %w", err)
if awsClient == nil {
awsClient, err = aws.New()
if err != nil {
return fmt.Errorf("error initializing AWS client - %w", err)
}
}
bucketName, err := config.Bucket(awsClient)
if err != nil {
Expand Down
91 changes: 91 additions & 0 deletions api/setup/setup_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package setup

import (
"flag"
"io/ioutil"
"strings"
"testing"

"github.com/mantil-io/mantil/api/dto"
"github.com/mantil-io/mantil/aws"
"github.com/mantil-io/mantil/shell"
"github.com/mantil-io/mantil/terraform"
"github.com/stretchr/testify/require"
)

var update = flag.Bool("update", false, "update expected files")

func TestInit(t *testing.T) {
cli := aws.NewForTests(t)
if cli == nil {
t.Skip("skip: AWS client not initialized")
}
init := func() *Setup {
req := dto.SetupRequest{}
s := New()
err := s.init(&req, cli)
require.NoError(t, err)
return s
}

t.Run("init", func(t *testing.T) {
s := init()
require.NotNil(t, s.tf)
require.NotNil(t, s.awsClient)
id, err := s.awsClient.AccountID()
require.NoError(t, err)
require.True(t, strings.HasSuffix(s.bucketName, id))

t.Logf("accountID: %s\n", id)
t.Logf("bucket: %s\n", s.bucketName)
})

}

func TestTerraformRender(t *testing.T) {
tf, err := terraform.New("mantil-setup")
require.NoError(t, err)
data := terraform.SetupTemplateData{
Bucket: "bucket-name",
BucketPrefix: "bucket-prefix",
FunctionsBucket: "functions-bucket",
FunctionsPath: "functions-path",
Region: "aws-region",
PublicKey: "public-key",
}
err = tf.RenderSetupTemplate(data)
require.NoError(t, err)
requireEqual(t, "./testdata/mantil-setup-main.tf", "/tmp/mantil-setup/main.tf")
}

func requireEqual(t *testing.T, expected, actual string) {
actualContent, err := ioutil.ReadFile(actual)
if err != nil {
t.Fatalf("failed reading actual file: %s", err)
}

if *update {
t.Logf("update expected file %s", expected)
if err := ioutil.WriteFile(expected, actualContent, 0644); err != nil {
t.Fatalf("failed to update expectexd file: %s", err)
}
return
}

expectedContent, err := ioutil.ReadFile(expected)
if err != nil {
t.Fatalf("failed reading expected file: %s", err)
}

if string(actualContent) != string(expectedContent) {
args := []string{"diff", expected, actual}
out, err := shell.Output(args, "")
if err != nil {
t.Logf("diff of files")
t.Logf("expected %s, actual %s", expected, actual)
t.Logf("%s", out)
t.Fatalf("failed")
}

}
}
113 changes: 113 additions & 0 deletions api/setup/testdata/mantil-setup-main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
locals {
aws_region = "aws-region"
functions_bucket = "functions-bucket" # bucket with backend functions
project_bucket = "bucket-name" # bucket for backend configuration/state
functions = {
"init" = {
s3_key = "functions-path/init.zip"
memory_size = 128
timeout = 900
},
"deploy" = {
s3_key = "functions-path/deploy.zip"
memory_size = 512,
timeout = 900
layers = ["arn:aws:lambda:aws-region:553035198032:layer:git-lambda2:8", "arn:aws:lambda:aws-region:477361877445:layer:terraform-lambda:1"]
},
"data" = {
s3_key = "functions-path/data.zip"
memory_size = 128,
timeout = 900
},
"security" = {
s3_key = "functions-path/security.zip"
memory_size = 128,
timeout = 900,
},
"destroy" = {
s3_key = "functions-path/destroy.zip"
memory_size = 512,
timeout = 900
layers = ["arn:aws:lambda:aws-region:553035198032:layer:git-lambda2:8", "arn:aws:lambda:aws-region:477361877445:layer:terraform-lambda:1"]
}
}
ws_handler = {
name = "ws-handler"
s3_key = "functions-path/ws-handler.zip"
memory_size = 128
timeout = 900
}
ws_sqs_forwarder = {
name = "ws-sqs-forwarder"
s3_key = "functions-path/ws-sqs-forwarder.zip"
memory_size = 128
timeout = 900
}
}

terraform {
backend "s3" {
bucket = "bucket-name"
key = "bucket-prefixterraform/state.tfstate"
region = "aws-region"
}
}

provider "aws" {
region = local.aws_region

skip_get_ec2_platforms = true
}

module "funcs" {
source = "http://localhost:8080/terraform/modules/backend-funcs.zip"
functions = local.functions
s3_bucket = local.functions_bucket
}

module "iam" {
source = "http://localhost:8080/terraform/modules/backend-iam.zip"
backend_role_arn = module.funcs.role_arn
}

module "api" {
source = "http://localhost:8080/terraform/modules/api.zip"
name_prefix = "mantil"
functions_bucket = local.functions_bucket
integrations = [for f in module.funcs.functions :
{
type : "AWS_PROXY"
method : "POST"
route : "/${f.name}"
uri : f.invoke_arn,
lambda_name : f.arn,
enable_auth : false,
}
]
authorizer = {
authorization_header = "X-Mantil-Access-Token"
public_key = "public-key"
s3_key = "functions-path/authorizer.zip"
}
}

# expose aws region and profile for use in shell scripts
output "aws_region" {
value = local.aws_region
}

output "project_bucket" {
value = local.project_bucket
}

output "url" {
value = module.api.http_url
}

output "cli_role" {
value = module.iam.cli_role
}

output "ws_url" {
value = module.api.ws_url
}

0 comments on commit a1f4f99

Please sign in to comment.