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
22
22
import tests.tools as tools
23
from tests.tools import *
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
38
#logger.LoggerInit("/dev/stdout", "/dev/null", verbose=True)
39
#logger.LoggerInit("/dev/stdout", "/dev/null", verbose=False)
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
38
def mock_fetch_config(self, url, headers=None, chk_proxy=False):
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
58
55
raise Exception("Bad url {0}".format(url))
61
def MockFetchManifest(uris):
58
def mock_fetch_manifest(self, uris):
59
return manifest_sample
64
def MockFetchCache(filePath):
61
def mock_fetch_cache(self, file_path):
66
if "Incarnation" in filePath:
63
if "Incarnation" in file_path:
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
83
raise Exception("Bad filepath {0}".format(filePath))
80
raise Exception("Bad filepath {0}".format(file_path))
83
data_with_bom = b'\xef\xbb\xbfhehe'
85
class MockResp(object):
86
def __init__(self, status=v1.httpclient.OK, data=None):
94
return MockResp(status = v1.httpclient.FORBIDDEN)
97
return MockResp(status = v1.httpclient.GONE)
100
return MockResp(status = v1.httpclient.SERVICE_UNAVAILABLE)
86
102
class TestWireClint(unittest.TestCase):
88
@Mockup(v1, '_fetchCache', MockFetchCache)
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)
109
@mock(v1.WireClient, 'fetch_cache', mock_fetch_cache)
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)
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)
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)
123
class MockResp(object):
124
def __init__(self, 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)
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,
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,
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())
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)
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)
173
status_blob.put_page_blob("http://foo.bar", data)
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")
183
ext_status = v1.ExtensionStatus()
184
handler_status.extensions.append(ext_name)
185
ext_statuses[ext_name] = ext_status
145
187
substatus = v1.ExtensionSubStatus()
146
extStatus = v1.ExtensionStatus()
148
vmStatus.extensionHandlers.append(handlerStatus)
149
v1.vm_status_to_v1(vmStatus)
151
handlerStatus.extensionStatusList.append(extStatus)
152
v1.vm_status_to_v1(vmStatus)
154
extStatus.substatusList.append(substatus)
155
v1.vm_status_to_v1(vmStatus)
188
ext_status.substatusList.append(substatus)
190
vm_status.vmAgent.extensionHandlers.append(handler_status)
191
v1_status = v1.vm_status_to_v1(vm_status, ext_statuses)
157
194
def test_param(self):
158
195
param = v1.TelemetryEventParam()
159
196
event = v1.TelemetryEvent()
160
197
event.parameters.append(param)
162
v1.event_to_xml(event)
199
v1.event_to_v1(event)
164
201
if __name__ == '__main__':