コード例 #1
0
    def test_client_request_id_echo(self):
        # client request id is different for every request, so it will never match the recorded one
        pytest.skip("Issue tracked here: https://github.com/Azure/azure-sdk-for-python/issues/8098")
        if TestMode.need_recording_file(self.test_mode):
            return

        # Arrange
        request_id_header_name = 'x-ms-client-request-id'
        service = ShareServiceClient(self.get_file_url(), credential=self.account_key)

        # Act make the client request ID slightly different
        def callback(response):
            response.http_response.status_code = 200
            response.http_response.headers[request_id_header_name] += '1'

        # Assert the client request ID validation is working
        with self.assertRaises(AzureError):
            service.get_service_properties(raw_response_hook=callback)

        # Act remove the echoed client request ID
        def callback(response):
            response.status_code = 200
            del response.http_response.headers[request_id_header_name]

        # Assert the client request ID validation is not throwing when the ID is not echoed
        service.get_service_properties(raw_response_hook=callback)
コード例 #2
0
    def test_user_agent_custom(self):
        custom_app = "TestApp/v1.0"
        service = ShareServiceClient(
            self.get_file_url(), credential=self.account_key, user_agent=custom_app)

        def callback1(response):
            self.assertTrue('User-Agent' in response.http_request.headers)
            self.assertEqual(
                response.http_request.headers['User-Agent'],
                "TestApp/v1.0 azsdk-python-storage-file-share/{} Python/{} ({})".format(
                    VERSION,
                    platform.python_version(),
                    platform.platform()))

        service.get_service_properties(raw_response_hook=callback1)

        def callback2(response):
            self.assertTrue('User-Agent' in response.http_request.headers)
            self.assertEqual(
                response.http_request.headers['User-Agent'],
                "TestApp/v2.0 azsdk-python-storage-file-share/{} Python/{} ({})".format(
                    VERSION,
                    platform.python_version(),
                    platform.platform()))

        service.get_service_properties(raw_response_hook=callback2, user_agent="TestApp/v2.0")
コード例 #3
0
    def test_user_agent_custom(self, resource_group, location, storage_account,
                               storage_account_key):
        self._setup(storage_account, storage_account_key)
        custom_app = "TestApp/v1.0"
        service = ShareServiceClient(self.account_url(storage_account, "file"),
                                     credential=self.account_key,
                                     user_agent=custom_app)

        def callback1(response):
            self.assertTrue('User-Agent' in response.http_request.headers)
            assert (
                "TestApp/v1.0 azsdk-python-storage-file-share/{} Python/{} ({})"
                .format(VERSION, platform.python_version(),
                        platform.platform())
            ) in response.http_request.headers['User-Agent']

        service.get_service_properties(raw_response_hook=callback1)

        def callback2(response):
            self.assertTrue('User-Agent' in response.http_request.headers)
            assert (
                "TestApp/v2.0 TestApp/v1.0 azsdk-python-storage-file-share/{} Python/{} ({})"
                .format(VERSION, platform.python_version(),
                        platform.platform())
            ) in response.http_request.headers['User-Agent']

        service.get_service_properties(raw_response_hook=callback2,
                                       user_agent="TestApp/v2.0")
コード例 #4
0
    def test_user_agent_default(self, resource_group, location,
                                storage_account, storage_account_key):
        self._setup(storage_account, storage_account_key)
        service = ShareServiceClient(self.account_url(storage_account, "file"),
                                     credential=self.account_key)

        def callback(response):
            self.assertTrue('User-Agent' in response.http_request.headers)
            assert "azsdk-python-storage-file-share/{}".format(
                VERSION) in response.http_request.headers['User-Agent']

        service.get_service_properties(raw_response_hook=callback)
    def test_user_agent_default(self):
        service = ShareServiceClient(self.get_file_url(),
                                     credential=self.account_key)

        def callback(response):
            self.assertTrue('User-Agent' in response.http_request.headers)
            self.assertEqual(
                response.http_request.headers['User-Agent'],
                "azsdk-python-storage-file-share/{} Python/{} ({})".format(
                    VERSION, platform.python_version(), platform.platform()))

        service.get_service_properties(raw_response_hook=callback)
