diff --git a/.travis.yml b/.travis.yml index 045d634f78..e191081770 100644 --- a/.travis.yml +++ b/.travis.yml @@ -23,25 +23,25 @@ matrix: - >- NOSEOPTS="--verbose --with-timer" SETUPOPTS="" PYLINTOPTS="--rcfile=2.7.pylintrc" - PYLINTFILES="azurelinuxagent/common/ azurelinuxagent/daemon azurelinuxagent/agent.py azurelinuxagent/ga setup.py makepkg.py" + PYLINTFILES="azurelinuxagent/ga azurelinuxagent/common/ azurelinuxagent/daemon azurelinuxagent/agent.py setup.py makepkg.py tests" - python: 3.4 env: - >- NOSEOPTS="--verbose --with-timer" SETUPOPTS="" PYLINTOPTS="--rcfile=2.7.pylintrc" - PYLINTFILES="azurelinuxagent/common/ azurelinuxagent/daemon azurelinuxagent/agent.py azurelinuxagent/ga setup.py makepkg.py" + PYLINTFILES="azurelinuxagent/ga azurelinuxagent/common/ azurelinuxagent/daemon azurelinuxagent/agent.py setup.py makepkg.py tests" - python: 3.6 env: - >- NOSEOPTS="--verbose --with-timer" SETUPOPTS="" PYLINTOPTS="--rcfile=3.6.pylintrc" - PYLINTFILES="azurelinuxagent/common/ azurelinuxagent/daemon azurelinuxagent/agent.py azurelinuxagent/ga setup.py makepkg.py" + PYLINTFILES="azurelinuxagent/ga azurelinuxagent/common/ azurelinuxagent/daemon azurelinuxagent/agent.py setup.py makepkg.py tests" - python: 3.7 env: - >- NOSEOPTS="--verbose --with-timer" SETUPOPTS="" PYLINTOPTS="--rcfile=3.6.pylintrc" - PYLINTFILES="azurelinuxagent/common/ azurelinuxagent/daemon azurelinuxagent/agent.py azurelinuxagent/ga setup.py makepkg.py" + PYLINTFILES="azurelinuxagent/ga azurelinuxagent/common/ azurelinuxagent/daemon azurelinuxagent/agent.py setup.py makepkg.py tests" - python: 3.8 env: - >- @@ -50,7 +50,7 @@ matrix: --cover-package=azurelinuxagent --cover-xml" SETUPOPTS="" PYLINTOPTS="--rcfile=3.6.pylintrc" - PYLINTFILES="azurelinuxagent/common/ azurelinuxagent/daemon azurelinuxagent/agent.py azurelinuxagent/ga setup.py makepkg.py" + PYLINTFILES="azurelinuxagent/ga azurelinuxagent/common/ azurelinuxagent/daemon azurelinuxagent/agent.py setup.py makepkg.py tests" install: - pip install -r requirements.txt diff --git a/2.7.pylintrc b/2.7.pylintrc index 154d83b72d..57b8355c08 100644 --- a/2.7.pylintrc +++ b/2.7.pylintrc @@ -6,30 +6,39 @@ # anomalous-backslash-in-string: (hi-pri) Used when a backslash is in a literal string but not as an escape. # arguments-differ: (hi-pri) Used when a method has a different number of arguments than in the implemented interface or in an overridden method. # attribute-defined-outside-init: (hi-pri) Used when an instance attribute is defined outside the __init__ method. -# bad-continuation: (needs review) **REMOVED in pylint-2.6.0** # bad-indentation: (hi-pri) Used when an unexpected number of indentation's tabulations or spaces has been found. +# bad-open-mode: (hi-pri) Python supports: r, w, a[, x] modes with b, +, and U (only with r) options. # bad-super-call: (hi-pri) Used when another argument than the current class is given as first argument of the super builtin. # bare-except: (hi-pri) Used when an except clause doesn't specify exceptions type to catch. +# blacklisted-name: (needs review) Used when the name is listed in the black list (unauthorized names). # cell-var-from-loop: (hi-pri) A variable used in a closure is defined in a loop. This will result in all closures using the same value for the closed-over variable. # cyclic-import: (hi-pri) Used when a cyclic import between two or more modules is detected. # dangerous-default-value: (hi-pri) Used when a mutable value as list or dictionary is detected in a default value for an argument. +# deprecated-method: (hi-pri) The method is marked as deprecated and will be removed in a future version of Python. +# duplicate-key: (hi-pri) Used when a dictionary expression binds the same key multiple times. +# expression-not-assigned: (hi-pri) Used when an expression that is not a function call is assigned to nothing. # duplicate-code: (hi-pri) Indicates that a set of similar lines has been detected among multiple file. (also disabled globally) # fixme: Used when a warning note as FIXME or TODO is detected +# function-redefined: (hi-pri) Used when a function / class / method is redefined. # global-statement: Used when you use the "global" statement to update a global variable. # import-error: (hi-pri) Used when pylint has been unable to import a module. # inconsistent-return-statements: (hi-pri) if any return statement returns an expression, any return statements where no value is returned should explicitly state this as return None, and an explicit return statement should be present at the end of the function # invalid-name: (needs review) Used when the name doesn't conform to naming rules associated to its type (constant, variable, class...). +# keyword-arg-before-vararg: (hi-pri) When defining a keyword argument before variable positional arguments, one can end up in having multiple values passed for the aforementioned parameter in case the method is called with keyword arguments. # len-as-condition: (hi-pri) Used when Pylint detects that len(sequence) is being used without explicit comparison inside a condition to determine if a sequence is empty. # logging-format-interpolation: (hi-pri) Used when a logging statement has a call form of "logging.(format_string.format(format_args...))". Use another type of string formatting instead. +# multiple-imports: (hi-pri) Used when import statement importing multiple modules is detected. # no-else-return: (hi-pri) Used in order to highlight an unnecessary block of code following an if containing a return statement. # no-member: (hi-pri) Used when a variable is accessed for an unexistent member. # no-name-in-module: (hi-pri) Used when a name cannot be found in a module. # no-value-for-parameter: (hi-pri) Used when a function call passes too few arguments. +# pointless-statement: (hi-pri) Used when a statement doesn’t have (or at least seems to) any effect. # pointless-string-statement: (hi-pri) Used when a string is used as a statement (which of course has no effect). This is a particular case of W0104 with its own message so you can easily disable it if you're using those strings as documentation, instead of comments. # protected-access: Used when a protected member (i.e. class member with a name beginning with an underscore) is access outside the class or a descendant of the class where it's defined. # raising-format-tuple: (hi-pri) Used when passing multiple arguments to an exception constructor, the first of them a string literal containing what appears to be placeholders intended for formatting # redefined-builtin: (hi-pri) Used when a variable or function override a built-in. # redefined-outer-name: (hi-pri) Used when a variable's name hides a name defined in the outer scope. +# redundant-unittest-assert: (hi-pri) The first argument of assertTrue and assertFalse is a condition. If a constant is passed as parameter, that condition will be always true. # reimported: (hi-pri) Used when a module is reimported multiple times. # simplifiable-if-statement: Used when an if statement can be replaced with 'bool(test)'. # singleton-comparison: (hi-pri) Used when an expression is compared to singleton values like True, False or None. @@ -79,7 +88,8 @@ [MESSAGES CONTROL] -disable=bad-option-value, # pylint does not recognize the error code/symbol (needed to supress breaking changes across pylint versions) +disable=bad-continuation, # Buggy, **REMOVED in pylint-2.6.0** + bad-option-value, # pylint does not recognize the error code/symbol (needed to supress breaking changes across pylint versions) bad-whitespace, # Used when a wrong number of spaces is used around an operator, bracket or block opener. broad-except, # Used when an except catches a too general exception, possibly burying unrelated errors. consider-iterating-dictionary, # Emitted when the keys of a dictionary are iterated through the .keys() method. diff --git a/3.6.pylintrc b/3.6.pylintrc index e7e1a6aeb7..68b21b644c 100644 --- a/3.6.pylintrc +++ b/3.6.pylintrc @@ -7,8 +7,8 @@ # assignment-from-no-return: (hi-pri) Used when an assignment is done on a function call but the inferred function doesn't return anything. # assignment-from-none: (hi-pri) Used when an assignment is done on a function call but the inferred function returns nothing but None. # attribute-defined-outside-init: (hi-pri) Used when an instance attribute is defined outside the __init__ method. -# bad-continuation: (needs review) **REMOVED in pylint-2.6.0** # bad-indentation: (hi-pri) Used when an unexpected number of indentation's tabulations or spaces has been found. +# bad-open-mode: (hi-pri) Python supports: r, w, a[, x] modes with b, +, and U (only with r) options. # bad-super-call: (hi-pri) Used when another argument than the current class is given as first argument of the super builtin. # bare-except: (hi-pri) Used when an except clause doesn't specify exceptions type to catch. # cell-var-from-loop: (hi-pri) A variable used in a closure is defined in a loop. This will result in all closures using the same value for the closed-over variable. @@ -19,16 +19,21 @@ # cyclic-import: (hi-pri) Used when a cyclic import between two or more modules is detected. # dangerous-default-value: (hi-pri) Used when a mutable value as list or dictionary is detected in a default value for an argument. # deprecated-method: (hi-pri) The method is marked as deprecated and will be removed in a future version of Python +# duplicate-key: (hi-pri) Used when a dictionary expression binds the same key multiple times. # duplicate-code: (hi-pri) Indicates that a set of similar lines has been detected among multiple file. (also disabled globally) # duplicate-string-formatting-argument: (hi-pri) Used when we detect that a string formatting is repeating an argument instead of using named string arguments +# expression-not-assigned: (hi-pri) Used when an expression that is not a function call is assigned to nothing. # fixme: Used when a warning note as FIXME or TODO is detected +# function-redefined: (hi-pri) Used when a function / class / method is redefined. # global-statement: Used when you use the "global" statement to update a global variable. # implicit-str-concat: (hi-pri) String literals are implicitly concatenated in a literal iterable definition : maybe a comma is missing ? # import-error: (hi-pri) Used when pylint has been unable to import a module. # inconsistent-return-statements: (hi-pri) if any return statement returns an expression, any return statements where no value is returned should explicitly state this as return None, and an explicit return statement should be present at the end of the function # invalid-name: (needs review) Used when the name doesn't conform to naming rules associated to its type (constant, variable, class...). +# keyword-arg-before-vararg: (hi-pri) When defining a keyword argument before variable positional arguments, one can end up in having multiple values passed for the aforementioned parameter in case the method is called with keyword arguments. # len-as-condition: (hi-pri) Used when Pylint detects that len(sequence) is being used without explicit comparison inside a condition to determine if a sequence is empty. # logging-format-interpolation: (hi-pri) Used when a logging statement has a call form of "logging.(format_string.format(format_args...))". Use another type of string formatting instead. +# multiple-imports: (hi-pri) Used when import statement importing multiple modules is detected. # lost-exception: (needs review) Used when a break or a return statement is found inside the finally clause of a try...finally block: the exceptions raised in the try clause will be silently swallowed instead of being re-raised. # no-else-break: (hi-pri) Used in order to highlight an unnecessary block of code following an if containing a break statement. # no-else-continue: (hi-pri) Used in order to highlight an unnecessary block of code following an if containing a continue statement. @@ -41,6 +46,7 @@ # raising-format-tuple: (hi-pri) Used when passing multiple arguments to an exception constructor, the first of them a string literal containing what appears to be placeholders intended for formatting # redefined-builtin: (hi-pri) Used when a variable or function override a built-in. # redefined-outer-name: (hi-pri) Used when a variable's name hides a name defined in the outer scope. +# redundant-unittest-assert: (hi-pri) The first argument of assertTrue and assertFalse is a condition. If a constant is passed as parameter, that condition will be always true. # reimported: (hi-pri) Used when a module is reimported multiple times. # signature-differs: (hi-pri) Used when a method signature is different than in the implemented interface or in an overridden method. # simplifiable-if-expression: Used when an if expression can be replaced with 'bool(test)'. @@ -49,6 +55,7 @@ # subprocess-popen-preexec-fn: (hi-pri) The preexec_fn parameter is not safe to use in the presence of threads in your application. The child process could deadlock before exec is called. # super-init-not-called: (hi-pri) Used when an ancestor class method has an __init__ method which is not called by a derived class. # too-few-public-methods: Used when class has too few public methods, so be sure it's really worth it. +# too-many-ancestors: (hi-pri) Used when class has too many parent classes, try to reduce this to get a simpler (and so easier to use) class. # too-many-arguments: Used when a function or method takes too many arguments. # too-many-branches: Used when a function or method has too many branches, making it hard to follow. # too-many-instance-attributes: Used when class has too many instance attributes, try to reduce this to get a simpler (and so easier to use) class. @@ -60,6 +67,7 @@ # undefined-variable: (hi-pri) Used when an undefined variable is accessed. # ungrouped-imports: (needs review) Used when imports are not grouped by packages # unidiomatic-typecheck: (hi-pri) The idiomatic way to perform an explicit typecheck in Python is to use isinstance(x, Y) rather than type(x) == Y, type(x) is Y. +# unnecessary-comprehension: (hi-pri) Instead of using an identity comprehension, consider using the list, dict or set constructor. # unnecessary-lambda: Used when the body of a lambda expression is a function call on the same argument list as the lambda itself # unnecessary-pass: (hi-pri) Used when a "pass" statement that can be avoided is encountered. # unnecessary-semicolon: Used when a statement is ended by a semi-colon (";"), which isn't necessary @@ -67,6 +75,7 @@ # unused-argument: (hi-pri) Used when a function or method argument is not used. # unused-import: (hi-pri) Used when an imported module or variable is not used. # unused-variable: (hi-pri) Used when a variable is defined but not used. +# used-before-assignment: (hi-pri) Used when a local variable is accessed before its assignment. # useless-else-on-loop: (hi-pri) Loops should only have an else clause if they can exit early with a break statement, otherwise the statements under else should be on the same scope as the loop itself. # useless-import-alias: (hi-pri) Used when an import alias is same as original package.e.g using import numpy as numpy instead of import numpy as np # useless-return: (hi-pri) Emitted when a single "return" or "return None" statement is found at the end of function or method definition. @@ -76,7 +85,8 @@ [MESSAGES CONTROL] -disable=bad-whitespace, # Used when a wrong number of spaces is used around an operator, bracket or block opener. **REMOVED IN pylint-2.6.0** +disable=bad-continuation, # Buggy, **REMOVED in pylint-2.6.0** + bad-whitespace, # Used when a wrong number of spaces is used around an operator, bracket or block opener. **REMOVED IN pylint-2.6.0** broad-except, # Used when an except catches a too general exception, possibly burying unrelated errors. consider-iterating-dictionary, # Emitted when the keys of a dictionary are iterated through the .keys() method. consider-using-dict-comprehension, # Although there is nothing syntactically wrong with this code, it is hard to read and can be simplified to a dict comprehension. diff --git a/azurelinuxagent/agent.py b/azurelinuxagent/agent.py index 96c42f5d63..733362c0d0 100644 --- a/azurelinuxagent/agent.py +++ b/azurelinuxagent/agent.py @@ -203,7 +203,7 @@ def parse_args(sys_args): # pylint: disable=R0912 conf_file_path = m.group(1) if not os.path.exists(conf_file_path): print("Error: Configuration file {0} does not exist".format( - conf_file_path), file=sys.stderr) # pylint: disable=C0330 + conf_file_path), file=sys.stderr) usage() sys.exit(1) @@ -256,7 +256,7 @@ def usage(): """ s = "\n" # pylint: disable=C0103 s += ("usage: {0} [-verbose] [-force] [-help] " # pylint: disable=C0103 - "-configuration-path:" # pylint: disable=C0330 + "-configuration-path:" "-deprovision[+user]|-register-service|-version|-daemon|-start|" "-run-exthandlers|-show-configuration]" "").format(sys.argv[0]) diff --git a/azurelinuxagent/common/cgroupapi.py b/azurelinuxagent/common/cgroupapi.py index 4a0f88fd2f..a51bdaeea2 100644 --- a/azurelinuxagent/common/cgroupapi.py +++ b/azurelinuxagent/common/cgroupapi.py @@ -225,9 +225,9 @@ def cgroup_path(tail=""): if not os.path.exists(path): fileutil.mkdir(path) osutil.mount(device='cgroup_root', - mount_point=path, # pylint: disable=C0330 - option="-t tmpfs", # pylint: disable=C0330 - chk_err=False) # pylint: disable=C0330 + mount_point=path, + option="-t tmpfs", + chk_err=False) elif not os.path.isdir(cgroup_path()): logger.error("Could not mount cgroups: ordinary file at {0}", path) return @@ -241,9 +241,9 @@ def cgroup_path(tail=""): if not os.path.exists(target_path): fileutil.mkdir(target_path) osutil.mount(device=controller, - mount_point=target_path, # pylint: disable=C0330 - option="-t cgroup -o {0}".format(controller), # pylint: disable=C0330 - chk_err=False) # pylint: disable=C0330 + mount_point=target_path, + option="-t cgroup -o {0}".format(controller), + chk_err=False) if controller == 'cpu,cpuacct': cpu_mounted = True except Exception as exception: diff --git a/azurelinuxagent/common/cgroupconfigurator.py b/azurelinuxagent/common/cgroupconfigurator.py index b4c9d2b85c..30a70f0286 100644 --- a/azurelinuxagent/common/cgroupconfigurator.py +++ b/azurelinuxagent/common/cgroupconfigurator.py @@ -274,14 +274,14 @@ def start_extension_command(self, extension_name, command, timeout, shell, cwd, error_code=error_code) else: process_output = self._cgroups_api.start_extension_command(extension_name, - command, # pylint: disable=C0330 - timeout, # pylint: disable=C0330 - shell=shell, # pylint: disable=C0330 - cwd=cwd, # pylint: disable=C0330 - env=env, # pylint: disable=C0330 - stdout=stdout, # pylint: disable=C0330 - stderr=stderr, # pylint: disable=C0330 - error_code=error_code) # pylint: disable=C0330 + command, + timeout, + shell=shell, + cwd=cwd, + env=env, + stdout=stdout, + stderr=stderr, + error_code=error_code) return process_output diff --git a/azurelinuxagent/common/conf.py b/azurelinuxagent/common/conf.py index 931ec73e6b..766b25bffc 100644 --- a/azurelinuxagent/common/conf.py +++ b/azurelinuxagent/common/conf.py @@ -285,12 +285,12 @@ def get_ssh_key_glob(conf=__conf__): def get_ssh_key_private_path(conf=__conf__): return os.path.join(get_ssh_dir(conf), - 'ssh_host_{0}_key'.format(get_ssh_host_keypair_type(conf))) # pylint: disable=C0330 + 'ssh_host_{0}_key'.format(get_ssh_host_keypair_type(conf))) def get_ssh_key_public_path(conf=__conf__): return os.path.join(get_ssh_dir(conf), - 'ssh_host_{0}_key.pub'.format(get_ssh_host_keypair_type(conf))) # pylint: disable=C0330 + 'ssh_host_{0}_key.pub'.format(get_ssh_host_keypair_type(conf))) def get_root_device_scsi_timeout(conf=__conf__): diff --git a/azurelinuxagent/common/event.py b/azurelinuxagent/common/event.py index e55fe6f7f4..43305f0153 100644 --- a/azurelinuxagent/common/event.py +++ b/azurelinuxagent/common/event.py @@ -185,7 +185,7 @@ def _save(self): __event_status__ = EventStatus() __event_status_operations__ = [ - WALAEventOperation.ReportStatus # pylint: disable=bad-continuation + WALAEventOperation.ReportStatus ] @@ -547,7 +547,7 @@ def add_common_event_parameters(self, event, event_timestamp): # Currently only the GuestAgentExtensionEvents has these columns, the other tables dont have them so skipping # this data in those tables. common_params.extend([TelemetryEventParam(GuestAgentExtensionEventsSchema.ExtensionType, event.file_type), - TelemetryEventParam(GuestAgentExtensionEventsSchema.IsInternal, False)]) # pylint: disable=C0330 + TelemetryEventParam(GuestAgentExtensionEventsSchema.IsInternal, False)]) event.parameters.extend(common_params) event.parameters.extend(self._common_parameters) diff --git a/azurelinuxagent/common/osutil/default.py b/azurelinuxagent/common/osutil/default.py index 48152b73bb..f992185fd9 100644 --- a/azurelinuxagent/common/osutil/default.py +++ b/azurelinuxagent/common/osutil/default.py @@ -308,11 +308,11 @@ def _correct_instance_id(id): # pylint: disable=W0622,C0103 parts = id.split('-') return '-'.join([ - textutil.swap_hexstring(parts[0], width=2), # pylint: disable=C0330 - textutil.swap_hexstring(parts[1], width=2), # pylint: disable=C0330 - textutil.swap_hexstring(parts[2], width=2), # pylint: disable=C0330 - parts[3], # pylint: disable=C0330 - parts[4] # pylint: disable=C0330 + textutil.swap_hexstring(parts[0], width=2), + textutil.swap_hexstring(parts[1], width=2), + textutil.swap_hexstring(parts[2], width=2), + parts[3], + parts[4] ]) def is_current_instance_id(self, id_that): @@ -821,7 +821,7 @@ def _build_route_list(proc_net_route): # pylint: disable=R0914 route = entry.split("\t") if len(route) > 0: # pylint: disable=len-as-condition route_obj = RouteEntry(route[idx_iface], route[idx_dest], route[idx_gw], route[idx_mask], - route[idx_flags], route[idx_metric]) # pylint: disable=C0330 + route[idx_flags], route[idx_metric]) route_list.append(route_obj) return route_list diff --git a/azurelinuxagent/common/osutil/nsbsd.py b/azurelinuxagent/common/osutil/nsbsd.py index 17795beffc..f2a3d4e6ea 100644 --- a/azurelinuxagent/common/osutil/nsbsd.py +++ b/azurelinuxagent/common/osutil/nsbsd.py @@ -102,7 +102,7 @@ def deploy_ssh_pubkey(self, username, pubkey): #overide parameters super(NSBSDOSUtil, self).deploy_ssh_pubkey('admin', - ["/usr/Firewall/.ssh/authorized_keys", thumbprint, value]) # pylint: disable=C0330 + ["/usr/Firewall/.ssh/authorized_keys", thumbprint, value]) def del_root_password(self): logger.warn("Root password deletion disabled") diff --git a/azurelinuxagent/common/osutil/openbsd.py b/azurelinuxagent/common/osutil/openbsd.py index ad2e933885..3eed42d507 100644 --- a/azurelinuxagent/common/osutil/openbsd.py +++ b/azurelinuxagent/common/osutil/openbsd.py @@ -243,9 +243,9 @@ def mount_dvd(self, # pylint: disable=R0913 for retry in range(0, max_retry): retcode = self.mount(dvd_device, - mount_point, # pylint: disable=C0330 - option="-o ro -t udf", # pylint: disable=C0330 - chk_err=False) # pylint: disable=C0330 + mount_point, + option="-o ro -t udf", + chk_err=False) if retcode == 0: logger.info("Successfully mounted DVD") return diff --git a/azurelinuxagent/common/protocol/goal_state.py b/azurelinuxagent/common/protocol/goal_state.py index 5d5a22b9c1..ec5041f494 100644 --- a/azurelinuxagent/common/protocol/goal_state.py +++ b/azurelinuxagent/common/protocol/goal_state.py @@ -351,7 +351,7 @@ def _parse_plugin_settings(ext_handler, plugin_settings): # pylint: disable=R091 if len(settings) != len(ext_handler_plugin_settings): msg = "ExtHandler PluginSettings Version Mismatch! Expected PluginSettings version: {0} for Handler: " \ "{1} but found versions: ({2})".format(version, name, ', '.join( - set([getattrib(x, "version") for x in ext_handler_plugin_settings]))) # pylint: disable=C0330 + set([getattrib(x, "version") for x in ext_handler_plugin_settings]))) add_event(AGENT_NAME, op=WALAEventOperation.PluginSettingsVersionMismatch, message=msg, log_event=False, is_success=False) if len(settings) == 0: # pylint: disable=len-as-condition diff --git a/azurelinuxagent/common/protocol/imds.py b/azurelinuxagent/common/protocol/imds.py index fe41f72893..3625fe65ad 100644 --- a/azurelinuxagent/common/protocol/imds.py +++ b/azurelinuxagent/common/protocol/imds.py @@ -288,11 +288,11 @@ def _get_metadata_from_endpoint(self, endpoint, resource_path, headers): # pylin if resp.status >= 500: return IMDS_INTERNAL_SERVER_ERROR, "IMDS error in /metadata/{0}: {1}".format( - resource_path, restutil.read_response_error(resp)) # pylint: disable=C0330 + resource_path, restutil.read_response_error(resp)) if restutil.request_failed(resp): return IMDS_RESPONSE_ERROR, "IMDS error in /metadata/{0}: {1}".format( - resource_path, restutil.read_response_error(resp)) # pylint: disable=C0330 + resource_path, restutil.read_response_error(resp)) return IMDS_RESPONSE_SUCCESS, resp.read() diff --git a/azurelinuxagent/common/rdma.py b/azurelinuxagent/common/rdma.py index 1c13e6b51a..1528961c80 100644 --- a/azurelinuxagent/common/rdma.py +++ b/azurelinuxagent/common/rdma.py @@ -362,7 +362,7 @@ def wait_rdma_device(path, timeout_sec, check_interval_sec): def wait_any_rdma_device(dir, timeout_sec, check_interval_sec): # pylint: disable=W0622 logger.info( "RDMA: waiting for any Infiniband device at directory={0} timeout={1}s".format( - dir, timeout_sec)) # pylint: disable=C0330 + dir, timeout_sec)) total_retries = timeout_sec/check_interval_sec n = 0 # pylint: disable=C0103 while n < total_retries: diff --git a/azurelinuxagent/common/utils/flexible_version.py b/azurelinuxagent/common/utils/flexible_version.py index b058de7e9a..603a3f6eb6 100644 --- a/azurelinuxagent/common/utils/flexible_version.py +++ b/azurelinuxagent/common/utils/flexible_version.py @@ -196,10 +196,10 @@ def _compile_pattern(self): # pylint: disable=R1711 tags = '|'.join(re.escape(tag) for tag in self.prerel_tags) self.prerel_tags_set = dict(zip(self.prerel_tags, range(len(self.prerel_tags)))) release_re = '(?:{prerel_sep}(?P<{tn}>{tags})(?P<{nn}>\d*))?'.format( # pylint: disable=W1401 - prerel_sep=self._re_prerel_sep, # pylint: disable=C0330 - tags=tags, # pylint: disable=C0330 - tn=self._nn_prerel_tag, # pylint: disable=C0330 - nn=self._nn_prerel_num) # pylint: disable=C0330 + prerel_sep=self._re_prerel_sep, + tags=tags, + tn=self._nn_prerel_tag, + nn=self._nn_prerel_num) else: release_re = '' diff --git a/azurelinuxagent/common/utils/networkutil.py b/azurelinuxagent/common/utils/networkutil.py index df4a05f050..df9bb274dd 100644 --- a/azurelinuxagent/common/utils/networkutil.py +++ b/azurelinuxagent/common/utils/networkutil.py @@ -51,7 +51,7 @@ def mask_quad(self): def to_json(self): f = '{{"Iface": "{0}", "Destination": "{1}", "Gateway": "{2}", "Mask": "{3}", "Flags": "{4:#06x}", "Metric": "{5}"}}' # pylint: disable=C0103 return f.format(self.interface, self.destination_quad(), self.gateway_quad(), self.mask_quad(), - self.flags, self.metric) # pylint: disable=C0330 + self.flags, self.metric) def __str__(self): f = "Iface: {0}\tDestination: {1}\tGateway: {2}\tMask: {3}\tFlags: {4:#06x}\tMetric: {5}" # pylint: disable=C0103 diff --git a/azurelinuxagent/common/utils/restutil.py b/azurelinuxagent/common/utils/restutil.py index 7ffffc28a5..0579e288ac 100644 --- a/azurelinuxagent/common/utils/restutil.py +++ b/azurelinuxagent/common/utils/restutil.py @@ -324,11 +324,11 @@ def _http_request(method, host, rel_uri, port=None, data=None, secure=False, # p def http_request(method, # pylint: disable=R0913,R0912,R0914,W0102 - url, data, headers=None, # pylint: disable=C0330 - use_proxy=False, # pylint: disable=C0330 - max_retry=DEFAULT_RETRIES, # pylint: disable=C0330 - retry_codes=RETRY_CODES, # pylint: disable=C0330 - retry_delay=DELAY_IN_SECONDS): # pylint: disable=C0330 + url, data, headers=None, + use_proxy=False, + max_retry=DEFAULT_RETRIES, + retry_codes=RETRY_CODES, + retry_delay=DELAY_IN_SECONDS): global SECURE_WARNING_EMITTED # pylint: disable=W0603 @@ -382,11 +382,11 @@ def http_request(method, # pylint: disable=R0913,R0912,R0914,W0102 delay = THROTTLE_DELAY_IN_SECONDS if was_throttled else retry_delay logger.verbose("[HTTP Retry] " - "Attempt {0} of {1} will delay {2} seconds: {3}", # pylint: disable=C0330 - attempt+1, # pylint: disable=C0330 - max_retry, # pylint: disable=C0330 - delay, # pylint: disable=C0330 - msg) # pylint: disable=C0330 + "Attempt {0} of {1} will delay {2} seconds: {3}", + attempt+1, + max_retry, + delay, + msg) time.sleep(delay) @@ -546,9 +546,9 @@ def read_response_error(resp): if resp is not None: try: result = "[HTTP Failed] [{0}: {1}] {2}".format( - resp.status, # pylint: disable=C0330 - resp.reason, # pylint: disable=C0330 - resp.read()) # pylint: disable=C0330 + resp.status, + resp.reason, + resp.read()) # this result string is passed upstream to several methods # which do a raise HttpError() or a format() of some kind; diff --git a/azurelinuxagent/common/utils/textutil.py b/azurelinuxagent/common/utils/textutil.py index ae54973531..c8cb3b8a87 100644 --- a/azurelinuxagent/common/utils/textutil.py +++ b/azurelinuxagent/common/utils/textutil.py @@ -347,10 +347,10 @@ def swap_hexstring(s, width=2): # pylint: disable=C0103 s = ('0' * (width - (len(s) % width))) + s return ''.join(reversed( - re.findall( # pylint: disable=C0330 - r'[a-f0-9]{{{0}}}'.format(width), # pylint: disable=C0330 - s, # pylint: disable=C0330 - re.IGNORECASE))) # pylint: disable=C0330 + re.findall( + r'[a-f0-9]{{{0}}}'.format(width), + s, + re.IGNORECASE))) def parse_json(json_str): diff --git a/azurelinuxagent/ga/exthandlers.py b/azurelinuxagent/ga/exthandlers.py index 12a502b1a5..5ab1a6d7f6 100644 --- a/azurelinuxagent/ga/exthandlers.py +++ b/azurelinuxagent/ga/exthandlers.py @@ -1347,10 +1347,10 @@ def update_settings(self): def create_handler_env(self): handler_env = { - HandlerEnvironment.logFolder: self.get_log_dir(), # pylint: disable=C0330 - HandlerEnvironment.configFolder: self.get_conf_dir(), # pylint: disable=C0330 - HandlerEnvironment.statusFolder: self.get_status_dir(), # pylint: disable=C0330 - HandlerEnvironment.heartbeatFile: self.get_heartbeat_file() # pylint: disable=C0330 + HandlerEnvironment.logFolder: self.get_log_dir(), + HandlerEnvironment.configFolder: self.get_conf_dir(), + HandlerEnvironment.statusFolder: self.get_status_dir(), + HandlerEnvironment.heartbeatFile: self.get_heartbeat_file() } if is_extension_telemetry_pipeline_enabled(): diff --git a/azurelinuxagent/ga/update.py b/azurelinuxagent/ga/update.py index 11eab236e8..98b5926945 100644 --- a/azurelinuxagent/ga/update.py +++ b/azurelinuxagent/ga/update.py @@ -417,7 +417,7 @@ def _emit_restart_event(self): # pylint: disable=R1711 try: if not self._is_clean_start: msg = u"Agent did not terminate cleanly: {0}".format( - fileutil.read_file(self._sentinel_file_path())) # pylint: disable=C0330 + fileutil.read_file(self._sentinel_file_path())) logger.info(msg) add_event( AGENT_NAME, @@ -599,7 +599,7 @@ def _is_version_eligible(self, version): def _load_agents(self): path = os.path.join(conf.get_lib_dir(), "{0}-*".format(AGENT_NAME)) return [GuestAgent(path=agent_dir) - for agent_dir in glob.iglob(path) if os.path.isdir(agent_dir)] # pylint: disable=C0330 + for agent_dir in glob.iglob(path) if os.path.isdir(agent_dir)] def _partition(self): return int(fileutil.read_file(self._partition_file)) @@ -698,7 +698,7 @@ def _upgrade_available(self, protocol, base_version=CURRENT_VERSION): if m.family == family and len(m.versionsManifestUris) > 0] if len(manifests) == 0: # pylint: disable=len-as-condition logger.verbose(u"Incarnation {0} has no {1} agent updates", - etag, family) # pylint: disable=C0330 + etag, family) return False pkg_list = protocol.get_vmagent_pkgs(manifests[0]) @@ -850,7 +850,7 @@ def __init__(self, path=None, pkg=None, host=None): self.mark_failure(is_fatal=os.path.isfile(self.get_agent_pkg_path())) msg = u"Agent {0} install failed with exception: {1}".format( - self.name, ustr(e)) # pylint: disable=C0330 + self.name, ustr(e)) detailed_msg = '{0} {1}'.format(msg, traceback.extract_tb(get_traceback(e))) add_event( AGENT_NAME, @@ -1050,9 +1050,9 @@ def _load_manifest(self): # pylint: disable=R1711 self.name, self.get_agent_manifest_path()) logger.verbose(u"Successfully loaded Agent {0} {1}: {2}", - self.name, # pylint: disable=C0330 - AGENT_MANIFEST_FILE, # pylint: disable=C0330 - ustr(self.manifest.data)) # pylint: disable=C0330 + self.name, + AGENT_MANIFEST_FILE, + ustr(self.manifest.data)) return def _unpack(self): # pylint: disable=R1711 @@ -1064,7 +1064,7 @@ def _unpack(self): # pylint: disable=R1711 except Exception as e: # pylint: disable=C0103 fileutil.clean_ioerror(e, - paths=[self.get_agent_dir(), self.get_agent_pkg_path()]) # pylint: disable=C0330 + paths=[self.get_agent_dir(), self.get_agent_pkg_path()]) msg = u"Exception unpacking Agent {0} from {1}: {2}".format( self.name, diff --git a/setup.py b/setup.py index 4a8c6bc08e..d19e60099d 100755 --- a/setup.py +++ b/setup.py @@ -142,7 +142,7 @@ def get_data_files(name, version, fullname): # pylint: disable=R0912 if fullname == 'SUSE Linux Enterprise Server' and \ version.startswith('11') or \ fullname == 'openSUSE' and version.startswith( - '13.1'): # pylint: disable=C0330 + '13.1'): set_sysv_files(data_files, dest='/etc/init.d', src=["init/suse/waagent"]) else: diff --git a/tests/common/dhcp/test_dhcp.py b/tests/common/dhcp/test_dhcp.py index 0b6adb0f2b..0bc6f915f9 100644 --- a/tests/common/dhcp/test_dhcp.py +++ b/tests/common/dhcp/test_dhcp.py @@ -48,7 +48,7 @@ def test_wireserver_route_exists(self): "00FCFFFF 0 0 0 \n" with patch("os.path.exists", return_value=True): - mo = mock.mock_open(read_data=routing_table) + mo = mock.mock_open(read_data=routing_table) # pylint: disable=invalid-name with patch(open_patch(), mo): self.assertTrue(dhcp_handler.wireserver_route_exists) diff --git a/tests/common/mock_cgroup_commands.py b/tests/common/mock_cgroup_commands.py index 1267f91d04..41d2c5bdf5 100644 --- a/tests/common/mock_cgroup_commands.py +++ b/tests/common/mock_cgroup_commands.py @@ -28,7 +28,7 @@ # # The output comes from an Ubuntu 18 system # -_default_commands = [ +_default_commands = [ # pylint: disable=invalid-name (r"^systemctl --version$", '''systemd 237 +PAM +AUDIT +SELINUX +IMA +APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ +LZ4 +SECCOMP +BLKID +ELFUTILS +KMOD -IDN2 +IDN -PCRE2 default-hierarchy=hybrid @@ -48,9 +48,8 @@ cgroup on /sys/fs/cgroup/cpu,cpuacct type cgroup (rw,nosuid,nodev,noexec,relatime,cpu,cpuacct) cgroup on /sys/fs/cgroup/blkio type cgroup (rw,nosuid,nodev,noexec,relatime,blkio) '''), - (r"^mount -t cgroup2$", -'''cgroup on /sys/fs/cgroup/unified type cgroup2 (rw,nosuid,nodev,noexec,relatime) +'''cgroup on /sys/fs/cgroup/unified type cgroup2 (rw,nosuid,nodev,noexec,relatime) '''), (r"^systemctl show walinuxagent\.service --property CPUAccounting$", @@ -62,7 +61,7 @@ '''), (r"^systemd-run --unit=([^\s]+) --scope ([^\s]+)", -''' +''' Running scope as unit: TEST_UNIT.scope Thu 28 May 2020 07:25:55 AM PDT '''), @@ -75,7 +74,7 @@ '''), ] -_default_files = [ +_default_files = [ # pylint: disable=invalid-name (r"^/proc/self/cgroup$", os.path.join(data_dir, 'cgroups', 'proc_self_cgroup')), (r"^/proc/[0-9]+/cgroup$", os.path.join(data_dir, 'cgroups', 'proc_pid_cgroup')), (r"^/sys/fs/cgroup/unified/cgroup.controllers$", os.path.join(data_dir, 'cgroups', 'sys_fs_cgroup_unified_cgroup.controllers')), @@ -105,14 +104,14 @@ def mock_popen(command, *args, **kwargs): return original_popen(command, *args, **kwargs) def mock_read_file(filepath, **kwargs): - for file in patcher.files: + for file in patcher.files: # pylint: disable=redefined-builtin match = re.match(file[0], filepath) if match is not None: filepath = file[1] return original_read_file(filepath, **kwargs) def mock_path_exists(path): - for file in patcher.files: + for file in patcher.files: # pylint: disable=redefined-builtin match = re.match(file[0], path) if match is not None: return True diff --git a/tests/common/osutil/test_alpine.py b/tests/common/osutil/test_alpine.py index ea0c6d8a4e..f0e50a91ae 100644 --- a/tests/common/osutil/test_alpine.py +++ b/tests/common/osutil/test_alpine.py @@ -16,8 +16,8 @@ # from azurelinuxagent.common.osutil.alpine import AlpineOSUtil from .test_default import osutil_get_dhcp_pid_should_return_a_list_of_pids -from tests.tools import AgentTestCase -import unittest +from tests.tools import AgentTestCase # pylint: disable=wrong-import-order +import unittest # pylint: disable=wrong-import-order class TestAlpineOSUtil(AgentTestCase): diff --git a/tests/common/osutil/test_arch.py b/tests/common/osutil/test_arch.py index eeda8560c6..055f3d00a1 100644 --- a/tests/common/osutil/test_arch.py +++ b/tests/common/osutil/test_arch.py @@ -16,8 +16,8 @@ # from azurelinuxagent.common.osutil.arch import ArchUtil from .test_default import osutil_get_dhcp_pid_should_return_a_list_of_pids -from tests.tools import AgentTestCase -import unittest +from tests.tools import AgentTestCase # pylint: disable=wrong-import-order +import unittest # pylint: disable=wrong-import-order class TestArchUtil(AgentTestCase): diff --git a/tests/common/osutil/test_bigip.py b/tests/common/osutil/test_bigip.py index 07fd3b9bdb..14da00aa28 100644 --- a/tests/common/osutil/test_bigip.py +++ b/tests/common/osutil/test_bigip.py @@ -28,15 +28,15 @@ import azurelinuxagent.common.logger as logger from azurelinuxagent.common.osutil.bigip import BigIpOSUtil from .test_default import osutil_get_dhcp_pid_should_return_a_list_of_pids -from tests.tools import AgentTestCase, patch +from tests.tools import AgentTestCase, patch # pylint: disable=wrong-import-order -class TestBigIpOSUtil_wait_until_mcpd_is_initialized(AgentTestCase): +class TestBigIpOSUtil_wait_until_mcpd_is_initialized(AgentTestCase): # pylint: disable=invalid-name @patch.object(shellutil, "run", return_value=0) @patch.object(logger, "info", return_value=None) def test_success(self, *args): - result = osutil.BigIpOSUtil._wait_until_mcpd_is_initialized( + result = osutil.BigIpOSUtil._wait_until_mcpd_is_initialized( # pylint: disable=protected-access osutil.BigIpOSUtil() ) self.assertEqual(result, True) @@ -48,32 +48,32 @@ def test_success(self, *args): @patch.object(shellutil, "run", return_value=1) @patch.object(logger, "info", return_value=None) @patch.object(time, "sleep", return_value=None) - def test_failure(self, *args): + def test_failure(self, *args): # pylint: disable=unused-argument self.assertRaises( OSUtilError, - osutil.BigIpOSUtil._wait_until_mcpd_is_initialized, + osutil.BigIpOSUtil._wait_until_mcpd_is_initialized, # pylint: disable=protected-access osutil.BigIpOSUtil() ) -class TestBigIpOSUtil_save_sys_config(AgentTestCase): +class TestBigIpOSUtil_save_sys_config(AgentTestCase): # pylint: disable=invalid-name @patch.object(shellutil, "run", return_value=0) @patch.object(logger, "error", return_value=None) def test_success(self, *args): - result = osutil.BigIpOSUtil._save_sys_config(osutil.BigIpOSUtil()) + result = osutil.BigIpOSUtil._save_sys_config(osutil.BigIpOSUtil()) # pylint: disable=protected-access self.assertEqual(result, 0) self.assertEqual(args[0].call_count, 0) @patch.object(shellutil, "run", return_value=1) @patch.object(logger, "error", return_value=None) def test_failure(self, *args): - result = osutil.BigIpOSUtil._save_sys_config(osutil.BigIpOSUtil()) + result = osutil.BigIpOSUtil._save_sys_config(osutil.BigIpOSUtil()) # pylint: disable=protected-access self.assertEqual(result, 1) self.assertEqual(args[0].call_count, 1) -class TestBigIpOSUtil_useradd(AgentTestCase): +class TestBigIpOSUtil_useradd(AgentTestCase): # pylint: disable=invalid-name @patch.object(osutil.BigIpOSUtil, 'get_userentry', return_value=None) @patch.object(shellutil, "run_command") @@ -93,7 +93,7 @@ def test_user_already_exists(self, *args): self.assertEqual(result, None) @patch.object(shellutil, "run", return_value=1) - def test_failure(self, *args): + def test_failure(self, *args): # pylint: disable=unused-argument self.assertRaises( OSUtilError, osutil.BigIpOSUtil.useradd, @@ -101,7 +101,7 @@ def test_failure(self, *args): ) -class TestBigIpOSUtil_chpasswd(AgentTestCase): +class TestBigIpOSUtil_chpasswd(AgentTestCase): # pylint: disable=invalid-name @patch.object(shellutil, "run_command") @patch.object(osutil.BigIpOSUtil, 'get_userentry', return_value=True) @@ -116,7 +116,7 @@ def test_success(self, *args): self.assertEqual(args[0].call_count, 1) @patch.object(osutil.BigIpOSUtil, 'is_sys_user', return_value=True) - def test_is_sys_user(self, *args): + def test_is_sys_user(self, *args): # pylint: disable=unused-argument self.assertRaises( OSUtilError, osutil.BigIpOSUtil.chpasswd, @@ -125,7 +125,7 @@ def test_is_sys_user(self, *args): @patch.object(shellutil, "run_get_output", return_value=(1, None)) @patch.object(osutil.BigIpOSUtil, 'is_sys_user', return_value=False) - def test_failed_to_set_user_password(self, *args): + def test_failed_to_set_user_password(self, *args): # pylint: disable=unused-argument self.assertRaises( OSUtilError, osutil.BigIpOSUtil.chpasswd, @@ -135,7 +135,7 @@ def test_failed_to_set_user_password(self, *args): @patch.object(shellutil, "run_get_output", return_value=(0, None)) @patch.object(osutil.BigIpOSUtil, 'is_sys_user', return_value=False) @patch.object(osutil.BigIpOSUtil, 'get_userentry', return_value=None) - def test_failed_to_get_user_entry(self, *args): + def test_failed_to_get_user_entry(self, *args): # pylint: disable=unused-argument self.assertRaises( OSUtilError, osutil.BigIpOSUtil.chpasswd, @@ -143,17 +143,17 @@ def test_failed_to_get_user_entry(self, *args): ) -class TestBigIpOSUtil_get_dvd_device(AgentTestCase): +class TestBigIpOSUtil_get_dvd_device(AgentTestCase): # pylint: disable=invalid-name @patch.object(os, "listdir", return_value=['tty1','cdrom0']) - def test_success(self, *args): + def test_success(self, *args): # pylint: disable=unused-argument result = osutil.BigIpOSUtil.get_dvd_device( osutil.BigIpOSUtil(), '/dev' ) self.assertEqual(result, '/dev/cdrom0') @patch.object(os, "listdir", return_value=['foo', 'bar']) - def test_failure(self, *args): + def test_failure(self, *args): # pylint: disable=unused-argument self.assertRaises( OSUtilError, osutil.BigIpOSUtil.get_dvd_device, @@ -161,83 +161,83 @@ def test_failure(self, *args): ) -class TestBigIpOSUtil_restart_ssh_service(AgentTestCase): +class TestBigIpOSUtil_restart_ssh_service(AgentTestCase): # pylint: disable=invalid-name @patch.object(shellutil, "run", return_value=0) - def test_success(self, *args): + def test_success(self, *args): # pylint: disable=unused-argument result = osutil.BigIpOSUtil.restart_ssh_service( osutil.BigIpOSUtil() ) self.assertEqual(result, 0) -class TestBigIpOSUtil_stop_agent_service(AgentTestCase): +class TestBigIpOSUtil_stop_agent_service(AgentTestCase): # pylint: disable=invalid-name @patch.object(shellutil, "run", return_value=0) - def test_success(self, *args): + def test_success(self, *args): # pylint: disable=unused-argument result = osutil.BigIpOSUtil.stop_agent_service( osutil.BigIpOSUtil() ) self.assertEqual(result, 0) -class TestBigIpOSUtil_start_agent_service(AgentTestCase): +class TestBigIpOSUtil_start_agent_service(AgentTestCase): # pylint: disable=invalid-name @patch.object(shellutil, "run", return_value=0) - def test_success(self, *args): + def test_success(self, *args): # pylint: disable=unused-argument result = osutil.BigIpOSUtil.start_agent_service( osutil.BigIpOSUtil() ) self.assertEqual(result, 0) -class TestBigIpOSUtil_register_agent_service(AgentTestCase): +class TestBigIpOSUtil_register_agent_service(AgentTestCase): # pylint: disable=invalid-name @patch.object(shellutil, "run", return_value=0) - def test_success(self, *args): + def test_success(self, *args): # pylint: disable=unused-argument result = osutil.BigIpOSUtil.register_agent_service( osutil.BigIpOSUtil() ) self.assertEqual(result, 0) -class TestBigIpOSUtil_unregister_agent_service(AgentTestCase): +class TestBigIpOSUtil_unregister_agent_service(AgentTestCase): # pylint: disable=invalid-name @patch.object(shellutil, "run", return_value=0) - def test_success(self, *args): + def test_success(self, *args): # pylint: disable=unused-argument result = osutil.BigIpOSUtil.unregister_agent_service( osutil.BigIpOSUtil() ) self.assertEqual(result, 0) -class TestBigIpOSUtil_set_hostname(AgentTestCase): +class TestBigIpOSUtil_set_hostname(AgentTestCase): # pylint: disable=invalid-name @patch.object(os.path, "exists", return_value=False) def test_success(self, *args): - result = osutil.BigIpOSUtil.set_hostname( + result = osutil.BigIpOSUtil.set_hostname( # pylint: disable=assignment-from-none osutil.BigIpOSUtil(), None ) self.assertEqual(args[0].call_count, 0) self.assertEqual(result, None) -class TestBigIpOSUtil_set_dhcp_hostname(AgentTestCase): +class TestBigIpOSUtil_set_dhcp_hostname(AgentTestCase): # pylint: disable=invalid-name @patch.object(os.path, "exists", return_value=False) def test_success(self, *args): - result = osutil.BigIpOSUtil.set_dhcp_hostname( + result = osutil.BigIpOSUtil.set_dhcp_hostname( # pylint: disable=assignment-from-none osutil.BigIpOSUtil(), None ) self.assertEqual(args[0].call_count, 0) self.assertEqual(result, None) -class TestBigIpOSUtil_get_first_if(AgentTestCase): +class TestBigIpOSUtil_get_first_if(AgentTestCase): # pylint: disable=invalid-name @patch.object(osutil.BigIpOSUtil, '_format_single_interface_name', return_value=b'eth0') - def test_success(self, *args): + def test_success(self, *args): # pylint: disable=unused-argument ifname, ipaddr = osutil.BigIpOSUtil().get_first_if() self.assertTrue(ifname.startswith('eth')) self.assertTrue(ipaddr is not None) @@ -248,7 +248,7 @@ def test_success(self, *args): @patch.object(osutil.BigIpOSUtil, '_format_single_interface_name', return_value=b'loenp0s3') - def test_success(self, *args): + def test_success(self, *args): # pylint: disable=unused-argument,function-redefined ifname, ipaddr = osutil.BigIpOSUtil().get_first_if() self.assertFalse(ifname.startswith('eth')) self.assertTrue(ipaddr is not None) @@ -258,7 +258,7 @@ def test_success(self, *args): self.fail("not a valid ip address") -class TestBigIpOSUtil_mount_dvd(AgentTestCase): +class TestBigIpOSUtil_mount_dvd(AgentTestCase): # pylint: disable=invalid-name @patch.object(shellutil, "run", return_value=0) @patch.object(time, "sleep", return_value=None) @@ -273,7 +273,7 @@ def test_success(self, *args): self.assertEqual(args[1].call_count, 1) -class TestBigIpOSUtil_route_add(AgentTestCase): +class TestBigIpOSUtil_route_add(AgentTestCase): # pylint: disable=invalid-name @patch.object(shellutil, "run", return_value=0) def test_success(self, *args): @@ -283,7 +283,7 @@ def test_success(self, *args): self.assertEqual(args[0].call_count, 1) -class TestBigIpOSUtil_device_for_ide_port(AgentTestCase): +class TestBigIpOSUtil_device_for_ide_port(AgentTestCase): # pylint: disable=invalid-name @patch.object(time, "sleep", return_value=None) @patch.object(os.path, "exists", return_value=False) diff --git a/tests/common/osutil/test_clearlinux.py b/tests/common/osutil/test_clearlinux.py index a11d79a9fb..3def3b2601 100644 --- a/tests/common/osutil/test_clearlinux.py +++ b/tests/common/osutil/test_clearlinux.py @@ -16,8 +16,8 @@ # from azurelinuxagent.common.osutil.clearlinux import ClearLinuxUtil from .test_default import osutil_get_dhcp_pid_should_return_a_list_of_pids -from tests.tools import AgentTestCase -import unittest +from tests.tools import AgentTestCase # pylint: disable=wrong-import-order +import unittest # pylint: disable=wrong-import-order class TestClearLinuxUtil(AgentTestCase): diff --git a/tests/common/osutil/test_coreos.py b/tests/common/osutil/test_coreos.py index fca73f6f16..ba1928bd97 100644 --- a/tests/common/osutil/test_coreos.py +++ b/tests/common/osutil/test_coreos.py @@ -16,8 +16,8 @@ # from azurelinuxagent.common.osutil.coreos import CoreOSUtil from .test_default import osutil_get_dhcp_pid_should_return_a_list_of_pids -from tests.tools import AgentTestCase -import unittest +from tests.tools import AgentTestCase # pylint: disable=wrong-import-order +import unittest # pylint: disable=wrong-import-order class TestAlpineOSUtil(AgentTestCase): diff --git a/tests/common/osutil/test_default.py b/tests/common/osutil/test_default.py index ecada3b5c4..552dbf47cc 100644 --- a/tests/common/osutil/test_default.py +++ b/tests/common/osutil/test_default.py @@ -17,12 +17,12 @@ import contextlib import glob import mock -import os -import socket -import subprocess -import tempfile -import traceback -import unittest +import os # pylint: disable=wrong-import-order +import socket # pylint: disable=wrong-import-order +import subprocess # pylint: disable=wrong-import-order +import tempfile # pylint: disable=wrong-import-order +import traceback # pylint: disable=wrong-import-order +import unittest # pylint: disable=wrong-import-order import azurelinuxagent.common.osutil.default as osutil import azurelinuxagent.common.utils.shellutil as shellutil @@ -33,17 +33,19 @@ from azurelinuxagent.common.osutil import get_osutil from azurelinuxagent.common.utils import fileutil from azurelinuxagent.common.utils.flexible_version import FlexibleVersion +# pylint: disable=unused-import from tests.tools import AgentTestCase, call, patch, open_patch, load_data, \ running_under_travis, skip_if_predicate_true +# pylint: enable=unused-import -actual_get_proc_net_route = 'azurelinuxagent.common.osutil.default.DefaultOSUtil._get_proc_net_route' +actual_get_proc_net_route = 'azurelinuxagent.common.osutil.default.DefaultOSUtil._get_proc_net_route' # pylint: disable=invalid-name def fake_is_loopback(_, iface): return iface.startswith('lo') -class TestOSUtil(AgentTestCase): +class TestOSUtil(AgentTestCase): # pylint: disable=too-many-public-methods def setUp(self): AgentTestCase.setUp(self) @@ -84,7 +86,7 @@ def test_mount_dvd_success(self, _): return_value='/dev/cdrom'): with patch.object(shellutil, 'run_get_output', - return_value=(0, msg)) as patch_run: + return_value=(0, msg)) as patch_run: # pylint: disable=unused-variable with patch.object(os, 'makedirs'): try: osutil.DefaultOSUtil().mount_dvd() @@ -111,14 +113,14 @@ def test_mount_dvd_failure(self, _): def test_empty_proc_net_route(self): routing_table = "" - mo = mock.mock_open(read_data=routing_table) + mo = mock.mock_open(read_data=routing_table) # pylint: disable=invalid-name with patch(open_patch(), mo): self.assertEqual(len(osutil.DefaultOSUtil().read_route_table()), 0) def test_no_routes(self): routing_table = 'Iface\tDestination\tGateway \tFlags\tRefCnt\tUse\tMetric\tMask\t\tMTU\tWindow\tIRTT \n' - mo = mock.mock_open(read_data=routing_table) + mo = mock.mock_open(read_data=routing_table) # pylint: disable=invalid-name with patch(open_patch(), mo): raw_route_list = osutil.DefaultOSUtil().read_route_table() @@ -127,7 +129,7 @@ def test_no_routes(self): def test_bogus_proc_net_route(self): routing_table = 'Iface\tDestination\tGateway \tFlags\t\tUse\tMetric\t\neth0\t00000000\t00000000\t0001\t\t0\t0\n' - mo = mock.mock_open(read_data=routing_table) + mo = mock.mock_open(read_data=routing_table) # pylint: disable=invalid-name with patch(open_patch(), mo): raw_route_list = osutil.DefaultOSUtil().read_route_table() @@ -143,7 +145,7 @@ def test_valid_routes(self): 'docker0\t002BA8C0\t00000000\t0001\t0\t0\t10\t00FFFFFF\t0\t0\t0 \n' known_sha1_hash = b'\x1e\xd1k\xae[\xf8\x9b\x1a\x13\xd0\xbbT\xa4\xe3Y\xa3\xdd\x0b\xbd\xa9' - mo = mock.mock_open(read_data=routing_table) + mo = mock.mock_open(read_data=routing_table) # pylint: disable=invalid-name with patch(open_patch(), mo): raw_route_list = osutil.DefaultOSUtil().read_route_table() @@ -168,7 +170,7 @@ def test_valid_routes(self): @patch('azurelinuxagent.common.osutil.default.DefaultOSUtil.get_primary_interface', return_value='eth0') @patch('azurelinuxagent.common.osutil.default.DefaultOSUtil._get_all_interfaces', return_value={'eth0':'10.0.0.1'}) @patch('azurelinuxagent.common.osutil.default.DefaultOSUtil.is_loopback', fake_is_loopback) - def test_get_first_if(self, get_all_interfaces_mock, get_primary_interface_mock): + def test_get_first_if(self, get_all_interfaces_mock, get_primary_interface_mock): # pylint: disable=unused-argument """ Validate that the agent can find the first active non-loopback interface. @@ -184,7 +186,7 @@ def test_get_first_if(self, get_all_interfaces_mock, get_primary_interface_mock) @patch('azurelinuxagent.common.osutil.default.DefaultOSUtil.get_primary_interface', return_value='bogus0') @patch('azurelinuxagent.common.osutil.default.DefaultOSUtil._get_all_interfaces', return_value={'eth0':'10.0.0.1', 'lo': '127.0.0.1'}) @patch('azurelinuxagent.common.osutil.default.DefaultOSUtil.is_loopback', fake_is_loopback) - def test_get_first_if_nosuchprimary(self, get_all_interfaces_mock, get_primary_interface_mock): + def test_get_first_if_nosuchprimary(self, get_all_interfaces_mock, get_primary_interface_mock): # pylint: disable=unused-argument ifname, ipaddr = osutil.DefaultOSUtil().get_first_if() self.assertTrue(ifname.startswith('eth')) self.assertTrue(ipaddr is not None) @@ -203,7 +205,7 @@ def test_get_all_interfaces(self): loopback_count = 0 non_loopback_count = 0 - for iface in osutil.DefaultOSUtil()._get_all_interfaces(): + for iface in osutil.DefaultOSUtil()._get_all_interfaces(): # pylint: disable=protected-access if iface == 'lo': loopback_count += 1 else: @@ -213,7 +215,7 @@ def test_get_all_interfaces(self): self.assertGreater(loopback_count, 0, 'At least 1 non-loopback network interface should exist') def test_isloopback(self): - for iface in osutil.DefaultOSUtil()._get_all_interfaces(): + for iface in osutil.DefaultOSUtil()._get_all_interfaces(): # pylint: disable=protected-access if iface == 'lo': self.assertTrue(osutil.DefaultOSUtil().is_loopback(iface)) else: @@ -226,7 +228,7 @@ def test_isprimary(self): eth0 00345B0A 00000000 0001 0 0 5 00000000 0 0 0 \n\ lo 00000000 01345B0A 0003 0 0 1 00FCFFFF 0 0 0 \n" - mo = mock.mock_open(read_data=routing_table) + mo = mock.mock_open(read_data=routing_table) # pylint: disable=invalid-name with patch(open_patch(), mo): self.assertFalse(osutil.DefaultOSUtil().is_primary_interface('lo')) self.assertTrue(osutil.DefaultOSUtil().is_primary_interface('eth0')) @@ -240,7 +242,7 @@ def test_sriov(self): "bond0 10813FA8 0100000A 0007 0 0 0 00000000 0 0 0 \n" \ "bond0 FEA9FEA9 0100000A 0007 0 0 0 00000000 0 0 0 \n" - mo = mock.mock_open(read_data=routing_table) + mo = mock.mock_open(read_data=routing_table) # pylint: disable=invalid-name with patch(open_patch(), mo): self.assertFalse(osutil.DefaultOSUtil().is_primary_interface('eth0')) self.assertTrue(osutil.DefaultOSUtil().is_primary_interface('bond0')) @@ -252,7 +254,7 @@ def test_multiple_default_routes(self): high 00000000 01345B0A 0003 0 0 5 00000000 0 0 0 \n\ low1 00000000 01345B0A 0003 0 0 1 00FCFFFF 0 0 0 \n" - mo = mock.mock_open(read_data=routing_table) + mo = mock.mock_open(read_data=routing_table) # pylint: disable=invalid-name with patch(open_patch(), mo): self.assertTrue(osutil.DefaultOSUtil().is_primary_interface('low1')) @@ -262,7 +264,7 @@ def test_multiple_interfaces(self): first 00000000 01345B0A 0003 0 0 1 00000000 0 0 0 \n\ secnd 00000000 01345B0A 0003 0 0 1 00FCFFFF 0 0 0 \n" - mo = mock.mock_open(read_data=routing_table) + mo = mock.mock_open(read_data=routing_table) # pylint: disable=invalid-name with patch(open_patch(), mo): self.assertTrue(osutil.DefaultOSUtil().is_primary_interface('first')) @@ -272,7 +274,7 @@ def test_interface_flags(self): nflg 00000000 01345B0A 0001 0 0 1 00000000 0 0 0 \n\ flgs 00000000 01345B0A 0003 0 0 1 00FCFFFF 0 0 0 \n" - mo = mock.mock_open(read_data=routing_table) + mo = mock.mock_open(read_data=routing_table) # pylint: disable=invalid-name with patch(open_patch(), mo): self.assertTrue(osutil.DefaultOSUtil().is_primary_interface('flgs')) @@ -282,7 +284,7 @@ def test_no_interface(self): ndst 00000001 01345B0A 0003 0 0 1 00000000 0 0 0 \n\ nflg 00000000 01345B0A 0001 0 0 1 00FCFFFF 0 0 0 \n" - mo = mock.mock_open(read_data=routing_table) + mo = mock.mock_open(read_data=routing_table) # pylint: disable=invalid-name with patch(open_patch(), mo): self.assertFalse(osutil.DefaultOSUtil().is_primary_interface('ndst')) self.assertFalse(osutil.DefaultOSUtil().is_primary_interface('nflg')) @@ -295,7 +297,7 @@ def test_no_primary_does_not_throw(self): patch_primary.return_value = '' try: osutil.DefaultOSUtil().get_first_if()[0] - except Exception as e: + except Exception as e: # pylint: disable=unused-variable,invalid-name print(traceback.format_exc()) exception = True self.assertFalse(exception) @@ -306,15 +308,15 @@ def test_dhcp_lease_default(self): def test_dhcp_lease_ubuntu(self): with patch.object(glob, "glob", return_value=['/var/lib/dhcp/dhclient.eth0.leases']): with patch(open_patch(), mock.mock_open(read_data=load_data("dhcp.leases"))): - endpoint = get_osutil(distro_name='ubuntu', distro_version='12.04').get_dhcp_lease_endpoint() + endpoint = get_osutil(distro_name='ubuntu', distro_version='12.04').get_dhcp_lease_endpoint() # pylint: disable=assignment-from-none self.assertTrue(endpoint is not None) self.assertEqual(endpoint, "168.63.129.16") - endpoint = get_osutil(distro_name='ubuntu', distro_version='12.04').get_dhcp_lease_endpoint() + endpoint = get_osutil(distro_name='ubuntu', distro_version='12.04').get_dhcp_lease_endpoint() # pylint: disable=assignment-from-none self.assertTrue(endpoint is not None) self.assertEqual(endpoint, "168.63.129.16") - endpoint = get_osutil(distro_name='ubuntu', distro_version='14.04').get_dhcp_lease_endpoint() + endpoint = get_osutil(distro_name='ubuntu', distro_version='14.04').get_dhcp_lease_endpoint() # pylint: disable=assignment-from-none self.assertTrue(endpoint is not None) self.assertEqual(endpoint, "168.63.129.16") @@ -326,13 +328,13 @@ def test_dhcp_lease_custom_dns(self): """ with patch.object(glob, "glob", return_value=['/var/lib/dhcp/dhclient.eth0.leases']): with patch(open_patch(), mock.mock_open(read_data=load_data("dhcp.leases.custom.dns"))): - endpoint = get_osutil(distro_name='ubuntu', distro_version='14.04').get_dhcp_lease_endpoint() + endpoint = get_osutil(distro_name='ubuntu', distro_version='14.04').get_dhcp_lease_endpoint() # pylint: disable=assignment-from-none self.assertEqual(endpoint, "168.63.129.16") def test_dhcp_lease_multi(self): with patch.object(glob, "glob", return_value=['/var/lib/dhcp/dhclient.eth0.leases']): with patch(open_patch(), mock.mock_open(read_data=load_data("dhcp.leases.multi"))): - endpoint = get_osutil(distro_name='ubuntu', distro_version='12.04').get_dhcp_lease_endpoint() + endpoint = get_osutil(distro_name='ubuntu', distro_version='12.04').get_dhcp_lease_endpoint() # pylint: disable=assignment-from-none self.assertTrue(endpoint is not None) self.assertEqual(endpoint, "168.63.129.2") @@ -512,18 +514,18 @@ def test_correct_instance_id(self): util = osutil.DefaultOSUtil() self.assertEqual( "12345678-1234-1234-1234-123456789012", - util._correct_instance_id("78563412-3412-3412-1234-123456789012")) + util._correct_instance_id("78563412-3412-3412-1234-123456789012")) # pylint: disable=protected-access self.assertEqual( "D0DF4C54-4ECB-4A4B-9954-5BDF3ED5C3B8", - util._correct_instance_id("544CDFD0-CB4E-4B4A-9954-5BDF3ED5C3B8")) + util._correct_instance_id("544CDFD0-CB4E-4B4A-9954-5BDF3ED5C3B8")) # pylint: disable=protected-access self.assertEqual( "d0df4c54-4ecb-4a4b-9954-5bdf3ed5c3b8", - util._correct_instance_id("544cdfd0-cb4e-4b4a-9954-5bdf3ed5c3b8")) + util._correct_instance_id("544cdfd0-cb4e-4b4a-9954-5bdf3ed5c3b8")) # pylint: disable=protected-access @patch('os.path.isfile', return_value=True) @patch('azurelinuxagent.common.utils.fileutil.read_file', return_value="33C2F3B9-1399-429F-8EB3-BA656DF32502") - def test_get_instance_id_from_file(self, mock_read, mock_isfile): + def test_get_instance_id_from_file(self, mock_read, mock_isfile): # pylint: disable=unused-argument util = osutil.DefaultOSUtil() self.assertEqual( util.get_instance_id(), @@ -532,7 +534,7 @@ def test_get_instance_id_from_file(self, mock_read, mock_isfile): @patch('os.path.isfile', return_value=True) @patch('azurelinuxagent.common.utils.fileutil.read_file', return_value="") - def test_get_instance_id_empty_from_file(self, mock_read, mock_isfile): + def test_get_instance_id_empty_from_file(self, mock_read, mock_isfile): # pylint: disable=unused-argument util = osutil.DefaultOSUtil() self.assertEqual( "", @@ -541,7 +543,7 @@ def test_get_instance_id_empty_from_file(self, mock_read, mock_isfile): @patch('os.path.isfile', return_value=True) @patch('azurelinuxagent.common.utils.fileutil.read_file', return_value="Value") - def test_get_instance_id_malformed_from_file(self, mock_read, mock_isfile): + def test_get_instance_id_malformed_from_file(self, mock_read, mock_isfile): # pylint: disable=unused-argument util = osutil.DefaultOSUtil() self.assertEqual( "Value", @@ -550,7 +552,7 @@ def test_get_instance_id_malformed_from_file(self, mock_read, mock_isfile): @patch('os.path.isfile', return_value=False) @patch('azurelinuxagent.common.utils.shellutil.run_get_output', return_value=[0, '33C2F3B9-1399-429F-8EB3-BA656DF32502']) - def test_get_instance_id_from_dmidecode(self, mock_shell, mock_isfile): + def test_get_instance_id_from_dmidecode(self, mock_shell, mock_isfile): # pylint: disable=unused-argument util = osutil.DefaultOSUtil() self.assertEqual( util.get_instance_id(), @@ -559,20 +561,20 @@ def test_get_instance_id_from_dmidecode(self, mock_shell, mock_isfile): @patch('os.path.isfile', return_value=False) @patch('azurelinuxagent.common.utils.shellutil.run_get_output', return_value=[1, 'Error Value']) - def test_get_instance_id_missing(self, mock_shell, mock_isfile): + def test_get_instance_id_missing(self, mock_shell, mock_isfile): # pylint: disable=unused-argument util = osutil.DefaultOSUtil() self.assertEqual("", util.get_instance_id()) @patch('os.path.isfile', return_value=False) @patch('azurelinuxagent.common.utils.shellutil.run_get_output', return_value=[0, 'Unexpected Value']) - def test_get_instance_id_unexpected(self, mock_shell, mock_isfile): + def test_get_instance_id_unexpected(self, mock_shell, mock_isfile): # pylint: disable=unused-argument util = osutil.DefaultOSUtil() self.assertEqual("", util.get_instance_id()) @patch('os.path.isfile', return_value=True) @patch('azurelinuxagent.common.utils.fileutil.read_file') - def test_is_current_instance_id_from_file(self, mock_read, mock_isfile): + def test_is_current_instance_id_from_file(self, mock_read, mock_isfile): # pylint: disable=unused-argument util = osutil.DefaultOSUtil() mock_read.return_value = "11111111-2222-3333-4444-556677889900" @@ -597,7 +599,7 @@ def test_is_current_instance_id_from_file(self, mock_read, mock_isfile): @patch('os.path.isfile', return_value=False) @patch('azurelinuxagent.common.utils.shellutil.run_get_output') - def test_is_current_instance_id_from_dmidecode(self, mock_shell, mock_isfile): + def test_is_current_instance_id_from_dmidecode(self, mock_shell, mock_isfile): # pylint: disable=unused-argument util = osutil.DefaultOSUtil() mock_shell.return_value = [0, 'B9F3C233-9913-9F42-8EB3-BA656DF32502'] @@ -621,7 +623,7 @@ def test_conf_sudoer(self, mock_dir): self.assertTrue(os.path.isfile(waagent_sudoers)) count = -1 - with open(waagent_sudoers, 'r') as f: + with open(waagent_sudoers, 'r') as f: # pylint: disable=invalid-name count = len(f.readlines()) self.assertEqual(1, count) @@ -629,7 +631,7 @@ def test_conf_sudoer(self, mock_dir): util.conf_sudoer("FooBar") count = -1 - with open(waagent_sudoers, 'r') as f: + with open(waagent_sudoers, 'r') as f: # pylint: disable=invalid-name count = len(f.readlines()) print("WRITING TO {0}".format(waagent_sudoers)) self.assertEqual(1, count) @@ -640,7 +642,7 @@ def _command_to_string(command): @staticmethod @contextlib.contextmanager - def _mock_iptables(version=osutil._IPTABLES_LOCKING_VERSION, destination='168.63.129.16'): + def _mock_iptables(version=osutil._IPTABLES_LOCKING_VERSION, destination='168.63.129.16'): # pylint: disable=protected-access """ Mock for the iptable commands used to set up the firewall. @@ -660,26 +662,26 @@ def set_command(command, output='', exit_code=0): mocked_commands[command_string] = (output.replace("'", "'\"'\"'"), exit_code) return command_string - wait = FlexibleVersion(version) >= osutil._IPTABLES_LOCKING_VERSION + wait = FlexibleVersion(version) >= osutil._IPTABLES_LOCKING_VERSION # pylint: disable=protected-access uid = 42 - version_command = set_command(osutil._get_iptables_version_command(), output=str(version)) - list_command = set_command(osutil._get_firewall_list_command(wait), output="Mock Output") - set_command(osutil._get_firewall_packets_command(wait)) - set_command(osutil._get_firewall_drop_command(wait, "-C", destination)) - set_command(osutil._get_firewall_drop_command(wait, "-A", destination)) - set_command(osutil._get_firewall_accept_command(wait, "-A", destination, uid)) + version_command = set_command(osutil._get_iptables_version_command(), output=str(version)) # pylint: disable=protected-access + list_command = set_command(osutil._get_firewall_list_command(wait), output="Mock Output") # pylint: disable=protected-access + set_command(osutil._get_firewall_packets_command(wait)) # pylint: disable=protected-access + set_command(osutil._get_firewall_drop_command(wait, "-C", destination)) # pylint: disable=protected-access + set_command(osutil._get_firewall_drop_command(wait, "-A", destination)) # pylint: disable=protected-access + set_command(osutil._get_firewall_accept_command(wait, "-A", destination, uid)) # pylint: disable=protected-access # the agent assumes the rules have been deleted when these commands return 1 - set_command(osutil._get_firewall_delete_conntrack_accept_command(wait, destination), exit_code=1) - set_command(osutil._get_firewall_delete_owner_accept_command(wait, destination, uid), exit_code=1) - set_command(osutil._get_firewall_delete_conntrack_drop_command(wait, destination), exit_code=1) + set_command(osutil._get_firewall_delete_conntrack_accept_command(wait, destination), exit_code=1) # pylint: disable=protected-access + set_command(osutil._get_firewall_delete_owner_accept_command(wait, destination, uid), exit_code=1) # pylint: disable=protected-access + set_command(osutil._get_firewall_delete_conntrack_drop_command(wait, destination), exit_code=1) # pylint: disable=protected-access command_calls = [] def mock_popen(command, *args, **kwargs): command_string = TestOSUtil._command_to_string(command) if command_string in mocked_commands: - if command_string != version_command and command_string != list_command: + if command_string != version_command and command_string != list_command: # pylint: disable=consider-using-in command_calls.append(command_string) output, exit_code = mocked_commands[command_string] command = "echo '{0}' && exit {1}".format(output, exit_code) @@ -698,33 +700,34 @@ def mock_popen(command, *args, **kwargs): yield popen_patcher def test_get_firewall_dropped_packets_returns_zero_if_firewall_disabled(self): - osutil._enable_firewall = False + osutil._enable_firewall = False # pylint: disable=protected-access util = osutil.DefaultOSUtil() self.assertEqual(0, util.get_firewall_dropped_packets("not used")) def test_get_firewall_dropped_packets_returns_negative_if_error(self): - osutil._enable_firewall = True + osutil._enable_firewall = True # pylint: disable=protected-access with TestOSUtil._mock_iptables() as mock_iptables: - mock_iptables.set_command(osutil._get_firewall_packets_command(mock_iptables.wait), exit_code=1) + mock_iptables.set_command(osutil._get_firewall_packets_command(mock_iptables.wait), exit_code=1) # pylint: disable=protected-access self.assertEqual(-1, osutil.DefaultOSUtil().get_firewall_dropped_packets()) def test_get_firewall_dropped_packets_should_ignore_transient_errors(self): - osutil._enable_firewall = True + osutil._enable_firewall = True # pylint: disable=protected-access with TestOSUtil._mock_iptables() as mock_iptables: - mock_iptables.set_command(osutil._get_firewall_packets_command(mock_iptables.wait), exit_code=3, output="can't initialize iptables table `security': iptables who? (do you need to insmod?)") + mock_iptables.set_command(osutil._get_firewall_packets_command(mock_iptables.wait), exit_code=3, output="can't initialize iptables table `security': iptables who? (do you need to insmod?)") # pylint: disable=protected-access self.assertEqual(0, osutil.DefaultOSUtil().get_firewall_dropped_packets()) def test_get_firewall_dropped_packets(self): - osutil._enable_firewall = True + osutil._enable_firewall = True # pylint: disable=protected-access destination = '168.63.129.16' with TestOSUtil._mock_iptables() as mock_iptables: + # pylint: disable=protected-access mock_iptables.set_command(osutil._get_firewall_packets_command(mock_iptables.wait), output=''' Chain OUTPUT (policy ACCEPT 104 packets, 43628 bytes) @@ -733,21 +736,22 @@ def test_get_firewall_dropped_packets(self): 32 1920 DROP tcp -- any any anywhere 168.63.129.16 ''') + # pylint: enable=protected-access self.assertEqual(32, osutil.DefaultOSUtil().get_firewall_dropped_packets(destination)) def test_enable_firewall_should_set_up_the_firewall(self): - osutil._enable_firewall = True + osutil._enable_firewall = True # pylint: disable=protected-access with TestOSUtil._mock_iptables() as mock_iptables: # fail the rule check to force enable of the firewall - mock_iptables.set_command(osutil._get_firewall_drop_command(mock_iptables.wait, "-C", mock_iptables.destination), exit_code=1) + mock_iptables.set_command(osutil._get_firewall_drop_command(mock_iptables.wait, "-C", mock_iptables.destination), exit_code=1) # pylint: disable=protected-access success = osutil.DefaultOSUtil().enable_firewall(dst_ip=mock_iptables.destination, uid=mock_iptables.uid) - drop_check_command = TestOSUtil._command_to_string(osutil._get_firewall_drop_command(mock_iptables.wait, "-C", mock_iptables.destination)) - accept_command = TestOSUtil._command_to_string(osutil._get_firewall_accept_command(mock_iptables.wait, "-A", mock_iptables.destination, mock_iptables.uid)) - drop_add_command = TestOSUtil._command_to_string(osutil._get_firewall_drop_command(mock_iptables.wait, "-A", mock_iptables.destination)) + drop_check_command = TestOSUtil._command_to_string(osutil._get_firewall_drop_command(mock_iptables.wait, "-C", mock_iptables.destination)) # pylint: disable=protected-access + accept_command = TestOSUtil._command_to_string(osutil._get_firewall_accept_command(mock_iptables.wait, "-A", mock_iptables.destination, mock_iptables.uid)) # pylint: disable=protected-access + drop_add_command = TestOSUtil._command_to_string(osutil._get_firewall_drop_command(mock_iptables.wait, "-A", mock_iptables.destination)) # pylint: disable=protected-access self.assertTrue(success, "Enabling the firewall was not successful") self.assertEqual(len(mock_iptables.command_calls), 3, "Incorrect number of calls to iptables: [{0}]". format(mock_iptables.command_calls)) @@ -755,14 +759,14 @@ def test_enable_firewall_should_set_up_the_firewall(self): self.assertEqual(mock_iptables.command_calls[1], accept_command, "The second command should add the accept rule") self.assertEqual(mock_iptables.command_calls[2], drop_add_command, "The third command should add the drop rule") - self.assertTrue(osutil._enable_firewall, "The firewall should not have been disabled") + self.assertTrue(osutil._enable_firewall, "The firewall should not have been disabled") # pylint: disable=protected-access def test_enable_firewall_should_not_use_wait_when_iptables_does_not_support_it(self): - osutil._enable_firewall = True + osutil._enable_firewall = True # pylint: disable=protected-access - with TestOSUtil._mock_iptables(version=osutil._IPTABLES_LOCKING_VERSION - 1) as mock_iptables: + with TestOSUtil._mock_iptables(version=osutil._IPTABLES_LOCKING_VERSION - 1) as mock_iptables: # pylint: disable=protected-access # fail the rule check to force enable of the firewall - mock_iptables.set_command(osutil._get_firewall_drop_command(mock_iptables.wait, "-C", mock_iptables.destination), exit_code=1) + mock_iptables.set_command(osutil._get_firewall_drop_command(mock_iptables.wait, "-C", mock_iptables.destination), exit_code=1) # pylint: disable=protected-access success = osutil.DefaultOSUtil().enable_firewall(dst_ip=mock_iptables.destination, uid=mock_iptables.uid) @@ -771,13 +775,13 @@ def test_enable_firewall_should_not_use_wait_when_iptables_does_not_support_it(s for command in mock_iptables.command_calls: self.assertNotIn("-w", command, "The -w option should have been used in {0}".format(command)) - self.assertTrue(osutil._enable_firewall, "The firewall should not have been disabled") + self.assertTrue(osutil._enable_firewall, "The firewall should not have been disabled") # pylint: disable=protected-access def test_enable_firewall_should_not_set_firewall_if_the_drop_rule_exists(self): - osutil._enable_firewall = True + osutil._enable_firewall = True # pylint: disable=protected-access with TestOSUtil._mock_iptables() as mock_iptables: - drop_check_command = mock_iptables.set_command(osutil._get_firewall_drop_command(mock_iptables.wait, "-C", mock_iptables.destination), exit_code=0) + drop_check_command = mock_iptables.set_command(osutil._get_firewall_drop_command(mock_iptables.wait, "-C", mock_iptables.destination), exit_code=0) # pylint: disable=protected-access success = osutil.DefaultOSUtil().enable_firewall(dst_ip=mock_iptables.destination, uid=mock_iptables.uid) @@ -785,10 +789,10 @@ def test_enable_firewall_should_not_set_firewall_if_the_drop_rule_exists(self): self.assertEqual(len(mock_iptables.command_calls), 1, "Incorrect number of calls to iptables: [{0}]". format(mock_iptables.command_calls)) self.assertEqual(mock_iptables.command_calls[0], drop_check_command, "Unexpected command: {0}".format(mock_iptables.command_calls[0])) - self.assertTrue(osutil._enable_firewall) + self.assertTrue(osutil._enable_firewall) # pylint: disable=protected-access def test_enable_firewall_should_check_for_invalid_iptables_options(self): - osutil._enable_firewall = True + osutil._enable_firewall = True # pylint: disable=protected-access with TestOSUtil._mock_iptables() as mock_iptables: # iptables uses the following exit codes @@ -796,13 +800,13 @@ def test_enable_firewall_should_check_for_invalid_iptables_options(self): # 1 - other errors # 2 - errors which appear to be caused by invalid or abused command # line parameters - drop_check_command = mock_iptables.set_command(osutil._get_firewall_drop_command(mock_iptables.wait, "-C", mock_iptables.destination), exit_code=2) + drop_check_command = mock_iptables.set_command(osutil._get_firewall_drop_command(mock_iptables.wait, "-C", mock_iptables.destination), exit_code=2) # pylint: disable=protected-access success = osutil.DefaultOSUtil().enable_firewall(dst_ip=mock_iptables.destination, uid=mock_iptables.uid) - delete_conntrack_accept_command = TestOSUtil._command_to_string(osutil._get_firewall_delete_conntrack_accept_command(mock_iptables.wait, mock_iptables.destination)) - delete_owner_accept_command = TestOSUtil._command_to_string(osutil._get_firewall_delete_owner_accept_command(mock_iptables.wait, mock_iptables.destination, mock_iptables.uid)) - delete_conntrack_drop_command = TestOSUtil._command_to_string(osutil._get_firewall_delete_conntrack_drop_command(mock_iptables.wait, mock_iptables.destination)) + delete_conntrack_accept_command = TestOSUtil._command_to_string(osutil._get_firewall_delete_conntrack_accept_command(mock_iptables.wait, mock_iptables.destination)) # pylint: disable=protected-access + delete_owner_accept_command = TestOSUtil._command_to_string(osutil._get_firewall_delete_owner_accept_command(mock_iptables.wait, mock_iptables.destination, mock_iptables.uid)) # pylint: disable=protected-access + delete_conntrack_drop_command = TestOSUtil._command_to_string(osutil._get_firewall_delete_conntrack_drop_command(mock_iptables.wait, mock_iptables.destination)) # pylint: disable=protected-access self.assertFalse(success, "Enable firewall should have failed") self.assertEqual(len(mock_iptables.command_calls), 4, "Incorrect number of calls to iptables: [{0}]". format(mock_iptables.command_calls)) @@ -811,10 +815,10 @@ def test_enable_firewall_should_check_for_invalid_iptables_options(self): self.assertEqual(mock_iptables.command_calls[2], delete_owner_accept_command, "The third command should delete the owner accept rule: {0}".format(mock_iptables.command_calls[2])) self.assertEqual(mock_iptables.command_calls[3], delete_conntrack_drop_command, "The fourth command should delete the conntrack accept rule : {0}".format(mock_iptables.command_calls[3])) - self.assertFalse(osutil._enable_firewall) + self.assertFalse(osutil._enable_firewall) # pylint: disable=protected-access def test_enable_firewall_skips_if_disabled(self): - osutil._enable_firewall = False + osutil._enable_firewall = False # pylint: disable=protected-access with TestOSUtil._mock_iptables() as mock_iptables: success = osutil.DefaultOSUtil().enable_firewall(dst_ip=mock_iptables.destination, uid=mock_iptables.uid) @@ -822,10 +826,10 @@ def test_enable_firewall_skips_if_disabled(self): self.assertFalse(success, "The firewall should not have been disabled") self.assertEqual(len(mock_iptables.command_calls), 0, "iptables should not have been invoked: [{0}]". format(mock_iptables.command_calls)) - self.assertFalse(osutil._enable_firewall) + self.assertFalse(osutil._enable_firewall) # pylint: disable=protected-access def test_remove_firewall(self): - osutil._enable_firewall = True + osutil._enable_firewall = True # pylint: disable=protected-access with TestOSUtil._mock_iptables() as mock_iptables: delete_commands = {} @@ -850,12 +854,12 @@ def mock_popen(command, *args, **kwargs): with patch("azurelinuxagent.common.cgroupapi.subprocess.Popen", side_effect=mock_popen): success = osutil.DefaultOSUtil().remove_firewall(mock_iptables.destination, mock_iptables.uid) - delete_conntrack_accept_command = TestOSUtil._command_to_string(osutil._get_firewall_delete_conntrack_accept_command(mock_iptables.wait, mock_iptables.destination)) - delete_owner_accept_command = TestOSUtil._command_to_string(osutil._get_firewall_delete_owner_accept_command(mock_iptables.wait, mock_iptables.destination, mock_iptables.uid)) - delete_conntrack_drop_command = TestOSUtil._command_to_string(osutil._get_firewall_delete_conntrack_drop_command(mock_iptables.wait, mock_iptables.destination)) + delete_conntrack_accept_command = TestOSUtil._command_to_string(osutil._get_firewall_delete_conntrack_accept_command(mock_iptables.wait, mock_iptables.destination)) # pylint: disable=protected-access + delete_owner_accept_command = TestOSUtil._command_to_string(osutil._get_firewall_delete_owner_accept_command(mock_iptables.wait, mock_iptables.destination, mock_iptables.uid)) # pylint: disable=protected-access + delete_conntrack_drop_command = TestOSUtil._command_to_string(osutil._get_firewall_delete_conntrack_drop_command(mock_iptables.wait, mock_iptables.destination)) # pylint: disable=protected-access self.assertTrue(success, "Removing the firewall should have succeeded") - self.assertEquals(len(delete_commands), 3, "Expected 3 delete commands: [{0}]".format(delete_commands)) + self.assertEquals(len(delete_commands), 3, "Expected 3 delete commands: [{0}]".format(delete_commands)) # pylint: disable=deprecated-method # delete rules < 2.2.26 self.assertIn(delete_conntrack_accept_command, delete_commands, "The delete conntrack accept command was not executed") self.assertEqual(delete_commands[delete_conntrack_accept_command], 2, "The delete conntrack accept command should have been executed twice") @@ -865,13 +869,13 @@ def mock_popen(command, *args, **kwargs): self.assertIn(delete_conntrack_drop_command, delete_commands, "The delete conntrack drop command was not executed") self.assertEqual(delete_commands[delete_conntrack_drop_command], 2, "The delete conntrack drop command should have been executed twice") - self.assertTrue(osutil._enable_firewall) + self.assertTrue(osutil._enable_firewall) # pylint: disable=protected-access def test_remove_firewall_should_not_retry_invalid_rule(self): - osutil._enable_firewall = True + osutil._enable_firewall = True # pylint: disable=protected-access with TestOSUtil._mock_iptables() as mock_iptables: - command = osutil._get_firewall_delete_conntrack_accept_command(mock_iptables.wait, mock_iptables.destination) + command = osutil._get_firewall_delete_conntrack_accept_command(mock_iptables.wait, mock_iptables.destination) # pylint: disable=protected-access # Note that the command is actually a valid rule, but we use the mock to report it as invalid (exit code 2) delete_conntrack_accept_command = mock_iptables.set_command(command, exit_code=2) @@ -881,7 +885,7 @@ def test_remove_firewall_should_not_retry_invalid_rule(self): self.assertEqual(len(mock_iptables.command_calls), 1, "Expected a single call to iptables: [{0}]". format(mock_iptables.command_calls)) self.assertEqual(mock_iptables.command_calls[0], delete_conntrack_accept_command, "Expected call to delete conntrack accept command: {0}".format(mock_iptables.command_calls[0])) - self.assertFalse(osutil._enable_firewall) + self.assertFalse(osutil._enable_firewall) # pylint: disable=protected-access @skip_if_predicate_true(running_under_travis, "The ip command isn't available in Travis") def test_get_nic_state(self): @@ -900,7 +904,7 @@ def test_get_dhcp_pid_should_return_a_list_of_pids(self): def test_get_dhcp_pid_should_return_an_empty_list_when_the_dhcp_client_is_not_running(self): original_run_command = shellutil.run_command - def mock_run_command(cmd): + def mock_run_command(cmd): # pylint: disable=unused-argument return original_run_command(["pidof", "non-existing-process"]) with patch("azurelinuxagent.common.utils.shellutil.run_command", side_effect=mock_run_command): @@ -914,10 +918,10 @@ def mock_run_command(cmd): @patch('os.path.exists', return_value=True) def test_device_for_ide_port_gen1_success( self, - os_path_exists, - os_listdir, - fileutil_read_file, - os_walk): + os_path_exists, # pylint: disable=unused-argument + os_listdir, # pylint: disable=unused-argument + fileutil_read_file, # pylint: disable=unused-argument + os_walk): # pylint: disable=unused-argument dev = osutil.DefaultOSUtil().device_for_ide_port(1) self.assertEqual(dev, 'sdb', 'The returned device should be the resource disk') @@ -927,10 +931,10 @@ def test_device_for_ide_port_gen1_success( @patch('os.path.exists', return_value=True) def test_device_for_ide_port_gen2_success( self, - os_path_exists, - os_listdir, - fileutil_read_file, - os_walk): + os_path_exists, # pylint: disable=unused-argument + os_listdir, # pylint: disable=unused-argument + fileutil_read_file, # pylint: disable=unused-argument + os_walk): # pylint: disable=unused-argument dev = osutil.DefaultOSUtil().device_for_ide_port(1) self.assertEqual(dev, 'sdb', 'The returned device should be the resource disk') @@ -938,8 +942,8 @@ def test_device_for_ide_port_gen2_success( @patch('os.path.exists', return_value=True) def test_device_for_ide_port_none( self, - os_path_exists, - os_listdir): + os_path_exists, # pylint: disable=unused-argument + os_listdir): # pylint: disable=unused-argument dev = osutil.DefaultOSUtil().device_for_ide_port(1) self.assertIsNone(dev, 'None should be returned if no resource disk found') @@ -954,7 +958,7 @@ def osutil_get_dhcp_pid_should_return_a_list_of_pids(test_instance, osutil_insta """ original_run_command = shellutil.run_command - def mock_run_command(cmd): + def mock_run_command(cmd): # pylint: disable=unused-argument return original_run_command(["pidof", "pidof"]) with patch("azurelinuxagent.common.utils.shellutil.run_command", side_effect=mock_run_command): diff --git a/tests/common/osutil/test_default_osutil.py b/tests/common/osutil/test_default_osutil.py index 6e40203d4e..db69b4324b 100644 --- a/tests/common/osutil/test_default_osutil.py +++ b/tests/common/osutil/test_default_osutil.py @@ -15,10 +15,10 @@ # Requires Python 2.4+ and Openssl 1.0+ # -from azurelinuxagent.common.osutil.default import DefaultOSUtil, shellutil -from tests.tools import AgentTestCase, patch +from azurelinuxagent.common.osutil.default import DefaultOSUtil, shellutil # pylint: disable=unused-import +from tests.tools import AgentTestCase, patch # pylint: disable=unused-import class DefaultOsUtilTestCase(AgentTestCase): def test_default_service_name(self): - self.assertEquals(DefaultOSUtil().get_service_name(), "waagent") + self.assertEquals(DefaultOSUtil().get_service_name(), "waagent") # pylint: disable=deprecated-method diff --git a/tests/common/osutil/test_factory.py b/tests/common/osutil/test_factory.py index aa7daebcf2..4df568e952 100644 --- a/tests/common/osutil/test_factory.py +++ b/tests/common/osutil/test_factory.py @@ -49,232 +49,232 @@ def test_get_osutil_it_should_return_default(self, patch_logger): distro_code_name="", distro_version="", distro_full_name="") - self.assertTrue(type(ret) == DefaultOSUtil) - self.assertEquals(patch_logger.call_count, 1) - self.assertEquals(ret.get_service_name(), "waagent") + self.assertTrue(type(ret) == DefaultOSUtil) # pylint: disable=unidiomatic-typecheck + self.assertEquals(patch_logger.call_count, 1) # pylint: disable=deprecated-method + self.assertEquals(ret.get_service_name(), "waagent") # pylint: disable=deprecated-method def test_get_osutil_it_should_return_ubuntu(self): ret = _get_osutil(distro_name="ubuntu", distro_code_name="", distro_version="10.04", distro_full_name="") - self.assertTrue(type(ret) == UbuntuOSUtil) - self.assertEquals(ret.get_service_name(), "walinuxagent") + self.assertTrue(type(ret) == UbuntuOSUtil) # pylint: disable=unidiomatic-typecheck + self.assertEquals(ret.get_service_name(), "walinuxagent") # pylint: disable=deprecated-method ret = _get_osutil(distro_name="ubuntu", distro_code_name="", distro_version="12.04", distro_full_name="") - self.assertTrue(type(ret) == Ubuntu12OSUtil) - self.assertEquals(ret.get_service_name(), "walinuxagent") + self.assertTrue(type(ret) == Ubuntu12OSUtil) # pylint: disable=unidiomatic-typecheck + self.assertEquals(ret.get_service_name(), "walinuxagent") # pylint: disable=deprecated-method ret = _get_osutil(distro_name="ubuntu", distro_code_name="trusty", distro_version="14.04", distro_full_name="") - self.assertTrue(type(ret) == Ubuntu14OSUtil) - self.assertEquals(ret.get_service_name(), "walinuxagent") + self.assertTrue(type(ret) == Ubuntu14OSUtil) # pylint: disable=unidiomatic-typecheck + self.assertEquals(ret.get_service_name(), "walinuxagent") # pylint: disable=deprecated-method ret = _get_osutil(distro_name="ubuntu", distro_code_name="xenial", distro_version="16.04", distro_full_name="") - self.assertTrue(type(ret) == Ubuntu16OSUtil) - self.assertEquals(ret.get_service_name(), "walinuxagent") + self.assertTrue(type(ret) == Ubuntu16OSUtil) # pylint: disable=unidiomatic-typecheck + self.assertEquals(ret.get_service_name(), "walinuxagent") # pylint: disable=deprecated-method ret = _get_osutil(distro_name="ubuntu", distro_code_name="bionic", distro_version="18.04", distro_full_name="") - self.assertTrue(type(ret) == Ubuntu18OSUtil) - self.assertEquals(ret.get_service_name(), "walinuxagent") + self.assertTrue(type(ret) == Ubuntu18OSUtil) # pylint: disable=unidiomatic-typecheck + self.assertEquals(ret.get_service_name(), "walinuxagent") # pylint: disable=deprecated-method ret = _get_osutil(distro_name="ubuntu", distro_code_name="focal", distro_version="20.04", distro_full_name="") - self.assertTrue(type(ret) == Ubuntu18OSUtil) - self.assertEquals(ret.get_service_name(), "walinuxagent") + self.assertTrue(type(ret) == Ubuntu18OSUtil) # pylint: disable=unidiomatic-typecheck + self.assertEquals(ret.get_service_name(), "walinuxagent") # pylint: disable=deprecated-method ret = _get_osutil(distro_name="ubuntu", distro_code_name="", distro_version="10.04", distro_full_name="Snappy Ubuntu Core") - self.assertTrue(type(ret) == UbuntuSnappyOSUtil) - self.assertEquals(ret.get_service_name(), "walinuxagent") + self.assertTrue(type(ret) == UbuntuSnappyOSUtil) # pylint: disable=unidiomatic-typecheck + self.assertEquals(ret.get_service_name(), "walinuxagent") # pylint: disable=deprecated-method def test_get_osutil_it_should_return_arch(self): ret = _get_osutil(distro_name="arch", distro_code_name="", distro_version="", distro_full_name="") - self.assertTrue(type(ret) == ArchUtil) - self.assertEquals(ret.get_service_name(), "waagent") + self.assertTrue(type(ret) == ArchUtil) # pylint: disable=unidiomatic-typecheck + self.assertEquals(ret.get_service_name(), "waagent") # pylint: disable=deprecated-method def test_get_osutil_it_should_return_clear_linux(self): ret = _get_osutil(distro_name="clear linux", distro_code_name="", distro_version="", distro_full_name="Clear Linux") - self.assertTrue(type(ret) == ClearLinuxUtil) - self.assertEquals(ret.get_service_name(), "waagent") + self.assertTrue(type(ret) == ClearLinuxUtil) # pylint: disable=unidiomatic-typecheck + self.assertEquals(ret.get_service_name(), "waagent") # pylint: disable=deprecated-method def test_get_osutil_it_should_return_alpine(self): ret = _get_osutil(distro_name="alpine", distro_code_name="", distro_version="", distro_full_name="") - self.assertTrue(type(ret) == AlpineOSUtil) - self.assertEquals(ret.get_service_name(), "waagent") + self.assertTrue(type(ret) == AlpineOSUtil) # pylint: disable=unidiomatic-typecheck + self.assertEquals(ret.get_service_name(), "waagent") # pylint: disable=deprecated-method def test_get_osutil_it_should_return_kali(self): ret = _get_osutil(distro_name="kali", distro_code_name="", distro_version="", distro_full_name="") - self.assertTrue(type(ret) == DebianOSBaseUtil) - self.assertEquals(ret.get_service_name(), "waagent") + self.assertTrue(type(ret) == DebianOSBaseUtil) # pylint: disable=unidiomatic-typecheck + self.assertEquals(ret.get_service_name(), "waagent") # pylint: disable=deprecated-method def test_get_osutil_it_should_return_coreos(self): ret = _get_osutil(distro_name="coreos", distro_code_name="", distro_version="", distro_full_name="") - self.assertTrue(type(ret) == CoreOSUtil) - self.assertEquals(ret.get_service_name(), "waagent") + self.assertTrue(type(ret) == CoreOSUtil) # pylint: disable=unidiomatic-typecheck + self.assertEquals(ret.get_service_name(), "waagent") # pylint: disable=deprecated-method def test_get_osutil_it_should_return_suse(self): ret = _get_osutil(distro_name="suse", distro_code_name="", distro_version="10", distro_full_name="") - self.assertTrue(type(ret) == SUSEOSUtil) - self.assertEquals(ret.get_service_name(), "waagent") + self.assertTrue(type(ret) == SUSEOSUtil) # pylint: disable=unidiomatic-typecheck + self.assertEquals(ret.get_service_name(), "waagent") # pylint: disable=deprecated-method ret = _get_osutil(distro_name="suse", distro_code_name="", distro_full_name="SUSE Linux Enterprise Server", distro_version="11") - self.assertTrue(type(ret) == SUSE11OSUtil) - self.assertEquals(ret.get_service_name(), "waagent") + self.assertTrue(type(ret) == SUSE11OSUtil) # pylint: disable=unidiomatic-typecheck + self.assertEquals(ret.get_service_name(), "waagent") # pylint: disable=deprecated-method ret = _get_osutil(distro_name="suse", distro_code_name="", distro_full_name="openSUSE", distro_version="12") - self.assertTrue(type(ret) == SUSE11OSUtil) - self.assertEquals(ret.get_service_name(), "waagent") + self.assertTrue(type(ret) == SUSE11OSUtil) # pylint: disable=unidiomatic-typecheck + self.assertEquals(ret.get_service_name(), "waagent") # pylint: disable=deprecated-method def test_get_osutil_it_should_return_debian(self): ret = _get_osutil(distro_name="debian", distro_code_name="", distro_full_name="", distro_version="7") - self.assertTrue(type(ret) == DebianOSBaseUtil) - self.assertEquals(ret.get_service_name(), "waagent") + self.assertTrue(type(ret) == DebianOSBaseUtil) # pylint: disable=unidiomatic-typecheck + self.assertEquals(ret.get_service_name(), "waagent") # pylint: disable=deprecated-method ret = _get_osutil(distro_name="debian", distro_code_name="", distro_full_name="", distro_version="8") - self.assertTrue(type(ret) == DebianOSModernUtil) - self.assertEquals(ret.get_service_name(), "walinuxagent") + self.assertTrue(type(ret) == DebianOSModernUtil) # pylint: disable=unidiomatic-typecheck + self.assertEquals(ret.get_service_name(), "walinuxagent") # pylint: disable=deprecated-method def test_get_osutil_it_should_return_redhat(self): ret = _get_osutil(distro_name="redhat", distro_code_name="", distro_full_name="", distro_version="6") - self.assertTrue(type(ret) == Redhat6xOSUtil) - self.assertEquals(ret.get_service_name(), "waagent") + self.assertTrue(type(ret) == Redhat6xOSUtil) # pylint: disable=unidiomatic-typecheck + self.assertEquals(ret.get_service_name(), "waagent") # pylint: disable=deprecated-method ret = _get_osutil(distro_name="centos", distro_code_name="", distro_full_name="", distro_version="6") - self.assertTrue(type(ret) == Redhat6xOSUtil) - self.assertEquals(ret.get_service_name(), "waagent") + self.assertTrue(type(ret) == Redhat6xOSUtil) # pylint: disable=unidiomatic-typecheck + self.assertEquals(ret.get_service_name(), "waagent") # pylint: disable=deprecated-method ret = _get_osutil(distro_name="oracle", distro_code_name="", distro_full_name="", distro_version="6") - self.assertTrue(type(ret) == Redhat6xOSUtil) - self.assertEquals(ret.get_service_name(), "waagent") + self.assertTrue(type(ret) == Redhat6xOSUtil) # pylint: disable=unidiomatic-typecheck + self.assertEquals(ret.get_service_name(), "waagent") # pylint: disable=deprecated-method ret = _get_osutil(distro_name="redhat", distro_code_name="", distro_full_name="", distro_version="7") - self.assertTrue(type(ret) == RedhatOSUtil) - self.assertEquals(ret.get_service_name(), "waagent") + self.assertTrue(type(ret) == RedhatOSUtil) # pylint: disable=unidiomatic-typecheck + self.assertEquals(ret.get_service_name(), "waagent") # pylint: disable=deprecated-method ret = _get_osutil(distro_name="centos", distro_code_name="", distro_full_name="", distro_version="7") - self.assertTrue(type(ret) == RedhatOSUtil) - self.assertEquals(ret.get_service_name(), "waagent") + self.assertTrue(type(ret) == RedhatOSUtil) # pylint: disable=unidiomatic-typecheck + self.assertEquals(ret.get_service_name(), "waagent") # pylint: disable=deprecated-method ret = _get_osutil(distro_name="oracle", distro_code_name="", distro_full_name="", distro_version="7") - self.assertTrue(type(ret) == RedhatOSUtil) - self.assertEquals(ret.get_service_name(), "waagent") + self.assertTrue(type(ret) == RedhatOSUtil) # pylint: disable=unidiomatic-typecheck + self.assertEquals(ret.get_service_name(), "waagent") # pylint: disable=deprecated-method def test_get_osutil_it_should_return_euleros(self): ret = _get_osutil(distro_name="euleros", distro_code_name="", distro_version="", distro_full_name="") - self.assertTrue(type(ret) == RedhatOSUtil) - self.assertEquals(ret.get_service_name(), "waagent") + self.assertTrue(type(ret) == RedhatOSUtil) # pylint: disable=unidiomatic-typecheck + self.assertEquals(ret.get_service_name(), "waagent") # pylint: disable=deprecated-method def test_get_osutil_it_should_return_freebsd(self): ret = _get_osutil(distro_name="freebsd", distro_code_name="", distro_version="", distro_full_name="") - self.assertTrue(type(ret) == FreeBSDOSUtil) - self.assertEquals(ret.get_service_name(), "waagent") + self.assertTrue(type(ret) == FreeBSDOSUtil) # pylint: disable=unidiomatic-typecheck + self.assertEquals(ret.get_service_name(), "waagent") # pylint: disable=deprecated-method def test_get_osutil_it_should_return_openbsd(self): ret = _get_osutil(distro_name="openbsd", distro_code_name="", distro_version="", distro_full_name="") - self.assertTrue(type(ret) == OpenBSDOSUtil) - self.assertEquals(ret.get_service_name(), "waagent") + self.assertTrue(type(ret) == OpenBSDOSUtil) # pylint: disable=unidiomatic-typecheck + self.assertEquals(ret.get_service_name(), "waagent") # pylint: disable=deprecated-method def test_get_osutil_it_should_return_bigip(self): ret = _get_osutil(distro_name="bigip", distro_code_name="", distro_version="", distro_full_name="") - self.assertTrue(type(ret) == BigIpOSUtil) - self.assertEquals(ret.get_service_name(), "waagent") + self.assertTrue(type(ret) == BigIpOSUtil) # pylint: disable=unidiomatic-typecheck + self.assertEquals(ret.get_service_name(), "waagent") # pylint: disable=deprecated-method def test_get_osutil_it_should_return_gaia(self): ret = _get_osutil(distro_name="gaia", distro_code_name="", distro_version="", distro_full_name="") - self.assertTrue(type(ret) == GaiaOSUtil) - self.assertEquals(ret.get_service_name(), "waagent") + self.assertTrue(type(ret) == GaiaOSUtil) # pylint: disable=unidiomatic-typecheck + self.assertEquals(ret.get_service_name(), "waagent") # pylint: disable=deprecated-method def test_get_osutil_it_should_return_iosxe(self): ret = _get_osutil(distro_name="iosxe", distro_code_name="", distro_version="", distro_full_name="") - self.assertTrue(type(ret) == IosxeOSUtil) - self.assertEquals(ret.get_service_name(), "waagent") + self.assertTrue(type(ret) == IosxeOSUtil) # pylint: disable=unidiomatic-typecheck + self.assertEquals(ret.get_service_name(), "waagent") # pylint: disable=deprecated-method def test_get_osutil_it_should_return_openwrt(self): ret = _get_osutil(distro_name="openwrt", distro_code_name="", distro_version="", distro_full_name="") - self.assertTrue(type(ret) == OpenWRTOSUtil) - self.assertEquals(ret.get_service_name(), "waagent") + self.assertTrue(type(ret) == OpenWRTOSUtil) # pylint: disable=unidiomatic-typecheck + self.assertEquals(ret.get_service_name(), "waagent") # pylint: disable=deprecated-method diff --git a/tests/common/osutil/test_freebsd.py b/tests/common/osutil/test_freebsd.py index b7caa60913..118f6f6b67 100644 --- a/tests/common/osutil/test_freebsd.py +++ b/tests/common/osutil/test_freebsd.py @@ -18,9 +18,9 @@ from azurelinuxagent.common.osutil.freebsd import FreeBSDOSUtil import azurelinuxagent.common.utils.shellutil as shellutil from .test_default import osutil_get_dhcp_pid_should_return_a_list_of_pids -from tests.tools import AgentTestCase, patch -import traceback -import unittest +from tests.tools import AgentTestCase, patch # pylint: disable=wrong-import-order +import traceback # pylint: disable=wrong-import-order +import unittest # pylint: disable=wrong-import-order class TestFreeBSDOSUtil(AgentTestCase): @@ -118,9 +118,9 @@ def test_no_primary_does_not_throw(self): with patch.object(freebsdosutil, '_get_net_info', return_value=('em0', '10.0.0.1', 'e5:f0:38:aa:da:52')): try: freebsdosutil.get_first_if()[0] - except Exception as e: + except Exception as e: # pylint: disable=unused-variable,invalid-name print(traceback.format_exc()) - exception = True + exception = True # pylint: disable=unused-variable if __name__ == '__main__': unittest.main() diff --git a/tests/common/osutil/test_nsbsd.py b/tests/common/osutil/test_nsbsd.py index 87615f664c..2e5dec5ea2 100644 --- a/tests/common/osutil/test_nsbsd.py +++ b/tests/common/osutil/test_nsbsd.py @@ -17,8 +17,8 @@ from azurelinuxagent.common.utils.fileutil import read_file from azurelinuxagent.common.osutil.nsbsd import NSBSDOSUtil from tests.tools import AgentTestCase, patch -from os import path -import unittest +from os import path # pylint: disable=wrong-import-order +import unittest # pylint: disable=wrong-import-order class TestNSBSDOSUtil(AgentTestCase): @@ -34,19 +34,19 @@ def test_get_dhcp_pid_should_return_a_list_of_pids(self): with patch.object(NSBSDOSUtil, "resolver"): # instantiating NSBSDOSUtil requires a resolver original_isfile = path.isfile - def mock_isfile(path): + def mock_isfile(path): # pylint: disable=redefined-outer-name return True if path == self.dhclient_pid_file else original_isfile(path) original_read_file = read_file - def mock_read_file(file, *args, **kwargs): + def mock_read_file(file, *args, **kwargs): # pylint: disable=redefined-builtin return "123" if file == self.dhclient_pid_file else original_read_file(file, *args, **kwargs) with patch("os.path.isfile", mock_isfile): with patch("azurelinuxagent.common.osutil.nsbsd.fileutil.read_file", mock_read_file): pid_list = NSBSDOSUtil().get_dhcp_pid() - self.assertEquals(pid_list, [123]) + self.assertEquals(pid_list, [123]) # pylint: disable=deprecated-method def test_get_dhcp_pid_should_return_an_empty_list_when_the_dhcp_client_is_not_running(self): with patch.object(NSBSDOSUtil, "resolver"): # instantiating NSBSDOSUtil requires a resolver @@ -55,32 +55,32 @@ def test_get_dhcp_pid_should_return_an_empty_list_when_the_dhcp_client_is_not_ru # original_isfile = path.isfile - def mock_isfile(path): + def mock_isfile(path): # pylint: disable=redefined-outer-name return False if path == self.dhclient_pid_file else original_isfile(path) with patch("os.path.isfile", mock_isfile): pid_list = NSBSDOSUtil().get_dhcp_pid() - self.assertEquals(pid_list, []) + self.assertEquals(pid_list, []) # pylint: disable=deprecated-method # # PID file is empty # original_isfile = path.isfile - def mock_isfile(path): + def mock_isfile(path): # pylint: disable=redefined-outer-name,function-redefined return True if path == self.dhclient_pid_file else original_isfile(path) original_read_file = read_file - def mock_read_file(file, *args, **kwargs): + def mock_read_file(file, *args, **kwargs): # pylint: disable=redefined-builtin return "" if file == self.dhclient_pid_file else original_read_file(file, *args, **kwargs) with patch("os.path.isfile", mock_isfile): with patch("azurelinuxagent.common.osutil.nsbsd.fileutil.read_file", mock_read_file): pid_list = NSBSDOSUtil().get_dhcp_pid() - self.assertEquals(pid_list, []) + self.assertEquals(pid_list, []) # pylint: disable=deprecated-method if __name__ == '__main__': diff --git a/tests/common/osutil/test_openbsd.py b/tests/common/osutil/test_openbsd.py index 1cee03f46a..ed20725060 100644 --- a/tests/common/osutil/test_openbsd.py +++ b/tests/common/osutil/test_openbsd.py @@ -16,8 +16,8 @@ # from azurelinuxagent.common.osutil.openbsd import OpenBSDOSUtil from .test_default import osutil_get_dhcp_pid_should_return_a_list_of_pids -from tests.tools import AgentTestCase -import unittest +from tests.tools import AgentTestCase # pylint: disable=wrong-import-order +import unittest # pylint: disable=wrong-import-order class TestAlpineOSUtil(AgentTestCase): diff --git a/tests/common/osutil/test_openwrt.py b/tests/common/osutil/test_openwrt.py index b6286dc9cf..594ed89956 100644 --- a/tests/common/osutil/test_openwrt.py +++ b/tests/common/osutil/test_openwrt.py @@ -16,8 +16,8 @@ # from azurelinuxagent.common.osutil.openwrt import OpenWRTOSUtil from .test_default import osutil_get_dhcp_pid_should_return_a_list_of_pids -from tests.tools import AgentTestCase -import unittest +from tests.tools import AgentTestCase # pylint: disable=wrong-import-order +import unittest # pylint: disable=wrong-import-order class TestOpenWRTOSUtil(AgentTestCase): diff --git a/tests/common/osutil/test_redhat.py b/tests/common/osutil/test_redhat.py index c2926e0372..4aef0f8430 100644 --- a/tests/common/osutil/test_redhat.py +++ b/tests/common/osutil/test_redhat.py @@ -16,8 +16,8 @@ # from azurelinuxagent.common.osutil.redhat import Redhat6xOSUtil from .test_default import osutil_get_dhcp_pid_should_return_a_list_of_pids -from tests.tools import AgentTestCase -import unittest +from tests.tools import AgentTestCase # pylint: disable=wrong-import-order +import unittest # pylint: disable=wrong-import-order class TestRedhat6xOSUtil(AgentTestCase): diff --git a/tests/common/osutil/test_suse.py b/tests/common/osutil/test_suse.py index e3ea661d92..dfdd822af8 100644 --- a/tests/common/osutil/test_suse.py +++ b/tests/common/osutil/test_suse.py @@ -16,8 +16,8 @@ # from azurelinuxagent.common.osutil.suse import SUSE11OSUtil from .test_default import osutil_get_dhcp_pid_should_return_a_list_of_pids -from tests.tools import AgentTestCase -import unittest +from tests.tools import AgentTestCase # pylint: disable=wrong-import-order +import unittest # pylint: disable=wrong-import-order class TestSUSE11OSUtil(AgentTestCase): diff --git a/tests/common/osutil/test_ubuntu.py b/tests/common/osutil/test_ubuntu.py index 995c1c4b85..c5d926125c 100644 --- a/tests/common/osutil/test_ubuntu.py +++ b/tests/common/osutil/test_ubuntu.py @@ -19,7 +19,7 @@ from azurelinuxagent.common.osutil.ubuntu import Ubuntu12OSUtil, Ubuntu18OSUtil from .test_default import osutil_get_dhcp_pid_should_return_a_list_of_pids -from tests.tools import AgentTestCase +from tests.tools import AgentTestCase # pylint: disable=wrong-import-order class TestUbuntu12OSUtil(AgentTestCase): diff --git a/tests/common/test_cgroupapi.py b/tests/common/test_cgroupapi.py index 0ab3cd228a..34534bd54e 100644 --- a/tests/common/test_cgroupapi.py +++ b/tests/common/test_cgroupapi.py @@ -72,17 +72,17 @@ def test_cgroups_should_be_supported_only_on_ubuntu_16_and_later(self): with patch("azurelinuxagent.common.cgroupapi.get_distro", return_value=distro): self.assertEqual(CGroupsApi.cgroups_supported(), supported, "cgroups_supported() failed on {0}".format(distro)) - def test_create_should_return_a_SystemdCgroupsApi_on_systemd_platforms(self): + def test_create_should_return_a_SystemdCgroupsApi_on_systemd_platforms(self): # pylint: disable=invalid-name with patch("azurelinuxagent.common.cgroupapi.CGroupsApi._is_systemd", return_value=True): api = CGroupsApi.create() - self.assertTrue(type(api) == SystemdCgroupsApi) + self.assertTrue(type(api) == SystemdCgroupsApi) # pylint: disable=unidiomatic-typecheck - def test_create_should_return_a_FileSystemCgroupsApi_on_non_systemd_platforms(self): + def test_create_should_return_a_FileSystemCgroupsApi_on_non_systemd_platforms(self): # pylint: disable=invalid-name with patch("azurelinuxagent.common.cgroupapi.CGroupsApi._is_systemd", return_value=False): api = CGroupsApi.create() - self.assertTrue(type(api) == FileSystemCgroupsApi) + self.assertTrue(type(api) == FileSystemCgroupsApi) # pylint: disable=unidiomatic-typecheck def test_is_systemd_should_return_true_when_systemd_manages_current_process(self): path_exists = os.path.exists @@ -96,7 +96,7 @@ def mock_path_exists(path): mock_path_exists.path_tested = False with patch("azurelinuxagent.common.cgroupapi.os.path.exists", mock_path_exists): - is_systemd = CGroupsApi._is_systemd() + is_systemd = CGroupsApi._is_systemd() # pylint: disable=protected-access self.assertTrue(is_systemd) @@ -114,7 +114,7 @@ def mock_path_exists(path): mock_path_exists.path_tested = False with patch("azurelinuxagent.common.cgroupapi.os.path.exists", mock_path_exists): - is_systemd = CGroupsApi._is_systemd() + is_systemd = CGroupsApi._is_systemd() # pylint: disable=protected-access self.assertFalse(is_systemd) @@ -126,7 +126,7 @@ def test_foreach_controller_should_execute_operation_on_all_mounted_controllers( def controller_operation(controller): executed_controllers.append(controller) - CGroupsApi._foreach_controller(controller_operation, 'A dummy message') + CGroupsApi._foreach_controller(controller_operation, 'A dummy message') # pylint: disable=protected-access # The setUp method mocks azurelinuxagent.common.cgroupapi.CGROUPS_FILE_SYSTEM_ROOT to have the cpu and memory controllers mounted self.assertIn('cpu', executed_controllers, 'The operation was not executed on the cpu controller') @@ -143,17 +143,17 @@ def controller_operation(controller): successful_controllers.append(controller) with patch("azurelinuxagent.common.cgroupapi.logger.warn") as mock_logger_warn: - CGroupsApi._foreach_controller(controller_operation, 'A dummy message') + CGroupsApi._foreach_controller(controller_operation, 'A dummy message') # pylint: disable=protected-access self.assertIn('memory', successful_controllers, 'The operation was not executed on the memory controller') self.assertEqual(len(successful_controllers), 1, 'The operation was not executed on unexpected controllers: {0}'.format(successful_controllers)) - args, kwargs = mock_logger_warn.call_args + args, kwargs = mock_logger_warn.call_args # pylint: disable=unused-variable (message_format, controller, error, message) = args - self.assertEquals(message_format, 'Error in cgroup controller "{0}": {1}. {2}') - self.assertEquals(controller, 'cpu') - self.assertEquals(error, 'A test exception') - self.assertEquals(message, 'A dummy message') + self.assertEquals(message_format, 'Error in cgroup controller "{0}": {1}. {2}') # pylint: disable=deprecated-method + self.assertEquals(controller, 'cpu') # pylint: disable=deprecated-method + self.assertEquals(error, 'A test exception') # pylint: disable=deprecated-method + self.assertEquals(message, 'A dummy message') # pylint: disable=deprecated-method class MountCgroupsTestCase(AgentTestCase): @@ -171,7 +171,7 @@ def tearDown(self): def _get_mount_commands(mock): mount_commands = '' for call_args in mock.call_args_list: - args, kwargs = call_args + args, kwargs = call_args # pylint: disable=unused-variable mount_commands += ';' + args[0] return mount_commands @@ -260,7 +260,7 @@ def mock_run_get_output(cmd, *args, **kwargs): self.assertRegex(mount_commands, ';mount.* cpu,cpuacct ', 'The cpu controller was not mounted') # A warning should have been logged for the memory controller - args, kwargs = mock_logger_warn.call_args + args, kwargs = mock_logger_warn.call_args # pylint: disable=unused-variable self.assertIn('A test exception mounting the memory controller', args) def test_mount_cgroups_should_raise_when_the_cgroups_filesystem_fails_to_mount(self): @@ -313,7 +313,7 @@ def mock_run_get_output(cmd, *args, **kwargs): with patch("azurelinuxagent.common.osutil.default.os.symlink") as patch_symlink: FileSystemCgroupsApi.mount_cgroups() - self.assertEquals(patch_symlink.call_count, 0, 'A symbolic link should not have been created') + self.assertEquals(patch_symlink.call_count, 0, 'A symbolic link should not have been created') # pylint: disable=deprecated-method class FileSystemCgroupsApiTestCase(_MockedFileSystemTestCase): @@ -346,7 +346,7 @@ def test_cleanup_legacy_cgroups_should_move_daemon_pid_to_new_cgroup_and_remove_ self.assertFalse(os.path.exists(legacy_memory_cgroup)) # Assert the event parameters that were sent out - self.assertEquals(len(mock_add_event.call_args_list), 2) + self.assertEquals(len(mock_add_event.call_args_list), 2) # pylint: disable=deprecated-method self.assertTrue(all(kwargs['op'] == 'CGroupsCleanUp' for _, kwargs in mock_add_event.call_args_list)) self.assertTrue(all(kwargs['is_success'] for _, kwargs in mock_add_event.call_args_list)) self.assertTrue(any( @@ -414,7 +414,7 @@ def test_remove_extension_cgroups_should_log_a_warning_when_the_cgroup_contains_ with patch("azurelinuxagent.common.cgroupapi.os.rmdir", side_effect=OSError(16, "Device or resource busy")): api.remove_extension_cgroups("Microsoft.Compute.TestExtension-1.2.3") - args, kwargs = mock_logger_warn.call_args + args, kwargs = mock_logger_warn.call_args # pylint: disable=unused-variable message = args[0] self.assertIn("still has active tasks", message) @@ -431,7 +431,7 @@ def test_get_extension_cgroups_should_return_all_cgroups(self): self.assertTrue(any(retrieved_cgroup.path == cgroup.path for retrieved_cgroup in retrieved)) @patch('time.sleep', side_effect=lambda _: mock_sleep()) - def test_start_extension_command_should_add_the_child_process_to_the_extension_cgroup(self, _): + def test_start_extension_command_should_add_the_child_process_to_the_extension_cgroup(self, _): # pylint: disable=too-many-locals api = FileSystemCgroupsApi() api.create_extension_cgroups_root() @@ -449,20 +449,20 @@ def test_start_extension_command_should_add_the_child_process_to_the_extension_c # The expected format of the process output is [stdout]\n{PID}\n\n\n[stderr]\n" pattern = re.compile(r"\[stdout\]\n(\d+)\n\n\n\[stderr\]\n") - m = pattern.match(process_output) + m = pattern.match(process_output) # pylint: disable=invalid-name try: pid_from_output = int(m.group(1)) - except Exception as e: + except Exception as e: # pylint: disable=invalid-name self.fail("No PID could be extracted from the process output! Error: {0}".format(ustr(e))) for cgroup in extension_cgroups: cgroups_procs_path = os.path.join(cgroup.path, "cgroup.procs") - with open(cgroups_procs_path, "r") as f: + with open(cgroups_procs_path, "r") as f: # pylint: disable=invalid-name contents = f.read() pid_from_cgroup = int(contents) - self.assertEquals(pid_from_output, pid_from_cgroup, + self.assertEquals(pid_from_output, pid_from_cgroup, # pylint: disable=deprecated-method "The PID from the process output ({0}) does not match the PID found in the" "process cgroup {1} ({2})".format(pid_from_output, cgroups_procs_path, pid_from_cgroup)) @@ -472,26 +472,26 @@ class SystemdCgroupsApiTestCase(AgentTestCase): def test_get_systemd_version_should_return_a_version_number(self): with mock_cgroup_commands(): version_info = SystemdCgroupsApi.get_systemd_version() - found = re.search("systemd \d+", version_info) is not None + found = re.search("systemd \d+", version_info) is not None # pylint: disable=anomalous-backslash-in-string self.assertTrue(found, "Could not determine the systemd version: {0}".format(version_info)) def test_get_cpu_and_memory_mount_points_should_return_the_cgroup_mount_points(self): with mock_cgroup_commands(): cpu, memory = SystemdCgroupsApi().get_cgroup_mount_points() - self.assertEquals(cpu, '/sys/fs/cgroup/cpu,cpuacct', "The mount point for the CPU controller is incorrect") - self.assertEquals(memory, '/sys/fs/cgroup/memory', "The mount point for the memory controller is incorrect") + self.assertEquals(cpu, '/sys/fs/cgroup/cpu,cpuacct', "The mount point for the CPU controller is incorrect") # pylint: disable=deprecated-method + self.assertEquals(memory, '/sys/fs/cgroup/memory', "The mount point for the memory controller is incorrect") # pylint: disable=deprecated-method def test_get_cpu_and_memory_cgroup_relative_paths_for_process_should_return_the_cgroup_relative_paths(self): with mock_cgroup_commands(): cpu, memory = SystemdCgroupsApi.get_process_cgroup_relative_paths('self') - self.assertEquals(cpu, "system.slice/walinuxagent.service", "The relative path for the CPU cgroup is incorrect") - self.assertEquals(memory, "system.slice/walinuxagent.service", "The relative memory for the CPU cgroup is incorrect") + self.assertEquals(cpu, "system.slice/walinuxagent.service", "The relative path for the CPU cgroup is incorrect") # pylint: disable=deprecated-method + self.assertEquals(memory, "system.slice/walinuxagent.service", "The relative memory for the CPU cgroup is incorrect") # pylint: disable=deprecated-method def test_get_cgroup2_controllers_should_return_the_v2_cgroup_controllers(self): with mock_cgroup_commands(): mount_point, controllers = SystemdCgroupsApi.get_cgroup2_controllers() - self.assertEquals(mount_point, "/sys/fs/cgroup/unified", "Invalid mount point for V2 cgroups") + self.assertEquals(mount_point, "/sys/fs/cgroup/unified", "Invalid mount point for V2 cgroups") # pylint: disable=deprecated-method self.assertIn("cpu", controllers, "The CPU controller is not in the list of V2 controllers") self.assertIn("memory", controllers, "The memory controller is not in the list of V2 controllers") @@ -499,15 +499,15 @@ def test_get_unit_property_should_return_the_value_of_the_given_property(self): with mock_cgroup_commands(): cpu_accounting = SystemdCgroupsApi.get_unit_property("walinuxagent.service", "CPUAccounting") - self.assertEquals(cpu_accounting, "no", "Property {0} of {1} is incorrect".format("CPUAccounting", "walinuxagent.service")) + self.assertEquals(cpu_accounting, "no", "Property {0} of {1} is incorrect".format("CPUAccounting", "walinuxagent.service")) # pylint: disable=deprecated-method def test_get_extensions_slice_root_name_should_return_the_root_slice_for_extensions(self): - root_slice_name = SystemdCgroupsApi()._get_extensions_slice_root_name() + root_slice_name = SystemdCgroupsApi()._get_extensions_slice_root_name() # pylint: disable=protected-access self.assertEqual(root_slice_name, "system-walinuxagent.extensions.slice") def test_get_extension_slice_name_should_return_the_slice_for_the_given_extension(self): extension_name = "Microsoft.Azure.DummyExtension-1.0" - extension_slice_name = SystemdCgroupsApi()._get_extension_slice_name(extension_name) + extension_slice_name = SystemdCgroupsApi()._get_extension_slice_name(extension_name) # pylint: disable=protected-access self.assertEqual(extension_slice_name, "system-walinuxagent.extensions-Microsoft.Azure.DummyExtension_1.0.slice") @attr('requires_sudo') @@ -516,7 +516,7 @@ def test_create_extension_cgroups_root_should_create_extensions_root_slice(self) SystemdCgroupsApi().create_extension_cgroups_root() - unit_name = SystemdCgroupsApi()._get_extensions_slice_root_name() + unit_name = SystemdCgroupsApi()._get_extensions_slice_root_name() # pylint: disable=protected-access _, status = shellutil.run_get_output("systemctl status {0}".format(unit_name)) self.assertIn("Loaded: loaded", status) self.assertIn("Active: active", status) @@ -536,7 +536,7 @@ def test_get_processes_in_cgroup_should_return_the_processes_within_the_cgroup(s daemon_present = any("waagent -daemon" in command for (pid, command) in processes) self.assertTrue(daemon_present, "Could not find the daemon in the cgroup: [{0}]".format(processes)) - extension_handler_present = any(re.search("(WALinuxAgent-.+\.egg|waagent) -run-exthandlers", command) for (pid, command) in processes) + extension_handler_present = any(re.search("(WALinuxAgent-.+\.egg|waagent) -run-exthandlers", command) for (pid, command) in processes) # pylint: disable=anomalous-backslash-in-string self.assertTrue(extension_handler_present, "Could not find the extension handler in the cgroup: [{0}]".format(processes)) @attr('requires_sudo') @@ -549,7 +549,7 @@ def test_create_extension_cgroups_should_create_extension_slice(self): self.assertEqual(cpu_cgroup.path, "/sys/fs/cgroup/cpu/system.slice/Microsoft.Azure.DummyExtension_1.0") self.assertEqual(memory_cgroup.path, "/sys/fs/cgroup/memory/system.slice/Microsoft.Azure.DummyExtension_1.0") - unit_name = SystemdCgroupsApi()._get_extension_slice_name(extension_name) + unit_name = SystemdCgroupsApi()._get_extension_slice_name(extension_name) # pylint: disable=protected-access self.assertEqual("system-walinuxagent.extensions-Microsoft.Azure.DummyExtension_1.0.slice", unit_name) _, status = shellutil.run_get_output("systemctl status {0}".format(unit_name)) @@ -591,9 +591,9 @@ def mock_popen(command, *args, **kwargs): command = "echo TEST_OUTPUT" return original_popen(command, *args, **kwargs) - with mock_cgroup_commands() as mock_commands: + with mock_cgroup_commands() as mock_commands: # pylint: disable=unused-variable with tempfile.TemporaryFile(dir=self.tmp_dir, mode="w+b") as output_file: - with patch("azurelinuxagent.common.cgroupapi.subprocess.Popen", side_effect=mock_popen) as popen_patch: + with patch("azurelinuxagent.common.cgroupapi.subprocess.Popen", side_effect=mock_popen) as popen_patch: # pylint: disable=unused-variable command_output = SystemdCgroupsApi().start_extension_command( extension_name="Microsoft.Compute.TestExtension-1.2.3", command="A_TEST_COMMAND", @@ -619,7 +619,7 @@ def test_start_extension_command_should_execute_the_command_in_a_cgroup(self, _) stdout=subprocess.PIPE, stderr=subprocess.PIPE) - tracked = CGroupsTelemetry._tracked + tracked = CGroupsTelemetry._tracked # pylint: disable=protected-access self.assertTrue( any(cg for cg in tracked if cg.name == 'Microsoft.Compute.TestExtension-1.2.3' and 'cpu' in cg.path), @@ -644,7 +644,7 @@ def test_start_extension_command_should_use_systemd_to_execute_the_command(self, extension_calls = [args[0] for (args, _) in popen_patch.call_args_list if "the-test-extension-command" in args[0]] - self.assertEquals(1, len(extension_calls), "The extension should have been invoked exactly once") + self.assertEquals(1, len(extension_calls), "The extension should have been invoked exactly once") # pylint: disable=deprecated-method self.assertIn("systemd-run --unit=Microsoft.Compute.TestExtension_1.2.3", extension_calls[0], "The extension should have been invoked using systemd") @patch('time.sleep', side_effect=lambda _: mock_sleep()) @@ -679,18 +679,18 @@ def mock_popen(command, *args, **kwargs): kwargs['message']) self.assertIn("Failed to find executable syntax_error: No such file or directory", kwargs['message']) - self.assertEquals(False, kwargs['is_success']) - self.assertEquals('InvokeCommandUsingSystemd', kwargs['op']) + self.assertEquals(False, kwargs['is_success']) # pylint: disable=deprecated-method + self.assertEquals('InvokeCommandUsingSystemd', kwargs['op']) # pylint: disable=deprecated-method extension_calls = [args[0] for (args, _) in popen_patch.call_args_list if command in args[0]] - self.assertEquals(2, len(extension_calls), "The extension should have been invoked exactly twice") + self.assertEquals(2, len(extension_calls), "The extension should have been invoked exactly twice") # pylint: disable=deprecated-method self.assertIn("systemd-run --unit=Microsoft.Compute.TestExtension_1.2.3", extension_calls[0], "The first call to the extension should have used systemd") - self.assertEquals(command, extension_calls[1], + self.assertEquals(command, extension_calls[1], # pylint: disable=deprecated-method "The second call to the extension should not have used systemd") - self.assertEquals(len(CGroupsTelemetry._tracked), 0, "No cgroups should have been created") + self.assertEquals(len(CGroupsTelemetry._tracked), 0, "No cgroups should have been created") # pylint: disable=deprecated-method,protected-access self.assertIn("TEST_OUTPUT\n", command_output, "The test output was not captured") @@ -728,11 +728,11 @@ def mock_popen(*args, **kwargs): extension_calls = [args[0] for (args, _) in popen_patch.call_args_list if "echo 'success'" in args[0]] - self.assertEquals(2, len(extension_calls), "The extension should have been invoked exactly twice") + self.assertEquals(2, len(extension_calls), "The extension should have been invoked exactly twice") # pylint: disable=deprecated-method self.assertIn("systemd-run --unit=Microsoft.Compute.TestExtension_1.2.3", extension_calls[0], "The first call to the extension should have used systemd") - self.assertEquals("echo 'success'", extension_calls[1], "The second call to the extension should not have used systemd") + self.assertEquals("echo 'success'", extension_calls[1], "The second call to the extension should not have used systemd") # pylint: disable=deprecated-method - self.assertEquals(len(CGroupsTelemetry._tracked), 0, "No cgroups should have been created") + self.assertEquals(len(CGroupsTelemetry._tracked), 0, "No cgroups should have been created") # pylint: disable=deprecated-method,protected-access @attr('requires_sudo') @patch("azurelinuxagent.common.cgroupapi.add_event") @@ -757,11 +757,11 @@ def test_start_extension_command_should_not_use_fallback_option_if_extension_fai extension_calls = [args[0] for (args, _) in popen_patch.call_args_list if command in args[0]] - self.assertEquals(1, len(extension_calls), "The extension should have been invoked exactly once") + self.assertEquals(1, len(extension_calls), "The extension should have been invoked exactly once") # pylint: disable=deprecated-method self.assertIn("systemd-run --unit=Microsoft.Compute.TestExtension_1.2.3", extension_calls[0], "The first call to the extension should have used systemd") - self.assertEquals(context_manager.exception.code, ExtensionErrorCodes.PluginUnknownFailure) + self.assertEquals(context_manager.exception.code, ExtensionErrorCodes.PluginUnknownFailure) # pylint: disable=deprecated-method self.assertIn("Non-zero exit code", ustr(context_manager.exception)) # The scope name should appear in the process output since systemd-run was invoked and stderr # wasn't truncated. @@ -793,11 +793,11 @@ def test_start_extension_command_should_not_use_fallback_option_if_extension_fai extension_calls = [args[0] for (args, _) in popen_patch.call_args_list if long_stdout_stderr_command in args[0]] - self.assertEquals(1, len(extension_calls), "The extension should have been invoked exactly once") + self.assertEquals(1, len(extension_calls), "The extension should have been invoked exactly once") # pylint: disable=deprecated-method self.assertIn("systemd-run --unit=Microsoft.Compute.TestExtension_1.2.3", extension_calls[0], "The first call to the extension should have used systemd") - self.assertEquals(context_manager.exception.code, ExtensionErrorCodes.PluginUnknownFailure) + self.assertEquals(context_manager.exception.code, ExtensionErrorCodes.PluginUnknownFailure) # pylint: disable=deprecated-method self.assertIn("Non-zero exit code", ustr(context_manager.exception)) # stdout and stderr should have been truncated, so the scope name doesn't appear in stderr # even though systemd-run ran @@ -805,7 +805,7 @@ def test_start_extension_command_should_not_use_fallback_option_if_extension_fai @attr('requires_sudo') @patch("azurelinuxagent.common.cgroupapi.add_event") - def test_start_extension_command_should_not_use_fallback_option_if_extension_times_out(self, *args): + def test_start_extension_command_should_not_use_fallback_option_if_extension_times_out(self, *args): # pylint: disable=unused-argument self.assertTrue(i_am_root(), "Test does not run when non-root") with tempfile.TemporaryFile(dir=self.tmp_dir, mode="w+b") as stdout: @@ -825,7 +825,7 @@ def test_start_extension_command_should_not_use_fallback_option_if_extension_tim stdout=stdout, stderr=stderr) - self.assertEquals(context_manager.exception.code, + self.assertEquals(context_manager.exception.code, # pylint: disable=deprecated-method ExtensionErrorCodes.PluginHandlerScriptTimedout) self.assertIn("Timeout", ustr(context_manager.exception)) @@ -856,7 +856,7 @@ def mock_popen(*args, **kwargs): stdout=stdout, stderr=stderr) - self.assertEquals(expected_output.format("very specific test message"), process_output) + self.assertEquals(expected_output.format("very specific test message"), process_output) # pylint: disable=deprecated-method class SystemdCgroupsApiMockedFileSystemTestCase(_MockedFileSystemTestCase): @@ -869,10 +869,10 @@ def test_cleanup_legacy_cgroups_should_remove_legacy_cgroups(self): legacy_cpu_cgroup = CGroupsTools.create_legacy_agent_cgroup(self.cgroups_file_system_root, "cpu", '') legacy_memory_cgroup = CGroupsTools.create_legacy_agent_cgroup(self.cgroups_file_system_root, "memory", '') - with patch("azurelinuxagent.common.cgroupapi.add_event") as mock_add_event: + with patch("azurelinuxagent.common.cgroupapi.add_event") as mock_add_event: # pylint: disable=unused-variable with patch("azurelinuxagent.common.cgroupapi.get_agent_pid_file_path", return_value=daemon_pid_file): legacy_cgroups = SystemdCgroupsApi().cleanup_legacy_cgroups() - self.assertEquals(legacy_cgroups, 2, "cleanup_legacy_cgroups() did not find all the expected cgroups") + self.assertEquals(legacy_cgroups, 2, "cleanup_legacy_cgroups() did not find all the expected cgroups") # pylint: disable=deprecated-method self.assertFalse(os.path.exists(legacy_cpu_cgroup), "cleanup_legacy_cgroups() did not remove the CPU legacy cgroup") self.assertFalse(os.path.exists(legacy_memory_cgroup), "cleanup_legacy_cgroups() did not remove the memory legacy cgroup") diff --git a/tests/common/test_cgroupconfigurator.py b/tests/common/test_cgroupconfigurator.py index 5e33c53c49..061c6a52e7 100644 --- a/tests/common/test_cgroupconfigurator.py +++ b/tests/common/test_cgroupconfigurator.py @@ -31,12 +31,12 @@ class CGroupConfiguratorSystemdTestCase(AgentTestCase): @classmethod def tearDownClass(cls): - CGroupConfigurator._instance = None + CGroupConfigurator._instance = None # pylint: disable=protected-access AgentTestCase.tearDownClass() @staticmethod def _get_new_cgroup_configurator_instance(initialize=True): - CGroupConfigurator._instance = None + CGroupConfigurator._instance = None # pylint: disable=protected-access configurator = CGroupConfigurator.get_instance() if initialize: with mock_cgroup_commands(): @@ -46,7 +46,7 @@ def _get_new_cgroup_configurator_instance(initialize=True): def test_initialize_should_start_tracking_the_agent_cgroups(self): CGroupConfiguratorSystemdTestCase._get_new_cgroup_configurator_instance() - tracked = CGroupsTelemetry._tracked + tracked = CGroupsTelemetry._tracked # pylint: disable=protected-access self.assertTrue( any(cg for cg in tracked if cg.name == 'walinuxagent.service' and 'cpu' in cg.path), @@ -66,7 +66,7 @@ def test_enable_and_disable_should_change_the_enabled_state_of_cgroups(self): configurator.enable() self.assertTrue(configurator.enabled(), "enable() should enable the CGroupConfigurator") - def test_enable_should_raise_CGroupsException_when_cgroups_are_not_supported(self): + def test_enable_should_raise_CGroupsException_when_cgroups_are_not_supported(self): # pylint: disable=invalid-name with patch("azurelinuxagent.common.cgroupapi.CGroupsApi.cgroups_supported", return_value=False): configurator = CGroupConfiguratorSystemdTestCase._get_new_cgroup_configurator_instance(initialize=False) configurator.initialize() @@ -82,7 +82,7 @@ def test_disable_should_reset_tracked_cgroups(self): CGroupConfiguratorSystemdTestCase._get_new_cgroup_configurator_instance().disable() - self.assertEquals(len(CGroupsTelemetry._tracked), 0) + self.assertEquals(len(CGroupsTelemetry._tracked), 0) # pylint: disable=deprecated-method,protected-access def test_cgroup_operations_should_not_invoke_the_cgroup_api_when_cgroups_are_not_enabled(self): configurator = CGroupConfiguratorSystemdTestCase._get_new_cgroup_configurator_instance() @@ -90,12 +90,12 @@ def test_cgroup_operations_should_not_invoke_the_cgroup_api_when_cgroups_are_not # List of operations to test, and the functions to mock used in order to do verifications operations = [ - [lambda: configurator.create_extension_cgroups_root(), "azurelinuxagent.common.cgroupapi.SystemdCgroupsApi.create_extension_cgroups_root"], + [lambda: configurator.create_extension_cgroups_root(), "azurelinuxagent.common.cgroupapi.SystemdCgroupsApi.create_extension_cgroups_root"], # pylint: disable=unnecessary-lambda [lambda: configurator.create_extension_cgroups("A.B.C-1.0.0"), "azurelinuxagent.common.cgroupapi.SystemdCgroupsApi.create_extension_cgroups"], [lambda: configurator.remove_extension_cgroups("A.B.C-1.0.0"), "azurelinuxagent.common.cgroupapi.SystemdCgroupsApi.remove_extension_cgroups"] ] - for op in operations: + for op in operations: # pylint: disable=invalid-name with patch(op[1]) as mock_cgroup_api_operation: op[0]() @@ -108,7 +108,7 @@ def test_cgroup_operations_should_log_a_warning_when_the_cgroup_api_raises_an_ex with patch.object(configurator, "disable"): # List of operations to test, and the functions to mock in order to raise exceptions operations = [ - [lambda: configurator.create_extension_cgroups_root(), "azurelinuxagent.common.cgroupapi.SystemdCgroupsApi.create_extension_cgroups_root"], + [lambda: configurator.create_extension_cgroups_root(), "azurelinuxagent.common.cgroupapi.SystemdCgroupsApi.create_extension_cgroups_root"], # pylint: disable=unnecessary-lambda [lambda: configurator.create_extension_cgroups("A.B.C-1.0.0"), "azurelinuxagent.common.cgroupapi.SystemdCgroupsApi.create_extension_cgroups"], [lambda: configurator.remove_extension_cgroups("A.B.C-1.0.0"), "azurelinuxagent.common.cgroupapi.SystemdCgroupsApi.remove_extension_cgroups"] ] @@ -116,14 +116,14 @@ def test_cgroup_operations_should_log_a_warning_when_the_cgroup_api_raises_an_ex def raise_exception(*_): raise Exception("A TEST EXCEPTION") - for op in operations: + for op in operations: # pylint: disable=invalid-name with patch("azurelinuxagent.common.cgroupconfigurator.logger.warn") as mock_logger_warn: with patch(op[1], raise_exception): op[0]() - self.assertEquals(mock_logger_warn.call_count, 1) + self.assertEquals(mock_logger_warn.call_count, 1) # pylint: disable=deprecated-method - args, kwargs = mock_logger_warn.call_args + args, kwargs = mock_logger_warn.call_args # pylint: disable=unused-variable message = args[0] self.assertIn("A TEST EXCEPTION", message) @@ -139,7 +139,7 @@ def test_get_processes_in_agent_cgroup_should_return_the_processes_within_the_ag daemon_present = any("waagent -daemon" in command for (pid, command) in processes) self.assertTrue(daemon_present, "Could not find the daemon in the cgroup: [{0}]".format(processes)) - extension_handler_present = any(re.search("(WALinuxAgent-.+\.egg|waagent) -run-exthandlers", command) for (pid, command) in processes) + extension_handler_present = any(re.search("(WALinuxAgent-.+\.egg|waagent) -run-exthandlers", command) for (pid, command) in processes) # pylint: disable=anomalous-backslash-in-string self.assertTrue(extension_handler_present, "Could not find the extension handler in the cgroup: [{0}]".format(processes)) @patch('time.sleep', side_effect=lambda _: mock_sleep()) @@ -197,7 +197,7 @@ def test_start_extension_command_should_start_tracking_the_extension_cgroups(sel stdout=subprocess.PIPE, stderr=subprocess.PIPE) - tracked = CGroupsTelemetry._tracked + tracked = CGroupsTelemetry._tracked # pylint: disable=protected-access self.assertTrue( any(cg for cg in tracked if cg.name == 'Microsoft.Compute.TestExtension-1.2.3' and 'cpu' in cg.path), @@ -216,7 +216,7 @@ def mock_popen(command_arg, *args, **kwargs): raise Exception("A TEST EXCEPTION") return original_popen(command_arg, *args, **kwargs) - with patch("azurelinuxagent.common.cgroupapi.subprocess.Popen", side_effect=mock_popen) as patcher: + with patch("azurelinuxagent.common.cgroupapi.subprocess.Popen", side_effect=mock_popen) as patcher: # pylint: disable=unused-variable with self.assertRaises(Exception) as context_manager: configurator.start_extension_command( extension_name="Microsoft.Compute.TestExtension-1.2.3", diff --git a/tests/common/test_cgroups.py b/tests/common/test_cgroups.py index 31412a4f1d..4183a65e0d 100644 --- a/tests/common/test_cgroups.py +++ b/tests/common/test_cgroups.py @@ -29,7 +29,7 @@ def consume_cpu_time(): waste = 0 - for x in range(1, 200000): + for x in range(1, 200000): # pylint: disable=unused-variable,invalid-name waste += random.random() return waste @@ -128,8 +128,8 @@ def test_initialize_cpu_usage_should_set_current_cpu_usage(self): cgroup.initialize_cpu_usage() - self.assertEquals(cgroup._current_cgroup_cpu, 63763) - self.assertEquals(cgroup._current_system_cpu, 5496872) + self.assertEquals(cgroup._current_cgroup_cpu, 63763) # pylint: disable=deprecated-method,protected-access + self.assertEquals(cgroup._current_system_cpu, 5496872) # pylint: disable=deprecated-method,protected-access def test_get_cpu_usage_should_return_the_cpu_usage_since_its_last_invocation(self): cgroup = CpuCgroup("test", "/sys/fs/cgroup/cpu/system.slice/test") @@ -148,7 +148,7 @@ def test_get_cpu_usage_should_return_the_cpu_usage_since_its_last_invocation(sel cpu_usage = cgroup.get_cpu_usage() - self.assertEquals(cpu_usage, 0.031) + self.assertEquals(cpu_usage, 0.031) # pylint: disable=deprecated-method TestCpuCgroup.mock_read_file_map = { "/proc/stat": os.path.join(data_dir, "cgroups", "proc_stat_t2"), @@ -157,7 +157,7 @@ def test_get_cpu_usage_should_return_the_cpu_usage_since_its_last_invocation(sel cpu_usage = cgroup.get_cpu_usage() - self.assertEquals(cpu_usage, 0.045) + self.assertEquals(cpu_usage, 0.045) # pylint: disable=deprecated-method def test_initialie_cpu_usage_should_set_the_cgroup_usage_to_0_when_the_cgroup_does_not_exist(self): cgroup = CpuCgroup("test", "/sys/fs/cgroup/cpu/system.slice/test") @@ -172,8 +172,8 @@ def test_initialie_cpu_usage_should_set_the_cgroup_usage_to_0_when_the_cgroup_do cgroup.initialize_cpu_usage() - self.assertEquals(cgroup._current_cgroup_cpu, 0) - self.assertEquals(cgroup._current_system_cpu, 5496872) # check the system usage just for test sanity + self.assertEquals(cgroup._current_cgroup_cpu, 0) # pylint: disable=deprecated-method,protected-access + self.assertEquals(cgroup._current_system_cpu, 5496872) # check the system usage just for test sanity # pylint: disable=deprecated-method,protected-access def test_initialize_cpu_usage_should_raise_an_exception_when_called_more_than_once(self): @@ -193,7 +193,7 @@ def test_get_cpu_usage_should_raise_an_exception_when_initialize_cpu_usage_has_n cgroup = CpuCgroup("test", "/sys/fs/cgroup/cpu/system.slice/test") with self.assertRaises(CGroupsException): - cpu_usage = cgroup.get_cpu_usage() + cpu_usage = cgroup.get_cpu_usage() # pylint: disable=unused-variable class TestMemoryCgroup(AgentTestCase): @@ -213,12 +213,12 @@ def test_get_metrics(self): def test_get_metrics_when_files_not_present(self): test_mem_cg = MemoryCgroup("test_extension", os.path.join(data_dir, "cgroups")) - with self.assertRaises(IOError) as e: + with self.assertRaises(IOError) as e: # pylint: disable=invalid-name test_mem_cg.get_memory_usage() self.assertEqual(e.exception.errno, errno.ENOENT) - with self.assertRaises(IOError) as e: + with self.assertRaises(IOError) as e: # pylint: disable=invalid-name test_mem_cg.get_max_memory_usage() self.assertEqual(e.exception.errno, errno.ENOENT) diff --git a/tests/common/test_cgroupstelemetry.py b/tests/common/test_cgroupstelemetry.py index d130eb3630..81e137eb1f 100644 --- a/tests/common/test_cgroupstelemetry.py +++ b/tests/common/test_cgroupstelemetry.py @@ -27,7 +27,7 @@ def raise_ioerror(*_): - e = IOError() + e = IOError() # pylint: disable=invalid-name from errno import EIO e.errno = EIO raise e @@ -38,7 +38,7 @@ def median(lst): l_len = len(data) if l_len < 1: return None - if l_len % 2 == 0: + if l_len % 2 == 0: # pylint: disable=no-else-return return (data[int((l_len - 1) / 2)] + data[int((l_len + 1) / 2)]) / 2.0 else: return data[int((l_len - 1) / 2)] @@ -54,14 +54,14 @@ def generate_metric_list(lst): def consume_cpu_time(): waste = 0 - for x in range(1, 200000): + for x in range(1, 200000): # pylint: disable=unused-variable,invalid-name waste += random.random() return waste def consume_memory(): waste = [] - for x in range(1, 3): + for x in range(1, 3): # pylint: disable=unused-variable,invalid-name waste.append([random.random()] * 10000) time.sleep(0.1) waste *= 0 @@ -135,7 +135,7 @@ def _assert_polled_metrics_equal(self, metrics, cpu_metric_value, memory_metric_ elif metric.counter == "Max Memory Usage": self.assertEqual(metric.value, max_memory_metric_value) - def test_telemetry_polling_with_active_cgroups(self, *args): + def test_telemetry_polling_with_active_cgroups(self, *args): # pylint: disable=unused-argument num_extensions = 3 self._track_new_extension_cgroups(num_extensions) @@ -157,7 +157,7 @@ def test_telemetry_polling_with_active_cgroups(self, *args): patch_get_memory_max_usage.return_value = current_max_memory # example 450 MB num_polls = 10 - for data_count in range(1, num_polls + 1): + for data_count in range(1, num_polls + 1): # pylint: disable=unused-variable metrics = CGroupsTelemetry.poll_all_tracked() self.assertEqual(len(metrics), num_extensions * num_of_metrics_per_extn_expected) @@ -170,7 +170,7 @@ def test_telemetry_polling_with_active_cgroups(self, *args): @patch("azurelinuxagent.common.cgroup.CGroup.is_active", return_value=False) def test_telemetry_polling_with_inactive_cgroups(self, *_): num_extensions = 5 - no_extensions_expected = 0 + no_extensions_expected = 0 # pylint: disable=unused-variable self._track_new_extension_cgroups(num_extensions) self._assert_cgroups_are_tracked(num_extensions) @@ -189,15 +189,15 @@ def test_telemetry_polling_with_inactive_cgroups(self, *_): @patch("azurelinuxagent.common.cgroup.CpuCgroup.get_cpu_usage") @patch("azurelinuxagent.common.cgroup.CGroup.is_active") @patch("azurelinuxagent.common.resourceusage.MemoryResourceUsage.get_memory_usage_from_proc_statm") - def test_telemetry_polling_with_changing_cgroups_state(self, patch_get_statm, patch_is_active, patch_get_cpu_usage, + def test_telemetry_polling_with_changing_cgroups_state(self, patch_get_statm, patch_is_active, patch_get_cpu_usage, # pylint: disable=unused-argument,too-many-arguments,too-many-locals patch_get_mem, patch_get_max_mem, *args): num_extensions = 5 self._track_new_extension_cgroups(num_extensions) patch_is_active.return_value = True - no_extensions_expected = 0 - expected_data_count = 1 + no_extensions_expected = 0 # pylint: disable=unused-variable + expected_data_count = 1 # pylint: disable=unused-variable current_cpu = 30 current_memory = 209715200 @@ -241,7 +241,7 @@ def test_telemetry_polling_to_not_generate_transient_logs_ioerror_file_not_found with patch("azurelinuxagent.common.utils.fileutil.read_file", side_effect=io_error_2): poll_count = 1 - for data_count in range(poll_count, 10): + for data_count in range(poll_count, 10): # pylint: disable=unused-variable CGroupsTelemetry.poll_all_tracked() self.assertEqual(0, patch_periodic_warn.call_count) @@ -264,7 +264,7 @@ def test_telemetry_polling_to_generate_transient_logs_ioerror_permission_denied( # each collect per controller would generate a log statement, and each cgroup would invoke a # is active check raising an exception - for data_count in range(poll_count, 10): + for data_count in range(poll_count, 10): # pylint: disable=unused-variable CGroupsTelemetry.poll_all_tracked() self.assertEqual(poll_count * expected_count_per_call, patch_periodic_warn.call_count) @@ -277,7 +277,7 @@ def test_telemetry_polling_to_generate_transient_logs_index_error(self): with patch("azurelinuxagent.common.utils.fileutil.read_file", return_value=''): with patch("azurelinuxagent.common.logger.periodic_warn") as patch_periodic_warn: expected_call_count = 2 # 1 periodic warning for the cpu cgroups, and 1 for memory - for data_count in range(1, 10): + for data_count in range(1, 10): # pylint: disable=unused-variable CGroupsTelemetry.poll_all_tracked() self.assertEqual(expected_call_count, patch_periodic_warn.call_count) @@ -285,7 +285,7 @@ def test_telemetry_polling_to_generate_transient_logs_index_error(self): @patch("azurelinuxagent.common.cgroup.MemoryCgroup.get_memory_usage") @patch("azurelinuxagent.common.cgroup.CpuCgroup.get_cpu_usage") @patch("azurelinuxagent.common.cgroup.CGroup.is_active") - def test_telemetry_calculations(self, patch_is_active, patch_get_cpu_usage, patch_get_memory_usage, patch_get_memory_max_usage, *args): + def test_telemetry_calculations(self, patch_is_active, patch_get_cpu_usage, patch_get_memory_usage, patch_get_memory_max_usage, *args): # pylint: disable=unused-argument num_polls = 10 num_extensions = 1 @@ -296,7 +296,7 @@ def test_telemetry_calculations(self, patch_is_active, patch_get_cpu_usage, pat max_memory_usage_values = [random.randint(0, 8 * 1024 ** 3) for _ in range(num_polls)] self._track_new_extension_cgroups(num_extensions) - self.assertEqual(2 * num_extensions, len(CGroupsTelemetry._tracked)) + self.assertEqual(2 * num_extensions, len(CGroupsTelemetry._tracked)) # pylint: disable=protected-access for i in range(num_polls): patch_is_active.return_value = True @@ -310,28 +310,28 @@ def test_telemetry_calculations(self, patch_is_active, patch_get_cpu_usage, pat self.assertEqual(len(metrics), 3 * num_extensions) self._assert_polled_metrics_equal(metrics, cpu_percent_values[i], memory_usage_values[i], max_memory_usage_values[i]) - def test_cgroup_tracking(self, *args): + def test_cgroup_tracking(self, *args): # pylint: disable=unused-argument num_extensions = 5 num_controllers = 2 self._track_new_extension_cgroups(num_extensions) self._assert_cgroups_are_tracked(num_extensions) - self.assertEqual(num_extensions * num_controllers, len(CGroupsTelemetry._tracked)) + self.assertEqual(num_extensions * num_controllers, len(CGroupsTelemetry._tracked)) # pylint: disable=protected-access - def test_cgroup_pruning(self, *args): + def test_cgroup_pruning(self, *args): # pylint: disable=unused-argument num_extensions = 5 num_controllers = 2 self._track_new_extension_cgroups(num_extensions) self._assert_cgroups_are_tracked(num_extensions) - self.assertEqual(num_extensions * num_controllers, len(CGroupsTelemetry._tracked)) + self.assertEqual(num_extensions * num_controllers, len(CGroupsTelemetry._tracked)) # pylint: disable=protected-access CGroupsTelemetry.prune_all_tracked() for i in range(num_extensions): self.assertFalse(CGroupsTelemetry.is_tracked("dummy_cpu_path_{0}".format(i))) self.assertFalse(CGroupsTelemetry.is_tracked("dummy_memory_path_{0}".format(i))) - self.assertEqual(0, len(CGroupsTelemetry._tracked)) + self.assertEqual(0, len(CGroupsTelemetry._tracked)) # pylint: disable=protected-access - def test_cgroup_is_tracked(self, *args): + def test_cgroup_is_tracked(self, *args): # pylint: disable=unused-argument num_extensions = 5 self._track_new_extension_cgroups(num_extensions) self._assert_cgroups_are_tracked(num_extensions) @@ -339,7 +339,7 @@ def test_cgroup_is_tracked(self, *args): self.assertFalse(CGroupsTelemetry.is_tracked("not_present_memory_dummy_path")) @patch("azurelinuxagent.common.cgroup.MemoryCgroup.get_memory_usage", side_effect=raise_ioerror) - def test_process_cgroup_metric_with_no_memory_cgroup_mounted(self, *args): + def test_process_cgroup_metric_with_no_memory_cgroup_mounted(self, *args): # pylint: disable=unused-argument num_extensions = 5 self._track_new_extension_cgroups(num_extensions) @@ -352,7 +352,7 @@ def test_process_cgroup_metric_with_no_memory_cgroup_mounted(self, *args): poll_count = 1 - for data_count in range(poll_count, 10): + for data_count in range(poll_count, 10): # pylint: disable=unused-variable metrics = CGroupsTelemetry.poll_all_tracked() self.assertEqual(len(metrics), num_extensions * 1) # Only CPU populated @@ -360,7 +360,7 @@ def test_process_cgroup_metric_with_no_memory_cgroup_mounted(self, *args): @patch("azurelinuxagent.common.cgroup.CpuCgroup.get_cpu_usage", side_effect=raise_ioerror) - def test_process_cgroup_metric_with_no_cpu_cgroup_mounted(self, *args): + def test_process_cgroup_metric_with_no_cpu_cgroup_mounted(self, *args): # pylint: disable=unused-argument num_extensions = 5 self._track_new_extension_cgroups(num_extensions) @@ -376,7 +376,7 @@ def test_process_cgroup_metric_with_no_cpu_cgroup_mounted(self, *args): patch_get_memory_usage.return_value = current_memory # example 200 MB patch_get_memory_max_usage.return_value = current_max_memory # example 450 MB num_polls = 10 - for data_count in range(1, num_polls + 1): + for data_count in range(1, num_polls + 1): # pylint: disable=unused-variable metrics = CGroupsTelemetry.poll_all_tracked() # Memory is only populated, CPU is not. Thus 2 metrics per cgroup. self.assertEqual(len(metrics), num_extensions * 2) @@ -385,7 +385,7 @@ def test_process_cgroup_metric_with_no_cpu_cgroup_mounted(self, *args): @patch("azurelinuxagent.common.cgroup.MemoryCgroup.get_memory_usage", side_effect=raise_ioerror) @patch("azurelinuxagent.common.cgroup.MemoryCgroup.get_max_memory_usage", side_effect=raise_ioerror) @patch("azurelinuxagent.common.cgroup.CpuCgroup.get_cpu_usage", side_effect=raise_ioerror) - def test_extension_telemetry_not_sent_for_empty_perf_metrics(self, *args): + def test_extension_telemetry_not_sent_for_empty_perf_metrics(self, *args): # pylint: disable=unused-argument num_extensions = 5 self._track_new_extension_cgroups(num_extensions) @@ -394,7 +394,7 @@ def test_extension_telemetry_not_sent_for_empty_perf_metrics(self, *args): patch_is_active.return_value = False poll_count = 1 - for data_count in range(poll_count, 10): + for data_count in range(poll_count, 10): # pylint: disable=unused-variable metrics = CGroupsTelemetry.poll_all_tracked() self.assertEqual(0, len(metrics)) diff --git a/tests/common/test_conf.py b/tests/common/test_conf.py index ef5bc810bd..a996a7c23a 100644 --- a/tests/common/test_conf.py +++ b/tests/common/test_conf.py @@ -141,30 +141,30 @@ def assert_get_cgroups_excluded(self, patch_get, config, expected_value): self.assertEqual(expected_value, conf.get_cgroups_excluded(self.conf)) def test_get_cgroups_excluded(self): - self.assert_get_cgroups_excluded(config=None, + self.assert_get_cgroups_excluded(config=None, # pylint: disable=no-value-for-parameter expected_value=[]) - self.assert_get_cgroups_excluded(config='', + self.assert_get_cgroups_excluded(config='', # pylint: disable=no-value-for-parameter expected_value=[]) - self.assert_get_cgroups_excluded(config=' ', + self.assert_get_cgroups_excluded(config=' ', # pylint: disable=no-value-for-parameter expected_value=[]) - self.assert_get_cgroups_excluded(config=' , ,, ,', + self.assert_get_cgroups_excluded(config=' , ,, ,', # pylint: disable=no-value-for-parameter expected_value=[]) standard_values = ['customscript', 'runcommand'] - self.assert_get_cgroups_excluded(config='CustomScript, RunCommand', + self.assert_get_cgroups_excluded(config='CustomScript, RunCommand', # pylint: disable=no-value-for-parameter expected_value=standard_values) - self.assert_get_cgroups_excluded(config='customScript, runCommand , , ,,', + self.assert_get_cgroups_excluded(config='customScript, runCommand , , ,,', # pylint: disable=no-value-for-parameter expected_value=standard_values) - self.assert_get_cgroups_excluded(config=' customscript,runcommand ', + self.assert_get_cgroups_excluded(config=' customscript,runcommand ', # pylint: disable=no-value-for-parameter expected_value=standard_values) - self.assert_get_cgroups_excluded(config='customscript,, runcommand', + self.assert_get_cgroups_excluded(config='customscript,, runcommand', # pylint: disable=no-value-for-parameter expected_value=standard_values) - self.assert_get_cgroups_excluded(config=',,customscript ,runcommand', + self.assert_get_cgroups_excluded(config=',,customscript ,runcommand', # pylint: disable=no-value-for-parameter expected_value=standard_values) diff --git a/tests/common/test_errorstate.py b/tests/common/test_errorstate.py index 263d95ed7d..bd88d8c6c4 100644 --- a/tests/common/test_errorstate.py +++ b/tests/common/test_errorstate.py @@ -46,7 +46,7 @@ def test_errorstate03(self, mock_time): """ test_subject = ErrorState(timedelta(minutes=15)) - for x in range(1, 10): + for x in range(1, 10): # pylint: disable=invalid-name mock_time.utcnow = Mock(return_value=datetime.utcnow() + timedelta(minutes=x)) test_subject.incr() diff --git a/tests/common/test_event.py b/tests/common/test_event.py index 993e2e090a..19b49a3c5a 100644 --- a/tests/common/test_event.py +++ b/tests/common/test_event.py @@ -43,7 +43,7 @@ from tests.utils.event_logger_tools import EventLoggerTools -class TestEvent(HttpRequestPredicates, AgentTestCase): +class TestEvent(HttpRequestPredicates, AgentTestCase): # pylint: disable=too-many-public-methods def setUp(self): AgentTestCase.setUp(self) @@ -84,34 +84,34 @@ def setUp(self): } @staticmethod - def _is_guest_extension_event(event): + def _is_guest_extension_event(event): # pylint: disable=redefined-outer-name return event.eventId == TELEMETRY_EVENT_EVENT_ID and event.providerId == TELEMETRY_EVENT_PROVIDER_ID @staticmethod - def _is_telemetry_log_event(event): + def _is_telemetry_log_event(event): # pylint: disable=redefined-outer-name return event.eventId == TELEMETRY_LOG_EVENT_ID and event.providerId == TELEMETRY_LOG_PROVIDER_ID - def test_parse_xml_event(self, *args): + def test_parse_xml_event(self, *args): # pylint: disable=unused-argument data_str = load_data('ext/event_from_extension.xml') - event = parse_xml_event(data_str) + event = parse_xml_event(data_str) # pylint: disable=redefined-outer-name self.assertNotEqual(None, event) self.assertNotEqual(0, event.parameters) self.assertTrue(all(param is not None for param in event.parameters)) - def test_parse_json_event(self, *args): + def test_parse_json_event(self, *args): # pylint: disable=unused-argument data_str = load_data('ext/event.json') - event = parse_json_event(data_str) + event = parse_json_event(data_str) # pylint: disable=redefined-outer-name self.assertNotEqual(None, event) self.assertNotEqual(0, event.parameters) self.assertTrue(all(param is not None for param in event.parameters)) def test_add_event_should_use_the_container_id_from_the_most_recent_goal_state(self): - def create_event_and_return_container_id(): + def create_event_and_return_container_id(): # pylint: disable=inconsistent-return-statements event.add_event(name='Event') event_list = event.collect_events() - self.assertEquals(len(event_list.events), 1, "Could not find the event created by add_event") + self.assertEquals(len(event_list.events), 1, "Could not find the event created by add_event") # pylint: disable=deprecated-method - for p in event_list.events[0].parameters: + for p in event_list.events[0].parameters: # pylint: disable=invalid-name if p.name == CommonTelemetryEventSchema.ContainerId: return p.value @@ -120,17 +120,17 @@ def create_event_and_return_container_id(): with mock_wire_protocol(mockwiredata.DATA_FILE) as protocol: contained_id = create_event_and_return_container_id() # The expect value comes from DATA_FILE - self.assertEquals(contained_id, 'c6d5526c-5ac2-4200-b6e2-56f2b70c5ab2', "Incorrect container ID") + self.assertEquals(contained_id, 'c6d5526c-5ac2-4200-b6e2-56f2b70c5ab2', "Incorrect container ID") # pylint: disable=deprecated-method protocol.mock_wire_data.set_container_id('AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE') protocol.update_goal_state() contained_id = create_event_and_return_container_id() - self.assertEquals(contained_id, 'AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE', "Incorrect container ID") + self.assertEquals(contained_id, 'AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE', "Incorrect container ID") # pylint: disable=deprecated-method protocol.mock_wire_data.set_container_id('11111111-2222-3333-4444-555555555555') protocol.update_goal_state() contained_id = create_event_and_return_container_id() - self.assertEquals(contained_id, '11111111-2222-3333-4444-555555555555', "Incorrect container ID") + self.assertEquals(contained_id, '11111111-2222-3333-4444-555555555555', "Incorrect container ID") # pylint: disable=deprecated-method def test_add_event_should_handle_event_errors(self): @@ -147,7 +147,7 @@ def test_add_event_should_handle_event_errors(self): self.assertIn("[EventError] Failed to create events folder", exception_message) def test_event_status_event_marked(self): - es = event.__event_status__ + es = event.__event_status__ # pylint: disable=invalid-name self.assertFalse(es.event_marked("Foo", "1.2", "FauxOperation")) es.mark_event_status("Foo", "1.2", "FauxOperation", True) @@ -155,15 +155,15 @@ def test_event_status_event_marked(self): event.__event_status__ = event.EventStatus() event.init_event_status(self.tmp_dir) - es = event.__event_status__ + es = event.__event_status__ # pylint: disable=invalid-name self.assertTrue(es.event_marked("Foo", "1.2", "FauxOperation")) def test_event_status_defaults_to_success(self): - es = event.__event_status__ + es = event.__event_status__ # pylint: disable=invalid-name self.assertTrue(es.event_succeeded("Foo", "1.2", "FauxOperation")) def test_event_status_records_status(self): - es = event.EventStatus() + es = event.EventStatus() # pylint: disable=invalid-name es.mark_event_status("Foo", "1.2", "FauxOperation", True) self.assertTrue(es.event_succeeded("Foo", "1.2", "FauxOperation")) @@ -172,14 +172,14 @@ def test_event_status_records_status(self): self.assertFalse(es.event_succeeded("Foo", "1.2", "FauxOperation")) def test_event_status_preserves_state(self): - es = event.__event_status__ + es = event.__event_status__ # pylint: disable=invalid-name es.mark_event_status("Foo", "1.2", "FauxOperation", False) self.assertFalse(es.event_succeeded("Foo", "1.2", "FauxOperation")) event.__event_status__ = event.EventStatus() event.init_event_status(self.tmp_dir) - es = event.__event_status__ + es = event.__event_status__ # pylint: disable=invalid-name self.assertFalse(es.event_succeeded("Foo", "1.2", "FauxOperation")) def test_should_emit_event_ignores_unknown_operations(self): @@ -198,25 +198,25 @@ def test_should_emit_event_handles_known_operations(self): event.__event_status__ = event.EventStatus() # Known operations always initially "fire" - for op in event.__event_status_operations__: + for op in event.__event_status_operations__: # pylint: disable=invalid-name self.assertTrue(event.should_emit_event("Foo", "1.2", op, True)) self.assertTrue(event.should_emit_event("Foo", "1.2", op, False)) # Note a success event... - for op in event.__event_status_operations__: + for op in event.__event_status_operations__: # pylint: disable=invalid-name event.mark_event_status("Foo", "1.2", op, True) # Subsequent success events should not fire, but failures will - for op in event.__event_status_operations__: + for op in event.__event_status_operations__: # pylint: disable=invalid-name self.assertFalse(event.should_emit_event("Foo", "1.2", op, True)) self.assertTrue(event.should_emit_event("Foo", "1.2", op, False)) # Note a failure event... - for op in event.__event_status_operations__: + for op in event.__event_status_operations__: # pylint: disable=invalid-name event.mark_event_status("Foo", "1.2", op, False) # Subsequent success events fire and failure do not - for op in event.__event_status_operations__: + for op in event.__event_status_operations__: # pylint: disable=invalid-name self.assertTrue(event.should_emit_event("Foo", "1.2", op, True)) self.assertFalse(event.should_emit_event("Foo", "1.2", op, False)) @@ -237,12 +237,12 @@ def test_should_log_errors_if_failed_operation_and_empty_event_dir(self, message="dummy event message", reporter=mock_reporter) - self.assertEquals(1, mock_logger_error.call_count) - self.assertEquals(1, mock_logger_warn.call_count) - self.assertEquals(0, mock_logger_info.call_count) + self.assertEquals(1, mock_logger_error.call_count) # pylint: disable=deprecated-method + self.assertEquals(1, mock_logger_warn.call_count) # pylint: disable=deprecated-method + self.assertEquals(0, mock_logger_info.call_count) # pylint: disable=deprecated-method args = mock_logger_error.call_args[0] - self.assertEquals(('dummy name', 'Download', 'dummy event message', 0), args[1:]) + self.assertEquals(('dummy name', 'Download', 'dummy event message', 0), args[1:]) # pylint: disable=deprecated-method @patch('azurelinuxagent.common.event.EventLogger') @patch('azurelinuxagent.common.logger.error') @@ -264,13 +264,13 @@ def test_should_log_errors_if_failed_operation_and_not_empty_event_dir(self, is_success=False, message="dummy event message") - self.assertEquals(1, mock_should_emit_event.call_count) - self.assertEquals(1, mock_logger_error.call_count) - self.assertEquals(0, mock_logger_warn.call_count) - self.assertEquals(0, mock_logger_info.call_count) + self.assertEquals(1, mock_should_emit_event.call_count) # pylint: disable=deprecated-method + self.assertEquals(1, mock_logger_error.call_count) # pylint: disable=deprecated-method + self.assertEquals(0, mock_logger_warn.call_count) # pylint: disable=deprecated-method + self.assertEquals(0, mock_logger_info.call_count) # pylint: disable=deprecated-method args = mock_logger_error.call_args[0] - self.assertEquals(('dummy name', 'Download', 'dummy event message', 0), args[1:]) + self.assertEquals(('dummy name', 'Download', 'dummy event message', 0), args[1:]) # pylint: disable=deprecated-method @patch('azurelinuxagent.common.event.EventLogger.add_event') def test_periodic_emits_if_not_previously_sent(self, mock_event): @@ -309,7 +309,7 @@ def test_periodic_emits_after_elapsed_delta(self, mock_event): event.add_periodic(logger.EVERY_DAY, "FauxEvent") self.assertEqual(1, mock_event.call_count) - h = hash("FauxEvent"+WALAEventOperation.Unknown+ustr(True)) + h = hash("FauxEvent"+WALAEventOperation.Unknown+ustr(True)) # pylint: disable=invalid-name event.__event_logger__.periodic_events[h] = \ datetime.now() - logger.EVERY_DAY - logger.EVERY_HOUR event.add_periodic(logger.EVERY_DAY, "FauxEvent") @@ -325,7 +325,7 @@ def test_periodic_forwards_args(self, mock_event): @patch("azurelinuxagent.common.event.datetime") @patch('azurelinuxagent.common.event.EventLogger.add_event') - def test_periodic_forwards_args_default_values(self, mock_event, mock_datetime): + def test_periodic_forwards_args_default_values(self, mock_event, mock_datetime): # pylint: disable=unused-argument event.__event_logger__.reset_periodic() event.add_periodic(logger.EVERY_DAY, "FauxEvent", message="FauxEventMessage") mock_event.assert_called_once_with("FauxEvent", op=WALAEventOperation.Unknown, is_success=True, duration=0, @@ -344,13 +344,13 @@ def test_collect_events_should_delete_event_files(self): add_event(name='Event3') event_files = os.listdir(self.event_dir) - self.assertEquals(len(event_files), 3, "Did not find all the event files that were created") + self.assertEquals(len(event_files), 3, "Did not find all the event files that were created") # pylint: disable=deprecated-method event_list = event.collect_events() event_files = os.listdir(self.event_dir) - self.assertEquals(len(event_list.events), 3, "Did not collect all the events that were created") - self.assertEquals(len(event_files), 0, "The event files were not deleted") + self.assertEquals(len(event_list.events), 3, "Did not collect all the events that were created") # pylint: disable=deprecated-method + self.assertEquals(len(event_files), 0, "The event files were not deleted") # pylint: disable=deprecated-method def test_save_event(self): add_event('test', message='test event') @@ -363,7 +363,7 @@ def test_save_event(self): @staticmethod def _get_event_message(evt): - for p in evt.parameters: + for p in evt.parameters: # pylint: disable=invalid-name if p.name == GuestAgentExtensionEventsSchema.Message: return p.value return None @@ -373,8 +373,8 @@ def test_collect_events_should_be_able_to_process_events_with_non_ascii_characte event_list = event.collect_events() - self.assertEquals(len(event_list.events), 1) - self.assertEquals(TestEvent._get_event_message(event_list.events[0]), u'World\u05e2\u05d9\u05d5\u05ea \u05d0\u05d7\u05e8\u05d5\u05ea\u0906\u091c') + self.assertEquals(len(event_list.events), 1) # pylint: disable=deprecated-method + self.assertEquals(TestEvent._get_event_message(event_list.events[0]), u'World\u05e2\u05d9\u05d5\u05ea \u05d0\u05d7\u05e8\u05d5\u05ea\u0906\u091c') # pylint: disable=deprecated-method def test_collect_events_should_ignore_invalid_event_files(self): self._create_test_event_file("custom_script_1.tld") # a valid event @@ -386,7 +386,7 @@ def test_collect_events_should_ignore_invalid_event_files(self): with patch("azurelinuxagent.common.event.add_event") as mock_add_event: event_list = event.collect_events() - self.assertEquals( + self.assertEquals( # pylint: disable=deprecated-method len(event_list.events), 2) self.assertTrue( all(TestEvent._get_event_message(evt) == "A test telemetry message." for evt in event_list.events), @@ -394,13 +394,13 @@ def test_collect_events_should_ignore_invalid_event_files(self): invalid_events = [] total_dropped_count = 0 - for args, kwargs in mock_add_event.call_args_list: - match = re.search("DroppedEventsCount: (\d+)", kwargs['message']) + for args, kwargs in mock_add_event.call_args_list: # pylint: disable=unused-variable + match = re.search("DroppedEventsCount: (\d+)", kwargs['message']) # pylint: disable=anomalous-backslash-in-string if match is not None: invalid_events.append(kwargs['op']) total_dropped_count += int(match.groups()[0]) - self.assertEquals(3, total_dropped_count, "Total dropped events dont match") + self.assertEquals(3, total_dropped_count, "Total dropped events dont match") # pylint: disable=deprecated-method self.assertIn(WALAEventOperation.CollectEventErrors, invalid_events, "{0} errors not reported".format(WALAEventOperation.CollectEventErrors)) self.assertIn(WALAEventOperation.CollectEventUnicodeErrors, invalid_events, @@ -447,7 +447,7 @@ def test_save_event_rollover(self): def test_save_event_cleanup(self): for i in range(0, 2000): evt = os.path.join(self.event_dir, '{0}.tld'.format(ustr(1491004920536531 + i))) - with open(evt, 'w') as fh: + with open(evt, 'w') as fh: # pylint: disable=invalid-name fh.write('test event {0}'.format(i)) events = os.listdir(self.event_dir) @@ -482,7 +482,7 @@ def _assert_event_includes_all_parameters_in_the_telemetry_schema(self, actual_e # which is verified using assert_timestamp() event_parameters = {} timestamp = None - for p in actual_event.parameters: + for p in actual_event.parameters: # pylint: disable=invalid-name if p.name == CommonTelemetryEventSchema.OpcodeName: # the timestamp is stored in the opcode name timestamp = p.value else: @@ -494,14 +494,14 @@ def _assert_event_includes_all_parameters_in_the_telemetry_schema(self, actual_e self.assertIsNotNone(telemetry_log_event_timestamp, "Context2 should be filled with a timestamp") assert_timestamp(telemetry_log_event_timestamp) - self.maxDiff = None # the dictionary diffs can be quite large; display the whole thing + self.maxDiff = None # the dictionary diffs can be quite large; display the whole thing # pylint: disable=invalid-name self.assertDictEqual(event_parameters, all_expected_parameters) self.assertIsNotNone(timestamp, "The event does not have a timestamp (Opcode)") assert_timestamp(timestamp) @staticmethod - def _datetime_to_event_timestamp(dt): + def _datetime_to_event_timestamp(dt): # pylint: disable=invalid-name return dt.strftime(logger.Logger.LogTimeFormatInUTC) def _test_create_event_function_should_create_events_that_have_all_the_parameters_in_the_telemetry_schema(self, create_event_function, expected_parameters): @@ -516,7 +516,7 @@ def _test_create_event_function_should_create_events_that_have_all_the_parameter # retrieve the event that was created event_list = event.collect_events() - self.assertEquals(len(event_list.events), 1) + self.assertEquals(len(event_list.events), 1) # pylint: disable=deprecated-method # verify the event parameters self._assert_event_includes_all_parameters_in_the_telemetry_schema( @@ -590,7 +590,7 @@ def test_add_log_event_should_always_create_events_when_forced(self): def test_add_log_event_should_not_create_event_if_not_allowed_and_not_forced(self): add_log_event(logger.LogLevel.WARNING, 'A test WARNING log event') event_list = event.collect_events() - self.assertEquals(len(event_list.events), 0, "No events should be created if not forced and not allowed") + self.assertEquals(len(event_list.events), 0, "No events should be created if not forced and not allowed") # pylint: disable=deprecated-method def test_report_metric_should_create_events_that_have_all_the_parameters_in_the_telemetry_schema(self): self._test_create_event_function_should_create_events_that_have_all_the_parameters_in_the_telemetry_schema( @@ -609,7 +609,7 @@ def _create_test_event_file(self, source_file): return target_file_path @staticmethod - def _get_file_creation_timestamp(file): + def _get_file_creation_timestamp(file): # pylint: disable=redefined-builtin return TestEvent._datetime_to_event_timestamp(datetime.fromtimestamp(os.path.getmtime(file))) def test_collect_events_should_add_all_the_parameters_in_the_telemetry_schema_to_legacy_agent_events(self): @@ -619,7 +619,7 @@ def test_collect_events_should_add_all_the_parameters_in_the_telemetry_schema_to event_list = event.collect_events() - self.assertEquals(len(event_list.events), 1) + self.assertEquals(len(event_list.events), 1) # pylint: disable=deprecated-method self._assert_event_includes_all_parameters_in_the_telemetry_schema( event_list.events[0], @@ -639,7 +639,7 @@ def test_collect_events_should_add_all_the_parameters_in_the_telemetry_schema_to CommonTelemetryEventSchema.TaskName: "ALegacyTask", CommonTelemetryEventSchema.KeywordName: "ALegacyKeywordName"}, assert_timestamp=lambda timestamp: - self.assertEquals(timestamp, '1970-01-01 12:00:00', "The event timestamp (opcode) is incorrect") + self.assertEquals(timestamp, '1970-01-01 12:00:00', "The event timestamp (opcode) is incorrect") # pylint: disable=deprecated-method ) def test_collect_events_should_use_the_file_creation_time_for_legacy_agent_events_missing_a_timestamp(self): @@ -649,7 +649,7 @@ def test_collect_events_should_use_the_file_creation_time_for_legacy_agent_event event_list = event.collect_events() - self.assertEquals(len(event_list.events), 1) + self.assertEquals(len(event_list.events), 1) # pylint: disable=deprecated-method self._assert_event_includes_all_parameters_in_the_telemetry_schema( event_list.events[0], @@ -669,7 +669,7 @@ def test_collect_events_should_use_the_file_creation_time_for_legacy_agent_event CommonTelemetryEventSchema.TaskName: "ALegacyTask", CommonTelemetryEventSchema.KeywordName: "ALegacyKeywordName"}, assert_timestamp=lambda timestamp: - self.assertEquals(timestamp, event_creation_time, "The event timestamp (opcode) is incorrect") + self.assertEquals(timestamp, event_creation_time, "The event timestamp (opcode) is incorrect") # pylint: disable=deprecated-method ) def _assert_extension_event_includes_all_parameters_in_the_telemetry_schema(self, event_file): @@ -681,7 +681,7 @@ def _assert_extension_event_includes_all_parameters_in_the_telemetry_schema(self event_list = event.collect_events() - self.assertEquals(len(event_list.events), 1) + self.assertEquals(len(event_list.events), 1) # pylint: disable=deprecated-method self._assert_event_includes_all_parameters_in_the_telemetry_schema( event_list.events[0], @@ -694,7 +694,7 @@ def _assert_extension_event_includes_all_parameters_in_the_telemetry_schema(self GuestAgentExtensionEventsSchema.Duration: 150000, GuestAgentExtensionEventsSchema.ExtensionType: 'json'}, assert_timestamp=lambda timestamp: - self.assertEquals(timestamp, event_creation_time, "The event timestamp (opcode) is incorrect") + self.assertEquals(timestamp, event_creation_time, "The event timestamp (opcode) is incorrect") # pylint: disable=deprecated-method ) def test_collect_events_should_add_all_the_parameters_in_the_telemetry_schema_to_extension_events(self): @@ -710,11 +710,11 @@ def test_report_event_should_encode_call_stack_correctly(self): Message we read from the event's file. """ def get_event_message_from_event_file(event_file): - with open(event_file, "rb") as fd: + with open(event_file, "rb") as fd: # pylint: disable=invalid-name event_data = fd.read().decode("utf-8") # event files are UTF-8 encoded telemetry_event = json.loads(event_data) - for p in telemetry_event['parameters']: + for p in telemetry_event['parameters']: # pylint: disable=invalid-name if p['name'] == GuestAgentExtensionEventsSchema.Message: return p['value'] @@ -723,7 +723,7 @@ def get_event_message_from_event_file(event_file): def get_event_message_from_http_request_body(event_body): # The XML for the event is sent over as a CDATA element ("Event") in the request's body http_request_body = event_body if ( - event_body is None or type(event_body) is ustr) else textutil.str_to_encoded_ustr(event_body) + event_body is None or type(event_body) is ustr) else textutil.str_to_encoded_ustr(event_body) # pylint: disable=unidiomatic-typecheck request_body_xml_doc = textutil.parse_doc(http_request_body) event_node = textutil.find(request_body_xml_doc, "Event") @@ -768,7 +768,7 @@ def http_post_handler(url, body, **__): event_message = get_event_message_from_http_request_body(http_post_handler.request_body) - self.assertEquals(event_message, expected_message, "The Message in the HTTP request does not match the Message in the event's *.tld file") + self.assertEquals(event_message, expected_message, "The Message in the HTTP request does not match the Message in the event's *.tld file") # pylint: disable=deprecated-method def test_report_event_should_encode_events_correctly(self): @@ -816,7 +816,7 @@ def test_report_metric(self, mock_event): event_json = mock_event.call_args[0][0] self.assertIn(event.TELEMETRY_EVENT_PROVIDER_ID, event_json) self.assertIn("%idle", event_json) - import json + import json # pylint: disable=redefined-outer-name,reimported event_dictionary = json.loads(event_json) self.assertEqual(event_dictionary['providerId'], event.TELEMETRY_EVENT_PROVIDER_ID) for parameter in event_dictionary["parameters"]: @@ -829,48 +829,48 @@ def test_report_metric(self, mock_event): def test_cleanup_message(self): ev_logger = event.EventLogger() - self.assertEqual(None, ev_logger._clean_up_message(None)) - self.assertEqual("", ev_logger._clean_up_message("")) - self.assertEqual("Daemon Activate resource disk failure", ev_logger._clean_up_message( + self.assertEqual(None, ev_logger._clean_up_message(None)) # pylint: disable=protected-access + self.assertEqual("", ev_logger._clean_up_message("")) # pylint: disable=protected-access + self.assertEqual("Daemon Activate resource disk failure", ev_logger._clean_up_message( # pylint: disable=protected-access "Daemon Activate resource disk failure")) - self.assertEqual("[M.A.E.CS-2.0.7] Target handler state", ev_logger._clean_up_message( + self.assertEqual("[M.A.E.CS-2.0.7] Target handler state", ev_logger._clean_up_message( # pylint: disable=protected-access '2019/10/07 21:54:16.629444 INFO [M.A.E.CS-2.0.7] Target handler state')) - self.assertEqual("[M.A.E.CS-2.0.7] Initializing extension M.A.E.CS-2.0.7", ev_logger._clean_up_message( + self.assertEqual("[M.A.E.CS-2.0.7] Initializing extension M.A.E.CS-2.0.7", ev_logger._clean_up_message( # pylint: disable=protected-access '2019/10/07 21:54:17.284385 INFO [M.A.E.CS-2.0.7] Initializing extension M.A.E.CS-2.0.7')) - self.assertEqual("ExtHandler ProcessGoalState completed [incarnation 4; 4197 ms]", ev_logger._clean_up_message( + self.assertEqual("ExtHandler ProcessGoalState completed [incarnation 4; 4197 ms]", ev_logger._clean_up_message( # pylint: disable=protected-access "2019/10/07 21:55:38.474861 INFO ExtHandler ProcessGoalState completed [incarnation 4; 4197 ms]")) - self.assertEqual("Daemon Azure Linux Agent Version:2.2.43", ev_logger._clean_up_message( + self.assertEqual("Daemon Azure Linux Agent Version:2.2.43", ev_logger._clean_up_message( # pylint: disable=protected-access "2019/10/07 21:52:28.615720 INFO Daemon Azure Linux Agent Version:2.2.43")) self.assertEqual('Daemon Cgroup controller "memory" is not mounted. Failed to create a cgroup for the VM Agent;' ' resource usage will not be tracked', - ev_logger._clean_up_message('Daemon Cgroup controller "memory" is not mounted. Failed to ' + ev_logger._clean_up_message('Daemon Cgroup controller "memory" is not mounted. Failed to ' # pylint: disable=protected-access 'create a cgroup for the VM Agent; resource usage will not be ' 'tracked')) self.assertEqual('ExtHandler Root directory /sys/fs/cgroup/memory/walinuxagent.extensions does not exist.', - ev_logger._clean_up_message("2019/10/08 23:45:05.691037 WARNING ExtHandler Root directory " + ev_logger._clean_up_message("2019/10/08 23:45:05.691037 WARNING ExtHandler Root directory " # pylint: disable=protected-access "/sys/fs/cgroup/memory/walinuxagent.extensions does not exist.")) self.assertEqual("LinuxAzureDiagnostic started to handle.", - ev_logger._clean_up_message("2019/10/07 22:02:40 LinuxAzureDiagnostic started to handle.")) + ev_logger._clean_up_message("2019/10/07 22:02:40 LinuxAzureDiagnostic started to handle.")) # pylint: disable=protected-access self.assertEqual("VMAccess started to handle.", - ev_logger._clean_up_message("2019/10/07 21:56:58 VMAccess started to handle.")) + ev_logger._clean_up_message("2019/10/07 21:56:58 VMAccess started to handle.")) # pylint: disable=protected-access self.assertEqual( '[PERIODIC] ExtHandler Root directory /sys/fs/cgroup/memory/walinuxagent.extensions does not exist.', - ev_logger._clean_up_message("2019/10/08 23:45:05.691037 WARNING [PERIODIC] ExtHandler Root directory " + ev_logger._clean_up_message("2019/10/08 23:45:05.691037 WARNING [PERIODIC] ExtHandler Root directory " # pylint: disable=protected-access "/sys/fs/cgroup/memory/walinuxagent.extensions does not exist.")) - self.assertEqual("[PERIODIC] LinuxAzureDiagnostic started to handle.", ev_logger._clean_up_message( + self.assertEqual("[PERIODIC] LinuxAzureDiagnostic started to handle.", ev_logger._clean_up_message( # pylint: disable=protected-access "2019/10/07 22:02:40 [PERIODIC] LinuxAzureDiagnostic started to handle.")) self.assertEqual("[PERIODIC] VMAccess started to handle.", - ev_logger._clean_up_message("2019/10/07 21:56:58 [PERIODIC] VMAccess started to handle.")) + ev_logger._clean_up_message("2019/10/07 21:56:58 [PERIODIC] VMAccess started to handle.")) # pylint: disable=protected-access self.assertEqual('[PERIODIC] Daemon Cgroup controller "memory" is not mounted. Failed to create a cgroup for ' 'the VM Agent; resource usage will not be tracked', - ev_logger._clean_up_message('[PERIODIC] Daemon Cgroup controller "memory" is not mounted. ' + ev_logger._clean_up_message('[PERIODIC] Daemon Cgroup controller "memory" is not mounted. ' # pylint: disable=protected-access 'Failed to create a cgroup for the VM Agent; resource usage will ' 'not be tracked')) - self.assertEquals('The time should be in UTC', ev_logger._clean_up_message( + self.assertEquals('The time should be in UTC', ev_logger._clean_up_message( # pylint: disable=deprecated-method,protected-access '2019-11-26T18:15:06.866746Z INFO The time should be in UTC')) - self.assertEquals('The time should be in UTC', ev_logger._clean_up_message( + self.assertEquals('The time should be in UTC', ev_logger._clean_up_message( # pylint: disable=deprecated-method,protected-access '2019-11-26T18:15:06.866746Z The time should be in UTC')) - self.assertEquals('[PERIODIC] The time should be in UTC', ev_logger._clean_up_message( + self.assertEquals('[PERIODIC] The time should be in UTC', ev_logger._clean_up_message( # pylint: disable=deprecated-method,protected-access '2019-11-26T18:15:06.866746Z INFO [PERIODIC] The time should be in UTC')) - self.assertEquals('[PERIODIC] The time should be in UTC', ev_logger._clean_up_message( + self.assertEquals('[PERIODIC] The time should be in UTC', ev_logger._clean_up_message( # pylint: disable=deprecated-method,protected-access '2019-11-26T18:15:06.866746Z [PERIODIC] The time should be in UTC')) diff --git a/tests/common/test_exception.py b/tests/common/test_exception.py index 52880bc934..b9a5348392 100644 --- a/tests/common/test_exception.py +++ b/tests/common/test_exception.py @@ -15,7 +15,7 @@ # Requires Python 2.6+ and Openssl 1.0+ # -import sys, inspect +import sys, inspect # pylint: disable=multiple-imports from azurelinuxagent.common.exception import AgentError from tests.tools import AgentTestCase diff --git a/tests/common/test_logcollector.py b/tests/common/test_logcollector.py index 1c0f10476e..ece2efe34d 100644 --- a/tests/common/test_logcollector.py +++ b/tests/common/test_logcollector.py @@ -123,8 +123,8 @@ def _build_manifest(cls, manifest_file): os.path.join(cls.root_collect_dir, "non_existing_file"), ] - with open(manifest_file, "w") as fh: - for file in files: + with open(manifest_file, "w") as fh: # pylint: disable=invalid-name + for file in files: # pylint: disable=redefined-builtin fh.write("copy,{0}\n".format(file)) @staticmethod @@ -132,7 +132,7 @@ def _create_file_of_specific_size(file_path, file_size, binary=False): binary_descriptor = "b" if binary else "" data = b'0' if binary else '0' - with open(file_path, "w{0}".format(binary_descriptor)) as fh: + with open(file_path, "w{0}".format(binary_descriptor)) as fh: # pylint: disable=bad-open-mode,invalid-name fh.seek(file_size - 1) fh.write(data) @@ -144,7 +144,7 @@ def _assert_files_are_in_archive(self, expected_files): with zipfile.ZipFile(self.compressed_archive_path, "r") as archive: archive_files = archive.namelist() - for file in expected_files: + for file in expected_files: # pylint: disable=redefined-builtin if file.lstrip(os.path.sep) not in archive_files: self.fail("File {0} was supposed to be collected, but is not present in the archive!".format(file)) @@ -152,24 +152,24 @@ def _assert_files_are_in_archive(self, expected_files): if "results.txt" not in archive_files: self.fail("File results.txt was supposed to be collected, but is not present in the archive!") - self.assertTrue(True) + self.assertTrue(True) # pylint: disable=redundant-unittest-assert def _assert_files_are_not_in_archive(self, unexpected_files): with zipfile.ZipFile(self.compressed_archive_path, "r") as archive: archive_files = archive.namelist() - for file in unexpected_files: + for file in unexpected_files: # pylint: disable=redefined-builtin if file.lstrip(os.path.sep) in archive_files: self.fail("File {0} wasn't supposed to be collected, but is present in the archive!".format(file)) - self.assertTrue(True) + self.assertTrue(True) # pylint: disable=redundant-unittest-assert def _assert_archive_created(self, archive): with open(self.output_results_file_path, "r") as out: error_message = out.readlines()[-1] self.assertTrue(archive, "Failed to collect logs, error message: {0}".format(error_message)) - def _get_uncompressed_file_size(self, file): + def _get_uncompressed_file_size(self, file): # pylint: disable=redefined-builtin with zipfile.ZipFile(self.compressed_archive_path, "r") as archive: return archive.getinfo(file.lstrip(os.path.sep)).file_size @@ -193,10 +193,10 @@ def test_log_collector_parses_commands_in_manifest(self): manifest_file_path = os.path.join(self.tmp_dir, "manifest") write_file(manifest_file_path, manifest_content) - lc = LogCollector(manifest_file_path) + lc = LogCollector(manifest_file_path) # pylint: disable=invalid-name archive = lc.collect_logs() - with open(self.output_results_file_path, "r") as fh: + with open(self.output_results_file_path, "r") as fh: # pylint: disable=invalid-name results = fh.readlines() # Assert echo was parsed @@ -210,12 +210,12 @@ def test_log_collector_parses_commands_in_manifest(self): self._assert_files_are_in_archive(expected_files=[file_to_collect]) no_files = self._get_number_of_files_in_archive() - self.assertEquals(1, no_files, "Expected 1 file in archive, found {0}!".format(no_files)) + self.assertEquals(1, no_files, "Expected 1 file in archive, found {0}!".format(no_files)) # pylint: disable=deprecated-method def test_log_collector_should_collect_all_files(self): # All files in the manifest should be collected, since none of them are over the individual file size limit, # and combined they do not cross the archive size threshold. - lc = LogCollector(self.manifest_path) + lc = LogCollector(self.manifest_path) # pylint: disable=invalid-name archive = lc.collect_logs() self._assert_archive_created(archive) @@ -231,12 +231,12 @@ def test_log_collector_should_collect_all_files(self): self._assert_files_are_in_archive(expected_files) no_files = self._get_number_of_files_in_archive() - self.assertEquals(6, no_files, "Expected 6 files in archive, found {0}!".format(no_files)) + self.assertEquals(6, no_files, "Expected 6 files in archive, found {0}!".format(no_files)) # pylint: disable=deprecated-method def test_log_collector_should_truncate_large_text_files_and_ignore_large_binary_files(self): # Set the size limit so that some files are too large to collect in full. with patch("azurelinuxagent.common.logcollector._FILE_SIZE_LIMIT", SMALL_FILE_SIZE): - lc = LogCollector(self.manifest_path) + lc = LogCollector(self.manifest_path) # pylint: disable=invalid-name archive = lc.collect_logs() self._assert_archive_created(archive) @@ -255,7 +255,7 @@ def test_log_collector_should_truncate_large_text_files_and_ignore_large_binary_ self._assert_files_are_not_in_archive(unexpected_files) no_files = self._get_number_of_files_in_archive() - self.assertEquals(5, no_files, "Expected 5 files in archive, found {0}!".format(no_files)) + self.assertEquals(5, no_files, "Expected 5 files in archive, found {0}!".format(no_files)) # pylint: disable=deprecated-method def test_log_collector_should_prioritize_important_files_if_archive_too_big(self): # Set the archive size limit so that not all files can be collected. In that case, files will be added to the @@ -269,7 +269,7 @@ def test_log_collector_should_prioritize_important_files_if_archive_too_big(self with patch("azurelinuxagent.common.logcollector._UNCOMPRESSED_ARCHIVE_SIZE_LIMIT", 10 * 1024 * 1024): with patch("azurelinuxagent.common.logcollector._MUST_COLLECT_FILES", must_collect_files): - lc = LogCollector(self.manifest_path) + lc = LogCollector(self.manifest_path) # pylint: disable=invalid-name archive = lc.collect_logs() self._assert_archive_created(archive) @@ -288,7 +288,7 @@ def test_log_collector_should_prioritize_important_files_if_archive_too_big(self self._assert_files_are_not_in_archive(unexpected_files) no_files = self._get_number_of_files_in_archive() - self.assertEquals(3, no_files, "Expected 3 files in archive, found {0}!".format(no_files)) + self.assertEquals(3, no_files, "Expected 3 files in archive, found {0}!".format(no_files)) # pylint: disable=deprecated-method # Second collection, if a file got deleted, delete it from the archive and add next file on the priority list # if there is enough space. @@ -314,12 +314,12 @@ def test_log_collector_should_prioritize_important_files_if_archive_too_big(self self._assert_archive_created(second_archive) no_files = self._get_number_of_files_in_archive() - self.assertEquals(5, no_files, "Expected 5 files in archive, found {0}!".format(no_files)) + self.assertEquals(5, no_files, "Expected 5 files in archive, found {0}!".format(no_files)) # pylint: disable=deprecated-method def test_log_collector_should_update_archive_when_files_are_new_or_modified_or_deleted(self): # Ensure the archive reflects the state of files on the disk at collection time. If a file was updated, it # needs to be updated in the archive, deleted if removed from disk, and added if not previously seen. - lc = LogCollector(self.manifest_path) + lc = LogCollector(self.manifest_path) # pylint: disable=invalid-name first_archive = lc.collect_logs() self._assert_archive_created(first_archive) @@ -335,7 +335,7 @@ def test_log_collector_should_update_archive_when_files_are_new_or_modified_or_d self._assert_files_are_in_archive(expected_files) no_files = self._get_number_of_files_in_archive() - self.assertEquals(6, no_files, "Expected 6 files in archive, found {0}!".format(no_files)) + self.assertEquals(6, no_files, "Expected 6 files in archive, found {0}!".format(no_files)) # pylint: disable=deprecated-method # Update a file and its last modified time to ensure the last modified time and last collection time are not # the same in this test @@ -366,13 +366,13 @@ def test_log_collector_should_update_archive_when_files_are_new_or_modified_or_d self._assert_files_are_in_archive(expected_files) self._assert_files_are_not_in_archive(unexpected_files) - file = os.path.join(self.root_collect_dir, "waagent.log") + file = os.path.join(self.root_collect_dir, "waagent.log") # pylint: disable=redefined-builtin new_file_size = self._get_uncompressed_file_size(file) - self.assertEquals(LARGE_FILE_SIZE, new_file_size, "File {0} hasn't been updated! Size in archive is {1}, but " + self.assertEquals(LARGE_FILE_SIZE, new_file_size, "File {0} hasn't been updated! Size in archive is {1}, but " # pylint: disable=deprecated-method "should be {2}.".format(file, new_file_size, LARGE_FILE_SIZE)) no_files = self._get_number_of_files_in_archive() - self.assertEquals(6, no_files, "Expected 6 files in archive, found {0}!".format(no_files)) + self.assertEquals(6, no_files, "Expected 6 files in archive, found {0}!".format(no_files)) # pylint: disable=deprecated-method def test_log_collector_should_clean_up_uncollected_truncated_files(self): # Make sure that truncated files that are no longer needed are cleaned up. If an existing truncated file @@ -389,7 +389,7 @@ def test_log_collector_should_clean_up_uncollected_truncated_files(self): with patch("azurelinuxagent.common.logcollector._UNCOMPRESSED_ARCHIVE_SIZE_LIMIT", 2 * SMALL_FILE_SIZE): with patch("azurelinuxagent.common.logcollector._MUST_COLLECT_FILES", must_collect_files): with patch("azurelinuxagent.common.logcollector._FILE_SIZE_LIMIT", SMALL_FILE_SIZE): - lc = LogCollector(self.manifest_path) + lc = LogCollector(self.manifest_path) # pylint: disable=invalid-name archive = lc.collect_logs() self._assert_archive_created(archive) @@ -401,7 +401,7 @@ def test_log_collector_should_clean_up_uncollected_truncated_files(self): self._assert_files_are_in_archive(expected_files) no_files = self._get_number_of_files_in_archive() - self.assertEquals(2, no_files, "Expected 2 files in archive, found {0}!".format(no_files)) + self.assertEquals(2, no_files, "Expected 2 files in archive, found {0}!".format(no_files)) # pylint: disable=deprecated-method # Remove the original file so it is not collected anymore. In the next collection, the truncated file should be # removed both from the archive and from the filesystem. @@ -410,7 +410,7 @@ def test_log_collector_should_clean_up_uncollected_truncated_files(self): with patch("azurelinuxagent.common.logcollector._UNCOMPRESSED_ARCHIVE_SIZE_LIMIT", 2 * SMALL_FILE_SIZE): with patch("azurelinuxagent.common.logcollector._MUST_COLLECT_FILES", must_collect_files): with patch("azurelinuxagent.common.logcollector._FILE_SIZE_LIMIT", SMALL_FILE_SIZE): - lc = LogCollector(self.manifest_path) + lc = LogCollector(self.manifest_path) # pylint: disable=invalid-name second_archive = lc.collect_logs() expected_files = [ @@ -426,7 +426,7 @@ def test_log_collector_should_clean_up_uncollected_truncated_files(self): self._assert_archive_created(second_archive) no_files = self._get_number_of_files_in_archive() - self.assertEquals(2, no_files, "Expected 2 files in archive, found {0}!".format(no_files)) + self.assertEquals(2, no_files, "Expected 2 files in archive, found {0}!".format(no_files)) # pylint: disable=deprecated-method truncated_files = os.listdir(self.truncated_files_dir) - self.assertEquals(0, len(truncated_files), "Uncollected truncated file waagent.log.1 should have been deleted!") + self.assertEquals(0, len(truncated_files), "Uncollected truncated file waagent.log.1 should have been deleted!") # pylint: disable=deprecated-method diff --git a/tests/common/test_logger.py b/tests/common/test_logger.py index 8698ed2045..8651ddd751 100644 --- a/tests/common/test_logger.py +++ b/tests/common/test_logger.py @@ -15,13 +15,16 @@ # Requires Python 2.6+ and Openssl 1.0+ # -import json +import json # pylint: disable=unused-import import os import tempfile from datetime import datetime, timedelta +# pylint: disable=unused-import from azurelinuxagent.common.event import __event_logger__, add_log_event, MAX_NUMBER_OF_EVENTS, TELEMETRY_LOG_EVENT_ID,\ - TELEMETRY_LOG_PROVIDER_ID, EVENTS_DIRECTORY + TELEMETRY_LOG_PROVIDER_ID, EVENTS_DIRECTORY +# pylint: enable=unused-import + import azurelinuxagent.common.logger as logger from azurelinuxagent.common.utils import fileutil from tests.tools import AgentTestCase, MagicMock, patch, skip_if_predicate_true @@ -231,7 +234,7 @@ def test_telemetry_logger(self): mock.assert_not_called() mock.reset_mock() - for i in range(5): + for i in range(5): # pylint: disable=unused-variable appender.write(logger.LogLevel.ERROR, "--unit-test-ERROR--") appender.write(logger.LogLevel.INFO, "--unit-test-INFO--") @@ -250,7 +253,7 @@ def test_telemetry_logger_not_on_by_default(self, mock_save): @patch("azurelinuxagent.common.logger.ConsoleAppender.write") @patch("azurelinuxagent.common.logger.FileAppender.write") def test_add_appender(self, mock_file_write, mock_console_write, mock_telem_write, mock_stdout_write): - lg = logger.Logger(logger.DEFAULT_LOGGER, "TestLogger1") + lg = logger.Logger(logger.DEFAULT_LOGGER, "TestLogger1") # pylint: disable=invalid-name lg.add_appender(logger.AppenderType.FILE, logger.LogLevel.INFO, path=self.log_file) lg.add_appender(logger.AppenderType.TELEMETRY, logger.LogLevel.WARNING, path=add_log_event) @@ -298,11 +301,11 @@ def test_add_appender(self, mock_file_write, mock_console_write, mock_telem_writ @patch("azurelinuxagent.common.logger.ConsoleAppender.write") @patch("azurelinuxagent.common.logger.FileAppender.write") def test_set_prefix(self, mock_file_write, mock_console_write, mock_telem_write, mock_stdout_write): - lg = logger.Logger(logger.DEFAULT_LOGGER) + lg = logger.Logger(logger.DEFAULT_LOGGER) # pylint: disable=invalid-name prefix = "YoloLogger" lg.set_prefix(prefix) - self.assertEquals(lg.prefix, prefix) + self.assertEquals(lg.prefix, prefix) # pylint: disable=deprecated-method lg.add_appender(logger.AppenderType.FILE, logger.LogLevel.INFO, path=self.log_file) lg.add_appender(logger.AppenderType.TELEMETRY, logger.LogLevel.WARNING, path=add_log_event) @@ -338,7 +341,7 @@ def test_nested_logger(self, mock_file_write, mock_console_write, mock_telem_wri logger.add_logger_appender(logger.AppenderType.STDOUT, logger.LogLevel.WARNING) logger.set_prefix(parent_prefix) - lg = logger.Logger(logger.DEFAULT_LOGGER, child_prefix) + lg = logger.Logger(logger.DEFAULT_LOGGER, child_prefix) # pylint: disable=invalid-name lg.error("Test Log") self.assertEqual(1, mock_file_write.call_count) @@ -383,8 +386,8 @@ def test_telemetry_logger_add_log_event(self, mock_lib_dir, *_): logcontent = logfile.read() # Checking the contents of the event file. self.assertIn("Test Log - Warning", logcontent) - except Exception as e: - self.assertFalse(True, "The log file looks like it isn't correctly setup for this test. Take a look. " + except Exception as e: # pylint: disable=invalid-name + self.assertFalse(True, "The log file looks like it isn't correctly setup for this test. Take a look. " # pylint: disable=redundant-unittest-assert "{0}".format(e)) @skip_if_predicate_true(lambda: True, "Enable this test when SEND_LOGS_TO_TELEMETRY is enabled") @@ -417,7 +420,7 @@ def test_telemetry_logger_verify_maximum_recursion_depths_doesnt_happen(self, *_ @patch("azurelinuxagent.common.logger.ConsoleAppender.write") @patch("azurelinuxagent.common.event.send_logs_to_telemetry", return_value=True) @patch("azurelinuxagent.common.conf.get_lib_dir") - def test_telemetry_logger_check_all_file_logs_written_when_events_gt_MAX_NUMBER_OF_EVENTS(self, mock_lib_dir, *_): + def test_telemetry_logger_check_all_file_logs_written_when_events_gt_MAX_NUMBER_OF_EVENTS(self, mock_lib_dir, *_): # pylint: disable=invalid-name mock_lib_dir.return_value = self.lib_dir __event_logger__.event_dir = self.event_dir no_of_log_statements = MAX_NUMBER_OF_EVENTS + 100 @@ -453,8 +456,8 @@ def test_telemetry_logger_check_all_file_logs_written_when_events_gt_MAX_NUMBER_ self.assertRegex(logcontent[1001], r"(.*WARNING\s*{0}\s*\[PERIODIC\]\s*Too many files under:.*{1}, " r"current count\:\s*\d+,\s*removing oldest\s*.*)".format(prefix, self.event_dir)) - except Exception as e: - self.assertFalse(True, "The log file looks like it isn't correctly setup for this test. " + except Exception as e: # pylint: disable=invalid-name + self.assertFalse(True, "The log file looks like it isn't correctly setup for this test. " # pylint: disable=redundant-unittest-assert "Take a look. {0}".format(e)) diff --git a/tests/common/test_resourceusage.py b/tests/common/test_resourceusage.py index 7d38799322..f1056824ec 100644 --- a/tests/common/test_resourceusage.py +++ b/tests/common/test_resourceusage.py @@ -23,7 +23,7 @@ def raise_ioerror(*_): - e = IOError() + e = IOError() # pylint: disable=invalid-name from errno import ENOENT e.errno = ENOENT raise e @@ -62,12 +62,12 @@ def test_get_proc_cmdline(self, patch_read_file): patch_read_file.read_file.side_effect = raise_ioerror # No such file exists; _get_proc_cmdline throws exception. with self.assertRaises(IOError): - ProcessInfo._get_proc_cmdline(1000) + ProcessInfo._get_proc_cmdline(1000) # pylint: disable=protected-access patch_read_file.read_file.side_effect = raise_exception # Other exception; _get_proc_cmdline throws exception. with self.assertRaises(ProcessInfoException): - ProcessInfo._get_proc_cmdline(1000) + ProcessInfo._get_proc_cmdline(1000) # pylint: disable=protected-access @patch("azurelinuxagent.common.resourceusage.fileutil") def test_get_proc_comm(self, patch_read_file): @@ -83,4 +83,4 @@ def test_get_proc_comm(self, patch_read_file): patch_read_file.read_file.side_effect = raise_exception # Other exception; _get_proc_cmdline throws exception. with self.assertRaises(ProcessInfoException): - ProcessInfo._get_proc_comm(1000) + ProcessInfo._get_proc_comm(1000) # pylint: disable=protected-access diff --git a/tests/common/test_singletonperthread.py b/tests/common/test_singletonperthread.py index f2371a3d67..e4e481aaad 100644 --- a/tests/common/test_singletonperthread.py +++ b/tests/common/test_singletonperthread.py @@ -6,7 +6,7 @@ from tests.tools import AgentTestCase, clear_singleton_instances -class TestClassToTestSingletonPerThread(SingletonPerThread): +class TestClassToTestSingletonPerThread(SingletonPerThread): # pylint: disable=too-few-public-methods """ Since these tests deal with testing in a multithreaded environment, we employ the use of multiprocessing.Queue() to ensure that the data is consistent. @@ -49,10 +49,10 @@ def setUp(self): self.errors = Queue() clear_singleton_instances(TestClassToTestSingletonPerThread) - def _setup_multithread_and_execute(self, func1, args1, func2, args2, t1_name=None, t2_name=None): + def _setup_multithread_and_execute(self, func1, args1, func2, args2, t1_name=None, t2_name=None): # pylint: disable=too-many-arguments - t1 = Thread(target=func1, args=args1) - t2 = Thread(target=func2, args=args2) + t1 = Thread(target=func1, args=args1) # pylint: disable=invalid-name + t2 = Thread(target=func2, args=args2) # pylint: disable=invalid-name t1.setName(t1_name if t1_name else self.THREAD_NAME_1) t2.setName(t2_name if t2_name else self.THREAD_NAME_2) t1.start() @@ -63,22 +63,22 @@ def _setup_multithread_and_execute(self, func1, args1, func2, args2, t1_name=Non errs = [] while not self.errors.empty(): errs.append(self.errors.get()) - if len(errs) > 0: + if len(errs) > 0: # pylint: disable=len-as-condition raise Exception("Errors: %s" % ' , '.join(errs)) @staticmethod - def _get_test_class_instance(q, err): + def _get_test_class_instance(q, err): # pylint: disable=invalid-name try: obj = TestClassToTestSingletonPerThread() q.put(obj) - except Exception as e: + except Exception as e: # pylint: disable=invalid-name err.put(str(e)) def _parse_instances_and_return_thread_objects(self, instances, t1_name=None, t2_name=None): obj1, obj2 = instances.get(), instances.get() def check_obj(name): - if obj1.name == name: + if obj1.name == name: # pylint: disable=no-else-return return obj1 elif obj2.name == name: return obj2 @@ -146,7 +146,7 @@ def test_singleton_object_should_match_thread_name(self): t1_name=t1_name, t2_name=t2_name) - singleton_instances = TestClassToTestSingletonPerThread._instances + singleton_instances = TestClassToTestSingletonPerThread._instances # pylint: disable=protected-access,no-member # Assert instance names are consistent with the thread names self.assertIn(test_class_obj_name(t1_name), singleton_instances) diff --git a/tests/common/test_telemetryevent.py b/tests/common/test_telemetryevent.py index e23ab68446..89adaad8e4 100644 --- a/tests/common/test_telemetryevent.py +++ b/tests/common/test_telemetryevent.py @@ -19,7 +19,7 @@ from tests.tools import AgentTestCase -def get_test_event(name="DummyExtension", op="Unknown", is_success=True, duration=0, version="foo", evt_type="", is_internal=False, +def get_test_event(name="DummyExtension", op="Unknown", is_success=True, duration=0, version="foo", evt_type="", is_internal=False, # pylint: disable=invalid-name,too-many-arguments message="DummyMessage", eventId=1): event = TelemetryEvent(eventId, "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX") event.parameters.append(TelemetryEventParam(GuestAgentExtensionEventsSchema.Name, name)) @@ -34,7 +34,7 @@ def get_test_event(name="DummyExtension", op="Unknown", is_success=True, duratio class TestTelemetryEvent(AgentTestCase): - def test_contains_works_for_TelemetryEvent(self): + def test_contains_works_for_TelemetryEvent(self): # pylint: disable=invalid-name test_event = get_test_event(message="Dummy Event") self.assertTrue(GuestAgentExtensionEventsSchema.Name in test_event) diff --git a/tests/common/test_version.py b/tests/common/test_version.py index 8767828077..f2fd085987 100644 --- a/tests/common/test_version.py +++ b/tests/common/test_version.py @@ -18,8 +18,8 @@ from __future__ import print_function import mock -import os -import textwrap +import os # pylint: disable=wrong-import-order +import textwrap # pylint: disable=wrong-import-order import azurelinuxagent.common.conf as conf from azurelinuxagent.common.event import EVENTS_DIRECTORY @@ -33,7 +33,7 @@ def freebsd_system(): return ["FreeBSD"] -def freebsd_system_release(x, y, z): +def freebsd_system_release(x, y, z): # pylint: disable=unused-argument,invalid-name return "10.0" @@ -41,7 +41,7 @@ def openbsd_system(): return ["OpenBSD"] -def openbsd_system_release(x, y, z): +def openbsd_system_release(x, y, z): # pylint: disable=unused-argument,invalid-name return "20.0" @@ -59,19 +59,19 @@ def default_system_exception(): def is_platform_dist_supported(): # platform.dist() and platform.linux_distribution() is deprecated from Python 3.8+ - if PY_VERSION_MAJOR == 3 and PY_VERSION_MINOR >= 8: + if PY_VERSION_MAJOR == 3 and PY_VERSION_MINOR >= 8: # pylint: disable=no-else-return return False else: return True class TestAgentVersion(AgentTestCase): - def setUp(self): + def setUp(self): # pylint: disable=useless-return AgentTestCase.setUp(self) return @mock.patch('platform.system', side_effect=freebsd_system) @mock.patch('re.sub', side_effect=freebsd_system_release) - def test_distro_is_correct_format_when_freebsd(self, platform_system_name, mock_variable): + def test_distro_is_correct_format_when_freebsd(self, platform_system_name, mock_variable): # pylint: disable=useless-return,unused-argument osinfo = get_distro() freebsd_list = ['freebsd', "10.0", '', 'freebsd'] self.assertListEqual(freebsd_list, osinfo) @@ -79,14 +79,14 @@ def test_distro_is_correct_format_when_freebsd(self, platform_system_name, mock_ @mock.patch('platform.system', side_effect=openbsd_system) @mock.patch('re.sub', side_effect=openbsd_system_release) - def test_distro_is_correct_format_when_openbsd(self, platform_system_name, mock_variable): + def test_distro_is_correct_format_when_openbsd(self, platform_system_name, mock_variable): # pylint: disable=useless-return,unused-argument osinfo = get_distro() openbsd_list = ['openbsd', "20.0", '', 'openbsd'] self.assertListEqual(openbsd_list, osinfo) return @mock.patch('platform.system', side_effect=default_system) - def test_distro_is_correct_format_when_default_case(self, *args): + def test_distro_is_correct_format_when_default_case(self, *args): # pylint: disable=useless-return,unused-argument default_list = ['', '', '', ''] unknown_list = ['unknown', 'FFFF', '', ''] @@ -101,7 +101,7 @@ def test_distro_is_correct_format_when_default_case(self, *args): return @mock.patch('platform.system', side_effect=default_system) - def test_distro_is_correct_for_exception_case(self, *args): + def test_distro_is_correct_for_exception_case(self, *args): # pylint: disable=useless-return,unused-argument default_list = ['unknown', 'FFFF', '', ''] if is_platform_dist_supported(): @@ -117,26 +117,26 @@ def test_distro_is_correct_for_exception_case(self, *args): class TestCurrentAgentName(AgentTestCase): - def setUp(self): + def setUp(self): # pylint: disable=useless-return AgentTestCase.setUp(self) return @patch("os.getcwd", return_value="/default/install/directory") - def test_extract_name_finds_installed(self, mock_cwd): + def test_extract_name_finds_installed(self, mock_cwd): # pylint: disable=useless-return,unused-argument current_agent, current_version = set_current_agent() self.assertEqual(AGENT_LONG_VERSION, current_agent) self.assertEqual(AGENT_VERSION, str(current_version)) return @patch("os.getcwd", return_value="/") - def test_extract_name_root_finds_installed(self, mock_cwd): + def test_extract_name_root_finds_installed(self, mock_cwd): # pylint: disable=useless-return,unused-argument current_agent, current_version = set_current_agent() self.assertEqual(AGENT_LONG_VERSION, current_agent) self.assertEqual(AGENT_VERSION, str(current_version)) return @patch("os.getcwd") - def test_extract_name_in_path_finds_installed(self, mock_cwd): + def test_extract_name_in_path_finds_installed(self, mock_cwd): # pylint: disable=useless-return path = os.path.join(conf.get_lib_dir(), EVENTS_DIRECTORY) mock_cwd.return_value = path current_agent, current_version = set_current_agent() @@ -145,7 +145,7 @@ def test_extract_name_in_path_finds_installed(self, mock_cwd): return @patch("os.getcwd") - def test_extract_name_finds_latest_agent(self, mock_cwd): + def test_extract_name_finds_latest_agent(self, mock_cwd): # pylint: disable=useless-return path = os.path.join(conf.get_lib_dir(), "{0}-{1}".format( AGENT_NAME, "1.2.3")) @@ -172,7 +172,7 @@ def test_get_f5_platform_bigip_12_1_1(self): Changelist: 1874858 JobID: 705993""") - mo = mock.mock_open(read_data=version_file) + mo = mock.mock_open(read_data=version_file) # pylint: disable=invalid-name with patch(open_patch(), mo): platform = get_f5_platform() self.assertTrue(platform[0] == 'bigip') @@ -193,7 +193,7 @@ def test_get_f5_platform_bigip_12_1_0_hf1(self): Changelist: 1773831 JobID: 673467""") - mo = mock.mock_open(read_data=version_file) + mo = mock.mock_open(read_data=version_file) # pylint: disable=invalid-name with patch(open_patch(), mo): platform = get_f5_platform() self.assertTrue(platform[0] == 'bigip') @@ -214,7 +214,7 @@ def test_get_f5_platform_bigip_12_0_0(self): Changelist: 1486072 JobID: 536212""") - mo = mock.mock_open(read_data=version_file) + mo = mock.mock_open(read_data=version_file) # pylint: disable=invalid-name with patch(open_patch(), mo): platform = get_f5_platform() self.assertTrue(platform[0] == 'bigip') @@ -235,7 +235,7 @@ def test_get_f5_platform_iworkflow_2_0_1(self): Changelist: 1924048 JobID: 734712""") - mo = mock.mock_open(read_data=version_file) + mo = mock.mock_open(read_data=version_file) # pylint: disable=invalid-name with patch(open_patch(), mo): platform = get_f5_platform() self.assertTrue(platform[0] == 'iworkflow') @@ -256,7 +256,7 @@ def test_get_f5_platform_bigiq_5_1_0(self): Changelist: 1907534 JobID: 726344""") - mo = mock.mock_open(read_data=version_file) + mo = mock.mock_open(read_data=version_file) # pylint: disable=invalid-name with patch(open_patch(), mo): platform = get_f5_platform() self.assertTrue(platform[0] == 'bigiq') diff --git a/tests/daemon/test_daemon.py b/tests/daemon/test_daemon.py index 5b412e4cb3..e1d0403a78 100644 --- a/tests/daemon/test_daemon.py +++ b/tests/daemon/test_daemon.py @@ -25,7 +25,7 @@ from tests.tools import AgentTestCase, Mock, patch -class MockDaemonCall(object): +class MockDaemonCall(object): # pylint: disable=too-few-public-methods def __init__(self, daemon_handler, count): self.daemon_handler = daemon_handler self.count = count @@ -52,7 +52,7 @@ def test_daemon_restart(self, mock_sleep): daemon_handler.run() mock_sleep.assert_any_call(15) - self.assertEquals(2, daemon_handler.daemon.call_count) + self.assertEquals(2, daemon_handler.daemon.call_count) # pylint: disable=deprecated-method @patch("time.sleep") @patch("azurelinuxagent.daemon.main.conf") @@ -91,7 +91,7 @@ def test_does_not_set_openssl_fips(self, _, __): @patch('azurelinuxagent.common.conf.get_provisioning_agent', return_value='waagent') @patch('azurelinuxagent.ga.update.UpdateHandler.run_latest') @patch('azurelinuxagent.pa.provision.default.ProvisionHandler.run') - def test_daemon_agent_enabled(self, patch_run_provision, patch_run_latest, gpa): + def test_daemon_agent_enabled(self, patch_run_provision, patch_run_latest, gpa): # pylint: disable=unused-argument """ Agent should run normally when no disable_agent is found """ @@ -102,7 +102,7 @@ def test_daemon_agent_enabled(self, patch_run_provision, patch_run_latest, gpa): self.assertFalse(os.path.exists(conf.get_disable_agent_file_path())) daemon_handler = get_daemon_handler() - def stop_daemon(child_args): + def stop_daemon(child_args): # pylint: disable=unused-argument daemon_handler.running = False patch_run_latest.side_effect = stop_daemon @@ -114,7 +114,7 @@ def stop_daemon(child_args): @patch('azurelinuxagent.common.conf.get_provisioning_agent', return_value='waagent') @patch('azurelinuxagent.ga.update.UpdateHandler.run_latest', side_effect=AgentTestCase.fail) @patch('azurelinuxagent.pa.provision.default.ProvisionHandler.run', side_effect=ProvisionHandler.write_agent_disabled) - def test_daemon_agent_disabled(self, _, patch_run_latest, gpa): + def test_daemon_agent_disabled(self, _, patch_run_latest, gpa): # pylint: disable=unused-argument """ Agent should provision, then sleep forever when disable_agent is found """ diff --git a/tests/daemon/test_resourcedisk.py b/tests/daemon/test_resourcedisk.py index a7396c5227..2c9ae2ea8c 100644 --- a/tests/daemon/test_resourcedisk.py +++ b/tests/daemon/test_resourcedisk.py @@ -47,11 +47,11 @@ def test_mount_flags_many(self): @patch( 'azurelinuxagent.daemon.resourcedisk.default.ResourceDiskHandler.check_existing_swap_file', return_value=False) - def test_create_swap_space( + def test_create_swap_space( # pylint: disable=too-many-arguments self, - mock_check_existing_swap_file, - mock_isfile, - mock_mkfile, + mock_check_existing_swap_file, # pylint: disable=unused-argument + mock_isfile, # pylint: disable=unused-argument + mock_mkfile, # pylint: disable=unused-argument mock_run, mock_run_get_output): mount_point = '/mnt/resource' @@ -59,12 +59,12 @@ def test_create_swap_space( rdh = ResourceDiskHandler() - def rgo_side_effect(*args, **kwargs): + def rgo_side_effect(*args, **kwargs): # pylint: disable=unused-argument if args[0] == 'swapon -s': return (0, 'Filename\t\t\t\tType\t\tSize\tUsed\tPriority\n/mnt/resource/swapfile \tfile \t131068\t0\t-2\n') return DEFAULT - def run_side_effect(*args, **kwargs): + def run_side_effect(*args, **kwargs): # pylint: disable=unused-argument # We have to change the default mock behavior to return a falsey value # (instead of the default truthy of the mock), because we are testing # really for the exit code of the the swapon command to return 0. diff --git a/tests/distro/test_resourceDisk.py b/tests/distro/test_resourceDisk.py index 3235899282..5c6b197c40 100644 --- a/tests/distro/test_resourceDisk.py +++ b/tests/distro/test_resourceDisk.py @@ -1,4 +1,4 @@ -# Copyright 2018 Microsoft Corporation +# Copyright 2018 Microsoft Corporation # pylint: disable=invalid-name # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -120,7 +120,7 @@ def test_check_existing_swap_file(self): if os.path.exists(test_file): os.remove(test_file) - with open(test_file, "wb") as file: + with open(test_file, "wb") as file: # pylint: disable=redefined-builtin file.write(bytearray(file_size)) os.chmod(test_file, stat.S_ISUID | stat.S_ISGID | stat.S_IRUSR | diff --git a/tests/distro/test_scvmm.py b/tests/distro/test_scvmm.py index abc9b5d70b..9cbf2b2b0b 100644 --- a/tests/distro/test_scvmm.py +++ b/tests/distro/test_scvmm.py @@ -37,14 +37,14 @@ def test_scvmm_detection_with_file(self): scvmm_file = os.path.join(self.tmp_dir, scvmm.VMM_CONF_FILE_NAME) fileutil.write_file(scvmm_file, "") - with patch.object(scvmm.ScvmmHandler, 'start_scvmm_agent') as po: + with patch.object(scvmm.ScvmmHandler, 'start_scvmm_agent') as po: # pylint: disable=invalid-name with patch('os.listdir', return_value=["sr0", "sr1", "sr2"]): with patch('time.sleep', return_value=0): # execute failed = False try: scvmm.get_scvmm_handler().run() - except: + except: # pylint: disable=bare-except failed = True # assert self.assertTrue(failed) diff --git a/tests/ga/test_env.py b/tests/ga/test_env.py index c0dc7cf1ad..aceed49a2e 100644 --- a/tests/ga/test_env.py +++ b/tests/ga/test_env.py @@ -41,29 +41,29 @@ def tearDown(self): def test_get_dhcp_client_pid_should_return_a_sorted_list_of_pids(self): with patch("azurelinuxagent.common.utils.shellutil.run_command", return_value="11 9 5 22 4 6"): pids = EnvHandler().get_dhcp_client_pid() - self.assertEquals(pids, [4, 5, 6, 9, 11, 22]) + self.assertEquals(pids, [4, 5, 6, 9, 11, 22]) # pylint: disable=deprecated-method def test_get_dhcp_client_pid_should_return_an_empty_list_and_log_a_warning_when_dhcp_client_is_not_running(self): with patch("azurelinuxagent.common.osutil.default.shellutil.run_command", side_effect=lambda _: self.shellutil_run_command(["pidof", "non-existing-process"])): with patch('azurelinuxagent.common.logger.Logger.warn') as mock_warn: pids = EnvHandler().get_dhcp_client_pid() - self.assertEquals(pids, []) + self.assertEquals(pids, []) # pylint: disable=deprecated-method - self.assertEquals(mock_warn.call_count, 1) - args, kwargs = mock_warn.call_args + self.assertEquals(mock_warn.call_count, 1) # pylint: disable=deprecated-method + args, kwargs = mock_warn.call_args # pylint: disable=unused-variable message = args[0] - self.assertEquals("Dhcp client is not running.", message) + self.assertEquals("Dhcp client is not running.", message) # pylint: disable=deprecated-method def test_get_dhcp_client_pid_should_return_and_empty_list_and_log_an_error_when_an_invalid_command_is_used(self): with patch("azurelinuxagent.common.osutil.default.shellutil.run_command", side_effect=lambda _: self.shellutil_run_command(["non-existing-command"])): with patch('azurelinuxagent.common.logger.Logger.error') as mock_error: pids = EnvHandler().get_dhcp_client_pid() - self.assertEquals(pids, []) + self.assertEquals(pids, []) # pylint: disable=deprecated-method - self.assertEquals(mock_error.call_count, 1) - args, kwargs = mock_error.call_args + self.assertEquals(mock_error.call_count, 1) # pylint: disable=deprecated-method + args, kwargs = mock_error.call_args # pylint: disable=unused-variable self.assertIn("Failed to get the PID of the DHCP client", args[0]) self.assertIn("No such file or directory", args[1]) @@ -72,41 +72,41 @@ def test_get_dhcp_client_pid_should_not_log_consecutive_errors(self): with patch('azurelinuxagent.common.logger.Logger.warn') as mock_warn: def assert_warnings(count): - self.assertEquals(mock_warn.call_count, count) + self.assertEquals(mock_warn.call_count, count) # pylint: disable=deprecated-method for call_args in mock_warn.call_args_list: - args, kwargs = call_args - self.assertEquals("Dhcp client is not running.", args[0]) + args, kwargs = call_args # pylint: disable=unused-variable + self.assertEquals("Dhcp client is not running.", args[0]) # pylint: disable=deprecated-method with patch("azurelinuxagent.common.osutil.default.shellutil.run_command", side_effect=lambda _: self.shellutil_run_command(["pidof", "non-existing-process"])): # it should log the first error pids = env_handler.get_dhcp_client_pid() - self.assertEquals(pids, []) + self.assertEquals(pids, []) # pylint: disable=deprecated-method assert_warnings(1) # it should not log subsequent errors - for i in range(0, 3): + for i in range(0, 3): # pylint: disable=unused-variable pids = env_handler.get_dhcp_client_pid() - self.assertEquals(pids, []) - self.assertEquals(mock_warn.call_count, 1) + self.assertEquals(pids, []) # pylint: disable=deprecated-method + self.assertEquals(mock_warn.call_count, 1) # pylint: disable=deprecated-method with patch("azurelinuxagent.common.osutil.default.shellutil.run_command", return_value="123"): # now it should succeed pids = env_handler.get_dhcp_client_pid() - self.assertEquals(pids, [123]) + self.assertEquals(pids, [123]) # pylint: disable=deprecated-method assert_warnings(1) with patch("azurelinuxagent.common.osutil.default.shellutil.run_command", side_effect=lambda _: self.shellutil_run_command(["pidof", "non-existing-process"])): # it should log the new error pids = env_handler.get_dhcp_client_pid() - self.assertEquals(pids, []) + self.assertEquals(pids, []) # pylint: disable=deprecated-method assert_warnings(2) # it should not log subsequent errors for i in range(0, 3): pids = env_handler.get_dhcp_client_pid() - self.assertEquals(pids, []) - self.assertEquals(mock_warn.call_count, 2) + self.assertEquals(pids, []) # pylint: disable=deprecated-method + self.assertEquals(mock_warn.call_count, 2) # pylint: disable=deprecated-method def test_handle_dhclient_restart_should_reconfigure_network_routes_when_dhcp_client_restarts(self): with patch("azurelinuxagent.common.dhcp.DhcpHandler.conf_routes") as mock_conf_routes: @@ -118,7 +118,7 @@ def test_handle_dhclient_restart_should_reconfigure_network_routes_when_dhcp_cli with patch.object(env_handler, "get_dhcp_client_pid", return_value=[123]): env_handler.dhcp_handler.conf_routes() env_handler.dhcp_id_list = env_handler.get_dhcp_client_pid() - self.assertEquals(mock_conf_routes.call_count, 1) + self.assertEquals(mock_conf_routes.call_count, 1) # pylint: disable=deprecated-method # # if the dhcp client has not been restarted then it should not reconfigure the network routes @@ -131,12 +131,12 @@ def mock_check_pid_alive(pid): with patch("azurelinuxagent.common.osutil.default.DefaultOSUtil.check_pid_alive", side_effect=mock_check_pid_alive): with patch.object(env_handler, "get_dhcp_client_pid", side_effect=Exception("get_dhcp_client_pid should not have been invoked")): env_handler.handle_dhclient_restart() - self.assertEquals(mock_conf_routes.call_count, 1) # count did not change + self.assertEquals(mock_conf_routes.call_count, 1) # count did not change # pylint: disable=deprecated-method # # if the process was restarted then it should reconfigure the network routes # - def mock_check_pid_alive(pid): + def mock_check_pid_alive(pid): # pylint: disable=function-redefined if pid == 123: return False raise Exception("Unexpected PID: {0}".format(pid)) @@ -144,12 +144,12 @@ def mock_check_pid_alive(pid): with patch("azurelinuxagent.common.osutil.default.DefaultOSUtil.check_pid_alive", side_effect=mock_check_pid_alive): with patch.object(env_handler, "get_dhcp_client_pid", return_value=[456, 789]): env_handler.handle_dhclient_restart() - self.assertEquals(mock_conf_routes.call_count, 2) # count increased + self.assertEquals(mock_conf_routes.call_count, 2) # count increased # pylint: disable=deprecated-method # # if the new dhcp client has not been restarted then it should not reconfigure the network routes # - def mock_check_pid_alive(pid): + def mock_check_pid_alive(pid): # pylint: disable=function-redefined if pid in [456, 789]: return True raise Exception("Unexpected PID: {0}".format(pid)) @@ -157,4 +157,4 @@ def mock_check_pid_alive(pid): with patch("azurelinuxagent.common.osutil.default.DefaultOSUtil.check_pid_alive", side_effect=mock_check_pid_alive): with patch.object(env_handler, "get_dhcp_client_pid", side_effect=Exception("get_dhcp_client_pid should not have been invoked")): env_handler.handle_dhclient_restart() - self.assertEquals(mock_conf_routes.call_count, 2) # count did not change + self.assertEquals(mock_conf_routes.call_count, 2) # count did not change # pylint: disable=deprecated-method diff --git a/tests/ga/test_extension.py b/tests/ga/test_extension.py index a242511078..c52800ac6a 100644 --- a/tests/ga/test_extension.py +++ b/tests/ga/test_extension.py @@ -1,4 +1,4 @@ -# Copyright 2018 Microsoft Corporation +# Copyright 2018 Microsoft Corporation # pylint: disable=too-many-lines # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -25,35 +25,41 @@ import time import unittest import uuid -import zipfile +import zipfile # pylint: disable=unused-import import datetime from azurelinuxagent.common import conf from azurelinuxagent.common.cgroupconfigurator import CGroupConfigurator from azurelinuxagent.common.datacontract import get_properties -from azurelinuxagent.common.protocol.util import get_protocol_util +from azurelinuxagent.common.protocol.util import get_protocol_util # pylint: disable=unused-import from azurelinuxagent.common.utils import fileutil from azurelinuxagent.common.utils.fileutil import read_file from azurelinuxagent.common.utils.flexible_version import FlexibleVersion from azurelinuxagent.common.version import PY_VERSION_MAJOR, PY_VERSION_MINOR, PY_VERSION_MICRO, AGENT_NAME, \ GOAL_STATE_AGENT_VERSION, CURRENT_VERSION, DISTRO_NAME, DISTRO_VERSION +# pylint: disable=unused-import from azurelinuxagent.ga.exthandlers import ExtHandlerState, ExtHandlersHandler, ExtHandlerInstance, HANDLER_PKG_EXT, \ migrate_handler_state, get_exthandlers_handler, AGENT_STATUS_FILE, ExtCommandEnvVariable, \ HandlerManifest, NOT_RUN, ValidHandlerStatus, HANDLER_COMPLETE_NAME_PATTERN, HandlerEnvironment, ExtensionRequestedState +# pylint: enable=unused-import -from azurelinuxagent.ga.monitor import get_monitor_handler -from nose.plugins.attrib import attr +from azurelinuxagent.ga.monitor import get_monitor_handler # pylint: disable=unused-import +from nose.plugins.attrib import attr # pylint: disable=wrong-import-order,unused-import from tests.protocol import mockwiredata from tests.protocol.mocks import mock_wire_protocol, HttpRequestPredicates from tests.protocol.mockwiredata import DATA_FILE -from tests.tools import AgentTestCase, data_dir, i_am_root, MagicMock, Mock, patch, mock_sleep +from tests.tools import AgentTestCase, data_dir, i_am_root, MagicMock, Mock, patch, mock_sleep # pylint: disable=unused-import +# pylint: disable=ungrouped-imports from azurelinuxagent.common.exception import ResourceGoneError, ExtensionDownloadError, ProtocolError, \ ExtensionErrorCodes, ExtensionError, ExtensionUpdateError +# pylint: enable=ungrouped-imports +# pylint: disable=ungrouped-imports from azurelinuxagent.common.protocol.restapi import Extension, ExtHandlerProperties, ExtHandler, ExtHandlerStatus, \ ExtensionStatus -from azurelinuxagent.common.protocol.wire import WireProtocol, InVMArtifactsProfile -from azurelinuxagent.common.utils.restutil import KNOWN_WIRESERVER_IP +# pylint: enable=ungrouped-imports +from azurelinuxagent.common.protocol.wire import WireProtocol, InVMArtifactsProfile # pylint: disable=ungrouped-imports +from azurelinuxagent.common.utils.restutil import KNOWN_WIRESERVER_IP # pylint: disable=ungrouped-imports # Mocking the original sleep to reduce test execution time SLEEP = time.sleep @@ -70,8 +76,8 @@ def raise_system_exception(): raise Exception -def raise_ioerror(*args): - e = IOError() +def raise_ioerror(*args): # pylint: disable=unused-argument + e = IOError() # pylint: disable=invalid-name from errno import EIO e.errno = EIO raise e @@ -101,22 +107,22 @@ def _count_extension_directories(): def _is_extension_dir(path): return re.match(HANDLER_COMPLETE_NAME_PATTERN, os.path.basename(path)) is not None - def _assert_ext_handler_status(self, aggregate_status, expected_status, version, expected_ext_handler_count=0): + def _assert_ext_handler_status(self, aggregate_status, expected_status, version, expected_ext_handler_count=0): # pylint: disable=useless-return self.assertIsNotNone(aggregate_status, "Aggregate status should not be None") handler_statuses = aggregate_status['aggregateStatus']['handlerAggregateStatus'] self.assertEqual(expected_ext_handler_count, len(handler_statuses), "All ExtensionHandlers: {0}".format(handler_statuses)) for ext_handler_status in handler_statuses: debug_info = "ExtensionHandler: {0}".format(ext_handler_status) - self.assertEquals(expected_status, ext_handler_status['status'], debug_info) - self.assertEquals(version, ext_handler_status['handlerVersion'], debug_info) + self.assertEquals(expected_status, ext_handler_status['status'], debug_info) # pylint: disable=deprecated-method + self.assertEquals(version, ext_handler_status['handlerVersion'], debug_info) # pylint: disable=deprecated-method return @contextlib.contextmanager def _setup_test_env(self, test_data): with mock_wire_protocol(test_data) as protocol: - def mock_http_put(url, *args, **kwargs): + def mock_http_put(url, *args, **kwargs): # pylint: disable=unused-argument,inconsistent-return-statements if HttpRequestPredicates.is_host_plugin_status_request(url): # Skip reading the HostGA request data as its encoded return None @@ -165,7 +171,7 @@ def test_cleanup_removes_orphaned_packages(self): # Create random extension directories for i in range(no_of_orphaned_packages): - eh = ExtHandler(name='Random.Extension.ShouldNot.Be.There') + eh = ExtHandler(name='Random.Extension.ShouldNot.Be.There') # pylint: disable=invalid-name eh.properties.version = FlexibleVersion("9.9.0") + i handler = ExtHandlerInstance(eh, "unused") os.mkdir(handler.get_base_dir()) @@ -181,7 +187,7 @@ def test_cleanup_removes_orphaned_packages(self): def test_cleanup_leaves_failed_extensions(self): original_popen = subprocess.Popen - def mock_fail_popen(*args, **kwargs): + def mock_fail_popen(*args, **kwargs): # pylint: disable=unused-argument return original_popen("fail_this_command", **kwargs) with self._setup_test_env(mockwiredata.DATA_FILE_EXT_SINGLE) as (exthandlers_handler, protocol, no_of_exts): @@ -208,7 +214,7 @@ def mock_fail_popen(*args, **kwargs): class TestHandlerStateMigration(AgentTestCase): - def setUp(self): + def setUp(self): # pylint: disable=useless-return AgentTestCase.setUp(self) handler_name = "Not.A.Real.Extension" @@ -226,7 +232,7 @@ def setUp(self): message="Uninteresting message") return - def _prepare_handler_state(self): + def _prepare_handler_state(self): # pylint: disable=useless-return handler_state_path = os.path.join( self.tmp_dir, "handler_state", @@ -240,7 +246,7 @@ def _prepare_handler_state(self): json.dumps(get_properties(self.handler_status))) return - def _prepare_handler_config(self): + def _prepare_handler_config(self): # pylint: disable=useless-return handler_config_path = os.path.join( self.tmp_dir, self.ext_handler_i.get_full_name(), @@ -248,19 +254,19 @@ def _prepare_handler_config(self): os.makedirs(handler_config_path) return - def test_migration_migrates(self): + def test_migration_migrates(self): # pylint: disable=useless-return self._prepare_handler_state() self._prepare_handler_config() migrate_handler_state() - self.assertEquals(self.ext_handler_i.get_handler_state(), self.handler_state) - self.assertEquals( + self.assertEquals(self.ext_handler_i.get_handler_state(), self.handler_state) # pylint: disable=deprecated-method + self.assertEquals( # pylint: disable=deprecated-method self.ext_handler_i.get_handler_status().status, self.handler_status.status) return - def test_migration_skips_if_empty(self): + def test_migration_skips_if_empty(self): # pylint: disable=useless-return self._prepare_handler_config() migrate_handler_state() @@ -271,7 +277,7 @@ def test_migration_skips_if_empty(self): os.path.isfile(os.path.join(self.ext_handler_i.get_conf_dir(), "HandlerStatus"))) return - def test_migration_cleans_up(self): + def test_migration_cleans_up(self): # pylint: disable=useless-return self._prepare_handler_state() self._prepare_handler_config() @@ -280,7 +286,7 @@ def test_migration_cleans_up(self): self.assertFalse(os.path.isdir(os.path.join(conf.get_lib_dir(), "handler_state"))) return - def test_migration_does_not_overwrite(self): + def test_migration_does_not_overwrite(self): # pylint: disable=useless-return self._prepare_handler_state() self._prepare_handler_config() @@ -288,21 +294,21 @@ def test_migration_does_not_overwrite(self): status = "NotReady" code = 1 message = "A message" - self.assertNotEquals(state, self.handler_state) - self.assertNotEquals(status, self.handler_status.status) - self.assertNotEquals(code, self.handler_status.code) - self.assertNotEquals(message, self.handler_status.message) + self.assertNotEquals(state, self.handler_state) # pylint: disable=deprecated-method + self.assertNotEquals(status, self.handler_status.status) # pylint: disable=deprecated-method + self.assertNotEquals(code, self.handler_status.code) # pylint: disable=deprecated-method + self.assertNotEquals(message, self.handler_status.message) # pylint: disable=deprecated-method self.ext_handler_i.set_handler_state(state) self.ext_handler_i.set_handler_status(status=status, code=code, message=message) migrate_handler_state() - self.assertEquals(self.ext_handler_i.get_handler_state(), state) + self.assertEquals(self.ext_handler_i.get_handler_state(), state) # pylint: disable=deprecated-method handler_status = self.ext_handler_i.get_handler_status() - self.assertEquals(handler_status.status, status) - self.assertEquals(handler_status.code, code) - self.assertEquals(handler_status.message, message) + self.assertEquals(handler_status.status, status) # pylint: disable=deprecated-method + self.assertEquals(handler_status.code, code) # pylint: disable=deprecated-method + self.assertEquals(handler_status.message, message) # pylint: disable=deprecated-method return def test_set_handler_status_ignores_none_content(self): @@ -320,29 +326,29 @@ def test_set_handler_status_ignores_none_content(self): try: with patch('json.dumps', return_value=None): self.ext_handler_i.set_handler_status(status=status, code=code, message=message) - except Exception as e: + except Exception as e: # pylint: disable=unused-variable,invalid-name self.fail("set_handler_status threw an exception") @patch("shutil.move", side_effect=Exception) - def test_migration_ignores_move_errors(self, shutil_mock): + def test_migration_ignores_move_errors(self, shutil_mock): # pylint: disable=useless-return,unused-argument self._prepare_handler_state() self._prepare_handler_config() try: migrate_handler_state() - except Exception as e: - self.assertTrue(False, "Unexpected exception: {0}".format(str(e))) + except Exception as e: # pylint: disable=invalid-name + self.assertTrue(False, "Unexpected exception: {0}".format(str(e))) # pylint: disable=redundant-unittest-assert return @patch("shutil.rmtree", side_effect=Exception) - def test_migration_ignores_tree_remove_errors(self, shutil_mock): + def test_migration_ignores_tree_remove_errors(self, shutil_mock): # pylint: disable=useless-return,unused-argument self._prepare_handler_state() self._prepare_handler_config() try: migrate_handler_state() - except Exception as e: - self.assertTrue(False, "Unexpected exception: {0}".format(str(e))) + except Exception as e: # pylint: disable=invalid-name + self.assertTrue(False, "Unexpected exception: {0}".format(str(e))) # pylint: disable=redundant-unittest-assert return @@ -360,6 +366,7 @@ def tearDownClass(cls): CGroupConfigurator.get_instance().disable() +# pylint: disable=too-many-public-methods @patch('time.sleep', side_effect=lambda _: mock_sleep(0.001)) @patch("azurelinuxagent.common.protocol.wire.CryptUtil") @patch("azurelinuxagent.common.utils.restutil.http_get") @@ -367,19 +374,19 @@ class TestExtension(ExtensionTestCase): def setUp(self): AgentTestCase.setUp(self) - def _assert_handler_status(self, report_vm_status, expected_status, + def _assert_handler_status(self, report_vm_status, expected_status, # pylint: disable=useless-return,too-many-arguments expected_ext_count, version, expected_handler_name="OSTCExtensions.ExampleHandlerLinux"): self.assertTrue(report_vm_status.called) - args, kw = report_vm_status.call_args + args, kw = report_vm_status.call_args # pylint: disable=unused-variable,invalid-name vm_status = args[0] - self.assertNotEquals(0, len(vm_status.vmAgent.extensionHandlers)) + self.assertNotEquals(0, len(vm_status.vmAgent.extensionHandlers)) # pylint: disable=deprecated-method handler_status = vm_status.vmAgent.extensionHandlers[0] - self.assertEquals(expected_status, handler_status.status) - self.assertEquals(expected_handler_name, + self.assertEquals(expected_status, handler_status.status) # pylint: disable=deprecated-method + self.assertEquals(expected_handler_name, # pylint: disable=deprecated-method handler_status.name) - self.assertEquals(version, handler_status.version) - self.assertEquals(expected_ext_count, len(handler_status.extensions)) + self.assertEquals(version, handler_status.version) # pylint: disable=deprecated-method + self.assertEquals(expected_ext_count, len(handler_status.extensions)) # pylint: disable=deprecated-method return def _assert_ext_pkg_file_status(self, expected_to_be_present=True, extension_version="1.0.0", @@ -390,14 +397,14 @@ def _assert_ext_pkg_file_status(self, expected_to_be_present=True, extension_ver else: self.assertNotIn(zip_file_format.format(extension_handler_name, extension_version), os.listdir(conf.get_lib_dir())) - def _assert_no_handler_status(self, report_vm_status): + def _assert_no_handler_status(self, report_vm_status): # pylint: disable=useless-return self.assertTrue(report_vm_status.called) - args, kw = report_vm_status.call_args + args, kw = report_vm_status.call_args # pylint: disable=unused-variable,invalid-name vm_status = args[0] - self.assertEquals(0, len(vm_status.vmAgent.extensionHandlers)) + self.assertEquals(0, len(vm_status.vmAgent.extensionHandlers)) # pylint: disable=deprecated-method return - def _create_mock(self, test_data, mock_http_get, MockCryptUtil, *args): + def _create_mock(self, test_data, mock_http_get, MockCryptUtil, *args): # pylint: disable=unused-argument,invalid-name # Mock protocol to return test data mock_http_get.side_effect = test_data.mock_http_get MockCryptUtil.side_effect = test_data.mock_crypt_util @@ -420,7 +427,7 @@ def _set_up_update_test_and_update_gs(self, patch_command, *args): :return: test_data, exthandlers_handler, protocol """ test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE_EXT_SINGLE) - exthandlers_handler, protocol = self._create_mock(test_data, *args) + exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=no-value-for-parameter # Ensure initial install and enable is successful exthandlers_handler.run() @@ -448,7 +455,7 @@ def _create_extension_handlers_handler(protocol): def test_ext_handler(self, *args): # Test enable scenario. test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE) - exthandlers_handler, protocol = self._create_mock(test_data, *args) + exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=no-value-for-parameter exthandlers_handler.run() @@ -526,7 +533,7 @@ def _assert_handler_status_and_manifest_download_count(protocol, test_data, mani "We should have downloaded extension manifest {0} times".format(manifest_count)) test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE) - exthandlers_handler, protocol = self._create_mock(test_data, *args) + exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=no-value-for-parameter exthandlers_handler.run() _assert_handler_status_and_manifest_download_count(protocol, test_data, 1) @@ -550,7 +557,7 @@ def _assert_handler_status_and_manifest_download_count(protocol, test_data, mani def test_ext_zip_file_packages_removed_in_update_case(self, *args): # Test enable scenario. test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE) - exthandlers_handler, protocol = self._create_mock(test_data, *args) + exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=no-value-for-parameter exthandlers_handler.run() @@ -587,7 +594,7 @@ def test_ext_zip_file_packages_removed_in_update_case(self, *args): def test_ext_zip_file_packages_removed_in_uninstall_case(self, *args): # Test enable scenario. test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE) - exthandlers_handler, protocol = self._create_mock(test_data, *args) + exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=no-value-for-parameter extension_version = "1.0.0" exthandlers_handler.run() @@ -609,7 +616,7 @@ def test_ext_zip_file_packages_removed_in_uninstall_case(self, *args): def test_ext_zip_file_packages_removed_in_update_and_uninstall_case(self, *args): # Test enable scenario. test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE) - exthandlers_handler, protocol = self._create_mock(test_data, *args) + exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=no-value-for-parameter exthandlers_handler.run() @@ -655,21 +662,21 @@ def test_ext_zip_file_packages_removed_in_update_and_uninstall_case(self, *args) def test_ext_handler_no_settings(self, *args): test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE_EXT_NO_SETTINGS) - exthandlers_handler, protocol = self._create_mock(test_data, *args) + exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=no-value-for-parameter exthandlers_handler.run() self._assert_handler_status(protocol.report_vm_status, "Ready", 0, "1.0.0") def test_ext_handler_no_public_settings(self, *args): test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE_EXT_NO_PUBLIC) - exthandlers_handler, protocol = self._create_mock(test_data, *args) + exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=no-value-for-parameter exthandlers_handler.run() self._assert_handler_status(protocol.report_vm_status, "Ready", 1, "1.0.0") def test_ext_handler_no_ext(self, *args): test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE_NO_EXT) - exthandlers_handler, protocol = self._create_mock(test_data, *args) + exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=no-value-for-parameter # Assert no extension handler status exthandlers_handler.run() @@ -678,7 +685,7 @@ def test_ext_handler_no_ext(self, *args): def test_ext_handler_sequencing(self, *args): # Test enable scenario. test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE_EXT_SEQUENCING) - exthandlers_handler, protocol = self._create_mock(test_data, *args) + exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=no-value-for-parameter exthandlers_handler.run() @@ -752,7 +759,7 @@ def test_ext_handler_sequencing(self, *args): def test_ext_handler_sequencing_default_dependency_level(self, *args): test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE) - exthandlers_handler, protocol = self._create_mock(test_data, *args) + exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=unused-variable,no-value-for-parameter exthandlers_handler.run() self.assertEqual(exthandlers_handler.ext_handlers.extHandlers[0].properties.extensions[0].dependencyLevel, 0) self.assertEqual(exthandlers_handler.ext_handlers.extHandlers[0].properties.extensions[0].dependencyLevel, 0) @@ -765,7 +772,7 @@ def test_ext_handler_sequencing_invalid_dependency_level(self, *args): "dependencyLevel=\"a6\"") test_data.ext_conf = test_data.ext_conf.replace("dependencyLevel=\"2\"", "dependencyLevel=\"5b\"") - exthandlers_handler, protocol = self._create_mock(test_data, *args) + exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=unused-variable,no-value-for-parameter exthandlers_handler.run() @@ -819,18 +826,18 @@ def test_ext_handler_reporting_status_file(self, *args): expected_status_json = json.loads(expected_status) test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE_MULTIPLE_EXT) - exthandlers_handler, protocol = self._create_mock(test_data, *args) + exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=unused-variable,no-value-for-parameter exthandlers_handler.run() status_path = os.path.join(conf.get_lib_dir(), AGENT_STATUS_FILE) actual_status_json = json.loads(fileutil.read_file(status_path)) - self.assertEquals(expected_status_json, actual_status_json) + self.assertEquals(expected_status_json, actual_status_json) # pylint: disable=deprecated-method def test_ext_handler_rollingupgrade(self, *args): # Test enable scenario. test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE_EXT_ROLLINGUPGRADE) - exthandlers_handler, protocol = self._create_mock(test_data, *args) + exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=no-value-for-parameter exthandlers_handler.run() @@ -924,15 +931,15 @@ def test_ext_handler_rollingupgrade(self, *args): @patch('azurelinuxagent.ga.exthandlers.add_event') def test_ext_handler_download_failure_transient(self, mock_add_event, *args): - original_sleep = time.sleep + original_sleep = time.sleep # pylint: disable=unused-variable test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE) - exthandlers_handler, protocol = self._create_mock(test_data, *args) + exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=no-value-for-parameter protocol.download_ext_handler_pkg = Mock(side_effect=ProtocolError) exthandlers_handler.run() - self.assertEquals(0, mock_add_event.call_count) + self.assertEquals(0, mock_add_event.call_count) # pylint: disable=deprecated-method def test_it_should_create_extension_events_dir_and_set_handler_environment_only_if_extension_telemetry_enabled(self, *args): @@ -944,7 +951,7 @@ def test_it_should_create_extension_events_dir_and_set_handler_environment_only_ # Create new object for each run to force re-installation of extensions as we # only create handler_environment on installation test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE_MULTIPLE_EXT) - exthandlers_handler, protocol = self._create_mock(test_data, *args) + exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=no-value-for-parameter exthandlers_handler.run() self._assert_handler_status(protocol.report_vm_status, "Ready", 1, "1.0.0") @@ -973,7 +980,7 @@ def test_it_should_create_extension_events_dir_and_set_handler_environment_only_ def test_it_should_not_delete_extension_events_directory_on_extension_uninstall(self, *args): test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE) - exthandlers_handler, protocol = self._create_mock(test_data, *args) + exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=no-value-for-parameter with patch('azurelinuxagent.ga.exthandlers.is_extension_telemetry_pipeline_enabled', return_value=True): exthandlers_handler.run() @@ -992,7 +999,7 @@ def test_it_should_not_delete_extension_events_directory_on_extension_uninstall( def test_it_should_uninstall_unregistered_extensions_properly(self, *args): test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE) - exthandlers_handler, protocol = self._create_mock(test_data, *args) + exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=no-value-for-parameter exthandlers_handler.run() self._assert_handler_status(protocol.report_vm_status, "Ready", 1, "1.0.0") @@ -1014,44 +1021,44 @@ def test_it_should_uninstall_unregistered_extensions_properly(self, *args): @patch('azurelinuxagent.ga.exthandlers.add_event') def test_ext_handler_report_status_permanent(self, mock_add_event, mock_error_state, *args): test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE) - exthandlers_handler, protocol = self._create_mock(test_data, *args) + exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=no-value-for-parameter protocol.report_vm_status = Mock(side_effect=ProtocolError) mock_error_state.return_value = True exthandlers_handler.run() - self.assertEquals(5, mock_add_event.call_count) - args, kw = mock_add_event.call_args - self.assertEquals(False, kw['is_success']) + self.assertEquals(5, mock_add_event.call_count) # pylint: disable=deprecated-method + args, kw = mock_add_event.call_args # pylint: disable=invalid-name + self.assertEquals(False, kw['is_success']) # pylint: disable=deprecated-method self.assertTrue("Failed to report vm agent status" in kw['message']) - self.assertEquals("ReportStatusExtended", kw['op']) + self.assertEquals("ReportStatusExtended", kw['op']) # pylint: disable=deprecated-method @patch('azurelinuxagent.ga.exthandlers.add_event') def test_ext_handler_report_status_resource_gone(self, mock_add_event, *args): test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE) - exthandlers_handler, protocol = self._create_mock(test_data, *args) + exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=no-value-for-parameter protocol.report_vm_status = Mock(side_effect=ResourceGoneError) exthandlers_handler.run() - self.assertEquals(4, mock_add_event.call_count) - args, kw = mock_add_event.call_args - self.assertEquals(False, kw['is_success']) + self.assertEquals(4, mock_add_event.call_count) # pylint: disable=deprecated-method + args, kw = mock_add_event.call_args # pylint: disable=invalid-name + self.assertEquals(False, kw['is_success']) # pylint: disable=deprecated-method self.assertTrue("ResourceGoneError" in kw['message']) - self.assertEquals("ExtensionProcessing", kw['op']) + self.assertEquals("ExtensionProcessing", kw['op']) # pylint: disable=deprecated-method @patch('azurelinuxagent.common.errorstate.ErrorState.is_triggered') @patch('azurelinuxagent.ga.exthandlers.ExtHandlerInstance.report_event') - def test_ext_handler_download_failure_permanent_ProtocolError(self, mock_add_event, mock_error_state, *args): + def test_ext_handler_download_failure_permanent_ProtocolError(self, mock_add_event, mock_error_state, *args): # pylint: disable=invalid-name test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE) - exthandlers_handler, protocol = self._create_mock(test_data, *args) + exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=no-value-for-parameter protocol.get_ext_handler_pkgs = Mock(side_effect=ProtocolError) mock_error_state.return_value = True exthandlers_handler.run() - self.assertEquals(1, mock_add_event.call_count) - args, kw = mock_add_event.call_args_list[0] - self.assertEquals(False, kw['is_success']) + self.assertEquals(1, mock_add_event.call_count) # pylint: disable=deprecated-method + args, kw = mock_add_event.call_args_list[0] # pylint: disable=invalid-name + self.assertEquals(False, kw['is_success']) # pylint: disable=deprecated-method self.assertTrue("Failed to get ext handler pkgs" in kw['message']) self.assertTrue("ProtocolError" in kw['message']) @@ -1061,13 +1068,13 @@ def test_ext_handler_download_errors_should_be_reported_only_on_new_goal_state(s def _assert_mock_add_event_call(expected_download_failed_event_count, err_msg_guid): event_occurrences = [kw for _, kw in mock_add_event.call_args_list if "Failed to download artifacts: [ExtensionDownloadError] {0}".format(err_msg_guid) in kw['message']] - self.assertEquals(expected_download_failed_event_count, len(event_occurrences), "Call count do not match") + self.assertEquals(expected_download_failed_event_count, len(event_occurrences), "Call count do not match") # pylint: disable=deprecated-method self.assertFalse(any([kw['is_success'] for kw in event_occurrences]), "The events should have failed") self.assertEqual(expected_download_failed_event_count, len([kw['op'] for kw in event_occurrences]), "Incorrect Operation, all events should be a download errors") test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE) - exthandlers_handler, protocol = self._create_mock(test_data, *args) + exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=no-value-for-parameter unique_error_message_guid = str(uuid.uuid4()) protocol.get_ext_handler_pkgs = Mock(side_effect=ExtensionDownloadError(unique_error_message_guid)) @@ -1091,56 +1098,56 @@ def _assert_mock_add_event_call(expected_download_failed_event_count, err_msg_gu @patch('azurelinuxagent.ga.exthandlers.fileutil') def test_ext_handler_io_error(self, mock_fileutil, *args): test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE) - exthandlers_handler, protocol = self._create_mock(test_data, *args) + exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=unused-variable,no-value-for-parameter mock_fileutil.write_file.return_value = IOError("Mock IO Error") exthandlers_handler.run() - def test_extension_processing_allowed(self, *args): + def test_extension_processing_allowed(self, *args): # pylint: disable=unused-argument exthandlers_handler = get_exthandlers_handler(Mock()) # disable extension handling in configuration with patch.object(conf, 'get_extensions_enabled', return_value=False): - self.assertFalse(exthandlers_handler._extension_processing_allowed()) + self.assertFalse(exthandlers_handler._extension_processing_allowed()) # pylint: disable=protected-access # enable extension handling in configuration with patch.object(conf, "get_extensions_enabled", return_value=True): # disable overprovisioning in configuration with patch.object(conf, 'get_enable_overprovisioning', return_value=False): - self.assertTrue(exthandlers_handler._extension_processing_allowed()) + self.assertTrue(exthandlers_handler._extension_processing_allowed()) # pylint: disable=protected-access # enable overprovisioning in configuration with patch.object(conf, "get_enable_overprovisioning", return_value=True): with patch.object(exthandlers_handler.protocol.get_artifacts_profile(), "is_on_hold", side_effect=[True, False]): # Enable on_hold property in artifact_blob - self.assertFalse(exthandlers_handler._extension_processing_allowed()) + self.assertFalse(exthandlers_handler._extension_processing_allowed()) # pylint: disable=protected-access # Disable on_hold property in artifact_blob - self.assertTrue(exthandlers_handler._extension_processing_allowed()) + self.assertTrue(exthandlers_handler._extension_processing_allowed()) # pylint: disable=protected-access def test_handle_ext_handlers_on_hold_true(self, *args): test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE) - exthandlers_handler, protocol = self._create_mock(test_data, *args) + exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=no-value-for-parameter exthandlers_handler.ext_handlers, exthandlers_handler.last_etag = protocol.get_ext_handlers() protocol.get_artifacts_profile = MagicMock() exthandlers_handler.protocol = protocol # Disable extension handling blocking - exthandlers_handler._extension_processing_allowed = Mock(return_value=False) + exthandlers_handler._extension_processing_allowed = Mock(return_value=False) # pylint: disable=protected-access with patch.object(ExtHandlersHandler, 'handle_ext_handlers') as patch_handle_ext_handlers: exthandlers_handler.run() self.assertEqual(0, patch_handle_ext_handlers.call_count) # enable extension handling blocking - exthandlers_handler._extension_processing_allowed = Mock(return_value=True) + exthandlers_handler._extension_processing_allowed = Mock(return_value=True) # pylint: disable=protected-access with patch.object(ExtHandlersHandler, 'handle_ext_handlers') as patch_handle_ext_handlers: exthandlers_handler.run() self.assertEqual(1, patch_handle_ext_handlers.call_count) def test_handle_ext_handlers_on_hold_false(self, *args): test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE) - exthandlers_handler, protocol = self._create_mock(test_data, *args) + exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=no-value-for-parameter exthandlers_handler.ext_handlers, exthandlers_handler.last_etag = protocol.get_ext_handlers() exthandlers_handler.protocol = protocol @@ -1148,7 +1155,7 @@ def test_handle_ext_handlers_on_hold_false(self, *args): conf.get_enable_overprovisioning = Mock(return_value=True) # Test when is_on_hold returns False - from azurelinuxagent.common.protocol.wire import InVMArtifactsProfile + from azurelinuxagent.common.protocol.wire import InVMArtifactsProfile # pylint: disable=redefined-outer-name,reimported mock_in_vm_artifacts_profile = InVMArtifactsProfile(MagicMock()) mock_in_vm_artifacts_profile.is_on_hold = Mock(return_value=False) protocol.get_artifacts_profile = Mock(return_value=mock_in_vm_artifacts_profile) @@ -1164,7 +1171,7 @@ def test_handle_ext_handlers_on_hold_false(self, *args): def test_last_etag_on_extension_processing(self, *args): test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE) - exthandlers_handler, protocol = self._create_mock(test_data, *args) + exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=no-value-for-parameter exthandlers_handler.ext_handlers, etag = protocol.get_ext_handlers() exthandlers_handler.protocol = protocol @@ -1184,14 +1191,14 @@ def test_last_etag_on_extension_processing(self, *args): def _assert_ext_status(self, report_ext_status, expected_status, expected_seq_no): self.assertTrue(report_ext_status.called) - args, kw = report_ext_status.call_args + args, kw = report_ext_status.call_args # pylint: disable=unused-variable,invalid-name ext_status = args[-1] - self.assertEquals(expected_status, ext_status.status) - self.assertEquals(expected_seq_no, ext_status.sequenceNumber) + self.assertEquals(expected_status, ext_status.status) # pylint: disable=deprecated-method + self.assertEquals(expected_seq_no, ext_status.sequenceNumber) # pylint: disable=deprecated-method def test_ext_handler_no_reporting_status(self, *args): test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE) - exthandlers_handler, protocol = self._create_mock(test_data, *args) + exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=no-value-for-parameter exthandlers_handler.run() self._assert_handler_status(protocol.report_vm_status, "Ready", 1, "1.0.0") @@ -1212,7 +1219,7 @@ def test_wait_for_handler_successful_completion_empty_exts(self, *args): Expected to return True. ''' test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE) - exthandlers_handler, protocol = self._create_mock(test_data, *args) + exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=unused-variable,no-value-for-parameter handler = ExtHandler(name="handler") @@ -1240,7 +1247,7 @@ def test_wait_for_handler_successful_completion_no_status(self, *args): Expected to return False. ''' test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE) - exthandlers_handler, protocol = self._create_mock(test_data, *args) + exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=unused-variable,no-value-for-parameter ExtHandlerInstance.get_ext_handling_status = MagicMock(return_value=None) self.assertFalse(self._helper_wait_for_handler_successful_completion(exthandlers_handler)) @@ -1251,7 +1258,7 @@ def test_wait_for_handler_successful_completion_success_status(self, *args): Expected to return True. ''' test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE) - exthandlers_handler, protocol = self._create_mock(test_data, *args) + exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=unused-variable,no-value-for-parameter status = "success" @@ -1264,7 +1271,7 @@ def test_wait_for_handler_successful_completion_error_status(self, *args): Expected to return False. ''' test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE) - exthandlers_handler, protocol = self._create_mock(test_data, *args) + exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=unused-variable,no-value-for-parameter status = "error" @@ -1277,7 +1284,7 @@ def test_wait_for_handler_successful_completion_timeout(self, *args): Expected to return False. ''' test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE) - exthandlers_handler, protocol = self._create_mock(test_data, *args) + exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=unused-variable,no-value-for-parameter # Choose a non-terminal status status = "warning" @@ -1291,7 +1298,7 @@ def test_get_ext_handling_status(self, *args): verifying against the expected values ''' test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE) - exthandlers_handler, protocol = self._create_mock(test_data, *args) + exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=unused-variable,no-value-for-parameter handler_name = "Handler" exthandler = ExtHandler(name=handler_name) @@ -1334,7 +1341,7 @@ def test_is_ext_handling_complete(self, *args): verifying against the expected output values. """ test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE) - exthandlers_handler, protocol = self._create_mock(test_data, *args) + exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=unused-variable,no-value-for-parameter handler_name = "Handler" exthandler = ExtHandler(name=handler_name) @@ -1382,7 +1389,7 @@ def test_ext_handler_version_decide_autoupgrade_internalversion(self, *args): else: datafile = mockwiredata.DATA_FILE - _, protocol = self._create_mock(mockwiredata.WireProtocolData(datafile), *args) + _, protocol = self._create_mock(mockwiredata.WireProtocolData(datafile), *args) # pylint: disable=no-value-for-parameter ext_handlers, _ = protocol.get_ext_handlers() self.assertEqual(1, len(ext_handlers.extHandlers)) ext_handler = ext_handlers.extHandlers[0] @@ -1415,7 +1422,7 @@ def test_ext_handler_version_decide_between_minor_versions(self, *args): (None, '4.1', '4.1.0.0'), ] - _, protocol = self._create_mock(mockwiredata.WireProtocolData(mockwiredata.DATA_FILE), *args) + _, protocol = self._create_mock(mockwiredata.WireProtocolData(mockwiredata.DATA_FILE), *args) # pylint: disable=no-value-for-parameter version_uri = Mock() version_uri.uri = 'http://mock-goal-state/Microsoft.OSTCExtensions_ExampleHandlerLinux_asiaeast_manifest.xml' @@ -1436,20 +1443,20 @@ def test_ext_handler_version_decide_between_minor_versions(self, *args): def test_extensions_disabled(self, _, *args): # test status is reported for no extensions test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE_NO_EXT) - exthandlers_handler, protocol = self._create_mock(test_data, *args) + exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=no-value-for-parameter exthandlers_handler.run() self._assert_no_handler_status(protocol.report_vm_status) # test status is reported, but extensions are not processed test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE) - exthandlers_handler, protocol = self._create_mock(test_data, *args) + exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=no-value-for-parameter exthandlers_handler.run() self._assert_no_handler_status(protocol.report_vm_status) def test_extensions_deleted(self, *args): # Ensure initial enable is successful test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE_EXT_DELETION) - exthandlers_handler, protocol = self._create_mock(test_data, *args) + exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=no-value-for-parameter exthandlers_handler.run() @@ -1476,7 +1483,7 @@ def test_install_failure(self, patch_get_install_command, patch_install, *args): When extension install fails, the operation should not be retried. """ test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE_EXT_SINGLE) - exthandlers_handler, protocol = self._create_mock(test_data, *args) + exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=no-value-for-parameter # Ensure initial install is unsuccessful patch_get_install_command.return_value = "exit.sh 1" @@ -1499,7 +1506,7 @@ def test_install_failure_check_exception_handling(self, patch_get_install_comman When extension install fails, the operation should be reported to our telemetry service. """ test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE_EXT_SINGLE) - exthandlers_handler, protocol = self._create_mock(test_data, *args) + exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=no-value-for-parameter # Ensure install is unsuccessful patch_get_install_command.return_value = "exit.sh 1" @@ -1514,7 +1521,7 @@ def test_enable_failure(self, patch_get_enable_command, *args): When extension enable fails, the operation should not be retried. """ test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE_EXT_SINGLE) - exthandlers_handler, protocol = self._create_mock(test_data, *args) + exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=no-value-for-parameter # Ensure initial install is successful, but enable fails patch_get_enable_command.call_count = 0 @@ -1537,7 +1544,7 @@ def test_enable_failure_check_exception_handling(self, patch_get_enable_command, When extension enable fails, the operation should be reported. """ test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE_EXT_SINGLE) - exthandlers_handler, protocol = self._create_mock(test_data, *args) + exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=no-value-for-parameter # Ensure initial install is successful, but enable fails patch_get_enable_command.call_count = 0 @@ -1555,7 +1562,7 @@ def test_disable_failure(self, patch_get_disable_command, *args): """ # Ensure initial install and enable is successful, but disable fails test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE_EXT_SINGLE) - exthandlers_handler, protocol = self._create_mock(test_data, *args) + exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=no-value-for-parameter patch_get_disable_command.call_count = 0 patch_get_disable_command.return_value = "exit.sh 1" @@ -1593,7 +1600,7 @@ def test_disable_failure_with_exception_handling(self, patch_get_disable_command """ # Ensure initial install and enable is successful, but disable fails test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE_EXT_SINGLE) - exthandlers_handler, protocol = self._create_mock(test_data, *args) + exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=no-value-for-parameter patch_get_disable_command.call_count = 0 patch_get_disable_command.return_value = "exit 1" @@ -1622,7 +1629,7 @@ def test_uninstall_failure(self, patch_get_uninstall_command, *args): """ # Ensure initial install and enable is successful, but uninstall fails test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE_EXT_SINGLE) - exthandlers_handler, protocol = self._create_mock(test_data, *args) + exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=no-value-for-parameter patch_get_uninstall_command.call_count = 0 patch_get_uninstall_command.return_value = "exit 1" @@ -1642,7 +1649,7 @@ def test_uninstall_failure(self, patch_get_uninstall_command, *args): self.assertEqual(1, patch_get_uninstall_command.call_count) self.assertEqual(2, protocol.report_vm_status.call_count) - self.assertEquals("Ready", protocol.report_vm_status.call_args[0][0].vmAgent.status) + self.assertEquals("Ready", protocol.report_vm_status.call_args[0][0].vmAgent.status) # pylint: disable=deprecated-method self._assert_no_handler_status(protocol.report_vm_status) # Ensure there are no further retries @@ -1650,7 +1657,7 @@ def test_uninstall_failure(self, patch_get_uninstall_command, *args): self.assertEqual(1, patch_get_uninstall_command.call_count) self.assertEqual(3, protocol.report_vm_status.call_count) - self.assertEquals("Ready", protocol.report_vm_status.call_args[0][0].vmAgent.status) + self.assertEquals("Ready", protocol.report_vm_status.call_args[0][0].vmAgent.status) # pylint: disable=deprecated-method self._assert_no_handler_status(protocol.report_vm_status) @patch('azurelinuxagent.ga.exthandlers.HandlerManifest.get_update_command') @@ -1680,23 +1687,23 @@ def mock_popen(*args, **kwargs): enable_command_count = len([extension_call for extension_call in extension_calls if "-enable" in extension_call]) - self.assertEquals(1, update_command_count) - self.assertEquals(0, enable_command_count) + self.assertEquals(1, update_command_count) # pylint: disable=deprecated-method + self.assertEquals(0, enable_command_count) # pylint: disable=deprecated-method # We report the failure of the new extension version self._assert_handler_status(protocol.report_vm_status, "NotReady", expected_ext_count=1, version="1.0.1") # Ensure we are processing the same goal state only once loop_run = 5 - for x in range(loop_run): + for x in range(loop_run): # pylint: disable=unused-variable,invalid-name exthandlers_handler.run() update_command_count = len([extension_call for extension_call in extension_calls if patch_get_update_command.return_value in extension_call]) enable_command_count = len([extension_call for extension_call in extension_calls if "-enable" in extension_call]) - self.assertEquals(1, update_command_count) - self.assertEquals(0, enable_command_count) + self.assertEquals(1, update_command_count) # pylint: disable=deprecated-method + self.assertEquals(0, enable_command_count) # pylint: disable=deprecated-method # If the incarnation number changes (there's a new goal state), ensure we go through the entire upgrade # process again. @@ -1709,15 +1716,15 @@ def mock_popen(*args, **kwargs): if patch_get_update_command.return_value in extension_call]) enable_command_count = len([extension_call for extension_call in extension_calls if "-enable" in extension_call]) - self.assertEquals(2, update_command_count) - self.assertEquals(0, enable_command_count) + self.assertEquals(2, update_command_count) # pylint: disable=deprecated-method + self.assertEquals(0, enable_command_count) # pylint: disable=deprecated-method # We report the failure of the new extension version self._assert_handler_status(protocol.report_vm_status, "NotReady", expected_ext_count=1, version="1.0.1") @patch('azurelinuxagent.ga.exthandlers.HandlerManifest.get_disable_command') def test_extension_upgrade_failure_when_prev_version_disable_fails(self, patch_get_disable_command, *args): - test_data, exthandlers_handler, protocol = self._set_up_update_test_and_update_gs(patch_get_disable_command, *args) + test_data, exthandlers_handler, protocol = self._set_up_update_test_and_update_gs(patch_get_disable_command, *args) # pylint: disable=unused-variable with patch('azurelinuxagent.ga.exthandlers.HandlerManifest.get_enable_command') as patch_get_enable_command: exthandlers_handler.run() @@ -1730,7 +1737,7 @@ def test_extension_upgrade_failure_when_prev_version_disable_fails(self, patch_g # Ensure we are processing the same goal state only once loop_run = 5 - for x in range(loop_run): + for x in range(loop_run): # pylint: disable=unused-variable,invalid-name exthandlers_handler.run() self.assertEqual(1, patch_get_disable_command.call_count) @@ -1753,7 +1760,7 @@ def test_extension_upgrade_failure_when_prev_version_disable_fails_and_recovers_ # Ensure we are processing the same goal state only once loop_run = 5 - for x in range(loop_run): + for x in range(loop_run): # pylint: disable=unused-variable,invalid-name exthandlers_handler.run() self.assertEqual(1, patch_get_disable_command.call_count) @@ -1764,7 +1771,7 @@ def test_extension_upgrade_failure_when_prev_version_disable_fails_and_recovers_ protocol.update_goal_state() # Ensure disable won't fail by making launch_command a no-op - with patch('azurelinuxagent.ga.exthandlers.ExtHandlerInstance.launch_command') as patch_launch_command: + with patch('azurelinuxagent.ga.exthandlers.ExtHandlerInstance.launch_command') as patch_launch_command: # pylint: disable=unused-variable exthandlers_handler.run() self.assertEqual(2, patch_get_disable_command.call_count) self.assertEqual(1, patch_get_enable_command.call_count) @@ -1773,7 +1780,7 @@ def test_extension_upgrade_failure_when_prev_version_disable_fails_and_recovers_ @patch('azurelinuxagent.ga.exthandlers.HandlerManifest.get_disable_command') def test_extension_upgrade_failure_when_prev_version_disable_fails_incorrect_zip(self, patch_get_disable_command, *args): - test_data, exthandlers_handler, protocol = self._set_up_update_test_and_update_gs(patch_get_disable_command, + test_data, exthandlers_handler, protocol = self._set_up_update_test_and_update_gs(patch_get_disable_command, # pylint: disable=unused-variable *args) # The download logic has retry logic that sleeps before each try - make sleep a no-op. @@ -1792,7 +1799,7 @@ def test_extension_upgrade_failure_when_prev_version_disable_fails_incorrect_zip # Ensure we are processing the same goal state only once loop_run = 5 - for x in range(loop_run): + for x in range(loop_run): # pylint: disable=unused-variable,invalid-name exthandlers_handler.run() self.assertEqual(0, patch_get_disable_command.call_count) @@ -1801,7 +1808,7 @@ def test_extension_upgrade_failure_when_prev_version_disable_fails_incorrect_zip @patch('azurelinuxagent.ga.exthandlers.HandlerManifest.get_disable_command') def test_old_handler_reports_failure_on_disable_fail_on_update(self, patch_get_disable_command, *args): old_version, new_version = "1.0.0", "1.0.1" - test_data, exthandlers_handler, protocol = self._set_up_update_test_and_update_gs(patch_get_disable_command, + test_data, exthandlers_handler, protocol = self._set_up_update_test_and_update_gs(patch_get_disable_command, # pylint: disable=unused-variable *args) with patch.object(ExtHandlerInstance, "report_event", autospec=True) as patch_report_event: @@ -1836,7 +1843,7 @@ def test_upgrade_failure_with_exception_handling(self, patch_get_update_command, """ Extension upgrade failure should not be retried """ - test_data, exthandlers_handler, protocol = self._set_up_update_test_and_update_gs(patch_get_update_command, + test_data, exthandlers_handler, protocol = self._set_up_update_test_and_update_gs(patch_get_update_command, # pylint: disable=unused-variable *args) exthandlers_handler.run() @@ -1846,7 +1853,7 @@ def test_upgrade_failure_with_exception_handling(self, patch_get_update_command, @patch('azurelinuxagent.ga.exthandlers.HandlerManifest.get_disable_command') def test_extension_upgrade_should_pass_when_continue_on_update_failure_is_true_and_prev_version_disable_fails( self, patch_get_disable_command, *args): - test_data, exthandlers_handler, protocol = self._set_up_update_test_and_update_gs(patch_get_disable_command, + test_data, exthandlers_handler, protocol = self._set_up_update_test_and_update_gs(patch_get_disable_command, # pylint: disable=unused-variable *args) with patch('azurelinuxagent.ga.exthandlers.HandlerManifest.is_continue_on_update_failure', return_value=True) \ @@ -1864,7 +1871,7 @@ def test_extension_upgrade_should_pass_when_continue_on_update_failure_is_true_a @patch('azurelinuxagent.ga.exthandlers.HandlerManifest.get_uninstall_command') def test_extension_upgrade_should_pass_when_continue_on_update_failue_is_true_and_prev_version_uninstall_fails( self, patch_get_uninstall_command, *args): - test_data, exthandlers_handler, protocol = self._set_up_update_test_and_update_gs(patch_get_uninstall_command, + test_data, exthandlers_handler, protocol = self._set_up_update_test_and_update_gs(patch_get_uninstall_command, # pylint: disable=unused-variable *args) with patch('azurelinuxagent.ga.exthandlers.HandlerManifest.is_continue_on_update_failure', return_value=True) \ @@ -1882,7 +1889,7 @@ def test_extension_upgrade_should_pass_when_continue_on_update_failue_is_true_an @patch('azurelinuxagent.ga.exthandlers.HandlerManifest.get_disable_command') def test_extension_upgrade_should_fail_when_continue_on_update_failure_is_false_and_prev_version_disable_fails( self, patch_get_disable_command, *args): - test_data, exthandlers_handler, protocol = self._set_up_update_test_and_update_gs(patch_get_disable_command, + test_data, exthandlers_handler, protocol = self._set_up_update_test_and_update_gs(patch_get_disable_command, # pylint: disable=unused-variable *args) with patch('azurelinuxagent.ga.exthandlers.HandlerManifest.is_continue_on_update_failure', return_value=False) \ @@ -1899,7 +1906,7 @@ def test_extension_upgrade_should_fail_when_continue_on_update_failure_is_false_ @patch('azurelinuxagent.ga.exthandlers.HandlerManifest.get_uninstall_command') def test_extension_upgrade_should_fail_when_continue_on_update_failure_is_false_and_prev_version_uninstall_fails( self, patch_get_uninstall_command, *args): - test_data, exthandlers_handler, protocol = self._set_up_update_test_and_update_gs(patch_get_uninstall_command, + test_data, exthandlers_handler, protocol = self._set_up_update_test_and_update_gs(patch_get_uninstall_command, # pylint: disable=unused-variable *args) with patch('azurelinuxagent.ga.exthandlers.HandlerManifest.is_continue_on_update_failure', return_value=False) \ @@ -1916,7 +1923,7 @@ def test_extension_upgrade_should_fail_when_continue_on_update_failure_is_false_ @patch('azurelinuxagent.ga.exthandlers.HandlerManifest.get_disable_command') def test_extension_upgrade_should_fail_when_continue_on_update_failure_is_true_and_old_disable_and_new_enable_fails( self, patch_get_disable_command, *args): - test_data, exthandlers_handler, protocol = self._set_up_update_test_and_update_gs(patch_get_disable_command, + test_data, exthandlers_handler, protocol = self._set_up_update_test_and_update_gs(patch_get_disable_command, # pylint: disable=unused-variable *args) with patch('azurelinuxagent.ga.exthandlers.HandlerManifest.is_continue_on_update_failure', return_value=True) \ @@ -1985,7 +1992,7 @@ def test_uninstall_rc_env_var_should_report_not_run_for_non_update_calls_to_exth def test_ext_path_and_version_env_variables_set_for_ever_operation(self, *args): test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE_EXT_SINGLE) - exthandlers_handler, protocol = self._create_mock(test_data, *args) + exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=no-value-for-parameter with patch.object(CGroupConfigurator.get_instance(), "start_extension_command") as patch_start_cmd: exthandlers_handler.run() @@ -2003,7 +2010,7 @@ def test_ext_path_and_version_env_variables_set_for_ever_operation(self, *args): @patch("azurelinuxagent.common.cgroupconfigurator.handle_process_completion", side_effect="Process Successful") def test_ext_sequence_no_should_be_set_for_every_command_call(self, _, *args): test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE_MULTIPLE_EXT) - exthandlers_handler, protocol = self._create_mock(test_data, *args) + exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=no-value-for-parameter with patch("subprocess.Popen") as patch_popen: exthandlers_handler.run() @@ -2019,7 +2026,7 @@ def test_ext_sequence_no_should_be_set_for_every_command_call(self, _, *args): test_data.ext_conf = test_data.ext_conf.replace('version="1.0.0"', 'version="1.0.1"') test_data.ext_conf = test_data.ext_conf.replace('seqNo="0"', 'seqNo="1"') test_data.manifest = test_data.manifest.replace('1.0.0', '1.0.1') - exthandlers_handler, protocol = self._create_mock(test_data, *args) + exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=no-value-for-parameter with patch("subprocess.Popen") as patch_popen: exthandlers_handler.run() @@ -2054,7 +2061,7 @@ def test_ext_sequence_no_should_be_set_from_within_extension(self, *args): self.create_script(test_file_name, test_file, base_dir) test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE_EXT_SINGLE) - exthandlers_handler, protocol = self._create_mock(test_data, *args) + exthandlers_handler, protocol = self._create_mock(test_data, *args) # pylint: disable=unused-variable,no-value-for-parameter expected_seq_no = 0 with patch.object(ExtHandlerInstance, "load_manifest", return_value=manifest): @@ -2087,7 +2094,7 @@ def test_ext_sequence_no_should_be_set_from_within_extension(self, *args): self.assertIn("{0}={1}".format(ExtCommandEnvVariable.ExtensionSeqNumber, expected_seq_no), kwargs['message']) - def test_correct_exit_code_should_be_set_on_uninstall_cmd_failure(self, *args): + def test_correct_exit_code_should_be_set_on_uninstall_cmd_failure(self, *args): # pylint: disable=too-many-locals test_file_name = "testfile.sh" test_error_file_name = "error.sh" handler_json = { @@ -2115,7 +2122,7 @@ def test_correct_exit_code_should_be_set_on_uninstall_cmd_failure(self, *args): error_dir = os.path.join(conf.get_lib_dir(), 'OSTCExtensions.ExampleHandlerLinux-1.0.0', test_error_file_name) self.create_script(test_error_file_name, test_error_content, error_dir) - test_data, exthandlers_handler, protocol = self._set_up_update_test_and_update_gs(Mock(), *args) + test_data, exthandlers_handler, protocol = self._set_up_update_test_and_update_gs(Mock(), *args) # pylint: disable=unused-variable base_dir = os.path.join(conf.get_lib_dir(), 'OSTCExtensions.ExampleHandlerLinux-1.0.1', test_file_name) self.create_script(test_file_name, test_file, base_dir) @@ -2124,22 +2131,22 @@ def test_correct_exit_code_should_be_set_on_uninstall_cmd_failure(self, *args): with patch.object(ExtHandlerInstance, 'report_event') as mock_report_event: exthandlers_handler.run() - _, disable_kwargs = mock_report_event.call_args_list[1] + _, disable_kwargs = mock_report_event.call_args_list[1] # pylint: disable=unused-variable _, update_kwargs = mock_report_event.call_args_list[2] - _, uninstall_kwargs = mock_report_event.call_args_list[3] + _, uninstall_kwargs = mock_report_event.call_args_list[3] # pylint: disable=unused-variable _, install_kwargs = mock_report_event.call_args_list[4] _, enable_kwargs = mock_report_event.call_args_list[5] self.assertIn("%s=%s" % (ExtCommandEnvVariable.DisableReturnCode, exit_code), update_kwargs['message']) self.assertIn("%s=%s" % (ExtCommandEnvVariable.UninstallReturnCode, exit_code), install_kwargs['message']) self.assertIn("%s=%s" % (ExtCommandEnvVariable.UninstallReturnCode, exit_code), enable_kwargs['message']) - +# pylint: enable=too-many-public-methods @patch("azurelinuxagent.common.protocol.wire.CryptUtil") @patch("azurelinuxagent.common.utils.restutil.http_get") class TestExtensionSequencing(AgentTestCase): - def _create_mock(self, mock_http_get, MockCryptUtil): + def _create_mock(self, mock_http_get, MockCryptUtil): # pylint: disable=invalid-name test_data = mockwiredata.WireProtocolData(mockwiredata.DATA_FILE) # Mock protocol to return test data @@ -2156,7 +2163,7 @@ def _create_mock(self, mock_http_get, MockCryptUtil): handler.ext_handlers, handler.last_etag = protocol.get_ext_handlers() conf.get_enable_overprovisioning = Mock(return_value=False) - def wait_for_handler_successful_completion(prev_handler, wait_until): + def wait_for_handler_successful_completion(prev_handler, wait_until): # pylint: disable=unused-argument return orig_wait_for_handler_successful_completion(prev_handler, datetime.datetime.utcnow() + datetime.timedelta( seconds=5)) @@ -2171,7 +2178,7 @@ def _set_dependency_levels(self, dependency_levels, exthandlers_handler): ''' handler_map = dict() all_handlers = [] - for h, level in dependency_levels: + for h, level in dependency_levels: # pylint: disable=invalid-name if handler_map.get(h) is None: handler = ExtHandler(name=h) extension = Extension(name=h) @@ -2189,7 +2196,7 @@ def _set_dependency_levels(self, dependency_levels, exthandlers_handler): exthandlers_handler.ext_handlers.extHandlers.append(handler) def _validate_extension_sequence(self, expected_sequence, exthandlers_handler): - installed_extensions = [a[0].name for a, k in exthandlers_handler.handle_ext_handler.call_args_list] + installed_extensions = [a[0].name for a, k in exthandlers_handler.handle_ext_handler.call_args_list] # pylint: disable=unused-variable self.assertListEqual(expected_sequence, installed_extensions, "Expected and actual list of extensions are not equal") @@ -2217,7 +2224,7 @@ def test_handle_ext_handlers(self, *args): Verifies that the sequencing is in the expected order and a failure in one extension skips the rest of the extensions in the sequence. ''' - exthandlers_handler = self._create_mock(*args) + exthandlers_handler = self._create_mock(*args) # pylint: disable=no-value-for-parameter self._set_dependency_levels([("A", 3), ("B", 2), ("C", 2), ("D", 1), ("E", 1), ("F", 1), ("G", 1)], exthandlers_handler) @@ -2261,7 +2268,7 @@ def test_handle_ext_handlers_with_uninstallation(self, *args): Verifies that the sequencing is in the expected order and the uninstallation takes place prior to all the installation/enable. ''' - exthandlers_handler = self._create_mock(*args) + exthandlers_handler = self._create_mock(*args) # pylint: disable=no-value-for-parameter # "A", "D" and "F" are marked as to be uninstalled self._set_dependency_levels([("A", 0), ("B", 2), ("C", 2), ("D", 0), ("E", 1), ("F", 0), ("G", 1)], @@ -2278,7 +2285,7 @@ def test_handle_ext_handlers_fallback(self, *args): When there is no dependency specified, the agent is expected to assign dependencyLevel=0 to all extension. Also, it is expected to install all the extension no matter if there is any failure in any of the extensions. ''' - exthandlers_handler = self._create_mock(*args) + exthandlers_handler = self._create_mock(*args) # pylint: disable=no-value-for-parameter self._set_dependency_levels([("A", 1), ("B", 1), ("C", 1), ("D", 1), ("E", 1), ("F", 1), ("G", 1)], exthandlers_handler) @@ -2366,14 +2373,14 @@ def _get_ext_handler_instance(name, version, handler=None, continue_on_update_fa return ext_handler_i def test_disable_failed_env_variable_should_be_set_for_update_cmd_when_continue_on_update_failure_is_true( - self, *args): + self, *args): # pylint: disable=unused-argument old_handler_i = self._get_ext_handler_instance('foo', '1.0.0') new_handler_i = self._get_ext_handler_instance('foo', '1.0.1', continue_on_update_failure=True) with patch.object(CGroupConfigurator.get_instance(), "start_extension_command", side_effect=ExtensionError('disable Failed')) as patch_start_cmd: with self.assertRaises(ExtensionError): - ExtHandlersHandler._update_extension_handler_and_return_if_failed(old_handler_i, new_handler_i) + ExtHandlersHandler._update_extension_handler_and_return_if_failed(old_handler_i, new_handler_i) # pylint: disable=protected-access args, kwargs = patch_start_cmd.call_args @@ -2381,7 +2388,7 @@ def test_disable_failed_env_variable_should_be_set_for_update_cmd_when_continue_ "The update command should have Disable Failed in env variable") def test_uninstall_failed_env_variable_should_set_for_install_when_continue_on_update_failure_is_true( - self, *args): + self, *args): # pylint: disable=unused-argument old_handler_i = self._get_ext_handler_instance('foo', '1.0.0') old_handler_i.set_handler_state(ExtHandlerState.Enabled) new_handler_i = self._get_ext_handler_instance('foo', '1.0.1', continue_on_update_failure=True) @@ -2389,14 +2396,14 @@ def test_uninstall_failed_env_variable_should_set_for_install_when_continue_on_u with patch.object(CGroupConfigurator.get_instance(), "start_extension_command", side_effect=['ok', 'ok', ExtensionError('uninstall Failed'), 'ok']) as patch_start_cmd: - ExtHandlersHandler._update_extension_handler_and_return_if_failed(old_handler_i, new_handler_i) + ExtHandlersHandler._update_extension_handler_and_return_if_failed(old_handler_i, new_handler_i) # pylint: disable=protected-access args, kwargs = patch_start_cmd.call_args self.assertTrue('-install' in kwargs['command'] and ExtCommandEnvVariable.UninstallReturnCode in kwargs['env'], "The install command should have Uninstall Failed in env variable") - def test_extension_error_should_be_raised_when_continue_on_update_failure_is_false_on_disable_failure(self, *args): + def test_extension_error_should_be_raised_when_continue_on_update_failure_is_false_on_disable_failure(self, *args): # pylint: disable=unused-argument old_handler_i = self._get_ext_handler_instance('foo', '1.0.0') old_handler_i.set_handler_state(ExtHandlerState.Enabled) new_handler_i = self._get_ext_handler_instance('foo', '1.0.1', continue_on_update_failure=False) @@ -2404,28 +2411,28 @@ def test_extension_error_should_be_raised_when_continue_on_update_failure_is_fal with patch.object(ExtHandlerInstance, "disable", side_effect=ExtensionError("Disable Failed")): with self.assertRaises(ExtensionUpdateError) as error: # Ensure the error is of type ExtensionUpdateError - ExtHandlersHandler._update_extension_handler_and_return_if_failed(old_handler_i, new_handler_i) + ExtHandlersHandler._update_extension_handler_and_return_if_failed(old_handler_i, new_handler_i) # pylint: disable=protected-access msg = str(error.exception) self.assertIn("Disable Failed", msg, "Update should fail with Disable Failed error") self.assertIn("ExtensionError", msg, "The Exception should initially be propagated as ExtensionError") @patch("azurelinuxagent.common.cgroupconfigurator.handle_process_completion", side_effect="Process Successful") - def test_extension_error_should_be_raised_when_continue_on_update_failure_is_false_on_uninstall_failure(self, *args): + def test_extension_error_should_be_raised_when_continue_on_update_failure_is_false_on_uninstall_failure(self, *args): # pylint: disable=unused-argument old_handler_i = self._get_ext_handler_instance('foo', '1.0.0') new_handler_i = self._get_ext_handler_instance('foo', '1.0.1', continue_on_update_failure=False) with patch.object(ExtHandlerInstance, "uninstall", side_effect=ExtensionError("Uninstall Failed")): with self.assertRaises(ExtensionUpdateError) as error: # Ensure the error is of type ExtensionUpdateError - ExtHandlersHandler._update_extension_handler_and_return_if_failed(old_handler_i, new_handler_i) + ExtHandlersHandler._update_extension_handler_and_return_if_failed(old_handler_i, new_handler_i) # pylint: disable=protected-access msg = str(error.exception) self.assertIn("Uninstall Failed", msg, "Update should fail with Uninstall Failed error") self.assertIn("ExtensionError", msg, "The Exception should initially be propagated as ExtensionError") @patch("azurelinuxagent.common.cgroupconfigurator.handle_process_completion", side_effect="Process Successful") - def test_extension_error_should_be_raised_when_continue_on_update_failure_is_true_on_command_failure(self, *args): + def test_extension_error_should_be_raised_when_continue_on_update_failure_is_true_on_command_failure(self, *args): # pylint: disable=unused-argument old_handler_i = self._get_ext_handler_instance('foo', '1.0.0') new_handler_i = self._get_ext_handler_instance('foo', '1.0.1', continue_on_update_failure=True) @@ -2433,7 +2440,7 @@ def test_extension_error_should_be_raised_when_continue_on_update_failure_is_tru with patch.object(ExtHandlerInstance, "disable", side_effect=ExtensionError("Disable Failed")): with patch.object(ExtHandlerInstance, "update", side_effect=ExtensionError("Update Failed")): with self.assertRaises(ExtensionError) as error: - ExtHandlersHandler._update_extension_handler_and_return_if_failed(old_handler_i, new_handler_i) + ExtHandlersHandler._update_extension_handler_and_return_if_failed(old_handler_i, new_handler_i) # pylint: disable=protected-access msg = str(error.exception) self.assertIn("Update Failed", msg, "Update should fail with Update Failed error") self.assertNotIn("ExtensionUpdateError", msg, "The exception should not be ExtensionUpdateError") @@ -2442,13 +2449,13 @@ def test_extension_error_should_be_raised_when_continue_on_update_failure_is_tru with patch.object(ExtHandlerInstance, "uninstall", side_effect=ExtensionError("Uninstall Failed")): with patch.object(ExtHandlerInstance, "install", side_effect=ExtensionError("Install Failed")): with self.assertRaises(ExtensionError) as error: - ExtHandlersHandler._update_extension_handler_and_return_if_failed(old_handler_i, new_handler_i) + ExtHandlersHandler._update_extension_handler_and_return_if_failed(old_handler_i, new_handler_i) # pylint: disable=protected-access msg = str(error.exception) self.assertIn("Install Failed", msg, "Update should fail with Install Failed error") self.assertNotIn("ExtensionUpdateError", msg, "The exception should not be ExtensionUpdateError") @patch("azurelinuxagent.common.cgroupconfigurator.handle_process_completion", side_effect="Process Successful") - def test_env_variable_should_not_set_when_continue_on_update_failure_is_false(self, *args): + def test_env_variable_should_not_set_when_continue_on_update_failure_is_false(self, *args): # pylint: disable=unused-argument old_handler_i = self._get_ext_handler_instance('foo', '1.0.0') old_handler_i.set_handler_state(ExtHandlerState.Enabled) new_handler_i = self._get_ext_handler_instance('foo', '1.0.1', continue_on_update_failure=False) @@ -2457,7 +2464,7 @@ def test_env_variable_should_not_set_when_continue_on_update_failure_is_false(se with patch.object(ExtHandlerInstance, "launch_command") as patch_launch_command: with patch.object(ExtHandlerInstance, "disable", side_effect=ExtensionError("Disable Failed")): with self.assertRaises(ExtensionUpdateError): - ExtHandlersHandler._update_extension_handler_and_return_if_failed(old_handler_i, new_handler_i) + ExtHandlersHandler._update_extension_handler_and_return_if_failed(old_handler_i, new_handler_i) # pylint: disable=protected-access self.assertEqual(0, patch_launch_command.call_count, "Launch command shouldn't be called even once for" " disable failures") @@ -2466,13 +2473,13 @@ def test_env_variable_should_not_set_when_continue_on_update_failure_is_false(se with patch.object(ExtHandlerInstance, "launch_command") as patch_launch_command: with patch.object(ExtHandlerInstance, "uninstall", side_effect=ExtensionError("Uninstall Failed")): with self.assertRaises(ExtensionUpdateError): - ExtHandlersHandler._update_extension_handler_and_return_if_failed(old_handler_i, new_handler_i) + ExtHandlersHandler._update_extension_handler_and_return_if_failed(old_handler_i, new_handler_i) # pylint: disable=protected-access self.assertEqual(2, patch_launch_command.call_count, "Launch command should be called 2 times for " "Disable->Update") @patch('time.sleep', side_effect=lambda _: mock_sleep(0.001)) - def test_failed_env_variables_should_be_set_from_within_extension_commands(self, *args): + def test_failed_env_variables_should_be_set_from_within_extension_commands(self, *args): # pylint: disable=unused-argument """ This test will test from the perspective of the extensions command weather the env variables are being set for those processes @@ -2502,7 +2509,7 @@ def test_failed_env_variables_should_be_set_from_within_extension_commands(self, # ExtCommandEnvVariable.DisableReturnCode and ExtCommandEnvVariable.UninstallReturnCode env variables. # For update and install we're running the script above to print all the env variables starting with AZURE_ # and verify accordingly if the corresponding env variables are set properly or not - ExtHandlersHandler._update_extension_handler_and_return_if_failed(old_handler_i, new_handler_i) + ExtHandlersHandler._update_extension_handler_and_return_if_failed(old_handler_i, new_handler_i) # pylint: disable=protected-access _, update_kwargs = mock_report.call_args_list[0] _, install_kwargs = mock_report.call_args_list[1] @@ -2554,7 +2561,7 @@ def test_correct_exit_code_should_set_on_disable_cmd_failure(self, _): with patch.object(new_handler_i, 'report_event', autospec=True) as mock_report: - uninstall_rc = ExtHandlersHandler._update_extension_handler_and_return_if_failed(old_handler_i, new_handler_i) + uninstall_rc = ExtHandlersHandler._update_extension_handler_and_return_if_failed(old_handler_i, new_handler_i) # pylint: disable=protected-access _, kwargs = mock_report.call_args self.assertEqual(exit_code, uninstall_rc) @@ -2590,7 +2597,7 @@ def test_timeout_code_should_set_on_cmd_timeout(self, _): file_path=os.path.join(old_handler_i.get_base_dir(), test_failure_file_name)) with patch.object(new_handler_i, 'report_event', autospec=True) as mock_report: - uninstall_rc = ExtHandlersHandler._update_extension_handler_and_return_if_failed(old_handler_i, + uninstall_rc = ExtHandlersHandler._update_extension_handler_and_return_if_failed(old_handler_i, # pylint: disable=protected-access new_handler_i) _, update_kwargs = mock_report.call_args_list[0] _, install_kwargs = mock_report.call_args_list[1] @@ -2633,7 +2640,7 @@ def test_success_code_should_set_in_env_variables_on_cmd_success(self, _): file_path=os.path.join(old_handler_i.get_base_dir(), test_success_file_name)) with patch.object(new_handler_i, 'report_event', autospec=True) as mock_report: - uninstall_rc = ExtHandlersHandler._update_extension_handler_and_return_if_failed(old_handler_i, + uninstall_rc = ExtHandlersHandler._update_extension_handler_and_return_if_failed(old_handler_i, # pylint: disable=protected-access new_handler_i) _, update_kwargs = mock_report.call_args_list[0] _, install_kwargs = mock_report.call_args_list[1] @@ -2653,7 +2660,7 @@ def test_disable_should_not_be_called_during_version_upgrade_if_not_enabled(self with patch.object(CGroupConfigurator.get_instance(), "start_extension_command", return_value="[stdout]\n\n\n[stderr]\n\n\n"): with patch.object(old_handler_i, 'disable', autospec=True) as mock_disable: - uninstall_rc = ExtHandlersHandler._update_extension_handler_and_return_if_failed(old_handler_i, + uninstall_rc = ExtHandlersHandler._update_extension_handler_and_return_if_failed(old_handler_i, # pylint: disable=unused-variable,protected-access new_handler_i) mock_disable.mock.assert_not_called() # Python2.6's mock library doesn't forward assert_not_called, so we have to do it ourselves. @@ -2664,7 +2671,7 @@ def setUp(self): ExtensionTestCase.setUp(self) self.lib_dir = tempfile.mkdtemp() - def _setup_extension_for_validating_collect_ext_status(self, mock_lib_dir, status_file, *args): + def _setup_extension_for_validating_collect_ext_status(self, mock_lib_dir, status_file, *args): # pylint: disable=unused-argument handler_name = "TestHandler" handler_version = "1.0.0" mock_lib_dir.return_value = self.lib_dir @@ -2725,12 +2732,12 @@ def test_collect_ext_status_very_large_status_message(self, mock_lib_dir, *args) # [TRUNCATED] comes from azurelinuxagent.ga.exthandlers._TRUNCATED_SUFFIX self.assertRegex(ext_status.message, r"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum non " r"lacinia urna, sit .*\[TRUNCATED\]") - self.maxDiff = None + self.maxDiff = None # pylint: disable=invalid-name self.assertEqual(ext_status.status, ValidHandlerStatus.success) self.assertEqual(len(ext_status.substatusList), 1) # NUM OF SUBSTATUS PARSED for sub_status in ext_status.substatusList: - self.assertRegex(sub_status.name, '\[\{"status"\: \{"status": "success", "code": "1", "snapshotInfo": ' - '\[\{"snapshotUri":.*') + self.assertRegex(sub_status.name, '\[\{"status"\: \{"status": "success", "code": "1", "snapshotInfo": ' # pylint: disable=anomalous-backslash-in-string + '\[\{"snapshotUri":.*') # pylint: disable=anomalous-backslash-in-string self.assertEqual(0, sub_status.code) self.assertRegex(sub_status.message, "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum " "non lacinia urna, sit amet venenatis orci.*") @@ -2756,8 +2763,8 @@ def test_collect_ext_status_very_large_status_file_with_multiple_substatus_nodes self.assertEqual(ext_status.status, ValidHandlerStatus.success) self.assertEqual(len(ext_status.substatusList), 12) # The original file has 41 substatus nodes. for sub_status in ext_status.substatusList: - self.assertRegex(sub_status.name, '\[\{"status"\: \{"status": "success", "code": "1", "snapshotInfo": ' - '\[\{"snapshotUri":.*') + self.assertRegex(sub_status.name, '\[\{"status"\: \{"status": "success", "code": "1", "snapshotInfo": ' # pylint: disable=anomalous-backslash-in-string + '\[\{"snapshotUri":.*') # pylint: disable=anomalous-backslash-in-string self.assertEqual(0, sub_status.code) self.assertRegex(sub_status.message, "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum " "non lacinia urna, sit amet venenatis orci.*") @@ -2772,17 +2779,17 @@ def test_collect_ext_status_read_file_read_exceptions(self, mock_lib_dir, *args) "sample-status.json", *args) original_read_file = read_file - def mock_read_file(file, *args, **kwargs): + def mock_read_file(file, *args, **kwargs): # pylint: disable=redefined-builtin expected_status_file_path = os.path.join(self.lib_dir, ext_handler_i.ext_handler.name + "-" + ext_handler_i.ext_handler. properties.version, "status", "0.status") - if file == expected_status_file_path: + if file == expected_status_file_path: # pylint: disable=no-else-raise raise IOError("No such file or directory: {0}".format(expected_status_file_path)) else: original_read_file(file, *args, **kwargs) - with patch('azurelinuxagent.common.utils.fileutil.read_file', mock_read_file) as patch_read_file: + with patch('azurelinuxagent.common.utils.fileutil.read_file', mock_read_file) as patch_read_file: # pylint: disable=unused-variable ext_status = ext_handler_i.collect_ext_status(extension) self.assertEqual(ext_status.code, ExtensionErrorCodes.PluginUnknownFailure) diff --git a/tests/ga/test_extension_telemetry.py b/tests/ga/test_extension_telemetry.py index 0095bbf571..172059f746 100644 --- a/tests/ga/test_extension_telemetry.py +++ b/tests/ga/test_extension_telemetry.py @@ -23,7 +23,7 @@ from azurelinuxagent.ga.extension_telemetry import ExtensionEventSchema, ProcessExtensionTelemetry from tests.protocol.mocks import mock_wire_protocol, HttpRequestPredicates, MockHttpResponse from tests.protocol.mockwiredata import DATA_FILE -from tests.tools import AgentTestCase, clear_singleton_instances, data_dir, skip_if_predicate_true +from tests.tools import AgentTestCase, clear_singleton_instances, data_dir, skip_if_predicate_true # pylint: disable=unused-import class TestExtensionTelemetryHandler(AgentTestCase, HttpRequestPredicates): @@ -66,7 +66,7 @@ def _parse_file_and_count_good_events(test_events_file_path): raise OSError("Test Events file {0} not found".format(test_events_file_path)) try: - with open(test_events_file_path, "rb") as fd: + with open(test_events_file_path, "rb") as fd: # pylint: disable=invalid-name event_data = fd.read().decode("utf-8") # Parse the string and get the list of events @@ -75,7 +75,7 @@ def _parse_file_and_count_good_events(test_events_file_path): if not isinstance(events, list): events = [events] - except Exception as e: + except Exception as e: # pylint: disable=invalid-name print("Error parsing json file: {0}".format(e)) return 0 @@ -91,7 +91,7 @@ def _create_random_extension_events_dir_with_events(no_of_extensions, events_pat test_events_paths = [events_path] extension_names = {} - for i in range(no_of_extensions): + for i in range(no_of_extensions): # pylint: disable=unused-variable ext_name = "Microsoft.OSTCExtensions.{0}".format(''.join(random.sample(string.ascii_letters, no_of_chars))) no_of_good_events = 0 @@ -114,12 +114,12 @@ def _get_no_of_events_from_body(body): @staticmethod def _replace_in_file(file_path, replace_from, replace_to): - with open(file_path, 'r') as f: + with open(file_path, 'r') as f: # pylint: disable=invalid-name content = f.read() content = content.replace(replace_from, replace_to) - with open(file_path, 'w') as f: + with open(file_path, 'w') as f: # pylint: disable=invalid-name f.write(content) @staticmethod @@ -206,7 +206,7 @@ def _assert_param_in_events(self, event_body, param_key, param_value, min_count= min_count)) @staticmethod def _get_ustr_from_event_body(body): - return body if (body is None or type(body) is ustr) else textutil.str_to_encoded_ustr(body) + return body if (body is None or type(body) is ustr) else textutil.str_to_encoded_ustr(body) # pylint: disable=unidiomatic-typecheck @staticmethod def _is_string_in_event_body(event_body, expected_string): @@ -362,7 +362,7 @@ def test_it_should_trim_message_if_more_than_limit(self): max_len = 100 no_of_extensions = 2 with patch("azurelinuxagent.ga.extension_telemetry.ProcessExtensionTelemetry._EXTENSION_EVENT_MAX_MSG_LEN", max_len): - handler_name_with_count, event_body = self._setup_and_assert_tests_for_max_sizes() + handler_name_with_count, event_body = self._setup_and_assert_tests_for_max_sizes() # pylint: disable=unused-variable context1_vals = self._get_param_value_from_event_body_if_exists(event_body, GuestAgentGenericLogsSchema.Context1) self.assertEqual(no_of_extensions, len(context1_vals), @@ -392,7 +392,7 @@ def test_it_should_skip_large_files_greater_than_max_file_size_and_report_event( pattern = r'Skipping file:\s*{0}/(?P.+?)/{1}.+'.format(conf.get_ext_log_dir(), EVENTS_DIRECTORY) self._assert_event_reported(mock_event, handler_name_with_count, pattern) - def test_it_should_map_extension_event_json_correctly_to_telemetry_event(self): + def test_it_should_map_extension_event_json_correctly_to_telemetry_event(self): # pylint: disable=too-many-locals # EventName maps to HandlerName + '-' + Version from event file expected_mapping = { @@ -509,7 +509,7 @@ def test_it_should_not_send_events_which_dont_have_all_required_keys_and_report_ ExtensionEventSchema.Message.lower(): 2, ExtensionEventSchema.Version.lower(): 3 } - for m in msg: + for m in msg: # pylint: disable=invalid-name match = re.search(patt, m) self.assertIsNotNone(match, "No InvalidExtensionEventError errors reported") self.assertEqual(match.group("reason").strip(), InvalidExtensionEventError.MissingKeyError, diff --git a/tests/ga/test_exthandlers.py b/tests/ga/test_exthandlers.py index 93a45089e5..3ed32f42fd 100644 --- a/tests/ga/test_exthandlers.py +++ b/tests/ga/test_exthandlers.py @@ -46,6 +46,7 @@ def test_parse_extension_status00(self): Parse a status report for a successful execution of an extension. """ + # pylint: disable=invalid-name s = '''[{ "status": { "status": "success", @@ -61,6 +62,7 @@ def test_parse_extension_status00(self): "timestampUTC": "2018-04-20T21:20:24Z" } ]''' + # pylint: enable=invalid-name ext_status = ExtensionStatus(seq_no=0) parse_ext_status(ext_status, json.loads(s)) @@ -80,8 +82,8 @@ def test_parse_extension_status01(self): The agent should handle this gracefully, and convert all unknown status/status values into an error. """ - - s = '''[{ + # pylint: disable=invalid-name + s = '''[{ "status": { "status": "failed", "formattedMessage": { @@ -95,6 +97,7 @@ def test_parse_extension_status01(self): "version": "1.0", "timestampUTC": "2018-04-20T20:50:22Z" }]''' + # pylint: enable=invalid-name ext_status = ExtensionStatus(seq_no=0) parse_ext_status(ext_status, json.loads(s)) @@ -163,7 +166,7 @@ def test_parse_extension_status_with_empty_status(self): """ # Validating empty status case - s = '''[]''' + s = '''[]''' # pylint: disable=invalid-name ext_status = ExtensionStatus(seq_no=0) parse_ext_status(ext_status, json.loads(s)) @@ -189,7 +192,7 @@ def test_parse_extension_status_with_empty_status(self): @patch('azurelinuxagent.common.event.EventLogger.add_event') @patch('azurelinuxagent.ga.exthandlers.ExtHandlerInstance._get_largest_seq_no') - def assert_extension_sequence_number(self, + def assert_extension_sequence_number(self, # pylint: disable=too-many-locals,too-many-arguments patch_get_largest_seq, patch_add_event, goal_state_sequence_number, @@ -215,7 +218,7 @@ def assert_extension_sequence_number(self, if gs_int and gs_seq_int != disk_sequence_number: self.assertEqual(1, patch_add_event.call_count) - args, kw_args = patch_add_event.call_args + args, kw_args = patch_add_event.call_args # pylint: disable=unused-variable self.assertEqual('SequenceNumberMismatch', kw_args['op']) self.assertEqual(False, kw_args['is_success']) self.assertEqual('Goal state: {0}, disk: {1}' @@ -231,19 +234,19 @@ def assert_extension_sequence_number(self, self.assertIsNone(path) def test_extension_sequence_number(self): - self.assert_extension_sequence_number(goal_state_sequence_number="12", + self.assert_extension_sequence_number(goal_state_sequence_number="12", # pylint: disable=no-value-for-parameter disk_sequence_number=366, expected_sequence_number=12) - self.assert_extension_sequence_number(goal_state_sequence_number=" 12 ", + self.assert_extension_sequence_number(goal_state_sequence_number=" 12 ", # pylint: disable=no-value-for-parameter disk_sequence_number=366, expected_sequence_number=12) - self.assert_extension_sequence_number(goal_state_sequence_number=" foo", + self.assert_extension_sequence_number(goal_state_sequence_number=" foo", # pylint: disable=no-value-for-parameter disk_sequence_number=3, expected_sequence_number=3) - self.assert_extension_sequence_number(goal_state_sequence_number="-1", + self.assert_extension_sequence_number(goal_state_sequence_number="-1", # pylint: disable=no-value-for-parameter disk_sequence_number=3, expected_sequence_number=-1) @@ -382,7 +385,7 @@ def sleep(seconds): start_time = time.time() - with patch("time.sleep", side_effect=sleep, autospec=True) as mock_sleep: + with patch("time.sleep", side_effect=sleep, autospec=True) as mock_sleep: # pylint: disable=redefined-outer-name with self.assertRaises(ExtensionError) as context_manager: self.ext_handler_instance.launch_command(command, timeout=timeout, extension_error_code=extension_error_code) @@ -393,7 +396,7 @@ def sleep(seconds): self.assertRegex(message, r"Timeout\(\d+\):\s+{0}\s+{1}".format(command_full_path, LaunchCommandTestCase._output_regex(stdout, stderr))) # the exception code should be as specified in the call to launch_command - self.assertEquals(context_manager.exception.code, extension_error_code) + self.assertEquals(context_manager.exception.code, extension_error_code) # pylint: disable=deprecated-method # the timeout period should have elapsed self.assertGreaterEqual(mock_sleep.call_count, timeout) @@ -426,7 +429,7 @@ def test_it_should_raise_an_exception_when_the_command_fails(self): message = str(context_manager.exception) self.assertRegex(message, r"Non-zero exit code: {0}.+{1}\s+{2}".format(exit_code, command, LaunchCommandTestCase._output_regex(stdout, stderr))) - self.assertEquals(context_manager.exception.code, extension_error_code) + self.assertEquals(context_manager.exception.code, extension_error_code) # pylint: disable=deprecated-method def test_it_should_not_wait_for_child_process(self): stdout = "stdout" @@ -567,7 +570,7 @@ def test_it_should_not_capture_the_output_of_commands_that_do_their_own_redirect with open(command_output_file, "r") as command_output: output = command_output.read() - self.assertEquals(output, "{0}\n{1}\n".format(stdout, stderr)) + self.assertEquals(output, "{0}\n{1}\n".format(stdout, stderr)) # pylint: disable=deprecated-method def test_it_should_truncate_the_command_output(self): stdout = "STDOUT" @@ -605,7 +608,7 @@ def test_it_should_read_only_the_head_of_large_outputs(self): mock_format.assert_called_once() - args, kwargs = mock_format.call_args + args, kwargs = mock_format.call_args # pylint: disable=unused-variable stdout, stderr = args self.assertGreaterEqual(len(stdout), 1024) @@ -626,7 +629,7 @@ def test_it_should_handle_errors_while_reading_the_command_output(self): # trying to use these files. original_capture_process_output = read_output - def capture_process_output(stdout_file, stderr_file): + def capture_process_output(stdout_file, stderr_file): # pylint: disable=unused-argument return original_capture_process_output(None, None) with patch('azurelinuxagent.common.utils.extensionprocessutil.read_output', side_effect=capture_process_output): @@ -649,7 +652,7 @@ def test_it_should_contain_all_helper_environment_variables(self): with patch("subprocess.Popen", wraps=subprocess.Popen) as patch_popen: output = self.ext_handler_instance.launch_command(test_file) - args, kwagrs = patch_popen.call_args + args, kwagrs = patch_popen.call_args # pylint: disable=unused-variable without_os_env = dict((k, v) for (k, v) in kwagrs['env'].items() if k not in os.environ) # This check will fail if any helper environment variables are added/removed later on diff --git a/tests/ga/test_exthandlers_download_extension.py b/tests/ga/test_exthandlers_download_extension.py index adbdebc193..eb076754f7 100644 --- a/tests/ga/test_exthandlers_download_extension.py +++ b/tests/ga/test_exthandlers_download_extension.py @@ -1,7 +1,7 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the Apache License. -import zipfile, time, os +import zipfile, time, os # pylint: disable=multiple-imports from azurelinuxagent.common.protocol.restapi import ExtHandler, ExtHandlerProperties, ExtHandlerPackage, ExtHandlerVersionUri from azurelinuxagent.common.protocol.wire import WireProtocol @@ -10,7 +10,7 @@ from tests.tools import AgentTestCase, patch, mock_sleep -class DownloadExtensionTestCase(AgentTestCase): +class DownloadExtensionTestCase(AgentTestCase): # pylint: disable=too-many-instance-attributes """ Test cases for launch_command """ @@ -69,7 +69,7 @@ def tearDown(self): @staticmethod def _create_zip_file(filename): - file = None + file = None # pylint: disable=redefined-builtin try: file = zipfile.ZipFile(filename, "w") info = zipfile.ZipInfo(DownloadExtensionTestCase._extension_command) @@ -82,7 +82,7 @@ def _create_zip_file(filename): @staticmethod def _create_invalid_zip_file(filename): - with open(filename, "w") as file: + with open(filename, "w") as file: # pylint: disable=redefined-builtin file.write("An invalid ZIP file\n") def _get_extension_package_file(self): @@ -184,7 +184,7 @@ def download_ext_handler_pkg(_uri, destination): "Ensure that the state is maintained for extension HandlerState") def test_it_should_use_alternate_uris_when_download_fails(self): - self.download_failures = 0 + self.download_failures = 0 # pylint: disable=attribute-defined-outside-init def download_ext_handler_pkg(_uri, destination): # fail a few times, then succeed @@ -197,12 +197,12 @@ def download_ext_handler_pkg(_uri, destination): with patch("azurelinuxagent.common.protocol.wire.WireProtocol.download_ext_handler_pkg", side_effect=download_ext_handler_pkg) as mock_download_ext_handler_pkg: self.ext_handler_instance.download() - self.assertEquals(mock_download_ext_handler_pkg.call_count, self.download_failures + 1) + self.assertEquals(mock_download_ext_handler_pkg.call_count, self.download_failures + 1) # pylint: disable=deprecated-method self._assert_download_and_expand_succeeded() def test_it_should_use_alternate_uris_when_download_raises_an_exception(self): - self.download_failures = 0 + self.download_failures = 0 # pylint: disable=attribute-defined-outside-init def download_ext_handler_pkg(_uri, destination): # fail a few times, then succeed @@ -215,12 +215,12 @@ def download_ext_handler_pkg(_uri, destination): with patch("azurelinuxagent.common.protocol.wire.WireProtocol.download_ext_handler_pkg", side_effect=download_ext_handler_pkg) as mock_download_ext_handler_pkg: self.ext_handler_instance.download() - self.assertEquals(mock_download_ext_handler_pkg.call_count, self.download_failures + 1) + self.assertEquals(mock_download_ext_handler_pkg.call_count, self.download_failures + 1) # pylint: disable=deprecated-method self._assert_download_and_expand_succeeded() def test_it_should_use_alternate_uris_when_it_downloads_an_invalid_package(self): - self.download_failures = 0 + self.download_failures = 0 # pylint: disable=attribute-defined-outside-init def download_ext_handler_pkg(_uri, destination): # fail a few times, then succeed @@ -234,7 +234,7 @@ def download_ext_handler_pkg(_uri, destination): with patch("azurelinuxagent.common.protocol.wire.WireProtocol.download_ext_handler_pkg", side_effect=download_ext_handler_pkg) as mock_download_ext_handler_pkg: self.ext_handler_instance.download() - self.assertEquals(mock_download_ext_handler_pkg.call_count, self.download_failures + 1) + self.assertEquals(mock_download_ext_handler_pkg.call_count, self.download_failures + 1) # pylint: disable=deprecated-method self._assert_download_and_expand_succeeded() @@ -248,10 +248,10 @@ def download_ext_handler_pkg(_uri, _destination): with self.assertRaises(ExtensionDownloadError) as context_manager: self.ext_handler_instance.download() - self.assertEquals(mock_download_ext_handler_pkg.call_count, NUMBER_OF_DOWNLOAD_RETRIES * len(self.pkg.uris)) + self.assertEquals(mock_download_ext_handler_pkg.call_count, NUMBER_OF_DOWNLOAD_RETRIES * len(self.pkg.uris)) # pylint: disable=deprecated-method self.assertRegex(str(context_manager.exception), "Failed to download extension") - self.assertEquals(context_manager.exception.code, ExtensionErrorCodes.PluginManifestDownloadError) + self.assertEquals(context_manager.exception.code, ExtensionErrorCodes.PluginManifestDownloadError) # pylint: disable=deprecated-method self.assertFalse(os.path.exists(self.extension_dir), "The extension directory was not removed") self.assertFalse(os.path.exists(self._get_extension_package_file()), "The extension package was not removed") diff --git a/tests/ga/test_exthandlers_exthandlerinstance.py b/tests/ga/test_exthandlers_exthandlerinstance.py index 159fefb2dd..884c80b4ad 100644 --- a/tests/ga/test_exthandlers_exthandlerinstance.py +++ b/tests/ga/test_exthandlers_exthandlerinstance.py @@ -4,9 +4,9 @@ from azurelinuxagent.ga.exthandlers import ExtHandlerInstance from azurelinuxagent.common.protocol.restapi import ExtHandler, ExtHandlerProperties, ExtHandlerPackage, \ ExtHandlerVersionUri -import os -import shutil -import sys +import os # pylint: disable=wrong-import-order +import shutil # pylint: disable=wrong-import-order +import sys # pylint: disable=wrong-import-order from tests.tools import AgentTestCase, patch @@ -117,7 +117,7 @@ def test_rm_ext_handler_dir_should_report_an_event_if_an_error_occurs_while_dele original_remove_api = getattr(shutil.os, remove_api_name) - def mock_remove(path, dir_fd=None): + def mock_remove(path, dir_fd=None): # pylint: disable=unused-argument if path.endswith("extension_file2"): raise IOError("A mocked error") original_remove_api(path) @@ -126,6 +126,6 @@ def mock_remove(path, dir_fd=None): with patch.object(self.ext_handler_instance, "report_event") as mock_report_event: self.ext_handler_instance.remove_ext_handler() - args, kwargs = mock_report_event.call_args + args, kwargs = mock_report_event.call_args # pylint: disable=unused-variable self.assertTrue("A mocked error" in kwargs["message"]) diff --git a/tests/ga/test_monitor.py b/tests/ga/test_monitor.py index 8c5ed9ae32..7735e4d9b9 100644 --- a/tests/ga/test_monitor.py +++ b/tests/ga/test_monitor.py @@ -26,7 +26,7 @@ import tempfile import time import uuid -from datetime import timedelta +from datetime import timedelta # pylint: disable=ungrouped-imports from azurelinuxagent.common.protocol.util import ProtocolUtil @@ -55,7 +55,7 @@ def random_generator(size=6, chars=string.ascii_uppercase + string.digits + stri return ''.join(random.choice(chars) for x in range(size)) @contextlib.contextmanager -def _create_monitor_handler(enabled_operations=[], iterations=1): +def _create_monitor_handler(enabled_operations=[], iterations=1): # pylint: disable=dangerous-default-value """ Creates an instance of MonitorHandler that * Uses a mock_wire_protocol for network requests, @@ -69,7 +69,7 @@ def _create_monitor_handler(enabled_operations=[], iterations=1): """ def run(self): - if len(enabled_operations) == 0 or self._name in enabled_operations: + if len(enabled_operations) == 0 or self._name in enabled_operations: # pylint: disable=protected-access,len-as-condition run.original_definition(self) run.original_definition = PeriodicOperation.run @@ -180,7 +180,7 @@ def tearDown(self): _TEST_EVENT_PROVIDER_ID = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" - def _create_extension_event(self, + def _create_extension_event(self, # pylint: disable=invalid-name,too-many-arguments size=0, name="DummyExtension", op=WALAEventOperation.Unknown, @@ -195,12 +195,12 @@ def _create_extension_event(self, version=version, message=random_generator(size) if size != 0 else message) event_file = os.path.join(self.event_dir, "{0}.tld".format(int(time.time() * 1000000))) - with open(event_file, 'wb+') as fd: + with open(event_file, 'wb+') as fd: # pylint: disable=invalid-name fd.write(event_data.encode('utf-8')) @staticmethod - def _get_event_data(duration, is_success, message, name, op, version, eventId=1): - event = TelemetryEvent(eventId, TestEventMonitoring._TEST_EVENT_PROVIDER_ID) + def _get_event_data(duration, is_success, message, name, op, version, eventId=1): # pylint: disable=invalid-name,too-many-arguments + event = TelemetryEvent(eventId, TestEventMonitoring._TEST_EVENT_PROVIDER_ID) # pylint: disable=redefined-outer-name event.parameters.append(TelemetryEventParam(GuestAgentExtensionEventsSchema.Name, name)) event.parameters.append(TelemetryEventParam(GuestAgentExtensionEventsSchema.Version, str(version))) event.parameters.append(TelemetryEventParam(GuestAgentExtensionEventsSchema.Operation, op)) @@ -243,7 +243,7 @@ def test_collect_and_send_events(self, mock_lib_dir, patch_send_event, *_): # Validating the crafted message by the collect_and_send_events call. self.assertEqual(1, patch_send_event.call_count) - send_event_call_args = monitor_handler.get_mock_wire_protocol().client.send_encoded_event.call_args[0] + send_event_call_args = monitor_handler.get_mock_wire_protocol().client.send_encoded_event.call_args[0] # pylint: disable=no-member # Some of those expected values come from the mock protocol and imds client set up during test initialization osutil = get_osutil() @@ -281,7 +281,7 @@ def test_collect_and_send_events(self, mock_lib_dir, patch_send_event, *_): test_eventpid, test_taskname, osversion, int(osutil.get_total_mem()), osutil.get_processor_cores()) - self.maxDiff = None + self.maxDiff = None # pylint: disable=invalid-name self.assertEqual(sample_message.encode('utf-8'), send_event_call_args[1]) @patch("azurelinuxagent.common.protocol.wire.WireClient.send_encoded_event") @@ -354,7 +354,7 @@ def http_post_handler(url, _, **__): @patch("azurelinuxagent.common.conf.get_lib_dir") - def test_collect_and_send_with_send_event_generating_exception(self, mock_lib_dir, *args): + def test_collect_and_send_with_send_event_generating_exception(self, mock_lib_dir, *args): # pylint: disable=unused-argument mock_lib_dir.return_value = self.lib_dir fileutil.mkdir(self.event_dir) @@ -377,7 +377,7 @@ def test_collect_and_send_with_send_event_generating_exception(self, mock_lib_di self._assert_error_event_reported(mock_add_event, test_str) @patch("azurelinuxagent.common.conf.get_lib_dir") - def test_collect_and_send_with_call_wireserver_returns_http_error_and_reports_event(self, mock_lib_dir, *args): + def test_collect_and_send_with_call_wireserver_returns_http_error_and_reports_event(self, mock_lib_dir, *args): # pylint: disable=unused-argument mock_lib_dir.return_value = self.lib_dir fileutil.mkdir(self.event_dir) add_event(name="MonitorTests", op=WALAEventOperation.HeartBeat, is_success=True, message="Test heartbeat") @@ -423,7 +423,7 @@ def tearDown(self): @patch('azurelinuxagent.common.event.EventLogger.add_metric') @patch('azurelinuxagent.common.event.EventLogger.add_event') @patch("azurelinuxagent.common.cgroupstelemetry.CGroupsTelemetry.poll_all_tracked") - def test_send_extension_metrics_telemetry(self, patch_poll_all_tracked, patch_add_event, + def test_send_extension_metrics_telemetry(self, patch_poll_all_tracked, patch_add_event, # pylint: disable=unused-argument patch_add_metric, *args): patch_poll_all_tracked.return_value = [MetricValue("Process", "% Processor Time", 1, 1), MetricValue("Memory", "Total Memory Usage", 1, 1), @@ -436,7 +436,7 @@ def test_send_extension_metrics_telemetry(self, patch_poll_all_tracked, patch_ad @patch('azurelinuxagent.common.event.EventLogger.add_metric') @patch('azurelinuxagent.common.event.EventLogger.add_event') @patch("azurelinuxagent.common.cgroupstelemetry.CGroupsTelemetry.poll_all_tracked") - def test_send_extension_metrics_telemetry_for_empty_cgroup(self, patch_poll_all_tracked, + def test_send_extension_metrics_telemetry_for_empty_cgroup(self, patch_poll_all_tracked, # pylint: disable=unused-argument patch_add_event, patch_add_metric,*args): patch_poll_all_tracked.return_value = [] @@ -448,14 +448,14 @@ def test_send_extension_metrics_telemetry_for_empty_cgroup(self, patch_poll_all_ @patch('azurelinuxagent.common.event.EventLogger.add_metric') @patch("azurelinuxagent.common.cgroup.MemoryCgroup.get_memory_usage") @patch('azurelinuxagent.common.logger.Logger.periodic_warn') - def test_send_extension_metrics_telemetry_handling_memory_cgroup_exceptions_errno2(self, patch_periodic_warn, + def test_send_extension_metrics_telemetry_handling_memory_cgroup_exceptions_errno2(self, patch_periodic_warn, # pylint: disable=unused-argument patch_get_memory_usage, patch_add_metric, *args): ioerror = IOError() ioerror.errno = 2 patch_get_memory_usage.side_effect = ioerror - CGroupsTelemetry._tracked.append(MemoryCgroup("cgroup_name", "/test/path")) + CGroupsTelemetry._tracked.append(MemoryCgroup("cgroup_name", "/test/path")) # pylint: disable=protected-access PollResourceUsageOperation().run() self.assertEqual(0, patch_periodic_warn.call_count) @@ -464,14 +464,14 @@ def test_send_extension_metrics_telemetry_handling_memory_cgroup_exceptions_errn @patch('azurelinuxagent.common.event.EventLogger.add_metric') @patch("azurelinuxagent.common.cgroup.CpuCgroup.get_cpu_usage") @patch('azurelinuxagent.common.logger.Logger.periodic_warn') - def test_send_extension_metrics_telemetry_handling_cpu_cgroup_exceptions_errno2(self, patch_periodic_warn, + def test_send_extension_metrics_telemetry_handling_cpu_cgroup_exceptions_errno2(self, patch_periodic_warn, # pylint: disable=unused-argument patch_cpu_usage, patch_add_metric, *args): ioerror = IOError() ioerror.errno = 2 patch_cpu_usage.side_effect = ioerror - CGroupsTelemetry._tracked.append(CpuCgroup("cgroup_name", "/test/path")) + CGroupsTelemetry._tracked.append(CpuCgroup("cgroup_name", "/test/path")) # pylint: disable=protected-access PollResourceUsageOperation().run() self.assertEqual(0, patch_periodic_warn.call_count) @@ -479,14 +479,14 @@ def test_send_extension_metrics_telemetry_handling_cpu_cgroup_exceptions_errno2( @patch('azurelinuxagent.common.event.EventLogger.add_metric') @patch('azurelinuxagent.common.logger.Logger.periodic_warn') - def test_send_extension_metrics_telemetry_for_unsupported_cgroup(self, patch_periodic_warn, patch_add_metric, *args): - CGroupsTelemetry._tracked.append(CGroup("cgroup_name", "/test/path", "io")) + def test_send_extension_metrics_telemetry_for_unsupported_cgroup(self, patch_periodic_warn, patch_add_metric, *args): # pylint: disable=unused-argument + CGroupsTelemetry._tracked.append(CGroup("cgroup_name", "/test/path", "io")) # pylint: disable=protected-access PollResourceUsageOperation().run() self.assertEqual(1, patch_periodic_warn.call_count) self.assertEqual(0, patch_add_metric.call_count) # No metrics should be sent. - def test_generate_extension_metrics_telemetry_dictionary(self, *args): + def test_generate_extension_metrics_telemetry_dictionary(self, *args): # pylint: disable=unused-argument num_polls = 10 num_extensions = 1 @@ -506,7 +506,7 @@ def test_generate_extension_metrics_telemetry_dictionary(self, *args): "dummy_extension_{0}".format(i)) CGroupsTelemetry.track_cgroup(dummy_memory_cgroup) - self.assertEqual(2 * num_extensions, len(CGroupsTelemetry._tracked)) + self.assertEqual(2 * num_extensions, len(CGroupsTelemetry._tracked)) # pylint: disable=protected-access with patch("azurelinuxagent.common.cgroup.MemoryCgroup.get_max_memory_usage") as patch_get_memory_max_usage: with patch("azurelinuxagent.common.cgroup.MemoryCgroup.get_memory_usage") as patch_get_memory_usage: @@ -525,19 +525,19 @@ class PollResourceUsageOperationTestCase(AgentTestCase): def setUpClass(cls): AgentTestCase.setUpClass() # ensure cgroups are enabled by forcing a new instance - CGroupConfigurator._instance = None + CGroupConfigurator._instance = None # pylint: disable=protected-access with mock_cgroup_commands(): CGroupConfigurator.get_instance().initialize() @classmethod def tearDownClass(cls): - CGroupConfigurator._instance = None + CGroupConfigurator._instance = None # pylint: disable=protected-access AgentTestCase.tearDownClass() def test_it_should_report_processes_that_do_not_belong_to_the_agent_cgroup(self): with mock_cgroup_commands() as mock_commands: mock_commands.add_command(r'^systemd-cgls.+/walinuxagent.service$', -''' +''' Directory /sys/fs/cgroup/cpu/system.slice/walinuxagent.service: ├─27519 /usr/bin/python3 -u /usr/sbin/waagent -daemon ├─27547 python3 -u bin/WALinuxAgent-2.2.48.1-py2.7.egg -run-exthandlers @@ -569,14 +569,14 @@ def test_it_should_report_processes_that_do_not_belong_to_the_agent_cgroup(self) '/bin/sh /var/lib/waagent/run-command/download/1/script.sh', ] - for fp in unexpected_processes: + for fp in unexpected_processes: # pylint: disable=invalid-name self.assertIn(fp, messages[0], "[{0}] was not reported as an unexpected process. Events: {1}".format(fp, messages)) # The list of processes in the message is an array of strings: "['foo', ..., 'bar']" search = re.search(r'\[(?P.+)\]', messages[0]) self.assertIsNotNone(search, "The event message is not in the expected format: {0}".format(messages[0])) processes = search.group('processes') - self.assertEquals(5, len(processes.split(',')), 'Extra processes were reported as unexpected: {0}'.format(processes)) + self.assertEquals(5, len(processes.split(',')), 'Extra processes were reported as unexpected: {0}'.format(processes)) # pylint: disable=deprecated-method @patch("azurelinuxagent.common.utils.restutil.http_post") @@ -586,7 +586,7 @@ def test_it_should_report_processes_that_do_not_belong_to_the_agent_cgroup(self) class TestMonitorFailure(AgentTestCase): @patch("azurelinuxagent.common.protocol.healthservice.HealthService.report_host_plugin_heartbeat") - def test_error_heartbeat_creates_no_signal(self, patch_report_heartbeat, patch_http_get, patch_add_event, *args): + def test_error_heartbeat_creates_no_signal(self, patch_report_heartbeat, patch_http_get, patch_add_event, *args): # pylint: disable=unused-argument monitor_handler = get_monitor_handler() protocol = WireProtocol('endpoint') diff --git a/tests/ga/test_periodic_operation.py b/tests/ga/test_periodic_operation.py index 8ed0ecce69..ecf4f0be65 100644 --- a/tests/ga/test_periodic_operation.py +++ b/tests/ga/test_periodic_operation.py @@ -26,7 +26,7 @@ def operation(): operation.run_time = datetime.datetime.utcnow() operation.run_time = None - op = PeriodicOperation("test_operation", operation, period=datetime.timedelta(hours=1)) + op = PeriodicOperation("test_operation", operation, period=datetime.timedelta(hours=1)) # pylint: disable=invalid-name op.run() expected = operation.run_time + datetime.timedelta(hours=1) @@ -39,7 +39,7 @@ def operation(): operation.run_time = datetime.datetime.utcnow() operation.run_time = None - op = PeriodicOperation("test_operation", operation, period=3600) + op = PeriodicOperation("test_operation", operation, period=3600) # pylint: disable=invalid-name op.run() expected = operation.run_time + datetime.timedelta(hours=1) @@ -116,7 +116,7 @@ def test_it_should_log_warnings_if_they_are_different(self): with patch("azurelinuxagent.common.logger.warn") as warn_patcher: for i in range(2): def operation(): - raise Exception("WARNING {0}".format(i)) + raise Exception("WARNING {0}".format(i)) # pylint: disable=cell-var-from-loop pop = PeriodicOperation("test_operation", operation, period=datetime.timedelta(hours=1)) for _ in range(5): @@ -133,7 +133,7 @@ def test_sleep_until_next_operation_should_wait_for_the_closest_operation(self): PeriodicOperation("one", lambda: None, period=datetime.timedelta(minutes=11)), PeriodicOperation("one", lambda: None, period=datetime.timedelta(days=1)) ] - for op in operations: + for op in operations: # pylint: disable=invalid-name op.run() def mock_sleep(seconds): diff --git a/tests/ga/test_remoteaccess.py b/tests/ga/test_remoteaccess.py index 0cc394fa5e..e060f3e107 100644 --- a/tests/ga/test_remoteaccess.py +++ b/tests/ga/test_remoteaccess.py @@ -16,8 +16,8 @@ # import xml -from azurelinuxagent.common.protocol.goal_state import GoalState, RemoteAccess -from tests.tools import AgentTestCase, load_data, patch, Mock +from azurelinuxagent.common.protocol.goal_state import GoalState, RemoteAccess # pylint: disable=unused-import +from tests.tools import AgentTestCase, load_data, patch, Mock # pylint: disable=unused-import from tests.protocol import mockwiredata from tests.protocol.mocks import mock_wire_protocol @@ -26,12 +26,12 @@ class TestRemoteAccess(AgentTestCase): def test_parse_remote_access(self): data_str = load_data('wire/remote_access_single_account.xml') remote_access = RemoteAccess(data_str) - self.assertNotEquals(None, remote_access) - self.assertEquals("1", remote_access.incarnation) - self.assertEquals(1, len(remote_access.user_list.users), "User count does not match.") - self.assertEquals("testAccount", remote_access.user_list.users[0].name, "Account name does not match") - self.assertEquals("encryptedPasswordString", remote_access.user_list.users[0].encrypted_password, "Encrypted password does not match.") - self.assertEquals("2019-01-01", remote_access.user_list.users[0].expiration, "Expiration does not match.") + self.assertNotEquals(None, remote_access) # pylint: disable=deprecated-method + self.assertEquals("1", remote_access.incarnation) # pylint: disable=deprecated-method + self.assertEquals(1, len(remote_access.user_list.users), "User count does not match.") # pylint: disable=deprecated-method + self.assertEquals("testAccount", remote_access.user_list.users[0].name, "Account name does not match") # pylint: disable=deprecated-method + self.assertEquals("encryptedPasswordString", remote_access.user_list.users[0].encrypted_password, "Encrypted password does not match.") # pylint: disable=deprecated-method + self.assertEquals("2019-01-01", remote_access.user_list.users[0].expiration, "Expiration does not match.") # pylint: disable=deprecated-method def test_goal_state_with_no_remote_access(self): with mock_wire_protocol(mockwiredata.DATA_FILE) as protocol: @@ -40,46 +40,46 @@ def test_goal_state_with_no_remote_access(self): def test_parse_two_remote_access_accounts(self): data_str = load_data('wire/remote_access_two_accounts.xml') remote_access = RemoteAccess(data_str) - self.assertNotEquals(None, remote_access) - self.assertEquals("1", remote_access.incarnation) - self.assertEquals(2, len(remote_access.user_list.users), "User count does not match.") - self.assertEquals("testAccount1", remote_access.user_list.users[0].name, "Account name does not match") - self.assertEquals("encryptedPasswordString", remote_access.user_list.users[0].encrypted_password, "Encrypted password does not match.") - self.assertEquals("2019-01-01", remote_access.user_list.users[0].expiration, "Expiration does not match.") - self.assertEquals("testAccount2", remote_access.user_list.users[1].name, "Account name does not match") - self.assertEquals("encryptedPasswordString", remote_access.user_list.users[1].encrypted_password, "Encrypted password does not match.") - self.assertEquals("2019-01-01", remote_access.user_list.users[1].expiration, "Expiration does not match.") + self.assertNotEquals(None, remote_access) # pylint: disable=deprecated-method + self.assertEquals("1", remote_access.incarnation) # pylint: disable=deprecated-method + self.assertEquals(2, len(remote_access.user_list.users), "User count does not match.") # pylint: disable=deprecated-method + self.assertEquals("testAccount1", remote_access.user_list.users[0].name, "Account name does not match") # pylint: disable=deprecated-method + self.assertEquals("encryptedPasswordString", remote_access.user_list.users[0].encrypted_password, "Encrypted password does not match.") # pylint: disable=deprecated-method + self.assertEquals("2019-01-01", remote_access.user_list.users[0].expiration, "Expiration does not match.") # pylint: disable=deprecated-method + self.assertEquals("testAccount2", remote_access.user_list.users[1].name, "Account name does not match") # pylint: disable=deprecated-method + self.assertEquals("encryptedPasswordString", remote_access.user_list.users[1].encrypted_password, "Encrypted password does not match.") # pylint: disable=deprecated-method + self.assertEquals("2019-01-01", remote_access.user_list.users[1].expiration, "Expiration does not match.") # pylint: disable=deprecated-method def test_parse_ten_remote_access_accounts(self): data_str = load_data('wire/remote_access_10_accounts.xml') remote_access = RemoteAccess(data_str) - self.assertNotEquals(None, remote_access) - self.assertEquals(10, len(remote_access.user_list.users), "User count does not match.") + self.assertNotEquals(None, remote_access) # pylint: disable=deprecated-method + self.assertEquals(10, len(remote_access.user_list.users), "User count does not match.") # pylint: disable=deprecated-method def test_parse_duplicate_remote_access_accounts(self): data_str = load_data('wire/remote_access_duplicate_accounts.xml') remote_access = RemoteAccess(data_str) - self.assertNotEquals(None, remote_access) - self.assertEquals(2, len(remote_access.user_list.users), "User count does not match.") - self.assertEquals("testAccount", remote_access.user_list.users[0].name, "Account name does not match") - self.assertEquals("encryptedPasswordString", remote_access.user_list.users[0].encrypted_password, "Encrypted password does not match.") - self.assertEquals("2019-01-01", remote_access.user_list.users[0].expiration, "Expiration does not match.") - self.assertEquals("testAccount", remote_access.user_list.users[1].name, "Account name does not match") - self.assertEquals("encryptedPasswordString", remote_access.user_list.users[1].encrypted_password, "Encrypted password does not match.") - self.assertEquals("2019-01-01", remote_access.user_list.users[1].expiration, "Expiration does not match.") + self.assertNotEquals(None, remote_access) # pylint: disable=deprecated-method + self.assertEquals(2, len(remote_access.user_list.users), "User count does not match.") # pylint: disable=deprecated-method + self.assertEquals("testAccount", remote_access.user_list.users[0].name, "Account name does not match") # pylint: disable=deprecated-method + self.assertEquals("encryptedPasswordString", remote_access.user_list.users[0].encrypted_password, "Encrypted password does not match.") # pylint: disable=deprecated-method + self.assertEquals("2019-01-01", remote_access.user_list.users[0].expiration, "Expiration does not match.") # pylint: disable=deprecated-method + self.assertEquals("testAccount", remote_access.user_list.users[1].name, "Account name does not match") # pylint: disable=deprecated-method + self.assertEquals("encryptedPasswordString", remote_access.user_list.users[1].encrypted_password, "Encrypted password does not match.") # pylint: disable=deprecated-method + self.assertEquals("2019-01-01", remote_access.user_list.users[1].expiration, "Expiration does not match.") # pylint: disable=deprecated-method def test_parse_zero_remote_access_accounts(self): data_str = load_data('wire/remote_access_no_accounts.xml') remote_access = RemoteAccess(data_str) - self.assertNotEquals(None, remote_access) - self.assertEquals(0, len(remote_access.user_list.users), "User count does not match.") + self.assertNotEquals(None, remote_access) # pylint: disable=deprecated-method + self.assertEquals(0, len(remote_access.user_list.users), "User count does not match.") # pylint: disable=deprecated-method def test_update_remote_access_conf_remote_access(self): with mock_wire_protocol(mockwiredata.DATA_FILE_REMOTE_ACCESS) as protocol: self.assertIsNotNone(protocol.client.get_remote_access()) - self.assertEquals(1, len(protocol.client.get_remote_access().user_list.users)) - self.assertEquals('testAccount', protocol.client.get_remote_access().user_list.users[0].name) - self.assertEquals('encryptedPasswordString', protocol.client.get_remote_access().user_list.users[0].encrypted_password) + self.assertEquals(1, len(protocol.client.get_remote_access().user_list.users)) # pylint: disable=deprecated-method + self.assertEquals('testAccount', protocol.client.get_remote_access().user_list.users[0].name) # pylint: disable=deprecated-method + self.assertEquals('encryptedPasswordString', protocol.client.get_remote_access().user_list.users[0].encrypted_password) # pylint: disable=deprecated-method def test_parse_bad_remote_access_data(self): data = "foobar" diff --git a/tests/ga/test_remoteaccess_handler.py b/tests/ga/test_remoteaccess_handler.py index 77fd82b6f9..0c1b838ef6 100644 --- a/tests/ga/test_remoteaccess_handler.py +++ b/tests/ga/test_remoteaccess_handler.py @@ -28,7 +28,7 @@ class MockOSUtil(DefaultOSUtil): - def __init__(self): + def __init__(self): # pylint: disable=super-init-not-called self.all_users = {} self.sudo_users = set() self.jit_enabled = True @@ -70,11 +70,11 @@ def get_user_dictionary(users): return user_dictionary -def mock_add_event(name, op, is_success, version, message): +def mock_add_event(name, op, is_success, version, message): # pylint: disable=invalid-name TestRemoteAccessHandler.eventing_data = (name, op, is_success, version, message) -class TestRemoteAccessHandler(AgentTestCase): +class TestRemoteAccessHandler(AgentTestCase): # pylint: disable=too-many-public-methods eventing_data = [()] def setUp(self): @@ -94,8 +94,8 @@ def test_add_user(self, *_): tstuser = "foobar" expiration_date = datetime.utcnow() + timedelta(days=1) pwd = tstpassword - rah._add_user(tstuser, pwd, expiration_date) - users = get_user_dictionary(rah._os_util.get_users()) + rah._add_user(tstuser, pwd, expiration_date) # pylint: disable=protected-access + users = get_user_dictionary(rah._os_util.get_users()) # pylint: disable=protected-access self.assertTrue(tstuser in users, "{0} missing from users".format(tstuser)) actual_user = users[tstuser] expected_expiration = (expiration_date + timedelta(days=1)).strftime("%Y-%m-%d") @@ -111,8 +111,8 @@ def test_add_user_bad_creation_data(self, *_): expiration = datetime.utcnow() + timedelta(days=1) pwd = tstpassword error = "test exception for bad username" - self.assertRaisesRegex(Exception, error, rah._add_user, tstuser, pwd, expiration) - self.assertEqual(0, len(rah._os_util.get_users())) + self.assertRaisesRegex(Exception, error, rah._add_user, tstuser, pwd, expiration) # pylint: disable=protected-access + self.assertEqual(0, len(rah._os_util.get_users())) # pylint: disable=protected-access @patch('azurelinuxagent.common.utils.cryptutil.CryptUtil.decrypt_secret', return_value="") def test_add_user_bad_password_data(self, *_): @@ -123,8 +123,8 @@ def test_add_user_bad_password_data(self, *_): expiration = datetime.utcnow() + timedelta(days=1) pwd = tstpassword error = "test exception for bad password" - self.assertRaisesRegex(Exception, error, rah._add_user, tstuser, pwd, expiration) - self.assertEqual(0, len(rah._os_util.get_users())) + self.assertRaisesRegex(Exception, error, rah._add_user, tstuser, pwd, expiration) # pylint: disable=protected-access + self.assertEqual(0, len(rah._os_util.get_users())) # pylint: disable=protected-access @patch('azurelinuxagent.common.utils.cryptutil.CryptUtil.decrypt_secret', return_value="]aPPEv}uNg1FPnl?") def test_add_user_already_existing(self, _): @@ -134,8 +134,8 @@ def test_add_user_already_existing(self, _): tstuser = "foobar" expiration_date = datetime.utcnow() + timedelta(days=1) pwd = tstpassword - rah._add_user(tstuser, pwd, expiration_date) - users = get_user_dictionary(rah._os_util.get_users()) + rah._add_user(tstuser, pwd, expiration_date) # pylint: disable=protected-access + users = get_user_dictionary(rah._os_util.get_users()) # pylint: disable=protected-access self.assertTrue(tstuser in users, "{0} missing from users".format(tstuser)) self.assertEqual(1, len(users.keys())) actual_user = users[tstuser] @@ -144,9 +144,9 @@ def test_add_user_already_existing(self, _): # this does not test the user add function as that's mocked, it tests processing skips the remaining # calls after the initial failure new_user_expiration = datetime.utcnow() + timedelta(days=5) - self.assertRaises(Exception, rah._add_user, tstuser, pwd, new_user_expiration) + self.assertRaises(Exception, rah._add_user, tstuser, pwd, new_user_expiration) # pylint: disable=protected-access # refresh users - users = get_user_dictionary(rah._os_util.get_users()) + users = get_user_dictionary(rah._os_util.get_users()) # pylint: disable=protected-access self.assertTrue(tstuser in users, "{0} missing from users after dup user attempted".format(tstuser)) self.assertEqual(1, len(users.keys())) actual_user = users[tstuser] @@ -160,14 +160,14 @@ def test_delete_user(self, *_): tstpassword = "]aPPEv}uNg1FPnl?" tstuser = "foobar" expiration_date = datetime.utcnow() + timedelta(days=1) - expected_expiration = (expiration_date + timedelta(days=1)).strftime("%Y-%m-%d") + expected_expiration = (expiration_date + timedelta(days=1)).strftime("%Y-%m-%d") # pylint: disable=unused-variable pwd = tstpassword - rah._add_user(tstuser, pwd, expiration_date) - users = get_user_dictionary(rah._os_util.get_users()) + rah._add_user(tstuser, pwd, expiration_date) # pylint: disable=protected-access + users = get_user_dictionary(rah._os_util.get_users()) # pylint: disable=protected-access self.assertTrue(tstuser in users, "{0} missing from users".format(tstuser)) - rah._remove_user(tstuser) + rah._remove_user(tstuser) # pylint: disable=protected-access # refresh users - users = get_user_dictionary(rah._os_util.get_users()) + users = get_user_dictionary(rah._os_util.get_users()) # pylint: disable=protected-access self.assertFalse(tstuser in users) @patch('azurelinuxagent.common.utils.cryptutil.CryptUtil.decrypt_secret', return_value="]aPPEv}uNg1FPnl?") @@ -180,9 +180,9 @@ def test_handle_new_user(self, _): expiration_date = datetime.utcnow() + timedelta(days=1) expiration = expiration_date.strftime("%a, %d %b %Y %H:%M:%S ") + "UTC" remote_access.user_list.users[0].expiration = expiration - rah._remote_access = remote_access - rah._handle_remote_access() - users = get_user_dictionary(rah._os_util.get_users()) + rah._remote_access = remote_access # pylint: disable=protected-access + rah._handle_remote_access() # pylint: disable=protected-access + users = get_user_dictionary(rah._os_util.get_users()) # pylint: disable=protected-access self.assertTrue(tstuser in users, "{0} missing from users".format(tstuser)) actual_user = users[tstuser] expected_expiration = (expiration_date + timedelta(days=1)).strftime("%Y-%m-%d") @@ -196,9 +196,9 @@ def test_do_not_add_expired_user(self): remote_access = RemoteAccess(data_str) expiration = (datetime.utcnow() - timedelta(days=2)).strftime("%a, %d %b %Y %H:%M:%S ") + "UTC" remote_access.user_list.users[0].expiration = expiration - rah._remote_access = remote_access - rah._handle_remote_access() - users = get_user_dictionary(rah._os_util.get_users()) + rah._remote_access = remote_access # pylint: disable=protected-access + rah._handle_remote_access() # pylint: disable=protected-access + users = get_user_dictionary(rah._os_util.get_users()) # pylint: disable=protected-access self.assertFalse("testAccount" in users) def test_error_add_user(self): @@ -207,9 +207,9 @@ def test_error_add_user(self): tstuser = "foobar" expiration = datetime.utcnow() + timedelta(days=1) pwd = "bad password" - error = "\[CryptError\] Error decoding secret\nInner error: Incorrect padding" - self.assertRaisesRegex(Exception, error, rah._add_user, tstuser, pwd, expiration) - users = get_user_dictionary(rah._os_util.get_users()) + error = "\[CryptError\] Error decoding secret\nInner error: Incorrect padding" # pylint: disable=anomalous-backslash-in-string + self.assertRaisesRegex(Exception, error, rah._add_user, tstuser, pwd, expiration) # pylint: disable=protected-access + users = get_user_dictionary(rah._os_util.get_users()) # pylint: disable=protected-access self.assertEqual(0, len(users)) def test_handle_remote_access_no_users(self): @@ -217,15 +217,15 @@ def test_handle_remote_access_no_users(self): rah = RemoteAccessHandler(Mock()) data_str = load_data('wire/remote_access_no_accounts.xml') remote_access = RemoteAccess(data_str) - rah._remote_access = remote_access - rah._handle_remote_access() - users = get_user_dictionary(rah._os_util.get_users()) + rah._remote_access = remote_access # pylint: disable=protected-access + rah._handle_remote_access() # pylint: disable=protected-access + users = get_user_dictionary(rah._os_util.get_users()) # pylint: disable=protected-access self.assertEqual(0, len(users.keys())) def test_handle_remote_access_validate_jit_user_valid(self): rah = RemoteAccessHandler(Mock()) comment = "JIT_Account" - result = rah._is_jit_user(comment) + result = rah._is_jit_user(comment) # pylint: disable=protected-access self.assertTrue(result, "Did not identify '{0}' as a JIT_Account".format(comment)) def test_handle_remote_access_validate_jit_user_invalid(self): @@ -233,9 +233,9 @@ def test_handle_remote_access_validate_jit_user_invalid(self): test_users = ["John Doe", None, "", " "] failed_results = "" for user in test_users: - if rah._is_jit_user(user): + if rah._is_jit_user(user): # pylint: disable=protected-access failed_results += "incorrectly identified '{0} as a JIT_Account'. ".format(user) - if len(failed_results) > 0: + if len(failed_results) > 0: # pylint: disable=len-as-condition self.fail(failed_results) @patch('azurelinuxagent.common.utils.cryptutil.CryptUtil.decrypt_secret', return_value="]aPPEv}uNg1FPnl?") @@ -253,9 +253,9 @@ def test_handle_remote_access_multiple_users(self, _): remote_access.user_list.users[count].expiration = expiration testusers.append(user) count += 1 - rah._remote_access = remote_access - rah._handle_remote_access() - users = get_user_dictionary(rah._os_util.get_users()) + rah._remote_access = remote_access # pylint: disable=protected-access + rah._handle_remote_access() # pylint: disable=protected-access + users = get_user_dictionary(rah._os_util.get_users()) # pylint: disable=protected-access self.assertTrue(testusers[0] in users, "{0} missing from users".format(testusers[0])) self.assertTrue(testusers[1] in users, "{0} missing from users".format(testusers[1])) @@ -272,9 +272,9 @@ def test_handle_remote_access_ten_users(self, _): user.name = "tstuser{0}".format(count) expiration_date = datetime.utcnow() + timedelta(days=count) user.expiration = expiration_date.strftime("%a, %d %b %Y %H:%M:%S ") + "UTC" - rah._remote_access = remote_access - rah._handle_remote_access() - users = get_user_dictionary(rah._os_util.get_users()) + rah._remote_access = remote_access # pylint: disable=protected-access + rah._handle_remote_access() # pylint: disable=protected-access + users = get_user_dictionary(rah._os_util.get_users()) # pylint: disable=protected-access self.assertEqual(10, len(users.keys())) @patch('azurelinuxagent.common.utils.cryptutil.CryptUtil.decrypt_secret', return_value="]aPPEv}uNg1FPnl?") @@ -289,11 +289,11 @@ def test_handle_remote_access_user_removed(self, _): user.name = "tstuser{0}".format(count) expiration_date = datetime.utcnow() + timedelta(days=count) user.expiration = expiration_date.strftime("%a, %d %b %Y %H:%M:%S ") + "UTC" - rah._remote_access = remote_access - rah._handle_remote_access() - users = get_user_dictionary(rah._os_util.get_users()) + rah._remote_access = remote_access # pylint: disable=protected-access + rah._handle_remote_access() # pylint: disable=protected-access + users = get_user_dictionary(rah._os_util.get_users()) # pylint: disable=protected-access self.assertEqual(10, len(users.keys())) - del rah._remote_access.user_list.users[:] + del rah._remote_access.user_list.users[:] # pylint: disable=protected-access self.assertEqual(10, len(users.keys())) @patch('azurelinuxagent.common.utils.cryptutil.CryptUtil.decrypt_secret', return_value="]aPPEv}uNg1FPnl?") @@ -310,9 +310,9 @@ def test_handle_remote_access_bad_data_and_good_data(self, _): user.name = "" expiration_date = datetime.utcnow() + timedelta(days=count) user.expiration = expiration_date.strftime("%a, %d %b %Y %H:%M:%S ") + "UTC" - rah._remote_access = remote_access - rah._handle_remote_access() - users = get_user_dictionary(rah._os_util.get_users()) + rah._remote_access = remote_access # pylint: disable=protected-access + rah._handle_remote_access() # pylint: disable=protected-access + users = get_user_dictionary(rah._os_util.get_users()) # pylint: disable=protected-access self.assertEqual(9, len(users.keys())) @patch('azurelinuxagent.common.utils.cryptutil.CryptUtil.decrypt_secret', return_value="]aPPEv}uNg1FPnl?") @@ -325,19 +325,19 @@ def test_handle_remote_access_deleted_user_readded(self, _): expiration_date = datetime.utcnow() + timedelta(days=1) expiration = expiration_date.strftime("%a, %d %b %Y %H:%M:%S ") + "UTC" remote_access.user_list.users[0].expiration = expiration - rah._remote_access = remote_access - rah._handle_remote_access() - users = get_user_dictionary(rah._os_util.get_users()) + rah._remote_access = remote_access # pylint: disable=protected-access + rah._handle_remote_access() # pylint: disable=protected-access + users = get_user_dictionary(rah._os_util.get_users()) # pylint: disable=protected-access self.assertTrue(tstuser in users, "{0} missing from users".format(tstuser)) - os_util = rah._os_util + os_util = rah._os_util # pylint: disable=protected-access os_util.__class__ = MockOSUtil - os_util.all_users.clear() + os_util.all_users.clear() # pylint: disable=no-member # refresh users - users = get_user_dictionary(rah._os_util.get_users()) + users = get_user_dictionary(rah._os_util.get_users()) # pylint: disable=protected-access self.assertTrue(tstuser not in users) - rah._handle_remote_access() + rah._handle_remote_access() # pylint: disable=protected-access # refresh users - users = get_user_dictionary(rah._os_util.get_users()) + users = get_user_dictionary(rah._os_util.get_users()) # pylint: disable=protected-access self.assertTrue(tstuser in users, "{0} missing from users".format(tstuser)) @patch('azurelinuxagent.common.utils.cryptutil.CryptUtil.decrypt_secret', return_value="]aPPEv}uNg1FPnl?") @@ -352,8 +352,8 @@ def test_remote_access_handler_run_bad_data(self, _1, _2, _3, _4, _5): tstuser = "foobar" expiration_date = datetime.utcnow() + timedelta(days=1) pwd = tstpassword - rah._add_user(tstuser, pwd, expiration_date) - users = get_user_dictionary(rah._os_util.get_users()) + rah._add_user(tstuser, pwd, expiration_date) # pylint: disable=protected-access + users = get_user_dictionary(rah._os_util.get_users()) # pylint: disable=protected-access self.assertTrue(tstuser in users, "{0} missing from users".format(tstuser)) rah.run() self.assertTrue(tstuser in users, "{0} missing from users".format(tstuser)) @@ -370,15 +370,15 @@ def test_handle_remote_access_multiple_users_one_removed(self, _): user.name = "tstuser{0}".format(count) expiration_date = datetime.utcnow() + timedelta(days=count) user.expiration = expiration_date.strftime("%a, %d %b %Y %H:%M:%S ") + "UTC" - rah._remote_access = remote_access - rah._handle_remote_access() - users = rah._os_util.get_users() + rah._remote_access = remote_access # pylint: disable=protected-access + rah._handle_remote_access() # pylint: disable=protected-access + users = rah._os_util.get_users() # pylint: disable=protected-access self.assertEqual(10, len(users)) # now remove the user from RemoteAccess - deleted_user = rah._remote_access.user_list.users[3] - del rah._remote_access.user_list.users[3] - rah._handle_remote_access() - users = rah._os_util.get_users() + deleted_user = rah._remote_access.user_list.users[3] # pylint: disable=protected-access + del rah._remote_access.user_list.users[3] # pylint: disable=protected-access + rah._handle_remote_access() # pylint: disable=protected-access + users = rah._os_util.get_users() # pylint: disable=protected-access self.assertTrue(deleted_user not in users, "{0} still in users".format(deleted_user)) self.assertEqual(9, len(users)) @@ -394,14 +394,14 @@ def test_handle_remote_access_multiple_users_null_remote_access(self, _): user.name = "tstuser{0}".format(count) expiration_date = datetime.utcnow() + timedelta(days=count) user.expiration = expiration_date.strftime("%a, %d %b %Y %H:%M:%S ") + "UTC" - rah._remote_access = remote_access - rah._handle_remote_access() - users = rah._os_util.get_users() + rah._remote_access = remote_access # pylint: disable=protected-access + rah._handle_remote_access() # pylint: disable=protected-access + users = rah._os_util.get_users() # pylint: disable=protected-access self.assertEqual(10, len(users)) # now remove the user from RemoteAccess - rah._remote_access = None - rah._handle_remote_access() - users = rah._os_util.get_users() + rah._remote_access = None # pylint: disable=protected-access + rah._handle_remote_access() # pylint: disable=protected-access + users = rah._os_util.get_users() # pylint: disable=protected-access self.assertEqual(0, len(users)) @patch('azurelinuxagent.common.utils.cryptutil.CryptUtil.decrypt_secret', return_value="]aPPEv}uNg1FPnl?") @@ -416,28 +416,28 @@ def test_handle_remote_access_multiple_users_error_with_null_remote_access(self, user.name = "tstuser{0}".format(count) expiration_date = datetime.utcnow() + timedelta(days=count) user.expiration = expiration_date.strftime("%a, %d %b %Y %H:%M:%S ") + "UTC" - rah._remote_access = remote_access - rah._handle_remote_access() - users = rah._os_util.get_users() + rah._remote_access = remote_access # pylint: disable=protected-access + rah._handle_remote_access() # pylint: disable=protected-access + users = rah._os_util.get_users() # pylint: disable=protected-access self.assertEqual(10, len(users)) # now remove the user from RemoteAccess - rah._remote_access = None - rah._handle_remote_access() - users = rah._os_util.get_users() + rah._remote_access = None # pylint: disable=protected-access + rah._handle_remote_access() # pylint: disable=protected-access + users = rah._os_util.get_users() # pylint: disable=protected-access self.assertEqual(0, len(users)) def test_remove_user_error(self): with patch("azurelinuxagent.ga.remoteaccess.get_osutil", return_value=MockOSUtil()): rah = RemoteAccessHandler(Mock()) error = "test exception, bad data" - self.assertRaisesRegex(Exception, error, rah._remove_user, "") + self.assertRaisesRegex(Exception, error, rah._remove_user, "") # pylint: disable=protected-access def test_remove_user_not_exists(self): with patch("azurelinuxagent.ga.remoteaccess.get_osutil", return_value=MockOSUtil()): rah = RemoteAccessHandler(Mock()) user = "bob" error = "test exception, user does not exist to delete" - self.assertRaisesRegex(Exception, error, rah._remove_user, user) + self.assertRaisesRegex(Exception, error, rah._remove_user, user) # pylint: disable=protected-access @patch('azurelinuxagent.common.utils.cryptutil.CryptUtil.decrypt_secret', return_value="]aPPEv}uNg1FPnl?") def test_handle_remote_access_remove_and_add(self, _): @@ -451,16 +451,16 @@ def test_handle_remote_access_remove_and_add(self, _): user.name = "tstuser{0}".format(count) expiration_date = datetime.utcnow() + timedelta(days=count) user.expiration = expiration_date.strftime("%a, %d %b %Y %H:%M:%S ") + "UTC" - rah._remote_access = remote_access - rah._handle_remote_access() - users = rah._os_util.get_users() + rah._remote_access = remote_access # pylint: disable=protected-access + rah._handle_remote_access() # pylint: disable=protected-access + users = rah._os_util.get_users() # pylint: disable=protected-access self.assertEqual(10, len(users)) # now remove the user from RemoteAccess new_user = "tstuser11" - deleted_user = rah._remote_access.user_list.users[3] - rah._remote_access.user_list.users[3].name = new_user - rah._handle_remote_access() - users = rah._os_util.get_users() + deleted_user = rah._remote_access.user_list.users[3] # pylint: disable=protected-access + rah._remote_access.user_list.users[3].name = new_user # pylint: disable=protected-access + rah._handle_remote_access() # pylint: disable=protected-access + users = rah._os_util.get_users() # pylint: disable=protected-access self.assertTrue(deleted_user not in users, "{0} still in users".format(deleted_user)) self.assertTrue(new_user in [u[0] for u in users], "user {0} not in users".format(new_user)) self.assertEqual(10, len(users)) diff --git a/tests/ga/test_update.py b/tests/ga/test_update.py index 21987ddc59..60629e99ca 100644 --- a/tests/ga/test_update.py +++ b/tests/ga/test_update.py @@ -1,4 +1,4 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. +# Copyright (c) Microsoft Corporation. All rights reserved. # pylint: disable=too-many-lines # Licensed under the Apache License. from __future__ import print_function @@ -143,24 +143,24 @@ def agent_pkgs(self): return get_agent_pkgs(in_dir=self.tmp_dir) def agent_versions(self): - v = [FlexibleVersion(AGENT_DIR_PATTERN.match(a).group(1)) for a in self.agent_dirs()] + v = [FlexibleVersion(AGENT_DIR_PATTERN.match(a).group(1)) for a in self.agent_dirs()] # pylint: disable=invalid-name v.sort(reverse=True) return v - def get_error_file(self, error_data=NO_ERROR): - fp = tempfile.NamedTemporaryFile(mode="w") + def get_error_file(self, error_data=NO_ERROR): # pylint: disable=dangerous-default-value + fp = tempfile.NamedTemporaryFile(mode="w") # pylint: disable=invalid-name json.dump(error_data if error_data is not None else NO_ERROR, fp) fp.seek(0) return fp - def create_error(self, error_data=NO_ERROR): + def create_error(self, error_data=NO_ERROR): # pylint: disable=dangerous-default-value with self.get_error_file(error_data) as path: err = GuestAgentError(path.name) err.load() return err - def copy_agents(self, *agents): - if len(agents) <= 0: + def copy_agents(self, *agents): # pylint: disable=useless-return + if len(agents) <= 0: # pylint: disable=len-as-condition agents = get_agent_pkgs() for agent in agents: shutil.copy(agent, self.tmp_dir) @@ -171,7 +171,7 @@ def expand_agents(self): path = os.path.join(self.tmp_dir, fileutil.trim_ext(agent, "zip")) zipfile.ZipFile(agent).extractall(path) - def prepare_agent(self, version): + def prepare_agent(self, version): # pylint: disable=useless-return """ Create a download for the current agent version, copied from test data """ @@ -212,14 +212,14 @@ def prepare_agents(self, count=count - agent_count, is_available=is_available) - def remove_agents(self): + def remove_agents(self): # pylint: disable=useless-return for agent in self.agent_paths(): try: if os.path.isfile(agent): os.remove(agent) else: shutil.rmtree(agent) - except: + except: # pylint: disable=bare-except pass return @@ -230,7 +230,7 @@ def replicate_agents(self, increment=1): from_path = self.agent_dir(src_v) dst_v = FlexibleVersion(str(src_v)) - for i in range(0, count): + for i in range(0, count): # pylint: disable=unused-variable dst_v += increment to_path = self.agent_dir(dst_v) shutil.copyfile(from_path + ".zip", to_path + ".zip") @@ -242,7 +242,7 @@ def replicate_agents(self, class TestGuestAgentError(UpdateTestCase): - def test_creation(self): + def test_creation(self): # pylint: disable=useless-return self.assertRaises(TypeError, GuestAgentError) self.assertRaises(UpdateError, GuestAgentError, None) @@ -257,7 +257,7 @@ def test_creation(self): self.assertEqual(WITH_ERROR["was_fatal"], err.was_fatal) return - def test_clear(self): + def test_clear(self): # pylint: disable=useless-return with self.get_error_file(error_data=WITH_ERROR) as path: err = GuestAgentError(path.name) err.load() @@ -280,11 +280,11 @@ def test_save(self): self.assertEqual(err1.failure_count, err2.failure_count) self.assertEqual(err1.was_fatal, err2.was_fatal) - def test_mark_failure(self): + def test_mark_failure(self): # pylint: disable=useless-return err = self.create_error() self.assertFalse(err.is_blacklisted) - for i in range(0, MAX_FAILURE): + for i in range(0, MAX_FAILURE): # pylint: disable=unused-variable err.mark_failure() # Agent failed >= MAX_FAILURE, it should be blacklisted @@ -292,7 +292,7 @@ def test_mark_failure(self): self.assertEqual(MAX_FAILURE, err.failure_count) return - def test_mark_failure_permanent(self): + def test_mark_failure_permanent(self): # pylint: disable=useless-return err = self.create_error() self.assertFalse(err.is_blacklisted) @@ -303,16 +303,16 @@ def test_mark_failure_permanent(self): self.assertTrue(err.failure_count < MAX_FAILURE) return - def test_str(self): + def test_str(self): # pylint: disable=useless-return err = self.create_error(error_data=NO_ERROR) - s = "Last Failure: {0}, Total Failures: {1}, Fatal: {2}".format( + s = "Last Failure: {0}, Total Failures: {1}, Fatal: {2}".format( # pylint: disable=invalid-name NO_ERROR["last_failure"], NO_ERROR["failure_count"], NO_ERROR["was_fatal"]) self.assertEqual(s, str(err)) err = self.create_error(error_data=WITH_ERROR) - s = "Last Failure: {0}, Total Failures: {1}, Fatal: {2}".format( + s = "Last Failure: {0}, Total Failures: {1}, Fatal: {2}".format( # pylint: disable=invalid-name WITH_ERROR["last_failure"], WITH_ERROR["failure_count"], WITH_ERROR["was_fatal"]) @@ -320,7 +320,7 @@ def test_str(self): return -class TestGuestAgent(UpdateTestCase): +class TestGuestAgent(UpdateTestCase): # pylint: disable=too-many-public-methods def setUp(self): UpdateTestCase.setUp(self) self.copy_agents(get_agent_file_path()) @@ -328,7 +328,7 @@ def setUp(self): def test_creation(self): self.assertRaises(UpdateError, GuestAgent, "A very bad file name") - n = "{0}-a.bad.version".format(AGENT_NAME) + n = "{0}-a.bad.version".format(AGENT_NAME) # pylint: disable=invalid-name self.assertRaises(UpdateError, GuestAgent, n) self.expand_agents() @@ -355,7 +355,7 @@ def test_creation(self): self.assertTrue(agent.is_available) @patch("azurelinuxagent.ga.update.GuestAgent._ensure_downloaded") - def test_clear_error(self, mock_downloaded): + def test_clear_error(self, mock_downloaded): # pylint: disable=unused-argument self.expand_agents() agent = GuestAgent(path=self.agent_path) @@ -374,11 +374,11 @@ def test_clear_error(self, mock_downloaded): @patch("azurelinuxagent.ga.update.GuestAgent._ensure_downloaded") @patch("azurelinuxagent.ga.update.GuestAgent._ensure_loaded") - def test_is_available(self, mock_loaded, mock_downloaded): + def test_is_available(self, mock_loaded, mock_downloaded): # pylint: disable=unused-argument agent = GuestAgent(path=self.agent_path) self.assertFalse(agent.is_available) - agent._unpack() + agent._unpack() # pylint: disable=protected-access self.assertTrue(agent.is_available) agent.mark_failure(is_fatal=True) @@ -386,11 +386,11 @@ def test_is_available(self, mock_loaded, mock_downloaded): @patch("azurelinuxagent.ga.update.GuestAgent._ensure_downloaded") @patch("azurelinuxagent.ga.update.GuestAgent._ensure_loaded") - def test_is_blacklisted(self, mock_loaded, mock_downloaded): + def test_is_blacklisted(self, mock_loaded, mock_downloaded): # pylint: disable=unused-argument agent = GuestAgent(path=self.agent_path) self.assertFalse(agent.is_blacklisted) - agent._unpack() + agent._unpack() # pylint: disable=protected-access self.assertFalse(agent.is_blacklisted) self.assertEqual(agent.is_blacklisted, agent.error.is_blacklisted) @@ -400,39 +400,39 @@ def test_is_blacklisted(self, mock_loaded, mock_downloaded): @patch("azurelinuxagent.ga.update.GuestAgent._ensure_downloaded") @patch("azurelinuxagent.ga.update.GuestAgent._ensure_loaded") - def test_resource_gone_error_not_blacklisted(self, mock_loaded, mock_downloaded): + def test_resource_gone_error_not_blacklisted(self, mock_loaded, mock_downloaded): # pylint: disable=unused-argument try: mock_downloaded.side_effect = ResourceGoneError() agent = GuestAgent(path=self.agent_path) self.assertFalse(agent.is_blacklisted) except ResourceGoneError: pass - except: + except: # pylint: disable=bare-except self.fail("Exception was not expected!") @patch("azurelinuxagent.ga.update.GuestAgent._ensure_downloaded") @patch("azurelinuxagent.ga.update.GuestAgent._ensure_loaded") - def test_ioerror_not_blacklisted(self, mock_loaded, mock_downloaded): + def test_ioerror_not_blacklisted(self, mock_loaded, mock_downloaded): # pylint: disable=unused-argument try: mock_downloaded.side_effect = IOError() agent = GuestAgent(path=self.agent_path) self.assertFalse(agent.is_blacklisted) except IOError: pass - except: + except: # pylint: disable=bare-except self.fail("Exception was not expected!") @patch("azurelinuxagent.ga.update.GuestAgent._ensure_downloaded") @patch("azurelinuxagent.ga.update.GuestAgent._ensure_loaded") - def test_is_downloaded(self, mock_loaded, mock_downloaded): + def test_is_downloaded(self, mock_loaded, mock_downloaded): # pylint: disable=unused-argument agent = GuestAgent(path=self.agent_path) self.assertFalse(agent.is_downloaded) - agent._unpack() + agent._unpack() # pylint: disable=protected-access self.assertTrue(agent.is_downloaded) @patch("azurelinuxagent.ga.update.GuestAgent._ensure_downloaded") @patch("azurelinuxagent.ga.update.GuestAgent._ensure_loaded") - def test_mark_failure(self, mock_loaded, mock_downloaded): + def test_mark_failure(self, mock_loaded, mock_downloaded): # pylint: disable=unused-argument agent = GuestAgent(path=self.agent_path) agent.mark_failure() @@ -444,74 +444,74 @@ def test_mark_failure(self, mock_loaded, mock_downloaded): @patch("azurelinuxagent.ga.update.GuestAgent._ensure_downloaded") @patch("azurelinuxagent.ga.update.GuestAgent._ensure_loaded") - def test_unpack(self, mock_loaded, mock_downloaded): + def test_unpack(self, mock_loaded, mock_downloaded): # pylint: disable=unused-argument agent = GuestAgent(path=self.agent_path) self.assertFalse(os.path.isdir(agent.get_agent_dir())) - agent._unpack() + agent._unpack() # pylint: disable=protected-access self.assertTrue(os.path.isdir(agent.get_agent_dir())) self.assertTrue(os.path.isfile(agent.get_agent_manifest_path())) @patch("azurelinuxagent.ga.update.GuestAgent._ensure_downloaded") @patch("azurelinuxagent.ga.update.GuestAgent._ensure_loaded") - def test_unpack_fail(self, mock_loaded, mock_downloaded): + def test_unpack_fail(self, mock_loaded, mock_downloaded): # pylint: disable=unused-argument agent = GuestAgent(path=self.agent_path) self.assertFalse(os.path.isdir(agent.get_agent_dir())) os.remove(agent.get_agent_pkg_path()) - self.assertRaises(UpdateError, agent._unpack) + self.assertRaises(UpdateError, agent._unpack) # pylint: disable=protected-access @patch("azurelinuxagent.ga.update.GuestAgent._ensure_downloaded") @patch("azurelinuxagent.ga.update.GuestAgent._ensure_loaded") - def test_load_manifest(self, mock_loaded, mock_downloaded): + def test_load_manifest(self, mock_loaded, mock_downloaded): # pylint: disable=unused-argument agent = GuestAgent(path=self.agent_path) - agent._unpack() - agent._load_manifest() + agent._unpack() # pylint: disable=protected-access + agent._load_manifest() # pylint: disable=protected-access self.assertEqual(agent.manifest.get_enable_command(), agent.get_agent_cmd()) @patch("azurelinuxagent.ga.update.GuestAgent._ensure_downloaded") @patch("azurelinuxagent.ga.update.GuestAgent._ensure_loaded") - def test_load_manifest_missing(self, mock_loaded, mock_downloaded): + def test_load_manifest_missing(self, mock_loaded, mock_downloaded): # pylint: disable=unused-argument agent = GuestAgent(path=self.agent_path) self.assertFalse(os.path.isdir(agent.get_agent_dir())) - agent._unpack() + agent._unpack() # pylint: disable=protected-access os.remove(agent.get_agent_manifest_path()) - self.assertRaises(UpdateError, agent._load_manifest) + self.assertRaises(UpdateError, agent._load_manifest) # pylint: disable=protected-access @patch("azurelinuxagent.ga.update.GuestAgent._ensure_downloaded") @patch("azurelinuxagent.ga.update.GuestAgent._ensure_loaded") - def test_load_manifest_is_empty(self, mock_loaded, mock_downloaded): + def test_load_manifest_is_empty(self, mock_loaded, mock_downloaded): # pylint: disable=unused-argument agent = GuestAgent(path=self.agent_path) self.assertFalse(os.path.isdir(agent.get_agent_dir())) - agent._unpack() + agent._unpack() # pylint: disable=protected-access self.assertTrue(os.path.isfile(agent.get_agent_manifest_path())) - with open(agent.get_agent_manifest_path(), "w") as file: + with open(agent.get_agent_manifest_path(), "w") as file: # pylint: disable=redefined-builtin json.dump(EMPTY_MANIFEST, file) - self.assertRaises(UpdateError, agent._load_manifest) + self.assertRaises(UpdateError, agent._load_manifest) # pylint: disable=protected-access @patch("azurelinuxagent.ga.update.GuestAgent._ensure_downloaded") @patch("azurelinuxagent.ga.update.GuestAgent._ensure_loaded") - def test_load_manifest_is_malformed(self, mock_loaded, mock_downloaded): + def test_load_manifest_is_malformed(self, mock_loaded, mock_downloaded): # pylint: disable=unused-argument agent = GuestAgent(path=self.agent_path) self.assertFalse(os.path.isdir(agent.get_agent_dir())) - agent._unpack() + agent._unpack() # pylint: disable=protected-access self.assertTrue(os.path.isfile(agent.get_agent_manifest_path())) - with open(agent.get_agent_manifest_path(), "w") as file: + with open(agent.get_agent_manifest_path(), "w") as file: # pylint: disable=redefined-builtin file.write("This is not JSON data") - self.assertRaises(UpdateError, agent._load_manifest) + self.assertRaises(UpdateError, agent._load_manifest) # pylint: disable=protected-access def test_load_error(self): agent = GuestAgent(path=self.agent_path) agent.error = None - agent._load_error() + agent._load_error() # pylint: disable=protected-access self.assertTrue(agent.error is not None) @patch("azurelinuxagent.ga.update.GuestAgent._ensure_downloaded") @patch("azurelinuxagent.ga.update.GuestAgent._ensure_loaded") @patch("azurelinuxagent.ga.update.restutil.http_get") - def test_download(self, mock_http_get, mock_loaded, mock_downloaded): + def test_download(self, mock_http_get, mock_loaded, mock_downloaded): # pylint: disable=unused-argument self.remove_agents() self.assertFalse(os.path.isdir(self.agent_path)) @@ -521,14 +521,14 @@ def test_download(self, mock_http_get, mock_loaded, mock_downloaded): pkg = ExtHandlerPackage(version=str(get_agent_version())) pkg.uris.append(ExtHandlerPackageUri()) agent = GuestAgent(pkg=pkg) - agent._download() + agent._download() # pylint: disable=protected-access self.assertTrue(os.path.isfile(agent.get_agent_pkg_path())) @patch("azurelinuxagent.ga.update.GuestAgent._ensure_downloaded") @patch("azurelinuxagent.ga.update.GuestAgent._ensure_loaded") @patch("azurelinuxagent.ga.update.restutil.http_get") - def test_download_fail(self, mock_http_get, mock_loaded, mock_downloaded): + def test_download_fail(self, mock_http_get, mock_loaded, mock_downloaded): # pylint: disable=unused-argument self.remove_agents() self.assertFalse(os.path.isdir(self.agent_path)) @@ -538,7 +538,7 @@ def test_download_fail(self, mock_http_get, mock_loaded, mock_downloaded): pkg.uris.append(ExtHandlerPackageUri()) agent = GuestAgent(pkg=pkg) - self.assertRaises(UpdateError, agent._download) + self.assertRaises(UpdateError, agent._download) # pylint: disable=protected-access self.assertFalse(os.path.isfile(agent.get_agent_pkg_path())) self.assertFalse(agent.is_downloaded) @@ -546,7 +546,7 @@ def test_download_fail(self, mock_http_get, mock_loaded, mock_downloaded): @patch("azurelinuxagent.ga.update.GuestAgent._ensure_loaded") @patch("azurelinuxagent.ga.update.restutil.http_get") @patch("azurelinuxagent.ga.update.restutil.http_post") - def test_download_fallback(self, mock_http_post, mock_http_get, mock_loaded, mock_downloaded): + def test_download_fallback(self, mock_http_post, mock_http_get, mock_loaded, mock_downloaded): # pylint: disable=unused-argument self.remove_agents() self.assertFalse(os.path.isdir(self.agent_path)) @@ -568,7 +568,7 @@ def test_download_fallback(self, mock_http_post, mock_http_get, mock_loaded, moc agent.host = mock_host # ensure fallback fails gracefully, no http - self.assertRaises(UpdateError, agent._download) + self.assertRaises(UpdateError, agent._download) # pylint: disable=protected-access self.assertEqual(mock_http_get.call_count, 2) self.assertEqual(mock_http_get.call_args_list[0][0][0], ext_uri) self.assertEqual(mock_http_get.call_args_list[1][0][0], api_uri) @@ -577,30 +577,30 @@ def test_download_fallback(self, mock_http_post, mock_http_get, mock_loaded, moc with patch.object(HostPluginProtocol, "ensure_initialized", return_value=True): - self.assertRaises(UpdateError, agent._download) + self.assertRaises(UpdateError, agent._download) # pylint: disable=protected-access self.assertEqual(mock_http_get.call_count, 4) self.assertEqual(mock_http_get.call_args_list[2][0][0], ext_uri) self.assertEqual(mock_http_get.call_args_list[3][0][0], art_uri) - a, k = mock_http_get.call_args_list[3] + a, k = mock_http_get.call_args_list[3] # pylint: disable=unused-variable,invalid-name self.assertEqual(False, k['use_proxy']) # ensure fallback works as expected with patch.object(HostPluginProtocol, "get_artifact_request", return_value=[art_uri, {}]): - self.assertRaises(UpdateError, agent._download) + self.assertRaises(UpdateError, agent._download) # pylint: disable=protected-access self.assertEqual(mock_http_get.call_count, 6) - a, k = mock_http_get.call_args_list[3] + a, k = mock_http_get.call_args_list[3] # pylint: disable=invalid-name self.assertEqual(False, k['use_proxy']) self.assertEqual(mock_http_get.call_args_list[4][0][0], ext_uri) - a, k = mock_http_get.call_args_list[4] + a, k = mock_http_get.call_args_list[4] # pylint: disable=invalid-name self.assertEqual(mock_http_get.call_args_list[5][0][0], art_uri) - a, k = mock_http_get.call_args_list[5] + a, k = mock_http_get.call_args_list[5] # pylint: disable=invalid-name self.assertEqual(False, k['use_proxy']) @patch("azurelinuxagent.ga.update.restutil.http_get") @@ -619,7 +619,7 @@ def test_ensure_downloaded(self, mock_http_get): self.assertTrue(agent.is_downloaded) @patch("azurelinuxagent.ga.update.GuestAgent._download", side_effect=UpdateError) - def test_ensure_downloaded_download_fails(self, mock_download): + def test_ensure_downloaded_download_fails(self, mock_download): # pylint: disable=unused-argument self.remove_agents() self.assertFalse(os.path.isdir(self.agent_path)) @@ -633,7 +633,7 @@ def test_ensure_downloaded_download_fails(self, mock_download): @patch("azurelinuxagent.ga.update.GuestAgent._download") @patch("azurelinuxagent.ga.update.GuestAgent._unpack", side_effect=UpdateError) - def test_ensure_downloaded_unpack_fails(self, mock_unpack, mock_download): + def test_ensure_downloaded_unpack_fails(self, mock_unpack, mock_download): # pylint: disable=unused-argument self.assertFalse(os.path.isdir(self.agent_path)) pkg = ExtHandlerPackage(version=str(get_agent_version())) @@ -647,7 +647,7 @@ def test_ensure_downloaded_unpack_fails(self, mock_unpack, mock_download): @patch("azurelinuxagent.ga.update.GuestAgent._download") @patch("azurelinuxagent.ga.update.GuestAgent._unpack") @patch("azurelinuxagent.ga.update.GuestAgent._load_manifest", side_effect=UpdateError) - def test_ensure_downloaded_load_manifest_fails(self, mock_manifest, mock_unpack, mock_download): + def test_ensure_downloaded_load_manifest_fails(self, mock_manifest, mock_unpack, mock_download): # pylint: disable=unused-argument self.assertFalse(os.path.isdir(self.agent_path)) pkg = ExtHandlerPackage(version=str(get_agent_version())) @@ -661,7 +661,7 @@ def test_ensure_downloaded_load_manifest_fails(self, mock_manifest, mock_unpack, @patch("azurelinuxagent.ga.update.GuestAgent._download") @patch("azurelinuxagent.ga.update.GuestAgent._unpack") @patch("azurelinuxagent.ga.update.GuestAgent._load_manifest") - def test_ensure_download_skips_blacklisted(self, mock_manifest, mock_unpack, mock_download): + def test_ensure_download_skips_blacklisted(self, mock_manifest, mock_unpack, mock_download): # pylint: disable=unused-argument agent = GuestAgent(path=self.agent_path) self.assertEqual(0, mock_download.call_count) @@ -680,7 +680,7 @@ def test_ensure_download_skips_blacklisted(self, mock_manifest, mock_unpack, moc self.assertEqual(0, mock_unpack.call_count) -class TestUpdate(UpdateTestCase): +class TestUpdate(UpdateTestCase): # pylint: disable=too-many-public-methods def setUp(self): UpdateTestCase.setUp(self) self.event_patch = patch('azurelinuxagent.common.event.add_event') @@ -708,16 +708,16 @@ def test_creation(self): def test_emit_restart_event_emits_event_if_not_clean_start(self): try: mock_event = self.event_patch.start() - self.update_handler._set_sentinel() - self.update_handler._emit_restart_event() + self.update_handler._set_sentinel() # pylint: disable=protected-access + self.update_handler._emit_restart_event() # pylint: disable=protected-access self.assertEqual(1, mock_event.call_count) - except Exception as e: + except Exception as e: # pylint: disable=unused-variable,invalid-name pass self.event_patch.stop() def _create_protocol(self, count=20, versions=None): latest_version = self.prepare_agents(count=count) - if versions is None or len(versions) <= 0: + if versions is None or len(versions) <= 0: # pylint: disable=len-as-condition versions = [latest_version] return ProtocolMock(versions=versions) @@ -731,18 +731,18 @@ def _test_ensure_no_orphans(self, invocations=3, interval=ORPHAN_WAIT_INTERVAL, # See http://stackoverflow.com/questions/26408941/python-nested-functions-and-variable-scope iterations = [0] - def iterator(*args, **kwargs): + def iterator(*args, **kwargs): # pylint: disable=unused-argument iterations[0] += 1 return iterations[0] < invocations mock_util.check_pid_alive = Mock(side_effect=iterator) - pid_files = self.update_handler._get_pid_files() + pid_files = self.update_handler._get_pid_files() # pylint: disable=protected-access self.assertEqual(pid_count, len(pid_files)) with patch('os.getpid', return_value=42): - with patch('time.sleep', return_value=None) as mock_sleep: - self.update_handler._ensure_no_orphans(orphan_wait_interval=interval) + with patch('time.sleep', return_value=None) as mock_sleep: # pylint: disable=redefined-outer-name + self.update_handler._ensure_no_orphans(orphan_wait_interval=interval) # pylint: disable=protected-access for pid_file in pid_files: self.assertFalse(os.path.exists(pid_file)) return mock_util.check_pid_alive.call_count, mock_sleep.call_count @@ -782,13 +782,13 @@ def test_ensure_partition_assigned(self, mock_time): self.assertFalse(os.path.exists(path)) - for n in range(0, 99): + for n in range(0, 99): # pylint: disable=invalid-name mock_time.utcnow.return_value = Mock(microsecond=n * 10000) - self.update_handler._ensure_partition_assigned() + self.update_handler._ensure_partition_assigned() # pylint: disable=protected-access self.assertTrue(os.path.exists(path)) - s = fileutil.read_file(path) + s = fileutil.read_file(path) # pylint: disable=invalid-name self.assertEqual(n, int(s)) os.remove(path) @@ -805,7 +805,7 @@ def test_ensure_readonly_sets_readonly(self): os.chmod(path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH) - self.update_handler._ensure_readonly_files() + self.update_handler._ensure_readonly_files() # pylint: disable=protected-access for path in test_files: mode = os.stat(path).st_mode @@ -824,7 +824,7 @@ def test_ensure_readonly_leaves_unmodified(self): os.chmod(path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH) - self.update_handler._ensure_readonly_files() + self.update_handler._ensure_readonly_files() # pylint: disable=protected-access for path in test_files: mode = os.stat(path).st_mode @@ -846,10 +846,10 @@ def _test_evaluate_agent_health(self, child_agent_index=0): self.assertFalse(child_agent.is_blacklisted) self.update_handler.child_agent = child_agent - self.update_handler._evaluate_agent_health(latest_agent) + self.update_handler._evaluate_agent_health(latest_agent) # pylint: disable=protected-access def test_evaluate_agent_health_ignores_installed_agent(self): - self.update_handler._evaluate_agent_health(None) + self.update_handler._evaluate_agent_health(None) # pylint: disable=protected-access def test_evaluate_agent_health_raises_exception_for_restarting_agent(self): self.update_handler.child_launch_time = time.time() - (4 * 60) @@ -875,46 +875,46 @@ def test_evaluate_agent_health_resets_with_new_agent(self): def test_filter_blacklisted_agents(self): self.prepare_agents() - self.update_handler._set_agents([GuestAgent(path=path) for path in self.agent_dirs()]) + self.update_handler._set_agents([GuestAgent(path=path) for path in self.agent_dirs()]) # pylint: disable=protected-access self.assertEqual(len(self.agent_dirs()), len(self.update_handler.agents)) kept_agents = self.update_handler.agents[::2] blacklisted_agents = self.update_handler.agents[1::2] for agent in blacklisted_agents: agent.mark_failure(is_fatal=True) - self.update_handler._filter_blacklisted_agents() + self.update_handler._filter_blacklisted_agents() # pylint: disable=protected-access self.assertEqual(kept_agents, self.update_handler.agents) def test_find_agents(self): self.prepare_agents() self.assertTrue(0 <= len(self.update_handler.agents)) - self.update_handler._find_agents() + self.update_handler._find_agents() # pylint: disable=protected-access self.assertEqual(len(get_agents(self.tmp_dir)), len(self.update_handler.agents)) def test_find_agents_does_reload(self): self.prepare_agents() - self.update_handler._find_agents() + self.update_handler._find_agents() # pylint: disable=protected-access agents = self.update_handler.agents - self.update_handler._find_agents() + self.update_handler._find_agents() # pylint: disable=protected-access self.assertNotEqual(agents, self.update_handler.agents) def test_find_agents_sorts(self): self.prepare_agents() - self.update_handler._find_agents() + self.update_handler._find_agents() # pylint: disable=protected-access - v = FlexibleVersion("100000") - for a in self.update_handler.agents: + v = FlexibleVersion("100000") # pylint: disable=invalid-name + for a in self.update_handler.agents: # pylint: disable=invalid-name self.assertTrue(v > a.version) - v = a.version + v = a.version # pylint: disable=invalid-name @patch('azurelinuxagent.common.protocol.wire.WireClient.get_host_plugin') def test_get_host_plugin_returns_host_for_wireserver(self, mock_get_host): protocol = WireProtocol('12.34.56.78') mock_get_host.return_value = "faux host" - host = self.update_handler._get_host_plugin(protocol=protocol) + host = self.update_handler._get_host_plugin(protocol=protocol) # pylint: disable=protected-access print("mock_get_host call cound={0}".format(mock_get_host.call_count)) self.assertEqual(1, mock_get_host.call_count) self.assertEqual("faux host", host) @@ -953,77 +953,77 @@ def test_get_latest_agent_skips_unavailable(self): self.assertEqual(latest_agent.version, prior_agent.version) def test_get_pid_files(self): - pid_files = self.update_handler._get_pid_files() + pid_files = self.update_handler._get_pid_files() # pylint: disable=protected-access self.assertEqual(0, len(pid_files)) def test_get_pid_files_returns_previous(self): - for n in range(1250): + for n in range(1250): # pylint: disable=invalid-name fileutil.write_file(os.path.join(self.tmp_dir, str(n) + "_waagent.pid"), ustr(n + 1)) - pid_files = self.update_handler._get_pid_files() + pid_files = self.update_handler._get_pid_files() # pylint: disable=protected-access self.assertEqual(1250, len(pid_files)) - pid_dir, pid_name, pid_re = self.update_handler._get_pid_parts() - for p in pid_files: + pid_dir, pid_name, pid_re = self.update_handler._get_pid_parts() # pylint: disable=unused-variable,protected-access + for p in pid_files: # pylint: disable=invalid-name self.assertTrue(pid_re.match(os.path.basename(p))) def test_is_clean_start_returns_true_when_no_sentinel(self): - self.assertFalse(os.path.isfile(self.update_handler._sentinel_file_path())) - self.assertTrue(self.update_handler._is_clean_start) + self.assertFalse(os.path.isfile(self.update_handler._sentinel_file_path())) # pylint: disable=protected-access + self.assertTrue(self.update_handler._is_clean_start) # pylint: disable=protected-access def test_is_clean_start_returns_false_when_sentinel_exists(self): - self.update_handler._set_sentinel(agent=CURRENT_AGENT) - self.assertFalse(self.update_handler._is_clean_start) + self.update_handler._set_sentinel(agent=CURRENT_AGENT) # pylint: disable=protected-access + self.assertFalse(self.update_handler._is_clean_start) # pylint: disable=protected-access def test_is_clean_start_returns_false_for_exceptions(self): - self.update_handler._set_sentinel() + self.update_handler._set_sentinel() # pylint: disable=protected-access with patch("azurelinuxagent.common.utils.fileutil.read_file", side_effect=Exception): - self.assertFalse(self.update_handler._is_clean_start) + self.assertFalse(self.update_handler._is_clean_start) # pylint: disable=protected-access def test_is_orphaned_returns_false_if_parent_exists(self): fileutil.write_file(conf.get_agent_pid_file_path(), ustr(42)) with patch('os.getppid', return_value=42): - self.assertFalse(self.update_handler._is_orphaned) + self.assertFalse(self.update_handler._is_orphaned) # pylint: disable=protected-access def test_is_orphaned_returns_true_if_parent_is_init(self): with patch('os.getppid', return_value=1): - self.assertTrue(self.update_handler._is_orphaned) + self.assertTrue(self.update_handler._is_orphaned) # pylint: disable=protected-access def test_is_orphaned_returns_true_if_parent_does_not_exist(self): fileutil.write_file(conf.get_agent_pid_file_path(), ustr(24)) with patch('os.getppid', return_value=42): - self.assertTrue(self.update_handler._is_orphaned) + self.assertTrue(self.update_handler._is_orphaned) # pylint: disable=protected-access def test_is_version_available(self): self.prepare_agents(is_available=True) self.update_handler.agents = self.agents() for agent in self.agents(): - self.assertTrue(self.update_handler._is_version_eligible(agent.version)) + self.assertTrue(self.update_handler._is_version_eligible(agent.version)) # pylint: disable=protected-access @patch("azurelinuxagent.ga.update.is_current_agent_installed", return_value=False) - def test_is_version_available_rejects(self, mock_current): + def test_is_version_available_rejects(self, mock_current): # pylint: disable=unused-argument self.prepare_agents(is_available=True) self.update_handler.agents = self.agents() self.update_handler.agents[0].mark_failure(is_fatal=True) - self.assertFalse(self.update_handler._is_version_eligible(self.agents()[0].version)) + self.assertFalse(self.update_handler._is_version_eligible(self.agents()[0].version)) # pylint: disable=protected-access @patch("azurelinuxagent.ga.update.is_current_agent_installed", return_value=True) - def test_is_version_available_accepts_current(self, mock_current): + def test_is_version_available_accepts_current(self, mock_current): # pylint: disable=unused-argument self.update_handler.agents = [] - self.assertTrue(self.update_handler._is_version_eligible(CURRENT_VERSION)) + self.assertTrue(self.update_handler._is_version_eligible(CURRENT_VERSION)) # pylint: disable=protected-access @patch("azurelinuxagent.ga.update.is_current_agent_installed", return_value=False) - def test_is_version_available_rejects_by_default(self, mock_current): + def test_is_version_available_rejects_by_default(self, mock_current): # pylint: disable=unused-argument self.prepare_agents() self.update_handler.agents = [] - v = self.agents()[0].version - self.assertFalse(self.update_handler._is_version_eligible(v)) + v = self.agents()[0].version # pylint: disable=invalid-name + self.assertFalse(self.update_handler._is_version_eligible(v)) # pylint: disable=protected-access def test_purge_agents(self): self.prepare_agents() - self.update_handler._find_agents() + self.update_handler._find_agents() # pylint: disable=protected-access # Ensure at least three agents initially exist self.assertTrue(2 < len(self.update_handler.agents)) @@ -1045,8 +1045,8 @@ def test_purge_agents(self): # Reload and assert only the kept agents remain on disk self.update_handler.agents = agents_to_keep - self.update_handler._purge_agents() - self.update_handler._find_agents() + self.update_handler._purge_agents() # pylint: disable=protected-access + self.update_handler._find_agents() # pylint: disable=protected-access self.assertEqual( [agent.version for agent in kept_agents], [agent.version for agent in self.update_handler.agents]) @@ -1098,8 +1098,8 @@ def test_run_latest(self): def test_run_latest_passes_child_args(self): self.prepare_agents() - agent = self.update_handler.get_latest_agent() - args, kwargs = self._test_run_latest(child_args="AnArgument") + agent = self.update_handler.get_latest_agent() # pylint: disable=unused-variable + args, kwargs = self._test_run_latest(child_args="AnArgument") # pylint: disable=unused-variable args = args[0] self.assertTrue(len(args) > 1) @@ -1128,7 +1128,7 @@ def test_run_latest_polling_stops_at_failure(self): self.assertEqual(0, mock_child.wait.call_count) def test_run_latest_polls_frequently_if_installed_is_latest(self): - mock_child = ChildMock(return_value=0) + mock_child = ChildMock(return_value=0) # pylint: disable=unused-variable mock_time = TimeMock(time_increment=CHILD_HEALTH_INTERVAL / 2) self._test_run_latest(mock_time=mock_time) self.assertEqual(1, mock_time.sleep_interval) @@ -1233,7 +1233,7 @@ def test_run_latest_creates_only_one_signal_handler(self, mock_signal): self._test_run_latest() self.assertEqual(0, mock_signal.call_count) - def _test_run(self, invocations=1, calls=[call.run()], enable_updates=False, sleep_interval=(6,)): + def _test_run(self, invocations=1, calls=[call.run()], enable_updates=False, sleep_interval=(6,)): # pylint: disable=dangerous-default-value conf.get_autoupdate_enabled = Mock(return_value=enable_updates) # Note: @@ -1244,7 +1244,7 @@ def _test_run(self, invocations=1, calls=[call.run()], enable_updates=False, sle # See http://stackoverflow.com/questions/26408941/python-nested-functions-and-variable-scope iterations = [0] - def iterator(*args, **kwargs): + def iterator(*args, **kwargs): # pylint: disable=useless-return,unused-argument iterations[0] += 1 if iterations[0] >= invocations: self.update_handler.running = False @@ -1257,7 +1257,7 @@ def iterator(*args, **kwargs): with patch('azurelinuxagent.ga.update.get_monitor_handler') as mock_monitor: with patch('azurelinuxagent.ga.update.get_env_handler') as mock_env: with patch('azurelinuxagent.ga.update.initialize_event_logger_vminfo_common_parameters'): - with patch('time.sleep', side_effect=iterator) as mock_sleep: + with patch('time.sleep', side_effect=iterator) as mock_sleep: # pylint: disable=redefined-outer-name with patch('sys.exit') as mock_exit: if isinstance(os.getppid, MagicMock): self.update_handler.run() @@ -1283,7 +1283,7 @@ def test_run_keeps_running(self): self._test_run(invocations=15, calls=[call.run()] * 15) def test_run_stops_if_update_available(self): - self.update_handler._upgrade_available = Mock(return_value=True) + self.update_handler._upgrade_available = Mock(return_value=True) # pylint: disable=protected-access self._test_run(invocations=0, calls=[], enable_updates=True) def test_run_stops_if_orphaned(self): @@ -1292,66 +1292,66 @@ def test_run_stops_if_orphaned(self): def test_run_clears_sentinel_on_successful_exit(self): self._test_run() - self.assertFalse(os.path.isfile(self.update_handler._sentinel_file_path())) + self.assertFalse(os.path.isfile(self.update_handler._sentinel_file_path())) # pylint: disable=protected-access def test_run_leaves_sentinel_on_unsuccessful_exit(self): - self.update_handler._upgrade_available = Mock(side_effect=Exception) + self.update_handler._upgrade_available = Mock(side_effect=Exception) # pylint: disable=protected-access self._test_run(invocations=0, calls=[], enable_updates=True) - self.assertTrue(os.path.isfile(self.update_handler._sentinel_file_path())) + self.assertTrue(os.path.isfile(self.update_handler._sentinel_file_path())) # pylint: disable=protected-access def test_run_emits_restart_event(self): - self.update_handler._emit_restart_event = Mock() + self.update_handler._emit_restart_event = Mock() # pylint: disable=protected-access self._test_run() - self.assertEqual(1, self.update_handler._emit_restart_event.call_count) + self.assertEqual(1, self.update_handler._emit_restart_event.call_count) # pylint: disable=protected-access def test_set_agents_sets_agents(self): self.prepare_agents() - self.update_handler._set_agents([GuestAgent(path=path) for path in self.agent_dirs()]) + self.update_handler._set_agents([GuestAgent(path=path) for path in self.agent_dirs()]) # pylint: disable=protected-access self.assertTrue(len(self.update_handler.agents) > 0) self.assertEqual(len(self.agent_dirs()), len(self.update_handler.agents)) def test_set_agents_sorts_agents(self): self.prepare_agents() - self.update_handler._set_agents([GuestAgent(path=path) for path in self.agent_dirs()]) + self.update_handler._set_agents([GuestAgent(path=path) for path in self.agent_dirs()]) # pylint: disable=protected-access - v = FlexibleVersion("100000") - for a in self.update_handler.agents: + v = FlexibleVersion("100000") # pylint: disable=invalid-name + for a in self.update_handler.agents: # pylint: disable=invalid-name self.assertTrue(v > a.version) - v = a.version + v = a.version # pylint: disable=invalid-name def test_set_sentinel(self): - self.assertFalse(os.path.isfile(self.update_handler._sentinel_file_path())) - self.update_handler._set_sentinel() - self.assertTrue(os.path.isfile(self.update_handler._sentinel_file_path())) + self.assertFalse(os.path.isfile(self.update_handler._sentinel_file_path())) # pylint: disable=protected-access + self.update_handler._set_sentinel() # pylint: disable=protected-access + self.assertTrue(os.path.isfile(self.update_handler._sentinel_file_path())) # pylint: disable=protected-access def test_set_sentinel_writes_current_agent(self): - self.update_handler._set_sentinel() + self.update_handler._set_sentinel() # pylint: disable=protected-access self.assertTrue( - fileutil.read_file(self.update_handler._sentinel_file_path()), + fileutil.read_file(self.update_handler._sentinel_file_path()), # pylint: disable=protected-access CURRENT_AGENT) def test_shutdown(self): - self.update_handler._set_sentinel() - self.update_handler._shutdown() + self.update_handler._set_sentinel() # pylint: disable=protected-access + self.update_handler._shutdown() # pylint: disable=protected-access self.assertFalse(self.update_handler.running) - self.assertFalse(os.path.isfile(self.update_handler._sentinel_file_path())) + self.assertFalse(os.path.isfile(self.update_handler._sentinel_file_path())) # pylint: disable=protected-access def test_shutdown_ignores_missing_sentinel_file(self): - self.assertFalse(os.path.isfile(self.update_handler._sentinel_file_path())) - self.update_handler._shutdown() + self.assertFalse(os.path.isfile(self.update_handler._sentinel_file_path())) # pylint: disable=protected-access + self.update_handler._shutdown() # pylint: disable=protected-access self.assertFalse(self.update_handler.running) - self.assertFalse(os.path.isfile(self.update_handler._sentinel_file_path())) + self.assertFalse(os.path.isfile(self.update_handler._sentinel_file_path())) # pylint: disable=protected-access def test_shutdown_ignores_exceptions(self): - self.update_handler._set_sentinel() + self.update_handler._set_sentinel() # pylint: disable=protected-access try: with patch("os.remove", side_effect=Exception): - self.update_handler._shutdown() - except Exception as e: - self.assertTrue(False, "Unexpected exception") + self.update_handler._shutdown() # pylint: disable=protected-access + except Exception as e: # pylint: disable=unused-variable,invalid-name + self.assertTrue(False, "Unexpected exception") # pylint: disable=redundant-unittest-assert def _test_upgrade_available( self, @@ -1366,7 +1366,7 @@ def _test_upgrade_available( self.update_handler.protocol_util = protocol conf.get_autoupdate_gafamily = Mock(return_value=protocol.family) - return self.update_handler._upgrade_available(protocol, base_version=base_version) + return self.update_handler._upgrade_available(protocol, base_version=base_version) # pylint: disable=protected-access def test_upgrade_available_returns_true_on_first_use(self): self.assertTrue(self._test_upgrade_available()) @@ -1375,11 +1375,11 @@ def test_upgrade_available_handles_missing_family(self): extensions_config = ExtensionsConfig(load_data("wire/ext_conf_missing_family.xml")) protocol = ProtocolMock() protocol.family = "Prod" - protocol.agent_manifests = extensions_config.vmagent_manifests + protocol.agent_manifests = extensions_config.vmagent_manifests # pylint: disable=attribute-defined-outside-init self.update_handler.protocol_util = protocol with patch('azurelinuxagent.common.logger.warn') as mock_logger: with patch('tests.ga.test_update.ProtocolMock.get_vmagent_pkgs', side_effect=ProtocolError): - self.assertFalse(self.update_handler._upgrade_available(protocol, base_version=CURRENT_VERSION)) + self.assertFalse(self.update_handler._upgrade_available(protocol, base_version=CURRENT_VERSION)) # pylint: disable=protected-access self.assertEqual(0, mock_logger.call_count) def test_upgrade_available_includes_old_agents(self): @@ -1409,7 +1409,7 @@ def test_upgrade_available_purges_old_agents(self): self.assertEqual(agent_versions, self.agent_versions()) def test_update_available_returns_true_if_current_gets_blacklisted(self): - self.update_handler._is_version_eligible = Mock(return_value=False) + self.update_handler._is_version_eligible = Mock(return_value=False) # pylint: disable=protected-access self.assertTrue(self._test_upgrade_available()) def test_upgrade_available_skips_if_too_frequent(self): @@ -1420,7 +1420,7 @@ def test_upgrade_available_skips_if_too_frequent(self): def test_upgrade_available_skips_if_when_no_new_versions(self): self.prepare_agents() base_version = self.agent_versions()[0] + 1 - self.update_handler._is_version_eligible = lambda x: x == base_version + self.update_handler._is_version_eligible = lambda x: x == base_version # pylint: disable=protected-access self.assertFalse(self._test_upgrade_available(base_version=base_version)) def test_upgrade_available_skips_when_no_versions(self): @@ -1434,16 +1434,16 @@ def test_upgrade_available_sorts(self): self.prepare_agents() self._test_upgrade_available() - v = FlexibleVersion("100000") - for a in self.update_handler.agents: + v = FlexibleVersion("100000") # pylint: disable=invalid-name + for a in self.update_handler.agents: # pylint: disable=invalid-name self.assertTrue(v > a.version) - v = a.version + v = a.version # pylint: disable=invalid-name def test_write_pid_file(self): - for n in range(1112): + for n in range(1112): # pylint: disable=invalid-name fileutil.write_file(os.path.join(self.tmp_dir, str(n) + "_waagent.pid"), ustr(n + 1)) with patch('os.getpid', return_value=1112): - pid_files, pid_file = self.update_handler._write_pid_file() + pid_files, pid_file = self.update_handler._write_pid_file() # pylint: disable=protected-access self.assertEqual(1112, len(pid_files)) self.assertEqual("1111_waagent.pid", os.path.basename(pid_files[-1])) self.assertEqual("1112_waagent.pid", os.path.basename(pid_file)) @@ -1452,7 +1452,7 @@ def test_write_pid_file(self): def test_write_pid_file_ignores_exceptions(self): with patch('azurelinuxagent.common.utils.fileutil.write_file', side_effect=Exception): with patch('os.getpid', return_value=42): - pid_files, pid_file = self.update_handler._write_pid_file() + pid_files, pid_file = self.update_handler._write_pid_file() # pylint: disable=protected-access self.assertEqual(0, len(pid_files)) self.assertEqual(None, pid_file) @@ -1463,7 +1463,7 @@ def test_update_happens_when_extensions_disabled(self, _): before an update is found, this test attempts to ensure that behavior never changes. """ - self.update_handler._upgrade_available = Mock(return_value=True) + self.update_handler._upgrade_available = Mock(return_value=True) # pylint: disable=protected-access self._test_run(invocations=0, calls=[], enable_updates=True, sleep_interval=(300,)) @patch('azurelinuxagent.common.conf.get_extensions_enabled', return_value=False) @@ -1471,7 +1471,7 @@ def test_interval_changes_when_extensions_disabled(self, _): """ When extension processing is disabled, the goal state interval should be larger. """ - self.update_handler._upgrade_available = Mock(return_value=False) + self.update_handler._upgrade_available = Mock(return_value=False) # pylint: disable=protected-access self._test_run(invocations=15, calls=[call.run()] * 15, sleep_interval=(300,)) @patch("azurelinuxagent.common.logger.info") @@ -1481,13 +1481,13 @@ def test_telemetry_heartbeat_creates_event(self, patch_add_event, patch_info, *_ mock_protocol = WireProtocol("foo.bar") update_handler.last_telemetry_heartbeat = datetime.utcnow() - timedelta(hours=1) - update_handler._send_heartbeat_telemetry(mock_protocol) + update_handler._send_heartbeat_telemetry(mock_protocol) # pylint: disable=protected-access self.assertEqual(1, patch_add_event.call_count) self.assertTrue(any(call_args[0] == "[HEARTBEAT] Agent {0} is running as the goal state agent {1}" for call_args in patch_info.call_args), "The heartbeat was not written to the agent's log") @contextlib.contextmanager - def _get_update_handler(self, iterations=1, test_data=DATA_FILE): + def _get_update_handler(self, iterations=1, test_data=DATA_FILE): # pylint: disable=dangerous-default-value """ This function returns a mocked version of the UpdateHandler object to be used for testing. It will only run the main loop [iterations] no of times. @@ -1498,12 +1498,12 @@ def _get_update_handler(self, iterations=1, test_data=DATA_FILE): def _set_iterations(iterations): # This will reset the current iteration and the max iterations to run for this test object. - update_handler._cur_iteration = 0 - update_handler._iterations = iterations + update_handler._cur_iteration = 0 # pylint: disable=protected-access + update_handler._iterations = iterations # pylint: disable=protected-access - def check_running(*args, **kwargs): + def check_running(*args, **kwargs): # pylint: disable=unused-argument # This method will determine if the current UpdateHandler object is supposed to run or not. - if update_handler._cur_iteration < update_handler._iterations: + if update_handler._cur_iteration < update_handler._iterations: # pylint: disable=protected-access update_handler._cur_iteration += 1 return True return False @@ -1515,9 +1515,9 @@ def check_running(*args, **kwargs): with patch("azurelinuxagent.common.conf.get_autoupdate_enabled", return_value=False): update_handler = get_update_handler() # Setup internal state for the object required for testing - update_handler._cur_iteration = 0 - update_handler._iterations = 0 - update_handler.set_iterations = lambda i: _set_iterations(i) + update_handler._cur_iteration = 0 # pylint: disable=protected-access + update_handler._iterations = 0 # pylint: disable=protected-access + update_handler.set_iterations = lambda i: _set_iterations(i) # pylint: disable=unnecessary-lambda type(update_handler).running = PropertyMock(side_effect=check_running) with patch("time.sleep", side_effect=lambda _: mock_sleep(0.001)): with patch('sys.exit'): @@ -1532,7 +1532,7 @@ def check_running(*args, **kwargs): @staticmethod def _get_test_ext_handler_instance(protocol, name="OSTCExtensions.ExampleHandlerLinux", version="1.0.0"): - eh = ExtHandler(name=name) + eh = ExtHandler(name=name) # pylint: disable=invalid-name eh.properties.version = version return ExtHandlerInstance(eh, protocol) @@ -1584,19 +1584,19 @@ def _setup_test_for_ext_event_dirs_retention(self): def test_it_should_delete_extension_events_directory_if_extension_telemetry_pipeline_disabled(self): # Disable extension telemetry pipeline and ensure events directory got deleted - with self._setup_test_for_ext_event_dirs_retention() as (update_handler, expected_events_dirs): - with patch('azurelinuxagent.ga.exthandlers._ENABLE_EXTENSION_TELEMETRY_PIPELINE', False): - update_handler.run(debug=True) - for ext_dir in expected_events_dirs: - self.assertFalse(os.path.exists(ext_dir), "Extension directory {0} still exists!".format(ext_dir)) + with self._setup_test_for_ext_event_dirs_retention() as (update_handler, expected_events_dirs): # pylint: disable=bad-indentation + with patch('azurelinuxagent.ga.exthandlers._ENABLE_EXTENSION_TELEMETRY_PIPELINE', False): # pylint: disable=bad-indentation + update_handler.run(debug=True) # pylint: disable=bad-indentation + for ext_dir in expected_events_dirs: # pylint: disable=bad-indentation + self.assertFalse(os.path.exists(ext_dir), "Extension directory {0} still exists!".format(ext_dir)) # pylint: disable=bad-indentation def test_it_should_retain_extension_events_directories_if_extension_telemetry_pipeline_enabled(self): # Rerun update handler again with extension telemetry pipeline enabled to ensure we dont delete events directories - with self._setup_test_for_ext_event_dirs_retention() as (update_handler, expected_events_dirs): - update_handler.run(debug=True) - for ext_dir in expected_events_dirs: - self.assertTrue(os.path.exists(ext_dir), "Extension directory {0} should exist!".format(ext_dir)) + with self._setup_test_for_ext_event_dirs_retention() as (update_handler, expected_events_dirs): # pylint: disable=bad-indentation + update_handler.run(debug=True) # pylint: disable=bad-indentation + for ext_dir in expected_events_dirs: # pylint: disable=bad-indentation + self.assertTrue(os.path.exists(ext_dir), "Extension directory {0} should exist!".format(ext_dir)) # pylint: disable=bad-indentation @patch('azurelinuxagent.ga.update.get_monitor_handler') @@ -1613,7 +1613,7 @@ def setUp(self): def _test_run(self, invocations=1): iterations = [0] - def iterator(*args, **kwargs): + def iterator(*args, **kwargs): # pylint: disable=useless-return,unused-argument iterations[0] += 1 if iterations[0] >= invocations: self.update_handler.running = False @@ -1659,42 +1659,42 @@ def test_start_threads(self, mock_env, mock_monitor): self.assertEqual(1, mock_env.call_count) self.assertEqual(1, mock_env_thread.run.call_count) - def test_check_if_monitor_thread_is_alive(self, mock_env, mock_monitor): + def test_check_if_monitor_thread_is_alive(self, mock_env, mock_monitor): # pylint: disable=unused-argument mock_monitor_thread = self._setup_mock_thread_and_start_test_run(mock_monitor, is_alive=True, invocations=0) self.assertEqual(1, mock_monitor.call_count) self.assertEqual(1, mock_monitor_thread.run.call_count) self.assertEqual(1, mock_monitor_thread.is_alive.call_count) self.assertEqual(0, mock_monitor_thread.start.call_count) - def test_check_if_env_thread_is_alive(self, mock_env, mock_monitor): + def test_check_if_env_thread_is_alive(self, mock_env, mock_monitor): # pylint: disable=unused-argument mock_env_thread = self._setup_mock_thread_and_start_test_run(mock_env, is_alive=True, invocations=1) self.assertEqual(1, mock_env.call_count) self.assertEqual(1, mock_env_thread.run.call_count) self.assertEqual(1, mock_env_thread.is_alive.call_count) self.assertEqual(0, mock_env_thread.start.call_count) - def test_restart_monitor_thread_if_not_alive(self, mock_env, mock_monitor): + def test_restart_monitor_thread_if_not_alive(self, mock_env, mock_monitor): # pylint: disable=unused-argument mock_monitor_thread = self._setup_mock_thread_and_start_test_run(mock_monitor, is_alive=False, invocations=1) self.assertEqual(1, mock_monitor.call_count) self.assertEqual(1, mock_monitor_thread.run.call_count) self.assertEqual(1, mock_monitor_thread.is_alive.call_count) self.assertEqual(1, mock_monitor_thread.start.call_count) - def test_restart_env_thread_if_not_alive(self, mock_env, mock_monitor): + def test_restart_env_thread_if_not_alive(self, mock_env, mock_monitor): # pylint: disable=unused-argument mock_env_thread = self._setup_mock_thread_and_start_test_run(mock_env, is_alive=False, invocations=1) self.assertEqual(1, mock_env.call_count) self.assertEqual(1, mock_env_thread.run.call_count) self.assertEqual(1, mock_env_thread.is_alive.call_count) self.assertEqual(1, mock_env_thread.start.call_count) - def test_restart_monitor_thread(self, mock_env, mock_monitor): + def test_restart_monitor_thread(self, mock_env, mock_monitor): # pylint: disable=unused-argument mock_monitor_thread = self._setup_mock_thread_and_start_test_run(mock_monitor, is_alive=False, invocations=0) self.assertEqual(True, mock_monitor.called) self.assertEqual(True, mock_monitor_thread.run.called) self.assertEqual(True, mock_monitor_thread.is_alive.called) self.assertEqual(True, mock_monitor_thread.start.called) - def test_restart_env_thread(self, mock_env, mock_monitor): + def test_restart_env_thread(self, mock_env, mock_monitor): # pylint: disable=unused-argument mock_env_thread = self._setup_mock_thread_and_start_test_run(mock_env, is_alive=False, invocations=0) self.assertEqual(True, mock_env.called) self.assertEqual(True, mock_env_thread.run.called) @@ -1702,7 +1702,7 @@ def test_restart_env_thread(self, mock_env, mock_monitor): self.assertEqual(True, mock_env_thread.start.called) -class ChildMock(Mock): +class ChildMock(Mock): # pylint: disable=too-many-ancestors def __init__(self, return_value=0, side_effect=None): Mock.__init__(self, return_value=return_value, side_effect=side_effect) @@ -1710,7 +1710,7 @@ def __init__(self, return_value=0, side_effect=None): self.wait = Mock(return_value=return_value, side_effect=side_effect) -class ProtocolMock(object): +class ProtocolMock(object): # pylint: disable=too-many-instance-attributes def __init__(self, family="TestAgent", etag=42, versions=None, client=None): self.family = family self.client = client @@ -1730,7 +1730,7 @@ def emulate_stale_goal_state(self): def create_manifests(self): self.agent_manifests = VMAgentManifestList() - if len(self.versions) <= 0: + if len(self.versions) <= 0: # pylint: disable=len-as-condition return if self.family is not None: @@ -1742,7 +1742,7 @@ def create_manifests(self): def create_packages(self): self.agent_packages = ExtHandlerPackageList() - if len(self.versions) <= 0: + if len(self.versions) <= 0: # pylint: disable=len-as-condition return for version in self.versions: @@ -1762,7 +1762,7 @@ def get_vmagent_manifests(self): raise ResourceGoneError() return self.agent_manifests, self.etag - def get_vmagent_pkgs(self, manifest): + def get_vmagent_pkgs(self, manifest): # pylint: disable=unused-argument self.call_counts["get_vmagent_pkgs"] += 1 if self.goal_state_is_stale: self.goal_state_is_stale = False @@ -1773,7 +1773,7 @@ def update_goal_state(self): self.call_counts["update_goal_state"] += 1 -class ResponseMock(Mock): +class ResponseMock(Mock): # pylint: disable=too-many-ancestors def __init__(self, status=restutil.httpclient.OK, response=None, reason=None): Mock.__init__(self) self.status = status @@ -1784,7 +1784,7 @@ def read(self): return self.response -class TimeMock(Mock): +class TimeMock(Mock): # pylint: disable=too-many-ancestors def __init__(self, time_increment=1): Mock.__init__(self) self.next_time = time.time() @@ -1793,7 +1793,7 @@ def __init__(self, time_increment=1): self.sleep_interval = None - def sleep(self, n): + def sleep(self, n): # pylint: disable=invalid-name self.sleep_interval = n def time(self): diff --git a/tests/pa/test_deprovision.py b/tests/pa/test_deprovision.py index 3d48495426..f00a5babba 100644 --- a/tests/pa/test_deprovision.py +++ b/tests/pa/test_deprovision.py @@ -32,8 +32,8 @@ class TestDeprovision(AgentTestCase): @patch('azurelinuxagent.common.protocol.util.get_protocol_util') @patch('azurelinuxagent.pa.deprovision.default.read_input') def test_confirmation(self, - mock_read, mock_protocol, mock_util, mock_signal): - dh = DeprovisionHandler() + mock_read, mock_protocol, mock_util, mock_signal): # pylint: disable=unused-argument + dh = DeprovisionHandler() # pylint: disable=invalid-name dh.setup = Mock() dh.setup.return_value = ([], []) @@ -88,9 +88,9 @@ def test_del_lib_dir_files(self, tmp = tempfile.mkdtemp() mock_conf.return_value = tmp - for d in dirs: + for d in dirs: # pylint: disable=invalid-name fileutil.mkdir(os.path.join(tmp, d)) - for f in files: + for f in files: # pylint: disable=invalid-name fileutil.write_file(os.path.join(tmp, f), "Value") deprovision_handler = get_deprovision_handler(distro_name, @@ -107,9 +107,9 @@ def test_del_lib_dir_files(self, self.assertEqual(fileutil.rm_files, actions[1].func) self.assertEqual(11, len(actions[0].args)) self.assertEqual(3, len(actions[1].args)) - for f in actions[0].args: + for f in actions[0].args: # pylint: disable=invalid-name self.assertTrue(os.path.basename(f) in files) - for f in actions[1].args: + for f in actions[1].args: # pylint: disable=invalid-name self.assertTrue(f[len(tmp)+1:] in files) @distros("redhat") @@ -120,7 +120,7 @@ def test_deprovision(self, deprovision_handler = get_deprovision_handler(distro_name, distro_version, distro_full_name) - warnings, actions = deprovision_handler.setup(deluser=False) + warnings, actions = deprovision_handler.setup(deluser=False) # pylint: disable=unused-variable assert any("/etc/resolv.conf" in w for w in warnings) @distros("ubuntu") @@ -133,7 +133,7 @@ def test_deprovision_ubuntu(self, distro_full_name) with patch("os.path.realpath", return_value="/run/resolvconf/resolv.conf"): - warnings, actions = deprovision_handler.setup(deluser=False) + warnings, actions = deprovision_handler.setup(deluser=False) # pylint: disable=unused-variable assert any("/etc/resolvconf/resolv.conf.d/tail" in w for w in warnings) diff --git a/tests/pa/test_provision.py b/tests/pa/test_provision.py index 6bb9857787..6d23bb1c04 100644 --- a/tests/pa/test_provision.py +++ b/tests/pa/test_provision.py @@ -36,7 +36,7 @@ class TestProvision(AgentTestCase): @distros("redhat") @patch('azurelinuxagent.common.osutil.default.DefaultOSUtil.get_instance_id', return_value='B9F3C233-9913-9F42-8EB3-BA656DF32502') - def test_provision(self, mock_util, distro_name, distro_version, distro_full_name): + def test_provision(self, mock_util, distro_name, distro_version, distro_full_name): # pylint: disable=unused-argument provision_handler = get_provision_handler(distro_name, distro_version, distro_full_name) mock_osutil = MagicMock() @@ -60,8 +60,8 @@ def test_customdata(self): @patch('azurelinuxagent.common.conf.get_provision_enabled', return_value=False) - def test_provisioning_is_skipped_when_not_enabled(self, mock_conf): - ph = ProvisionHandler() + def test_provisioning_is_skipped_when_not_enabled(self, mock_conf): # pylint: disable=unused-argument + ph = ProvisionHandler() # pylint: disable=invalid-name ph.osutil = DefaultOSUtil() ph.osutil.get_instance_id = Mock( return_value='B9F3C233-9913-9F42-8EB3-BA656DF32502') @@ -77,8 +77,8 @@ def test_provisioning_is_skipped_when_not_enabled(self, mock_conf): self.assertEqual(1, ph.write_provisioned.call_count) @patch('os.path.isfile', return_value=False) - def test_is_provisioned_not_provisioned(self, mock_isfile): - ph = ProvisionHandler() + def test_is_provisioned_not_provisioned(self, mock_isfile): # pylint: disable=unused-argument + ph = ProvisionHandler() # pylint: disable=invalid-name self.assertFalse(ph.is_provisioned()) @patch('os.path.isfile', return_value=True) @@ -86,9 +86,9 @@ def test_is_provisioned_not_provisioned(self, mock_isfile): return_value="B9F3C233-9913-9F42-8EB3-BA656DF32502") @patch('azurelinuxagent.pa.deprovision.get_deprovision_handler') def test_is_provisioned_is_provisioned(self, - mock_deprovision, mock_read, mock_isfile): + mock_deprovision, mock_read, mock_isfile): # pylint: disable=unused-argument - ph = ProvisionHandler() + ph = ProvisionHandler() # pylint: disable=invalid-name ph.osutil = Mock() ph.osutil.is_current_instance_id = Mock(return_value=True) ph.write_provisioned = Mock() @@ -105,9 +105,9 @@ def test_is_provisioned_is_provisioned(self, return_value="B9F3C233-9913-9F42-8EB3-BA656DF32502") @patch('azurelinuxagent.pa.deprovision.get_deprovision_handler') def test_is_provisioned_not_deprovisioned(self, - mock_deprovision, mock_read, mock_isfile): + mock_deprovision, mock_read, mock_isfile): # pylint: disable=unused-argument - ph = ProvisionHandler() + ph = ProvisionHandler() # pylint: disable=invalid-name ph.osutil = Mock() ph.osutil.is_current_instance_id = Mock(return_value=False) ph.report_ready = Mock() @@ -129,7 +129,7 @@ def test_provision_telemetry_pga_false(self, """ ProvisionGuestAgent flag is 'false' """ - self._provision_test(distro_name, + self._provision_test(distro_name, # pylint: disable=no-value-for-parameter distro_version, distro_full_name, OVF_FILE_NAME, @@ -145,7 +145,7 @@ def test_provision_telemetry_pga_true(self, """ ProvisionGuestAgent flag is 'true' """ - self._provision_test(distro_name, + self._provision_test(distro_name, # pylint: disable=no-value-for-parameter distro_version, distro_full_name, 'ovf-env-2.xml', @@ -161,7 +161,7 @@ def test_provision_telemetry_pga_empty(self, """ ProvisionGuestAgent flag is '' """ - self._provision_test(distro_name, + self._provision_test(distro_name, # pylint: disable=no-value-for-parameter distro_version, distro_full_name, 'ovf-env-3.xml', @@ -177,7 +177,7 @@ def test_provision_telemetry_pga_bad(self, """ ProvisionGuestAgent flag is 'bad data' """ - self._provision_test(distro_name, + self._provision_test(distro_name, # pylint: disable=no-value-for-parameter distro_version, distro_full_name, 'ovf-env-4.xml', @@ -187,7 +187,7 @@ def test_provision_telemetry_pga_bad(self, @patch('azurelinuxagent.common.osutil.default.DefaultOSUtil.get_instance_id', return_value='B9F3C233-9913-9F42-8EB3-BA656DF32502') @patch('azurelinuxagent.pa.provision.default.ProvisionHandler.write_agent_disabled') - def _provision_test(self, + def _provision_test(self, # pylint: disable=too-many-locals,invalid-name,too-many-arguments distro_name, distro_version, distro_full_name, @@ -195,7 +195,7 @@ def _provision_test(self, provisionMessage, expect_success, patch_write_agent_disabled, - patch_get_instance_id): + patch_get_instance_id): # pylint: disable=unused-argument """ Assert that the agent issues two telemetry messages as part of a successful provisioning. @@ -203,7 +203,7 @@ def _provision_test(self, 1. Provision 2. GuestState """ - ph = get_provision_handler(distro_name, + ph = get_provision_handler(distro_name, # pylint: disable=invalid-name distro_version, distro_full_name) ph.report_event = MagicMock() @@ -236,7 +236,7 @@ def _provision_test(self, self.assertTrue(kw_args['message'] == provisionMessage) self.assertTrue(kw_args['is_success']) - expected_disabled = True if provisionMessage == 'false' else False + expected_disabled = True if provisionMessage == 'false' else False # pylint: disable=simplifiable-if-expression self.assertTrue(patch_write_agent_disabled.call_count == expected_disabled) else: @@ -252,7 +252,7 @@ def _provision_test(self, return_value='B9F3C233-9913-9F42-8EB3-BA656DF32502') @patch('azurelinuxagent.common.conf.get_provisioning_agent', return_value='waagent') def test_provision_telemetry_fail(self, - mock_util, + mock_util, # pylint: disable=unused-argument distro_name, distro_version, distro_full_name, _): @@ -262,7 +262,7 @@ def test_provision_telemetry_fail(self, 1. Provision """ - ph = get_provision_handler(distro_name, distro_version, + ph = get_provision_handler(distro_name, distro_version, # pylint: disable=invalid-name distro_full_name) ph.report_event = MagicMock() ph.reg_ssh_host_key = MagicMock(side_effect=ProvisionError( @@ -281,7 +281,7 @@ def test_provision_telemetry_fail(self, fileutil.write_file(ovfenv_file, ovfenv_data) ph.run() - positional_args, kw_args = ph.report_event.call_args_list[0] + positional_args, kw_args = ph.report_event.call_args_list[0] # pylint: disable=unused-variable self.assertTrue(re.match(r'Provisioning failed: \[ProvisionError\] --unit-test-- \(\d+\.\d+s\)', positional_args[0]) is not None) @patch('azurelinuxagent.pa.provision.default.ProvisionHandler.write_agent_disabled') @@ -291,7 +291,7 @@ def test_handle_provision_guest_agent(self, distro_name, distro_version, distro_full_name): - ph = get_provision_handler(distro_name, + ph = get_provision_handler(distro_name, # pylint: disable=invalid-name distro_version, distro_full_name) @@ -334,8 +334,8 @@ def test_handle_provision_guest_agent(self, ) def test_get_provision_handler_config_auto_no_cloudinit( self, - patch_cloud_init_is_enabled, - patch_get_provisioning_agent): + patch_cloud_init_is_enabled, # pylint: disable=unused-argument + patch_get_provisioning_agent): # pylint: disable=unused-argument provisioning_handler = get_provision_handler() self.assertIsInstance(provisioning_handler, ProvisionHandler, 'Auto provisioning handler should be waagent if cloud-init is not enabled') @@ -349,8 +349,8 @@ def test_get_provision_handler_config_auto_no_cloudinit( ) def test_get_provision_handler_config_waagent( self, - patch_cloud_init_is_enabled, - patch_get_provisioning_agent): + patch_cloud_init_is_enabled, # pylint: disable=unused-argument + patch_get_provisioning_agent): # pylint: disable=unused-argument provisioning_handler = get_provision_handler() self.assertIsInstance(provisioning_handler, ProvisionHandler, 'Provisioning handler should be waagent if agent is set to waagent') @@ -364,8 +364,8 @@ def test_get_provision_handler_config_waagent( ) def test_get_provision_handler_config_auto_cloudinit( self, - patch_cloud_init_is_enabled, - patch_get_provisioning_agent): + patch_cloud_init_is_enabled, # pylint: disable=unused-argument + patch_get_provisioning_agent): # pylint: disable=unused-argument provisioning_handler = get_provision_handler() self.assertIsInstance(provisioning_handler, CloudInitProvisionHandler, 'Auto provisioning handler should be cloud-init if cloud-init is enabled') @@ -375,7 +375,7 @@ def test_get_provision_handler_config_auto_cloudinit( ) def test_get_provision_handler_config_cloudinit( self, - patch_get_provisioning_agent): + patch_get_provisioning_agent): # pylint: disable=unused-argument provisioning_handler = get_provision_handler() self.assertIsInstance(provisioning_handler, CloudInitProvisionHandler, 'Provisioning handler should be cloud-init if agent is set to cloud-init') diff --git a/tests/protocol/mocks.py b/tests/protocol/mocks.py index 87bc79120d..31e70e493e 100644 --- a/tests/protocol/mocks.py +++ b/tests/protocol/mocks.py @@ -72,7 +72,7 @@ def http_handlers(get, post, put): # original_http_request = restutil.http_request - def http_request(method, url, data, **kwargs): + def http_request(method, url, data, **kwargs): # pylint: disable=too-many-branches # if there is a handler for the request, use it handler = None if method == 'GET': @@ -134,7 +134,7 @@ def stop(): protocol.mock_wire_data = mockwiredata.WireProtocolData(mock_wire_data_file) protocol.start = start protocol.stop = stop - protocol.track_url = lambda url: tracked_urls.append(url) + protocol.track_url = lambda url: tracked_urls.append(url) # pylint: disable=unnecessary-lambda protocol.get_tracked_urls = lambda: tracked_urls protocol.set_http_handlers = lambda http_get_handler=None, http_post_handler=None, http_put_handler=None:\ http_handlers(get=http_get_handler, post=http_post_handler, put=http_put_handler) @@ -209,7 +209,7 @@ def is_host_plugin_put_logs_request(url): restutil.HOST_PLUGIN_PORT) -class MockHttpResponse: +class MockHttpResponse: # pylint: disable=too-few-public-methods def __init__(self, status, body=''): self.body = body self.status = status diff --git a/tests/protocol/mockwiredata.py b/tests/protocol/mockwiredata.py index 1a33ad3ba4..1510b80f24 100644 --- a/tests/protocol/mockwiredata.py +++ b/tests/protocol/mockwiredata.py @@ -19,9 +19,9 @@ from azurelinuxagent.common.utils.textutil import parse_doc, find, findall from tests.tools import load_bin_data, load_data, MagicMock, Mock -from azurelinuxagent.common.exception import HttpError, ResourceGoneError -from azurelinuxagent.common.future import httpclient -from azurelinuxagent.common.utils.cryptutil import CryptUtil +from azurelinuxagent.common.exception import HttpError, ResourceGoneError # pylint: disable=ungrouped-imports +from azurelinuxagent.common.future import httpclient # pylint: disable=ungrouped-imports +from azurelinuxagent.common.utils.cryptutil import CryptUtil # pylint: disable=ungrouped-imports DATA_FILE = { "version_info": "wire/version_info.xml", @@ -91,8 +91,8 @@ DATA_FILE_PLUGIN_SETTINGS_MISMATCH["ext_conf"] = "wire/ext_conf_plugin_settings_version_mismatch.xml" -class WireProtocolData(object): - def __init__(self, data_files=DATA_FILE): +class WireProtocolData(object): # pylint: disable=too-many-instance-attributes + def __init__(self, data_files=DATA_FILE): # pylint: disable=dangerous-default-value self.emulate_stale_goal_state = False self.call_counts = { "comp=versions": 0, @@ -152,7 +152,7 @@ def reload(self): if in_vm_artifacts_profile_file is not None: self.in_vm_artifacts_profile = load_data(in_vm_artifacts_profile_file) - def mock_http_get(self, url, *args, **kwargs): + def mock_http_get(self, url, *args, **kwargs): # pylint: disable=unused-argument,too-many-branches content = None resp = MagicMock() @@ -193,7 +193,7 @@ def mock_http_get(self, url, *args, **kwargs): # A stale GoalState results in a 400 from the HostPlugin # for which the HTTP handler in restutil raises ResourceGoneError if self.emulate_stale_goal_state: - if "extensionArtifact" in url: + if "extensionArtifact" in url: # pylint: disable=no-else-raise self.emulate_stale_goal_state = False self.call_counts["extensionArtifact"] += 1 raise ResourceGoneError() @@ -205,9 +205,9 @@ def mock_http_get(self, url, *args, **kwargs): if "extensionArtifact" in url: self.call_counts["extensionArtifact"] += 1 if "headers" not in kwargs: - raise ValueError("HostPlugin request is missing the HTTP headers: {0}", kwargs) + raise ValueError("HostPlugin request is missing the HTTP headers: {0}", kwargs) # pylint: disable=raising-format-tuple if "x-ms-artifact-location" not in kwargs["headers"]: - raise ValueError("HostPlugin request is missing the x-ms-artifact-location header: {0}", kwargs) + raise ValueError("HostPlugin request is missing the x-ms-artifact-location header: {0}", kwargs) # pylint: disable=raising-format-tuple url = kwargs["headers"]["x-ms-artifact-location"] if "manifest.xml" in url: @@ -230,7 +230,7 @@ def mock_http_get(self, url, *args, **kwargs): resp.read = Mock(return_value=content.encode("utf-8")) return resp - def mock_http_post(self, url, *args, **kwargs): + def mock_http_post(self, url, *args, **kwargs): # pylint: disable=unused-argument content = None resp = MagicMock() @@ -245,7 +245,7 @@ def mock_http_post(self, url, *args, **kwargs): resp.read = Mock(return_value=content.encode("utf-8")) return resp - def mock_http_put(self, url, *args, **kwargs): + def mock_http_put(self, url, *args, **kwargs): # pylint: disable=unused-argument content = None resp = MagicMock() @@ -295,7 +295,7 @@ def get_no_of_plugins_in_extension_config(self): def replace_xml_element_value(xml_document, element_name, element_value): new_xml_document = re.sub(r'(?<=<{0}>).+(?=)'.format(element_name), element_value, xml_document) if new_xml_document == xml_document: - raise Exception("Could not match element '{0}'", element_name) + raise Exception("Could not match element '{0}'", element_name) # pylint: disable=raising-format-tuple return new_xml_document @staticmethod diff --git a/tests/protocol/test_datacontract.py b/tests/protocol/test_datacontract.py index 342e84918e..87bb7b3f87 100644 --- a/tests/protocol/test_datacontract.py +++ b/tests/protocol/test_datacontract.py @@ -20,10 +20,10 @@ from azurelinuxagent.common.datacontract import get_properties, set_properties, DataContract, DataContractList -class SampleDataContract(DataContract): +class SampleDataContract(DataContract): # pylint: disable=too-few-public-methods def __init__(self): - self.foo = None - self.bar = DataContractList(int) + self.foo = None # pylint: disable=blacklisted-name + self.bar = DataContractList(int) # pylint: disable=blacklisted-name class TestDataContract(unittest.TestCase): @@ -32,13 +32,13 @@ def test_get_properties(self): obj.foo = "foo" obj.bar.append(1) data = get_properties(obj) - self.assertEquals("foo", data["foo"]) - self.assertEquals(list, type(data["bar"])) + self.assertEquals("foo", data["foo"]) # pylint: disable=deprecated-method + self.assertEquals(list, type(data["bar"])) # pylint: disable=deprecated-method def test_set_properties(self): obj = SampleDataContract() data = { - 'foo' : 1, + 'foo' : 1, 'baz': 'a' } set_properties('sample', obj, data) diff --git a/tests/protocol/test_healthservice.py b/tests/protocol/test_healthservice.py index 8af322f3cc..2be7a684c7 100644 --- a/tests/protocol/test_healthservice.py +++ b/tests/protocol/test_healthservice.py @@ -29,16 +29,16 @@ def assert_status_code(self, status_code, expected_healthy): is_healthy = not restutil.request_failed_at_hostplugin(response) self.assertEqual(expected_healthy, is_healthy) - def assert_observation(self, call_args, name, is_healthy, value, description): + def assert_observation(self, call_args, name, is_healthy, value, description): # pylint: disable=too-many-locals,too-many-arguments endpoint = call_args[0][0] content = call_args[0][1] - jo = json.loads(content) + jo = json.loads(content) # pylint: disable=invalid-name api = jo['Api'] source = jo['Source'] version = jo['Version'] obs = jo['Observations'] - fo = obs[0] + fo = obs[0] # pylint: disable=invalid-name obs_name = fo['ObservationName'] obs_healthy = fo['IsHealthy'] obs_value = fo['Value'] @@ -56,7 +56,7 @@ def assert_observation(self, call_args, name, is_healthy, value, description): self.assertEqual(description, obs_description) def assert_telemetry(self, call_args, response=''): - args, kw_args = call_args + args, kw_args = call_args # pylint: disable=unused-variable self.assertFalse(kw_args['is_success']) self.assertEqual('HealthObservation', kw_args['op']) obs = json.loads(kw_args['message']) @@ -75,12 +75,12 @@ def test_observation_validity(self): except ValueError: pass - o = Observation(name='Name', is_healthy=True, value=None, description=None) + o = Observation(name='Name', is_healthy=True, value=None, description=None) # pylint: disable=invalid-name self.assertEqual('', o.value) self.assertEqual('', o.description) long_str = 's' * 200 - o = Observation(name=long_str, is_healthy=True, value=long_str, description=long_str) + o = Observation(name=long_str, is_healthy=True, value=long_str, description=long_str) # pylint: disable=invalid-name self.assertEqual(200, len(o.name)) self.assertEqual(200, len(o.value)) self.assertEqual(200, len(o.description)) @@ -208,7 +208,7 @@ def test_observation_length(self): # make 100 observations for i in range(0, 100): - health_service._observe(is_healthy=True, name='{0}'.format(i)) + health_service._observe(is_healthy=True, name='{0}'.format(i)) # pylint: disable=protected-access # ensure we keep only 10 self.assertEqual(10, len(health_service.observations)) diff --git a/tests/protocol/test_hostplugin.py b/tests/protocol/test_hostplugin.py index 42141b5965..b51f7daa31 100644 --- a/tests/protocol/test_hostplugin.py +++ b/tests/protocol/test_hostplugin.py @@ -38,33 +38,33 @@ from tests.tools import AgentTestCase, PY_VERSION_MAJOR, Mock, patch if sys.version_info[0] == 3: - import http.client as httpclient - bytebuffer = memoryview + import http.client as httpclient # pylint: disable=import-error + bytebuffer = memoryview # pylint: disable=invalid-name elif sys.version_info[0] == 2: - import httplib as httpclient - bytebuffer = buffer + import httplib as httpclient # pylint: disable=import-error + bytebuffer = buffer # pylint: disable=undefined-variable,invalid-name -hostplugin_status_url = "http://168.63.129.16:32526/status" -hostplugin_versions_url = "http://168.63.129.16:32526/versions" -health_service_url = 'http://168.63.129.16:80/HealthService' -hostplugin_logs_url = "http://168.63.129.16:32526/vmAgentLog" -sas_url = "http://sas_url" -wireserver_url = "168.63.129.16" +hostplugin_status_url = "http://168.63.129.16:32526/status" # pylint: disable=invalid-name +hostplugin_versions_url = "http://168.63.129.16:32526/versions" # pylint: disable=invalid-name +health_service_url = 'http://168.63.129.16:80/HealthService' # pylint: disable=invalid-name +hostplugin_logs_url = "http://168.63.129.16:32526/vmAgentLog" # pylint: disable=invalid-name +sas_url = "http://sas_url" # pylint: disable=invalid-name +wireserver_url = "168.63.129.16" # pylint: disable=invalid-name -block_blob_type = 'BlockBlob' -page_blob_type = 'PageBlob' +block_blob_type = 'BlockBlob' # pylint: disable=invalid-name +page_blob_type = 'PageBlob' # pylint: disable=invalid-name -api_versions = '["2015-09-01"]' -storage_version = "2014-02-14" +api_versions = '["2015-09-01"]' # pylint: disable=invalid-name +storage_version = "2014-02-14" # pylint: disable=invalid-name -faux_status = "{ 'dummy' : 'data' }" -faux_status_b64 = base64.b64encode(bytes(bytearray(faux_status, encoding='utf-8'))) +faux_status = "{ 'dummy' : 'data' }" # pylint: disable=invalid-name +faux_status_b64 = base64.b64encode(bytes(bytearray(faux_status, encoding='utf-8'))) # pylint: disable=invalid-name if PY_VERSION_MAJOR > 2: - faux_status_b64 = faux_status_b64.decode('utf-8') + faux_status_b64 = faux_status_b64.decode('utf-8') # pylint: disable=invalid-name -class TestHostPlugin(HttpRequestPredicates, AgentTestCase): +class TestHostPlugin(HttpRequestPredicates, AgentTestCase): # pylint: disable=too-many-public-methods def _init_host(self): with mock_wire_protocol(DATA_FILE) as protocol: @@ -102,13 +102,13 @@ def _compare_data(self, actual, expected): expected['headers'] = self._relax_timestamp(expected['headers']) for k in iter(expected.keys()): - if k == 'content' or k == 'requestUri': + if k == 'content' or k == 'requestUri': # pylint: disable=consider-using-in if actual[k] != expected[k]: print("Mismatch: Actual '{0}'='{1}', " "Expected '{0}'='{2}'".format(k, actual[k], expected[k])) return False elif k == 'headers': - for h in expected['headers']: + for h in expected['headers']: # pylint: disable=invalid-name if not (h in actual['headers']): print("Missing Header: '{0}'".format(h)) return False @@ -130,9 +130,9 @@ def _hostplugin_data(self, blob_headers, content=None): 'headers': headers } if not content is None: - s = base64.b64encode(bytes(content)) + s = base64.b64encode(bytes(content)) # pylint: disable=invalid-name if PY_VERSION_MAJOR > 2: - s = s.decode('utf-8') + s = s.decode('utf-8') # pylint: disable=invalid-name data['content'] = s return data @@ -144,7 +144,7 @@ def _hostplugin_headers(self, goal_state): 'x-ms-host-config-name': goal_state.role_config_name } - def _validate_hostplugin_args(self, args, goal_state, exp_method, exp_url, exp_data): + def _validate_hostplugin_args(self, args, goal_state, exp_method, exp_url, exp_data): # pylint: disable=too-many-arguments args, kwargs = args self.assertEqual(exp_method, args[0]) self.assertEqual(exp_url, args[1]) @@ -160,7 +160,7 @@ def create_mock_protocol(): with mock_wire_protocol(DATA_FILE_NO_EXT) as protocol: # These tests use mock wire data that don't have any extensions (extension config will be empty). # Populate the upload blob and set an initial empty status before returning the protocol. - ext_conf = protocol.client._goal_state.ext_conf + ext_conf = protocol.client._goal_state.ext_conf # pylint: disable=protected-access ext_conf.status_upload_blob = sas_url ext_conf.status_upload_blob_type = page_blob_type @@ -175,7 +175,7 @@ def create_mock_protocol(): @patch("azurelinuxagent.common.protocol.healthservice.HealthService.report_host_plugin_versions") @patch("azurelinuxagent.ga.update.restutil.http_get") @patch("azurelinuxagent.common.protocol.hostplugin.add_event") - def assert_ensure_initialized(self, patch_event, patch_http_get, patch_report_health, + def assert_ensure_initialized(self, patch_event, patch_http_get, patch_report_health, # pylint: disable=too-many-arguments response_body, response_status_code, should_initialize, @@ -212,32 +212,32 @@ def test_ensure_initialized(self): """ Test calls to ensure_initialized """ - self.assert_ensure_initialized(response_body=api_versions, + self.assert_ensure_initialized(response_body=api_versions, # pylint: disable=no-value-for-parameter response_status_code=200, should_initialize=True, should_report_healthy=True) - self.assert_ensure_initialized(response_body='invalid ip', + self.assert_ensure_initialized(response_body='invalid ip', # pylint: disable=no-value-for-parameter response_status_code=400, should_initialize=False, should_report_healthy=True) - self.assert_ensure_initialized(response_body='generic bad request', + self.assert_ensure_initialized(response_body='generic bad request', # pylint: disable=no-value-for-parameter response_status_code=400, should_initialize=False, should_report_healthy=True) - self.assert_ensure_initialized(response_body='resource gone', + self.assert_ensure_initialized(response_body='resource gone', # pylint: disable=no-value-for-parameter response_status_code=410, should_initialize=False, should_report_healthy=True) - self.assert_ensure_initialized(response_body='generic error', + self.assert_ensure_initialized(response_body='generic error', # pylint: disable=no-value-for-parameter response_status_code=500, should_initialize=False, should_report_healthy=False) - self.assert_ensure_initialized(response_body='upstream error', + self.assert_ensure_initialized(response_body='upstream error', # pylint: disable=no-value-for-parameter response_status_code=502, should_initialize=False, should_report_healthy=True) @@ -391,7 +391,7 @@ def test_put_status_error_reporting(self, patch_add_event): # http_put having a side effect of "put_error" # # The agent tries to upload using a direct connection, and that succeeds. - self.assertEqual(1, wire_protocol_client.status_blob.upload.call_count) + self.assertEqual(1, wire_protocol_client.status_blob.upload.call_count) # pylint: disable=no-member # The agent never touches the default protocol is this code path, so no change. self.assertFalse(wire.HostPluginProtocol.is_default_channel()) # The agent never logs telemetry event for direct fallback @@ -404,7 +404,7 @@ def test_validate_http_request_when_uploading_status(self): """Validate correct set of data is sent to HostGAPlugin when reporting VM status""" with mock_wire_protocol(DATA_FILE) as protocol: - test_goal_state = protocol.client._goal_state + test_goal_state = protocol.client._goal_state # pylint: disable=protected-access plugin = protocol.client.get_host_plugin() status_blob = protocol.client.status_blob @@ -438,7 +438,7 @@ def test_validate_http_request_when_uploading_status(self): def test_validate_block_blob(self): with mock_wire_protocol(DATA_FILE) as protocol: - test_goal_state = protocol.client._goal_state + test_goal_state = protocol.client._goal_state # pylint: disable=protected-access host_client = wire.HostPluginProtocol(wireserver_url, test_goal_state.container_id, @@ -481,7 +481,7 @@ def test_validate_block_blob(self): def test_validate_page_blobs(self): """Validate correct set of data is sent for page blobs""" with mock_wire_protocol(DATA_FILE) as protocol: - test_goal_state = protocol.client._goal_state + test_goal_state = protocol.client._goal_state # pylint: disable=protected-access host_client = wire.HostPluginProtocol(wireserver_url, test_goal_state.container_id, @@ -539,7 +539,7 @@ def test_validate_page_blobs(self): exp_method, exp_url, exp_data) def test_validate_http_request_for_put_vm_log(self): - def http_put_handler(url, *args, **kwargs): + def http_put_handler(url, *args, **kwargs): # pylint: disable=inconsistent-return-statements if self.is_host_plugin_put_logs_request(url): http_put_handler.args, http_put_handler.kwargs = args, kwargs return MockResponse(body=b'', status_code=200) @@ -581,7 +581,7 @@ def http_put_handler(url, *args, **kwargs): self.assertTrue(UUID_PATTERN.match(headers["x-ms-client-correlationid"]), "Correlation id is not in GUID form!") def test_put_vm_log_should_raise_an_exception_when_request_fails(self): - def http_put_handler(url, *args, **kwargs): + def http_put_handler(url, *args, **kwargs): # pylint: disable=inconsistent-return-statements if self.is_host_plugin_put_logs_request(url): http_put_handler.args, http_put_handler.kwargs = args, kwargs return MockResponse(body=ustr('Gone'), status_code=410) @@ -607,7 +607,7 @@ def http_put_handler(url, *args, **kwargs): def test_validate_get_extension_artifacts(self): with mock_wire_protocol(DATA_FILE) as protocol: - test_goal_state = protocol.client._goal_state + test_goal_state = protocol.client._goal_state # pylint: disable=protected-access expected_url = hostplugin.URI_FORMAT_GET_EXTENSION_ARTIFACT.format(wireserver_url, hostplugin.HOST_PLUGIN_PORT) expected_headers = {'x-ms-version': '2015-09-01', @@ -622,7 +622,7 @@ def test_validate_get_extension_artifacts(self): self.assertTrue(host_client.api_versions is None) self.assertTrue(host_client.health_service is not None) - with patch.object(wire.HostPluginProtocol, "get_api_versions", return_value=api_versions) as patch_get: + with patch.object(wire.HostPluginProtocol, "get_api_versions", return_value=api_versions) as patch_get: # pylint: disable=unused-variable actual_url, actual_headers = host_client.get_artifact_request(sas_url) self.assertTrue(host_client.is_initialized) self.assertFalse(host_client.api_versions is None) @@ -860,7 +860,7 @@ def test_should_report(self): self.assertEqual(False, actual) -class MockResponse: +class MockResponse: # pylint: disable=too-few-public-methods def __init__(self, body, status_code, reason=''): self.body = body self.status = status_code diff --git a/tests/protocol/test_image_info_matcher.py b/tests/protocol/test_image_info_matcher.py index bd538b3958..b2516dd8b0 100644 --- a/tests/protocol/test_image_info_matcher.py +++ b/tests/protocol/test_image_info_matcher.py @@ -17,7 +17,7 @@ # Requires Python 2.6+ and Openssl 1.0+ from azurelinuxagent.common.protocol.imds import ImageInfoMatcher -import unittest +import unittest # pylint: disable=wrong-import-order class TestImageInfoMatcher(unittest.TestCase): diff --git a/tests/protocol/test_imds.py b/tests/protocol/test_imds.py index a730ded035..e57b5f6455 100644 --- a/tests/protocol/test_imds.py +++ b/tests/protocol/test_imds.py @@ -88,7 +88,8 @@ def test_get_empty_response(self, mock_http_get): test_subject = imds.ImdsClient(restutil.KNOWN_WIRESERVER_IP) self.assertRaises(ValueError, test_subject.get_compute) - def test_deserialize_ComputeInfo(self): + def test_deserialize_ComputeInfo(self): # pylint: disable=invalid-name + # pylint: disable=invalid-name s = '''{ "location": "westcentralus", "name": "unit_test", @@ -108,6 +109,7 @@ def test_deserialize_ComputeInfo(self): "vmScaleSetName": "MyScaleSet", "zone": "In" }''' + # pylint: enable=invalid-name data = json.loads(s, encoding='utf-8') @@ -138,7 +140,7 @@ def test_is_custom_image(self): image_origin = self._setup_image_origin_assert("", "", "", "") self.assertEqual(imds.IMDS_IMAGE_ORIGIN_CUSTOM, image_origin) - def test_is_endorsed_CentOS(self): + def test_is_endorsed_CentOS(self): # pylint: disable=invalid-name self.assertEqual(imds.IMDS_IMAGE_ORIGIN_ENDORSED, self._setup_image_origin_assert("OpenLogic", "CentOS", "6.3", "")) self.assertEqual(imds.IMDS_IMAGE_ORIGIN_ENDORSED, self._setup_image_origin_assert("OpenLogic", "CentOS", "6.4", "")) self.assertEqual(imds.IMDS_IMAGE_ORIGIN_ENDORSED, self._setup_image_origin_assert("OpenLogic", "CentOS", "6.5", "")) @@ -163,7 +165,7 @@ def test_is_endorsed_CentOS(self): self.assertEqual(imds.IMDS_IMAGE_ORIGIN_PLATFORM, self._setup_image_origin_assert("OpenLogic", "CentOS", "6.2", "")) self.assertEqual(imds.IMDS_IMAGE_ORIGIN_PLATFORM, self._setup_image_origin_assert("OpenLogic", "CentOS", "6.1", "")) - def test_is_endorsed_CoreOS(self): + def test_is_endorsed_CoreOS(self): # pylint: disable=invalid-name self.assertEqual(imds.IMDS_IMAGE_ORIGIN_ENDORSED, self._setup_image_origin_assert("CoreOS", "CoreOS", "stable", "494.4.0")) self.assertEqual(imds.IMDS_IMAGE_ORIGIN_ENDORSED, self._setup_image_origin_assert("CoreOS", "CoreOS", "stable", "899.17.0")) self.assertEqual(imds.IMDS_IMAGE_ORIGIN_ENDORSED, self._setup_image_origin_assert("CoreOS", "CoreOS", "stable", "1688.5.3")) @@ -172,7 +174,7 @@ def test_is_endorsed_CoreOS(self): self.assertEqual(imds.IMDS_IMAGE_ORIGIN_PLATFORM, self._setup_image_origin_assert("CoreOS", "CoreOS", "alpha", "")) self.assertEqual(imds.IMDS_IMAGE_ORIGIN_PLATFORM, self._setup_image_origin_assert("CoreOS", "CoreOS", "beta", "")) - def test_is_endorsed_Debian(self): + def test_is_endorsed_Debian(self): # pylint: disable=invalid-name self.assertEqual(imds.IMDS_IMAGE_ORIGIN_ENDORSED, self._setup_image_origin_assert("credativ", "Debian", "7", "")) self.assertEqual(imds.IMDS_IMAGE_ORIGIN_ENDORSED, self._setup_image_origin_assert("credativ", "Debian", "8", "")) self.assertEqual(imds.IMDS_IMAGE_ORIGIN_ENDORSED, self._setup_image_origin_assert("credativ", "Debian", "9", "")) @@ -180,7 +182,7 @@ def test_is_endorsed_Debian(self): self.assertEqual(imds.IMDS_IMAGE_ORIGIN_PLATFORM, self._setup_image_origin_assert("credativ", "Debian", "9-DAILY", "")) self.assertEqual(imds.IMDS_IMAGE_ORIGIN_PLATFORM, self._setup_image_origin_assert("credativ", "Debian", "10-DAILY", "")) - def test_is_endorsed_Rhel(self): + def test_is_endorsed_Rhel(self): # pylint: disable=invalid-name self.assertEqual(imds.IMDS_IMAGE_ORIGIN_ENDORSED, self._setup_image_origin_assert("RedHat", "RHEL", "6.7", "")) self.assertEqual(imds.IMDS_IMAGE_ORIGIN_ENDORSED, self._setup_image_origin_assert("RedHat", "RHEL", "6.8", "")) self.assertEqual(imds.IMDS_IMAGE_ORIGIN_ENDORSED, self._setup_image_origin_assert("RedHat", "RHEL", "6.9", "")) @@ -206,7 +208,7 @@ def test_is_endorsed_Rhel(self): self.assertEqual(imds.IMDS_IMAGE_ORIGIN_PLATFORM, self._setup_image_origin_assert("RedHat", "RHEL", "6.6", "")) - def test_is_endorsed_SuSE(self): + def test_is_endorsed_SuSE(self): # pylint: disable=invalid-name self.assertEqual(imds.IMDS_IMAGE_ORIGIN_ENDORSED, self._setup_image_origin_assert("SuSE", "SLES", "11-SP4", "")) self.assertEqual(imds.IMDS_IMAGE_ORIGIN_ENDORSED, self._setup_image_origin_assert("SuSE", "SLES-BYOS", "11-SP4", "")) @@ -230,7 +232,7 @@ def test_is_endorsed_SuSE(self): self.assertEqual(imds.IMDS_IMAGE_ORIGIN_PLATFORM, self._setup_image_origin_assert("SuSE", "SLES", "11-SP3", "")) - def test_is_endorsed_UbuntuServer(self): + def test_is_endorsed_UbuntuServer(self): # pylint: disable=invalid-name self.assertEqual(imds.IMDS_IMAGE_ORIGIN_ENDORSED, self._setup_image_origin_assert("Canonical", "UbuntuServer", "14.04.0-LTS", "")) self.assertEqual(imds.IMDS_IMAGE_ORIGIN_ENDORSED, self._setup_image_origin_assert("Canonical", "UbuntuServer", "14.04.1-LTS", "")) self.assertEqual(imds.IMDS_IMAGE_ORIGIN_ENDORSED, self._setup_image_origin_assert("Canonical", "UbuntuServer", "14.04.2-LTS", "")) @@ -251,12 +253,14 @@ def test_is_endorsed_UbuntuServer(self): @staticmethod def _setup_image_origin_assert(publisher, offer, sku, version): + # pylint: disable=invalid-name s = '''{{ "publisher": "{0}", "offer": "{1}", "sku": "{2}", "version": "{3}" }}'''.format(publisher, offer, sku, version) + # pylint: enable=invalid-name data = json.loads(s, encoding='utf-8') compute_info = imds.ComputeInfo() @@ -311,7 +315,7 @@ def test_response_validation(self): expected_response='') def test_field_validation(self): - # TODO: compute fields (#1249) + # TODO: compute fields (#1249) # pylint: disable=fixme self._assert_field('network', 'interface', 'ipv4', 'ipAddress', 'privateIpAddress') self._assert_field('network', 'interface', 'ipv4', 'ipAddress') @@ -343,7 +347,7 @@ def _update_field(self, obj, fields, val): if isinstance(obj, list): self._update_field(obj[0], fields, val) else: - f = fields[0] + f = fields[0] # pylint: disable=invalid-name if len(fields) == 1: if val is None: del obj[f] @@ -353,9 +357,9 @@ def _update_field(self, obj, fields, val): self._update_field(obj[f], fields[1:], val) @staticmethod - def _imds_response(f): + def _imds_response(f): # pylint: disable=invalid-name path = os.path.join(data_dir, "imds", "{0}.json".format(f)) - with open(path, "rb") as fh: + with open(path, "rb") as fh: # pylint: disable=invalid-name return fh.read() def _assert_validation(self, http_status_code, http_response, expected_valid, expected_response): @@ -395,75 +399,75 @@ def test_endpoint_fallback(self): for has_primary_ioerror in (False, True): # secondary endpoint unreachable - test_subject._http_get = Mock(side_effect=self._mock_http_get) + test_subject._http_get = Mock(side_effect=self._mock_http_get) # pylint: disable=protected-access self._mock_imds_setup(primary_ioerror=has_primary_ioerror, secondary_ioerror=True) result = test_subject.get_metadata(resource_path=resource_path, is_health=is_health) - self.assertFalse(result.success) if has_primary_ioerror else self.assertTrue(result.success) + self.assertFalse(result.success) if has_primary_ioerror else self.assertTrue(result.success) # pylint: disable=expression-not-assigned self.assertFalse(result.service_error) if has_primary_ioerror: self.assertEqual('IMDS error in /metadata/{0}: Unable to connect to endpoint'.format(resource_path), result.response) else: self.assertEqual('Mock success response', result.response) - for _, kwargs in test_subject._http_get.call_args_list: + for _, kwargs in test_subject._http_get.call_args_list: # pylint: disable=protected-access self.assertTrue('User-Agent' in kwargs['headers']) self.assertEqual(expected_useragent, kwargs['headers']['User-Agent']) - self.assertEqual(2 if has_primary_ioerror else 1, test_subject._http_get.call_count) + self.assertEqual(2 if has_primary_ioerror else 1, test_subject._http_get.call_count) # pylint: disable=protected-access # IMDS success - test_subject._http_get = Mock(side_effect=self._mock_http_get) + test_subject._http_get = Mock(side_effect=self._mock_http_get) # pylint: disable=protected-access self._mock_imds_setup(primary_ioerror=has_primary_ioerror) result = test_subject.get_metadata(resource_path=resource_path, is_health=is_health) self.assertTrue(result.success) self.assertFalse(result.service_error) self.assertEqual('Mock success response', result.response) - for _, kwargs in test_subject._http_get.call_args_list: + for _, kwargs in test_subject._http_get.call_args_list: # pylint: disable=protected-access self.assertTrue('User-Agent' in kwargs['headers']) self.assertEqual(expected_useragent, kwargs['headers']['User-Agent']) - self.assertEqual(2 if has_primary_ioerror else 1, test_subject._http_get.call_count) + self.assertEqual(2 if has_primary_ioerror else 1, test_subject._http_get.call_count) # pylint: disable=protected-access # IMDS throttled - test_subject._http_get = Mock(side_effect=self._mock_http_get) + test_subject._http_get = Mock(side_effect=self._mock_http_get) # pylint: disable=protected-access self._mock_imds_setup(primary_ioerror=has_primary_ioerror, throttled=True) result = test_subject.get_metadata(resource_path=resource_path, is_health=is_health) self.assertFalse(result.success) self.assertFalse(result.service_error) self.assertEqual('IMDS error in /metadata/{0}: Throttled'.format(resource_path), result.response) - for _, kwargs in test_subject._http_get.call_args_list: + for _, kwargs in test_subject._http_get.call_args_list: # pylint: disable=protected-access self.assertTrue('User-Agent' in kwargs['headers']) self.assertEqual(expected_useragent, kwargs['headers']['User-Agent']) - self.assertEqual(2 if has_primary_ioerror else 1, test_subject._http_get.call_count) + self.assertEqual(2 if has_primary_ioerror else 1, test_subject._http_get.call_count) # pylint: disable=protected-access # IMDS gone error - test_subject._http_get = Mock(side_effect=self._mock_http_get) + test_subject._http_get = Mock(side_effect=self._mock_http_get) # pylint: disable=protected-access self._mock_imds_setup(primary_ioerror=has_primary_ioerror, gone_error=True) result = test_subject.get_metadata(resource_path=resource_path, is_health=is_health) self.assertFalse(result.success) self.assertTrue(result.service_error) self.assertEqual('IMDS error in /metadata/{0}: HTTP Failed with Status Code 410: Gone'.format(resource_path), result.response) - for _, kwargs in test_subject._http_get.call_args_list: + for _, kwargs in test_subject._http_get.call_args_list: # pylint: disable=protected-access self.assertTrue('User-Agent' in kwargs['headers']) self.assertEqual(expected_useragent, kwargs['headers']['User-Agent']) - self.assertEqual(2 if has_primary_ioerror else 1, test_subject._http_get.call_count) + self.assertEqual(2 if has_primary_ioerror else 1, test_subject._http_get.call_count) # pylint: disable=protected-access # IMDS bad request - test_subject._http_get = Mock(side_effect=self._mock_http_get) + test_subject._http_get = Mock(side_effect=self._mock_http_get) # pylint: disable=protected-access self._mock_imds_setup(primary_ioerror=has_primary_ioerror, bad_request=True) result = test_subject.get_metadata(resource_path=resource_path, is_health=is_health) self.assertFalse(result.success) self.assertFalse(result.service_error) self.assertEqual('IMDS error in /metadata/{0}: [HTTP Failed] [404: reason] Mock not found'.format(resource_path), result.response) - for _, kwargs in test_subject._http_get.call_args_list: + for _, kwargs in test_subject._http_get.call_args_list: # pylint: disable=protected-access self.assertTrue('User-Agent' in kwargs['headers']) self.assertEqual(expected_useragent, kwargs['headers']['User-Agent']) - self.assertEqual(2 if has_primary_ioerror else 1, test_subject._http_get.call_count) - - def _mock_imds_setup(self, primary_ioerror=False, secondary_ioerror=False, gone_error=False, throttled=False, bad_request=False): - self._mock_imds_expect_fallback = primary_ioerror - self._mock_imds_primary_ioerror = primary_ioerror - self._mock_imds_secondary_ioerror = secondary_ioerror - self._mock_imds_gone_error = gone_error - self._mock_imds_throttled = throttled - self._mock_imds_bad_request = bad_request + self.assertEqual(2 if has_primary_ioerror else 1, test_subject._http_get.call_count) # pylint: disable=protected-access + + def _mock_imds_setup(self, primary_ioerror=False, secondary_ioerror=False, gone_error=False, throttled=False, bad_request=False): # pylint: disable=too-many-arguments + self._mock_imds_expect_fallback = primary_ioerror # pylint: disable=attribute-defined-outside-init + self._mock_imds_primary_ioerror = primary_ioerror # pylint: disable=attribute-defined-outside-init + self._mock_imds_secondary_ioerror = secondary_ioerror # pylint: disable=attribute-defined-outside-init + self._mock_imds_gone_error = gone_error # pylint: disable=attribute-defined-outside-init + self._mock_imds_throttled = throttled # pylint: disable=attribute-defined-outside-init + self._mock_imds_bad_request = bad_request # pylint: disable=attribute-defined-outside-init def _mock_http_get(self, *_, **kwargs): if "foo.bar" == kwargs['endpoint'] and not self._mock_imds_expect_fallback: diff --git a/tests/protocol/test_metadata_server_migration_util.py b/tests/protocol/test_metadata_server_migration_util.py index 8fe999a039..87c38e929e 100644 --- a/tests/protocol/test_metadata_server_migration_util.py +++ b/tests/protocol/test_metadata_server_migration_util.py @@ -19,7 +19,7 @@ import tempfile import unittest -import azurelinuxagent.common.osutil.default as osutil +import azurelinuxagent.common.osutil.default as osutil # pylint: disable=unused-import import azurelinuxagent.common.protocol.metadata_server_migration_util as migration_util from azurelinuxagent.common.protocol.metadata_server_migration_util import _LEGACY_METADATA_SERVER_TRANSPORT_PRV_FILE_NAME, \ @@ -32,7 +32,7 @@ class TestMetadataServerMigrationUtil(AgentTestCase): @patch('azurelinuxagent.common.conf.get_lib_dir') def test_is_metadata_server_artifact_present(self, mock_get_lib_dir): - dir = tempfile.gettempdir() + dir = tempfile.gettempdir() # pylint: disable=redefined-builtin metadata_server_transport_cert_file = os.path.join(dir, _LEGACY_METADATA_SERVER_TRANSPORT_CERT_FILE_NAME) open(metadata_server_transport_cert_file, 'w').close() mock_get_lib_dir.return_value = dir @@ -48,7 +48,7 @@ def test_is_metadata_server_artifact_not_present(self, mock_get_lib_dir): def test_cleanup_metadata_server_artifacts_does_not_throw_with_no_metadata_certs(self, mock_get_lib_dir, mock_enable_firewall): mock_get_lib_dir.return_value = tempfile.gettempdir() mock_enable_firewall.return_value = False - osutil = MagicMock() + osutil = MagicMock() # pylint: disable=redefined-outer-name migration_util.cleanup_metadata_server_artifacts(osutil) @patch('azurelinuxagent.common.conf.enable_firewall') @@ -56,7 +56,7 @@ def test_cleanup_metadata_server_artifacts_does_not_throw_with_no_metadata_certs @patch('os.getuid') def test_cleanup_metadata_server_artifacts_firewall_enabled(self, mock_os_getuid, mock_get_lib_dir, mock_enable_firewall): # Setup Certificate Files - dir = tempfile.gettempdir() + dir = tempfile.gettempdir() # pylint: disable=redefined-builtin metadata_server_transport_prv_file = os.path.join(dir, _LEGACY_METADATA_SERVER_TRANSPORT_PRV_FILE_NAME) metadata_server_transport_cert_file = os.path.join(dir, _LEGACY_METADATA_SERVER_TRANSPORT_CERT_FILE_NAME) metadata_server_p7b_file = os.path.join(dir, _LEGACY_METADATA_SERVER_P7B_FILE_NAME) @@ -69,7 +69,7 @@ def test_cleanup_metadata_server_artifacts_firewall_enabled(self, mock_os_getuid mock_enable_firewall.return_value = True fixed_uid = 0 mock_os_getuid.return_value = fixed_uid - osutil = MagicMock() + osutil = MagicMock() # pylint: disable=redefined-outer-name # Run migration_util.cleanup_metadata_server_artifacts(osutil) @@ -88,7 +88,7 @@ def test_cleanup_metadata_server_artifacts_firewall_enabled(self, mock_os_getuid @patch('os.getuid') def test_cleanup_metadata_server_artifacts_firewall_disabled(self, mock_os_getuid, mock_get_lib_dir, mock_enable_firewall): # Setup Certificate Files - dir = tempfile.gettempdir() + dir = tempfile.gettempdir() # pylint: disable=redefined-builtin metadata_server_transport_prv_file = os.path.join(dir, _LEGACY_METADATA_SERVER_TRANSPORT_PRV_FILE_NAME) metadata_server_transport_cert_file = os.path.join(dir, _LEGACY_METADATA_SERVER_TRANSPORT_CERT_FILE_NAME) metadata_server_p7b_file = os.path.join(dir, _LEGACY_METADATA_SERVER_P7B_FILE_NAME) @@ -101,7 +101,7 @@ def test_cleanup_metadata_server_artifacts_firewall_disabled(self, mock_os_getui mock_enable_firewall.return_value = False fixed_uid = 0 mock_os_getuid.return_value = fixed_uid - osutil = MagicMock() + osutil = MagicMock() # pylint: disable=redefined-outer-name # Run migration_util.cleanup_metadata_server_artifacts(osutil) @@ -117,6 +117,7 @@ def test_cleanup_metadata_server_artifacts_firewall_disabled(self, mock_os_getui # Cleanup certificate files def tearDown(self): + # pylint: disable=redefined-builtin dir = tempfile.gettempdir() for file in [_LEGACY_METADATA_SERVER_TRANSPORT_PRV_FILE_NAME, \ _LEGACY_METADATA_SERVER_TRANSPORT_CERT_FILE_NAME, \ @@ -124,6 +125,7 @@ def tearDown(self): path = os.path.join(dir, file) if os.path.exists(path): os.remove(path) + # pylint: enable=redefined-builtin if __name__ == '__main__': unittest.main() diff --git a/tests/protocol/test_protocol_util.py b/tests/protocol/test_protocol_util.py index eedd8a172f..5a559b2c40 100644 --- a/tests/protocol/test_protocol_util.py +++ b/tests/protocol/test_protocol_util.py @@ -48,7 +48,7 @@ def setUp(self): # Cleanup certificate files, protocol file, and endpoint files def tearDown(self): - dir = tempfile.gettempdir() + dir = tempfile.gettempdir() # pylint: disable=redefined-builtin for path in [os.path.join(dir, mds_cert) for mds_cert in TestProtocolUtil.MDS_CERTIFICATES]: if os.path.exists(path): os.remove(path) @@ -75,11 +75,11 @@ def test_get_protocol_util_should_return_different_object_for_different_thread(s def get_protocol_util_instance(): try: protocol_util_instances.append(get_protocol_util()) - except Exception as e: + except Exception as e: # pylint: disable=invalid-name errors.append(e) - t1 = Thread(target=get_protocol_util_instance) - t2 = Thread(target=get_protocol_util_instance) + t1 = Thread(target=get_protocol_util_instance) # pylint: disable=invalid-name + t2 = Thread(target=get_protocol_util_instance) # pylint: disable=invalid-name t1.start() t2.start() t1.join() @@ -89,7 +89,7 @@ def get_protocol_util_instance(): self.assertNotEqual(protocol_util_instances[0], protocol_util_instances[1], "The instances created by different threads should be different") @patch("azurelinuxagent.common.protocol.util.WireProtocol") - def test_detect_protocol(self, WireProtocol, _): + def test_detect_protocol(self, WireProtocol, _): # pylint: disable=invalid-name WireProtocol.return_value = MagicMock() protocol_util = get_protocol_util() @@ -99,7 +99,7 @@ def test_detect_protocol(self, WireProtocol, _): # Test wire protocol is available protocol = protocol_util.get_protocol() - self.assertEquals(WireProtocol.return_value, protocol) + self.assertEquals(WireProtocol.return_value, protocol) # pylint: disable=deprecated-method # Test wire protocol is not available protocol_util.clear_protocol() @@ -109,7 +109,7 @@ def test_detect_protocol(self, WireProtocol, _): @patch("azurelinuxagent.common.conf.get_lib_dir") @patch("azurelinuxagent.common.protocol.util.WireProtocol") - def test_detect_protocol_no_dhcp(self, WireProtocol, mock_get_lib_dir, _): + def test_detect_protocol_no_dhcp(self, WireProtocol, mock_get_lib_dir, _): # pylint: disable=invalid-name WireProtocol.return_value.detect = Mock() mock_get_lib_dir.return_value = self.tmp_dir @@ -122,30 +122,30 @@ def test_detect_protocol_no_dhcp(self, WireProtocol, mock_get_lib_dir, _): protocol_util.dhcp_handler.endpoint = None protocol_util.dhcp_handler.run = Mock() - endpoint_file = protocol_util._get_wireserver_endpoint_file_path() + endpoint_file = protocol_util._get_wireserver_endpoint_file_path() # pylint: disable=unused-variable,protected-access # Test wire protocol when no endpoint file has been written - protocol_util._detect_protocol() + protocol_util._detect_protocol() # pylint: disable=protected-access self.assertEqual(KNOWN_WIRESERVER_IP, protocol_util.get_wireserver_endpoint()) # Test wire protocol on dhcp failure protocol_util.osutil.is_dhcp_available.return_value = True protocol_util.dhcp_handler.run.side_effect = DhcpError() - self.assertRaises(ProtocolError, protocol_util._detect_protocol) + self.assertRaises(ProtocolError, protocol_util._detect_protocol) # pylint: disable=protected-access @patch("azurelinuxagent.common.protocol.util.WireProtocol") - def test_get_protocol(self, WireProtocol, _): + def test_get_protocol(self, WireProtocol, _): # pylint: disable=invalid-name WireProtocol.return_value = MagicMock() protocol_util = get_protocol_util() protocol_util.get_wireserver_endpoint = Mock() - protocol_util._detect_protocol = MagicMock() - protocol_util._save_protocol("WireProtocol") + protocol_util._detect_protocol = MagicMock() # pylint: disable=protected-access + protocol_util._save_protocol("WireProtocol") # pylint: disable=protected-access protocol = protocol_util.get_protocol() - self.assertEquals(WireProtocol.return_value, protocol) + self.assertEquals(WireProtocol.return_value, protocol) # pylint: disable=deprecated-method protocol_util.get_wireserver_endpoint.assert_any_call() @patch('azurelinuxagent.common.conf.get_lib_dir') @@ -159,9 +159,9 @@ def test_get_protocol_wireserver_to_wireserver_update_removes_metadataserver_art because we already expect them to be created since we are updating from a WireServer agent. """ # Setup Protocol file with WireProtocol - dir = tempfile.gettempdir() + dir = tempfile.gettempdir() # pylint: disable=redefined-builtin filename = os.path.join(dir, PROTOCOL_FILE_NAME) - with open(filename, "w") as f: + with open(filename, "w") as f: # pylint: disable=invalid-name f.write(WIRE_PROTOCOL_NAME) # Setup MDS Certificates @@ -198,9 +198,9 @@ def test_get_protocol_metadataserver_to_wireserver_update_removes_metadataserver WireServer certificates are present, and protocol/endpoint files are written to appropriately. """ # Setup Protocol file with MetadataProtocol - dir = tempfile.gettempdir() + dir = tempfile.gettempdir() # pylint: disable=redefined-builtin protocol_filename = os.path.join(dir, PROTOCOL_FILE_NAME) - with open(protocol_filename, "w") as f: + with open(protocol_filename, "w") as f: # pylint: disable=invalid-name f.write(_METADATA_PROTOCOL_NAME) # Setup MDS Certificates @@ -234,12 +234,12 @@ def test_get_protocol_metadataserver_to_wireserver_update_removes_metadataserver protocol_util.osutil.enable_firewall.assert_called_once() # Check Protocol File is updated to WireProtocol - with open(os.path.join(dir, PROTOCOL_FILE_NAME), "r") as f: - self.assertEquals(f.read(), WIRE_PROTOCOL_NAME) + with open(os.path.join(dir, PROTOCOL_FILE_NAME), "r") as f: # pylint: disable=invalid-name + self.assertEquals(f.read(), WIRE_PROTOCOL_NAME) # pylint: disable=deprecated-method # Check Endpoint file is updated to WireServer IP - with open(os.path.join(dir, ENDPOINT_FILE_NAME), 'r') as f: - self.assertEquals(f.read(), KNOWN_WIRESERVER_IP) + with open(os.path.join(dir, ENDPOINT_FILE_NAME), 'r') as f: # pylint: disable=invalid-name + self.assertEquals(f.read(), KNOWN_WIRESERVER_IP) # pylint: disable=deprecated-method @patch('azurelinuxagent.common.conf.get_lib_dir') @patch('azurelinuxagent.common.conf.enable_firewall') @@ -250,7 +250,7 @@ def test_get_protocol_new_wireserver_agent_generates_certificates(self, mock_wir protocol file, and endpoint file. """ # Setup mocks - dir = tempfile.gettempdir() + dir = tempfile.gettempdir() # pylint: disable=redefined-builtin mock_get_lib_dir.return_value = dir mock_enable_firewall.return_value = True protocol_util = get_protocol_util() @@ -272,12 +272,12 @@ def test_get_protocol_new_wireserver_agent_generates_certificates(self, mock_wir protocol_util.osutil.enable_firewall.assert_not_called() # Check Protocol File is updated to WireProtocol - with open(os.path.join(dir, PROTOCOL_FILE_NAME), "r") as f: - self.assertEquals(f.read(), WIRE_PROTOCOL_NAME) + with open(os.path.join(dir, PROTOCOL_FILE_NAME), "r") as f: # pylint: disable=invalid-name + self.assertEquals(f.read(), WIRE_PROTOCOL_NAME) # pylint: disable=deprecated-method # Check Endpoint file is updated to WireServer IP - with open(os.path.join(dir, ENDPOINT_FILE_NAME), 'r') as f: - self.assertEquals(f.read(), KNOWN_WIRESERVER_IP) + with open(os.path.join(dir, ENDPOINT_FILE_NAME), 'r') as f: # pylint: disable=invalid-name + self.assertEquals(f.read(), KNOWN_WIRESERVER_IP) # pylint: disable=deprecated-method @patch("azurelinuxagent.common.protocol.util.fileutil") @patch("azurelinuxagent.common.conf.get_lib_dir") @@ -285,52 +285,52 @@ def test_endpoint_file_states(self, mock_get_lib_dir, mock_fileutil, _): mock_get_lib_dir.return_value = self.tmp_dir protocol_util = get_protocol_util() - endpoint_file = protocol_util._get_wireserver_endpoint_file_path() + endpoint_file = protocol_util._get_wireserver_endpoint_file_path() # pylint: disable=protected-access # Test get endpoint for io error mock_fileutil.read_file.side_effect = IOError() - ep = protocol_util.get_wireserver_endpoint() - self.assertEquals(ep, KNOWN_WIRESERVER_IP) + ep = protocol_util.get_wireserver_endpoint() # pylint: disable=invalid-name + self.assertEquals(ep, KNOWN_WIRESERVER_IP) # pylint: disable=deprecated-method # Test get endpoint when file not found mock_fileutil.read_file.side_effect = IOError(ENOENT, 'File not found') - ep = protocol_util.get_wireserver_endpoint() - self.assertEquals(ep, KNOWN_WIRESERVER_IP) + ep = protocol_util.get_wireserver_endpoint() # pylint: disable=invalid-name + self.assertEquals(ep, KNOWN_WIRESERVER_IP) # pylint: disable=deprecated-method # Test get endpoint for empty file mock_fileutil.read_file.return_value = "" - ep = protocol_util.get_wireserver_endpoint() - self.assertEquals(ep, KNOWN_WIRESERVER_IP) + ep = protocol_util.get_wireserver_endpoint() # pylint: disable=invalid-name + self.assertEquals(ep, KNOWN_WIRESERVER_IP) # pylint: disable=deprecated-method # Test set endpoint for io error mock_fileutil.write_file.side_effect = IOError() - ep = protocol_util.get_wireserver_endpoint() - self.assertRaises(OSUtilError, protocol_util._set_wireserver_endpoint, 'abc') + ep = protocol_util.get_wireserver_endpoint() # pylint: disable=invalid-name + self.assertRaises(OSUtilError, protocol_util._set_wireserver_endpoint, 'abc') # pylint: disable=protected-access # Test clear endpoint for io error with open(endpoint_file, "w+") as ep_fd: ep_fd.write("") with patch('os.remove') as mock_remove: - protocol_util._clear_wireserver_endpoint() + protocol_util._clear_wireserver_endpoint() # pylint: disable=protected-access self.assertEqual(1, mock_remove.call_count) self.assertEqual(endpoint_file, mock_remove.call_args_list[0][0][0]) # Test clear endpoint when file not found with patch('os.remove') as mock_remove: mock_remove = Mock(side_effect=IOError(ENOENT, 'File not found')) - protocol_util._clear_wireserver_endpoint() + protocol_util._clear_wireserver_endpoint() # pylint: disable=protected-access mock_remove.assert_not_called() def test_protocol_file_states(self, _): protocol_util = get_protocol_util() - protocol_util._clear_wireserver_endpoint = Mock() + protocol_util._clear_wireserver_endpoint = Mock() # pylint: disable=protected-access - protocol_file = protocol_util._get_protocol_file_path() + protocol_file = protocol_util._get_protocol_file_path() # pylint: disable=protected-access # Test clear protocol for io error with open(protocol_file, "w+") as proto_fd: @@ -338,16 +338,16 @@ def test_protocol_file_states(self, _): with patch('os.remove') as mock_remove: protocol_util.clear_protocol() - self.assertEqual(1, protocol_util._clear_wireserver_endpoint.call_count) + self.assertEqual(1, protocol_util._clear_wireserver_endpoint.call_count) # pylint: disable=protected-access self.assertEqual(1, mock_remove.call_count) self.assertEqual(protocol_file, mock_remove.call_args_list[0][0][0]) # Test clear protocol when file not found - protocol_util._clear_wireserver_endpoint.reset_mock() + protocol_util._clear_wireserver_endpoint.reset_mock() # pylint: disable=protected-access with patch('os.remove') as mock_remove: protocol_util.clear_protocol() - self.assertEqual(1, protocol_util._clear_wireserver_endpoint.call_count) + self.assertEqual(1, protocol_util._clear_wireserver_endpoint.call_count) # pylint: disable=protected-access self.assertEqual(1, mock_remove.call_count) self.assertEqual(protocol_file, mock_remove.call_args_list[0][0][0]) diff --git a/tests/protocol/test_wire.py b/tests/protocol/test_wire.py index 09e29bdcf3..ddd5a31f07 100644 --- a/tests/protocol/test_wire.py +++ b/tests/protocol/test_wire.py @@ -1,4 +1,4 @@ -# -*- encoding: utf-8 -*- +# -*- encoding: utf-8 -*- # pylint: disable=too-many-lines # Copyright 2018 Microsoft Corporation # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -43,13 +43,13 @@ from tests.protocol.mockwiredata import WireProtocolData from tests.tools import Mock, patch, AgentTestCase -data_with_bom = b'\xef\xbb\xbfhehe' -testurl = 'http://foo' -testtype = 'BlockBlob' +data_with_bom = b'\xef\xbb\xbfhehe' # pylint: disable=invalid-name +testurl = 'http://foo' # pylint: disable=invalid-name +testtype = 'BlockBlob' # pylint: disable=invalid-name WIRESERVER_URL = '168.63.129.16' -def get_event(message, duration=30000, evt_type="", is_internal=False, is_success=True, +def get_event(message, duration=30000, evt_type="", is_internal=False, is_success=True, # pylint: disable=invalid-name,too-many-arguments name="", op="Unknown", version=CURRENT_VERSION, eventId=1): event = TelemetryEvent(eventId, "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX") event.parameters.append(TelemetryEventParam(GuestAgentExtensionEventsSchema.Name, name)) @@ -72,11 +72,12 @@ def create_mock_protocol(artifacts_profile_blob=None, status_upload_blob=None, s ext_conf.artifacts_profile_blob = artifacts_profile_blob ext_conf.status_upload_blob = status_upload_blob ext_conf.status_upload_blob_type = status_upload_blob_type - protocol.client._goal_state.ext_conf = ext_conf + protocol.client._goal_state.ext_conf = ext_conf # pylint: disable=protected-access yield protocol +# pylint: disable=too-many-public-methods @patch("time.sleep") @patch("azurelinuxagent.common.protocol.wire.CryptUtil") @patch("azurelinuxagent.common.protocol.healthservice.HealthService._report") @@ -86,7 +87,7 @@ def setUp(self): super(TestWireProtocol, self).setUp() HostPluginProtocol.set_default_channel(False) - def _test_getters(self, test_data, certsMustBePresent, __, MockCryptUtil, _): + def _test_getters(self, test_data, certsMustBePresent, __, MockCryptUtil, _): # pylint: disable=invalid-name MockCryptUtil.side_effect = test_data.mock_crypt_util with patch.object(restutil, 'http_get', test_data.mock_http_get): @@ -94,7 +95,7 @@ def _test_getters(self, test_data, certsMustBePresent, __, MockCryptUtil, _): protocol.detect() protocol.get_vminfo() protocol.get_certs() - ext_handlers, etag = protocol.get_ext_handlers() + ext_handlers, etag = protocol.get_ext_handlers() # pylint: disable=unused-variable for ext_handler in ext_handlers.extHandlers: protocol.get_ext_handler_pkgs(ext_handler) @@ -160,8 +161,8 @@ def test_getters_with_stale_goal_state(self, patch_report, *args): self.assertEqual(1, test_data.call_counts["hostingenvuri"]) self.assertEqual(1, patch_report.call_count) - def test_call_storage_kwargs(self, *args): - from azurelinuxagent.common.utils import restutil + def test_call_storage_kwargs(self, *args): # pylint: disable=unused-argument + from azurelinuxagent.common.utils import restutil # pylint: disable=redefined-outer-name,reimported with patch.object(restutil, 'http_get') as http_patch: http_req = restutil.http_get url = testurl @@ -195,10 +196,10 @@ def test_call_storage_kwargs(self, *args): # assert self.assertTrue(http_patch.call_count == 5) for i in range(0, 5): - c = http_patch.call_args_list[i][-1]['use_proxy'] - self.assertTrue(c == (True if i != 3 else False)) + c = http_patch.call_args_list[i][-1]['use_proxy'] # pylint: disable=invalid-name + self.assertTrue(c == (True if i != 3 else False)) # pylint: disable=simplifiable-if-expression - def test_status_blob_parsing(self, *args): + def test_status_blob_parsing(self, *args): # pylint: disable=unused-argument with mock_wire_protocol(mockwiredata.DATA_FILE) as protocol: self.assertEqual(protocol.client.get_ext_conf().status_upload_blob, 'https://test.blob.core.windows.net/vhds/test-cs12.test-cs12.test-cs12.status?' @@ -206,7 +207,7 @@ def test_status_blob_parsing(self, *args): 'sig=hfRh7gzUE7sUtYwke78IOlZOrTRCYvkec4hGZ9zZzXo') self.assertEqual(protocol.client.get_ext_conf().status_upload_blob_type, u'BlockBlob') - def test_get_host_ga_plugin(self, *args): + def test_get_host_ga_plugin(self, *args): # pylint: disable=unused-argument with mock_wire_protocol(mockwiredata.DATA_FILE) as protocol: host_plugin = protocol.client.get_host_plugin() goal_state = protocol.client.get_goal_state() @@ -214,7 +215,7 @@ def test_get_host_ga_plugin(self, *args): self.assertEqual(goal_state.role_config_name, host_plugin.role_config_name) def test_upload_status_blob_should_use_the_host_channel_by_default(self, *_): - def http_put_handler(url, *_, **__): + def http_put_handler(url, *_, **__): # pylint: disable=inconsistent-return-statements if protocol.get_endpoint() in url and url.endswith('/status'): return MockResponse(body=b'', status_code=200) @@ -264,7 +265,7 @@ def test_upload_status_blob_reports_prepare_error(self, *_): def test_get_in_vm_artifacts_profile_blob_not_available(self, *_): # Test when artifacts_profile_blob is null/None with mock_wire_protocol(DATA_FILE_NO_EXT) as protocol: - protocol.client._goal_state.ext_conf = ExtensionsConfig(None) + protocol.client._goal_state.ext_conf = ExtensionsConfig(None) # pylint: disable=protected-access self.assertEqual(None, protocol.client.get_artifacts_profile()) @@ -295,7 +296,7 @@ def test_get_in_vm_artifacts_profile_response_body_not_valid(self, *_): host_plugin_get_artifact_url_and_headers.assert_called_with(testurl) @patch("azurelinuxagent.common.event.add_event") - def test_artifacts_profile_json_parsing(self, patch_event, *args): + def test_artifacts_profile_json_parsing(self, patch_event, *args): # pylint: disable=unused-argument with create_mock_protocol(artifacts_profile_blob=testurl) as protocol: # response is invalid json protocol.client.call_storage_service = Mock(return_value=MockResponse("invalid json".encode('utf-8'), 200)) @@ -310,7 +311,7 @@ def test_artifacts_profile_json_parsing(self, patch_event, *args): self.assertTrue('invalid json' in patch_event.call_args[1]['message']) self.assertEqual('ArtifactsProfileBlob', patch_event.call_args[1]['op']) - def test_get_in_vm_artifacts_profile_default(self, *args): + def test_get_in_vm_artifacts_profile_default(self, *args): # pylint: disable=unused-argument with create_mock_protocol(artifacts_profile_blob=testurl) as protocol: protocol.client.call_storage_service = Mock(return_value=MockResponse('{"onHold": "true"}'.encode('utf-8'), 200)) in_vm_artifacts_profile = protocol.client.get_artifacts_profile() @@ -319,7 +320,7 @@ def test_get_in_vm_artifacts_profile_default(self, *args): @patch("socket.gethostname", return_value="hostname") @patch("time.gmtime", return_value=time.localtime(1485543256)) - def test_report_vm_status(self, *args): + def test_report_vm_status(self, *args): # pylint: disable=unused-argument status = 'status' message = 'message' @@ -365,7 +366,7 @@ def test_send_encoded_event(self, mock_http_request, *args): first_call = mock_http_request.call_args_list[0] args, kwargs = first_call - method, url, body_received = args + method, url, body_received = args # pylint: disable=unused-variable headers = kwargs['headers'] # the headers should include utf-8 encoding... @@ -374,7 +375,7 @@ def test_send_encoded_event(self, mock_http_request, *args): self.assertIn(event_str, body_received.decode('utf-8')) @patch("azurelinuxagent.common.protocol.wire.WireClient.send_encoded_event") - def test_report_event_small_event(self, patch_send_event, *args): + def test_report_event_small_event(self, patch_send_event, *args): # pylint: disable=unused-argument event_list = TelemetryEventList() client = WireProtocol(WIRESERVER_URL).client @@ -396,7 +397,7 @@ def test_report_event_small_event(self, patch_send_event, *args): self.assertEqual(patch_send_event.call_count, 1) @patch("azurelinuxagent.common.protocol.wire.WireClient.send_encoded_event") - def test_report_event_multiple_events_to_fill_buffer(self, patch_send_event, *args): + def test_report_event_multiple_events_to_fill_buffer(self, patch_send_event, *args): # pylint: disable=unused-argument event_list = TelemetryEventList() client = WireProtocol(WIRESERVER_URL).client @@ -410,7 +411,7 @@ def test_report_event_multiple_events_to_fill_buffer(self, patch_send_event, *ar self.assertEqual(patch_send_event.call_count, 2) @patch("azurelinuxagent.common.protocol.wire.WireClient.send_encoded_event") - def test_report_event_large_event(self, patch_send_event, *args): + def test_report_event_large_event(self, patch_send_event, *args): # pylint: disable=unused-argument event_list = TelemetryEventList() event_str = random_generator(2 ** 18) event_list.events.append(get_event(message=event_str)) @@ -421,7 +422,7 @@ def test_report_event_large_event(self, patch_send_event, *args): class TestWireClient(HttpRequestPredicates, AgentTestCase): - def test_get_ext_conf_without_extensions_should_retrieve_vmagent_manifests_info(self, *args): + def test_get_ext_conf_without_extensions_should_retrieve_vmagent_manifests_info(self, *args): # pylint: disable=unused-argument # Basic test for get_ext_conf() when extensions are not present in the config. The test verifies that # get_ext_conf() fetches the correct data by comparing the returned data with the test data provided the # mock_wire_protocol. @@ -479,11 +480,11 @@ def http_get_handler(url, *_, **__): success = protocol.download_ext_handler_pkg(extension_url, target_file) urls = protocol.get_tracked_urls() - self.assertEquals(success, True, 'The download should have succeeded') - self.assertEquals(len(urls), 1, "Unexpected number of HTTP requests: [{0}]".format(urls)) - self.assertEquals(urls[0], extension_url, "The extension should have been downloaded over the direct channel") + self.assertEquals(success, True, 'The download should have succeeded') # pylint: disable=deprecated-method + self.assertEquals(len(urls), 1, "Unexpected number of HTTP requests: [{0}]".format(urls)) # pylint: disable=deprecated-method + self.assertEquals(urls[0], extension_url, "The extension should have been downloaded over the direct channel") # pylint: disable=deprecated-method self.assertTrue(os.path.exists(target_file), 'The extension package was not downloaded') - self.assertEquals(HostPluginProtocol.is_default_channel(), False, "The host channel should not have been set as the default") + self.assertEquals(HostPluginProtocol.is_default_channel(), False, "The host channel should not have been set as the default") # pylint: disable=deprecated-method def test_download_ext_handler_pkg_should_use_host_channel_when_direct_channel_fails_and_set_host_as_default(self): extension_url = 'https://fake_host/fake_extension.zip' @@ -502,18 +503,18 @@ def http_get_handler(url, *_, **kwargs): success = protocol.download_ext_handler_pkg(extension_url, target_file) urls = protocol.get_tracked_urls() - self.assertEquals(success, True, 'The download should have succeeded') - self.assertEquals(len(urls), 2, "Unexpected number of HTTP requests: [{0}]".format(urls)) - self.assertEquals(urls[0], extension_url, "The first attempt should have been over the direct channel") + self.assertEquals(success, True, 'The download should have succeeded') # pylint: disable=deprecated-method + self.assertEquals(len(urls), 2, "Unexpected number of HTTP requests: [{0}]".format(urls)) # pylint: disable=deprecated-method + self.assertEquals(urls[0], extension_url, "The first attempt should have been over the direct channel") # pylint: disable=deprecated-method self.assertTrue(self.is_host_plugin_extension_artifact_request(urls[1]), "The retry attempt should have been over the host channel") self.assertTrue(os.path.exists(target_file), 'The extension package was not downloaded') - self.assertEquals(HostPluginProtocol.is_default_channel(), True, "The host channel should have been set as the default") + self.assertEquals(HostPluginProtocol.is_default_channel(), True, "The host channel should have been set as the default") # pylint: disable=deprecated-method def test_download_ext_handler_pkg_should_retry_the_host_channel_after_refreshing_host_plugin(self): extension_url = 'https://fake_host/fake_extension.zip' target_file = os.path.join(self.tmp_dir, 'fake_extension.zip') - def http_get_handler(url, *args, **kwargs): + def http_get_handler(url, *args, **kwargs): # pylint: disable=unused-argument if url == extension_url: return HttpError("Exception to fake an error on the direct channel") if self.is_host_plugin_extension_request(url, kwargs, extension_url): @@ -539,14 +540,14 @@ def http_get_handler(url, *args, **kwargs): success = protocol.download_ext_handler_pkg(extension_url, target_file) urls = protocol.get_tracked_urls() - self.assertEquals(success, True, 'The download should have succeeded') - self.assertEquals(len(urls), 4, "Unexpected number of HTTP requests: [{0}]".format(urls)) - self.assertEquals(urls[0], extension_url, "The first attempt should have been over the direct channel") + self.assertEquals(success, True, 'The download should have succeeded') # pylint: disable=deprecated-method + self.assertEquals(len(urls), 4, "Unexpected number of HTTP requests: [{0}]".format(urls)) # pylint: disable=deprecated-method + self.assertEquals(urls[0], extension_url, "The first attempt should have been over the direct channel") # pylint: disable=deprecated-method self.assertTrue(self.is_host_plugin_extension_artifact_request(urls[1]), "The second attempt should have been over the host channel") self.assertTrue(self.is_goal_state_request(urls[2]), "The host channel should have been refreshed the goal state") self.assertTrue(self.is_host_plugin_extension_artifact_request(urls[3]), "The third attempt should have been over the host channel") self.assertTrue(os.path.exists(target_file), 'The extension package was not downloaded') - self.assertEquals(HostPluginProtocol.is_default_channel(), True, "The host channel should have been set as the default") + self.assertEquals(HostPluginProtocol.is_default_channel(), True, "The host channel should have been set as the default") # pylint: disable=deprecated-method finally: HostPluginProtocol.set_default_channel(False) @@ -573,13 +574,13 @@ def http_get_handler(url, *_, **kwargs): success = protocol.download_ext_handler_pkg(extension_url, "/an-invalid-directory/an-invalid-file.zip") urls = protocol.get_tracked_urls() - self.assertEquals(success, False, "The download should have failed") - self.assertEquals(len(urls), 4, "Unexpected number of HTTP requests: [{0}]".format(urls)) - self.assertEquals(urls[0], extension_url, "The first attempt should have been over the direct channel") + self.assertEquals(success, False, "The download should have failed") # pylint: disable=deprecated-method + self.assertEquals(len(urls), 4, "Unexpected number of HTTP requests: [{0}]".format(urls)) # pylint: disable=deprecated-method + self.assertEquals(urls[0], extension_url, "The first attempt should have been over the direct channel") # pylint: disable=deprecated-method self.assertTrue(self.is_host_plugin_extension_artifact_request(urls[1]), "The second attempt should have been over the host channel") self.assertTrue(self.is_goal_state_request(urls[2]), "The host channel should have been refreshed the goal state") self.assertTrue(self.is_host_plugin_extension_artifact_request(urls[3]), "The third attempt should have been over the host channel") - self.assertEquals(HostPluginProtocol.is_default_channel(), False, "The host channel should not have been set as the default") + self.assertEquals(HostPluginProtocol.is_default_channel(), False, "The host channel should not have been set as the default") # pylint: disable=deprecated-method def test_fetch_manifest_should_not_invoke_host_channel_when_direct_channel_succeeds(self): manifest_url = 'https://fake_host/fake_manifest.xml' @@ -598,10 +599,10 @@ def http_get_handler(url, *_, **__): manifest = protocol.client.fetch_manifest([VMAgentManifestUri(uri=manifest_url)]) urls = protocol.get_tracked_urls() - self.assertEquals(manifest, manifest_xml, 'The expected manifest was not downloaded') - self.assertEquals(len(urls), 1, "Unexpected number of HTTP requests: [{0}]".format(urls)) - self.assertEquals(urls[0], manifest_url, "The manifest should have been downloaded over the direct channel") - self.assertEquals(HostPluginProtocol.is_default_channel(), False, "The default channel should not have changed") + self.assertEquals(manifest, manifest_xml, 'The expected manifest was not downloaded') # pylint: disable=deprecated-method + self.assertEquals(len(urls), 1, "Unexpected number of HTTP requests: [{0}]".format(urls)) # pylint: disable=deprecated-method + self.assertEquals(urls[0], manifest_url, "The manifest should have been downloaded over the direct channel") # pylint: disable=deprecated-method + self.assertEquals(HostPluginProtocol.is_default_channel(), False, "The default channel should not have changed") # pylint: disable=deprecated-method def test_fetch_manifest_should_use_host_channel_when_direct_channel_fails_and_set_it_to_default(self): manifest_url = 'https://fake_host/fake_manifest.xml' @@ -621,11 +622,11 @@ def http_get_handler(url, *_, **kwargs): manifest = protocol.client.fetch_manifest([VMAgentManifestUri(uri=manifest_url)]) urls = protocol.get_tracked_urls() - self.assertEquals(manifest, manifest_xml, 'The expected manifest was not downloaded') - self.assertEquals(len(urls), 2, "Unexpected number of HTTP requests: [{0}]".format(urls)) - self.assertEquals(urls[0], manifest_url, "The first attempt should have been over the direct channel") + self.assertEquals(manifest, manifest_xml, 'The expected manifest was not downloaded') # pylint: disable=deprecated-method + self.assertEquals(len(urls), 2, "Unexpected number of HTTP requests: [{0}]".format(urls)) # pylint: disable=deprecated-method + self.assertEquals(urls[0], manifest_url, "The first attempt should have been over the direct channel") # pylint: disable=deprecated-method self.assertTrue(self.is_host_plugin_extension_artifact_request(urls[1]), "The retry should have been over the host channel") - self.assertEquals(HostPluginProtocol.is_default_channel(), True, "The host should have been set as the default channel") + self.assertEquals(HostPluginProtocol.is_default_channel(), True, "The host should have been set as the default channel") # pylint: disable=deprecated-method finally: HostPluginProtocol.set_default_channel(False) # Reset default channel @@ -636,7 +637,7 @@ def test_fetch_manifest_should_retry_the_host_channel_after_refreshing_the_host_ def http_get_handler(url, *_, **kwargs): if url == manifest_url: return HttpError("Exception to fake an error on the direct channel") - if self.is_host_plugin_extension_request(url, kwargs, manifest_url): + if self.is_host_plugin_extension_request(url, kwargs, manifest_url): # pylint: disable=no-else-return # fake a stale goal state then succeed once the goal state has been refreshed if http_get_handler.goal_state_requests == 0: http_get_handler.goal_state_requests += 1 @@ -658,13 +659,13 @@ def http_get_handler(url, *_, **kwargs): manifest = protocol.client.fetch_manifest([VMAgentManifestUri(uri=manifest_url)]) urls = protocol.get_tracked_urls() - self.assertEquals(manifest, manifest_xml) - self.assertEquals(len(urls), 4, "Unexpected number of HTTP requests: [{0}]".format(urls)) - self.assertEquals(urls[0], manifest_url, "The first attempt should have been over the direct channel") + self.assertEquals(manifest, manifest_xml) # pylint: disable=deprecated-method + self.assertEquals(len(urls), 4, "Unexpected number of HTTP requests: [{0}]".format(urls)) # pylint: disable=deprecated-method + self.assertEquals(urls[0], manifest_url, "The first attempt should have been over the direct channel") # pylint: disable=deprecated-method self.assertTrue(self.is_host_plugin_extension_artifact_request(urls[1]), "The second attempt should have been over the host channel") self.assertTrue(self.is_goal_state_request(urls[2]), "The host channel should have been refreshed the goal state") self.assertTrue(self.is_host_plugin_extension_artifact_request(urls[3]), "The third attempt should have been over the host channel") - self.assertEquals(HostPluginProtocol.is_default_channel(), True, "The host should have been set as the default channel") + self.assertEquals(HostPluginProtocol.is_default_channel(), True, "The host should have been set as the default channel") # pylint: disable=deprecated-method finally: HostPluginProtocol.set_default_channel(False) # Reset default channel @@ -672,7 +673,7 @@ def test_fetch_manifest_should_update_goal_state_and_not_change_default_channel_ manifest_url = 'https://fake_host/fake_manifest.xml' def http_get_handler(url, *_, **kwargs): - if url == manifest_url or self.is_host_plugin_extension_request(url, kwargs, manifest_url): + if url == manifest_url or self.is_host_plugin_extension_request(url, kwargs, manifest_url): # pylint: disable=no-else-return return ResourceGoneError("Exception to fake an error on either channel") elif self.is_goal_state_request(url): protocol.track_url(url) # keep track of goal state requests @@ -692,21 +693,21 @@ def http_get_handler(url, *_, **kwargs): protocol.client.fetch_manifest([VMAgentManifestUri(uri=manifest_url)]) urls = protocol.get_tracked_urls() - self.assertEquals(len(urls), 4, "Unexpected number of HTTP requests: [{0}]".format(urls)) - self.assertEquals(urls[0], manifest_url, "The first attempt should have been over the direct channel") + self.assertEquals(len(urls), 4, "Unexpected number of HTTP requests: [{0}]".format(urls)) # pylint: disable=deprecated-method + self.assertEquals(urls[0], manifest_url, "The first attempt should have been over the direct channel") # pylint: disable=deprecated-method self.assertTrue(self.is_host_plugin_extension_artifact_request(urls[1]), "The second attempt should have been over the host channel") self.assertTrue(self.is_goal_state_request(urls[2]), "The host channel should have been refreshed the goal state") self.assertTrue(self.is_host_plugin_extension_artifact_request(urls[3]), "The third attempt should have been over the host channel") - self.assertEquals(HostPluginProtocol.is_default_channel(), False, + self.assertEquals(HostPluginProtocol.is_default_channel(), False, # pylint: disable=deprecated-method "The host should not have been set as the default channel") - self.assertEquals(HostPluginProtocol.is_default_channel(), False) + self.assertEquals(HostPluginProtocol.is_default_channel(), False) # pylint: disable=deprecated-method def test_get_artifacts_profile_should_not_invoke_host_channel_when_direct_channel_succeeds(self): - def http_get_handler(url, *_, **__): + def http_get_handler(url, *_, **__): # pylint: disable=useless-return if self.is_in_vm_artifacts_profile_request(url): protocol.track_url(url) return None @@ -718,8 +719,8 @@ def http_get_handler(url, *_, **__): self.assertIsInstance(return_value, InVMArtifactsProfile, 'The request did not return a valid artifacts profile: {0}'.format(return_value)) urls = protocol.get_tracked_urls() - self.assertEquals(len(urls), 1, "Unexpected HTTP requests: [{0}]".format(urls)) - self.assertEquals(HostPluginProtocol.is_default_channel(), False) + self.assertEquals(len(urls), 1, "Unexpected HTTP requests: [{0}]".format(urls)) # pylint: disable=deprecated-method + self.assertEquals(HostPluginProtocol.is_default_channel(), False) # pylint: disable=deprecated-method def test_get_artifacts_profile_should_use_host_channel_when_direct_channel_fails(self): def http_get_handler(url, *_, **kwargs): @@ -739,12 +740,12 @@ def http_get_handler(url, *_, **kwargs): self.assertIsNotNone(return_value, "The artifacts profile request should have succeeded") self.assertIsInstance(return_value, InVMArtifactsProfile, 'The request did not return a valid artifacts profile: {0}'.format(return_value)) - self.assertTrue(return_value.onHold, 'The OnHold property should be True') + self.assertTrue(return_value.onHold, 'The OnHold property should be True') # pylint: disable=no-member urls = protocol.get_tracked_urls() - self.assertEquals(len(urls), 2, "Invalid number of requests: [{0}]".format(urls)) + self.assertEquals(len(urls), 2, "Invalid number of requests: [{0}]".format(urls)) # pylint: disable=deprecated-method self.assertTrue(self.is_in_vm_artifacts_profile_request(urls[0]), "The first request should have been over the direct channel") self.assertTrue(self.is_host_plugin_extension_artifact_request(urls[1]), "The second request should have been over the host channel") - self.assertEquals(HostPluginProtocol.is_default_channel(), True, "The default channel should have changed to the host") + self.assertEquals(HostPluginProtocol.is_default_channel(), True, "The default channel should have changed to the host") # pylint: disable=deprecated-method finally: HostPluginProtocol.set_default_channel(False) @@ -775,14 +776,14 @@ def http_get_handler(url, *_, **kwargs): self.assertIsNotNone(return_value, "The artifacts profile request should have succeeded") self.assertIsInstance(return_value, InVMArtifactsProfile, 'The request did not return a valid artifacts profile: {0}'.format(return_value)) - self.assertTrue(return_value.onHold, 'The OnHold property should be True') + self.assertTrue(return_value.onHold, 'The OnHold property should be True') # pylint: disable=no-member urls = protocol.get_tracked_urls() - self.assertEquals(len(urls), 4, "Invalid number of requests: [{0}]".format(urls)) + self.assertEquals(len(urls), 4, "Invalid number of requests: [{0}]".format(urls)) # pylint: disable=deprecated-method self.assertTrue(self.is_in_vm_artifacts_profile_request(urls[0]), "The first request should have been over the direct channel") self.assertTrue(self.is_host_plugin_extension_artifact_request(urls[1]), "The second request should have been over the host channel") self.assertTrue(self.is_goal_state_request(urls[2]), "The goal state should have been refreshed before retrying the host channel") self.assertTrue(self.is_host_plugin_extension_artifact_request(urls[3]), "The retry request should have been over the host channel") - self.assertEquals(HostPluginProtocol.is_default_channel(), True, "The default channel should have changed to the host") + self.assertEquals(HostPluginProtocol.is_default_channel(), True, "The default channel should have changed to the host") # pylint: disable=deprecated-method finally: HostPluginProtocol.set_default_channel(False) @@ -808,15 +809,15 @@ def http_get_handler(url, *_, **kwargs): self.assertIsNone(return_value, "The artifacts profile request should have failed") urls = protocol.get_tracked_urls() - self.assertEquals(len(urls), 4, "Invalid number of requests: [{0}]".format(urls)) + self.assertEquals(len(urls), 4, "Invalid number of requests: [{0}]".format(urls)) # pylint: disable=deprecated-method self.assertTrue(self.is_in_vm_artifacts_profile_request(urls[0]), "The first request should have been over the direct channel") self.assertTrue(self.is_host_plugin_extension_artifact_request(urls[1]), "The second request should have been over the host channel") self.assertTrue(self.is_goal_state_request(urls[2]), "The goal state should have been refreshed before retrying the host channel") self.assertTrue(self.is_host_plugin_extension_artifact_request(urls[3]), "The retry request should have been over the host channel") - self.assertEquals(HostPluginProtocol.is_default_channel(), False, "The default channel should not have changed") + self.assertEquals(HostPluginProtocol.is_default_channel(), False, "The default channel should not have changed") # pylint: disable=deprecated-method def test_upload_logs_should_not_refresh_plugin_when_first_attempt_succeeds(self): - def http_put_handler(url, *_, **__): + def http_put_handler(url, *_, **__): # pylint: disable=inconsistent-return-statements if self.is_host_plugin_put_logs_request(url): return MockResponse(body=b'', status_code=200) @@ -843,7 +844,7 @@ def http_put_handler(url, *_, **__): protocol.client.upload_logs(content) urls = protocol.get_tracked_urls() - self.assertEquals(len(urls), 2, "Invalid number of requests: [{0}]".format(urls)) + self.assertEquals(len(urls), 2, "Invalid number of requests: [{0}]".format(urls)) # pylint: disable=deprecated-method self.assertTrue(self.is_host_plugin_put_logs_request(urls[0]), "The first request should have been over the host channel") self.assertTrue(self.is_host_plugin_put_logs_request(urls[1]), @@ -853,11 +854,11 @@ def test_send_request_using_appropriate_channel_should_not_invoke_host_channel_w with mock_wire_protocol(mockwiredata.DATA_FILE) as protocol: protocol.client.get_host_plugin().set_default_channel(False) - def direct_func(*args): + def direct_func(*args): # pylint: disable=unused-argument direct_func.counter += 1 return 42 - def host_func(*args): + def host_func(*args): # pylint: disable=useless-return,unused-argument host_func.counter += 1 return None @@ -866,19 +867,19 @@ def host_func(*args): # Assert we've only called the direct channel functions and that it succeeded. ret = protocol.client.send_request_using_appropriate_channel(direct_func, host_func) - self.assertEquals(42, ret) - self.assertEquals(1, direct_func.counter) - self.assertEquals(0, host_func.counter) + self.assertEquals(42, ret) # pylint: disable=deprecated-method + self.assertEquals(1, direct_func.counter) # pylint: disable=deprecated-method + self.assertEquals(0, host_func.counter) # pylint: disable=deprecated-method def test_send_request_using_appropriate_channel_should_not_use_direct_channel_when_host_channel_is_default(self): with mock_wire_protocol(mockwiredata.DATA_FILE) as protocol: protocol.client.get_host_plugin().set_default_channel(True) - def direct_func(*args): + def direct_func(*args): # pylint: disable=unused-argument direct_func.counter += 1 return 42 - def host_func(*args): + def host_func(*args): # pylint: disable=unused-argument host_func.counter += 1 return 43 @@ -887,20 +888,20 @@ def host_func(*args): # Assert we've only called the host channel function since it's the default channel ret = protocol.client.send_request_using_appropriate_channel(direct_func, host_func) - self.assertEquals(43, ret) - self.assertEquals(0, direct_func.counter) - self.assertEquals(1, host_func.counter) + self.assertEquals(43, ret) # pylint: disable=deprecated-method + self.assertEquals(0, direct_func.counter) # pylint: disable=deprecated-method + self.assertEquals(1, host_func.counter) # pylint: disable=deprecated-method def test_send_request_using_appropriate_channel_should_use_host_channel_when_direct_channel_fails(self): with mock_wire_protocol(mockwiredata.DATA_FILE) as protocol: host = protocol.client.get_host_plugin() host.set_default_channel(False) - def direct_func(*args): + def direct_func(*args): # pylint: disable=unused-argument direct_func.counter += 1 raise InvalidContainerError() - def host_func(*args): + def host_func(*args): # pylint: disable=unused-argument host_func.counter += 1 return 42 @@ -910,20 +911,20 @@ def host_func(*args): # Assert we've called both the direct channel function and the host channel function, which succeeded. # After the host channel succeeds, the host plugin should have been set as the default channel. ret = protocol.client.send_request_using_appropriate_channel(direct_func, host_func) - self.assertEquals(42, ret) - self.assertEquals(1, direct_func.counter) - self.assertEquals(1, host_func.counter) - self.assertEquals(True, host.is_default_channel()) + self.assertEquals(42, ret) # pylint: disable=deprecated-method + self.assertEquals(1, direct_func.counter) # pylint: disable=deprecated-method + self.assertEquals(1, host_func.counter) # pylint: disable=deprecated-method + self.assertEquals(True, host.is_default_channel()) # pylint: disable=deprecated-method def test_send_request_using_appropriate_channel_should_retry_the_host_channel_after_reloading_goal_state(self): with mock_wire_protocol(mockwiredata.DATA_FILE) as protocol: protocol.client.get_host_plugin().set_default_channel(False) - def direct_func(*args): + def direct_func(*args): # pylint: disable=unused-argument direct_func.counter += 1 raise InvalidContainerError() - def host_func(*args): + def host_func(*args): # pylint: disable=unused-argument host_func.counter += 1 if host_func.counter == 1: raise ResourceGoneError("Resource is gone") @@ -937,11 +938,11 @@ def host_func(*args): with patch( 'azurelinuxagent.common.protocol.wire.WireClient.update_host_plugin_from_goal_state') as mock_update_host_plugin_from_goal_state: ret = protocol.client.send_request_using_appropriate_channel(direct_func, host_func) - self.assertEquals(42, ret) - self.assertEquals(1, direct_func.counter) - self.assertEquals(2, host_func.counter) - self.assertEquals(1, mock_update_host_plugin_from_goal_state.call_count) - self.assertEquals(True, protocol.client.get_host_plugin().is_default_channel()) + self.assertEquals(42, ret) # pylint: disable=deprecated-method + self.assertEquals(1, direct_func.counter) # pylint: disable=deprecated-method + self.assertEquals(2, host_func.counter) # pylint: disable=deprecated-method + self.assertEquals(1, mock_update_host_plugin_from_goal_state.call_count) # pylint: disable=deprecated-method + self.assertEquals(True, protocol.client.get_host_plugin().is_default_channel()) # pylint: disable=deprecated-method class UpdateGoalStateTestCase(AgentTestCase): @@ -1052,7 +1053,7 @@ def test_forced_update_should_update_the_goal_state_and_the_host_plugin_when_the self.assertEqual(protocol.client.get_host_plugin().container_id, new_container_id) self.assertEqual(protocol.client.get_host_plugin().role_config_name, new_role_config_name) - +# pylint: enable=too-many-public-methods class TryUpdateGoalStateTestCase(HttpRequestPredicates, AgentTestCase): """ @@ -1073,7 +1074,7 @@ def http_get_handler(url, *_, **__): self.assertFalse(protocol.client.try_update_goal_state(), "try_update_goal_state should have failed") - def test_it_should_log_errors_only_when_the_error_state_changes(self): + def test_it_should_log_errors_only_when_the_error_state_changes(self): # pylint: disable=too-many-locals with mock_wire_protocol(mockwiredata.DATA_FILE) as protocol: def http_get_handler(url, *_, **__): if self.is_goal_state_request(url): @@ -1091,11 +1092,11 @@ def create_log_and_telemetry_mocks(): calls_to_strings = lambda calls: (str(c) for c in calls) filter_calls = lambda calls, regex=None: (c for c in calls_to_strings(calls) if regex is None or re.match(regex, c)) - logger_calls = lambda regex=None: [m for m in filter_calls(logger.method_calls, regex)] + logger_calls = lambda regex=None: [m for m in filter_calls(logger.method_calls, regex)] # pylint: disable=used-before-assignment,unnecessary-comprehension warnings = lambda: logger_calls(r'call.warn\(.*An error occurred while retrieving the goal state.*') periodic_warnings = lambda: logger_calls(r'call.periodic_warn\(.*Attempts to retrieve the goal state are failing.*') success_messages = lambda: logger_calls(r'call.info\(.*Retrieving the goal state recovered from previous errors.*') - telemetry_calls = lambda regex=None: [m for m in filter_calls(add_event.mock_calls, regex)] + telemetry_calls = lambda regex=None: [m for m in filter_calls(add_event.mock_calls, regex)] # pylint: disable=used-before-assignment,unnecessary-comprehension goal_state_events = lambda: telemetry_calls(r".*op='FetchGoalState'.*") # @@ -1105,10 +1106,10 @@ def create_log_and_telemetry_mocks(): with create_log_and_telemetry_mocks() as (logger, add_event): protocol.client.try_update_goal_state() - lc = logger_calls() + lc = logger_calls() # pylint: disable=invalid-name self.assertTrue(len(lc) == 0, "A successful call should not produce any log messages: [{0}]".format(lc)) - tc = telemetry_calls() + tc = telemetry_calls() # pylint: disable=invalid-name self.assertTrue(len(tc) == 0, "A successful call should not produce any telemetry events: [{0}]".format(tc)) # @@ -1118,12 +1119,12 @@ def create_log_and_telemetry_mocks(): with create_log_and_telemetry_mocks() as (logger, add_event): protocol.client.try_update_goal_state() - w = warnings() - pw = periodic_warnings() - self.assertEquals(len(w), 1, "A failure should have produced a warning: [{0}]".format(w)) - self.assertEquals(len(pw), 1, "A failure should have produced a periodic warning: [{0}]".format(pw)) + w = warnings() # pylint: disable=invalid-name + pw = periodic_warnings() # pylint: disable=invalid-name + self.assertEquals(len(w), 1, "A failure should have produced a warning: [{0}]".format(w)) # pylint: disable=deprecated-method + self.assertEquals(len(pw), 1, "A failure should have produced a periodic warning: [{0}]".format(pw)) # pylint: disable=deprecated-method - gs = goal_state_events() + gs = goal_state_events() # pylint: disable=invalid-name self.assertTrue(len(gs) == 1 and 'is_success=False' in gs[0], "A failure should produce a telemetry event (success=false): [{0}]".format(gs)) # @@ -1134,12 +1135,12 @@ def create_log_and_telemetry_mocks(): protocol.client.try_update_goal_state() protocol.client.try_update_goal_state() - w = warnings() - pw = periodic_warnings() + w = warnings() # pylint: disable=invalid-name + pw = periodic_warnings() # pylint: disable=invalid-name self.assertTrue(len(w) == 0, "Subsequent failures should not produce warnings: [{0}]".format(w)) - self.assertEquals(len(pw), 3, "Subsequent failures should produce periodic warnings: [{0}]".format(pw)) + self.assertEquals(len(pw), 3, "Subsequent failures should produce periodic warnings: [{0}]".format(pw)) # pylint: disable=deprecated-method - tc = telemetry_calls() + tc = telemetry_calls() # pylint: disable=invalid-name self.assertTrue(len(tc) == 0, "Subsequent failures should not produce any telemetry events: [{0}]".format(tc)) # @@ -1149,13 +1150,13 @@ def create_log_and_telemetry_mocks(): with create_log_and_telemetry_mocks() as (logger, add_event): protocol.client.try_update_goal_state() - s = success_messages() - w = warnings() - pw = periodic_warnings() - self.assertEquals(len(s), 1, "Recovering after failures should have produced an info message: [{0}]".format(s)) + s = success_messages() # pylint: disable=invalid-name + w = warnings() # pylint: disable=invalid-name + pw = periodic_warnings() # pylint: disable=invalid-name + self.assertEquals(len(s), 1, "Recovering after failures should have produced an info message: [{0}]".format(s)) # pylint: disable=deprecated-method self.assertTrue(len(w) == 0 and len(pw) == 0, "Recovering after failures should have not produced any warnings: [{0}] [{1}]".format(w, pw)) - gs = goal_state_events() + gs = goal_state_events() # pylint: disable=invalid-name self.assertTrue(len(gs) == 1 and 'is_success=True' in gs[0], "Recovering after failures should produce a telemetry event (success=true): [{0}]".format(gs)) @@ -1196,7 +1197,7 @@ def test_it_should_update_the_host_plugin_with_or_without_incarnation_changes(se self.assertEqual(protocol.client.get_shared_conf().xml_text, shared_conf_xml_text) -class MockResponse: +class MockResponse: # pylint: disable=too-few-public-methods def __init__(self, body, status_code): self.body = body self.status = status_code diff --git a/tests/test_agent.py b/tests/test_agent.py index 42fe842492..904a0f8fb7 100644 --- a/tests/test_agent.py +++ b/tests/test_agent.py @@ -82,28 +82,28 @@ class TestAgent(AgentTestCase): def test_accepts_configuration_path(self): conf_path = os.path.join(data_dir, "test_waagent.conf") - c, f, v, d, cfp = parse_args(["-configuration-path:" + conf_path]) + c, f, v, d, cfp = parse_args(["-configuration-path:" + conf_path]) # pylint: disable=unused-variable,invalid-name self.assertEqual(cfp, conf_path) @patch("os.path.exists", return_value=True) def test_checks_configuration_path(self, mock_exists): conf_path = "/foo/bar-baz/something.conf" - c, f, v, d, cfp = parse_args(["-configuration-path:"+conf_path]) + c, f, v, d, cfp = parse_args(["-configuration-path:"+conf_path]) # pylint: disable=unused-variable,invalid-name self.assertEqual(cfp, conf_path) self.assertEqual(mock_exists.call_count, 1) @patch("sys.stderr") @patch("os.path.exists", return_value=False) @patch("sys.exit", side_effect=Exception) - def test_rejects_missing_configuration_path(self, mock_exit, mock_exists, mock_stderr): + def test_rejects_missing_configuration_path(self, mock_exit, mock_exists, mock_stderr): # pylint: disable=unused-argument try: - c, f, v, d, cfp = parse_args(["-configuration-path:/foo/bar.conf"]) - self.assertTrue(False) + c, f, v, d, cfp = parse_args(["-configuration-path:/foo/bar.conf"]) # pylint: disable=unused-variable,invalid-name + self.assertTrue(False) # pylint: disable=redundant-unittest-assert except Exception: self.assertEqual(mock_exit.call_count, 1) def test_configuration_path_defaults_to_none(self): - c, f, v, d, cfp = parse_args([]) + c, f, v, d, cfp = parse_args([]) # pylint: disable=unused-variable,invalid-name self.assertEqual(cfp, None) def test_agent_accepts_configuration_path(self): @@ -151,7 +151,7 @@ def test_agent_ensures_extension_log_directory(self, mock_dir): mock_dir.return_value = ext_log_dir self.assertFalse(os.path.isdir(ext_log_dir)) - agent = Agent(False, + agent = Agent(False, # pylint: disable=unused-variable conf_file_path=os.path.join(data_dir, "test_waagent.conf")) self.assertTrue(os.path.isdir(ext_log_dir)) @@ -164,7 +164,7 @@ def test_agent_logs_if_extension_log_directory_is_a_file(self, mock_dir, mock_lo self.assertTrue(os.path.isfile(ext_log_dir)) self.assertFalse(os.path.isdir(ext_log_dir)) - agent = Agent(False, + agent = Agent(False, # pylint: disable=unused-variable conf_file_path=os.path.join(data_dir, "test_waagent.conf")) self.assertTrue(os.path.isfile(ext_log_dir)) self.assertFalse(os.path.isdir(ext_log_dir)) diff --git a/tests/tools.py b/tests/tools.py index 23575dc33a..aaad50de4f 100644 --- a/tests/tools.py +++ b/tests/tools.py @@ -34,26 +34,26 @@ import azurelinuxagent.common.conf as conf import azurelinuxagent.common.event as event import azurelinuxagent.common.logger as logger -from azurelinuxagent.common.cgroupconfigurator import CGroupConfigurator -from azurelinuxagent.common.osutil.factory import _get_osutil -from azurelinuxagent.common.osutil.ubuntu import Ubuntu14OSUtil, Ubuntu16OSUtil +from azurelinuxagent.common.cgroupconfigurator import CGroupConfigurator # pylint: disable=unused-import +from azurelinuxagent.common.osutil.factory import _get_osutil # pylint: disable=unused-import +from azurelinuxagent.common.osutil.ubuntu import Ubuntu14OSUtil, Ubuntu16OSUtil # pylint: disable=unused-import from azurelinuxagent.common.utils import fileutil from azurelinuxagent.common.version import PY_VERSION_MAJOR try: - from unittest.mock import Mock, patch, MagicMock, ANY, DEFAULT, call, PropertyMock + from unittest.mock import Mock, patch, MagicMock, ANY, DEFAULT, call, PropertyMock # pylint: disable=unused-import,ungrouped-imports # Import mock module for Python2 and Python3 - from bin.waagent2 import Agent + from bin.waagent2 import Agent # pylint: disable=unused-import except ImportError: from mock import Mock, patch, MagicMock, ANY, DEFAULT, call, PropertyMock -test_dir = os.path.dirname(os.path.abspath(__file__)) -data_dir = os.path.join(test_dir, "data") +test_dir = os.path.dirname(os.path.abspath(__file__)) # pylint: disable=invalid-name +data_dir = os.path.join(test_dir, "data") # pylint: disable=invalid-name -debug = False +debug = False # pylint: disable=invalid-name if os.environ.get('DEBUG') == '1': - debug = True + debug = True # pylint: disable=invalid-name # Enable verbose logger to stdout if debug: @@ -133,13 +133,13 @@ def is_python_version_26(): class AgentTestCase(unittest.TestCase): @classmethod - def setUpClass(cls): + def setUpClass(cls): # pylint: disable=too-many-branches # Setup newer unittest assertions missing in prior versions of Python if not hasattr(cls, "assertRegex"): cls.assertRegex = cls.assertRegexpMatches if hasattr(cls, "assertRegexpMatches") else cls.emulate_assertRegexpMatches if not hasattr(cls, "assertNotRegex"): - cls.assertNotRegex = cls.assertNotRegexpMatches if hasattr(cls, "assertNotRegexpMatches") else cls.emulate_assertNotRegexpMatches + cls.assertNotRegex = cls.assertNotRegexpMatches if hasattr(cls, "assertNotRegexpMatches") else cls.emulate_assertNotRegexpMatches # pylint: disable=no-member if not hasattr(cls, "assertIn"): cls.assertIn = cls.emulate_assertIn if not hasattr(cls, "assertNotIn"): @@ -196,53 +196,53 @@ def tearDown(self): if not debug and self.tmp_dir is not None: shutil.rmtree(self.tmp_dir) - def emulate_assertIn(self, a, b, msg=None): + def emulate_assertIn(self, a, b, msg=None): # pylint: disable=invalid-name if a not in b: msg = msg if msg is not None else "{0} not found in {1}".format(_safe_repr(a), _safe_repr(b)) self.fail(msg) - def emulate_assertNotIn(self, a, b, msg=None): + def emulate_assertNotIn(self, a, b, msg=None): # pylint: disable=invalid-name if a in b: msg = msg if msg is not None else "{0} unexpectedly found in {1}".format(_safe_repr(a), _safe_repr(b)) self.fail(msg) - def emulate_assertGreater(self, a, b, msg=None): + def emulate_assertGreater(self, a, b, msg=None): # pylint: disable=invalid-name if not a > b: msg = msg if msg is not None else '{0} not greater than {1}'.format(_safe_repr(a), _safe_repr(b)) self.fail(msg) - def emulate_assertGreaterEqual(self, a, b, msg=None): + def emulate_assertGreaterEqual(self, a, b, msg=None): # pylint: disable=invalid-name if not a >= b: msg = msg if msg is not None else '{0} not greater or equal to {1}'.format(_safe_repr(a), _safe_repr(b)) self.fail(msg) - def emulate_assertLess(self, a, b, msg=None): + def emulate_assertLess(self, a, b, msg=None): # pylint: disable=invalid-name if not a < b: msg = msg if msg is not None else '{0} not less than {1}'.format(_safe_repr(a), _safe_repr(b)) self.fail(msg) - def emulate_assertLessEqual(self, a, b, msg=None): + def emulate_assertLessEqual(self, a, b, msg=None): # pylint: disable=invalid-name if not a <= b: msg = msg if msg is not None else '{0} not less or equal to {1}'.format(_safe_repr(a), _safe_repr(b)) self.fail(msg) - def emulate_assertIsNone(self, x, msg=None): + def emulate_assertIsNone(self, x, msg=None): # pylint: disable=invalid-name if x is not None: msg = msg if msg is not None else '{0} is not None'.format(_safe_repr(x)) self.fail(msg) - def emulate_assertIsNotNone(self, x, msg=None): + def emulate_assertIsNotNone(self, x, msg=None): # pylint: disable=invalid-name if x is None: msg = msg if msg is not None else '{0} is None'.format(_safe_repr(x)) self.fail(msg) - def emulate_assertRegexpMatches(self, text, regexp, msg=None): + def emulate_assertRegexpMatches(self, text, regexp, msg=None): # pylint: disable=invalid-name if re.search(regexp, text) is not None: return msg = msg if msg is not None else "'{0}' does not match '{1}'.".format(text, regexp) self.fail(msg) - def emulate_assertNotRegexpMatches(self, text, regexp, msg=None): + def emulate_assertNotRegexpMatches(self, text, regexp, msg=None): # pylint: disable=invalid-name if re.search(regexp, text, flags=1) is None: return msg = msg if msg is not None else "'{0}' should not match '{1}'.".format(text, regexp) @@ -257,22 +257,22 @@ def __enter__(self): return self @staticmethod - def _get_type_name(type): + def _get_type_name(type): # pylint: disable=redefined-builtin return type.__name__ if hasattr(type, "__name__") else str(type) def __exit__(self, exception_type, exception, *_): if exception_type is None: - expected = AgentTestCase._AssertRaisesContextManager._get_type_name(self._expected_exception_type) + expected = AgentTestCase._AssertRaisesContextManager._get_type_name(self._expected_exception_type) # pylint: disable=protected-access self._test_case.fail("Did not raise an exception; expected '{0}'".format(expected)) if not issubclass(exception_type, self._expected_exception_type): - raised = AgentTestCase._AssertRaisesContextManager._get_type_name(exception_type) - expected = AgentTestCase._AssertRaisesContextManager._get_type_name(self._expected_exception_type) + raised = AgentTestCase._AssertRaisesContextManager._get_type_name(exception_type) # pylint: disable=protected-access + expected = AgentTestCase._AssertRaisesContextManager._get_type_name(self._expected_exception_type) # pylint: disable=protected-access self._test_case.fail("Raised '{0}', but expected '{1}'".format(raised, expected)) - self.exception = exception + self.exception = exception # pylint: disable=attribute-defined-outside-init return True - def emulate_assertRaises(self, exception_type, function=None, *args, **kwargs): + def emulate_assertRaises(self, exception_type, function=None, *args, **kwargs): # pylint: disable=invalid-name,keyword-arg-before-vararg # return a context manager only when function is not provided; otherwise use the original assertRaises if function is None: return AgentTestCase._AssertRaisesContextManager(exception_type, self) @@ -284,15 +284,15 @@ def emulate_assertRaises(self, exception_type, function=None, *args, **kwargs): def emulate_raises_regex(self, exception_type, regex, function, *args, **kwargs): try: function(*args, **kwargs) - except Exception as e: - if re.search(regex, str(e), flags=1) is not None: + except Exception as e: # pylint: disable=invalid-name + if re.search(regex, str(e), flags=1) is not None: # pylint: disable=no-else-return return else: self.fail("Expected exception {0} matching {1}. Actual: {2}".format( exception_type, regex, str(e))) self.fail("No exception was thrown. Expected exception {0} matching {1}".format(exception_type, regex)) - def emulate_assertDictEqual(self, first, second, msg=None): + def emulate_assertDictEqual(self, first, second, msg=None): # pylint: disable=invalid-name def fail(message): self.fail(self._formatMessage(msg, message)) @@ -306,7 +306,7 @@ def fail(message): if k not in first: fail("'{0}' is missing from first".format(k)) - def emulate_assertListEqual(self, seq1, seq2, msg=None, seq_type=None): + def emulate_assertListEqual(self, seq1, seq2, msg=None, seq_type=None): # pylint: disable=too-many-locals,too-many-branches,invalid-name """An equality assertion for ordered sequences (like lists and tuples). For the purposes of this function, a valid ordered sequence type is one @@ -358,7 +358,7 @@ def emulate_assertListEqual(self, seq1, seq2, msg=None, seq_type=None): elements = (seq_type_name.capitalize(), seq1_repr, seq2_repr) differing = '%ss differ: %s != %s\n' % elements - for i in xrange(min(len1, len2)): + for i in xrange(min(len1, len2)): # pylint: disable=undefined-variable try: item1 = seq1[i] except (TypeError, IndexError, NotImplementedError): @@ -379,7 +379,7 @@ def emulate_assertListEqual(self, seq1, seq2, msg=None, seq_type=None): break else: if (len1 == len2 and seq_type is None and - type(seq1) != type(seq2)): + type(seq1) != type(seq2)): # pylint: disable=unidiomatic-typecheck # The sequences are the same, but have differing types. return @@ -401,15 +401,15 @@ def emulate_assertListEqual(self, seq1, seq2, msg=None, seq_type=None): except (TypeError, IndexError, NotImplementedError): differing += ('Unable to index element %d ' 'of second %s\n' % (len1, seq_type_name)) - standardMsg = differing - diffMsg = '\n' + '\n'.join( + standardMsg = differing # pylint: disable=invalid-name + diffMsg = '\n' + '\n'.join( # pylint: disable=invalid-name difflib.ndiff(pprint.pformat(seq1).splitlines(), pprint.pformat(seq2).splitlines())) - standardMsg = self._truncateMessage(standardMsg, diffMsg) + standardMsg = self._truncateMessage(standardMsg, diffMsg) # pylint: disable=invalid-name msg = self._formatMessage(msg, standardMsg) self.fail(msg) - def emulate_assertIsInstance(self, obj, object_type, msg=None): + def emulate_assertIsInstance(self, obj, object_type, msg=None): # pylint: disable=invalid-name if not isinstance(obj, object_type): msg = msg if msg is not None else '{0} is not an instance of {1}'.format(_safe_repr(obj), _safe_repr(object_type)) @@ -418,7 +418,7 @@ def emulate_assertIsInstance(self, obj, object_type, msg=None): @staticmethod def _create_files(tmp_dir, prefix, suffix, count, with_sleep=0): for i in range(count): - f = os.path.join(tmp_dir, '.'.join((prefix, str(i), suffix))) + f = os.path.join(tmp_dir, '.'.join((prefix, str(i), suffix))) # pylint: disable=invalid-name fileutil.write_file(f, "faux content") time.sleep(with_sleep) @@ -464,7 +464,7 @@ def load_bin_data(name): return data_file.read() -supported_distro = [ +supported_distro = [ # pylint: disable=invalid-name ["ubuntu", "12.04", ""], ["ubuntu", "14.04", ""], ["ubuntu", "14.10", ""], @@ -519,7 +519,7 @@ def wrapper(self, *args, **kwargs): def clear_singleton_instances(cls): # Adding this lock to avoid any race conditions - with cls._lock: + with cls._lock: # pylint: disable=protected-access obj_name = "%s__%s" % (cls.__name__, currentThread().getName()) # Object Name = className__threadName - if obj_name in cls._instances: - del cls._instances[obj_name] + if obj_name in cls._instances: # pylint: disable=protected-access + del cls._instances[obj_name] # pylint: disable=protected-access diff --git a/tests/utils/test_archive.py b/tests/utils/test_archive.py index 22949e4804..40e9f103b9 100644 --- a/tests/utils/test_archive.py +++ b/tests/utils/test_archive.py @@ -12,9 +12,9 @@ from azurelinuxagent.common.utils.archive import StateFlusher, StateArchiver, MAX_ARCHIVED_STATES from tests.tools import AgentTestCase -debug = False +debug = False # pylint: disable=invalid-name if os.environ.get('DEBUG') == '1': - debug = True + debug = True # pylint: disable=invalid-name # Enable verbose logger to stdout if debug: @@ -32,11 +32,11 @@ def tearDown(self): if not debug and self.tmp_dir is not None: shutil.rmtree(self.tmp_dir) - def _write_file(self, fn, contents=None): + def _write_file(self, fn, contents=None): # pylint: disable=invalid-name full_name = os.path.join(self.tmp_dir, fn) fileutil.mkdir(os.path.dirname(full_name)) - with open(full_name, 'w') as fh: + with open(full_name, 'w') as fh: # pylint: disable=invalid-name data = contents if contents is not None else fn fh.write(data) return full_name @@ -56,7 +56,7 @@ def test_archive00(self): 'Microsoft.Azure.Extensions.CustomScript.0.xml' ] - for f in temp_files: + for f in temp_files: # pylint: disable=invalid-name self._write_file(f) test_subject = StateFlusher(self.tmp_dir) @@ -69,10 +69,10 @@ def test_archive00(self): self.assertEqual(1, len(timestamp_dirs)) self.assertIsIso8601(timestamp_dirs[0]) - ts = self.parse_isoformat(timestamp_dirs[0]) + ts = self.parse_isoformat(timestamp_dirs[0]) # pylint: disable=invalid-name self.assertDateTimeCloseTo(ts, datetime.utcnow(), timedelta(seconds=30)) - for f in temp_files: + for f in temp_files: # pylint: disable=invalid-name history_path = os.path.join(self.history_dir, timestamp_dirs[0], f) msg = "expected the temp file {0} to exist".format(history_path) self.assertTrue(os.path.exists(history_path), msg) @@ -91,7 +91,7 @@ def test_archive01(self): 'Microsoft.Azure.Extensions.CustomScript.0.xml' ] - for f in temp_files: + for f in temp_files: # pylint: disable=invalid-name self._write_file(f) flusher = StateFlusher(self.tmp_dir) @@ -107,7 +107,7 @@ def test_archive01(self): ts_s = os.path.splitext(zip_fn)[0] # 2000-01-01T00:00:00.000000 self.assertIsIso8601(ts_s) - ts = self.parse_isoformat(ts_s) + ts = self.parse_isoformat(ts_s) # pylint: disable=invalid-name self.assertDateTimeCloseTo(ts, datetime.utcnow(), timedelta(seconds=30)) zip_full = os.path.join(self.history_dir, zip_fn) @@ -130,13 +130,13 @@ def test_archive02(self): timestamps = [] for i in range(0, total): - ts = start + timedelta(seconds=i) + ts = start + timedelta(seconds=i) # pylint: disable=invalid-name timestamps.append(ts) if i % 2 == 0: - fn = os.path.join('history', ts.isoformat(), 'Prod.0.manifest.xml') + fn = os.path.join('history', ts.isoformat(), 'Prod.0.manifest.xml') # pylint: disable=invalid-name else: - fn = os.path.join('history', "{0}.zip".format(ts.isoformat())) + fn = os.path.join('history', "{0}.zip".format(ts.isoformat())) # pylint: disable=invalid-name self._write_file(fn) @@ -151,11 +151,11 @@ def test_archive02(self): archived_entries.sort() for i in range(0, MAX_ARCHIVED_STATES): - ts = timestamps[i + count].isoformat() + ts = timestamps[i + count].isoformat() # pylint: disable=invalid-name if i % 2 == 0: - fn = ts + fn = ts # pylint: disable=invalid-name else: - fn = "{0}.zip".format(ts) + fn = "{0}.zip".format(ts) # pylint: disable=invalid-name self.assertTrue(fn in archived_entries, "'{0}' is not in the list of unpurged entires".format(fn)) def test_archive03(self): @@ -170,11 +170,11 @@ def test_archive03(self): ] def _write_goal_state_files(temp_files, content=None): - for f in temp_files: + for f in temp_files: # pylint: disable=invalid-name self._write_file(f, content) def _check_history_files(timestamp_dir, files, content=None): - for f in files: + for f in files: # pylint: disable=invalid-name history_path = os.path.join(self.history_dir, timestamp_dir, f) msg = "expected the temp file {0} to exist".format(history_path) self.assertTrue(os.path.exists(history_path), msg) @@ -196,7 +196,7 @@ def _check_history_files(timestamp_dir, files, content=None): self.assertEqual(1, len(timestamp_dirs)) self.assertIsIso8601(timestamp_dirs[0]) - ts = self.parse_isoformat(timestamp_dirs[0]) + ts = self.parse_isoformat(timestamp_dirs[0]) # pylint: disable=invalid-name self.assertDateTimeCloseTo(ts, datetime.utcnow(), timedelta(seconds=30)) # Ensure saved files contain the right content @@ -219,22 +219,22 @@ def test_archive04(self): test_subject = StateArchiver(os.path.join(self.tmp_dir, 'does-not-exist')) test_subject.purge() - def parse_isoformat(self, s): + def parse_isoformat(self, s): # pylint: disable=invalid-name return datetime.strptime(s, '%Y-%m-%dT%H:%M:%S.%f') - def assertIsIso8601(self, s): + def assertIsIso8601(self, s): # pylint: disable=invalid-name try: self.parse_isoformat(s) except: raise AssertionError("the value '{0}' is not an ISO8601 formatted timestamp".format(s)) - def _total_seconds(self, td): + def _total_seconds(self, td): # pylint: disable=invalid-name """ Compute the total_seconds for a timedelta because 2.6 does not have total_seconds. """ return (0.0 + td.microseconds + (td.seconds + td.days * 24 * 60 * 60) * 10 ** 6) / 10 ** 6 - def assertDateTimeCloseTo(self, t1, t2, within): + def assertDateTimeCloseTo(self, t1, t2, within): # pylint: disable=invalid-name if t1 <= t2: diff = t2 -t1 else: @@ -244,10 +244,10 @@ def assertDateTimeCloseTo(self, t1, t2, within): if secs < 0: self.fail("the timestamps are outside of the tolerance of by {0} seconds".format(secs)) - def assertZipContains(self, zip_fn, files): + def assertZipContains(self, zip_fn, files): # pylint: disable=invalid-name ziph = zipfile.ZipFile(zip_fn, 'r') zip_files = [x.filename for x in ziph.filelist] - for f in files: + for f in files: # pylint: disable=invalid-name self.assertTrue(f in zip_files, "'{0}' was not found in {1}".format(f, zip_fn)) ziph.close() diff --git a/tests/utils/test_crypt_util.py b/tests/utils/test_crypt_util.py index b447ac8025..28c72e5c8e 100644 --- a/tests/utils/test_crypt_util.py +++ b/tests/utils/test_crypt_util.py @@ -29,12 +29,12 @@ class TestCryptoUtilOperations(AgentTestCase): def test_decrypt_encrypted_text(self): encrypted_string = load_data("wire/encrypted.enc") prv_key = os.path.join(self.tmp_dir, "TransportPrivate.pem") - with open(prv_key, 'w+') as c: + with open(prv_key, 'w+') as c: # pylint: disable=invalid-name c.write(load_data("wire/sample.pem")) secret = ']aPPEv}uNg1FPnl?' crypto = CryptUtil(conf.get_openssl_cmd()) decrypted_string = crypto.decrypt_secret(encrypted_string, prv_key) - self.assertEquals(secret, decrypted_string, "decrypted string does not match expected") + self.assertEquals(secret, decrypted_string, "decrypted string does not match expected") # pylint: disable=deprecated-method def test_decrypt_encrypted_text_missing_private_key(self): encrypted_string = load_data("wire/encrypted.enc") @@ -46,7 +46,7 @@ def test_decrypt_encrypted_text_missing_private_key(self): def test_decrypt_encrypted_text_wrong_private_key(self): encrypted_string = load_data("wire/encrypted.enc") prv_key = os.path.join(self.tmp_dir, "wrong.pem") - with open(prv_key, 'w+') as c: + with open(prv_key, 'w+') as c: # pylint: disable=invalid-name c.write(load_data("wire/trans_prv")) crypto = CryptUtil(conf.get_openssl_cmd()) self.assertRaises(CryptError, crypto.decrypt_secret, encrypted_string, prv_key) @@ -54,7 +54,7 @@ def test_decrypt_encrypted_text_wrong_private_key(self): def test_decrypt_encrypted_text_text_not_encrypted(self): encrypted_string = "abc@123" prv_key = os.path.join(self.tmp_dir, "TransportPrivate.pem") - with open(prv_key, 'w+') as c: + with open(prv_key, 'w+') as c: # pylint: disable=invalid-name c.write(load_data("wire/sample.pem")) crypto = CryptUtil(conf.get_openssl_cmd()) self.assertRaises(CryptError, crypto.decrypt_secret, encrypted_string, prv_key) @@ -64,7 +64,7 @@ def test_get_pubkey_from_crt(self): prv_key = os.path.join(data_dir, "wire", "trans_prv") expected_pub_key = os.path.join(data_dir, "wire", "trans_pub") - with open(expected_pub_key) as fh: + with open(expected_pub_key) as fh: # pylint: disable=invalid-name self.assertEqual(fh.read(), crypto.get_pubkey_from_prv(prv_key)) def test_get_pubkey_from_crt_invalid_file(self): diff --git a/tests/utils/test_extension_process_util.py b/tests/utils/test_extension_process_util.py index 0ef7803513..f0fd4287b3 100644 --- a/tests/utils/test_extension_process_util.py +++ b/tests/utils/test_extension_process_util.py @@ -19,10 +19,10 @@ from azurelinuxagent.common.utils.extensionprocessutil import format_stdout_stderr, read_output, \ wait_for_process_completion_or_timeout, handle_process_completion from tests.tools import AgentTestCase, patch -import os -import shutil -import subprocess -import tempfile +import os # pylint: disable=wrong-import-order +import shutil # pylint: disable=wrong-import-order +import subprocess # pylint: disable=wrong-import-order +import tempfile # pylint: disable=wrong-import-order class TestProcessUtils(AgentTestCase): @@ -49,12 +49,12 @@ def test_wait_for_process_completion_or_timeout_should_terminate_cleanly(self): stderr=subprocess.PIPE) timed_out, ret = wait_for_process_completion_or_timeout(process=process, timeout=5) - self.assertEquals(timed_out, False) - self.assertEquals(ret, 0) + self.assertEquals(timed_out, False) # pylint: disable=deprecated-method + self.assertEquals(ret, 0) # pylint: disable=deprecated-method def test_wait_for_process_completion_or_timeout_should_kill_process_on_timeout(self): timeout = 5 - process = subprocess.Popen( + process = subprocess.Popen( # pylint: disable=subprocess-popen-preexec-fn "sleep 1m", shell=True, cwd=self.tmp_dir, @@ -70,11 +70,11 @@ def test_wait_for_process_completion_or_timeout_should_kill_process_on_timeout(s # We're mocking sleep to avoid prolonging the test execution time, but we still want to make sure # we're "waiting" the correct amount of time before killing the process - self.assertEquals(mock_sleep.call_count, timeout) + self.assertEquals(mock_sleep.call_count, timeout) # pylint: disable=deprecated-method - self.assertEquals(patch_kill.call_count, 1) - self.assertEquals(timed_out, True) - self.assertEquals(ret, None) + self.assertEquals(patch_kill.call_count, 1) # pylint: disable=deprecated-method + self.assertEquals(timed_out, True) # pylint: disable=deprecated-method + self.assertEquals(ret, None) # pylint: disable=deprecated-method def test_handle_process_completion_should_return_nonzero_when_process_fails(self): process = subprocess.Popen( @@ -86,14 +86,14 @@ def test_handle_process_completion_should_return_nonzero_when_process_fails(self stderr=subprocess.PIPE) timed_out, ret = wait_for_process_completion_or_timeout(process=process, timeout=5) - self.assertEquals(timed_out, False) - self.assertEquals(ret, 2) + self.assertEquals(timed_out, False) # pylint: disable=deprecated-method + self.assertEquals(ret, 2) # pylint: disable=deprecated-method def test_handle_process_completion_should_return_process_output(self): command = "echo 'dummy stdout' && 1>&2 echo 'dummy stderr'" with tempfile.TemporaryFile(dir=self.tmp_dir, mode="w+b") as stdout: with tempfile.TemporaryFile(dir=self.tmp_dir, mode="w+b") as stderr: - process = subprocess.Popen(command, + process = subprocess.Popen(command, # pylint: disable=subprocess-popen-preexec-fn shell=True, cwd=self.tmp_dir, env={}, @@ -109,7 +109,7 @@ def test_handle_process_completion_should_return_process_output(self): error_code=42) expected_output = "[stdout]\ndummy stdout\n\n\n[stderr]\ndummy stderr\n" - self.assertEquals(process_output, expected_output) + self.assertEquals(process_output, expected_output) # pylint: disable=deprecated-method def test_handle_process_completion_should_raise_on_timeout(self): command = "sleep 1m" @@ -118,7 +118,7 @@ def test_handle_process_completion_should_raise_on_timeout(self): with tempfile.TemporaryFile(dir=self.tmp_dir, mode="w+b") as stderr: with patch('time.sleep') as mock_sleep: with self.assertRaises(ExtensionError) as context_manager: - process = subprocess.Popen(command, + process = subprocess.Popen(command, # pylint: disable=subprocess-popen-preexec-fn shell=True, cwd=self.tmp_dir, env={}, @@ -135,9 +135,9 @@ def test_handle_process_completion_should_raise_on_timeout(self): # We're mocking sleep to avoid prolonging the test execution time, but we still want to make sure # we're "waiting" the correct amount of time before killing the process and raising an exception - self.assertEquals(mock_sleep.call_count, timeout) + self.assertEquals(mock_sleep.call_count, timeout) # pylint: disable=deprecated-method - self.assertEquals(context_manager.exception.code, ExtensionErrorCodes.PluginHandlerScriptTimedout) + self.assertEquals(context_manager.exception.code, ExtensionErrorCodes.PluginHandlerScriptTimedout) # pylint: disable=deprecated-method self.assertIn("Timeout({0})".format(timeout), ustr(context_manager.exception)) def test_handle_process_completion_should_raise_on_nonzero_exit_code(self): @@ -146,7 +146,7 @@ def test_handle_process_completion_should_raise_on_nonzero_exit_code(self): with tempfile.TemporaryFile(dir=self.tmp_dir, mode="w+b") as stdout: with tempfile.TemporaryFile(dir=self.tmp_dir, mode="w+b") as stderr: with self.assertRaises(ExtensionError) as context_manager: - process = subprocess.Popen(command, + process = subprocess.Popen(command, # pylint: disable=subprocess-popen-preexec-fn shell=True, cwd=self.tmp_dir, env={}, @@ -161,7 +161,7 @@ def test_handle_process_completion_should_raise_on_nonzero_exit_code(self): stderr=stderr, error_code=error_code) - self.assertEquals(context_manager.exception.code, error_code) + self.assertEquals(context_manager.exception.code, error_code) # pylint: disable=deprecated-method self.assertIn("Non-zero exit code:", ustr(context_manager.exception)) def test_read_output_should_return_no_content(self): diff --git a/tests/utils/test_file_util.py b/tests/utils/test_file_util.py index c3d4fdb977..889b97adb0 100644 --- a/tests/utils/test_file_util.py +++ b/tests/utils/test_file_util.py @@ -15,7 +15,7 @@ # Requires Python 2.6+ and Openssl 1.0+ # -import errno as errno +import errno as errno # pylint: disable=useless-import-alias import glob import random import string @@ -39,10 +39,10 @@ def test_read_write_file(self): fileutil.write_file(test_file, content) content_read = fileutil.read_file(test_file) - self.assertEquals(content, content_read) + self.assertEquals(content, content_read) # pylint: disable=deprecated-method os.remove(test_file) - def test_write_file_content_is_None(self): + def test_write_file_content_is_None(self): # pylint: disable=invalid-name """ write_file throws when content is None. No file is created. """ @@ -51,8 +51,8 @@ def test_write_file_content_is_None(self): fileutil.write_file(test_file, None) self.fail("expected write_file to throw an exception") - except: - self.assertEquals(False, os.path.exists(test_file)) + except: # pylint: disable=bare-except + self.assertEquals(False, os.path.exists(test_file)) # pylint: disable=deprecated-method def test_rw_utf8_file(self): test_file=os.path.join(self.tmp_dir, self.test_file) @@ -60,7 +60,7 @@ def test_rw_utf8_file(self): fileutil.write_file(test_file, content, encoding="utf-8") content_read = fileutil.read_file(test_file) - self.assertEquals(content, content_read) + self.assertEquals(content, content_read) # pylint: disable=deprecated-method os.remove(test_file) def test_remove_bom(self): @@ -68,7 +68,7 @@ def test_remove_bom(self): data = b'\xef\xbb\xbfhehe' fileutil.write_file(test_file, data, asbin=True) data = fileutil.read_file(test_file, remove_bom=True) - self.assertNotEquals(0xbb, ord(data[0])) + self.assertNotEquals(0xbb, ord(data[0])) # pylint: disable=deprecated-method def test_append_file(self): test_file=os.path.join(self.tmp_dir, self.test_file) @@ -76,13 +76,13 @@ def test_append_file(self): fileutil.append_file(test_file, content) content_read = fileutil.read_file(test_file) - self.assertEquals(content, content_read) + self.assertEquals(content, content_read) # pylint: disable=deprecated-method os.remove(test_file) def test_findre_in_file(self): - fp = tempfile.mktemp() - with open(fp, 'w') as f: + fp = tempfile.mktemp() # pylint: disable=invalid-name + with open(fp, 'w') as f: # pylint: disable=invalid-name f.write( ''' First line @@ -91,25 +91,25 @@ def test_findre_in_file(self): ''' ) - self.assertNotEquals( + self.assertNotEquals( # pylint: disable=deprecated-method None, fileutil.findre_in_file(fp, ".*rst line$")) - self.assertNotEquals( + self.assertNotEquals( # pylint: disable=deprecated-method None, fileutil.findre_in_file(fp, ".*ond line$")) - self.assertNotEquals( + self.assertNotEquals( # pylint: disable=deprecated-method None, fileutil.findre_in_file(fp, ".*with more.*")) - self.assertNotEquals( + self.assertNotEquals( # pylint: disable=deprecated-method None, fileutil.findre_in_file(fp, "^Third.*")) - self.assertEquals( + self.assertEquals( # pylint: disable=deprecated-method None, fileutil.findre_in_file(fp, "^Do not match.*")) def test_findstr_in_file(self): - fp = tempfile.mktemp() - with open(fp, 'w') as f: + fp = tempfile.mktemp() # pylint: disable=invalid-name + with open(fp, 'w') as f: # pylint: disable=invalid-name f.write( ''' First line @@ -127,11 +127,11 @@ def test_findstr_in_file(self): def test_get_last_path_element(self): filepath = '/tmp/abc.def' filename = fileutil.base_name(filepath) - self.assertEquals('abc.def', filename) + self.assertEquals('abc.def', filename) # pylint: disable=deprecated-method filepath = '/tmp/abc' filename = fileutil.base_name(filepath) - self.assertEquals('abc', filename) + self.assertEquals('abc', filename) # pylint: disable=deprecated-method def test_remove_files(self): random_word = lambda : ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(5)) @@ -141,7 +141,7 @@ def test_remove_files(self): test_file2 = os.path.join(self.tmp_dir, 'another_file') test_files = [test_file + random_word() for _ in range(5)] + \ [test_file2 + random_word() for _ in range(5)] - for file in test_files: + for file in test_files: # pylint: disable=redefined-builtin open(file, 'a').close() #Remove files using fileutil.rm_files @@ -154,22 +154,22 @@ def test_remove_files(self): def test_remove_dirs(self): dirs = [] - for n in range(0,5): + for n in range(0,5): # pylint: disable=invalid-name dirs.append(tempfile.mkdtemp()) - for d in dirs: - for n in range(0, random.choice(range(0,10))): + for d in dirs: # pylint: disable=invalid-name + for n in range(0, random.choice(range(0,10))): # pylint: disable=invalid-name fileutil.write_file(os.path.join(d, "test"+str(n)), "content") - for n in range(0, random.choice(range(0,10))): - dd = os.path.join(d, "testd"+str(n)) + for n in range(0, random.choice(range(0,10))): # pylint: disable=invalid-name + dd = os.path.join(d, "testd"+str(n)) # pylint: disable=invalid-name os.mkdir(dd) - for nn in range(0, random.choice(range(0,10))): + for nn in range(0, random.choice(range(0,10))): # pylint: disable=invalid-name os.symlink(dd, os.path.join(dd, "sym"+str(nn))) - for n in range(0, random.choice(range(0,10))): + for n in range(0, random.choice(range(0,10))): # pylint: disable=invalid-name os.symlink(d, os.path.join(d, "sym"+str(n))) fileutil.rm_dirs(*dirs) - for d in dirs: + for d in dirs: # pylint: disable=invalid-name self.assertEqual(len(os.listdir(d)), 0) def test_get_all_files(self): @@ -188,7 +188,7 @@ def test_get_all_files(self): expected_files.extend([test_file_in_subdir + random_word() for _ in range(5)] + \ [test_file_in_subdir2 + random_word() for _ in range(5)]) - for file in expected_files: + for file in expected_files: # pylint: disable=redefined-builtin open(file, 'a').close() # Get All files using fileutil.get_all_files @@ -258,7 +258,7 @@ def test_update_conf_file(self, _): patch_write.assert_called_once_with(path, updated_file) def test_clean_ioerror_ignores_missing(self): - e = IOError() + e = IOError() # pylint: disable=invalid-name e.errno = errno.ENOSPC # Send no paths @@ -269,19 +269,19 @@ def test_clean_ioerror_ignores_missing(self): def test_clean_ioerror_ignores_unless_ioerror(self): try: - d = tempfile.mkdtemp() - fd, f = tempfile.mkstemp() + d = tempfile.mkdtemp() # pylint: disable=invalid-name + fd, f = tempfile.mkstemp() # pylint: disable=invalid-name os.close(fd) fileutil.write_file(f, 'Not empty') # Send non-IOError exception - e = Exception() + e = Exception() # pylint: disable=invalid-name fileutil.clean_ioerror(e, paths=[d, f]) self.assertTrue(os.path.isdir(d)) self.assertTrue(os.path.isfile(f)) # Send unrecognized IOError - e = IOError() + e = IOError() # pylint: disable=invalid-name e.errno = errno.EFAULT self.assertFalse(e.errno in fileutil.KNOWN_IOERRORS) fileutil.clean_ioerror(e, paths=[d, f]) @@ -293,23 +293,23 @@ def test_clean_ioerror_ignores_unless_ioerror(self): os.remove(f) def test_clean_ioerror_removes_files(self): - fd, f = tempfile.mkstemp() + fd, f = tempfile.mkstemp() # pylint: disable=invalid-name os.close(fd) fileutil.write_file(f, 'Not empty') - e = IOError() + e = IOError() # pylint: disable=invalid-name e.errno = errno.ENOSPC fileutil.clean_ioerror(e, paths=[f]) self.assertFalse(os.path.isdir(f)) self.assertFalse(os.path.isfile(f)) def test_clean_ioerror_removes_directories(self): - d1 = tempfile.mkdtemp() - d2 = tempfile.mkdtemp() - for n in ['foo', 'bar']: + d1 = tempfile.mkdtemp() # pylint: disable=invalid-name + d2 = tempfile.mkdtemp() # pylint: disable=invalid-name + for n in ['foo', 'bar']: # pylint: disable=invalid-name fileutil.write_file(os.path.join(d2, n), 'Not empty') - e = IOError() + e = IOError() # pylint: disable=invalid-name e.errno = errno.ENOSPC fileutil.clean_ioerror(e, paths=[d1, d2]) self.assertFalse(os.path.isdir(d1)) @@ -319,10 +319,10 @@ def test_clean_ioerror_removes_directories(self): def test_clean_ioerror_handles_a_range_of_errors(self): for err in fileutil.KNOWN_IOERRORS: - e = IOError() + e = IOError() # pylint: disable=invalid-name e.errno = err - d = tempfile.mkdtemp() + d = tempfile.mkdtemp() # pylint: disable=invalid-name fileutil.clean_ioerror(e, paths=[d]) self.assertFalse(os.path.isdir(d)) self.assertFalse(os.path.isfile(d)) diff --git a/tests/utils/test_flexible_version.py b/tests/utils/test_flexible_version.py index 89a7dbfc44..034e3280e0 100644 --- a/tests/utils/test_flexible_version.py +++ b/tests/utils/test_flexible_version.py @@ -1,4 +1,4 @@ -import random +import random # pylint: disable=unused-import import re import unittest @@ -7,23 +7,23 @@ class TestFlexibleVersion(unittest.TestCase): def setUp(self): - self.v = FlexibleVersion() + self.v = FlexibleVersion() # pylint: disable=invalid-name - def test_compile_separator(self): + def test_compile_separator(self): # pylint: disable=useless-return tests = [ '.', '', '-' ] - for t in tests: + for t in tests: # pylint: disable=invalid-name t_escaped = re.escape(t) t_re = re.compile(t_escaped) - self.assertEqual((t_escaped, t_re), self.v._compile_separator(t)) - self.assertEqual(('', re.compile('')), self.v._compile_separator(None)) + self.assertEqual((t_escaped, t_re), self.v._compile_separator(t)) # pylint: disable=protected-access + self.assertEqual(('', re.compile('')), self.v._compile_separator(None)) # pylint: disable=protected-access return - def test_compile_pattern(self): - self.v._compile_pattern() + def test_compile_pattern(self): # pylint: disable=useless-return + self.v._compile_pattern() # pylint: disable=protected-access tests = { '1': True, '1.2': True, @@ -85,10 +85,10 @@ def test_compile_pattern(self): "test: {0} expected: {1} ".format(test, expectation)) return - def test_compile_pattern_sep(self): + def test_compile_pattern_sep(self): # pylint: disable=useless-return self.v.sep = '-' - self.v._compile_pattern() - tests = { + self.v._compile_pattern() # pylint: disable=protected-access + tests = { # pylint: disable=duplicate-key '1': True, '1-2': True, '1-2-3': True, @@ -149,9 +149,9 @@ def test_compile_pattern_sep(self): "test: {0} expected: {1} ".format(test, expectation)) return - def test_compile_pattern_prerel(self): + def test_compile_pattern_prerel(self): # pylint: disable=useless-return self.v.prerel_tags = ('a', 'b', 'c') - self.v._compile_pattern() + self.v._compile_pattern() # pylint: disable=protected-access tests = { '1': True, '1.2': True, @@ -214,63 +214,71 @@ def test_compile_pattern_prerel(self): "test: {0} expected: {1} ".format(test, expectation)) return - def test_ensure_compatible_separators(self): - v1 = FlexibleVersion('1.2.3') - v2 = FlexibleVersion('1-2-3', sep='-') + def test_ensure_compatible_separators(self): # pylint: disable=useless-return + v1 = FlexibleVersion('1.2.3') # pylint: disable=invalid-name + v2 = FlexibleVersion('1-2-3', sep='-') # pylint: disable=invalid-name try: - v1 == v2 - self.assertTrue(False, "Incompatible separators failed to raise an exception") + v1 == v2 # pylint: disable=pointless-statement + self.assertTrue(False, "Incompatible separators failed to raise an exception") # pylint: disable=redundant-unittest-assert except ValueError: pass - except Exception as e: - t = e.__class__.__name__ + except Exception as e: # pylint: disable=invalid-name + t = e.__class__.__name__ # pylint: disable=invalid-name + # pylint: disable=redundant-unittest-assert self.assertTrue(False, "Incompatible separators raised an unexpected exception: {0}" \ .format(t)) + # pylint: enable=redundant-unittest-assert return - def test_ensure_compatible_prerel(self): - v1 = FlexibleVersion('1.2.3', prerel_tags=('alpha', 'beta', 'rc')) - v2 = FlexibleVersion('1.2.3', prerel_tags=('a', 'b', 'c')) + def test_ensure_compatible_prerel(self): # pylint: disable=useless-return + v1 = FlexibleVersion('1.2.3', prerel_tags=('alpha', 'beta', 'rc')) # pylint: disable=invalid-name + v2 = FlexibleVersion('1.2.3', prerel_tags=('a', 'b', 'c')) # pylint: disable=invalid-name try: - v1 == v2 - self.assertTrue(False, "Incompatible prerel_tags failed to raise an exception") + v1 == v2 # pylint: disable=pointless-statement + self.assertTrue(False, "Incompatible prerel_tags failed to raise an exception") # pylint: disable=redundant-unittest-assert except ValueError: pass - except Exception as e: - t = e.__class__.__name__ + except Exception as e: # pylint: disable=invalid-name + t = e.__class__.__name__ # pylint: disable=invalid-name + # pylint: disable=redundant-unittest-assert self.assertTrue(False, "Incompatible prerel_tags raised an unexpected exception: {0}" \ .format(t)) + # pylint: enable=redundant-unittest-assert return - def test_ensure_compatible_prerel_length(self): - v1 = FlexibleVersion('1.2.3', prerel_tags=('a', 'b', 'c')) - v2 = FlexibleVersion('1.2.3', prerel_tags=('a', 'b')) + def test_ensure_compatible_prerel_length(self): # pylint: disable=useless-return + v1 = FlexibleVersion('1.2.3', prerel_tags=('a', 'b', 'c')) # pylint: disable=invalid-name + v2 = FlexibleVersion('1.2.3', prerel_tags=('a', 'b')) # pylint: disable=invalid-name try: - v1 == v2 - self.assertTrue(False, "Incompatible prerel_tags failed to raise an exception") + v1 == v2 # pylint: disable=pointless-statement + self.assertTrue(False, "Incompatible prerel_tags failed to raise an exception") # pylint: disable=redundant-unittest-assert except ValueError: pass - except Exception as e: - t = e.__class__.__name__ + except Exception as e: # pylint: disable=invalid-name + t = e.__class__.__name__ # pylint: disable=invalid-name + # pylint: disable=redundant-unittest-assert self.assertTrue(False, "Incompatible prerel_tags raised an unexpected exception: {0}" \ .format(t)) + # pylint: enable=redundant-unittest-assert return - def test_ensure_compatible_prerel_order(self): - v1 = FlexibleVersion('1.2.3', prerel_tags=('a', 'b')) - v2 = FlexibleVersion('1.2.3', prerel_tags=('b', 'a')) + def test_ensure_compatible_prerel_order(self): # pylint: disable=useless-return + v1 = FlexibleVersion('1.2.3', prerel_tags=('a', 'b')) # pylint: disable=invalid-name + v2 = FlexibleVersion('1.2.3', prerel_tags=('b', 'a')) # pylint: disable=invalid-name try: - v1 == v2 - self.assertTrue(False, "Incompatible prerel_tags failed to raise an exception") + v1 == v2 # pylint: disable=pointless-statement + self.assertTrue(False, "Incompatible prerel_tags failed to raise an exception") # pylint: disable=redundant-unittest-assert except ValueError: pass - except Exception as e: - t = e.__class__.__name__ + except Exception as e: # pylint: disable=invalid-name + t = e.__class__.__name__ # pylint: disable=invalid-name + # pylint: disable=redundant-unittest-assert self.assertTrue(False, "Incompatible prerel_tags raised an unexpected exception: {0}" \ .format(t)) + # pylint: enable=redundant-unittest-assert return - def test_major(self): + def test_major(self): # pylint: disable=useless-return tests = { '1' : 1, '1.2' : 1, @@ -284,7 +292,7 @@ def test_major(self): FlexibleVersion(test).major) return - def test_minor(self): + def test_minor(self): # pylint: disable=useless-return tests = { '1' : 0, '1.2' : 2, @@ -298,7 +306,7 @@ def test_minor(self): FlexibleVersion(test).minor) return - def test_patch(self): + def test_patch(self): # pylint: disable=useless-return tests = { '1' : 0, '1.2' : 0, @@ -312,7 +320,7 @@ def test_patch(self): FlexibleVersion(test).patch) return - def test_parse(self): + def test_parse(self): # pylint: disable=useless-return tests = { "1.2.3.4": ((1, 2, 3, 4), None), "1.2.3.4alpha5": ((1, 2, 3, 4), ('alpha', 5)), @@ -321,11 +329,11 @@ def test_parse(self): } for test in iter(tests): expectation = tests[test] - self.v._parse(test) + self.v._parse(test) # pylint: disable=protected-access self.assertEqual(expectation, (self.v.version, self.v.prerelease)) return - def test_decrement(self): + def test_decrement(self): # pylint: disable=useless-return src_v = FlexibleVersion('1.0.0.0.10') dst_v = FlexibleVersion(str(src_v)) for i in range(1,10): @@ -333,18 +341,18 @@ def test_decrement(self): self.assertEqual(i, src_v.version[-1] - dst_v.version[-1]) return - def test_decrement_disallows_below_zero(self): + def test_decrement_disallows_below_zero(self): # pylint: disable=useless-return try: - FlexibleVersion('1.0') - 1 - self.assertTrue(False, "Decrement failed to raise an exception") + FlexibleVersion('1.0') - 1 # pylint: disable=expression-not-assigned + self.assertTrue(False, "Decrement failed to raise an exception") # pylint: disable=redundant-unittest-assert except ArithmeticError: pass - except Exception as e: - t = e.__class__.__name__ - self.assertTrue(False, "Decrement raised an unexpected exception: {0}".format(t)) + except Exception as e: # pylint: disable=invalid-name + t = e.__class__.__name__ # pylint: disable=invalid-name + self.assertTrue(False, "Decrement raised an unexpected exception: {0}".format(t)) # pylint: disable=redundant-unittest-assert return - def test_increment(self): + def test_increment(self): # pylint: disable=useless-return src_v = FlexibleVersion('1.0.0.0.0') dst_v = FlexibleVersion(str(src_v)) for i in range(1,10): @@ -352,7 +360,7 @@ def test_increment(self): self.assertEqual(i, dst_v.version[-1] - src_v.version[-1]) return - def test_str(self): + def test_str(self): # pylint: disable=useless-return tests = [ '1', '1.2', @@ -405,7 +413,7 @@ def test_str(self): self.assertEqual(test, str(FlexibleVersion(test))) return - def test_creation_from_flexible_version(self): + def test_creation_from_flexible_version(self): # pylint: disable=useless-return tests = [ '1', '1.2', @@ -455,16 +463,16 @@ def test_creation_from_flexible_version(self): '1.2.3.4alpha5', ] for test in tests: - v = FlexibleVersion(test) + v = FlexibleVersion(test) # pylint: disable=invalid-name self.assertEqual(test, str(FlexibleVersion(v))) return def test_repr(self): - v = FlexibleVersion('1,2,3rc4', ',', ['lol', 'rc']) + v = FlexibleVersion('1,2,3rc4', ',', ['lol', 'rc']) # pylint: disable=invalid-name expected = "FlexibleVersion ('1,2,3rc4', ',', ('lol', 'rc'))" self.assertEqual(expected, repr(v)) - def test_order(self): + def test_order(self): # pylint: disable=useless-return test0 = ["1.7.0", "1.7.0rc0", "1.11.0"] expected0 = ['1.7.0rc0', '1.7.0', '1.11.0'] self.assertEqual(expected0, list(map(str, sorted([FlexibleVersion(v) for v in test0])))) diff --git a/tests/utils/test_rest_util.py b/tests/utils/test_rest_util.py index a200a96f7c..ecf0044f90 100644 --- a/tests/utils/test_rest_util.py +++ b/tests/utils/test_rest_util.py @@ -86,54 +86,54 @@ def test_get_and_reset(self): self.assertEqual(2, counts.get("other")) self.assertEqual( {"hostplugin":0, "protocol":0, "other":0}, - restutil.IOErrorCounter._counts) + restutil.IOErrorCounter._counts) # pylint: disable=protected-access -class TestHttpOperations(AgentTestCase): +class TestHttpOperations(AgentTestCase): # pylint: disable=too-many-public-methods def test_parse_url(self): test_uri = "http://abc.def/ghi#hash?jkl=mn" - host, port, secure, rel_uri = restutil._parse_url(test_uri) - self.assertEquals("abc.def", host) - self.assertEquals("/ghi#hash?jkl=mn", rel_uri) + host, port, secure, rel_uri = restutil._parse_url(test_uri) # pylint: disable=unused-variable,protected-access + self.assertEquals("abc.def", host) # pylint: disable=deprecated-method + self.assertEquals("/ghi#hash?jkl=mn", rel_uri) # pylint: disable=deprecated-method test_uri = "http://abc.def/" - host, port, secure, rel_uri = restutil._parse_url(test_uri) - self.assertEquals("abc.def", host) - self.assertEquals("/", rel_uri) - self.assertEquals(False, secure) + host, port, secure, rel_uri = restutil._parse_url(test_uri) # pylint: disable=protected-access + self.assertEquals("abc.def", host) # pylint: disable=deprecated-method + self.assertEquals("/", rel_uri) # pylint: disable=deprecated-method + self.assertEquals(False, secure) # pylint: disable=deprecated-method test_uri = "https://abc.def/ghi?jkl=mn" - host, port, secure, rel_uri = restutil._parse_url(test_uri) - self.assertEquals(True, secure) + host, port, secure, rel_uri = restutil._parse_url(test_uri) # pylint: disable=protected-access + self.assertEquals(True, secure) # pylint: disable=deprecated-method test_uri = "http://abc.def:80/" - host, port, secure, rel_uri = restutil._parse_url(test_uri) - self.assertEquals("abc.def", host) + host, port, secure, rel_uri = restutil._parse_url(test_uri) # pylint: disable=protected-access + self.assertEquals("abc.def", host) # pylint: disable=deprecated-method - host, port, secure, rel_uri = restutil._parse_url("") - self.assertEquals(None, host) - self.assertEquals(rel_uri, "") + host, port, secure, rel_uri = restutil._parse_url("") # pylint: disable=protected-access + self.assertEquals(None, host) # pylint: disable=deprecated-method + self.assertEquals(rel_uri, "") # pylint: disable=deprecated-method - host, port, secure, rel_uri = restutil._parse_url("None") - self.assertEquals(None, host) - self.assertEquals(rel_uri, "None") + host, port, secure, rel_uri = restutil._parse_url("None") # pylint: disable=protected-access + self.assertEquals(None, host) # pylint: disable=deprecated-method + self.assertEquals(rel_uri, "None") # pylint: disable=deprecated-method def test_cleanup_sas_tokens_from_urls_for_normal_cases(self): test_url = "http://abc.def/ghi#hash?jkl=mn" filtered_url = restutil.redact_sas_tokens_in_urls(test_url) - self.assertEquals(test_url, filtered_url) + self.assertEquals(test_url, filtered_url) # pylint: disable=deprecated-method test_url = "http://abc.def:80/" filtered_url = restutil.redact_sas_tokens_in_urls(test_url) - self.assertEquals(test_url, filtered_url) + self.assertEquals(test_url, filtered_url) # pylint: disable=deprecated-method test_url = "http://abc.def/" filtered_url = restutil.redact_sas_tokens_in_urls(test_url) - self.assertEquals(test_url, filtered_url) + self.assertEquals(test_url, filtered_url) # pylint: disable=deprecated-method test_url = "https://abc.def/ghi?jkl=mn" filtered_url = restutil.redact_sas_tokens_in_urls(test_url) - self.assertEquals(test_url, filtered_url) + self.assertEquals(test_url, filtered_url) # pylint: disable=deprecated-method def test_cleanup_sas_tokens_from_urls_containing_sas_tokens(self): # Contains pair of URLs (RawURL, RedactedURL) @@ -177,15 +177,15 @@ def test_cleanup_sas_tokens_from_urls_containing_sas_tokens(self): "=" + restutil.REDACTED_TEXT) ] - for x in urls_tuples: - self.assertEquals(restutil.redact_sas_tokens_in_urls(x[0]), x[1]) + for x in urls_tuples: # pylint: disable=invalid-name + self.assertEquals(restutil.redact_sas_tokens_in_urls(x[0]), x[1]) # pylint: disable=deprecated-method @patch('azurelinuxagent.common.conf.get_httpproxy_port') @patch('azurelinuxagent.common.conf.get_httpproxy_host') def test_get_http_proxy_none_is_default(self, mock_host, mock_port): mock_host.return_value = None mock_port.return_value = None - h, p = restutil._get_http_proxy() + h, p = restutil._get_http_proxy() # pylint: disable=protected-access,invalid-name self.assertEqual(None, h) self.assertEqual(None, p) @@ -194,7 +194,7 @@ def test_get_http_proxy_none_is_default(self, mock_host, mock_port): def test_get_http_proxy_configuration_overrides_env(self, mock_host, mock_port): mock_host.return_value = "host" mock_port.return_value = None - h, p = restutil._get_http_proxy() + h, p = restutil._get_http_proxy() # pylint: disable=protected-access,invalid-name self.assertEqual("host", h) self.assertEqual(None, p) self.assertEqual(1, mock_host.call_count) @@ -205,7 +205,7 @@ def test_get_http_proxy_configuration_overrides_env(self, mock_host, mock_port): def test_get_http_proxy_configuration_requires_host(self, mock_host, mock_port): mock_host.return_value = None mock_port.return_value = None - h, p = restutil._get_http_proxy() + h, p = restutil._get_http_proxy() # pylint: disable=protected-access,invalid-name self.assertEqual(None, h) self.assertEqual(None, p) self.assertEqual(1, mock_host.call_count) @@ -218,7 +218,7 @@ def test_get_http_proxy_http_uses_httpproxy(self, mock_host): 'http_proxy' : 'http://foo.com:80', 'https_proxy' : 'https://bar.com:443' }): - h, p = restutil._get_http_proxy() + h, p = restutil._get_http_proxy() # pylint: disable=protected-access,invalid-name self.assertEqual("foo.com", h) self.assertEqual(80, p) @@ -229,7 +229,7 @@ def test_get_http_proxy_https_uses_httpsproxy(self, mock_host): 'http_proxy' : 'http://foo.com:80', 'https_proxy' : 'https://bar.com:443' }): - h, p = restutil._get_http_proxy(secure=True) + h, p = restutil._get_http_proxy(secure=True) # pylint: disable=protected-access,invalid-name self.assertEqual("bar.com", h) self.assertEqual(443, p) @@ -239,7 +239,7 @@ def test_get_http_proxy_ignores_user_in_httpproxy(self, mock_host): with patch.dict(os.environ, { 'http_proxy' : 'http://user:pw@foo.com:80' }): - h, p = restutil._get_http_proxy() + h, p = restutil._get_http_proxy() # pylint: disable=protected-access,invalid-name self.assertEqual("foo.com", h) self.assertEqual(80, p) @@ -250,7 +250,7 @@ def test_get_no_proxy_with_values_set(self): }): no_proxy_from_environment = restutil.get_no_proxy() - self.assertEquals(len(no_proxy_list), len(no_proxy_from_environment)) + self.assertEquals(len(no_proxy_list), len(no_proxy_from_environment)) # pylint: disable=deprecated-method for i, j in zip(no_proxy_from_environment, no_proxy_list): self.assertEqual(i, j) @@ -264,7 +264,7 @@ def test_get_no_proxy_with_incorrect_variable_set(self): }): no_proxy_from_environment = restutil.get_no_proxy() - self.assertEquals(len(no_proxy_list_cleaned), len(no_proxy_from_environment)) + self.assertEquals(len(no_proxy_list_cleaned), len(no_proxy_from_environment)) # pylint: disable=deprecated-method for i, j in zip(no_proxy_from_environment, no_proxy_list_cleaned): print(i, j) @@ -280,7 +280,7 @@ def test_get_no_proxy_with_ip_addresses_set(self): }): no_proxy_from_environment = restutil.get_no_proxy() - self.assertEquals(len(no_proxy_list), len(no_proxy_from_environment)) + self.assertEquals(len(no_proxy_list), len(no_proxy_from_environment)) # pylint: disable=deprecated-method for i, j in zip(no_proxy_from_environment, no_proxy_list): self.assertEqual(i, j) @@ -307,11 +307,11 @@ def test_address_in_network(self): self.assertFalse(restutil.address_in_network('172.16.0.1', '192.168.1.0/24')) def test_dotted_netmask(self): - self.assertEquals(restutil.dotted_netmask(0), '0.0.0.0') - self.assertEquals(restutil.dotted_netmask(8), '255.0.0.0') - self.assertEquals(restutil.dotted_netmask(16), '255.255.0.0') - self.assertEquals(restutil.dotted_netmask(24), '255.255.255.0') - self.assertEquals(restutil.dotted_netmask(32), '255.255.255.255') + self.assertEquals(restutil.dotted_netmask(0), '0.0.0.0') # pylint: disable=deprecated-method + self.assertEquals(restutil.dotted_netmask(8), '255.0.0.0') # pylint: disable=deprecated-method + self.assertEquals(restutil.dotted_netmask(16), '255.255.0.0') # pylint: disable=deprecated-method + self.assertEquals(restutil.dotted_netmask(24), '255.255.255.0') # pylint: disable=deprecated-method + self.assertEquals(restutil.dotted_netmask(32), '255.255.255.255') # pylint: disable=deprecated-method self.assertRaises(ValueError, restutil.dotted_netmask, 33) def test_bypass_proxy(self): @@ -328,7 +328,7 @@ def test_bypass_proxy(self): @patch("azurelinuxagent.common.future.httpclient.HTTPSConnection") @patch("azurelinuxagent.common.future.httpclient.HTTPConnection") - def test_http_request_direct(self, HTTPConnection, HTTPSConnection): + def test_http_request_direct(self, HTTPConnection, HTTPSConnection): # pylint: disable=invalid-name mock_conn = \ MagicMock(getresponse=\ Mock(return_value=\ @@ -336,7 +336,7 @@ def test_http_request_direct(self, HTTPConnection, HTTPSConnection): HTTPConnection.return_value = mock_conn - resp = restutil._http_request("GET", "foo", "/bar") + resp = restutil._http_request("GET", "foo", "/bar") # pylint: disable=protected-access HTTPConnection.assert_has_calls([ call("foo", 80, timeout=10) @@ -346,12 +346,12 @@ def test_http_request_direct(self, HTTPConnection, HTTPSConnection): call(method="GET", url="/bar", body=None, headers={'User-Agent': HTTP_USER_AGENT, 'Connection': 'close'}) ]) self.assertEqual(1, mock_conn.getresponse.call_count) - self.assertNotEquals(None, resp) - self.assertEquals("TheResults", resp.read()) + self.assertNotEquals(None, resp) # pylint: disable=deprecated-method + self.assertEquals("TheResults", resp.read()) # pylint: disable=deprecated-method @patch("azurelinuxagent.common.future.httpclient.HTTPSConnection") @patch("azurelinuxagent.common.future.httpclient.HTTPConnection") - def test_http_request_direct_secure(self, HTTPConnection, HTTPSConnection): + def test_http_request_direct_secure(self, HTTPConnection, HTTPSConnection): # pylint: disable=invalid-name mock_conn = \ MagicMock(getresponse=\ Mock(return_value=\ @@ -359,7 +359,7 @@ def test_http_request_direct_secure(self, HTTPConnection, HTTPSConnection): HTTPSConnection.return_value = mock_conn - resp = restutil._http_request("GET", "foo", "/bar", secure=True) + resp = restutil._http_request("GET", "foo", "/bar", secure=True) # pylint: disable=protected-access HTTPConnection.assert_not_called() HTTPSConnection.assert_has_calls([ @@ -369,12 +369,12 @@ def test_http_request_direct_secure(self, HTTPConnection, HTTPSConnection): call(method="GET", url="/bar", body=None, headers={'User-Agent': HTTP_USER_AGENT, 'Connection': 'close'}) ]) self.assertEqual(1, mock_conn.getresponse.call_count) - self.assertNotEquals(None, resp) - self.assertEquals("TheResults", resp.read()) + self.assertNotEquals(None, resp) # pylint: disable=deprecated-method + self.assertEquals("TheResults", resp.read()) # pylint: disable=deprecated-method @patch("azurelinuxagent.common.future.httpclient.HTTPSConnection") @patch("azurelinuxagent.common.future.httpclient.HTTPConnection") - def test_http_request_proxy(self, HTTPConnection, HTTPSConnection): + def test_http_request_proxy(self, HTTPConnection, HTTPSConnection): # pylint: disable=invalid-name mock_conn = \ MagicMock(getresponse=\ Mock(return_value=\ @@ -382,7 +382,7 @@ def test_http_request_proxy(self, HTTPConnection, HTTPSConnection): HTTPConnection.return_value = mock_conn - resp = restutil._http_request("GET", "foo", "/bar", + resp = restutil._http_request("GET", "foo", "/bar", # pylint: disable=protected-access proxy_host="foo.bar", proxy_port=23333) HTTPConnection.assert_has_calls([ @@ -393,13 +393,13 @@ def test_http_request_proxy(self, HTTPConnection, HTTPSConnection): call(method="GET", url="http://foo:80/bar", body=None, headers={'User-Agent': HTTP_USER_AGENT, 'Connection': 'close'}) ]) self.assertEqual(1, mock_conn.getresponse.call_count) - self.assertNotEquals(None, resp) - self.assertEquals("TheResults", resp.read()) + self.assertNotEquals(None, resp) # pylint: disable=deprecated-method + self.assertEquals("TheResults", resp.read()) # pylint: disable=deprecated-method @patch("azurelinuxagent.common.utils.restutil._get_http_proxy") @patch("time.sleep") @patch("azurelinuxagent.common.utils.restutil._http_request") - def test_http_request_proxy_with_no_proxy_check(self, _http_request, sleep, mock_get_http_proxy): + def test_http_request_proxy_with_no_proxy_check(self, _http_request, sleep, mock_get_http_proxy): # pylint: disable=unused-argument mock_http_resp = MagicMock() mock_http_resp.read = Mock(return_value="hehe") _http_request.return_value = mock_http_resp @@ -411,13 +411,13 @@ def test_http_request_proxy_with_no_proxy_check(self, _http_request, sleep, mock }): # Test http get resp = restutil.http_get("http://foo.com", use_proxy=True) - self.assertEquals("hehe", resp.read()) - self.assertEquals(0, mock_get_http_proxy.call_count) + self.assertEquals("hehe", resp.read()) # pylint: disable=deprecated-method + self.assertEquals(0, mock_get_http_proxy.call_count) # pylint: disable=deprecated-method # Test http get resp = restutil.http_get("http://bar.com", use_proxy=True) - self.assertEquals("hehe", resp.read()) - self.assertEquals(1, mock_get_http_proxy.call_count) + self.assertEquals("hehe", resp.read()) # pylint: disable=deprecated-method + self.assertEquals(1, mock_get_http_proxy.call_count) # pylint: disable=deprecated-method def test_proxy_conditions_with_no_proxy(self): should_use_proxy = True @@ -429,32 +429,32 @@ def test_proxy_conditions_with_no_proxy(self): 'no_proxy': ",".join(no_proxy_list) }): host = "10.0.0.1" - self.assertEquals(should_use_proxy, use_proxy and not restutil.bypass_proxy(host)) + self.assertEquals(should_use_proxy, use_proxy and not restutil.bypass_proxy(host)) # pylint: disable=deprecated-method host = "foo.com" - self.assertEquals(should_not_use_proxy, use_proxy and not restutil.bypass_proxy(host)) + self.assertEquals(should_not_use_proxy, use_proxy and not restutil.bypass_proxy(host)) # pylint: disable=deprecated-method host = "www.google.com" - self.assertEquals(should_not_use_proxy, use_proxy and not restutil.bypass_proxy(host)) + self.assertEquals(should_not_use_proxy, use_proxy and not restutil.bypass_proxy(host)) # pylint: disable=deprecated-method host = "168.63.129.16" - self.assertEquals(should_not_use_proxy, use_proxy and not restutil.bypass_proxy(host)) + self.assertEquals(should_not_use_proxy, use_proxy and not restutil.bypass_proxy(host)) # pylint: disable=deprecated-method host = "www.bar.com" - self.assertEquals(should_use_proxy, use_proxy and not restutil.bypass_proxy(host)) + self.assertEquals(should_use_proxy, use_proxy and not restutil.bypass_proxy(host)) # pylint: disable=deprecated-method no_proxy_list = ["10.0.0.1/24"] with patch.dict(os.environ, { 'no_proxy': ",".join(no_proxy_list) }): host = "www.bar.com" - self.assertEquals(should_use_proxy, use_proxy and not restutil.bypass_proxy(host)) + self.assertEquals(should_use_proxy, use_proxy and not restutil.bypass_proxy(host)) # pylint: disable=deprecated-method host = "10.0.0.1" - self.assertEquals(should_not_use_proxy, use_proxy and not restutil.bypass_proxy(host)) + self.assertEquals(should_not_use_proxy, use_proxy and not restutil.bypass_proxy(host)) # pylint: disable=deprecated-method host = "10.0.1.1" - self.assertEquals(should_use_proxy, use_proxy and not restutil.bypass_proxy(host)) + self.assertEquals(should_use_proxy, use_proxy and not restutil.bypass_proxy(host)) # pylint: disable=deprecated-method # When No_proxy is empty with patch.dict(os.environ, { @@ -506,7 +506,7 @@ def test_proxy_conditions_with_no_proxy(self): @patch("azurelinuxagent.common.future.httpclient.HTTPSConnection") @patch("azurelinuxagent.common.future.httpclient.HTTPConnection") - def test_http_request_proxy_secure(self, HTTPConnection, HTTPSConnection): + def test_http_request_proxy_secure(self, HTTPConnection, HTTPSConnection): # pylint: disable=invalid-name mock_conn = \ MagicMock(getresponse=\ Mock(return_value=\ @@ -514,7 +514,7 @@ def test_http_request_proxy_secure(self, HTTPConnection, HTTPSConnection): HTTPSConnection.return_value = mock_conn - resp = restutil._http_request("GET", "foo", "/bar", + resp = restutil._http_request("GET", "foo", "/bar", # pylint: disable=protected-access proxy_host="foo.bar", proxy_port=23333, secure=True) @@ -526,23 +526,23 @@ def test_http_request_proxy_secure(self, HTTPConnection, HTTPSConnection): call(method="GET", url="https://foo:443/bar", body=None, headers={'User-Agent': HTTP_USER_AGENT, 'Connection': 'close'}) ]) self.assertEqual(1, mock_conn.getresponse.call_count) - self.assertNotEquals(None, resp) - self.assertEquals("TheResults", resp.read()) + self.assertNotEquals(None, resp) # pylint: disable=deprecated-method + self.assertEquals("TheResults", resp.read()) # pylint: disable=deprecated-method @patch("time.sleep") @patch("azurelinuxagent.common.utils.restutil._http_request") - def test_http_request_with_retry(self, _http_request, sleep): + def test_http_request_with_retry(self, _http_request, sleep): # pylint: disable=unused-argument mock_http_resp = MagicMock() mock_http_resp.read = Mock(return_value="hehe") _http_request.return_value = mock_http_resp # Test http get resp = restutil.http_get("http://foo.bar") - self.assertEquals("hehe", resp.read()) + self.assertEquals("hehe", resp.read()) # pylint: disable=deprecated-method # Test https get resp = restutil.http_get("https://foo.bar") - self.assertEquals("hehe", resp.read()) + self.assertEquals("hehe", resp.read()) # pylint: disable=deprecated-method # Test http failure _http_request.side_effect = httpclient.HTTPException("Http failure") @@ -612,7 +612,7 @@ def test_http_request_retries_with_constant_delay_when_throttled(self, _http_req _http_request.side_effect = [ Mock(status=httpclient.SERVICE_UNAVAILABLE) - for i in range(restutil.DEFAULT_RETRIES) + for i in range(restutil.DEFAULT_RETRIES) # pylint: disable=unused-variable ] + [Mock(status=httpclient.OK)] restutil.http_get("https://foo.bar", @@ -632,7 +632,7 @@ def test_http_request_retries_for_safe_minimum_number_when_throttled(self, _http _http_request.side_effect = [ Mock(status=httpclient.SERVICE_UNAVAILABLE) - for i in range(restutil.THROTTLE_RETRIES-1) + for i in range(restutil.THROTTLE_RETRIES-1) # pylint: disable=unused-variable ] + [Mock(status=httpclient.OK)] restutil.http_get("https://foo.bar", @@ -758,7 +758,7 @@ def test_read_response_error(self): response.status = 'status' response.reason = 'reason' with patch.object(response, 'read') as patch_response: - for s in responses: + for s in responses: # pylint: disable=invalid-name patch_response.return_value = s result = restutil.read_response_error(response) print("RESPONSE: {0}".format(s)) @@ -804,7 +804,7 @@ def test_read_response_bytes(self): self.assertEqual(result, expected_response) try: raise HttpError("{0}".format(result)) - except HttpError as e: + except HttpError as e: # pylint: disable=invalid-name self.assertTrue(result in ustr(e)) diff --git a/tests/utils/test_shell_util.py b/tests/utils/test_shell_util.py index 3565a7ee04..a5e794aada 100644 --- a/tests/utils/test_shell_util.py +++ b/tests/utils/test_shell_util.py @@ -16,7 +16,7 @@ # Requires Python 2.6+ and Openssl 1.0+ # from tests.tools import AgentTestCase, patch -import unittest +import unittest # pylint: disable=wrong-import-order import azurelinuxagent.common.utils.shellutil as shellutil @@ -30,31 +30,31 @@ def test_shellquote(self): class RunTestCase(AgentTestCase): def test_it_should_return_the_exit_code_of_the_command(self): exit_code = shellutil.run("exit 123") - self.assertEquals(123, exit_code) + self.assertEquals(123, exit_code) # pylint: disable=deprecated-method def test_it_should_be_a_pass_thru_to_run_get_output(self): with patch.object(shellutil, "run_get_output", return_value=(0, "")) as mock_run_get_output: shellutil.run("echo hello word!", chk_err=False, expected_errors=[1, 2, 3]) - self.assertEquals(mock_run_get_output.call_count, 1) + self.assertEquals(mock_run_get_output.call_count, 1) # pylint: disable=deprecated-method args, kwargs = mock_run_get_output.call_args - self.assertEquals(args[0], "echo hello word!") - self.assertEquals(kwargs["chk_err"], False) - self.assertEquals(kwargs["expected_errors"], [1, 2, 3]) + self.assertEquals(args[0], "echo hello word!") # pylint: disable=deprecated-method + self.assertEquals(kwargs["chk_err"], False) # pylint: disable=deprecated-method + self.assertEquals(kwargs["expected_errors"], [1, 2, 3]) # pylint: disable=deprecated-method class RunGetOutputTestCase(AgentTestCase): def test_run_get_output(self): output = shellutil.run_get_output(u"ls /") - self.assertNotEquals(None, output) - self.assertEquals(0, output[0]) + self.assertNotEquals(None, output) # pylint: disable=deprecated-method + self.assertEquals(0, output[0]) # pylint: disable=deprecated-method err = shellutil.run_get_output(u"ls /not-exists") - self.assertNotEquals(0, err[0]) + self.assertNotEquals(0, err[0]) # pylint: disable=deprecated-method err = shellutil.run_get_output(u"ls 我") - self.assertNotEquals(0, err[0]) + self.assertNotEquals(0, err[0]) # pylint: disable=deprecated-method def test_it_should_log_the_command(self): command = "echo hello world!" @@ -62,9 +62,9 @@ def test_it_should_log_the_command(self): with patch("azurelinuxagent.common.utils.shellutil.logger", autospec=True) as mock_logger: shellutil.run_get_output(command) - self.assertEquals(mock_logger.verbose.call_count, 1) + self.assertEquals(mock_logger.verbose.call_count, 1) # pylint: disable=deprecated-method - args, kwargs = mock_logger.verbose.call_args + args, kwargs = mock_logger.verbose.call_args # pylint: disable=unused-variable command_in_message = args[1] self.assertEqual(command_in_message, command) @@ -75,17 +75,17 @@ def test_it_should_log_command_failures_as_errors(self): with patch("azurelinuxagent.common.utils.shellutil.logger", autospec=True) as mock_logger: shellutil.run_get_output(command, log_cmd=False) - self.assertEquals(mock_logger.error.call_count, 1) + self.assertEquals(mock_logger.error.call_count, 1) # pylint: disable=deprecated-method - args, kwargs = mock_logger.error.call_args + args, kwargs = mock_logger.error.call_args # pylint: disable=unused-variable message = args[0] # message is similar to "Command: [exit 99], return code: [99], result: []" self.assertIn("[{0}]".format(command), message) self.assertIn("[{0}]".format(return_code), message) - self.assertEquals(mock_logger.verbose.call_count, 0) - self.assertEquals(mock_logger.info.call_count, 0) - self.assertEquals(mock_logger.warn.call_count, 0) + self.assertEquals(mock_logger.verbose.call_count, 0) # pylint: disable=deprecated-method + self.assertEquals(mock_logger.info.call_count, 0) # pylint: disable=deprecated-method + self.assertEquals(mock_logger.warn.call_count, 0) # pylint: disable=deprecated-method def test_it_should_log_expected_errors_as_info(self): return_code = 99 @@ -94,17 +94,17 @@ def test_it_should_log_expected_errors_as_info(self): with patch("azurelinuxagent.common.utils.shellutil.logger", autospec=True) as mock_logger: shellutil.run_get_output(command, log_cmd=False, expected_errors=[return_code]) - self.assertEquals(mock_logger.info.call_count, 1) + self.assertEquals(mock_logger.info.call_count, 1) # pylint: disable=deprecated-method - args, kwargs = mock_logger.info.call_args + args, kwargs = mock_logger.info.call_args # pylint: disable=unused-variable message = args[0] # message is similar to "Command: [exit 99], return code: [99], result: []" self.assertIn("[{0}]".format(command), message) self.assertIn("[{0}]".format(return_code), message) - self.assertEquals(mock_logger.verbose.call_count, 0) - self.assertEquals(mock_logger.warn.call_count, 0) - self.assertEquals(mock_logger.error.call_count, 0) + self.assertEquals(mock_logger.verbose.call_count, 0) # pylint: disable=deprecated-method + self.assertEquals(mock_logger.warn.call_count, 0) # pylint: disable=deprecated-method + self.assertEquals(mock_logger.error.call_count, 0) # pylint: disable=deprecated-method def test_it_should_log_unexpected_errors_as_errors(self): return_code = 99 @@ -113,24 +113,24 @@ def test_it_should_log_unexpected_errors_as_errors(self): with patch("azurelinuxagent.common.utils.shellutil.logger", autospec=True) as mock_logger: shellutil.run_get_output(command, log_cmd=False, expected_errors=[return_code + 1]) - self.assertEquals(mock_logger.error.call_count, 1) + self.assertEquals(mock_logger.error.call_count, 1) # pylint: disable=deprecated-method - args, kwargs = mock_logger.error.call_args + args, kwargs = mock_logger.error.call_args # pylint: disable=unused-variable message = args[0] # message is similar to "Command: [exit 99], return code: [99], result: []" self.assertIn("[{0}]".format(command), message) self.assertIn("[{0}]".format(return_code), message) - self.assertEquals(mock_logger.info.call_count, 0) - self.assertEquals(mock_logger.verbose.call_count, 0) - self.assertEquals(mock_logger.warn.call_count, 0) + self.assertEquals(mock_logger.info.call_count, 0) # pylint: disable=deprecated-method + self.assertEquals(mock_logger.verbose.call_count, 0) # pylint: disable=deprecated-method + self.assertEquals(mock_logger.warn.call_count, 0) # pylint: disable=deprecated-method class RunCommandTestCase(AgentTestCase): def test_run_command_should_execute_the_command(self): command = ["echo", "-n", "A TEST STRING"] ret = shellutil.run_command(command) - self.assertEquals(ret, "A TEST STRING") + self.assertEquals(ret, "A TEST STRING") # pylint: disable=deprecated-method def test_run_command_should_raise_an_exception_when_the_command_fails(self): command = ["ls", "-d", "/etc", "nonexistent_file"] @@ -141,9 +141,9 @@ def test_run_command_should_raise_an_exception_when_the_command_fails(self): exception = context_manager.exception self.assertIn("'ls' failed: 2", str(exception)) self.assertIn("No such file or directory", str(exception)) - self.assertEquals(exception.stdout, "/etc\n") + self.assertEquals(exception.stdout, "/etc\n") # pylint: disable=deprecated-method self.assertIn("No such file or directory", exception.stderr) - self.assertEquals(exception.returncode, 2) + self.assertEquals(exception.returncode, 2) # pylint: disable=deprecated-method def test_run_command_should_raise_an_exception_when_it_cannot_execute_the_command(self): command = "nonexistent_command" @@ -160,13 +160,13 @@ def test_run_command_it_should_not_log_by_default(self, mock_logger): def assert_no_message_logged(command): try: shellutil.run_command(command) - except: + except: # pylint: disable=bare-except pass - self.assertEquals(mock_logger.info.call_count, 0) - self.assertEquals(mock_logger.verbose.call_count, 0) - self.assertEquals(mock_logger.warn.call_count, 0) - self.assertEquals(mock_logger.error.call_count, 0) + self.assertEquals(mock_logger.info.call_count, 0) # pylint: disable=deprecated-method + self.assertEquals(mock_logger.verbose.call_count, 0) # pylint: disable=deprecated-method + self.assertEquals(mock_logger.warn.call_count, 0) # pylint: disable=deprecated-method + self.assertEquals(mock_logger.error.call_count, 0) # pylint: disable=deprecated-method assert_no_message_logged(["ls", "nonexistent_file"]) assert_no_message_logged("nonexistent_command") @@ -177,12 +177,12 @@ def test_run_command_it_should_log_an_error_when_log_error_is_set(self): with patch("azurelinuxagent.common.utils.shellutil.logger.error") as mock_log_error: try: shellutil.run_command(command, log_error=True) - except: + except: # pylint: disable=bare-except pass - self.assertEquals(mock_log_error.call_count, 1) + self.assertEquals(mock_log_error.call_count, 1) # pylint: disable=deprecated-method - args, kwargs = mock_log_error.call_args + args, kwargs = mock_log_error.call_args # pylint: disable=unused-variable self.assertIn("ls -d /etc nonexistent_file", args, msg="The command was not logged") self.assertIn(2, args, msg="The command's return code was not logged") self.assertIn("/etc\n", args, msg="The command's stdout was not logged") @@ -193,10 +193,10 @@ def test_run_command_it_should_log_an_error_when_log_error_is_set(self): with patch("azurelinuxagent.common.utils.shellutil.logger.error") as mock_log_error: try: shellutil.run_command(command, log_error=True) - except: + except: # pylint: disable=bare-except pass - self.assertEquals(mock_log_error.call_count, 1) + self.assertEquals(mock_log_error.call_count, 1) # pylint: disable=deprecated-method args, kwargs = mock_log_error.call_args self.assertIn(command, args, msg="The command was not logged") @@ -208,7 +208,7 @@ def test_run_command_it_should_read_from_stdin_if_cmd_input_is_set(self): random_hash = ''.join(random.choice('0123456789ABCDEF') for _ in range(16)) try: output = shellutil.run_command(command, cmd_input=random_hash) - except: + except: # pylint: disable=bare-except self.fail("No exception should've been thrown when trying to read from stdin in run_command") self.assertEqual(output, random_hash, "We're reading from stdin and printing it shell, output should match") diff --git a/tests/utils/test_text_util.py b/tests/utils/test_text_util.py index 0701704aed..1a40605385 100644 --- a/tests/utils/test_text_util.py +++ b/tests/utils/test_text_util.py @@ -15,12 +15,12 @@ # Requires Python 2.6+ and Openssl 1.0+ # -from distutils.version import LooseVersion as Version +from distutils.version import LooseVersion as Version # pylint: disable=no-name-in-module,import-error from tests.tools import AgentTestCase -import hashlib -import os -import unittest +import hashlib # pylint: disable=wrong-import-order +import os # pylint: disable=wrong-import-order +import unittest # pylint: disable=wrong-import-order import azurelinuxagent.common.utils.textutil as textutil @@ -35,7 +35,7 @@ def test_get_password_hash(self): data = textutil.remove_bom(data) data = ustr(data, encoding='utf-8') password_hash = textutil.gen_password_hash(data, 6, 10) - self.assertNotEquals(None, password_hash) + self.assertNotEquals(None, password_hash) # pylint: disable=deprecated-method def test_replace_non_ascii(self): data = ustr(b'\xef\xbb\xbfhehe', encoding='utf-8') @@ -54,31 +54,31 @@ def test_remove_bom(self): #Test bom could be removed data = ustr(b'\xef\xbb\xbfhehe', encoding='utf-8') data = textutil.remove_bom(data) - self.assertNotEquals(0xbb, data[0]) + self.assertNotEquals(0xbb, data[0]) # pylint: disable=deprecated-method #bom is comprised of a sequence of three bytes and ff length of the input is shorter # than three bytes, remove_bom should not do anything data = u"\xa7" data = textutil.remove_bom(data) - self.assertEquals(data, data[0]) + self.assertEquals(data, data[0]) # pylint: disable=deprecated-method data = u"\xa7\xef" data = textutil.remove_bom(data) - self.assertEquals(u"\xa7", data[0]) - self.assertEquals(u"\xef", data[1]) + self.assertEquals(u"\xa7", data[0]) # pylint: disable=deprecated-method + self.assertEquals(u"\xef", data[1]) # pylint: disable=deprecated-method #Test string without BOM is not affected data = u"hehe" data = textutil.remove_bom(data) - self.assertEquals(u"h", data[0]) + self.assertEquals(u"h", data[0]) # pylint: disable=deprecated-method data = u"" data = textutil.remove_bom(data) - self.assertEquals(u"", data) + self.assertEquals(u"", data) # pylint: disable=deprecated-method data = u" " data = textutil.remove_bom(data) - self.assertEquals(u" ", data) + self.assertEquals(u" ", data) # pylint: disable=deprecated-method def test_version_compare(self): self.assertTrue(Version("1.0") < Version("1.1")) @@ -102,14 +102,14 @@ def test_get_bytes_from_pem(self): "certificate\n" "-----END CERTIFICATE----\n") base64_bytes = textutil.get_bytes_from_pem(content) - self.assertEquals("certificate", base64_bytes) + self.assertEquals("certificate", base64_bytes) # pylint: disable=deprecated-method content = ("-----BEGIN PRIVATE KEY-----\n" "private key\n" "-----END PRIVATE Key-----\n") base64_bytes = textutil.get_bytes_from_pem(content) - self.assertEquals("private key", base64_bytes) + self.assertEquals("private key", base64_bytes) # pylint: disable=deprecated-method def test_swap_hexstring(self): data = [ @@ -139,7 +139,7 @@ def test_swap_hexstring(self): ['aBcdEf12', 4, 'Ef12aBcd'] ] - for t in data: + for t in data: # pylint: disable=invalid-name self.assertEqual(t[2], textutil.swap_hexstring(t[0], width=t[1])) def test_compress(self):