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 on_post(self, req, resp):
     """
     Get validations from all Airship components
     Functionality does not exist yet
     """
     # force and dryrun query parameter is False unless explicitly true
     force = req.get_param_as_bool(name='force') or False
     dryrun = req.get_param_as_bool(name='dryrun') or False
     helper = ConfigdocsHelper(req.context)
     validations = self.commit_configdocs(helper, force, dryrun)
     resp.body = self.to_json(validations)
     resp.status = validations.get('code', falcon.HTTP_200)
def test__get_deckhand_validations_empty_errors():
    """
    Tets the functionality of processing a response from deckhand
    """
    helper = ConfigdocsHelper(CTX)
    helper.deckhand._get_base_validation_resp = (
        lambda revision_id: FK_VAL_BASE_RESP)
    helper.deckhand._get_subset_validation_response = (
        lambda reivsion_id, subset_name: FK_VAL_SUBSET_RESP)
    helper.deckhand._get_entry_validation_response = (
        lambda reivsion_id, subset_name, entry_id: FK_VAL_ENTRY_RESP_EMPTY)
    assert len(helper._get_deckhand_validation_errors(5)) == 0
def test_get_rendered_configdocs():
    """
    Tests the RenderedConfigDocsResource method get_rendered_configdocs
    """
    rcdr = RenderedConfigDocsResource()

    with patch.object(ConfigdocsHelper,
                      'get_rendered_configdocs') as mock_method:
        helper = ConfigdocsHelper(CTX)
        rcdr.get_rendered_configdocs(helper, version='buffer')

    mock_method.assert_called_once_with('buffer')
Exemple #5
0
def test_add_messages_to_validation_status():
    helper = ConfigdocsHelper(CTX)
    status = {
        "kind": "Status",
        "apiVersion": "v1.0",
        "metadata": {},
        "status": "Success",
        "message": "Validations succeeded",
        "reason": "Validation",
        "details": {
            "errorCount": 0,
            "messageList": [],
        },
        "code": falcon.HTTP_200
    }
    info_messages = ['message 1', 'message 2']
    warn_messages = ['message 3']
    error_messages = ['message 4', 'message 5']

    add_messages_to_validation_status(status, info_messages, 'info')
    assert status['details']['errorCount'] == 0
    assert len(status['details']['messageList']) == 2
    assert type(status['details']['messageList'][0]) == dict
    assert status['details']['messageList'][0]['code'] == falcon.HTTP_200
    assert status['details']['messageList'][0]['message'] == 'message 1'
    assert status['details']['messageList'][0]['status'] == 'Info'
    assert status['details']['messageList'][0]['level'] == 'info'
    assert status["status"] == "Success"
    assert status["message"] == "Validations succeeded"
    assert status["code"] == falcon.HTTP_200

    add_messages_to_validation_status(status, warn_messages, 'warning')
    assert status['details']['errorCount'] == 0
    assert len(status['details']['messageList']) == 3
    assert status['details']['messageList'][2]['code'] == falcon.HTTP_200
    assert status['details']['messageList'][2]['message'] == 'message 3'
    assert status['details']['messageList'][2]['status'] == 'Warning'
    assert status['details']['messageList'][2]['level'] == 'warning'
    assert status["status"] == "Success"
    assert status["message"] == "Validations succeeded"
    assert status["code"] == falcon.HTTP_200

    add_messages_to_validation_status(status, error_messages, 'error')
    assert status['details']['errorCount'] == 2
    assert len(status['details']['messageList']) == 5
    assert status['details']['messageList'][3]['code'] == falcon.HTTP_400
    assert status['details']['messageList'][3]['message'] == 'message 4'
    assert status['details']['messageList'][3]['status'] == 'Error'
    assert status['details']['messageList'][3]['level'] == 'error'
    assert status["status"] == "Failure"
    assert status["message"] == "Validations failed"
    assert status["code"] == falcon.HTTP_400
    def test_commit_configdocs_buffer_err(self):
        """
        Tests the CommitConfigDocsResource method commit_configdocs
        """
        ccdr = CommitConfigDocsResource()

        with pytest.raises(ApiError):
            helper = ConfigdocsHelper(CTX)
            helper.is_buffer_empty = lambda: True
            helper.get_validations_for_revision = lambda x: {
                'status': 'Success'
            }
            ccdr.commit_configdocs(helper, False, False)
 def on_get(self, req, resp, collection_id):
     """
     Returns a collection of documents
     """
     version = (req.params.get('version') or 'buffer')
     self._validate_version_parameter(version)
     helper = ConfigdocsHelper(req.context)
     # Not reformatting to JSON or YAML since just passing through
     resp.body = self.get_collection(helper=helper,
                                     collection_id=collection_id,
                                     version=version)
     resp.append_header('Content-Type', 'application/x-yaml')
     resp.status = falcon.HTTP_200
