| 1 |  |  | # Licensed to the StackStorm, Inc ('StackStorm') under one or more | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | # contributor license agreements.  See the NOTICE file distributed with | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | # this work for additional information regarding copyright ownership. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | # The ASF licenses this file to You under the Apache License, Version 2.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | # (the "License"); you may not use this file except in compliance with | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | # the License.  You may obtain 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, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | # See the License for the specific language governing permissions and | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | # limitations under the License. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | Mock classes for use in pack testing. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | from st2reactor.container.sensor_wrapper import SensorService | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | from st2client.models.keyvalue import KeyValuePair | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | __all__ = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     'MockSensorWrapper', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     'MockSensorService' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | class MockSensorWrapper(object): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     def __init__(self, pack, class_name): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         self._pack = pack | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         self._class_name = class_name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | class MockSensorService(SensorService): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     Mock SensorService for use in testing. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     def __init__(self, sensor_wrapper): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         self._sensor_wrapper = sensor_wrapper | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         # Holds mock KeyValuePair objects | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         # Key is a KeyValuePair name and value is the KeyValuePair object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         self._datastore_items = {} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         # Holds a list of triggers which were dispatched | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         self.dispatched_triggers = [] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     def get_logger(self, name): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         return None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     def dispatch_with_context(self, trigger, payload=None, trace_context=None): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         item = { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             'trigger': trigger, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             'payload': payload, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             'trace_context': trace_context | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         self.dispatched_triggers.append(item) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     def list_values(self, local=True, prefix=None): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         key_prefix = self._get_full_key_prefix(local=local, prefix=prefix) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         if not key_prefix: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             return self._datastore_items.values() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         result = [] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         for name, kvp in self._datastore_items.items(): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             if name.startswith(key_prefix): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |                 result.append(kvp) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         return result | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     def get_value(self, name, local=True): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         name = self._get_full_key_name(name=name, local=local) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         if name not in self._datastore_items: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             return None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         kvp = self._datastore_items[name] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         return kvp.value | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |     def set_value(self, name, value, ttl=None, local=True): | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |         if ttl: | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |             raise ValueError('MockSensorService.set_value doesn\'t support "ttl" argument') | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |         name = self._get_full_key_name(name=name, local=local) | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |         instance = KeyValuePair() | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |         instance.id = name | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |         instance.name = name | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |         instance.value = value | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |         self._datastore_items[name] = instance | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |         return True | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     def delete_value(self, name, local=True): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         name = self._get_full_key_name(name=name, local=local) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         if name not in self._datastore_items: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             return False | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         del self._datastore_items[name] | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 104 |  |  |         return True | 
            
                                                        
            
                                    
            
            
                | 105 |  |  |  |