Revision fa382f9e docs/developers/extending-clients-api.rst

b/docs/developers/extending-clients-api.rst
1 1
Extending kamaki.clients
2 2
========================
3 3

  
4
By default, kamaki clients are REST clients (they manage HTTP requests and responses to communicate with services).
4
By default, kamaki clients are REST clients (they manage HTTP requests and
5
responses to communicate with services).
5 6

  
6 7
How to build a client
7 8
---------------------
8 9

  
9
All service clients consist of a subclass of the Client class and implement separate client functionalities as member methods. There is also an error class to raise exceptions that can be handled by kamaki interfaces.
10
All service clients consist of a subclass of the Client class and implement
11
separate client functionalities as member methods. There is also an error class
12
to raise exceptions that can be handled by kamaki interfaces.
10 13

  
11 14
.. code-block:: python
12 15
    
......
65 68
Concurrency control
66 69
-------------------
67 70

  
68
Kamaki clients may handle multiple requests at once, using threads. In that case, users might implement their own thread handling mechanism, use an external solution or take advantage of the mechanism featured in kamaki.clients
71
Kamaki clients may handle multiple requests at once, using threads. In that
72
case, users might implement their own thread handling mechanism, use an
73
external solution or take advantage of the mechanism featured in kamaki.clients
69 74

  
70 75
.. code-block:: python
71 76

  
......
94 99
Going agile
95 100
-----------
96 101

  
97
The kamaki.clients package contains a set of fine-grained unit-tests for all its packages. 
102
The kamaki.clients package contains a set of fine-grained unit-tests for all
103
its packages. 
98 104

  
99
.. note:: unit tests require the optional python-mock package, version 1.X or better
105
.. note:: unit tests require the optional python-mock package, version 1.X or
106
    better
100 107

  
101 108
Using the tests
102 109
^^^^^^^^^^^^^^^
......
108 115
    $ git clone https://code.grnet.gr/git/kamaki
109 116
    $ cd kamaki/kamaki/clients
110 117

  
111
In each package under kamaki.clients, there is a test module (test.py) where the tests are implemented. To run all tests, run the test.py file from kamaki.clients
118
In each package under kamaki.clients, there is a test module (test.py) where
119
the tests are implemented. To run all tests, run the test.py file from
120
kamaki.clients
112 121

  
113 122
.. code-block:: console
114 123

  
115 124
    $ python test.py
116 125

  
117
To test a specific class, add the class name as an argument. E.g. for the Client class:
126
To test a specific class, add the class name as an argument. E.g. for the
127
Client class:
118 128

  
119 129
.. code-block:: console
120 130

  
121 131
    $ python test.py Client
122 132

  
123
To test a specific method in a class, apply an extra argument, e.g. for the request method in the Client class:
133
To test a specific method in a class, apply an extra argument, e.g. for the
134
request method in the Client class:
124 135

  
125 136
.. code-block:: console
126 137

  
127 138
    $ python test.py Client request
128 139

  
129
Each package contains a test module (test.py) which is also runnable from the command line. E.g. in the pithos package there is a test module which contains, among others, the **download** sub-test:
140
Each package contains a test module (test.py) which is also runnable from the
141
command line. E.g. in the pithos package there is a test module which
142
contains, among others, the **download** sub-test:
130 143

  
131 144
.. code-block:: console
132 145

  
......
141 154
    # Test kamaki.clients.pithos.PithosClient.download
142 155
    $ python test.py Pithos download
