Skip to content

Commit

Permalink
Merge pull request #41 from xiao201208/feature/clean_suppoort_stack_id
Browse files Browse the repository at this point in the history
test clean support specific stack id
  • Loading branch information
xiao201208 authored May 16, 2024
2 parents a9341df + a8263fd commit 33f4778
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 15 deletions.
5 changes: 3 additions & 2 deletions iact3/cli_modules/delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ def __init__(self, regions: str = None, config_file: str = None, project_path: s
async def create(cls, regions: str = None,
config_file: str = None,
project_path: str = None,
tags: dict = None):
tags: dict = None,
stack_id: str = None):
credential = List.get_credential(config_file, project_path)
all_stacks = await List.create(regions, config_file, project_path, tags)
all_stacks = await List.create(regions, config_file, project_path, tags, stack_id=stack_id)
if not all_stacks:
LOG.info('can not find stack to delete.')
return
Expand Down
5 changes: 3 additions & 2 deletions iact3/cli_modules/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ def __init__(self, regions: str = None, config_file: str = None, project_path: s
async def create(cls, regions: str = None,
config_file: str = None,
project_path: str = None,
tags: dict = None):
tags: dict = None,
stack_id: str = None):
credential = cls.get_credential(config_file, project_path)
if regions:
regions = regions.split(',')
Expand All @@ -45,7 +46,7 @@ async def create(cls, regions: str = None,
for region in regions:
stack_plugin = StackPlugin(region_id=region, credential=credential)
list_tasks.append(
asyncio.create_task(stack_plugin.fetch_all_stacks(tags))
asyncio.create_task(stack_plugin.fetch_all_stacks(tags, stack_id=stack_id))
)
stacks = await asyncio.gather(*list_tasks)
all_stacks, project_length, test_length, stack_name_length = cls._get_all_stacks(stacks)
Expand Down
6 changes: 4 additions & 2 deletions iact3/cli_modules/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,14 @@ async def run(template: str = None,
await tests.report(output_directory, project_path, log_format)

@staticmethod
async def clean(regions: str = None):
@CliCore.longform_param_required('stack_id')
async def clean(regions: str = None, stack_id: str = None):
'''
Manually clean up the stacks which were created by Iact3
:param regions: comma separated list of regions to delete from, default will scan all regions
:param stack_id: stack_id to delete from, default will scan all regions
'''
await Delete.create(regions)
await Delete.create(regions, stack_id=stack_id)

@staticmethod
async def list(regions: str = None):
Expand Down
4 changes: 2 additions & 2 deletions iact3/plugin/ros.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ async def list_stacks(self, stack_id=None, stack_name=None):
result = await self.send_request('ListStacksRequest', **request_kwargs)
return result.get('Stacks')

async def fetch_all_stacks(self, tags):
kwargs = {}
async def fetch_all_stacks(self, tags, stack_id=None):
kwargs = {'StackId': stack_id}
self._convert_tags(tags, kwargs, tag_key='Tag')
return await self.fetch_all('ListStacksRequest', kwargs, 'Stacks')

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

setuptools.setup(
name="alibabacloud-ros-iact3",
version="0.1.6",
version="0.1.7",

description="Iact3 is a tool that tests Terraform and ROS(Resource Orchestration Service) templates.",
long_description=long_description,
Expand Down
14 changes: 8 additions & 6 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class TestConfig(BaseTest):
async def test_main(self):
sys.argv = [
'', 'test', 'run', '--project-path', str(self.DATA_PATH),
'-t', 'ecs_instance.template.json', '-c', 'full_config.yml'
'-t', 'simple_template.yml', '-c', '.iact3.yml', '--no-delete'
]
await run()

Expand All @@ -29,6 +29,12 @@ async def test_delete(self):
]
await run()

async def test_delete_with_stack_id(self):
sys.argv = [
'', 'test', 'clean', '--stack-id', 'd5ea4c0f-3ec7-416e-afe7-06d41bdf56ba'
]
await run()

async def test_params(self):
sys.argv = [
'', 'test', 'run', '--project-path', str(self.DATA_PATH),
Expand All @@ -38,7 +44,7 @@ async def test_params(self):

async def test_debug(self):
sys.argv = [
'', 'test', 'run', '--project-path', str(self.DATA_PATH), '-c', 'real.iact3.yml'
'', 'test', 'run', '--project-path', str(self.DATA_PATH), '-c', 'real.iact3.yml'
]
await run()

Expand Down Expand Up @@ -91,7 +97,6 @@ async def test_validate(self):
logs = memory_handler.buffer
self.assertIn("LegalTemplate Check passed", logs[3].getMessage())


async def test_validate_fail(self):
memory_handler = logging.handlers.MemoryHandler(capacity=10240)
logger = logging.getLogger()
Expand All @@ -105,7 +110,6 @@ async def test_validate_fail(self):
logs = memory_handler.buffer
self.assertIn("InvalidTemplate", logs[3].getMessage())


async def test_preview(self):
memory_handler = logging.handlers.MemoryHandler(capacity=10240)
logger = logging.getLogger()
Expand Down Expand Up @@ -135,7 +139,6 @@ async def test_preview_fail(self):
self.assertIn("StackValidationFailed", logs[3].getMessage())
self.assertIn("code:", logs[4].getMessage())


async def test_policy(self):
memory_handler = logging.handlers.MemoryHandler(capacity=10240)
logger = logging.getLogger()
Expand All @@ -162,4 +165,3 @@ async def test_policy_fail(self):

logs = memory_handler.buffer
self.assertIn("\"Statement\": []", logs[1].getMessage())

6 changes: 6 additions & 0 deletions tests/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,9 @@ async def test_run_with_simple(self):
config_file = os.path.join(self.DATA_PATH, f'.{IAC_NAME}.yml')
template = os.path.join(self.DATA_PATH, 'simple_template.yml')
await Test.run(config_file=config_file, template=template)

async def test_run_with_delete_one_stack(self):
config_file = os.path.join(self.DATA_PATH, f'.{IAC_NAME}.yml')
template = os.path.join(self.DATA_PATH, 'simple_template.yml')
await Test.run(config_file=config_file, template=template, no_delete=True)

0 comments on commit 33f4778

Please sign in to comment.