-
Notifications
You must be signed in to change notification settings - Fork 102
CMR-10313: Setup the bamboo builds - code changed as well as I needed to change the structure and added tests. #2218
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
d932064
CMR-10313: Setup the bamboo builds - code changed as well as I needed…
eereiter fc10265
CMR-10313: trying to make build compatible with bamboo.
eereiter 98d197e
CMR-10313: trying to make build compatible with bamboo.
eereiter 7bcae24
CMR-10313: trying to make build compatible with bamboo.
eereiter f508813
CMR-10313: trying to make build compatible with bamboo.
eereiter fcdb3ab
CMR-10313: updating PR requests.
eereiter efc299f
CMR-10313: Adding dockerfile to build with the correct version of pyt…
eereiter c2f3352
CMR-10313: building testing tool.
eereiter 7d8e4d9
CMR-10313: building testing tool.
eereiter 6841b05
CMR-10313: building testing tool.
eereiter 01241b8
CMR-10313: Rename the test lambda
eereiter b940cd0
Merge branch 'master' into CMR-10313
eereiter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| # Use an official Python runtime as a parent image | ||
| FROM python:3.13-slim | ||
|
|
||
| # Install zip utility | ||
| RUN apt-get update && apt-get install -y zip | ||
|
|
||
| # Set the working directory in the container | ||
| WORKDIR /app | ||
|
|
||
| # Copy the current directory contents into the container at /app | ||
| COPY . /app | ||
|
|
||
| # Make sure build.sh is executable | ||
| RUN chmod +x build.sh | ||
|
|
||
| # Run build.sh when the container launches | ||
| CMD ["./build.sh"] | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| #!/bin/bash | ||
| # This script is used by the bamboo build project. | ||
|
|
||
| mkdir -p package | ||
|
|
||
| pip3 install --target ./package -r requirements.txt --no-compile | ||
|
|
||
| # Zip dependencies | ||
| cd package | ||
| zip -r ../notify_lambda_deployment_package.zip . -x "__pycache__/*" | ||
| cd .. | ||
|
|
||
| # Add contents of src directory to the zip file | ||
| cd src | ||
| zip -r ../notify_lambda_deployment_package.zip . -x "__pycache__/*" | ||
|
|
||
| # Return to the top directory | ||
| cd .. | ||
|
|
||
| rm -r package | ||
1 change: 1 addition & 0 deletions
1
subscription/notify_lambda/requirements.txt → subscription/notify-lambda/requirements.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| requests | ||
|
|
||
eereiter marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| #!/bin/bash | ||
|
|
||
| export PYTHONPATH=src | ||
|
|
||
| pip3 install requests | ||
| python3 -m unittest discover -v -s ./test -p "*_test.py" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| import json | ||
| import requests | ||
| from logger import logger | ||
|
|
||
| # This lambda is triggered through a subscription to the cmr-internal-subscription-<env> SNS topic. It processes the events which are notifications that get sent | ||
| # to an external URL. | ||
|
|
||
| def handler(event, context): | ||
| """The handler is the starting point that is triggered by an SNS topic subscription with a filter that designates tha the notification sent is a URL notification. | ||
| Input: event: Dict[str, Any], context: Any | ||
| Returns: None""" | ||
|
|
||
| logger.debug(f"Ingest notification lambda received event: {json.dumps(event, indent=2)}") | ||
| for record in event['Records']: | ||
| process_message(record) | ||
|
|
||
| def process_message(record): | ||
| """Processes the record in the event. | ||
| Input: record: Dict[str, Any] | ||
| Returns: None""" | ||
|
|
||
| try: | ||
| logger.info(f"Ingest notification lambda processing message - record: {record}") | ||
| message = record['Sns'] | ||
| message_attributes = record['Sns']['MessageAttributes'] | ||
| url = message_attributes['endpoint']['Value'] | ||
| send_message(url, message) | ||
|
|
||
| except Exception as e: | ||
| logger.error(f"Ingest notification lambda an error occurred {e} while trying to send the record: {record}") | ||
| raise e | ||
|
|
||
| def send_message(url, message): | ||
| """Sends the passed message to the external URL. If not successful the message is put onto a dead letter queue. | ||
| Input: url: str, message: Dict[str, Any] | ||
| Returns: None""" | ||
|
|
||
| # Prepare the data to be sent | ||
|
|
||
| try: | ||
| # Send a POST request to the URL with the message data | ||
| headers = {'Content-Type': 'application/json'} | ||
| logger.info(f"Ingest notification lambda sending message ID: {message['MessageId']} to URL: {url}") | ||
| response = requests.post(url, headers=headers, json=message) | ||
|
|
||
| # Check if the request was successful | ||
| if response.status_code == 200: | ||
| logger.info(f"Ingest notification lambda successfully sent message ID: {message['MessageId']}") | ||
| else: | ||
| logger.error(f"Ingest notification lambda failed to send message ID: {message['MessageId']}. Status code: {response.status_code}. Response: {response.text}") | ||
|
|
||
| except requests.exceptions.RequestException as e: | ||
| logger.error(f"Ingest notification lambda an error occurred while sending the message id {message['MessageId']} to URL: {url} {e}") | ||
|
|
94 changes: 94 additions & 0 deletions
94
subscription/notify-lambda/test/notification_lambda_test.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| import unittest | ||
| from unittest.mock import patch, MagicMock | ||
| import json | ||
| import requests | ||
| from notification_lambda import handler, process_message, send_message | ||
|
|
||
| class TestNotificationHandler(unittest.TestCase): | ||
|
|
||
| @patch('notification_lambda.process_message') | ||
| def test_handler(self, mock_process_message): | ||
| event = { | ||
| 'Records': [ | ||
| {'some': 'data1'}, | ||
| {'some': 'data2'} | ||
| ] | ||
| } | ||
| context = {} | ||
|
|
||
| handler(event, context) | ||
|
|
||
| self.assertEqual(mock_process_message.call_count, 2) | ||
| mock_process_message.assert_any_call({'some': 'data1'}) | ||
| mock_process_message.assert_any_call({'some': 'data2'}) | ||
|
|
||
| @patch('notification_lambda.send_message') | ||
| def test_process_message(self, mock_send_message): | ||
| record = { | ||
| 'Sns': { | ||
| 'MessageId': '12345', | ||
| 'MessageAttributes': { | ||
| 'endpoint': { | ||
| 'Value': 'http://example.com' | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| process_message(record) | ||
|
|
||
| mock_send_message.assert_called_once_with('http://example.com', record['Sns']) | ||
|
|
||
| @patch('notification_lambda.send_message') | ||
| def test_process_message_exception(self, mock_send_message): | ||
| record = { | ||
| 'Sns': { | ||
| 'MessageId': '12345', | ||
| 'MessageAttributes': {} # Missing 'endpoint' to cause an exception | ||
| } | ||
| } | ||
|
|
||
| with self.assertRaises(Exception): | ||
| process_message(record) | ||
|
|
||
| @patch('requests.post') | ||
| def test_send_message_success(self, mock_post): | ||
| mock_response = MagicMock() | ||
| mock_response.status_code = 200 | ||
| mock_post.return_value = mock_response | ||
|
|
||
| url = 'http://example.com' | ||
| message = {'MessageId': '12345', 'some': 'data'} | ||
|
|
||
| send_message(url, message) | ||
|
|
||
| mock_post.assert_called_once_with(url, headers={'Content-Type': 'application/json'}, json=message) | ||
|
|
||
| @patch('requests.post') | ||
| def test_send_message_failure(self, mock_post): | ||
| mock_response = MagicMock() | ||
| mock_response.status_code = 400 | ||
| mock_response.text = 'Bad Request' | ||
| mock_post.return_value = mock_response | ||
|
|
||
| url = 'http://example.com' | ||
| message = {'MessageId': '12345', 'some': 'data'} | ||
|
|
||
| send_message(url, message) | ||
|
|
||
| mock_post.assert_called_once_with(url, headers={'Content-Type': 'application/json'}, json=message) | ||
|
|
||
| @patch('requests.post') | ||
| def test_send_message_exception(self, mock_post): | ||
| mock_post.side_effect = requests.exceptions.RequestException('Network error') | ||
|
|
||
| url = 'http://example.com' | ||
| message = {'MessageId': '12345', 'some': 'data'} | ||
|
|
||
| send_message(url, message) | ||
|
|
||
| mock_post.assert_called_once_with(url, headers={'Content-Type': 'application/json'}, json=message) | ||
|
|
||
| if __name__ == '__main__': | ||
| unittest.main() | ||
|
|
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| # Use an official Python runtime as a parent image | ||
| FROM python:3.13-slim | ||
|
|
||
| # Install zip utility | ||
| RUN apt-get update && apt-get install -y zip | ||
|
|
||
| # Set the working directory in the container | ||
| WORKDIR /app | ||
|
|
||
| # Copy the current directory contents into the container at /app | ||
| COPY . /app | ||
|
|
||
| # Make sure build.sh is executable | ||
| RUN chmod +x build.sh | ||
|
|
||
| # Run build.sh when the container launches | ||
| CMD ["./build.sh"] | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| url-endpoint-test-lambda function is a test lambda meant to test subscriptions with a URL endpoint. | ||
| The test lambda will run in SIT and WL and it provides an endpoint that testers can use to test URL endpoint based subscriptions. | ||
|
|
||
| A user creates an ingest granule subscription. An example follows: | ||
| {"Name": "Ingest-Subscription-Test-Sit-http", | ||
| "Type": "granule", | ||
| "SubscriberId": "user1", | ||
| "CollectionConceptId": "C1200463968-CMR_ONLY", | ||
| "EndPoint": "http://<the-internal-loadbalancer-url>/notification/tester", | ||
| "Mode": ["New", "Update", "Delete"], | ||
| "Method": "ingest", | ||
| "MetadataSpecification": { | ||
| "URL": "https://cdn.earthdata.nasa.gov/umm/subscription/v1.1.1", | ||
| "Name": "UMM-Sub", | ||
| "Version": "1.1.1" | ||
| } | ||
| } | ||
|
|
||
| Make sure the URL is the CMR internal load balancer followed by /notification/tester. | ||
|
|
||
| Ingest a granule. | ||
|
|
||
| To verify that the notification is correct and that it was sent through the tunnel into the CMR internal load balancer, | ||
| issue a get request to the load balancer with the correct tunnel port number such as | ||
|
|
||
| curl http://localhost:8081/notification/tester | ||
|
|
||
| The contents of the notification will be sent back to the caller and can then be verified. | ||
|
|
||
| To send a test notification to the test tool send a post request. An example follows: | ||
|
|
||
| curl -XPOST -H "Content-Type: application/json" http://localhost:8081/notification/tester -d '{ | ||
| "Records": [ | ||
| { | ||
| "EventSource": "aws:sns", | ||
| "EventVersion": "1.0", | ||
| "EventSubscriptionArn": "arn:aws:sns:<region>:<account>:<SNS name>:<unique ID>", | ||
| "Sns": { | ||
| "Type": "Notification", | ||
| "MessageId": "ed8c7ee0-c70a-5050-8ef9-1effe57d3dde", | ||
| "TopicArn": "arn:aws:sns:<region>:<account>:<sns name>", | ||
| "Subject": "testing again", | ||
| "Message": "testing again", | ||
| "Timestamp": "2025-02-06T20:48:55.564Z", | ||
| "SignatureVersion": "1", | ||
| "Signature": "iN...TXas7iBEoT5Nw==", | ||
| "SigningCertUrl": "https://sns.<region>.amazonaws.com/SimpleNotificationService-9...6.pem", | ||
| "UnsubscribeUrl": "https://sns.<region>.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=<subscription arn>", | ||
| "MessageAttributes": { | ||
| "endpoint": "URL" | ||
| } | ||
| } | ||
| } | ||
| ] | ||
| }' | ||
|
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.