def test_module_utils_basic_load_platform_subclass(self): class LinuxTest: pass class Foo(LinuxTest): platform = "Linux" distribution = None class Bar(LinuxTest): platform = "Linux" distribution = "Bar" from ansible.module_utils.basic import load_platform_subclass # match just the platform class, not a specific distribution with patch('ansible.module_utils.basic.get_platform', return_value="Linux"): with patch('ansible.module_utils.basic.get_distribution', return_value=None): self.assertIs(type(load_platform_subclass(LinuxTest)), Foo) # match both the distribution and platform class with patch('ansible.module_utils.basic.get_platform', return_value="Linux"): with patch('ansible.module_utils.basic.get_distribution', return_value="Bar"): self.assertIs(type(load_platform_subclass(LinuxTest)), Bar) # if neither match, the fallback should be the top-level class with patch('ansible.module_utils.basic.get_platform', return_value="Foo"): with patch('ansible.module_utils.basic.get_distribution', return_value=None): self.assertIs(type(load_platform_subclass(LinuxTest)), LinuxTest)
def test_module_utils_basic_load_platform_subclass(self): class LinuxTest: pass class Foo(LinuxTest): platform = "Linux" distribution = None class Bar(LinuxTest): platform = "Linux" distribution = "Bar" from ansible.module_utils.basic import load_platform_subclass # match just the platform class, not a specific distribution with patch('ansible.module_utils.basic.get_platform', return_value="Linux"): with patch('ansible.module_utils.basic.get_distribution', return_value=None): self.assertIs(type(load_platform_subclass(LinuxTest)), Foo) # match both the distribution and platform class with patch('ansible.module_utils.basic.get_platform', return_value="Linux"): with patch('ansible.module_utils.basic.get_distribution', return_value="Bar"): self.assertIs(type(load_platform_subclass(LinuxTest)), Bar) # if neither match, the fallback should be the top-level class with patch('ansible.module_utils.basic.get_platform', return_value="Foo"): with patch('ansible.module_utils.basic.get_distribution', return_value=None): self.assertIs(type(load_platform_subclass(LinuxTest)), LinuxTest)
def test_not_linux(self): # if neither match, the fallback should be the top-level class with patch('platform.system', return_value="Foo"): with patch('ansible.module_utils.common.sys_info.get_distribution', return_value=None): assert isinstance(load_platform_subclass(self.LinuxTest), self.LinuxTest)
def __new__(cls, *args, **kwargs): return load_platform_subclass(Hostname, args, kwargs)
def test_get_distribution_found(self): # match both the distribution and platform class with patch('ansible.module_utils.common.sys_info.get_distribution', return_value="Bar"): assert isinstance(load_platform_subclass(self.LinuxTest), self.Bar)
def test_get_distribution_none(self): # match just the platform class, not a specific distribution with patch('ansible.module_utils.common.sys_info.get_distribution', return_value=None): assert isinstance(load_platform_subclass(self.LinuxTest), self.Foo)
def __new__(cls, *args, **kwargs): return load_platform_subclass(TCPConnectionInfo, args, kwargs)
def __new__(cls, *args, **kwargs): return load_platform_subclass(Group, args, kwargs)
def __new__(cls, *args, **kwargs): return load_platform_subclass(TCPConnectionInfo, args, kwargs)
def __new__(cls, *args, **kwargs): return load_platform_subclass(Hostname, args, kwargs)
def __new__(cls, *args, **kwargs): return load_platform_subclass(Group, args, kwargs)