~termie/nova/clean_ec2

« back to all changes in this revision

Viewing changes to nova/tests/clean_unittest.py

  • Committer: Andy Smith
  • Date: 2010-12-07 23:53:11 UTC
  • Revision ID: code@term.ie-20101207235311-tbbzacpaqblnig0u
Basic input cleaning infrastructure

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
 
2
 
 
3
# Copyright 2010 United States Government as represented by the
 
4
# Administrator of the National Aeronautics and Space Administration.
 
5
# All Rights Reserved.
 
6
#
 
7
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
 
8
#    not use this file except in compliance with the License. You may obtain
 
9
#    a copy of the License at
 
10
#
 
11
#         http://www.apache.org/licenses/LICENSE-2.0
 
12
#
 
13
#    Unless required by applicable law or agreed to in writing, software
 
14
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 
15
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 
16
#    License for the specific language governing permissions and limitations
 
17
#    under the License.
 
18
 
 
19
from nova import clean
 
20
from nova import exception
 
21
from nova import test
 
22
 
 
23
 
 
24
def lower(s):
 
25
    return s.lower()
 
26
 
 
27
class CleanTestCase(test.TrialTestCase):
 
28
    def assertAllPass(self, func, values):
 
29
        for v in values:
 
30
            self.assertEqual(func(v), v)
 
31
 
 
32
    def assertAllFail(self, func, values):
 
33
        for v in values:
 
34
            self.assertRaises(AssertionError, func, v)
 
35
 
 
36
    def test_cleaner(self):
 
37
        cleaner = clean.Cleaner(lowerkey=lower)
 
38
 
 
39
        out = cleaner({'lowerkey': 'DUDE',
 
40
                       'ignorekey': 'FOO'})
 
41
        self.assertEqual(out['lowerkey'], 'dude')
 
42
        self.assertEqual(out['ignorekey'], 'FOO')
 
43
 
 
44
    def test_cleaner_error(self):
 
45
        cleaner = clean.Cleaner(errorkey=self.assert_)
 
46
        
 
47
        self.assertRaises(exception.ApiError, cleaner, {'errorkey': False})
 
48
 
 
49
    def test_key_pair_name(self):
 
50
        good = ['asdf', 'FOO', '1234', 'a_d2- ']
 
51
        bad = ['a*d', 'a' * 256, '[][]]']
 
52
 
 
53
        self.assertAllPass(clean.key_pair_name, good)
 
54
        self.assertAllFail(clean.key_pair_name, bad)
 
55