Exemple #8
0
    def test__validate_deployment_version(self):
        """Test of the validate deployment version function
        """
        helper = None
        with patch.object(ConfigdocsHelper,
                          'check_for_document') as mock_method:
            cdr = ConfigDocsResource()
            helper = ConfigdocsHelper(CTX)
            cdr._validate_deployment_version(helper, 'oranges')

        mock_method.assert_called_once_with('oranges',
                                            DEPLOYMENT_DATA_DOC['name'],
                                            DEPLOYMENT_DATA_DOC['schema'])
Exemple #9
0
def test_get_validations_for_revision_dh_render(dh_client):
    """
    Tests the functionality of the get_validations_for_revision method
    """
    helper = ConfigdocsHelper(CTX)
    hold_ve = configdocs_helper._get_validation_endpoints
    helper._get_deckhand_validation_errors = lambda revision_id: []
    val_status = helper.get_validations_for_revision(3)
    err_count = val_status['details']['errorCount']
    err_list_count = len(val_status['details']['messageList'])
    assert err_count == err_list_count
    assert val_status['details']['errorCount'] == 1
    assert val_status['details']['messageList'][0]['message'] == 'broken!'
def test_get_rendered_successful_site_action_configdocs():
    """
    Tests the RenderedConfigDocsResource method get_rendered_configdocs
    for successful_site_action tag
    """
    rcdr = RenderedConfigDocsResource()

    with patch.object(ConfigdocsHelper,
                      'get_rendered_configdocs') as mock_method:
        helper = ConfigdocsHelper(CTX)
        rcdr.get_rendered_configdocs(helper, version='successful_site_action')

    mock_method.assert_called_once_with('successful_site_action')
Exemple #11
0
def test_check_intermediate_commit():
    helper_no_revs = ConfigdocsHelper(CTX)
    helper_no_revs.deckhand.get_revision_list = lambda: []

    helper_no_intermidiate_commits = ConfigdocsHelper(CTX)
    revs = 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
...
""")
    helper_no_intermidiate_commits.deckhand.get_revision_list = lambda: revs
    revs_interm = copy.deepcopy(revs)
    revs_interm[0]['tags'] = ['committed']

    helper_with_intermidiate_commits = ConfigdocsHelper(CTX)
    helper_with_intermidiate_commits.deckhand.get_revision_list = \
        lambda: revs_interm

    assert not helper_no_revs.check_intermediate_commit()
    assert not helper_no_intermidiate_commits.check_intermediate_commit()
    assert helper_with_intermidiate_commits.check_intermediate_commit()
Exemple #12
0
    def test_post_collection(self):
        """
        Tests the post collection method of the ConfigdocsResource
        """
        CONF.set_override('deployment_version_create', 'Skip', 'validations')
        helper = None
        collection_id = 'trees'
        document_data = 'lots of info'
        with patch.object(ConfigdocsHelper, 'add_collection') as mock_method:
            cdr = ConfigDocsResource()
            helper = ConfigdocsHelper(CTX)
            cdr.post_collection(helper=helper,
                                collection_id=collection_id,
                                document_data=document_data)

        mock_method.assert_called_once_with(collection_id, document_data)
Exemple #13
0
 def test_post_collection_not_valid_for_buffer(self):
     """
     Tests the post collection method of the ConfigdocsResource
     """
     CONF.set_override('deployment_version_create', 'Skip', 'validations')
     helper = None
     collection_id = 'trees'
     document_data = 'lots of info'
     with pytest.raises(ApiError) as apie:
         cdr = ConfigDocsResource()
         helper = ConfigdocsHelper(CTX)
         # not valid for bucket
         cdr.post_collection(helper=helper,
                             collection_id=collection_id,
                             document_data=document_data)
     assert apie.value.status == '409 Conflict'
Exemple #14
0
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
Exemple #15
0
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_post_collection_not_valid_for_buffer(self):
     """
     Tests the post collection method of the ConfigdocsResource
     """
     helper = None
     collection_id = 'trees'
     document_data = 'lots of info'
     with pytest.raises(ApiError) as apie:
         cdr = ConfigDocsResource()
         helper = ConfigdocsHelper(CTX)
         # not valid for bucket
         helper.get_deckhand_validation_status = (
             lambda a: configdocs_helper._format_validations_to_status([], 0
                                                                       ))
         cdr.post_collection(helper=helper,
                             collection_id=collection_id,
                             document_data=document_data)
     assert apie.value.status == '409 Conflict'
Exemple #17
0
def test_parse_received_doc_data():
    helper = ConfigdocsHelper(CTX)
    yaml = """
