~ubuntu-branches/debian/stretch/waagent/stretch

« back to all changes in this revision

Viewing changes to tests/test_v1.py

  • Committer: Package Import Robot
  • Author(s): Bastian Blank
  • Date: 2016-02-01 13:11:28 UTC
  • mfrom: (1.2.2)
  • Revision ID: package-import@ubuntu.com-20160201131128-4wxc2tklmq3x40xe
Tags: 2.1.2-1
* New upstream version.
* Depend on host, needed by Microsofts custom script stuff.
* Use cloud-init:
  - Use Ubuntu provisioning handler, disable provisioning.
  - Depend on new enough version of cloud-init.
  - Update dependencies in init script and service.
  - Disable recursive agent invocation and hostname bounce in clout-init.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
# http://msdn.microsoft.com/en-us/library/cc227282%28PROT.10%29.aspx
19
19
# http://msdn.microsoft.com/en-us/library/cc227259%28PROT.13%29.aspx
20
20
 
21
 
import env
 
21
import tests.env
22
22
import tests.tools as tools
23
 
from tools import *
 
23
from tests.tools import *
24
24
import uuid
25
25
import unittest
26
26
import os
27
27
import time
28
 
import httplib
 
28
from azurelinuxagent.utils.restutil import httpclient
29
29
import azurelinuxagent.logger as logger
30
30
import azurelinuxagent.protocol.v1 as v1
31
 
from test_version import VersionInfoSample
32
 
from test_goalstate import GoalStateSample
33
 
from test_hostingenv import HostingEnvSample
34
 
from test_sharedconfig import SharedConfigSample
35
 
from test_certificates import CertificatesSample, TransportCert
36
 
from test_extensionsconfig import ExtensionsConfigSample, ManifestSample
37
 
 
38
 
#logger.LoggerInit("/dev/stdout", "/dev/null", verbose=True)
39
 
#logger.LoggerInit("/dev/stdout", "/dev/null", verbose=False)
40
 
 
41
 
def MockFetchUri(url, headers=None, chkProxy=False):
 
31
from tests.test_version import VersionInfoSample
 
32
from tests.test_goalstate import goal_state_sample
 
33
from tests.test_hostingenv import hosting_env_sample
 
34
from tests.test_sharedconfig import shared_config_sample
 
35
from tests.test_certificates import certs_sample, transport_cert
 
36
from tests.test_extensionsconfig import ext_conf_sample, manifest_sample
 
37
 
 
38
def mock_fetch_config(self, url, headers=None, chk_proxy=False):
42
39
    content = None
43
40
    if "versions" in url:
44
41
        content = VersionInfoSample
45
42
    elif "goalstate" in url:
46
 
        content = GoalStateSample
 
43
        content = goal_state_sample
47
44
    elif "hostingenvuri" in url:
48
 
        content = HostingEnvSample
 
45
        content = hosting_env_sample
49
46
    elif "sharedconfiguri" in url:
50
 
        content = SharedConfigSample
 
47
        content = shared_config_sample
51
48
    elif "certificatesuri" in url:
52
 
        content = CertificatesSample
 
49
        content = certs_sample
53
50
    elif "extensionsconfiguri" in url:
54
 
        content = ExtensionsConfigSample
 
51
        content = ext_conf_sample
55
52
    elif "manifest.xml" in url:
56
 
        content = ManifestSample
 
53
        content = manifest_sample
57
54
    else:
58
55
        raise Exception("Bad url {0}".format(url))
59
56
    return content
60
57
 
61
 
def MockFetchManifest(uris):
62
 
    return ManifestSample
 
58
def mock_fetch_manifest(self, uris):
 
59
    return manifest_sample
63
60
 
64
 
def MockFetchCache(filePath):
 
61
def mock_fetch_cache(self, file_path):
65
62
    content = None
66
 
    if "Incarnation" in filePath:
 
63
    if "Incarnation" in file_path:
67
64
        content = 1
68
 
    elif "GoalState" in filePath:
69
 
        content = GoalStateSample
70
 
    elif "HostingEnvironmentConfig" in filePath:
71
 
        content = HostingEnvSample
72
 
    elif "SharedConfig" in filePath:
73
 
        content = SharedConfigSample
74
 
    elif "Certificates" in filePath:
75
 
        content = CertificatesSample
76
 
    elif "TransportCert" in filePath:
77
 
        content = TransportCert
78
 
    elif "ExtensionsConfig" in filePath:
79
 
        content = ExtensionsConfigSample
80
 
    elif "manifest" in filePath:
81
 
        content = ManifestSample
 
65
    elif "GoalState" in file_path:
 
66
        content = goal_state_sample
 
67
    elif "HostingEnvironmentConfig" in file_path:
 
