예제 #1
0
    def test_sources_network_info_no_endpoint(self):
        """Test to get additional Source context from Sources API with no endpoint found."""
        test_source_id = 1
        mock_source_name = "source name"
        source_type_id = 1
        source_uid = faker.uuid4()
        test_auth_header = Config.SOURCES_FAKE_HEADER
        ocp_source = Sources(source_id=test_source_id, auth_header=test_auth_header, offset=1)
        ocp_source.save()

        with requests_mock.mock() as m:
            m.get(
                f"http://www.sources.com/api/v1.0/sources/{test_source_id}",
                status_code=200,
                json={"name": mock_source_name, "source_type_id": source_type_id, "uid": source_uid},
            )
            m.get(
                f"http://www.sources.com/api/v1.0/source_types?filter[id]={source_type_id}",
                status_code=200,
                json={"data": [{"name": mock_source_name}]},
            )
            m.get(
                f"http://www.sources.com/api/v1.0/endpoints?filter[source_id]={test_source_id}",
                status_code=200,
                json={"data": []},
            )
            source_integration.sources_network_info(test_source_id, test_auth_header)

        source_obj = Sources.objects.get(source_id=test_source_id)
        self.assertIsNone(source_obj.name)
        self.assertEquals(source_obj.source_type, "")
        self.assertEquals(source_obj.authentication, {})
예제 #2
0
    def test_sources_network_info_sync_ocp(self):
        """Test to get additional Source context from Sources API for OCP."""
        test_source_id = 1
        test_auth_header = Config.SOURCES_FAKE_HEADER
        source_name = 'OCP Source'
        source_uid = faker.uuid4()
        ocp_source = Sources(source_id=test_source_id,
                             auth_header=test_auth_header,
                             offset=1)
        ocp_source.save()
        source_type_id = 3
        resource_id = 2
        authentication_id = 4
        mock_source_name = 'openshift'
        with requests_mock.mock() as m:
            m.get(f'http://www.sources.com/api/v1.0/sources/{test_source_id}',
                  status_code=200, json={'name': source_name, 'source_type_id': source_type_id, 'uid': source_uid})
            m.get(f'http://www.sources.com/api/v1.0/source_types?filter[id]={source_type_id}',
                  status_code=200, json={'data': [{'name': mock_source_name}]})
            m.get(f'http://www.sources.com/api/v1.0/endpoints?filter[source_id]={test_source_id}',
                  status_code=200, json={'data': [{'id': resource_id}]})
            m.get((f'http://www.sources.com/api/v1.0/authentications?filter[resource_type]=Endpoint'
                  f'&[authtype]=token&[resource_id]={resource_id}'),
                  status_code=200, json={'data': [{'id': authentication_id}]})
            source_integration.sources_network_info(test_source_id, test_auth_header)

        source_obj = Sources.objects.get(source_id=test_source_id)
        self.assertEqual(source_obj.name, source_name)
        self.assertEqual(source_obj.source_type, 'OCP')
        self.assertEqual(source_obj.authentication, {'resource_name': source_uid})
예제 #3
0
    def test_sources_network_info_sync_aws(self):
        """Test to get additional Source context from Sources API for AWS."""
        test_source_id = 2
        test_auth_header = Config.SOURCES_FAKE_HEADER
        source_name = 'AWS Source'
        source_uid = faker.uuid4()
        authentication = 'roleARNhere'
        aws_source = Sources(source_id=test_source_id,
                             auth_header=test_auth_header,
                             offset=1)
        aws_source.save()
        source_type_id = 1
        mock_source_name = 'amazon'
        resource_id = 2
        authentication_id = 3
        with requests_mock.mock() as m:
            m.get(f'http://www.sources.com/api/v1.0/sources/{test_source_id}',
                  status_code=200, json={'name': source_name, 'source_type_id': source_type_id, 'uid': source_uid})
            m.get(f'http://www.sources.com/api/v1.0/source_types?filter[id]={source_type_id}',
                  status_code=200, json={'data': [{'name': mock_source_name}]})
            m.get(f'http://www.sources.com/api/v1.0/endpoints?filter[source_id]={test_source_id}',
                  status_code=200, json={'data': [{'id': resource_id}]})
            m.get((f'http://www.sources.com/api/v1.0/authentications?filter[resource_type]=Endpoint'
                  f'&[authtype]=arn&[resource_id]={resource_id}'),
                  status_code=200, json={'data': [{'id': authentication_id}]})
            m.get((f'http://www.sources.com/internal/v1.0/authentications/{authentication_id}'
                  f'?expose_encrypted_attribute[]=password'),
                  status_code=200, json={'password': authentication})

            source_integration.sources_network_info(test_source_id, test_auth_header)

        source_obj = Sources.objects.get(source_id=test_source_id)
        self.assertEqual(source_obj.name, source_name)
        self.assertEqual(source_obj.source_type, 'AWS')
        self.assertEqual(source_obj.authentication, {'resource_name': authentication})
