def test_initialize_state_with_multiple_triggers_compound_no_match(self, logger_filter): """ NEXT_COMMAND trigger loads multiple triggers skips a non-matching compound """ recore.fsm.TRIGGERS = new_triggers_two_triggers_compound_no_match() db = mock.MagicMock() collection = mock.MagicMock() collection.update = mock.MagicMock(return_value=12345) db.__getitem__.return_value = collection group = 'testgroup' user_id = 'justabro' with nested( mock.patch('recore.mongo.lookup_playbook'), mock.patch('recore.mongo.datetime.datetime') ) as (mongo.lookup_playbook, mongo.datetime.datetime): mongo.lookup_playbook = mock.MagicMock() PLAYBOOK_ID = 1234567 mongo.lookup_playbook.return_value = new_playbook() mongo.datetime.datetime = mock.MagicMock('datetime') mongo.datetime.datetime.utcnow = mock.MagicMock( return_value=UTCNOW) get_field = mock.Mock(return_value=user_id) filter = mock.Mock() filter.get_field = get_field logger_filter.return_value = filter mongo.initialize_state(db, PLAYBOOK_ID, '000000000000000007654321', dynamic={}) db['state'].update.assert_called_once_with( {'_id': ObjectId('000000000000000007654321')}, { '$set': { 'executed': [], 'group': group, 'user_id': user_id, 'failed': False, 'created': UTCNOW, 'dynamic': {}, 'triggers': new_triggers_two_triggers_compound_no_match(), 'active_sequence': { 'steps': [ {'sleep:seconds': {'seconds': 1}}, 'bigip:OutOfRotation', {'misc:Echo': {'input': 'This is a test message'}}, {'frob:Nicate': {'things': 'all the things'}} ], 'completed_steps': [], 'hosts': ['bar.ops.example.com'], 'description': 'frobnicate these lil guys' }, 'ended': None, 'active_step': None, 'reply_to': None, 'execution': [], 'playbook_id': PLAYBOOK_ID }})
def test_initialize_state(self): """ Make sure that creating the initial state uses proper data """ db = mock.MagicMock() collection = mock.MagicMock() collection.insert = mock.MagicMock(return_value=12345) db.__getitem__.return_value = collection group = 'testgroup' with mock.patch('recore.mongo.lookup_playbook') as ( mongo.lookup_playbook): mongo.lookup_playbook = mock.MagicMock() PLAYBOOK_ID = 1234567 mongo.lookup_playbook.return_value = new_playbook() with mock.patch('recore.mongo.datetime.datetime') as ( mongo.datetime.datetime): mongo.datetime.datetime = mock.MagicMock('datetime') mongo.datetime.datetime.utcnow = mock.MagicMock( return_value=UTCNOW) mongo.initialize_state(db, PLAYBOOK_ID, dynamic={}) db['state'].insert.assert_called_once_with({ 'executed': [], 'group': group, 'failed': False, 'created': UTCNOW, 'dynamic': {}, 'active_sequence': { 'steps': [ 'bigip:OutOfRotation', {'misc:Echo': {'input': 'This is a test message'}}, {'frob:Nicate': {'things': 'all the things'}} ], 'completed_steps': [], 'hosts': ['bar.ops.example.com'], 'description': 'frobnicate these lil guys' }, 'ended': None, 'active_step': None, 'reply_to': None, 'execution': [], 'playbook_id': PLAYBOOK_ID })
def test_initialize_state(self, logger_filter): """ Make sure that creating the initial state uses proper data """ db = mock.MagicMock() collection = mock.MagicMock() collection.update = mock.MagicMock(return_value=12345) db.__getitem__.return_value = collection group = 'testgroup' user_id = 'justabro' with mock.patch('recore.mongo.lookup_playbook') as ( mongo.lookup_playbook): mongo.lookup_playbook = mock.MagicMock() PLAYBOOK_ID = 1234567 mongo.lookup_playbook.return_value = new_playbook() with mock.patch('recore.mongo.datetime.datetime') as ( mongo.datetime.datetime): mongo.datetime.datetime = mock.MagicMock('datetime') mongo.datetime.datetime.utcnow = mock.MagicMock( return_value=UTCNOW) # initialize state adds the calling user's ID to the # state record. It pulls this from the contextfilter # attached to the current logger. We need to slip some # values in there while nobody is watching get_field = mock.Mock(return_value=user_id) filter = mock.Mock() filter.get_field = get_field logger_filter.return_value = filter mongo.initialize_state(db, PLAYBOOK_ID, '000000000000000007654321', dynamic={}) db['state'].update.assert_called_once_with( { '_id': ObjectId('000000000000000007654321') }, { '$set': { 'executed': [], 'group': group, 'user_id': user_id, 'failed': False, 'created': UTCNOW, 'dynamic': {}, 'triggers': [], 'active_sequence': { 'steps': [ 'bigip:OutOfRotation', {'misc:Echo': {'input': 'This is a test message'}}, {'frob:Nicate': {'things': 'all the things'}} ], 'completed_steps': [], 'hosts': ['bar.ops.example.com'], 'description': 'frobnicate these lil guys' }, 'ended': None, 'active_step': None, 'reply_to': None, 'execution': [], 'playbook_id': PLAYBOOK_ID } })