---
document1:
    - a
    - b
    - c
---
document2:
    - 1
    - 2
    - 3
...
"""
    parsed = helper.parse_received_doc_data(yaml)
    assert type(parsed) == list
    assert len(parsed) == 2
    def test_commit_configdocs_dryrun(self):
        """
        Tests the CommitConfigDocsResource method commit_configdocs
        """
        ccdr = CommitConfigDocsResource()
        commit_resp = None
        with patch.object(ConfigdocsHelper, 'tag_buffer') as mock_method:
            helper = ConfigdocsHelper(CTX)
            helper.is_buffer_empty = lambda: False
            helper.get_validations_for_revision = lambda x: {
                'status': 'Success'
            }
            helper.get_revision_id = lambda x: 1
            commit_resp = ccdr.commit_configdocs(helper, False, True)

        assert '200' in commit_resp['code']
        assert commit_resp['message'] == 'DRYRUN'
        assert commit_resp['status'] == 'Success'
Exemple #19
0
    def test_post_collection_deployment_version_missing_error(self):
        """
        Tests the post collection method of the ConfigdocsResource
        """
        # Make sure that the configuration value is handled case-insensitively
        CONF.set_override('deployment_version_create', 'eRRoR', 'validations')
        helper = None
        collection_id = 'trees'
        document_data = 'lots of info'
        cdr = ConfigDocsResource()
        helper = ConfigdocsHelper(CTX)
        with pytest.raises(ApiError) as apie:
            cdr.post_collection(helper=helper,
                                collection_id=collection_id,
                                document_data=document_data)

        assert apie.value.status == '400 Bad Request'
        assert apie.value.title == ('Deployment version document missing from '
                                    'collection')
Exemple #20
0
    def test_post_collection_deployment_version_missing_info(self):
        """
        Tests the post collection method of the ConfigdocsResource
        """
        # Make sure that the configuration value is handled case-insensitively
        CONF.set_override('deployment_version_create', 'iNfO', 'validations')
        helper = None
        collection_id = 'trees'
        document_data = 'lots of info'
        with patch.object(ConfigdocsHelper, 'add_collection') as mock_method:
            cdr = ConfigDocsResource()
            helper = ConfigdocsHelper(CTX)
            status = cdr.post_collection(helper=helper,
                                         collection_id=collection_id,
                                         document_data=document_data)
            assert len(status['details']['messageList']) == 1
            assert status['details']['messageList'][0]['level'] == 'info'

        mock_method.assert_called_once_with(collection_id, document_data)
    def test_post_collection(self):
        """
        Tests the post collection method of the ConfigdocsResource
        """
        helper = None
        collection_id = 'trees'
        document_data = 'lots of info'
        with patch.object(ConfigdocsHelper, 'add_collection') as mock_method:
            cdr = ConfigDocsResource()
            helper = ConfigdocsHelper(CTX)
            helper.is_buffer_valid_for_bucket = lambda a, b: True
            helper.get_deckhand_validation_status = (
                lambda a: configdocs_helper._format_validations_to_status([], 0
                                                                          ))
            cdr.post_collection(helper=helper,
                                collection_id=collection_id,
                                document_data=document_data)

        mock_method.assert_called_once_with(collection_id, document_data)
Exemple #22
0
    def on_get(self, req, resp, collection_id):
        """
        Returns a collection of documents
        """
        version = (req.params.get('version') or 'buffer')
        cleartext_secrets = req.get_param_as_bool('cleartext-secrets') or False
        self._validate_version_parameter(version)
        helper = ConfigdocsHelper(req.context)

        # Check access to cleartext_secrets
        if cleartext_secrets:
            policy.check_auth(req.context, policy.GET_CONFIGDOCS_CLRTXT)

        # Not reformatting to JSON or YAML since just passing through
        resp.body = self.get_collection(
            helper=helper, collection_id=collection_id, version=version,
            cleartext_secrets=cleartext_secrets)
        resp.append_header('Content-Type', 'application/x-yaml')
        resp.status = falcon.HTTP_200
    def test_commit_configdocs_force(self):
        """
        Tests the CommitConfigDocsResource method commit_configdocs
        """
        ccdr = CommitConfigDocsResource()
        commit_resp = None
        with patch.object(ConfigdocsHelper, 'tag_buffer') as mock_method:
            helper = ConfigdocsHelper(CTX)
            helper.is_buffer_empty = lambda: False
            helper.get_validations_for_revision = lambda x: {
                'status': 'Failure'
            }
            helper.get_revision_id = lambda x: 1
            commit_resp = ccdr.commit_configdocs(helper, True, False)

        mock_method.assert_called_once_with('committed')
        assert '200' in commit_resp['code']
        assert 'FORCED' in commit_resp['message']
        assert commit_resp['status'] == 'Failure'
Exemple #24
0
def test_check_for_document():
    helper = ConfigdocsHelper(CTX)
    yaml = """
