Skip to content

Commit

Permalink
Add unit tests for config retrieval (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
yamanmalkoc authored and SkylarLJY committed Feb 14, 2022
1 parent c9303ce commit e2c236c
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 1 deletion.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@ TODO.md
spot/benchmarks/*/workload.json

spot-env/
dump/

#Unittest Logs
.coverage

dump/
Empty file added tests/__init__.py
Empty file.
Empty file added tests/config_tests/__init__.py
Empty file.
82 changes: 82 additions & 0 deletions tests/config_tests/sample_function_configuration.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
{
"FunctionName": "string",
"FunctionArn": "string",
"Runtime": "python3.8",
"Role": "string",
"Handler": "string",
"CodeSize": 123,
"Description": "string",
"Timeout": 123,
"MemorySize": 123,
"LastModified": "2022-02-09T21:58:22.000+0000",
"CodeSha256": "string",
"Version": "string",
"VpcConfig": {
"SubnetIds": [
"string"
],
"SecurityGroupIds": [
"string"
],
"VpcId": "string"
},
"DeadLetterConfig": {
"TargetArn": "string"
},
"Environment": {
"Variables": {
"string": "string"
},
"Error": {
"ErrorCode": "string",
"Message": "string"
}
},
"KMSKeyArn": "string",
"TracingConfig": {
"Mode": "Active"
},
"MasterArn": "string",
"RevisionId": "string",
"Layers": [
{
"Arn": "string",
"CodeSize": 123,
"SigningProfileVersionArn": "string",
"SigningJobArn": "string"
}
],
"State": "Active",
"StateReason": "string",
"StateReasonCode": "Idle",
"LastUpdateStatus": "Successful",
"LastUpdateStatusReason": "string",
"LastUpdateStatusReasonCode": "EniLimitExceeded",
"FileSystemConfigs": [
{
"Arn": "string",
"LocalMountPath": "string"
}
],
"PackageType": "Zip",
"ImageConfigResponse": {
"ImageConfig": {
"EntryPoint": [
"string"
],
"Command": [
"string"
],
"WorkingDirectory": "string"
},
"Error": {
"ErrorCode": "string",
"Message": "string"
}
},
"SigningProfileVersionArn": "string",
"SigningJobArn": "string",
"Architectures": [
"x86_64"
]
}
58 changes: 58 additions & 0 deletions tests/config_tests/test_config_retriever.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import unittest
import json
from unittest.mock import patch, call
import datetime

import spot
from spot.configs.aws_config_retriever import AWSConfigRetriever
from spot.db.db import DBClient


class TestConfigRetrieval(unittest.TestCase):
def setUp(self) -> None:
self.function = "AWSHelloWorld"
self.timestamp = 0
self.configRetriever = AWSConfigRetriever(self.function, "localhost", 27017)

@patch.object(
spot.configs.aws_config_retriever.DBClient, "add_new_config_if_changed"
)
@patch("spot.configs.aws_config_retriever.boto3")
def test_get_latest_config(self, mockBoto3, mockDBClient):
sample_config = []
with open("tests/config_tests/sample_function_configuration.json") as f:
sample_config = json.load(f)
mockBoto3.client(
"lambda"
).get_function_configuration.return_value = sample_config
self.configRetriever.get_latest_config()

# assert a database call has been made with function add_new_config_if_changed with appropriate variables
date = datetime.datetime.strptime(
sample_config["LastModified"], "%Y-%m-%dT%H:%M:%S.%f+0000"
)
timestamp = str(
(date - datetime.datetime(1970, 1, 1)).total_seconds() * 1000
)
sample_config["LastModifiedInMs"] = int(timestamp[:-2])
sample_config["Architectures"] = sample_config["Architectures"][0]
callTemp = call(self.function, "config", sample_config)
mockDBClient.assert_has_calls([callTemp])

@patch.object(
spot.configs.aws_config_retriever.DBClient, "get_all_collection_documents"
)
def test_print_configs(self, mockDBClient):
sample_config = []
with open("tests/config_tests/sample_function_configuration.json") as f:
sample_config = json.load(f)
mockDBClient.get_all_collection_documents.return_value = [sample_config]
self.configRetriever.print_configs()

# assert a database call to get_all_collection_documents has been made with proper variables
callTemp = call(self.function, "config")
mockDBClient.assert_has_calls([callTemp])


if __name__ == "__main__":
unittest.main()

0 comments on commit e2c236c

Please sign in to comment.