1+ #!/usr/bin/env python
2+ # encoding: utf-8
3+
4+ from base64 import b64encode
5+
6+ import mock
7+ import json
8+
9+ from ec2stack .helpers import read_file , generate_signature
10+ from . import Ec2StackAppTestCase
11+
12+
13+ class SnapshotTestCase (Ec2StackAppTestCase ):
14+
15+ def test_create_snapshot (self ):
16+ data = self .get_example_data ()
17+ data ['Action' ] = 'CreateSnapshot'
18+ data ['VolumeId' ] = 'daa492b4-bd09-46b0-a4ad-142e187ecdbe'
19+ data ['Signature' ] = generate_signature (data , 'POST' , 'localhost' , '/' )
20+
21+ get = mock .Mock ()
22+ get .return_value .text = read_file (
23+ 'tests/data/valid_create_snapshot.json'
24+ )
25+ get .return_value .status_code = 200
26+
27+ with mock .patch ('requests.get' , get ):
28+ response = self .post (
29+ '/' ,
30+ data = data
31+ )
32+
33+ self .assert_ok (response )
34+ assert 'CreateSnapshotResponse' in response .data
35+
36+ def test_describe_snapshots (self ):
37+ data = self .get_example_data ()
38+ data ['Action' ] = 'DescribeSnapshots'
39+ data ['Signature' ] = generate_signature (data , 'POST' , 'localhost' , '/' )
40+
41+ get = mock .Mock ()
42+ get .return_value .text = read_file (
43+ 'tests/data/valid_describe_snapshots.json'
44+ )
45+ get .return_value .status_code = 200
46+
47+ with mock .patch ('requests.get' , get ):
48+ response = self .post (
49+ '/' ,
50+ data = data
51+ )
52+
53+ self .assert_ok (response )
54+ assert 'DescribeSnapshotsResponse' in response .data
55+
56+ def test_describe_snapshot_by_name (self ):
57+ data = self .get_example_data ()
58+ data ['Action' ] = 'DescribeVpcs'
59+ data ['SnapshotId' ] = 'examplesnapshot'
60+ data ['Signature' ] = generate_signature (data , 'POST' , 'localhost' , '/' )
61+
62+ get = mock .Mock ()
63+ get .return_value .text = read_file (
64+ 'tests/data/valid_describe_snapshot.json'
65+ )
66+ get .return_value .status_code = 200
67+
68+ with mock .patch ('requests.get' , get ):
69+ response = self .post (
70+ '/' ,
71+ data = data
72+ )
73+
74+ self .assert_ok (response )
75+ assert 'DescribeSnapshotsResponse' in response .data
76+ assert 'examplesnapshot' in response .data
77+
78+ def test_describe_snapshot_by_name_invalid_name (self ):
79+ data = self .get_example_data ()
80+ data ['Action' ] = 'DescribeVpcs'
81+ data ['SnapshotId' ] = 'invalidsnapshot'
82+ data ['Signature' ] = generate_signature (data , 'POST' , 'localhost' , '/' )
83+
84+ get = mock .Mock ()
85+ get .return_value .text = read_file (
86+ 'tests/data/valid_describe_snapshot.json'
87+ )
88+ get .return_value .status_code = 200
89+
90+ with mock .patch ('requests.get' , get ):
91+ response = self .post (
92+ '/' ,
93+ data = data
94+ )
95+
96+ self .assert_bad_request (response )
97+ assert 'InvalidSnapshotID.NotFound' in response .data
98+
99+ def test_delete_snapshot (self ):
100+ data = self .get_example_data ()
101+ data ['Action' ] = 'DeleteSnapshot'
102+ data ['SnapshotId' ] = 'Test'
103+ data ['Signature' ] = generate_signature (data , 'POST' , 'localhost' , '/' )
104+
105+ get = mock .Mock ()
106+ get .return_value .text = read_file (
107+ 'tests/data/valid_delete_snapshot.json'
108+ )
109+ get .return_value .status_code = 200
110+
111+ with mock .patch ('requests.get' , get ):
112+ response = self .post (
113+ '/' ,
114+ data = data
115+ )
116+
117+ self .assert_ok (response )
118+ assert 'DeleteSnapshotResponse' in response .data
0 commit comments