예제 #4
0
    def test_sources_network_info_sync_ocp(self):
        """Test to get additional Source context from Sources API for OCP."""
        test_source_id = 1
        application_type = 2
        app_id = 1
        test_auth_header = Config.SOURCES_FAKE_HEADER
        source_name = "OCP Source"
        source_uid = faker.uuid4()
        ocp_source = Sources(source_id=test_source_id, auth_header=test_auth_header, offset=1)
        ocp_source.save()
        source_type_id = 3
        resource_id = 2
        authentication_id = 4
        mock_source_name = "openshift"
        with requests_mock.mock() as m:
            m.get(
                f"http://www.sources.com/api/v1.0/sources/{test_source_id}",
                status_code=200,
                json={"name": source_name, "source_type_id": source_type_id, "uid": source_uid},
            )
            m.get(
                f"http://www.sources.com/api/v1.0/application_types?filter[name]=/insights/platform/cost-management",
                status_code=200,
                json={"data": [{"id": application_type}]},
            )
            m.get(
                SOURCES_APPS.format(application_type, test_source_id), status_code=200, json={"data": [{"id": app_id}]}
            )
            m.get(
                f"http://www.sources.com/api/v1.0/source_types?filter[id]={source_type_id}",
                status_code=200,
                json={"data": [{"name": mock_source_name}]},
            )
            m.get(
                f"http://www.sources.com/api/v1.0/endpoints?filter[source_id]={test_source_id}",
                status_code=200,
                json={"data": [{"id": resource_id}]},
            )
            m.get(
                (
                    f"http://www.sources.com/api/v1.0/authentications?filter[resource_type]=Endpoint"
                    f"&[authtype]=token&[resource_id]={resource_id}"
                ),
                status_code=200,
                json={"data": [{"id": authentication_id}]},
            )
            m.patch(f"http://www.sources.com/api/v1.0/applications/{app_id}", status_code=204)
            source_integration.sources_network_info(test_source_id, test_auth_header)

        source_obj = Sources.objects.get(source_id=test_source_id)
        self.assertEqual(source_obj.name, source_name)
        self.assertEqual(source_obj.source_type, Provider.PROVIDER_OCP)
        self.assertEqual(source_obj.authentication, {})
예제 #5
0
    def test_sources_network_info_sync_aws(self):
        """Test to get additional Source context from Sources API for AWS."""
        test_source_id = 2
        test_auth_header = Config.SOURCES_FAKE_HEADER
        source_name = "AWS Source"
        source_uid = faker.uuid4()
        authentication = "roleARNhere"
        aws_source = Sources(source_id=test_source_id, auth_header=test_auth_header, offset=1)
        aws_source.save()
        source_type_id = 1
        mock_source_name = "amazon"
        resource_id = 2
        authentication_id = 3
        with requests_mock.mock() as m:
            m.get(
                f"http://www.sources.com/api/v1.0/sources/{test_source_id}",
                status_code=200,
                json={"name": source_name, "source_type_id": source_type_id, "uid": source_uid},
            )
            m.get(
                f"http://www.sources.com/api/v1.0/source_types?filter[id]={source_type_id}",
                status_code=200,
                json={"data": [{"name": mock_source_name}]},
            )
            m.get(
                f"http://www.sources.com/api/v1.0/endpoints?filter[source_id]={test_source_id}",
                status_code=200,
                json={"data": [{"id": resource_id}]},
            )
            m.get(
                (
                    f"http://www.sources.com/api/v1.0/authentications?filter[resource_type]=Endpoint"
                    f"&[authtype]=arn&[resource_id]={resource_id}"
                ),
                status_code=200,
                json={"data": [{"id": authentication_id}]},
            )
            m.get(
                (
                    f"http://www.sources.com/internal/v1.0/authentications/{authentication_id}"
                    f"?expose_encrypted_attribute[]=password"
                ),
                status_code=200,
                json={"password": authentication},
            )

            source_integration.sources_network_info(test_source_id, test_auth_header)

        source_obj = Sources.objects.get(source_id=test_source_id)
        self.assertEqual(source_obj.name, source_name)
        self.assertEqual(source_obj.source_type, Provider.PROVIDER_AWS)
        self.assertEqual(source_obj.authentication, {"resource_name": authentication})
예제 #6
0
    def test_sources_network_info_sync_connection_error(self):
        """Test to get additional Source context from Sources API with connection_error."""
        test_source_id = 1
        test_auth_header = Config.SOURCES_FAKE_HEADER
        ocp_source = Sources(source_id=test_source_id, auth_header=test_auth_header, offset=1)
        ocp_source.save()

        with requests_mock.mock() as m:
            m.get(f"http://www.sources.com/api/v1.0/sources/{test_source_id}", exc=SourcesHTTPClientError)

            source_integration.sources_network_info(test_source_id, test_auth_header)

        source_obj = Sources.objects.get(source_id=test_source_id)
        self.assertIsNone(source_obj.name)
        self.assertEquals(source_obj.source_type, "")
        self.assertEquals(source_obj.authentication, {})
