File: test_mgmt_advisor.py

package info (click to toggle)
python-azure 20250603%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 851,724 kB
  • sloc: python: 7,362,925; ansic: 804; javascript: 287; makefile: 195; sh: 145; xml: 109
file content (204 lines) | stat: -rw-r--r-- 7,252 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# coding: utf-8

#-------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
#--------------------------------------------------------------------------
import azure.mgmt.advisor
import datetime
import re
import unittest

from azure.mgmt.advisor.models import (
    ConfigData
)

from devtools_testutils import (
    AzureMgmtRecordedTestCase, ResourceGroupPreparer, recorded_by_proxy
)

# the goal of these tests is to validate AutoRest generation of the Python wrapper
# and NOT to validate the behavior of the API. so the tests will primarily attempt
# to verify that all operations are possible using the generated client and that
# the operations can accept valid input and produce valid output.

class TestMgmtAdvisor(AzureMgmtRecordedTestCase):

    def setup_method(self, method):
        self.client = self.create_mgmt_client(
            azure.mgmt.advisor.AdvisorManagementClient
        )

    @recorded_by_proxy
    def test_generate_recommendations(self):

        def call(response, *args, **kwargs):
            return response.http_response

        # trigger generate recommendations
        response = self.client.recommendations.generate(cls=call)

        # we should get a valid Location header back
        assert 'Location' in response.headers
        location = response.headers['Location']

        # extract the operation ID from the Location header
        operation_id = re.findall("[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}", location)

        # the operation ID should be a valid GUID
        assert operation_id != None
        assert len(operation_id), 1

        # we should be able to get generation status for this operation ID
        response = self.client.recommendations.get_generate_status(
            cls=call,
            operation_id = operation_id[0]
        )
        status_code = response.status_code

        # and the status should be 202 or 204
        assert status_code == 202 or status_code == 204

    @unittest.skip("unavailable")
    @recorded_by_proxy
    def test_suppressions(self):

        # first, get all recommendations
        response = list(self.client.recommendations.list())

        # we should have at least one recommendation
        assert len(response) != 0
        recommendation = None

        # the recommendation should have all relevant properties populated
        for rec in response:
            assert rec.id != None
            assert rec.name != None
            assert rec.type != None
            assert rec.category != None
            assert rec.impact != None
            # assert rec.risk != None
            assert rec.short_description != None
            assert rec.short_description.problem != None
            assert rec.short_description.solution != None
            if (rec.impacted_value != None):
                recommendation = rec

        # construct the properties needed for further operations
        resourceUri = recommendation.id[:recommendation.id.find("/providers/Microsoft.Advisor/recommendations")]
        recommendationName = recommendation.name
        suppressionName = "Python_SDK_Test"
        timeToLive = "00:01:00:00"

        # get the individual recommendation
        output = self.client.recommendations.get(
            resource_uri = resourceUri,
            recommendation_id = recommendationName
        )

        # it should be identical to what we got from list
        assert output.id == rec.id
        assert output.name == rec.name

        # create a new suppression
        suppression = self.client.suppressions.create(
            resource_uri = resourceUri,
            recommendation_id = recommendationName,
            name = suppressionName,
            ttl = timeToLive
        )

        # it should get created successfully
        assert suppression.ttl == "01:00:00"

        # get the suppression
        sup = self.client.suppressions.get(
            resource_uri = resourceUri,
            recommendation_id = recommendationName,
            name = suppressionName
        )

        # it should be identical to what we just added
        assert sup.name == suppressionName
        assert sup.id == resourceUri + "/providers/Microsoft.Advisor/recommendations/" + recommendationName + "/suppressions/" + suppressionName

        # delete the suppression
        self.client.suppressions.delete(
            resource_uri = resourceUri,
            recommendation_id = recommendationName,
            name = suppressionName
        )

        # the suppression should be gone
        #response = list(self.client.suppressions.list())
        #for sup in response:
        #    assert sup.Name != suppressionName

    @unittest.skip("unavailable")
    @recorded_by_proxy
    def test_configurations_subscription(self):

        # create a new configuration to update low CPU threshold to 20
        input = ConfigData()
        input.low_cpu_threshold=20

        # update the configuration
        response = self.client.configurations.create_in_subscription(input)

        # retrieve the configurations
        output = list(self.client.configurations.list_by_subscription())[0]

        # it should be identical to what we just set
        assert output.low_cpu_threshold == "20"

        # restore the default configuration
        input.low_cpu_threshold=5
        response = self.client.configurations.create_in_subscription(input)

        # retrieve the configurations
        output = list(self.client.configurations.list_by_subscription())[0]

        # it should be identical to what we just set
        assert output.low_cpu_threshold == "5"

    @ResourceGroupPreparer()
    @recorded_by_proxy
    def test_configurations_resourcegroup(self, resource_group):
        resourceGroupName = resource_group.name
        configurationName = "default"

        # create a new configuration to update exclude to True
        input = ConfigData()
        input.exclude=True

        # update the configuration
        self.client.configurations.create_in_resource_group(
            configuration_name=configurationName,
            resource_group=resourceGroupName,
            config_contract=input
        )

        # retrieve the configurations
        output = list(self.client.configurations.list_by_resource_group(resource_group = resourceGroupName))[0]

        # it should be identical to what we just set
        assert output.exclude == True

        # restore the default configuration
        input.exclude=False
        self.client.configurations.create_in_resource_group(
            configuration_name=configurationName,
            resource_group=resourceGroupName,
            config_contract=input
        )

        # retrieve the configurations
        output = list(self.client.configurations.list_by_resource_group(resource_group = resourceGroupName))[0]

        # it should be identical to what we just set
        assert output.exclude == False

#------------------------------------------------------------------------------
if __name__ == '__main__':
    unittest.main()