143 156

  
144
To fully test a specific package, run test.py from the package location. E.g. to test everything in kamaki.clients.pithos package:
157
To fully test a specific package, run test.py from the package location. E.g.
158
to test everything in kamaki.clients.pithos package:
145 159

  
146 160
.. code-block:: console
147 161

  
......
151 165
Mechanism
152 166
^^^^^^^^^
153 167

  
154
Each folder / package contains a test.py file, that represents the test module of this package. All test modules contain a set of classes that extent the TestCase class. They also contain a main method to run the tests.
168
Each folder / package contains a test.py file, that represents the test module
169
of this package. All test modules contain a set of classes that extent the
170
TestCase class. They also contain a main method to run the tests.
155 171

  
156
By convention, testing classes are named as <Tested Class> where <Test Class> is the name of the tested class or module. Methods not grouped in classes are tested by classes named after their respective module.
172
By convention, testing classes are named as <Tested Class> where <Test Class>
173
is the name of the tested class or module. Methods not grouped in classes are
174
tested by classes named after their respective module.
157 175

  
158
For example, the kamaki.clients.pithos.PithosClient class is tested by the kamaki.clients.pithos.test.PithosClient class, while the methods in kamaki.clients.utils module are tested by the kamaki.clients.utils.test.Utils testing class.
176
For example, the kamaki.clients.pithos.PithosClient class is tested by the
177
kamaki.clients.pithos.test.PithosClient class, while the methods in
178
kamaki.clients.utils module are tested by the kamaki.clients.utils.test.Utils
179
testing class.
159 180

  
160 181
Adding unit tests
161 182
^^^^^^^^^^^^^^^^^
162
After modifying or extending kamaki.clients method, classes, modules or packages, it is a good practice to also modify or extend the corresponding unit tests. What's more, it is recommended to modify or implement the testing of new behavior before implementing the behavior itself. The aim for kamaki.clients package is an 1 to 1 mapping between methods and their tests.
183

  
184
After modifying or extending kamaki.clients method, classes, modules or
185
packages, it is a good practice to also modify or extend the corresponding
186
unit tests. What's more, it is recommended to modify or implement the testing
187
of new behavior before implementing the behavior itself. The aim for
188
kamaki.clients package is an 1 to 1 mapping between methods and their tests.
163 189

  
164 190
Modifying an existing method
165 191
""""""""""""""""""""""""""""
166 192

  
167
In case of an existing method modification, the programmer has to modify the corresponding test as well. By convention, the test method is located in the test module under the same package, in a TestCase subclass that is named with a name similar to the package or class that contains the tested method.
193
In case of an existing method modification, the programmer has to modify the
194
corresponding test as well. By convention, the test method is located in the
195
test module under the same package, in a TestCase subclass that is named with a
196
name similar to the package or class that contains the tested method.
168 197

  
169
Example 1: to modify the kamaki.clients.utils.filter_in method, the programmer has to also adjust the kamaki.clients.utils.test.Utils.test_filter_in method.
198
Example 1: to modify the kamaki.clients.utils.filter_in method, the programmer
199
has to also adjust the kamaki.clients.utils.test.Utils.test_filter_in method.
170 200

  
171
Example 2: to modify the kamaki.clients.pithos.PithosRestClient.object_get, the programmer has to also adjust the kamaki.clients.pithos.test.PithosRestClient.test_object_get method.
201
Example 2: to modify the kamaki.clients.pithos.PithosRestClient.object_get, the
202
programmer has to also adjust the
203
kamaki.clients.pithos.test.PithosRestClient.test_object_get method.
172 204

  
173 205
Adding a new method
174 206
"""""""""""""""""""
175 207

  
176
Programmers who want to implement a new method in an existing class, are encouraged to implement the corresponding unit test first. In order to do that, they should find the testing class that is mapped to the class or module they need to extend.
208
Programmers who want to implement a new method in an existing class, are
209
encouraged to implement the corresponding unit test first. In order to do that,
210
they should find the testing class that is mapped to the class or module they
211
need to extend.
177 212

  
178
Example 1: To add a **list_special** method to kamaki.clients.astakos.AstakosClient, extend the kamaki.clients.astakos.test.AstakosClient class, as shown bellow:
213
Example 1: To add a **list_special** method to
214
kamaki.clients.astakos.AstakosClient, extend the
215
kamaki.clients.astakos.test.AstakosClient class, as shown bellow:
179 216

  
180 217
.. code-block:: python
181 218

  
......
187 224
            """Test the list_special method"""
188 225
            ...
189 226

  
190
Example 2: To add a **get_random_int** method in kamaki.clients.utils module, extend the kamaki.clients.utils.test.Utils test class, as shown bellow:
227
Example 2: To add a **get_random_int** method in kamaki.clients.utils module,
228
extend the kamaki.clients.utils.test.Utils test class, as shown bellow:
191 229

  
192 230
.. code-block:: python
193 231

  
......
202 240
Implementing a new class or module
203 241
""""""""""""""""""""""""""""""""""
204 242

  
205
Each class or module needs a seperate test sub-module. By convention, each class or module under the kamaki.clients should be located in a separate directory.
243
Each class or module needs a seperate test sub-module. By convention, each
244
class or module under the kamaki.clients should be located in a separate
245
directory.
206 246

  
207
Example 1: To add a NewService class that implements the kamaki.clients.Client interface: 
247
Example 1: To add a NewService class that implements the kamaki.clients.Client
248
interface: 
208 249

  
209 250
* create a new_service package and implement the unit tests in the kamaki.clients.new_service.test module:
210 251

  
......
250 291

  
251 292
    from kamaki.clients.new_service.test import NewService
252 293

  
253
.. note:: If the new class or module is part of an existing sub-package, it is acceptable to append its testing class in the existing test.py file of the sub-package it belongs to. For example, the kamaki.clients.pithos.PithosClient and kamaki.clients.pithos.rest_api.PithosRestClient classes are tested by two different classes (PithosClient and PithosRestClient respectively) in the same module (kamaki.clients.pithos.test).
294
.. note:: If the new class or module is part of an existing sub-package, it is
295
    acceptable to append its testing class in the existing test.py file of the
296
    sub-package it belongs to. For example, the
297
    kamaki.clients.pithos.PithosClient and
298
    kamaki.clients.pithos.rest_api.PithosRestClient classes are tested by two
299
    different classes (PithosClient and PithosRestClient respectively) in the
300
    same module (kamaki.clients.pithos.test).
254 301

  

Also available in: Unified diff