---
schema: mycool/Document/v1
metadata:
  schema: metadata/Document/v1
  name: cool-doc
  storagePolicy: cleartext
  layeringDefinition:
    abstract: false
    layer: global
data:
  hello world
...
"""
    assert helper.check_for_document(yaml, 'cool-doc', 'mycool/Document/v1')
    assert not helper.check_for_document(yaml, 'cool-doc', 'nope')
    assert not helper.check_for_document(yaml, 'nope', 'mycool/Document/v1')
    assert not helper.check_for_document(yaml, 'nope', 'nope')
Exemple #25
0
    def test_post_collection_not_added(self):
        """
        Tests the post collection method of the ConfigdocsResource
        """
        CONF.set_override('deployment_version_create', 'Skip', 'validations')
        helper = None
        collection_id = 'trees'
        document_data = 'lots of info'
        with patch.object(ConfigdocsHelper, 'add_collection') as mock_method:
            cdr = ConfigDocsResource()
            helper = ConfigdocsHelper(CTX)
            with pytest.raises(ApiError) as apie:
                cdr.post_collection(helper=helper,
                                    collection_id=collection_id,
                                    document_data=document_data)

            assert apie.value.status == '400 Bad Request'
            assert apie.value.title == ('Collection {} not added to Shipyard '
                                        'buffer'.format(collection_id))
        mock_method.assert_called_once_with(collection_id, document_data)
Exemple #26
0
    def on_get(self, req, resp):
        """
        Returns the whole set of rendered documents
        """
        version = (req.params.get('version') or 'buffer')
        cleartext_secrets = req.get_param_as_bool('cleartext-secrets') or False
        self._validate_version_parameter(version)
        helper = ConfigdocsHelper(req.context)

        # Check access to cleartext_secrets
        if cleartext_secrets:
            policy.check_auth(req.context,
                              policy.GET_RENDEREDCONFIGDOCS_CLRTXT)

        resp.body = self.get_rendered_configdocs(
            helper=helper,
            version=version,
            cleartext_secrets=cleartext_secrets)
        resp.append_header('Content-Type', 'application/x-yaml')
        resp.status = falcon.HTTP_200
    def test_post_collection_not_added(self):
        """
        Tests the post collection method of the ConfigdocsResource
        """
        helper = None
        collection_id = 'trees'
        document_data = 'lots of info'
        with patch.object(ConfigdocsHelper, 'add_collection') as mock_method:
            cdr = ConfigDocsResource()
            helper = ConfigdocsHelper(CTX)
            helper.get_deckhand_validation_status = (
                lambda a: configdocs_helper._format_validations_to_status([], 0
                                                                          ))
            with pytest.raises(ApiError) as apie:
                cdr.post_collection(helper=helper,
                                    collection_id=collection_id,
                                    document_data=document_data)

            assert apie.value.status == '400 Bad Request'
        mock_method.assert_called_once_with(collection_id, document_data)
Exemple #28
0
def test_get_doc_names_and_schemas():
    helper = ConfigdocsHelper(CTX)
    yaml = """
---
doc_that_does_not_have_a: name or schema
---
schema: mycool/Document/v1
metadata:
  schema: metadata/Document/v1
  name: cool-doc
  storagePolicy: cleartext
  layeringDefinition:
    abstract: false
    layer: global
data:
  hello world
---
schema: notascool/Document/v1
metadata:
  schema: metadata/Document/v1
  name: average-document
  storagePolicy: cleartext
  layeringDefinition:
    abstract: false
    layer: global
data:
  goodbye space
...
"""
    names_and_schemas = helper.get_doc_names_and_schemas(yaml)
    assert type(names_and_schemas) == list
    assert len(names_and_schemas) == 3
    assert type(names_and_schemas[0]) == tuple
    assert names_and_schemas[0][0] == ''
    assert names_and_schemas[0][1] == ''
    assert names_and_schemas[1][0] == 'cool-doc'
    assert names_and_schemas[1][1] == 'mycool/Document/v1'
    assert names_and_schemas[2][0] == 'average-document'
    assert names_and_schemas[2][1] == 'notascool/Document/v1'
Exemple #29
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'])
Exemple #30
0
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')