-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
setup: test render terraform template
- Loading branch information
Showing
3 changed files
with
211 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
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") | ||
} | ||
|
||
} | ||
} |
This file contains 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
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 | ||
} |