Skip to content

Commit 38858c5

Browse files
committed
updated to run on latest version of LocalStack (locally on dev laptop is "version": "4.6.1.dev69" )
refactored localstack test obj setup code into Temp__Random__AWS_Credentials() fixed couple tests (caused by latest version of LocalStack on DynamoDB, which doesn't have the 'Replicas' variable)
1 parent f687776 commit 38858c5

File tree

11 files changed

+77
-57
lines changed

11 files changed

+77
-57
lines changed

.github/workflows/ci-pipeline__dev.yml

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,24 @@ jobs:
2727
with:
2828
LOCAL_STACK_SERVICES: 's3,lambda,iam,cloudwatch,dynamodb,logs,sts,ec2'
2929

30-
- name: Poetry - Install Dependencies
31-
uses: owasp-sbot/OSBot-GitHub-Actions/.github/actions/poetry__install@dev
30+
- name: "run-tests"
31+
uses: owasp-sbot/OSBot-GitHub-Actions/.github/actions/pytest__run-tests@dev # run tests (which use Local Stack)
3232
with:
33-
pip_install: '-r requirements-test.txt'
33+
test_target: "tests"
3434

35-
- name: Run Unit Tests (using Poetry)
36-
uses: owasp-sbot/OSBot-GitHub-Actions/.github/actions/poetry__run-unit-tests@dev
35+
# - name: Poetry - Install Dependencies
36+
# uses: owasp-sbot/OSBot-GitHub-Actions/.github/actions/poetry__install@dev
37+
# with:
38+
# pip_install: '-r requirements-test.txt'
3739

38-
- name: Run Integrations Tests (using Poetry)
39-
uses: owasp-sbot/OSBot-GitHub-Actions/.github/actions/poetry__run-unit-tests@dev
40-
with:
41-
test_target: 'tests/integration'
40+
41+
# - name: Run Unit Tests (using Poetry)
42+
# uses: owasp-sbot/OSBot-GitHub-Actions/.github/actions/poetry__run-unit-tests@dev
43+
#
44+
# - name: Run Integrations Tests (using Poetry)
45+
# uses: owasp-sbot/OSBot-GitHub-Actions/.github/actions/poetry__run-unit-tests@dev
46+
# with:
47+
# test_target: 'tests/integration'
4248
# env:
4349
# AWS_SECRET_ACCESS_KEY : ${{ secrets.AWS_SECRET_ACCESS_KEY }}
4450
# AWS_DEFAULT_REGION : ${{ secrets.AWS_DEFAULT_REGION }}

osbot_aws/testing/Temp__Random__AWS_Credentials.py

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,48 @@
11
import random
22
import string
3-
from osbot_utils.testing.Temp_Env_Vars import Temp_Env_Vars
3+
from osbot_utils.utils.Env import get_env
4+
from osbot_utils.testing.Temp_Env_Vars import Temp_Env_Vars
45

5-
class Temp__Random__AWS_Credentials(Temp_Env_Vars):
6-
7-
def __init__(self, **kwargs):
8-
super().__init__(**kwargs)
9-
random_aws_credentials = dict( # Create the AWS credentials with realistic random values
10-
AWS_ACCESS_KEY_ID = self.random_aws_access_key_id (),
11-
AWS_SECRET_ACCESS_KEY = self.random_aws_secret_access_key(),
12-
AWS_ACCOUNT_ID = self.random_aws_account_id (),
13-
AWS_DEFAULT_REGION = self.random_aws_region ()
14-
)
15-
self.env_vars.update(random_aws_credentials) # add temp aws vars to exiting env_vars (set in ctor)
6+
OSBOT_AWS__LOCAL_STACK__AWS_ACCOUNT_ID = '000000000000' # default local-stack account id for lambdas
7+
OSBOT_AWS__LOCAL_STACK__AWS_DEFAULT_REGION = 'us-east-1' # default local-stack region for lambdas
168

179

18-
# Helper functions to generate random values matching AWS schema
10+
class Temp__Random__AWS_Credentials(Temp_Env_Vars):
11+
AWS_ACCESS_KEY_ID : str = None
12+
AWS_SECRET_ACCESS_KEY : str = None
13+
AWS_ACCOUNT_ID : str = None
14+
AWS_DEFAULT_REGION : str = None
15+
16+
def __enter__(self):
17+
self.with_default_credentials() # this will call the super().set_vars()
18+
return self
19+
20+
21+
def with_localstack_credentials(self):
22+
self.AWS_ACCOUNT_ID = OSBOT_AWS__LOCAL_STACK__AWS_ACCOUNT_ID
23+
self.AWS_DEFAULT_REGION = OSBOT_AWS__LOCAL_STACK__AWS_DEFAULT_REGION
24+
self.AWS_ACCESS_KEY_ID = self.random_aws_access_key_id()
25+
self.AWS_SECRET_ACCESS_KEY = self.random_aws_secret_access_key()
26+
self.set_aws_env_vars()
27+
return self
28+
29+
def with_default_credentials(self): # Set default credentials, using existing values or generating new ones.
30+
if not self.AWS_ACCESS_KEY_ID : self.AWS_ACCESS_KEY_ID = get_env('AWS_ACCESS_KEY_ID' ) or self.random_aws_access_key_id () # Generate new values only if not already set
31+
if not self.AWS_SECRET_ACCESS_KEY: self.AWS_SECRET_ACCESS_KEY = get_env('AWS_SECRET_ACCESS_KEY') or self.random_aws_secret_access_key()
32+
if not self.AWS_ACCOUNT_ID : self.AWS_ACCOUNT_ID = get_env('AWS_ACCOUNT_ID' ) or self.random_aws_account_id ()
33+
if not self.AWS_DEFAULT_REGION : self.AWS_DEFAULT_REGION = get_env('AWS_DEFAULT_REGION' ) or self.random_aws_region ()
34+
35+
self.set_aws_env_vars()
36+
return self
37+
38+
def set_aws_env_vars(self): # Update env_vars with the credentials
39+
aws_credentials = { 'AWS_ACCESS_KEY_ID' : self.AWS_ACCESS_KEY_ID ,
40+
'AWS_SECRET_ACCESS_KEY' : self.AWS_SECRET_ACCESS_KEY,
41+
'AWS_ACCOUNT_ID' : self.AWS_ACCOUNT_ID ,
42+
'AWS_DEFAULT_REGION' : self.AWS_DEFAULT_REGION }
43+
self.env_vars.update(aws_credentials)
44+
self.set_vars()
45+
return self
1946

2047
def random_aws_access_key_id(self):
2148
return 'AKIA' + ''.join(random.choices(string.ascii_uppercase + string.digits, k=16)) # AWS access key IDs are typically 20 characters long, uppercase letters and digits

osbot_aws/testing/TestCase__Dynamo_DB__Local.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
import boto3
2-
from osbot_aws.AWS_Config import AWS_Config
3-
4-
from osbot_aws.apis.Session import URL__LOCAL_STACK__ENDPOINT_URL
5-
from osbot_aws.aws.dynamo_db.Dynamo_DB import Dynamo_DB
6-
from osbot_utils.testing.Temp_Env_Vars import Temp_Env_Vars
7-
from unittest import TestCase
8-
from osbot_utils.testing.Hook_Method import Hook_Method
9-
from osbot_utils.utils.Env import get_env
2+
from osbot_aws.AWS_Config import AWS_Config
3+
from osbot_aws.apis.Session import URL__LOCAL_STACK__ENDPOINT_URL
4+
from osbot_aws.aws.dynamo_db.Dynamo_DB import Dynamo_DB
5+
from unittest import TestCase
106
from tests.integration.osbot_aws__objs_for__integration_tests import setup__osbot_aws__integration_tests
117

128
# TEST__AWS_ACCOUNT_ID = '000011110000'

osbot_aws/testing/TestCase__S3__Temp_DB.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class TestCase__S3__Temp_DB(TestCase):
1010
def setUpClass(cls):
1111
setup__osbot_aws__integration_tests()
1212
#cls.local_stack = Local_Stack().activate()
13-
cls.random_aws_creds = Temp__Random__AWS_Credentials().set_vars()
13+
cls.random_aws_creds = Temp__Random__AWS_Credentials().with_default_credentials()
1414
cls.s3_db_base = S3__DB_Base()
1515
cls.s3_db = cls.s3_db_base # todo: see if this is a better name to use when using this class
1616
with cls.s3_db_base as _:

tests/integration/aws/dynamo_db/domains/test_DyDB__Table.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,6 @@ def test_info(self):
153153
'NumberOfDecreasesToday': 0 ,
154154
'ReadCapacityUnits': 0 ,
155155
'WriteCapacityUnits': 0 },
156-
'Replicas' : [] ,
157156
'TableArn' : f'arn:aws:dynamodb:{self.region_name}:{self.account_id}:table/{self.table_name}',
158157
'TableId' : TableId ,
159158
'TableName' : self.table_name ,