예제 #7
0
    def test_sources_network_info_sync_azure(self):
        """Test to get additional Source context from Sources API for AZURE."""
        test_source_id = 3
        test_auth_header = Config.SOURCES_FAKE_HEADER
        source_name = 'AZURE Source'
        source_uid = faker.uuid4()
        username = '******'
        authentication = 'testclientcreds'
        tenent_id = 'test_tenent_id'
        azure_source = Sources(source_id=test_source_id,
                               auth_header=test_auth_header,
                               offset=1)
        azure_source.save()
        source_type_id = 2
        mock_source_name = 'azure'
        resource_id = 3
        authentication_id = 4
        authentications_response = {'id': authentication_id, 'username': username,
                                    'extra': {'azure': {'tenant_id': tenent_id}}}
        with requests_mock.mock() as m:
            m.get(f'http://www.sources.com/api/v1.0/sources/{test_source_id}',
                  status_code=200, json={'name': source_name, 'source_type_id': source_type_id, 'uid': source_uid})
            m.get(f'http://www.sources.com/api/v1.0/source_types?filter[id]={source_type_id}',
                  status_code=200, json={'data': [{'name': mock_source_name}]})
            m.get(f'http://www.sources.com/api/v1.0/endpoints?filter[source_id]={test_source_id}',
                  status_code=200, json={'data': [{'id': resource_id}]})
            m.get((f'http://www.sources.com/api/v1.0/authentications?filter[resource_type]=Endpoint'
                  f'&[authtype]=tenant_id_client_id_client_secret&[resource_id]={resource_id}'),
                  status_code=200, json={'data': [authentications_response]})
            m.get((f'http://www.sources.com/internal/v1.0/authentications/{authentication_id}'
                  f'?expose_encrypted_attribute[]=password'),
                  status_code=200, json={'password': authentication})

            source_integration.sources_network_info(test_source_id, test_auth_header)

        source_obj = Sources.objects.get(source_id=test_source_id)
        self.assertEqual(source_obj.name, source_name)
        self.assertEqual(source_obj.source_type, 'AZURE')
        self.assertEqual(source_obj.authentication, {'credentials': {'client_id': username,
                                                                     'client_secret': authentication,
                                                                     'tenant_id': tenent_id}})
예제 #8
0
    def test_sources_network_info_sync_azure(self):
        """Test to get additional Source context from Sources API for AZURE."""
        test_source_id = 3
        test_auth_header = Config.SOURCES_FAKE_HEADER
        source_name = "AZURE Source"
        source_uid = faker.uuid4()
        username = "******"
        authentication = "testclientcreds"
        tenent_id = "test_tenent_id"
        azure_source = Sources(source_id=test_source_id, auth_header=test_auth_header, offset=1)
        azure_source.save()
        source_type_id = 2
        mock_source_name = "azure"
        resource_id = 3
        authentication_id = 4
        authentications_response = {
            "id": authentication_id,
            "username": username,
            "extra": {"azure": {"tenant_id": tenent_id}},
        }
        with requests_mock.mock() as m:
            m.get(
                f"http://www.sources.com/api/v1.0/sources/{test_source_id}",
                status_code=200,
                json={"name": source_name, "source_type_id": source_type_id, "uid": source_uid},
            )
            m.get(
                f"http://www.sources.com/api/v1.0/source_types?filter[id]={source_type_id}",
                status_code=200,
                json={"data": [{"name": mock_source_name}]},
            )
            m.get(
                f"http://www.sources.com/api/v1.0/endpoints?filter[source_id]={test_source_id}",
                status_code=200,
                json={"data": [{"id": resource_id}]},
            )
            m.get(
                (
                    f"http://www.sources.com/api/v1.0/authentications?filter[resource_type]=Endpoint"
                    f"&[authtype]=tenant_id_client_id_client_secret&[resource_id]={resource_id}"
                ),
                status_code=200,
                json={"data": [authentications_response]},
            )
            m.get(
                (
                    f"http://www.sources.com/internal/v1.0/authentications/{authentication_id}"
                    f"?expose_encrypted_attribute[]=password"
                ),
                status_code=200,
                json={"password": authentication},
            )

            source_integration.sources_network_info(test_source_id, test_auth_header)

        source_obj = Sources.objects.get(source_id=test_source_id)
        self.assertEqual(source_obj.name, source_name)
        self.assertEqual(source_obj.source_type, Provider.PROVIDER_AZURE)
        self.assertEqual(
            source_obj.authentication,
            {"credentials": {"client_id": username, "client_secret": authentication, "tenant_id": tenent_id}},
        )