68
        content = hosting_env_sample
 
69
    elif "SharedConfig" in file_path:
 
70
        content = shared_config_sample
 
71
    elif "Certificates" in file_path:
 
72
        content = certs_sample
 
73
    elif "TransportCert" in file_path:
 
74
        content = transport_cert
 
75
    elif "ExtensionsConfig" in file_path:
 
76
        content = ext_conf_sample
 
77
    elif "manifest" in file_path:
 
78
        content = manifest_sample
82
79
    else:
83
 
        raise Exception("Bad filepath {0}".format(filePath))
 
80
        raise Exception("Bad filepath {0}".format(file_path))
84
81
    return content
85
82
 
 
83
data_with_bom = b'\xef\xbb\xbfhehe'
 
84
 
 
85
class MockResp(object):
 
86
    def __init__(self, status=v1.httpclient.OK, data=None):
 
87
        self.status = status
 
88
        self.data = data
 
89
 
 
90
    def read(self):
 
91
        return self.data
 
92
 
 
93
def mock_403():
 
94
    return MockResp(status = v1.httpclient.FORBIDDEN)
 
95
 
 
96
def mock_410():
 
97
    return MockResp(status = v1.httpclient.GONE)
 
98
 
 
99
def mock_503():
 
100
    return MockResp(status = v1.httpclient.SERVICE_UNAVAILABLE)
 
101
 
86
102
class TestWireClint(unittest.TestCase):
87
103
 
88
 
    @Mockup(v1, '_fetchCache', MockFetchCache)
89
 
    def testGet(self):
 
104
    @mock(v1.restutil, 'http_get', MockFunc(retval=MockResp(data=data_with_bom)))
 
105
    def test_fetch_uri_with_bom(self):
 
106
        client = v1.WireClient("http://foo.bar/")
 
107
        client.fetch_config("http://foo.bar", None)
 
108
 
 
109
    @mock(v1.WireClient, 'fetch_cache', mock_fetch_cache)
 
110
    def test_get(self):
90
111
        os.chdir('/tmp')
91
112
        client = v1.WireClient("foobar")
92
 
        goalState = client.getGoalState()
 
113
        goalState = client.get_goal_state()
93
114
        self.assertNotEquals(None, goalState)
94
 
        hostingEnv = client.getHostingEnv()
 
115
        hostingEnv = client.get_hosting_env()
95
116
        self.assertNotEquals(None, hostingEnv)
96
 
        sharedConfig = client.getSharedConfig()
 
117
        sharedConfig = client.get_shared_conf()
97
118
        self.assertNotEquals(None, sharedConfig)
98
 
        extensionsConfig = client.getExtensionsConfig()
 
119
        extensionsConfig = client.get_ext_conf()
99
120
        self.assertNotEquals(None, extensionsConfig)
100
121
   
101
122
    
102
 
    @Mockup(v1, '_fetchCache', MockFetchCache)
103
 
    def testGetHeaderWithCert(self):
 
123
    @mock(v1.WireClient, 'fetch_cache', mock_fetch_cache)
 
124
    def test_get_head_for_cert(self):
104
125
        client = v1.WireClient("foobar")
105
 
        header = client.getHeaderWithCert()
 
126
        header = client.get_header_for_cert()
106
127
        self.assertNotEquals(None, header)
107
128
 
108
 
    @Mockup(v1.WireClient, 'getHeaderWithCert', MockFunc()) 
109
 
    @Mockup(v1, '_fetchUri', MockFetchUri)
110
 
    @Mockup(v1.fileutil, 'SetFileContents', MockFunc())
111
 
    def testUpdateGoalState(self):
112
 
        client = v1.WireClient("foobar")
113
 
        client.updateGoalState()
114
 
        goalState = client.getGoalState()
115
 
        self.assertNotEquals(None, goalState)
116
 
        hostingEnv = client.getHostingEnv()
117
 
        self.assertNotEquals(None, hostingEnv)
118
 
        sharedConfig = client.getSharedConfig()
119
 
        self.assertNotEquals(None, sharedConfig)
120
 
        extensionsConfig = client.getExtensionsConfig()
121
 
        self.assertNotEquals(None, extensionsConfig)
122
 
 
123
 
class MockResp(object):
124
 
    def __init__(self, status):
125
 
        self.status = status
 
129
    @mock(v1.WireClient, 'get_header_for_cert', MockFunc()) 
 
130
    @mock(v1.WireClient, 'fetch_config', mock_fetch_config)
 
131
    @mock(v1.WireClient, 'fetch_manifest', mock_fetch_manifest)
 
132
    @mock(v1.fileutil, 'write_file', MockFunc())
 
133
    def test_update_goal_state(self):
 
134
        client = v1.WireClient("foobar")
 