tests/integration/aws/dynamo_db/test_Dynamo_DB__Cached.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def test_table_info(self):
3636
assert self.dynamo_db.table_info(table_name='AAAA-Not-Exists') == {}
3737
table_info = self.dynamo_db.table_info(table_name=self.table_name)
3838
assert list_set(table_info) == ['AttributeDefinitions', 'BillingModeSummary', 'CreationDateTime', 'DeletionProtectionEnabled',
39-
'ItemCount', 'KeySchema', 'ProvisionedThroughput', 'Replicas', 'TableArn', 'TableId',
39+
'ItemCount', 'KeySchema', 'ProvisionedThroughput', 'TableArn', 'TableId',
4040
'TableName', 'TableSizeBytes', 'TableStatus']
4141

4242
def test_table_status(self):

tests/integration/aws/dynamo_db/test_Dynamo_DB__Table.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import datetime
2-
3-
import pytest
42
from dateutil.tz import tzlocal
53
from osbot_utils.utils.Misc import list_set, is_guid
64
from tests.integration.aws.dynamo_db.TestCase__Temp_Dynamo_DB_Table import TestCase__Temp_Dynamo_DB_Table
@@ -60,10 +58,9 @@ def test_info(self):
6058
table_id = data.get('TableId')
6159
del data['CreationDateTime']
6260
del data['BillingModeSummary']['LastUpdateToPayPerRequestDateTime']
63-
6461
assert list_set(result) == ['data', 'status']
6562
assert list_set(data) == ['AttributeDefinitions', 'BillingModeSummary', 'DeletionProtectionEnabled', 'ItemCount', 'KeySchema',
66-
'ProvisionedThroughput', 'Replicas', 'TableArn', 'TableId', 'TableName', 'TableSizeBytes', 'TableStatus']
63+
'ProvisionedThroughput', 'TableArn', 'TableId', 'TableName', 'TableSizeBytes', 'TableStatus']
6764
assert is_guid(table_id) is True
6865

