def test__get_revision_dict_errs(): """ tests getting a revision dictionary method when the deckhand client has raised an exception """ def _raise_dre(): raise DeckhandResponseError(status_code=9000, response_message='This is bogus') def _raise_nree(): raise NoRevisionsExistError() helper = ConfigdocsHelper(CTX) helper.deckhand.get_revision_list = _raise_dre with pytest.raises(AppError): helper._get_revision_dict() helper.deckhand.get_revision_list = _raise_nree rev_dict = helper._get_revision_dict() committed = rev_dict.get(configdocs_helper.COMMITTED) buffer = rev_dict.get(configdocs_helper.BUFFER) latest = rev_dict.get(configdocs_helper.LATEST) count = rev_dict.get(configdocs_helper.REVISION_COUNT) assert committed is None assert buffer is None assert latest is None assert count == 0
def test_get_collection_docs(): """ Returns the representation of the yaml docs from deckhand """ helper = ConfigdocsHelper(CTX) helper.deckhand.get_docs_from_revision = ( lambda revision_id, bucket_id: "{'yaml': 'yaml'}") helper._get_revision_dict = lambda: REV_EMPTY_DICT helper.deckhand.get_diff = ( lambda old_revision_id, new_revision_id: DIFF_EMPTY_DICT) with pytest.raises(ApiError): helper.get_collection_docs(configdocs_helper.BUFFER, 'mop') with pytest.raises(ApiError): helper.get_collection_docs(configdocs_helper.COMMITTED, 'mop') helper._get_revision_dict = lambda: REV_COMMIT_AND_BUFFER_DICT helper.deckhand.get_diff = ( lambda old_revision_id, new_revision_id: DIFF_COMMIT_AND_BUFFER_DICT) yaml_str = helper.get_collection_docs(configdocs_helper.BUFFER, 'mop') assert len(yaml_str) == 16 yaml_str = helper.get_collection_docs(configdocs_helper.COMMITTED, 'mop') assert len(yaml_str) == 16
def test_get_revision_dict_last_site_action_and_successful_site_action(): """ Tests the processing of revision dict response from deckhand for last_site_action and successful_site_action revision """ helper = ConfigdocsHelper(CTX) helper.deckhand.get_revision_list = lambda: yaml.load(""" --- - id: 1 url: https://deckhand/api/v1.0/revisions/1 createdAt: 2018-04-30T21:23Z buckets: [mop] tags: [committed, site-action-success] validationPolicies: site-deploy-validation: status: succeeded - id: 2 url: https://deckhand/api/v1.0/revisions/2 createdAt: 2018-04-30T23:35Z buckets: [flop, mop] tags: [committed, site-action-failure] validationPolicies: site-deploy-validation: status: succeeded ... """) rev_dict = helper._get_revision_dict() successful_site_action = rev_dict.get( configdocs_helper.SUCCESSFUL_SITE_ACTION) last_site_action = rev_dict.get(configdocs_helper.LAST_SITE_ACTION) assert successful_site_action.get('id') == 1 assert last_site_action.get('id') == 2
def test_is_buffer_emtpy(): """ Test the method to check if the configdocs buffer is empty """ helper = ConfigdocsHelper(CTX) helper._get_revision_dict = lambda: REV_BUFFER_DICT assert not helper.is_buffer_empty() helper._get_revision_dict = lambda: REV_BUFF_EMPTY_DICT assert helper.is_buffer_empty() helper._get_revision_dict = lambda: REV_NO_COMMIT_DICT assert not helper.is_buffer_empty() helper._get_revision_dict = lambda: REV_EMPTY_DICT assert helper.is_buffer_empty()
def test__get_revision_dict_commit_and_buff(): """ Tests the processing of revision dict response from deckhand with a committed and a buffer revision """ helper = ConfigdocsHelper(CTX) helper.deckhand.get_revision_list = lambda: yaml.load(""" --- - id: 1 url: https://deckhand/api/v1.0/revisions/1 createdAt: 2017-07-14T21:23Z buckets: [mop] tags: [a, b, committed] validationPolicies: site-deploy-validation: status: failed - id: 2 url: https://deckhand/api/v1.0/revisions/2 createdAt: 2017-07-16T01:15Z buckets: [flop, mop] tags: [b] validationPolicies: site-deploy-validation: status: succeeded ... """) rev_dict = helper._get_revision_dict() committed = rev_dict.get(configdocs_helper.COMMITTED) buffer = rev_dict.get(configdocs_helper.BUFFER) latest = rev_dict.get(configdocs_helper.LATEST) count = rev_dict.get(configdocs_helper.REVISION_COUNT) assert committed.get('id') == 1 assert buffer.get('id') == 2 assert latest.get('id') == 2 assert count == 2
def test_tag_buffer(): """ Tests that the tag buffer method attempts to tag the right version """ with patch.object(ConfigdocsHelper, 'tag_revision') as mock_method: helper = ConfigdocsHelper(CTX) helper._get_revision_dict = lambda: REV_BUFFER_DICT helper.tag_buffer('artful') mock_method.assert_called_once_with(5, 'artful')
def test_add_collection(): """ Tests the adding of a collection to deckhand - primarily error handling """ with patch.object(DeckhandClient, 'put_bucket') as mock_method: helper = ConfigdocsHelper(CTX) helper._get_revision_dict = lambda: REV_BUFFER_DICT assert helper.add_collection('mop', 'yaml:yaml') == 5 mock_method.assert_called_once_with('mop', 'yaml:yaml')
def test_is_collection_in_buffer(): """ Test that collections are found in the buffer """ helper = ConfigdocsHelper(CTX) helper._get_revision_dict = lambda: REV_BUFFER_DICT helper.deckhand.get_diff = ( lambda old_revision_id, new_revision_id: DIFF_BUFFER_DICT) # mop is not in buffer; chum and slop are in buffer. # unmodified means it is not in buffer assert not helper.is_collection_in_buffer('mop') # created means it is in buffer assert helper.is_collection_in_buffer('slop') # deleted means it is in buffer assert helper.is_collection_in_buffer('chum') def _raise_dre(): raise DeckhandResponseError(status_code=9000, response_message='This is bogus') helper._get_revision_dict = _raise_dre with pytest.raises(DeckhandResponseError): helper.is_collection_in_buffer('does not matter')
def test_is_buffer_empty(): """ Test the method to check if the configdocs buffer is empty """ helper = ConfigdocsHelper(CTX) # BUFFER revision is none, short circuit case (no buffer revision) # buffer is empty. helper._get_revision_dict = lambda: REV_BUFF_EMPTY_DICT assert helper.is_buffer_empty() # BUFFER revision is none, also a short circuit case (no revisions at all) # buffer is empty helper._get_revision_dict = lambda: REV_EMPTY_DICT assert helper.is_buffer_empty() # BUFFER revision is not none, collections have been modified # buffer is NOT empty. helper._get_revision_dict = lambda: REV_BUFFER_DICT helper.deckhand.get_diff = ( lambda old_revision_id, new_revision_id: DIFF_BUFFER_DICT) assert not helper.is_buffer_empty() # BUFFER revision is not none, all collections unmodified # buffer is empty. helper._get_revision_dict = lambda: REV_NO_COMMIT_DICT helper.deckhand.get_diff = ( lambda old_revision_id, new_revision_id: UNMOD_BUFFER_DICT) assert helper.is_buffer_empty() # BUFFER revision is not none, no collections listed (deleted, rollback 0) # buffer is empty. helper._get_revision_dict = lambda: REV_NO_COMMIT_DICT helper.deckhand.get_diff = ( lambda old_revision_id, new_revision_id: EMPTY_BUFFER_DICT) assert helper.is_buffer_empty()
def test__get_revision_dict_empty(): """ Tests the processing of revision dict response from deckhand where the response is an empty list """ helper = ConfigdocsHelper(CTX) helper.deckhand.get_revision_list = lambda: [] rev_dict = helper._get_revision_dict() committed = rev_dict.get(configdocs_helper.COMMITTED) buffer = rev_dict.get(configdocs_helper.BUFFER) latest = rev_dict.get(configdocs_helper.LATEST) count = rev_dict.get(configdocs_helper.REVISION_COUNT) assert committed is None assert buffer is None assert latest is None assert count == 0
def test_get_configdocs_status(): helper = ConfigdocsHelper(CTX) helper._get_revision_dict = lambda: REV_BUFFER_DICT helper._get_ordered_versions = lambda versions: ORDERED_VER helper._get_versions_name_id = lambda ordered_versions: REV_NAME_ID helper.deckhand.get_diff = ( lambda old_revision_id, new_revision_id: DIFF_BUFFER_DICT) result = helper.get_configdocs_status() expected = [{ "collection_name": 'chum', "base_version": 'committed', "base_revision": 3, "new_version": 'buffer', "new_revision": 5, "new_status": 'created', "base_status": 'not present' }, { "collection_name": 'mop', "base_version": 'committed', "base_revision": 3, "new_version": 'buffer', "new_revision": 5, "new_status": 'unmodified', "base_status": 'present' }, { "collection_name": 'slop', "base_version": 'committed', "base_revision": 3, "new_version": 'buffer', "new_revision": 5, "new_status": 'deleted', "base_status": 'present' }] assert expected == sorted(result, key=lambda x: x['collection_name'])
def test_is_buffer_valid_for_bucket(): """ def is_buffer_valid_for_bucket(self, collection_id, buffermode) """ def set_revision_dict(helper, revision_dict, diff_dict): helper._get_revision_dict = lambda: revision_dict helper.deckhand.get_diff = lambda: diff_dict helper = ConfigdocsHelper(CTX) helper._get_revision_dict = lambda: REV_BUFFER_DICT helper.deckhand.get_diff = ( lambda old_revision_id, new_revision_id: DIFF_BUFFER_DICT) helper.deckhand.rollback = lambda target_revision_id: (set_revision_dict( helper, REV_BUFF_EMPTY_DICT, DIFF_BUFF_EMPTY_DICT)) # patch the deckhand client method to set to empty if reset_to_empty # is invoked. helper.deckhand.reset_to_empty = lambda: (set_revision_dict( helper, REV_EMPTY_DICT, DIFF_EMPTY_DICT)) # can add 'mop' on append, it was unmodified in buffer # can add 'water' on append, it was not anywhere before # can't add 'slop' on append, it was deleted (modified in buffer) # can't add 'chum' on append, it is already in buffer # can't add anything on rejectoncontents # can add anything on replace assert helper.is_buffer_valid_for_bucket('mop', BufferMode.APPEND) assert helper.is_buffer_valid_for_bucket('water', BufferMode.APPEND) assert not helper.is_buffer_valid_for_bucket('slop', BufferMode.APPEND) assert not helper.is_buffer_valid_for_bucket('chum', BufferMode.APPEND) assert not helper.is_buffer_valid_for_bucket('new', BufferMode.REJECTONCONTENTS) # because of the patched methods above, replace mode will set the # buffer/diff to values as if it were rolled back. assert helper.is_buffer_valid_for_bucket('mop', BufferMode.REPLACE) # should be able to replace mode even if no buffer contents. assert helper.is_buffer_valid_for_bucket('mop', BufferMode.REPLACE) # in rolled back state as per last two commands, should be ok # to use rejectoncontents to add. assert helper.is_buffer_valid_for_bucket('mop', BufferMode.REJECTONCONTENTS) # set up as if there is no committed revision yet helper._get_revision_dict = lambda: REV_NO_COMMIT_DICT helper.deckhand.get_diff = ( lambda old_revision_id, new_revision_id: DIFF_NO_COMMIT_DICT) assert helper.is_buffer_valid_for_bucket('slop', BufferMode.APPEND) assert helper.is_buffer_valid_for_bucket('chum', BufferMode.APPEND) # buffer is not empty, reject on contents. assert not helper.is_buffer_valid_for_bucket('new', BufferMode.REJECTONCONTENTS) # This should rollback to "nothing" using reset to empty. assert helper.is_buffer_valid_for_bucket('mop', BufferMode.REPLACE) assert helper.is_buffer_empty() # set up as if there is nothing in deckhand. helper._get_revision_dict = lambda: REV_EMPTY_DICT helper.deckhand.get_diff = ( lambda old_revision_id, new_revision_id: DIFF_EMPTY_DICT) # should be able to add in any mode. assert helper.is_buffer_valid_for_bucket('slop', BufferMode.APPEND) assert helper.is_buffer_valid_for_bucket('chum', BufferMode.APPEND) assert helper.is_buffer_valid_for_bucket('new', BufferMode.REJECTONCONTENTS) assert helper.is_buffer_valid_for_bucket('mop', BufferMode.REPLACE)