1+ require 'awspec'
2+ require 'hcl/checker'
3+
4+ tf_state_file = 'terraform.tfstate'
5+ module_hcl_file = 'tests/fixtures/tf_module/main.tf'
6+
7+ tf_state = JSON . parse ( File . read ( tf_state_file ) )
8+
9+ ###############################################################
10+ # lambda resource
11+ ###############################################################
12+ # testing vars from module's HCL
13+ lambda_hcl = HCL ::Checker . parse ( File . read ( module_hcl_file ) )
14+ lambda_name = lambda_hcl [ 'module' ] [ 'module_test' ] [ 'lambda_name' ]
15+ lambda_runtime = lambda_hcl [ 'module' ] [ 'module_test' ] [ 'runtime' ]
16+ lambda_handler = lambda_hcl [ 'module' ] [ 'module_test' ] [ 'handler' ]
17+ lambda_timeout = lambda_hcl [ 'module' ] [ 'module_test' ] [ 'timeout' ]
18+
19+ # testing vars from .tfvars file
20+ lambda_arn = tf_state [ 'modules' ] [ 1 ] [ 'outputs' ] [ 'lambda_arn' ] [ 'value' ] [ 0 ]
21+ lambda_resource = tf_state [ 'modules' ] [ 1 ] [ 'resources' ] [ 'aws_lambda_function.self' ]
22+ lambda_description = lambda_resource [ 'primary' ] [ 'attributes' ] [ 'description' ]
23+
24+ # aws_iam_role
25+ iam_role_resource = tf_state [ 'modules' ] [ 1 ] [ 'resources' ] [ 'aws_iam_role.lambda_role' ]
26+ iam_role_id = iam_role_resource [ 'primary' ] [ 'attributes' ] [ 'name' ]
27+ iam_role_description = iam_role_resource [ 'primary' ] [ 'attributes' ] [ 'description' ]
28+
29+ # aws_iam_policy
30+ iam_policy_resource = tf_state [ 'modules' ] [ 1 ] [ 'resources' ] [ 'aws_iam_policy.lambda-policy' ]
31+ iam_policy_name = iam_policy_resource [ 'primary' ] [ 'attributes' ] [ 'name' ]
32+
33+ # "aws_cloudwatch_event_rule" "cron"
34+ cloudwatch_cron_resource = tf_state [ 'modules' ] [ 1 ] [ 'resources' ] [ 'aws_cloudwatch_event_rule.cron' ]
35+ cloudwatch_cron_name = cloudwatch_cron_resource [ 'primary' ] [ 'attributes' ] [ 'id' ]
36+ cloudwatch_cron_schedule = lambda_hcl [ 'module' ] [ 'module_test' ] [ 'schedule_expression' ]
37+
38+ # Testing lambda
39+ describe lambda ( lambda_name ) do
40+ it { should exist }
41+ its ( :function_name ) { should eq lambda_name }
42+ its ( :function_arn ) { should eq lambda_arn }
43+ its ( :runtime ) { should eq lambda_runtime }
44+ its ( :handler ) { should eq lambda_handler }
45+ its ( :description ) { should eq lambda_description }
46+ its ( :timeout ) { should eq lambda_timeout }
47+ end
48+
49+ # Testing iam role
50+ describe iam_role ( iam_role_id ) do
51+ it { should exist }
52+ its ( :role_name ) { should eq iam_role_id }
53+ its ( :description ) { should eq iam_role_description }
54+ it { should have_iam_policy ( iam_policy_name ) }
55+ end
56+
57+ # testing iam policy
58+ describe iam_policy ( iam_policy_name ) do
59+ it { should exist }
60+ it { should be_attached_to_role ( iam_role_id ) }
61+ end
62+
63+ # testing cloudwatch cron
64+ describe cloudwatch_event ( cloudwatch_cron_name ) do
65+ it { should exist }
66+ its ( :schedule_expression ) { should eq cloudwatch_cron_schedule }
67+ end
0 commit comments