6966
assert data == { 'AttributeDefinitions' : [{'AttributeName': self.key_name, 'AttributeType': 'S'}] ,
@@ -77,7 +74,6 @@ def test_info(self):
7774
'NumberOfDecreasesToday': 0 ,
7875
'ReadCapacityUnits' : 0 ,
7976
'WriteCapacityUnits' : 0 } ,
80-
'Replicas' : [],
8177
'TableArn' : f'arn:aws:dynamodb:{region_name}:{account_id}:table/{self.table_name}',
8278
'TableId' : table_id ,
8379
'TableName' : self.table_name ,

tests/integration/aws/lambda_/dependencies/test_Lambda__Dependency.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from unittest import TestCase
22
from osbot_aws.aws.lambda_.dependencies.Lambda__Dependency import Lambda__Dependency
3-
from osbot_aws.aws.lambda_.dependencies.Lambda__Dependency__Local import Lambda__Dependency__Local
4-
from osbot_aws.aws.lambda_.dependencies.Lambda__Dependency__S3 import Lambda__Dependency__S3
3+
from osbot_aws.aws.lambda_.dependencies.Lambda__Dependency__Local import Lambda__Dependency__Local
4+
from osbot_aws.aws.lambda_.dependencies.Lambda__Dependency__S3 import Lambda__Dependency__S3
55
from tests.integration.aws.lambda_.dependencies.test_Lambda__Dependency__Local import LAMBDA_DEPENDENCY__SMALL_TEST__PACKAGE
66
from tests.integration.osbot_aws__objs_for__integration_tests import setup__osbot_aws__integration_tests
77

