cc1  v2.1
CC1 source code docs
 All Classes Namespaces Files Functions Variables Pages
key_pair_test.py
Go to the documentation of this file.
1 # -*- coding: utf-8 -*-
2 # @COPYRIGHT_begin
3 #
4 # Copyright [2010-2014] Institute of Nuclear Physics PAN, Krakow, Poland
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18 # @COPYRIGHT_end
19 
20 ##
21 # @package src.ec2.key_pair_test
22 #
23 # @copyright Copyright (c) 2012 Institute of Nuclear Physics PAS <http://www.ifj.edu.pl/>
24 # @author Oleksandr Gituliar <gituliar@gmail.com>
25 #
26 
27 from __future__ import with_statement
28 
29 import mock
30 
31 from ec2.base.action import Action, CLMException
32 from ec2.base.test import TestCase
33 from ec2.error import InvalidKeyPair, MissingParameter
34 
35 
36 class KeyPairTestCase(TestCase):
37 
38  def test_CreateKeyPair(self):
39  self.cluster_manager.key.user.gen.return_value = 'key data'
40  self.cluster_manager.key.user.get.return_value = {
41  'fingerprint': 'fingerprint data',
42  }
43 
44  # 1. Test success.
45  response = Action({
46  'Action': 'CreateKeyPair',
47  'KeyName': 'test key pair',
48  }, self.cluster_manager).execute()
49 
50  self.assertMultiLineEqual(
51  response,
52  """<?xml version="1.0" encoding="UTF-8"?>
53  <CreateKeyPairResponse xmlns="http://ec2.amazonaws.com/doc/2012-04-01/">
54  <requestId>59dbff89-35bd-4eac-99ed-be587EXAMPLE</requestId>
55  <keyName>test key pair</keyName>
56  <keyFingerprint>fingerprint data</keyFingerprint>
57  <keyMaterial>key data</keyMaterial>
58  </CreateKeyPairResponse>
59  """
60  )
61 
62  # 2. Test `MissingParameter`.
63  with self.assertRaises(MissingParameter):
64  response = Action({
65  'Action': 'CreateKeyPair',
66  }, self.cluster_manager).execute()
67 
68  self.cluster_manager.key.user.gen.return_value = None
69 
70  # 3. Test `InvalidKeyPair.Duplicate` exception.
71  self.cluster_manager.key.user.gen.side_effect = \
72  CLMException('ssh_key_already_exist', 'key.user.gen')
73  with self.assertRaises(InvalidKeyPair.Duplicate):
74  response = Action({
75  'Action': 'CreateKeyPair',
76  'KeyName': 'test key pair',
77  }, self.cluster_manager).execute()
78 
79  def test_DeleteKeyPair(self):
80  self.cluster_manager.key.user.delete.return_value = None
81 
82  # 1. Test success.
83  response = Action({
84  'Action': 'DeleteKeyPair',
85  'KeyName': 'test key pair',
86  }, self.cluster_manager).execute()
87 
88  self.assertMultiLineEqual(
89  response,
90  """<?xml version="1.0" encoding="UTF-8"?>
91  <DeleteKeyPairResponse xmlns="http://ec2.amazonaws.com/doc/2012-04-01/">
92  <requestId>59dbff89-35bd-4eac-99ed-be587EXAMPLE</requestId>
93  <return>true</return>
94  </DeleteKeyPairResponse>
95  """
96  )
97 
98  # 2. Test `MissingParameter`.
99  with self.assertRaises(MissingParameter):
100  response = Action({
101  'Action': 'DeleteKeyPair',
102  }, self.cluster_manager).execute()
103 
105  self.cluster_manager.key.user.list.return_value = [
106  {'name': 'key01', 'fingerprint': 'fp01'},
107  {'name': 'key02', 'fingerprint': 'fp02'},
108  ]
109 
110  # 1. Test success.
111  response = Action({
112  'Action': 'DescribeKeyPairs',
113  }, self.cluster_manager).execute()
114 
115  self.assertMultiLineEqual(
116  response,
117  """<?xml version="1.0" encoding="UTF-8"?>
118  <DescribeKeyPairsResponse xmlns="http://ec2.amazonaws.com/doc/2012-04-01/">
119  <requestId>59dbff89-35bd-4eac-99ed-be587EXAMPLE</requestId>
120  <keySet>
121  <item>
122  <keyName>key01</keyName>
123  <keyFingerprint>fp01</keyFingerprint>
124  </item>
125  <item>
126  <keyName>key02</keyName>
127  <keyFingerprint>fp02</keyFingerprint>
128  </item>
129  </keySet>
130  </DescribeKeyPairsResponse>
131  """
132  )
133 
135  self.cluster_manager.key.user.add.return_value = None
136  self.cluster_manager.key.user.get.return_value = {
137  'fingerprint': 'test fingerprint',
138  }
139 
140  # 1. Test success.
141  response = Action({
142  'Action': 'ImportKeyPair',
143  'KeyName': 'test key name',
144  'PublicKeyMaterial': 'test key material',
145  }, self.cluster_manager).execute()
146 
147  self.cluster_manager.key.user.add.assert_called_once_with({
148  'name': 'test key name',
149  'key': 'test key material'
150  })
151 
152  self.assertMultiLineEqual(
153  response,
154  """<?xml version="1.0" encoding="UTF-8"?>
155  <ImportKeyPairResponse xmlns="http://ec2.amazonaws.com/doc/2012-04-01/">
156  <requestId>7a62c49f-347e-4fc4-9331-6e8eEXAMPLE</requestId>
157  <keyName>test key name</keyName>
158  <keyFingerprint>test fingerprint</keyFingerprint>
159  </ImportKeyPairResponse>
160  """
161  )
162 
163  # 2. Test `MissingParameter`.
164  with self.assertRaises(MissingParameter):
165  response = Action({
166  'Action': 'ImportKeyPair',
167  }, self.cluster_manager).execute()
168 
169  with self.assertRaises(MissingParameter):
170  response = Action({
171  'Action': 'ImportKeyPair',
172  'KeyName': 'test key name'
173  }, self.cluster_manager).execute()
174 
175  with self.assertRaises(MissingParameter):
176  response = Action({
177  'Action': 'ImportKeyPair',
178  'PublicKeyMaterial': 'test key material',
179  }, self.cluster_manager).execute()
180