コード例 #6
0
    def test_user_agent_append(self, resource_group, location, storage_account, storage_account_key):
        self._setup(storage_account, storage_account_key)
        service = ShareServiceClient(self.account_url(storage_account, "file"), credential=self.account_key)

        def callback(response):
            self.assertTrue('User-Agent' in response.http_request.headers)
            self.assertEqual(
                response.http_request.headers['User-Agent'],
                "azsdk-python-storage-file-share/{} Python/{} ({}) customer_user_agent".format(
                    VERSION,
                    platform.python_version(),
                    platform.platform()))

        custom_headers = {'User-Agent': 'customer_user_agent'}
        service.get_service_properties(raw_response_hook=callback, headers=custom_headers)
コード例 #7
0
    def test_user_agent_append(self, storage_account_name,
                               storage_account_key):
        self._setup(storage_account_name, storage_account_key)
        service = ShareServiceClient(self.account_url(storage_account_name,
                                                      "file"),
                                     credential=self.account_key)

        def callback(response):
            self.assertTrue('User-Agent' in response.http_request.headers)
            assert (
                "customer_user_agent azsdk-python-storage-file-share/{} Python/{} ({})"
                .format(VERSION, platform.python_version(),
                        platform.platform())
            ) in response.http_request.headers['User-Agent']

        service.get_service_properties(raw_response_hook=callback,
                                       user_agent='customer_user_agent')
コード例 #8
0
class FileServicePropertiesTest(FileTestCase):
    def setUp(self):
        super(FileServicePropertiesTest, self).setUp()

        url = self.get_file_url()
        credential = self.get_shared_key_credential()
        self.fsc = ShareServiceClient(url, credential=credential)

    # --Helpers-----------------------------------------------------------------
    def _assert_metrics_equal(self, metrics1, metrics2):
        if metrics1 is None or metrics2 is None:
            self.assertEqual(metrics1, metrics2)
            return

        self.assertEqual(metrics1.version, metrics2.version)
        self.assertEqual(metrics1.enabled, metrics2.enabled)
        self.assertEqual(metrics1.include_apis, metrics2.include_apis)
        self._assert_retention_equal(metrics1.retention_policy,
                                     metrics2.retention_policy)

    def _assert_cors_equal(self, cors1, cors2):
        if cors1 is None or cors2 is None:
            self.assertEqual(cors1, cors2)
            return

        self.assertEqual(len(cors1), len(cors2))

        for i in range(0, len(cors1)):
            rule1 = cors1[i]
            rule2 = cors2[i]
            self.assertEqual(len(rule1.allowed_origins),
                             len(rule2.allowed_origins))
            self.assertEqual(len(rule1.allowed_methods),
                             len(rule2.allowed_methods))
            self.assertEqual(rule1.max_age_in_seconds,
                             rule2.max_age_in_seconds)
            self.assertEqual(len(rule1.exposed_headers),
                             len(rule2.exposed_headers))
            self.assertEqual(len(rule1.allowed_headers),
                             len(rule2.allowed_headers))

    def _assert_retention_equal(self, ret1, ret2):
        self.assertEqual(ret1.enabled, ret2.enabled)
        self.assertEqual(ret1.days, ret2.days)

    # --Test cases per service ---------------------------------------
    @record
    def test_file_service_properties(self):
        # Arrange

        # Act
        resp = self.fsc.set_service_properties(hour_metrics=Metrics(),
                                               minute_metrics=Metrics(),
                                               cors=list())

        # Assert
        self.assertIsNone(resp)
        props = self.fsc.get_service_properties()
        self._assert_metrics_equal(props['hour_metrics'], Metrics())
        self._assert_metrics_equal(props['minute_metrics'], Metrics())
        self._assert_cors_equal(props['cors'], list())

    # --Test cases per feature ---------------------------------------
    @record
    def test_set_hour_metrics(self):
        # Arrange
        hour_metrics = Metrics(enabled=True,
                               include_apis=True,
                               retention_policy=RetentionPolicy(enabled=True,
                                                                days=5))

        # Act
        self.fsc.set_service_properties(hour_metrics=hour_metrics)

        # Assert
        received_props = self.fsc.get_service_properties()
        self._assert_metrics_equal(received_props['hour_metrics'],
                                   hour_metrics)

    @record
    def test_set_minute_metrics(self):
        # Arrange
        minute_metrics = Metrics(enabled=True,
                                 include_apis=True,
                                 retention_policy=RetentionPolicy(enabled=True,
                                                                  days=5))

        # Act
        self.fsc.set_service_properties(minute_metrics=minute_metrics)

        # Assert
        received_props = self.fsc.get_service_properties()
        self._assert_metrics_equal(received_props['minute_metrics'],
                                   minute_metrics)

    @record
    def test_set_cors(self):
        # Arrange
        cors_rule1 = CorsRule(['www.xyz.com'], ['GET'])

        allowed_origins = ['www.xyz.com', "www.ab.com", "www.bc.com"]
        allowed_methods = ['GET', 'PUT']
        max_age_in_seconds = 500
        exposed_headers = [
            "x-ms-meta-data*", "x-ms-meta-source*", "x-ms-meta-abc",
            "x-ms-meta-bcd"
        ]
        allowed_headers = [
            "x-ms-meta-data*", "x-ms-meta-target*", "x-ms-meta-xyz",
            "x-ms-meta-foo"
        ]
        cors_rule2 = CorsRule(allowed_origins,
                              allowed_methods,
                              max_age_in_seconds=max_age_in_seconds,
                              exposed_headers=exposed_headers,
                              allowed_headers=allowed_headers)

        cors = [cors_rule1, cors_rule2]

        # Act
        self.fsc.set_service_properties(cors=cors)

        # Assert
        received_props = self.fsc.get_service_properties()
        self._assert_cors_equal(received_props['cors'], cors)

    # --Test cases for errors ---------------------------------------
    @record
    def test_retention_no_days(self):
        # Assert
        self.assertRaises(ValueError, RetentionPolicy, True, None)

    @record
    def test_too_many_cors_rules(self):
        # Arrange
        cors = []
        for i in range(0, 6):
            cors.append(CorsRule(['www.xyz.com'], ['GET']))

        # Assert
        self.assertRaises(HttpResponseError, self.fsc.set_service_properties,
                          None, None, cors)