tests/integration/aws/lambda_/dependencies/test_Lambda__Dependency__Local.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
from unittest import TestCase
2+
from osbot_aws.testing.Temp__Random__AWS_Credentials import OSBOT_AWS__LOCAL_STACK__AWS_ACCOUNT_ID, OSBOT_AWS__LOCAL_STACK__AWS_DEFAULT_REGION
23
from osbot_utils.utils.Env import in_github_action
34
from osbot_utils.utils.Zip import zip_bytes__file_list, zip_bytes__files
45
from osbot_aws.AWS_Config import AWS_Config, DEFAULT__BUCKET_NAME__INFIX__LAMBDA
56
from osbot_aws.aws.lambda_.schemas.Safe_Str__File__Name__Python_Package import Safe_Str__File__Name__Python_Package
67
from osbot_utils.utils.Objects import __
78
from osbot_utils.utils.Files import file_exists, folder_exists, current_temp_folder, path_combine, file_bytes
89
from osbot_aws.aws.lambda_.dependencies.Lambda__Dependency__Local import Lambda__Dependency__Local, Schema__Lambda__Dependency__Local_Install__Data, FOLDER_NAME__LAMBDA_DEPENDENCIES_STORAGE
9-
from tests.integration.osbot_aws__objs_for__integration_tests import setup__osbot_aws__integration_tests, OSBOT_AWS__TEST__AWS_ACCOUNT_ID, OSBOT_AWS__TEST__AWS_DEFAULT_REGION
10+
from tests.integration.osbot_aws__objs_for__integration_tests import setup__osbot_aws__integration_tests
1011

1112
LAMBDA_DEPENDENCY__SMALL_TEST__PACKAGE = 'colorama==0.4.6'
1213

@@ -156,8 +157,8 @@ def test__folder__bucket(self):
156157
'/osbot-aws__lambda-dependencies-storage' + # the FOLDER_NAME__LOCAL__DEPENDENCIES_STORAGE
157158
'/000000000000--osbot-lambdas--us-east-1') # using localstack default account-id and region_name and the AWS_Config.DEFAULT__BUCKET_NAME__INFIX__LAMBDA
158159

159-
assert _.folder__bucket() == (_.base_folder() + '/ ' +
160-
FOLDER_NAME__LAMBDA_DEPENDENCIES_STORAGE + '/' +
161-
OSBOT_AWS__TEST__AWS_ACCOUNT_ID + '--' +
162-
DEFAULT__BUCKET_NAME__INFIX__LAMBDA + '--' +
163-
OSBOT_AWS__TEST__AWS_DEFAULT_REGION )
160+
assert _.folder__bucket() == (_.base_folder() + '/ ' +
161+
FOLDER_NAME__LAMBDA_DEPENDENCIES_STORAGE + '/' +
162+
OSBOT_AWS__LOCAL_STACK__AWS_ACCOUNT_ID + '--' +
163+
DEFAULT__BUCKET_NAME__INFIX__LAMBDA + '--' +
164+
OSBOT_AWS__LOCAL_STACK__AWS_DEFAULT_REGION )

tests/integration/osbot_aws__objs_for__integration_tests.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
from osbot_utils.utils.Env import set_env
21
from osbot_aws.testing.Temp__Random__AWS_Credentials import Temp_AWS_Credentials
32
from osbot_local_stack.local_stack.Local_Stack import Local_Stack
43
from osbot_utils.type_safe.Type_Safe import Type_Safe
54

6-
OSBOT_AWS__TEST__AWS_ACCOUNT_ID = '000000000000' # default local-stack account id for lambdas
7-
OSBOT_AWS__TEST__AWS_DEFAULT_REGION = 'us-east-1' # default local-stack region for lambdas
85

96
class OSBot_AWS__Integration_Tests(Type_Safe):
107
local_stack : Local_Stack = None
@@ -13,9 +10,7 @@ class OSBot_AWS__Integration_Tests(Type_Safe):
1310
osbot_aws_integration_tests = OSBot_AWS__Integration_Tests()
1411

1512
def setup_local_stack() -> Local_Stack: # todo: refactor this to the OSBot_Local_Stack code base
16-
Temp_AWS_Credentials().set_vars()
17-
set_env('AWS_ACCOUNT_ID' , OSBOT_AWS__TEST__AWS_ACCOUNT_ID ) # todo: fix the Temp_AWS_Credentials so that we don't need use this set_env
18-
set_env('AWS_DEFAULT_REGION', OSBOT_AWS__TEST__AWS_DEFAULT_REGION)
13+
Temp_AWS_Credentials().with_localstack_credentials()
1914
local_stack = Local_Stack().activate()
2015
return local_stack
2116

0 commit comments

Comments
 (0)