Esempio n. 1
0
    def test_get_webconfiguration_settings(self):
        # Setup
        name = 'IIS'
        collection_setting = {'name': 'Collection[{yaml:\n\tdata}]', 'filter': 'value'}
        filter_setting = {'name': 'enabled',
                          'filter': 'system.webServer / security / authentication / anonymousAuthentication'}
        settings = [collection_setting, filter_setting]

        ps_cmd = ['$Settings = New-Object System.Collections.ArrayList;', ]
        for setting in settings:
            ps_cmd.extend([
                "$Property = Get-WebConfigurationProperty -PSPath '{}'".format(name),
                "-Name '{name}' -Filter '{filter}' -ErrorAction Stop;".format(
                    filter=setting['filter'], name=setting['name']),
                'if (([String]::IsNullOrEmpty($Property) -eq $False) -and',
                "($Property.GetType()).Name -eq 'ConfigurationAttribute') {",
                '$Property = $Property | Select-Object',
                '-ExpandProperty Value };',
                "$Settings.add(@{{filter='{filter}';name='{name}';value=[String] $Property}})| Out-Null;".format(
                    filter=setting['filter'], name=setting['name']),
                '$Property = $Null;',
            ])
        ps_cmd.append('$Settings')

        # Execute
        with patch.dict(win_iis.__salt__), \
             patch('salt.modules.win_iis._prepare_settings',
                   MagicMock(return_value=settings)), \
             patch('salt.modules.win_iis._srvmgr',
                   MagicMock(return_value={'retcode': 0, 'stdout': '{}'})):
            ret = win_iis.get_webconfiguration_settings(name, settings=settings)

            # Verify
            win_iis._srvmgr.assert_called_with(cmd=ps_cmd, return_json=True)
            self.assertEqual(ret, {})
Esempio n. 2
0
    def test_get_webconfiguration_settings_pass(self):
        settings = [{
            "name":
            "enabled",
            "filter":
            ("system.webServer/security/authentication/anonymousAuthentication"
             ),
        }]

        ps_cmd_validate = [
            "Get-WebConfigurationProperty",
            "-PSPath",
            "'salt'",
            "-Filter",
            "'system.webServer/security/authentication/anonymousAuthentication'",
            "-Name",
            "'enabled'",
            "-ErrorAction",
            "Stop",
            "|",
            "Out-Null;",
        ]

        ps_cmd = [
            "$Settings = New-Object System.Collections.ArrayList;",
            "$Property = Get-WebConfigurationProperty -PSPath 'salt'",
            "-Name 'enabled' -Filter"
            " 'system.webServer/security/authentication/anonymousAuthentication'"
            " -ErrorAction Stop;",
            "if (([String]::IsNullOrEmpty($Property) -eq $False) -and",
            "($Property.GetType()).Name -eq 'ConfigurationAttribute') {",
            "$Property = $Property | Select-Object",
            "-ExpandProperty Value };",
            "$Settings.add(@{filter='system.webServer/security/authentication/anonymousAuthentication';name='enabled';value=[String]"
            " $Property})| Out-Null;",
            "$Property = $Null;",
            "$Settings",
        ]

        func_ret = {"name": "enabled", "value": True}
        with patch.object(win_iis,
                          "_srvmgr",
                          return_value={
                              "retcode": 0,
                              "stdout": "json data"
                          }) as _srvmgr:
            with patch.object(win_iis.salt.utils.json,
                              "loads",
                              return_value=func_ret) as loads:
                ret = win_iis.get_webconfiguration_settings("salt", settings)

                self.assertEqual(_srvmgr.call_count, 2)
                self.assertEqual(_srvmgr.mock_calls[0],
                                 call(cmd=ps_cmd_validate, return_json=True))
                self.assertEqual(_srvmgr.mock_calls[1],
                                 call(cmd=ps_cmd, return_json=True))

                loads.assert_called_once_with("json data", strict=False)
                self.assertEqual(func_ret, ret)
