Marc Koderer | 6ee82dc | 2014-02-17 10:26:29 +0100 | [diff] [blame] | 1 | # Copyright 2014 Deutsche Telekom AG |
| 2 | # All Rights Reserved. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 | # not use this file except in compliance with the License. You may obtain |
| 6 | # a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | # License for the specific language governing permissions and limitations |
| 14 | # under the License. |
| 15 | |
| 16 | import tempest.common.generator.base_generator as base |
| 17 | from tempest.openstack.common import log as logging |
| 18 | |
| 19 | |
| 20 | LOG = logging.getLogger(__name__) |
| 21 | |
| 22 | |
| 23 | class ValidTestGenerator(base.BasicGeneratorSet): |
| 24 | @base.generator_type("string") |
| 25 | @base.simple_generator |
| 26 | def generate_valid_string(self, schema): |
Marc Koderer | 4ee4190 | 2014-08-19 13:29:30 +0200 | [diff] [blame] | 27 | size = schema.get("minLength", 1) |
Marc Koderer | 6ee82dc | 2014-02-17 10:26:29 +0100 | [diff] [blame] | 28 | # TODO(dkr mko): handle format and pattern |
| 29 | return "x" * size |
| 30 | |
| 31 | @base.generator_type("integer") |
| 32 | @base.simple_generator |
| 33 | def generate_valid_integer(self, schema): |
| 34 | # TODO(dkr mko): handle multipleOf |
| 35 | if "minimum" in schema: |
| 36 | minimum = schema["minimum"] |
| 37 | if "exclusiveMinimum" not in schema: |
| 38 | return minimum |
| 39 | else: |
| 40 | return minimum + 1 |
| 41 | if "maximum" in schema: |
| 42 | maximum = schema["maximum"] |
| 43 | if "exclusiveMaximum" not in schema: |
| 44 | return maximum |
| 45 | else: |
| 46 | return maximum - 1 |
| 47 | return 0 |
| 48 | |
| 49 | @base.generator_type("object") |
| 50 | @base.simple_generator |
| 51 | def generate_valid_object(self, schema): |
| 52 | obj = {} |
| 53 | for k, v in schema["properties"].iteritems(): |
| 54 | obj[k] = self.generate_valid(v) |
| 55 | return obj |
| 56 | |
Marc Koderer | f07f5d1 | 2014-09-01 09:47:23 +0200 | [diff] [blame] | 57 | def generate(self, schema): |
| 58 | schema_type = schema["type"] |
| 59 | if isinstance(schema_type, list): |
| 60 | if "integer" in schema_type: |
| 61 | schema_type = "integer" |
| 62 | else: |
| 63 | raise Exception("non-integer list types not supported") |
| 64 | result = [] |
| 65 | if schema_type not in self.types_dict: |
| 66 | raise TypeError("generator (%s) doesn't support type: %s" |
| 67 | % (self.__class__.__name__, schema_type)) |
| 68 | for generator in self.types_dict[schema_type]: |
| 69 | ret = generator(schema) |
| 70 | if ret is not None: |
| 71 | if isinstance(ret, list): |
| 72 | result.extend(ret) |
| 73 | elif isinstance(ret, tuple): |
| 74 | result.append(ret) |
| 75 | else: |
| 76 | raise Exception("generator (%s) returns invalid result: %s" |
| 77 | % (generator, ret)) |
| 78 | return result |
| 79 | |
Marc Koderer | 6ee82dc | 2014-02-17 10:26:29 +0100 | [diff] [blame] | 80 | def generate_valid(self, schema): |
| 81 | return self.generate(schema)[0][1] |