Best Python code snippet using localstack_python
test_s3_bucket_tagging.py
Source:test_s3_bucket_tagging.py
1#2# Licensed to the Apache Software Foundation (ASF) under one3# or more contributor license agreements. See the NOTICE file4# distributed with this work for additional information5# regarding copyright ownership. The ASF licenses this file6# to you under the Apache License, Version 2.0 (the7# "License"); you may not use this file except in compliance8# with the License. You may obtain a copy of the License at9#10# http://www.apache.org/licenses/LICENSE-2.011#12# Unless required by applicable law or agreed to in writing,13# software distributed under the License is distributed on an14# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY15# KIND, either express or implied. See the License for the16# specific language governing permissions and limitations17# under the License.18import os19import unittest20from unittest import mock21from moto import mock_s322from airflow.providers.amazon.aws.hooks.s3 import S3Hook23from airflow.providers.amazon.aws.operators.s3 import (24 S3DeleteBucketTaggingOperator,25 S3GetBucketTaggingOperator,26 S3PutBucketTaggingOperator,27)28BUCKET_NAME = os.environ.get("BUCKET_NAME", "test-airflow-bucket")29TAG_SET = [{'Key': 'Color', 'Value': 'Green'}]30TASK_ID = os.environ.get("TASK_ID", "test-s3-operator")31class TestS3GetBucketTaggingOperator(unittest.TestCase):32 def setUp(self):33 self.get_bucket_tagging_operator = S3GetBucketTaggingOperator(34 task_id=TASK_ID,35 bucket_name=BUCKET_NAME,36 )37 @mock_s338 @mock.patch.object(S3Hook, "get_bucket_tagging")39 @mock.patch.object(S3Hook, "check_for_bucket")40 def test_execute_if_bucket_exist(self, mock_check_for_bucket, get_bucket_tagging):41 mock_check_for_bucket.return_value = True42 # execute s3 get bucket tagging operator43 self.get_bucket_tagging_operator.execute({})44 mock_check_for_bucket.assert_called_once_with(BUCKET_NAME)45 get_bucket_tagging.assert_called_once_with(BUCKET_NAME)46 @mock_s347 @mock.patch.object(S3Hook, "get_bucket_tagging")48 @mock.patch.object(S3Hook, "check_for_bucket")49 def test_execute_if_not_bucket_exist(self, mock_check_for_bucket, get_bucket_tagging):50 mock_check_for_bucket.return_value = False51 # execute s3 get bucket tagging operator52 self.get_bucket_tagging_operator.execute({})53 mock_check_for_bucket.assert_called_once_with(BUCKET_NAME)54 get_bucket_tagging.assert_not_called()55class TestS3PutBucketTaggingOperator(unittest.TestCase):56 def setUp(self):57 self.put_bucket_tagging_operator = S3PutBucketTaggingOperator(58 task_id=TASK_ID,59 tag_set=TAG_SET,60 bucket_name=BUCKET_NAME,61 )62 @mock_s363 @mock.patch.object(S3Hook, "put_bucket_tagging")64 @mock.patch.object(S3Hook, "check_for_bucket")65 def test_execute_if_bucket_exist(self, mock_check_for_bucket, put_bucket_tagging):66 mock_check_for_bucket.return_value = True67 # execute s3 put bucket tagging operator68 self.put_bucket_tagging_operator.execute({})69 mock_check_for_bucket.assert_called_once_with(BUCKET_NAME)70 put_bucket_tagging.assert_called_once_with(71 key=None, value=None, tag_set=TAG_SET, bucket_name=BUCKET_NAME72 )73 @mock_s374 @mock.patch.object(S3Hook, "put_bucket_tagging")75 @mock.patch.object(S3Hook, "check_for_bucket")76 def test_execute_if_not_bucket_exist(self, mock_check_for_bucket, put_bucket_tagging):77 mock_check_for_bucket.return_value = False78 # execute s3 put bucket tagging operator79 self.put_bucket_tagging_operator.execute({})80 mock_check_for_bucket.assert_called_once_with(BUCKET_NAME)81 put_bucket_tagging.assert_not_called()82class TestS3DeleteBucketTaggingOperator(unittest.TestCase):83 def setUp(self):84 self.delete_bucket_tagging_operator = S3DeleteBucketTaggingOperator(85 task_id=TASK_ID,86 bucket_name=BUCKET_NAME,87 )88 @mock_s389 @mock.patch.object(S3Hook, "delete_bucket_tagging")90 @mock.patch.object(S3Hook, "check_for_bucket")91 def test_execute_if_bucket_exist(self, mock_check_for_bucket, delete_bucket_tagging):92 mock_check_for_bucket.return_value = True93 # execute s3 delete bucket tagging operator94 self.delete_bucket_tagging_operator.execute({})95 mock_check_for_bucket.assert_called_once_with(BUCKET_NAME)96 delete_bucket_tagging.assert_called_once_with(BUCKET_NAME)97 @mock_s398 @mock.patch.object(S3Hook, "delete_bucket_tagging")99 @mock.patch.object(S3Hook, "check_for_bucket")100 def test_execute_if_not_bucket_exist(self, mock_check_for_bucket, delete_bucket_tagging):101 mock_check_for_bucket.return_value = False102 # execute s3 delete bucket tagging operator103 self.delete_bucket_tagging_operator.execute({})104 mock_check_for_bucket.assert_called_once_with(BUCKET_NAME)...
lambda_s3_tagging.py
Source:lambda_s3_tagging.py
...13 except Exception as error:14 print(error)15# ------------------LIST TAGS FOR THE BUCKETS & LIST BUCKET WITHOUT tag:Name16list_buckets = list_buckets()17def get_bucket_tagging(list_buckets):18 try:19 for i in list_buckets:20 response = client.get_bucket_tagging(21 Bucket= i22 )23 tag_keys = {}24 tag_items = tag_keys.items()25 for j in response['TagSet']:26 tag_keys.update(j)27 no_name_bucket = []28 if tag_keys['Key'] != 'Name':29 no_name_bucket.append(i)30 return(no_name_bucket)31 except Exception as error:32 print(error)33# --------------------------------- CREATE/UPDATE TAGS34# get_bucket_tagging = get_bucket_tagging(list_buckets) >>> IF there is no tag:Name35# def put_bucket_tagging(get_bucket_tagging):36def put_bucket_tagging(list_buckets):37 try:38 # for i in get_bucket_tagging: >>> IF there is no tag:Name39 for i in list_buckets:40 response = client.put_bucket_tagging(41 Bucket = i,42 Tagging = {43 'TagSet': [44 {45 'Key': 'Name',46 'Value': i47 },48 {...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!