Esempio n. 3
0
    def test_get_webconfiguration_settings(self):
        # Setup
        name = "IIS"
        collection_setting = {
            "name": "Collection[{yaml:\n\tdata}]",
            "filter": "value"
        }
        filter_setting = {
            "name":
            "enabled",
            "filter":
            ("system.webServer / security / authentication / anonymousAuthentication"
             ),
        }
        settings = [collection_setting, filter_setting]

        ps_cmd = [
            "$Settings = New-Object System.Collections.ArrayList;",
        ]
        for setting in settings:
            ps_cmd.extend([
                "$Property = Get-WebConfigurationProperty -PSPath '{}'".format(
                    name),
                "-Name '{name}' -Filter '{filter}' -ErrorAction Stop;".format(
                    filter=setting["filter"], name=setting["name"]),
                "if (([String]::IsNullOrEmpty($Property) -eq $False) -and",
                "($Property.GetType()).Name -eq 'ConfigurationAttribute') {",
                "$Property = $Property | Select-Object",
                "-ExpandProperty Value };",
                "$Settings.add(@{{filter='{filter}';name='{name}';value=[String]"
                " $Property}})| Out-Null;".format(filter=setting["filter"],
                                                  name=setting["name"]),
                "$Property = $Null;",
            ])
        ps_cmd.append("$Settings")

        # Execute
        with patch.dict(win_iis.__salt__), patch(
                "salt.modules.win_iis._prepare_settings",
                MagicMock(return_value=settings)), patch(
                    "salt.modules.win_iis._srvmgr",
                    MagicMock(return_value={
                        "retcode": 0,
                        "stdout": "{}"
                    }),
                ):
            ret = win_iis.get_webconfiguration_settings(name,
                                                        settings=settings)

            # Verify
            win_iis._srvmgr.assert_called_with(cmd=ps_cmd, return_json=True)
            self.assertEqual(ret, {})
Esempio n. 4
0
    def test_get_webconfiguration_settings_pass(self):
        settings = [{'name': 'enabled',
                     'filter': 'system.webServer/security/authentication/anonymousAuthentication'}]

        ps_cmd_validate = ['Get-WebConfigurationProperty',
                           '-PSPath',
                           "'salt'",
                           '-Filter',
                           "'system.webServer/security/authentication/anonymousAuthentication'",
                           '-Name',
                           "'enabled'",
                           '-Location',
                           "''",
                           '-ErrorAction',
                           'Stop',
                           '|',
                           'Out-Null;']

        ps_cmd = ['$Settings = New-Object System.Collections.ArrayList;',
                  "$Property = Get-WebConfigurationProperty -PSPath 'salt'",
                  "-Name 'enabled' -Filter 'system.webServer/security/authentication/anonymousAuthentication' -Location '' -ErrorAction Stop;",
                  'if (([String]::IsNullOrEmpty($Property) -eq $False) -and',
                  "($Property.GetType()).Name -eq 'ConfigurationAttribute') {",
                  '$Property = $Property | Select-Object',
                  '-ExpandProperty Value };',
                  "$Settings.add(@{filter='system.webServer/security/authentication/anonymousAuthentication';name='enabled';location='';value=[String] $Property})| Out-Null;",
                  '$Property = $Null;',
                  '$Settings']

        func_ret = {'name': 'enabled', 'value': True}
        with patch.object(win_iis, '_srvmgr', return_value={'retcode': 0, 'stdout': 'json data'}) as _srvmgr:
            with patch.object(win_iis.salt.utils.json, 'loads', return_value=func_ret) as loads:
                ret = win_iis.get_webconfiguration_settings('salt', settings)

                self.assertEqual(_srvmgr.call_count, 2)
                self.assertEqual(_srvmgr.mock_calls[0], call(cmd=ps_cmd_validate, return_json=True))
                self.assertEqual(_srvmgr.mock_calls[1], call(cmd=ps_cmd, return_json=True))

                loads.assert_called_once_with('json data', strict=False)
                self.assertEqual(func_ret, ret)
Esempio n. 5
0
 def test_get_webconfiguration_settings_no_settings(self):
     self.assertEqual(win_iis.get_webconfiguration_settings("salt", {}), {})
Esempio n. 6
0
 def test_get_webconfiguration_settings_empty(self, mock_log):
     ret = win_iis.get_webconfiguration_settings("name", settings=[])
     mock_log.warning.assert_called_once_with("No settings provided")
     self.assertEqual(ret, {})