コード例 #9
0
class FileServicePropertiesTest(StorageTestCase):
    def _setup(self, storage_account_name, storage_account_key):
        url = self.account_url(storage_account_name, "file")
        credential = storage_account_key
        self.fsc = ShareServiceClient(url, credential=credential)

    def _teardown(self, FILE_PATH):
        if os.path.isfile(FILE_PATH):
            try:
                os.remove(FILE_PATH)
            except:
                pass

    # --Helpers-----------------------------------------------------------------
    def _assert_metrics_equal(self, metrics1, metrics2):
        if metrics1 is None or metrics2 is None:
            self.assertEqual(metrics1, metrics2)
            return

        self.assertEqual(metrics1.version, metrics2.version)
        self.assertEqual(metrics1.enabled, metrics2.enabled)
        self.assertEqual(metrics1.include_apis, metrics2.include_apis)
        self._assert_retention_equal(metrics1.retention_policy,
                                     metrics2.retention_policy)

    def _assert_cors_equal(self, cors1, cors2):
        if cors1 is None or cors2 is None:
            self.assertEqual(cors1, cors2)
            return

        self.assertEqual(len(cors1), len(cors2))

        for i in range(0, len(cors1)):
            rule1 = cors1[i]
            rule2 = cors2[i]
            self.assertEqual(len(rule1.allowed_origins),
                             len(rule2.allowed_origins))
            self.assertEqual(len(rule1.allowed_methods),
                             len(rule2.allowed_methods))
            self.assertEqual(rule1.max_age_in_seconds,
                             rule2.max_age_in_seconds)
            self.assertEqual(len(rule1.exposed_headers),
                             len(rule2.exposed_headers))
            self.assertEqual(len(rule1.allowed_headers),
                             len(rule2.allowed_headers))

    def _assert_retention_equal(self, ret1, ret2):
        self.assertEqual(ret1.enabled, ret2.enabled)
        self.assertEqual(ret1.days, ret2.days)

    # --Test cases per service ---------------------------------------
    @pytest.mark.playback_test_only
    @FileSharePreparer()
    def test_file_service_properties(self, storage_account_name,
                                     storage_account_key):
        self._setup(storage_account_name, storage_account_key)

        protocol_properties1 = ShareProtocolSettings(smb=ShareSmbSettings(
            multichannel=SmbMultichannel(enabled=False)))
        protocol_properties2 = ShareProtocolSettings(smb=ShareSmbSettings(
            multichannel=SmbMultichannel(enabled=True)))

        # Act
        resp = self.fsc.set_service_properties(hour_metrics=Metrics(),
                                               minute_metrics=Metrics(),
                                               cors=list(),
                                               protocol=protocol_properties1)
        # Assert
        self.assertIsNone(resp)
        props = self.fsc.get_service_properties()
        self._assert_metrics_equal(props['hour_metrics'], Metrics())
        self._assert_metrics_equal(props['minute_metrics'], Metrics())
        self._assert_cors_equal(props['cors'], list())
        self.assertEqual(props['protocol'].smb.multichannel.enabled, False)
        # Assert
        with self.assertRaises(ValueError):
            ShareProtocolSettings(smb=ShareSmbSettings(
                multichannel=SmbMultichannel()))
        with self.assertRaises(ValueError):
            ShareProtocolSettings(smb=ShareSmbSettings())
        with self.assertRaises(ValueError):
            ShareProtocolSettings()

        # Act
        self.fsc.set_service_properties(hour_metrics=Metrics(),
                                        minute_metrics=Metrics(),
                                        cors=list(),
                                        protocol=protocol_properties2)
        props = self.fsc.get_service_properties()
        self.assertEqual(props['protocol'].smb.multichannel.enabled, True)

    # --Test cases per feature ---------------------------------------
    @FileSharePreparer()
    def test_set_hour_metrics(self, storage_account_name, storage_account_key):
        self._setup(storage_account_name, storage_account_key)
        hour_metrics = Metrics(enabled=True,
                               include_apis=True,
                               retention_policy=RetentionPolicy(enabled=True,
                                                                days=5))

        # Act
        self.fsc.set_service_properties(hour_metrics=hour_metrics)

        # Assert
        received_props = self.fsc.get_service_properties()
        self._assert_metrics_equal(received_props['hour_metrics'],
                                   hour_metrics)

    @FileSharePreparer()
    def test_set_minute_metrics(self, storage_account_name,
                                storage_account_key):
        self._setup(storage_account_name, storage_account_key)
        minute_metrics = Metrics(enabled=True,
                                 include_apis=True,
                                 retention_policy=RetentionPolicy(enabled=True,
                                                                  days=5))

        # Act
        self.fsc.set_service_properties(minute_metrics=minute_metrics)

        # Assert
        received_props = self.fsc.get_service_properties()
        self._assert_metrics_equal(received_props['minute_metrics'],
                                   minute_metrics)

    @FileSharePreparer()
    def test_set_cors(self, storage_account_name, storage_account_key):
        self._setup(storage_account_name, storage_account_key)
        cors_rule1 = CorsRule(['www.xyz.com'], ['GET'])

        allowed_origins = ['www.xyz.com', "www.ab.com", "www.bc.com"]
        allowed_methods = ['GET', 'PUT']
        max_age_in_seconds = 500
        exposed_headers = [
            "x-ms-meta-data*", "x-ms-meta-source*", "x-ms-meta-abc",
            "x-ms-meta-bcd"
        ]
        allowed_headers = [
            "x-ms-meta-data*", "x-ms-meta-target*", "x-ms-meta-xyz",
            "x-ms-meta-foo"
        ]
        cors_rule2 = CorsRule(allowed_origins,
                              allowed_methods,
                              max_age_in_seconds=max_age_in_seconds,
                              exposed_headers=exposed_headers,
                              allowed_headers=allowed_headers)

        cors = [cors_rule1, cors_rule2]

        # Act
        self.fsc.set_service_properties(cors=cors)

        # Assert
        received_props = self.fsc.get_service_properties()
        self._assert_cors_equal(received_props['cors'], cors)

    # --Test cases for errors ---------------------------------------
    @FileSharePreparer()
    def test_retention_no_days(self, storage_account_name,
                               storage_account_key):
        self._setup(storage_account_name, storage_account_key)
        # Assert
        self.assertRaises(ValueError, RetentionPolicy, True, None)

    @FileSharePreparer()
    def test_too_many_cors_rules(self, storage_account_name,
                                 storage_account_key):
        self._setup(storage_account_name, storage_account_key)
        cors = []
        for i in range(0, 6):
            cors.append(CorsRule(['www.xyz.com'], ['GET']))

        # Assert
        self.assertRaises(HttpResponseError, self.fsc.set_service_properties,
                          None, None, cors)