File: test_layer1.py

package info (click to toggle)
python-boto 2.34.0-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 8,584 kB
  • ctags: 10,521
  • sloc: python: 78,553; makefile: 123
file content (122 lines) | stat: -rw-r--r-- 5,445 bytes parent folder | download | duplicates (14)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env python
# Copyright (c) 2012 Amazon.com, Inc. or its affiliates.  All Rights Reserved
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish, dis-
# tribute, sublicense, and/or sell copies of the Software, and to permit
# persons to whom the Software is furnished to do so, subject to the fol-
# lowing conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
#
import time
from tests.unit import unittest

from boto.datapipeline import layer1


class TestDataPipeline(unittest.TestCase):
    datapipeline = True

    def setUp(self):
        self.connection = layer1.DataPipelineConnection()
        self.sample_pipeline_objects = [
            {'fields': [
                {'key': 'workerGroup', 'stringValue': 'MyworkerGroup'}],
             'id': 'Default',
             'name': 'Default'},
            {'fields': [
                {'key': 'startDateTime', 'stringValue': '2012-09-25T17:00:00'},
                {'key': 'type', 'stringValue': 'Schedule'},
                {'key': 'period', 'stringValue': '1 hour'},
                {'key': 'endDateTime', 'stringValue': '2012-09-25T18:00:00'}],
             'id': 'Schedule',
             'name': 'Schedule'},
            {'fields': [
                {'key': 'type', 'stringValue': 'ShellCommandActivity'},
                {'key': 'command', 'stringValue': 'echo hello'},
                {'key': 'parent', 'refValue': 'Default'},
                {'key': 'schedule', 'refValue': 'Schedule'}],
             'id': 'SayHello',
             'name': 'SayHello'}
        ]
        self.connection.auth_service_name = 'datapipeline'

    def create_pipeline(self, name, unique_id, description=None):
        response = self.connection.create_pipeline(name, unique_id,
                                                   description)
        pipeline_id = response['pipelineId']
        self.addCleanup(self.connection.delete_pipeline, pipeline_id)
        return pipeline_id

    def get_pipeline_state(self, pipeline_id):
        response = self.connection.describe_pipelines([pipeline_id])
        for attr in response['pipelineDescriptionList'][0]['fields']:
            if attr['key'] == '@pipelineState':
                return attr['stringValue']

    def test_can_create_and_delete_a_pipeline(self):
        response = self.connection.create_pipeline('name', 'unique_id',
                                                   'description')
        self.connection.delete_pipeline(response['pipelineId'])

    def test_validate_pipeline(self):
        pipeline_id = self.create_pipeline('name2', 'unique_id2')

        self.connection.validate_pipeline_definition(
            self.sample_pipeline_objects, pipeline_id)

    def test_put_pipeline_definition(self):
        pipeline_id = self.create_pipeline('name3', 'unique_id3')
        self.connection.put_pipeline_definition(self.sample_pipeline_objects,
                                                pipeline_id)

        # We should now be able to get the pipeline definition and see
        # that it matches what we put.
        response = self.connection.get_pipeline_definition(pipeline_id)
        objects = response['pipelineObjects']
        self.assertEqual(len(objects), 3)
        self.assertEqual(objects[0]['id'], 'Default')
        self.assertEqual(objects[0]['name'], 'Default')
        self.assertEqual(objects[0]['fields'],
                         [{'key': 'workerGroup', 'stringValue': 'MyworkerGroup'}])

    def test_activate_pipeline(self):
        pipeline_id = self.create_pipeline('name4', 'unique_id4')
        self.connection.put_pipeline_definition(self.sample_pipeline_objects,
                                                pipeline_id)
        self.connection.activate_pipeline(pipeline_id)

        attempts = 0
        state = self.get_pipeline_state(pipeline_id)
        while state != 'SCHEDULED' and attempts < 10:
            time.sleep(10)
            attempts += 1
            state = self.get_pipeline_state(pipeline_id)
            if attempts > 10:
                self.fail("Pipeline did not become scheduled "
                          "after 10 attempts.")
        objects = self.connection.describe_objects(['Default'], pipeline_id)
        field = objects['pipelineObjects'][0]['fields'][0]
        self.assertDictEqual(field, {'stringValue': 'COMPONENT', 'key': '@sphere'})

    def test_list_pipelines(self):
        pipeline_id = self.create_pipeline('name5', 'unique_id5')
        pipeline_id_list = [p['id'] for p in
                            self.connection.list_pipelines()['pipelineIdList']]
        self.assertTrue(pipeline_id in pipeline_id_list)


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