Skip to content

Commit

Permalink
[cli] fix a minor bug when list-targets
Browse files Browse the repository at this point in the history
  • Loading branch information
Chunyong Lin committed Mar 25, 2020
1 parent 530a154 commit 41badb6
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 3 deletions.
1 change: 0 additions & 1 deletion streamalert_cli/manage_lambda/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

# Build .zip files in the top-level of the terraform directory
THIS_DIRECTORY = os.path.dirname(os.path.realpath(__file__))
BUILD_DIRECTORY = os.path.join(THIS_DIRECTORY, '..', '..', 'terraform')
LOGGER = get_logger(__name__)


Expand Down
2 changes: 1 addition & 1 deletion streamalert_cli/terraform/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ def generate_global_lambda_settings(config, config_name, generate_func, tf_tmp_f
'It is required to explicitly set "file_format" for '
'athena_partition_refresh_config in "conf/lambda.json" when upgrading to v3.1.0. '
'Available values are "parquet" and "json". For more information, refer to '
'https://github.com/airbnb/streamalert/issues/1143'
'https://github.com/airbnb/streamalert/issues/1143. '
'In the future release, the default value of "file_format" will '
'be changed to "parquet".'
)
Expand Down
2 changes: 1 addition & 1 deletion streamalert_cli/terraform/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ def get_tf_modules(config, generate=False):

modules = set()
resources = set()
for root, _, files in os.walk('terraform'):
for root, _, files in os.walk(TERRAFORM_FILES_PATH):
for file_name in files:
path = os.path.join(root, file_name)
if path.endswith('.tf.json'):
Expand Down
81 changes: 81 additions & 0 deletions tests/unit/streamalert_cli/terraform/test_handlers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
"""
Copyright 2017-present Airbnb, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import json
import os

from mock import patch, Mock
from nose.tools import assert_equal, assert_false
from pyfakefs import fake_filesystem_unittest

from streamalert_cli.terraform import TERRAFORM_FILES_PATH
from streamalert_cli.terraform.handlers import get_tf_modules

class TestTerraformHandlers(fake_filesystem_unittest.TestCase):
"""Test class for the Terraform handler functions"""
# pylint: disable=attribute-defined-outside-init,no-self-use

def setUp(self):
"""Setup before each method"""
self.setUpPyfakefs()

mock_main_tf_json = {
'module': {
'module1': {
'foo': 'bar'
}
}
}
mock_prod_tf_json = {
'module': {
'module2': {
'foo': 'bar'
}
},
'resource': {
'resource1': {'foo': 'test'},
'resource2': {'bar': 'test'},
'resource3': {'pan': 'test'}
}
}
# fake *.tf.json files
self.fs.create_file(
os.path.join(TERRAFORM_FILES_PATH, 'main.tf.json'),
contents=json.dumps(mock_main_tf_json)
)
self.fs.create_file(
os.path.join(TERRAFORM_FILES_PATH, 'prod.tf.json'),
contents=json.dumps(mock_prod_tf_json)
)

@patch('streamalert_cli.terraform.handlers.terraform_generate_handler', Mock(return_value=True))
def test_get_tf_modules_read_tf_json_files(self):
"""CLI - Terraform handler function get tf modules read all *.tf.json files"""
config = {}
result = get_tf_modules(config)

expected_result = {
'module': {'module1', 'module2'},
'resource': {'resource1.foo', 'resource2.bar', 'resource3.pan'}
}

assert_equal(result, expected_result)

@patch(
'streamalert_cli.terraform.handlers.terraform_generate_handler', Mock(return_value=False)
)
def test_get_tf_modules_early_return(self):
"""CLI - Terraform handler function get tf modules return early"""
assert_false(get_tf_modules(config={}, generate=True))

0 comments on commit 41badb6

Please sign in to comment.