Completed
Push — master ( 6f2f3a...d6c94d )
by Tomaz
22s
created

GetBucketTestCase.setUp()   A

Complexity

Conditions 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
1
# Licensed to the StackStorm, Inc ('StackStorm') under one or more
2
# contributor license agreements.  See the NOTICE file distributed with
3
# this work for additional information regarding copyright ownership.
4
# The ASF licenses this file to You under the Apache License, Version 2.0
5
# (the "License"); you may not use this file except in compliance with
6
# the License.  You may obtain a copy of the License at
7
#
8
#     http://www.apache.org/licenses/LICENSE-2.0
9
#
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS,
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
# See the License for the specific language governing permissions and
14
15
import mock
16
17
from aws_base_action_test_case import AWSBaseActionTestCase
18
19
from boto.s3.connection import S3Connection
20
from boto.s3.bucket import Bucket
21
22
from run import ActionManager
23
from datetime import datetime
24
25
26
class MockBucket(Bucket):
27
    def __init__(self, **kwargs):
28
        super(MockBucket, self).__init__(**kwargs)
29
30
        self.creation_date = datetime.now().isoformat()
31
32
33
class GetBucketTestCase(AWSBaseActionTestCase):
34
    __test__ = True
35
    action_cls = ActionManager
36
37
    _MOCK_BUCKETS = [
38
        MockBucket(name='foo'),
39
        MockBucket(name='bar'),
40
    ]
41
42
    def setUp(self):
43
        super(GetBucketTestCase, self).setUp()
44
45
        # default params of each actions
46
        self._params = {
47
            'headers': None,
48
            'module_path': 'boto.s3.connection',
49
            'cls': 'S3Connection',
50
        }
51
52
    @mock.patch.object(S3Connection, 'get_all_buckets',
53
                       mock.MagicMock(return_value=_MOCK_BUCKETS))
54
    def test_get_all_buckets(self):
55
        self._params['action'] = 'get_all_buckets'
56
57
        action = self.get_action_instance(self.full_config)
58
        result = action.run(**self._params)
59
60
        self.assertIsNotNone(result)
61
        self.assertTrue(isinstance(result, list))
62
        self.assertEqual(len(result), len(self._MOCK_BUCKETS))
63
64
    @mock.patch.object(S3Connection, 'get_bucket',
65
                       mock.MagicMock(return_value=MockBucket(name='foo')))
66
    def test_get_bucket(self):
67
        self._params['action'] = 'get_bucket'
68
        self._params['validate'] = True
69
70
        action = self.get_action_instance(self.full_config)
71
        result = action.run(**self._params)
72
73
        self.assertIsNotNone(result)
74
        self.assertEqual(len(result), 1)
75
        self.assertTrue(isinstance(result[0], dict))
76
        self.assertEqual(result[0]['name'], 'foo')
77