mirror of
https://github.com/clearlinux/WALinuxAgent.git
synced 2026-04-28 11:03:36 +00:00
Support both python2 and python3
This commit is contained in:
@@ -4,7 +4,7 @@ python:
|
||||
- "2.7"
|
||||
#- "3.2"
|
||||
#- "3.3"
|
||||
#- "3.4"
|
||||
- "3.4"
|
||||
# command to install dependencies
|
||||
install:
|
||||
#- pip install .
|
||||
|
||||
@@ -76,7 +76,7 @@ def validate_dhcp_resp(request, response):
|
||||
bytes_recv = len(response)
|
||||
if bytes_recv < 0xF6:
|
||||
logger.error("HandleDhcpResponse: Too few bytes received:{0}",
|
||||
str(bytes_recv))
|
||||
bytes_recv)
|
||||
return False
|
||||
|
||||
logger.verb("BytesReceived:{0}", hex(bytes_recv))
|
||||
@@ -113,7 +113,7 @@ def parse_route(response, option, i, length, bytes_recv):
|
||||
hex(length))
|
||||
routes = []
|
||||
if length < 5:
|
||||
logger.error("Data too small for option:{0}", str(option))
|
||||
logger.error("Data too small for option:{0}", option)
|
||||
j = i + 2
|
||||
while j < (i + length + 2):
|
||||
mask_len_bits = str_to_ord(response[j])
|
||||
@@ -140,7 +140,7 @@ def parse_ip_addr(response, option, i, length, bytes_recv):
|
||||
ip_addr = int_to_ip4_addr(addr)
|
||||
return ip_addr
|
||||
else:
|
||||
logger.error("Data too small for option:{0}", str(option))
|
||||
logger.error("Data too small for option:{0}", option)
|
||||
return None
|
||||
|
||||
def parse_dhcp_resp(response):
|
||||
|
||||
@@ -22,6 +22,7 @@ import time
|
||||
import json
|
||||
import subprocess
|
||||
import azurelinuxagent.logger as logger
|
||||
from azurelinuxagent.future import text
|
||||
from azurelinuxagent.utils.osutil import OSUTIL
|
||||
import azurelinuxagent.protocol as prot
|
||||
from azurelinuxagent.event import add_event, WALAEventOperation
|
||||
@@ -129,10 +130,10 @@ class ExtensionsHandler(object):
|
||||
logger.error("Failed to handle extension: {0}-{1}\n {2}",
|
||||
ext.get_name(), ext.get_version(), e)
|
||||
add_event(name=ext.get_name(), is_success=False,
|
||||
op=ext.get_curr_op(), message = str(e))
|
||||
op=ext.get_curr_op(), message = text(e))
|
||||
ext_status = prot.ExtensionStatus(status='error', code='-1',
|
||||
operation = ext.get_curr_op(),
|
||||
message = str(e),
|
||||
message = text(e),
|
||||
seq_no = ext.get_seq_no())
|
||||
status = ext.create_handler_status(ext_status)
|
||||
status.status = "Ready"
|
||||
|
||||
@@ -26,6 +26,7 @@ import time
|
||||
import pwd
|
||||
import fcntl
|
||||
import azurelinuxagent.logger as logger
|
||||
from azurelinuxagent.future import text
|
||||
import azurelinuxagent.utils.fileutil as fileutil
|
||||
import azurelinuxagent.utils.shellutil as shellutil
|
||||
import azurelinuxagent.utils.textutil as textutil
|
||||
@@ -603,7 +604,7 @@ class DefaultOSUtil(object):
|
||||
for vmbus in os.listdir(path):
|
||||
deviceid = fileutil.read_file(os.path.join(path, vmbus, "device_id"))
|
||||
guid = deviceid.lstrip('{').split('-')
|
||||
if guid[0] == g0 and guid[1] == "000" + str(port_id):
|
||||
if guid[0] == g0 and guid[1] == "000" + text(port_id):
|
||||
for root, dirs, files in os.walk(path + vmbus):
|
||||
if root.endswith("/block"):
|
||||
device = dirs[0]
|
||||
|
||||
@@ -21,6 +21,7 @@ Provision handler
|
||||
|
||||
import os
|
||||
import azurelinuxagent.logger as logger
|
||||
from azurelinuxagent.future import text
|
||||
import azurelinuxagent.conf as conf
|
||||
from azurelinuxagent.event import add_event, WALAEventOperation
|
||||
from azurelinuxagent.exception import *
|
||||
@@ -65,9 +66,9 @@ class ProvisionHandler(object):
|
||||
except ProvisionError as e:
|
||||
logger.error("Provision failed: {0}", e)
|
||||
status = prot.ProvisionStatus(status="NotReady",
|
||||
subStatus= str(e))
|
||||
subStatus= text(e))
|
||||
protocol.report_provision_status(status)
|
||||
add_event(name="WALA", is_success=False, message=str(e),
|
||||
add_event(name="WALA", is_success=False, message=text(e),
|
||||
op=WALAEventOperation.Provision)
|
||||
|
||||
def reg_ssh_host_key(self):
|
||||
|
||||
@@ -21,6 +21,7 @@ import os
|
||||
import re
|
||||
import threading
|
||||
import azurelinuxagent.logger as logger
|
||||
from azurelinuxagent.future import text
|
||||
import azurelinuxagent.conf as conf
|
||||
from azurelinuxagent.utils.osutil import OSUTIL
|
||||
from azurelinuxagent.event import add_event, WALAEventOperation
|
||||
@@ -67,7 +68,7 @@ class ResourceDiskHandler(object):
|
||||
return mount_point
|
||||
except ResourceDiskError as e:
|
||||
logger.error("Failed to mount resource disk {0}", e)
|
||||
add_event(name="WALA", is_success=False, message=str(e),
|
||||
add_event(name="WALA", is_success=False, message=text(e),
|
||||
op=WALAEventOperation.ActivateResourceDisk)
|
||||
|
||||
def enable_swap(self, mount_point):
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
import os
|
||||
import time
|
||||
import azurelinuxagent.logger as logger
|
||||
from azurelinuxagent.future import text
|
||||
import azurelinuxagent.conf as conf
|
||||
from azurelinuxagent.metadata import AGENT_LONG_NAME, AGENT_VERSION, \
|
||||
DISTRO_NAME, DISTRO_VERSION, DISTRO_FULL_NAME
|
||||
@@ -38,8 +39,7 @@ class MainHandler(object):
|
||||
logger.info("OS: {0} {1}", DISTRO_NAME, DISTRO_VERSION)
|
||||
|
||||
event.enable_unhandled_err_dump("Azure Linux Agent")
|
||||
fileutil.write_file(OSUTIL.get_agent_pid_file_path(),
|
||||
str(os.getpid()))
|
||||
fileutil.write_file(OSUTIL.get_agent_pid_file_path(), text(os.getpid()))
|
||||
|
||||
if conf.get_switch("DetectScvmmEnv", False):
|
||||
if self.handlers.scvmm_handler.detect_scvmm_env():
|
||||
|
||||
@@ -27,6 +27,7 @@ import fcntl
|
||||
import time
|
||||
import base64
|
||||
import azurelinuxagent.logger as logger
|
||||
from azurelinuxagent.future import text, bytebuffer
|
||||
import azurelinuxagent.utils.fileutil as fileutil
|
||||
import azurelinuxagent.utils.shellutil as shellutil
|
||||
import azurelinuxagent.utils.textutil as textutil
|
||||
@@ -70,8 +71,6 @@ class Redhat6xOSUtil(DefaultOSUtil):
|
||||
key = der_decoder.decode(self.bits_to_bytes(der_encoded))[0]
|
||||
n=key[0]
|
||||
e=key[1]
|
||||
print(n)
|
||||
print(e)
|
||||
keydata = bytearray()
|
||||
keydata.extend(struct.pack('>I', len("ssh-rsa")))
|
||||
keydata.extend(b"ssh-rsa")
|
||||
@@ -80,13 +79,12 @@ class Redhat6xOSUtil(DefaultOSUtil):
|
||||
keydata.extend(struct.pack('>I', len(self.num_to_bytes(n)) + 1))
|
||||
keydata.extend(b"\0")
|
||||
keydata.extend(self.num_to_bytes(n))
|
||||
return str(b"ssh-rsa " + base64.b64encode(keydata) + b"\n",
|
||||
encoding='utf-8')
|
||||
keydata_base64 = base64.b64encode(bytebuffer(keydata))
|
||||
return text(b"ssh-rsa " + keydata_base64 + b"\n",
|
||||
encoding='utf-8')
|
||||
except ImportError as e:
|
||||
raise OSUtilError("Failed to load pyasn1.codec.der")
|
||||
#except Exception as e:
|
||||
#raise OSUtilError(("Failed to convert public key: {0} {1}"
|
||||
#"").format(type(e).__name__, e))
|
||||
|
||||
def num_to_bytes(self, num):
|
||||
"""
|
||||
Pack number into bytes. Retun as string.
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
import os
|
||||
import time
|
||||
import azurelinuxagent.logger as logger
|
||||
from azurelinuxagent.future import text
|
||||
import azurelinuxagent.conf as conf
|
||||
import azurelinuxagent.protocol as prot
|
||||
from azurelinuxagent.exception import *
|
||||
@@ -57,7 +58,7 @@ class UbuntuProvisionHandler(ProvisionHandler):
|
||||
|
||||
except ProvisionError as e:
|
||||
logger.error("Provision failed: {0}", e)
|
||||
protocol.report_provision_status(status="NotReady", subStatus=str(e))
|
||||
protocol.report_provision_status(status="NotReady", subStatus=text(e))
|
||||
|
||||
def wait_for_ssh_host_key(self, max_retry=60):
|
||||
kepair_type = conf.get("Provisioning.SshHostKeyPairType", "rsa")
|
||||
|
||||
@@ -25,9 +25,10 @@ import datetime
|
||||
import threading
|
||||
import platform
|
||||
import azurelinuxagent.logger as logger
|
||||
from azurelinuxagent.future import text
|
||||
import azurelinuxagent.protocol as prot
|
||||
from azurelinuxagent.metadata import DISTRO_NAME, DISTRO_VERSION, DISTRO_CODE_NAME,\
|
||||
AGENT_VERSION
|
||||
from azurelinuxagent.metadata import DISTRO_NAME, DISTRO_VERSION, \
|
||||
DISTRO_CODE_NAME, AGENT_VERSION
|
||||
from azurelinuxagent.utils.osutil import OSUTIL
|
||||
|
||||
class EventError(Exception):
|
||||
@@ -142,7 +143,7 @@ def save_event(data):
|
||||
if len(os.listdir(event_dir)) > 1000:
|
||||
raise EventError("Too many files under: {0}", event_dir)
|
||||
|
||||
filename = os.path.join(event_dir, str(int(time.time()*1000000)))
|
||||
filename = os.path.join(event_dir, text(int(time.time()*1000000)))
|
||||
try:
|
||||
with open(filename+".tmp",'wb+') as hfile:
|
||||
hfile.write(data.encode("utf-8"))
|
||||
|
||||
19
azurelinuxagent/future.py
Normal file
19
azurelinuxagent/future.py
Normal file
@@ -0,0 +1,19 @@
|
||||
import sys
|
||||
|
||||
"""
|
||||
Add alies for python2 and python3 libs and fucntions.
|
||||
"""
|
||||
|
||||
if sys.version_info[0]== 3:
|
||||
import http.client as httpclient
|
||||
from urllib.parse import urlparse
|
||||
text = str
|
||||
bytebuffer = memoryview
|
||||
elif sys.version_info[0] == 2:
|
||||
import httplib as httpclient
|
||||
from urlparse import urlparse
|
||||
text = unicode
|
||||
bytebuffer = buffer
|
||||
else:
|
||||
raise ImportError("Unknown python version:".format(sys.version_info))
|
||||
|
||||
@@ -20,10 +20,11 @@
|
||||
import os
|
||||
import re
|
||||
import platform
|
||||
from azurelinuxagent.future import text
|
||||
|
||||
def get_distro():
|
||||
if 'FreeBSD' in platform.system():
|
||||
release = re.sub('\-.*\Z', '', str(platform.release()))
|
||||
release = re.sub('\-.*\Z', '', text(platform.release()))
|
||||
osinfo = ['freebsd', release, '', 'freebsd']
|
||||
if 'linux_distribution' in dir(platform):
|
||||
osinfo = list(platform.linux_distribution(full_distribution_name=0))
|
||||
|
||||
@@ -23,6 +23,7 @@ import os
|
||||
import re
|
||||
import xml.dom.minidom as minidom
|
||||
import azurelinuxagent.logger as logger
|
||||
from azurelinuxagent.future import text
|
||||
import azurelinuxagent.utils.fileutil as fileutil
|
||||
from azurelinuxagent.utils.textutil import parse_doc, findall, find, findtext
|
||||
from azurelinuxagent.utils.osutil import OSUTIL, OSUtilError
|
||||
@@ -59,9 +60,9 @@ def copy_ovf_env():
|
||||
fileutil.write_file(ovf_file_path, ovfxml)
|
||||
OSUTIL.umount_dvd()
|
||||
except IOError as e:
|
||||
raise ProtocolError(str(e))
|
||||
raise ProtocolError(text(e))
|
||||
except OSUtilError as e:
|
||||
raise ProtocolError(str(e))
|
||||
raise ProtocolError(text(e))
|
||||
return ovfenv
|
||||
|
||||
def _validate_ovf(val, msg):
|
||||
|
||||
@@ -20,6 +20,7 @@ import os
|
||||
import traceback
|
||||
import threading
|
||||
import azurelinuxagent.logger as logger
|
||||
from azurelinuxagent.future import text
|
||||
import azurelinuxagent.utils.fileutil as fileutil
|
||||
from azurelinuxagent.utils.osutil import OSUTIL
|
||||
from azurelinuxagent.protocol.common import *
|
||||
@@ -64,7 +65,7 @@ def detect_available_protocols(prob_funcs=[detect_wire_protocol,
|
||||
protocol = probe_func()
|
||||
available_protocols.append(protocol)
|
||||
except ProtocolNotFound as e:
|
||||
logger.info(str(e))
|
||||
logger.info(text(e))
|
||||
return available_protocols
|
||||
|
||||
def detect_default_protocol():
|
||||
@@ -92,7 +93,7 @@ def get_available_protocols(getters=[get_wire_protocol, get_metadata_protocol]):
|
||||
protocol = getter()
|
||||
available_protocols.append(protocol)
|
||||
except ProtocolNotFound as e:
|
||||
logger.info(str(e))
|
||||
logger.info(text(e))
|
||||
return available_protocols
|
||||
|
||||
class ProtocolFactory(object):
|
||||
|
||||
@@ -21,10 +21,10 @@ import json
|
||||
import re
|
||||
import time
|
||||
import traceback
|
||||
import http.client
|
||||
import xml.sax.saxutils as saxutils
|
||||
import xml.etree.ElementTree as ET
|
||||
import azurelinuxagent.logger as logger
|
||||
from azurelinuxagent.future import text, httpclient
|
||||
import azurelinuxagent.utils.restutil as restutil
|
||||
from azurelinuxagent.utils.textutil import parse_doc, findall, find, findtext, \
|
||||
getattrib, gettext
|
||||
@@ -116,13 +116,13 @@ def _fetch_uri(uri, headers, chk_proxy=False):
|
||||
try:
|
||||
resp = restutil.http_get(uri, headers, chk_proxy=chk_proxy)
|
||||
except restutil.HttpError as e:
|
||||
raise ProtocolError(str(e))
|
||||
raise ProtocolError(text(e))
|
||||
|
||||
if(resp.status == http.client.GONE):
|
||||
if(resp.status == httpclient.GONE):
|
||||
raise WireProtocolResourceGone(uri)
|
||||
if(resp.status != http.client.OK):
|
||||
if(resp.status != httpclient.OK):
|
||||
raise ProtocolError("{0} - {1}".format(resp.status, uri))
|
||||
return str(resp.read(), encoding='utf-8')
|
||||
return text(resp.read(), encoding='utf-8')
|
||||
|
||||
def _fetch_manifest(version_uris):
|
||||
for version_uri in version_uris:
|
||||
@@ -302,7 +302,7 @@ class StatusBlob(object):
|
||||
"x-ms-date" : timestamp,
|
||||
'x-ms-version' : self.__class__.__storage_version__
|
||||
})
|
||||
if resp is None or resp.status != http.client.OK:
|
||||
if resp is None or resp.status != httpclient.OK:
|
||||
raise ProtocolError(("Failed to get status blob type: {0}"
|
||||
"").format(resp.status))
|
||||
|
||||
@@ -316,10 +316,10 @@ class StatusBlob(object):
|
||||
resp = restutil.http_put(url, data, {
|
||||
"x-ms-date" : timestamp,
|
||||
"x-ms-blob-type" : "BlockBlob",
|
||||
"Content-Length": str(len(data)),
|
||||
"Content-Length": text(len(data)),
|
||||
"x-ms-version" : self.__class__.__storage_version__
|
||||
})
|
||||
if resp is None or resp.status != http.client.CREATED:
|
||||
if resp is None or resp.status != httpclient.CREATED:
|
||||
raise ProtocolError(("Failed to upload block blob: {0}"
|
||||
"").format(resp.status))
|
||||
|
||||
@@ -332,10 +332,10 @@ class StatusBlob(object):
|
||||
"x-ms-date" : timestamp,
|
||||
"x-ms-blob-type" : "PageBlob",
|
||||
"Content-Length": "0",
|
||||
"x-ms-blob-content-length" : str(page_blob_size),
|
||||
"x-ms-blob-content-length" : text(page_blob_size),
|
||||
"x-ms-version" : self.__class__.__storage_version__
|
||||
})
|
||||
if resp is None or resp.status != http.client.CREATED:
|
||||
if resp is None or resp.status != httpclient.CREATED:
|
||||
raise ProtocolError(("Failed to clean up page blob: {0}"
|
||||
"").format(resp.status))
|
||||
|
||||
@@ -361,9 +361,9 @@ class StatusBlob(object):
|
||||
"x-ms-range" : "bytes={0}-{1}".format(start, page_end - 1),
|
||||
"x-ms-page-write" : "update",
|
||||
"x-ms-version" : self.__class__.__storage_version__,
|
||||
"Content-Length": str(page_end - start)
|
||||
"Content-Length": text(page_end - start)
|
||||
})
|
||||
if resp is None or resp.status != http.client.CREATED:
|
||||
if resp is None or resp.status != httpclient.CREATED:
|
||||
raise ProtocolError(("Failed to upload page blob: {0}"
|
||||
"").format(resp.status))
|
||||
start = end
|
||||
@@ -376,13 +376,13 @@ def event_param_to_v1(param):
|
||||
attr_type = 'mt:uint64'
|
||||
elif param_type is str:
|
||||
attr_type = 'mt:wstr'
|
||||
elif str(param_type).count("'unicode'") > 0:
|
||||
elif text(param_type).count("'unicode'") > 0:
|
||||
attr_type = 'mt:wstr'
|
||||
elif param_type is bool:
|
||||
attr_type = 'mt:bool'
|
||||
elif param_type is float:
|
||||
attr_type = 'mt:float64'
|
||||
return param_format.format(param.name, saxutils.quoteattr(str(param.value)),
|
||||
return param_format.format(param.name, saxutils.quoteattr(text(param.value)),
|
||||
attr_type)
|
||||
|
||||
def event_to_v1(event):
|
||||
@@ -581,7 +581,7 @@ class WireClient(object):
|
||||
except restutil.HttpError as e:
|
||||
raise ProtocolError("Failed to send events:{0}".format(e))
|
||||
|
||||
if resp.status != http.client.OK:
|
||||
if resp.status != httpclient.OK:
|
||||
logger.verb(resp.read())
|
||||
raise ProtocolError("Failed to send events:{0}".format(resp.status))
|
||||
|
||||
|
||||
@@ -16,8 +16,8 @@
|
||||
#
|
||||
# Requires Python 2.4+ and Openssl 1.0+
|
||||
|
||||
import http.client
|
||||
import json
|
||||
from azurelinuxagent.future import httpclient, text
|
||||
import azurelinuxagent.utils.restutil as restutil
|
||||
from azurelinuxagent.protocol.common import *
|
||||
|
||||
@@ -55,14 +55,14 @@ class MetadataProtocol(Protocol):
|
||||
try:
|
||||
resp = restutil.http_get(url, headers=headers)
|
||||
except restutil.HttpError as e:
|
||||
raise ProtocolError(str(e))
|
||||
raise ProtocolError(text(e))
|
||||
|
||||
if resp.status != http.client.OK:
|
||||
if resp.status != httpclient.OK:
|
||||
raise ProtocolError("{0} - GET: {1}".format(resp.status, url))
|
||||
try:
|
||||
data = json.loads(resp.read())
|
||||
except ValueError as e:
|
||||
raise ProtocolError(str(e))
|
||||
raise ProtocolError(text(e))
|
||||
obj = data_type()
|
||||
set_properties(obj, data)
|
||||
return obj
|
||||
@@ -73,8 +73,8 @@ class MetadataProtocol(Protocol):
|
||||
try:
|
||||
resp = restutil.http_put(url, json.dumps(data), headers=headers)
|
||||
except restutil.HttpError as e:
|
||||
raise ProtocolError(str(e))
|
||||
if resp.status != http.client.OK:
|
||||
raise ProtocolError(text(e))
|
||||
if resp.status != httpclient.OK:
|
||||
raise ProtocolError("{0} - PUT: {1}".format(resp.status, url))
|
||||
|
||||
def _post_data(self, url, obj, headers=None):
|
||||
@@ -83,8 +83,8 @@ class MetadataProtocol(Protocol):
|
||||
try:
|
||||
resp = restutil.http_post(url, json.dumps(data), headers=headers)
|
||||
except restutil.HttpError as e:
|
||||
raise ProtocolError(str(e))
|
||||
if resp.status != http.client.CREATED:
|
||||
raise ProtocolError(text(e))
|
||||
if resp.status != httpclient.CREATED:
|
||||
raise ProtocolError("{0} - POST: {1}".format(resp.status, url))
|
||||
|
||||
def initialize(self):
|
||||
|
||||
@@ -27,6 +27,7 @@ import shutil
|
||||
import pwd
|
||||
import tempfile
|
||||
import azurelinuxagent.logger as logger
|
||||
from azurelinuxagent.future import text
|
||||
import azurelinuxagent.utils.textutil as textutil
|
||||
|
||||
def read_file(filepath, asbin=False, remove_bom=False):
|
||||
@@ -51,7 +52,7 @@ def write_file(filepath, contents, asbin=False):
|
||||
else:
|
||||
mode = 'w'
|
||||
if type(contents) != str:
|
||||
contents = str(contents)
|
||||
contents = text(contents)
|
||||
with open(filepath, mode) as out_file:
|
||||
out_file.write(contents)
|
||||
|
||||
@@ -64,7 +65,7 @@ def append_file(filepath, contents, asbin=False):
|
||||
else:
|
||||
mode = 'a'
|
||||
if type(contents) != str:
|
||||
contents = str(contents)
|
||||
contents = text(contents)
|
||||
with open(filepath, mode) as out_file:
|
||||
out_file.write(contents)
|
||||
|
||||
|
||||
@@ -17,18 +17,18 @@
|
||||
# Requires Python 2.4+ and Openssl 1.0+
|
||||
#
|
||||
|
||||
import time
|
||||
import platform
|
||||
import os
|
||||
import subprocess
|
||||
import azurelinuxagent.logger as logger
|
||||
import azurelinuxagent.conf as conf
|
||||
import http.client
|
||||
import time
|
||||
from urllib.parse import urlparse
|
||||
from azurelinuxagent.future import httpclient, urlparse
|
||||
|
||||
"""
|
||||
REST api util functions
|
||||
"""
|
||||
|
||||
RETRY_WAITING_INTERVAL = 10
|
||||
|
||||
class HttpError(Exception):
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
import platform
|
||||
import os
|
||||
import subprocess
|
||||
from azurelinuxagent.future import text
|
||||
import azurelinuxagent.logger as logger
|
||||
|
||||
if not hasattr(subprocess,'check_output'):
|
||||
@@ -79,6 +80,6 @@ def run_get_output(cmd, chk_err=True):
|
||||
logger.error("Error Code:{0}", e.returncode)
|
||||
logger.error("Result:{0}", e.output[:-1].decode('latin-1'))
|
||||
return e.returncode, e.output.decode('latin-1')
|
||||
return 0, str(output, encoding="utf-8")
|
||||
return 0, text(output, encoding="utf-8")
|
||||
|
||||
#End shell command util functions
|
||||
|
||||
@@ -185,18 +185,6 @@ def int_to_ip4_addr(a):
|
||||
(a >> 8) & 0xFF,
|
||||
(a) & 0xFF)
|
||||
|
||||
def ascii(val):
|
||||
uni = None
|
||||
if type(val) == str:
|
||||
pass
|
||||
#uni = str(val, 'utf-8', errors='ignore')
|
||||
else:
|
||||
uni = str(val)
|
||||
if uni is None:
|
||||
raise ValueError('<Unsupported charset>')
|
||||
else:
|
||||
return uni.encode('ascii', 'backslashreplace')
|
||||
|
||||
def hexstr_to_bytearray(a):
|
||||
"""
|
||||
Return hex string packed into a binary struct.
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
# http://msdn.microsoft.com/en-us/library/cc227282%28PROT.10%29.aspx
|
||||
# http://msdn.microsoft.com/en-us/library/cc227259%28PROT.13%29.aspx
|
||||
|
||||
from . import env
|
||||
import tests.env
|
||||
import tests.tools as tools
|
||||
import uuid
|
||||
import unittest
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
# http://msdn.microsoft.com/en-us/library/cc227282%28PROT.10%29.aspx
|
||||
# http://msdn.microsoft.com/en-us/library/cc227259%28PROT.13%29.aspx
|
||||
|
||||
from . import env
|
||||
import tests.env
|
||||
from .tools import *
|
||||
import uuid
|
||||
import unittest
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
# http://msdn.microsoft.com/en-us/library/cc227259%28PROT.13%29.aspx
|
||||
|
||||
import os
|
||||
from . import env
|
||||
import tests.env
|
||||
import uuid
|
||||
import unittest
|
||||
import tests.tools as tools
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
# http://msdn.microsoft.com/en-us/library/cc227282%28PROT.10%29.aspx
|
||||
# http://msdn.microsoft.com/en-us/library/cc227259%28PROT.13%29.aspx
|
||||
|
||||
from . import env
|
||||
import tests.env
|
||||
from tests.tools import *
|
||||
import unittest
|
||||
import azurelinuxagent.distro.default.deprovision as deprovision_handler
|
||||
|
||||
@@ -18,8 +18,8 @@
|
||||
# http://msdn.microsoft.com/en-us/library/cc227282%28PROT.10%29.aspx
|
||||
# http://msdn.microsoft.com/en-us/library/cc227259%28PROT.13%29.aspx
|
||||
|
||||
from . import env
|
||||
from .tools import *
|
||||
import tests.env as env
|
||||
from tests.tools import *
|
||||
import uuid
|
||||
import unittest
|
||||
import os
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
# http://msdn.microsoft.com/en-us/library/cc227282%28PROT.10%29.aspx
|
||||
# http://msdn.microsoft.com/en-us/library/cc227259%28PROT.13%29.aspx
|
||||
|
||||
from . import env
|
||||
import tests.env
|
||||
from tests.tools import *
|
||||
import unittest
|
||||
from azurelinuxagent.utils.osutil import OSUTIL, OSUtilError
|
||||
|
||||
@@ -18,10 +18,11 @@
|
||||
# http://msdn.microsoft.com/en-us/library/cc227282%28PROT.10%29.aspx
|
||||
# http://msdn.microsoft.com/en-us/library/cc227259%28PROT.13%29.aspx
|
||||
|
||||
from . import env
|
||||
import tests.env
|
||||
from tests.tools import *
|
||||
import unittest
|
||||
import time
|
||||
from azurelinuxagent.future import text
|
||||
from azurelinuxagent.utils.osutil import OSUTIL
|
||||
from azurelinuxagent.distro.default.env import EnvMonitor
|
||||
|
||||
@@ -33,7 +34,7 @@ def mock_get_dhcp_pid():
|
||||
return "1234"
|
||||
|
||||
def mock_dhcp_pid_change():
|
||||
return str(time.time())
|
||||
return text(time.time())
|
||||
|
||||
class TestEnvMonitor(unittest.TestCase):
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
# http://msdn.microsoft.com/en-us/library/cc227282%28PROT.10%29.aspx
|
||||
# http://msdn.microsoft.com/en-us/library/cc227259%28PROT.13%29.aspx
|
||||
|
||||
from . import env
|
||||
import tests.env
|
||||
from .tools import *
|
||||
import uuid
|
||||
import unittest
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
# http://msdn.microsoft.com/en-us/library/cc227282%28PROT.10%29.aspx
|
||||
# http://msdn.microsoft.com/en-us/library/cc227259%28PROT.13%29.aspx
|
||||
|
||||
from . import env
|
||||
import tests.env
|
||||
from tests.tools import *
|
||||
import uuid
|
||||
import unittest
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
# http://msdn.microsoft.com/en-us/library/cc227282%28PROT.10%29.aspx
|
||||
# http://msdn.microsoft.com/en-us/library/cc227259%28PROT.13%29.aspx
|
||||
|
||||
from . import env
|
||||
import tests.env
|
||||
import tests.tools as tools
|
||||
import uuid
|
||||
import unittest
|
||||
|
||||
@@ -24,12 +24,13 @@ import uuid
|
||||
import unittest
|
||||
import os
|
||||
import sys
|
||||
from azurelinuxagent.future import text
|
||||
import azurelinuxagent.utils.fileutil as fileutil
|
||||
|
||||
class TestFileOperations(unittest.TestCase):
|
||||
def test_read_write_file(self):
|
||||
test_file='/tmp/test_file'
|
||||
content = str(uuid.uuid4())
|
||||
content = text(uuid.uuid4())
|
||||
fileutil.write_file(test_file, content)
|
||||
self.assertTrue(tools.simple_file_grep(test_file, content))
|
||||
|
||||
@@ -51,7 +52,7 @@ class TestFileOperations(unittest.TestCase):
|
||||
|
||||
def test_append_file(self):
|
||||
test_file='/tmp/test_file2'
|
||||
content = str(uuid.uuid4())
|
||||
content = text(uuid.uuid4())
|
||||
fileutil.append_file(test_file, content)
|
||||
self.assertTrue(tools.simple_file_grep(test_file, content))
|
||||
os.remove(test_file)
|
||||
|
||||
37
tests/test_future.py
Normal file
37
tests/test_future.py
Normal file
@@ -0,0 +1,37 @@
|
||||
# Copyright 2014 Microsoft Corporation
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
# Requires Python 2.4+ and Openssl 1.0+
|
||||
#
|
||||
# Implements parts of RFC 2131, 1541, 1497 and
|
||||
# http://msdn.microsoft.com/en-us/library/cc227282%28PROT.10%29.aspx
|
||||
# http://msdn.microsoft.com/en-us/library/cc227259%28PROT.13%29.aspx
|
||||
|
||||
import tests.env
|
||||
from tests.tools import *
|
||||
import uuid
|
||||
import unittest
|
||||
import os
|
||||
import shutil
|
||||
import time
|
||||
import azurelinuxagent.future as future
|
||||
|
||||
class TestFuture(unittest.TestCase):
|
||||
def test_future_pkgs(self):
|
||||
future.httpclient
|
||||
future.urlparse
|
||||
future.text(b"asdf", encoding="utf-8")
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -18,7 +18,7 @@
|
||||
# http://msdn.microsoft.com/en-us/library/cc227282%28PROT.10%29.aspx
|
||||
# http://msdn.microsoft.com/en-us/library/cc227259%28PROT.13%29.aspx
|
||||
|
||||
from . import env
|
||||
import tests.env
|
||||
import tests.tools as tools
|
||||
import uuid
|
||||
import unittest
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
# http://msdn.microsoft.com/en-us/library/cc227282%28PROT.10%29.aspx
|
||||
# http://msdn.microsoft.com/en-us/library/cc227259%28PROT.13%29.aspx
|
||||
|
||||
from . import env
|
||||
import tests.env
|
||||
import tests.tools as tools
|
||||
import uuid
|
||||
import unittest
|
||||
|
||||
@@ -18,12 +18,12 @@
|
||||
# http://msdn.microsoft.com/en-us/library/cc227282%28PROT.10%29.aspx
|
||||
# http://msdn.microsoft.com/en-us/library/cc227259%28PROT.13%29.aspx
|
||||
|
||||
from . import env
|
||||
import tests.env
|
||||
import tests.tools as tools
|
||||
import uuid
|
||||
import unittest
|
||||
import azurelinuxagent.logger as logger
|
||||
import test
|
||||
from azurelinuxagent.future import text
|
||||
|
||||
class TestLogger(unittest.TestCase):
|
||||
|
||||
@@ -55,11 +55,11 @@ class TestLogger(unittest.TestCase):
|
||||
logger.LogLevel.INFO,
|
||||
'/tmp/testlog')
|
||||
|
||||
msg = str(uuid.uuid4())
|
||||
msg = text(uuid.uuid4())
|
||||
_logger.info("Test logger: {0}", msg)
|
||||
self.assertTrue(tools.simple_file_grep('/tmp/testlog', msg))
|
||||
|
||||
msg = str(uuid.uuid4())
|
||||
msg = text(uuid.uuid4())
|
||||
_logger.verbose("Verbose should not be logged: {0}", msg)
|
||||
self.assertFalse(tools.simple_file_grep('/tmp/testlog', msg))
|
||||
|
||||
@@ -73,11 +73,11 @@ class TestLogger(unittest.TestCase):
|
||||
logger.LogLevel.VERBOSE,
|
||||
'/tmp/testlog')
|
||||
|
||||
msg = str(uuid.uuid4())
|
||||
msg = text(uuid.uuid4())
|
||||
_logger.info("Test logger: {0}", msg)
|
||||
self.assertTrue(tools.simple_file_grep('/tmp/testlog', msg))
|
||||
|
||||
msg = str(uuid.uuid4())
|
||||
msg = text(uuid.uuid4())
|
||||
_logger.verbose("Test logger: {0}", msg)
|
||||
self.assertFalse(tools.simple_file_grep('/tmp/testlog', msg))
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
# http://msdn.microsoft.com/en-us/library/cc227282%28PROT.10%29.aspx
|
||||
# http://msdn.microsoft.com/en-us/library/cc227259%28PROT.13%29.aspx
|
||||
|
||||
from . import env
|
||||
import tests.env
|
||||
from tests.tools import *
|
||||
import unittest
|
||||
from azurelinuxagent.metadata import AGENT_NAME, AGENT_VERSION, \
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
# http://msdn.microsoft.com/en-us/library/cc227282%28PROT.10%29.aspx
|
||||
# http://msdn.microsoft.com/en-us/library/cc227259%28PROT.13%29.aspx
|
||||
|
||||
from . import env
|
||||
import tests.env as env
|
||||
from tests.tools import *
|
||||
import uuid
|
||||
import unittest
|
||||
@@ -158,7 +158,7 @@ class TestCurrOS(unittest.TestCase):
|
||||
'ClientAliveInterval 180'))
|
||||
|
||||
@mock(shellutil, 'run_get_output', MockFunc(retval=[0, '']))
|
||||
@mock(OSUTIL, 'get_dvd_device', MockFunc(retval='abc'))
|
||||
@mock(OSUTIL, 'get_dvd_device', MockFunc(retval=[0, 'abc']))
|
||||
@mock(OSUTIL, 'get_mount_point', MockFunc(retval='/tmp/cdrom'))
|
||||
def test_mount(self):
|
||||
OSUTIL.mount_dvd()
|
||||
@@ -166,8 +166,9 @@ class TestCurrOS(unittest.TestCase):
|
||||
mount_point = OSUTIL.get_mount_point(mount_list_sample, '/dev/sda')
|
||||
self.assertNotEquals(None, mount_point)
|
||||
|
||||
def _test_getdvd(self):
|
||||
OSUTIL.get_dvd_device()
|
||||
def test_getdvd(self):
|
||||
fileutil.write_file("/tmp/sr0", '')
|
||||
OSUTIL.get_dvd_device(dev_dir='/tmp')
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
# http://msdn.microsoft.com/en-us/library/cc227282%28PROT.10%29.aspx
|
||||
# http://msdn.microsoft.com/en-us/library/cc227259%28PROT.13%29.aspx
|
||||
|
||||
from . import env
|
||||
import tests.env
|
||||
import tests.tools as tools
|
||||
import uuid
|
||||
import unittest
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
# http://msdn.microsoft.com/en-us/library/cc227282%28PROT.10%29.aspx
|
||||
# http://msdn.microsoft.com/en-us/library/cc227259%28PROT.13%29.aspx
|
||||
|
||||
from . import env
|
||||
import tests.env
|
||||
import tests.tools as tools
|
||||
from .tools import *
|
||||
import uuid
|
||||
@@ -26,7 +26,6 @@ import unittest
|
||||
import os
|
||||
import time
|
||||
import json
|
||||
import http.client
|
||||
from azurelinuxagent.protocol.common import *
|
||||
|
||||
extensionDataStr = """
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
# http://msdn.microsoft.com/en-us/library/cc227282%28PROT.10%29.aspx
|
||||
# http://msdn.microsoft.com/en-us/library/cc227259%28PROT.13%29.aspx
|
||||
|
||||
from . import env
|
||||
import tests.env
|
||||
from tests.tools import *
|
||||
import uuid
|
||||
import unittest
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
# http://msdn.microsoft.com/en-us/library/cc227282%28PROT.10%29.aspx
|
||||
# http://msdn.microsoft.com/en-us/library/cc227259%28PROT.13%29.aspx
|
||||
|
||||
from . import env
|
||||
import tests.env
|
||||
from tests.tools import *
|
||||
import unittest
|
||||
from azurelinuxagent.distro.redhat.osutil import RedhatOSUtil
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
# http://msdn.microsoft.com/en-us/library/cc227282%28PROT.10%29.aspx
|
||||
# http://msdn.microsoft.com/en-us/library/cc227259%28PROT.13%29.aspx
|
||||
|
||||
from . import env
|
||||
import tests.env
|
||||
from tests.tools import *
|
||||
import unittest
|
||||
import azurelinuxagent.distro.default.resourceDisk as rdh
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
# http://msdn.microsoft.com/en-us/library/cc227282%28PROT.10%29.aspx
|
||||
# http://msdn.microsoft.com/en-us/library/cc227259%28PROT.13%29.aspx
|
||||
|
||||
from . import env
|
||||
import tests.env
|
||||
from tests.tools import *
|
||||
import uuid
|
||||
import unittest
|
||||
@@ -52,7 +52,7 @@ class TestHttpOperations(unittest.TestCase):
|
||||
resp = restutil.http_get("http://httpbin.org/get").read()
|
||||
self.assertNotEquals(None, resp)
|
||||
|
||||
msg = str(uuid.uuid4())
|
||||
msg = text(uuid.uuid4())
|
||||
resp = restutil.http_get("http://httpbin.org/get", {"x-abc":msg}).read()
|
||||
self.assertNotEquals(None, resp)
|
||||
self.assertTrue(msg in resp)
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
# http://msdn.microsoft.com/en-us/library/cc227282%28PROT.10%29.aspx
|
||||
# http://msdn.microsoft.com/en-us/library/cc227259%28PROT.13%29.aspx
|
||||
|
||||
from . import env
|
||||
import tests.env
|
||||
import tests.tools as tools
|
||||
import uuid
|
||||
import unittest
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
# http://msdn.microsoft.com/en-us/library/cc227282%28PROT.10%29.aspx
|
||||
# http://msdn.microsoft.com/en-us/library/cc227259%28PROT.13%29.aspx
|
||||
|
||||
from . import env
|
||||
import tests.env
|
||||
import tests.tools as tools
|
||||
import uuid
|
||||
import unittest
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
# http://msdn.microsoft.com/en-us/library/cc227282%28PROT.10%29.aspx
|
||||
# http://msdn.microsoft.com/en-us/library/cc227259%28PROT.13%29.aspx
|
||||
|
||||
from . import env
|
||||
import tests.env
|
||||
from .tools import *
|
||||
import uuid
|
||||
import unittest
|
||||
|
||||
@@ -18,14 +18,14 @@
|
||||
# http://msdn.microsoft.com/en-us/library/cc227282%28PROT.10%29.aspx
|
||||
# http://msdn.microsoft.com/en-us/library/cc227259%28PROT.13%29.aspx
|
||||
|
||||
from . import env
|
||||
import tests.env
|
||||
import tests.tools as tools
|
||||
from .tools import *
|
||||
import uuid
|
||||
import unittest
|
||||
import os
|
||||
import time
|
||||
import http.client
|
||||
from azurelinuxagent.utils.restutil import httpclient
|
||||
import azurelinuxagent.logger as logger
|
||||
import azurelinuxagent.protocol.v1 as v1
|
||||
from .test_version import VersionInfoSample
|
||||
@@ -130,8 +130,8 @@ class TestStatusBlob(unittest.TestCase):
|
||||
status_blob = v1.StatusBlob(vm_status)
|
||||
self.assertNotEquals(None, status_blob.to_json())
|
||||
|
||||
@mock(v1.restutil, 'http_put', MockFunc(retval=MockResp(http.client.CREATED)))
|
||||
@mock(v1.restutil, 'http_head', MockFunc(retval=MockResp(http.client.OK)))
|
||||
@mock(v1.restutil, 'http_put', MockFunc(retval=MockResp(httpclient.CREATED)))
|
||||
@mock(v1.restutil, 'http_head', MockFunc(retval=MockResp(httpclient.OK)))
|
||||
def test_put_page_blob(self):
|
||||
vm_status = v1.VMStatus()
|
||||
status_blob = v1.StatusBlob(vm_status)
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
# http://msdn.microsoft.com/en-us/library/cc227282%28PROT.10%29.aspx
|
||||
# http://msdn.microsoft.com/en-us/library/cc227259%28PROT.13%29.aspx
|
||||
|
||||
from . import env
|
||||
import tests.env
|
||||
import tests.tools as tools
|
||||
import uuid
|
||||
import unittest
|
||||
|
||||
Reference in New Issue
Block a user