135
        client.update_goal_state()
 
136
        goal_state = client.get_goal_state()
 
137
        self.assertNotEquals(None, goal_state)
 
138
        hosting_env = client.get_hosting_env()
 
139
        self.assertNotEquals(None, hosting_env)
 
140
        shared_config = client.get_shared_conf()
 
141
        self.assertNotEquals(None, shared_config)
 
142
        ext_conf = client.get_ext_conf()
 
143
        self.assertNotEquals(None, ext_conf)
 
144
 
 
145
    @mock(v1.time, "sleep", MockFunc())
 
146
    def test_call_wireserver(self):
 
147
        client = v1.WireClient("foobar")
 
148
        self.assertRaises(v1.ProtocolError, client.call_wireserver, mock_403)
 
149
        self.assertRaises(v1.WireProtocolResourceGone, client.call_wireserver, 
 
150
                          mock_410)
 
151
 
 
152
    @mock(v1.time, "sleep", MockFunc())
 
153
    def test_call_storage_service(self):
 
154
        client = v1.WireClient("foobar")
 
155
        self.assertRaises(v1.ProtocolError, client.call_storage_service, 
 
156
                          mock_503)
 
157
 
126
158
 
127
159
class TestStatusBlob(unittest.TestCase):
128
160
    def testToJson(self):
129
 
        vmStatus = v1.VMStatus()
130
 
        statusBlob = v1.StatusBlob(vmStatus)
131
 
        self.assertNotEquals(None, statusBlob.toJson())
 
161
        vm_status = v1.VMStatus()
 
162
        status_blob = v1.StatusBlob(v1.WireClient("http://foo.bar/"))
 
163
        status_blob.set_vm_status(vm_status)
 
164
        self.assertNotEquals(None, status_blob.to_json())
132
165
 
133
 
    @Mockup(v1.restutil, 'HttpPut', MockFunc(retval=MockResp(httplib.CREATED)))
134
 
    @Mockup(v1.restutil, 'HttpHead', MockFunc(retval=MockResp(httplib.OK)))
 
166
    @mock(v1.restutil, 'http_put', MockFunc(retval=MockResp(httpclient.CREATED)))
 
167
    @mock(v1.restutil, 'http_head', MockFunc(retval=MockResp(httpclient.OK)))
135
168
    def test_put_page_blob(self):
136
 
        vmStatus = v1.VMStatus()
137
 
        statusBlob = v1.StatusBlob(vmStatus)
138
 
        data = ['a'] * 100
139
 
        statusBlob.putPageBlob("http://foo.bar", data)
 
169
        vm_status = v1.VMStatus()
 
170
        status_blob = v1.StatusBlob(v1.WireClient("http://foo.bar/"))
 
171
        status_blob.set_vm_status(vm_status)
 
172
        data = 'a' * 100
 
173
        status_blob.put_page_blob("http://foo.bar", data)
140
174
 
141
175
class TestConvert(unittest.TestCase):
142
176
    def test_status(self):
143
 
        vmStatus = v1.VMStatus() 
144
 
        handlerStatus = v1.ExtensionHandlerStatus()
 
177
        vm_status = v1.VMStatus() 
 
178
        handler_status = v1.ExtHandlerStatus(name="foo")
 
179
 
 
180
        ext_statuses = {}
 
181
 
 
182
        ext_name="bar"
 
183
        ext_status = v1.ExtensionStatus()
 
184
        handler_status.extensions.append(ext_name)
 
185
        ext_statuses[ext_name] = ext_status
 
186
 
145
187
        substatus = v1.ExtensionSubStatus()
146
 
        extStatus = v1.ExtensionStatus()
147
 
 
148
 
        vmStatus.extensionHandlers.append(handlerStatus)
149
 
        v1.vm_status_to_v1(vmStatus)
150
 
 
151
 
        handlerStatus.extensionStatusList.append(extStatus)
152
 
        v1.vm_status_to_v1(vmStatus)
153
 
 
154
 
        extStatus.substatusList.append(substatus)
155
 
        v1.vm_status_to_v1(vmStatus)
 
188
        ext_status.substatusList.append(substatus)
 
189
 
 
190
        vm_status.vmAgent.extensionHandlers.append(handler_status)
 
191
        v1_status = v1.vm_status_to_v1(vm_status, ext_statuses)
 
192
        print(v1_status)
156
193
 
157
194
    def test_param(self):
158
195
        param = v1.TelemetryEventParam()
159
196
        event = v1.TelemetryEvent()
160
197
        event.parameters.append(param)
161
198
        
162
 
        v1.event_to_xml(event)
 
199
        v1.event_to_v1(event)
163
200
 
164
201
if __name__ == '__main__':
165
202
    unittest.main()