Beispiel #1
0
    def test_linux_distribution(self):
        distro_linux_dist = (get_distribution(), get_distribution_version(),
                             get_distribution_codename())

        platform_linux_dist = platform.linux_distribution()

        assert isinstance(distro_linux_dist, type(platform_linux_dist)), \
            'linux_distribution() returned type (%s) which is different from platform.linux_distribution type (%s)' % \
            (type(distro_linux_dist), type(platform_linux_dist))

        # TODO: add the cases where we expect them to differ

        # The third item in the tuple is different.
        assert distro_linux_dist[0] == platform_linux_dist[0]
        assert distro_linux_dist[1] == platform_linux_dist[1]

        if platform_linux_dist[0] == 'Fedora' and 20 < int(
                platform_linux_dist[1]) < 28:
            pytest.skip(
                "Fedora versions between 20 and 28 return the variant instead of the code name making this test unreliable"
            )
            # Fedora considers the platform_linux behaviour to have been a bug as it's finding the
            # variant, not the code name.  Fedora wants this to be the empty string.
            platform_linux_dist = platform_linux_dist[:2] + ('', )
        assert distro_linux_dist[2] == platform_linux_dist[2]
Beispiel #2
0
    def _guess_distribution(self):
        # try to find out which linux distribution this is
        dist = (get_distribution(), get_distribution_version(), get_distribution_codename())
        distribution_guess = {
            'distribution': dist[0] or 'NA',
            'distribution_version': dist[1] or 'NA',
            # distribution_release can be the empty string
            'distribution_release': 'NA' if dist[2] is None else dist[2]
        }

        distribution_guess['distribution_major_version'] = distribution_guess['distribution_version'].split('.')[0] or 'NA'
        return distribution_guess
Beispiel #3
0
    def parse_distribution_file_Flatcar(self, name, data, path, collected_facts):
        flatcar_facts = {}
        distro = get_distribution()

        if distro.lower() == 'flatcar':
            if not data:
                return False, flatcar_facts
            release = re.search("^GROUP=(.*)", data)
            if release:
                flatcar_facts['distribution_release'] = release.group(1).strip('"')
        else:
            return False, flatcar_facts

        return True, flatcar_facts
Beispiel #4
0
    def parse_distribution_file_Coreos(self, name, data, path, collected_facts):
        coreos_facts = {}
        # FIXME: pass in ro copy of facts for this kind of thing
        distro = get_distribution()

        if distro.lower() == 'coreos':
            if not data:
                # include fix from #15230, #15228
                # TODO: verify this is ok for above bugs
                return False, coreos_facts
            release = re.search("^GROUP=(.*)", data)
            if release:
                coreos_facts['distribution_release'] = release.group(1).strip('"')
        else:
            return False, coreos_facts  # TODO: remove if tested without this

        return True, coreos_facts
Beispiel #5
0
    def test_module_utils_basic_get_distribution(self):
        from ansible.module_utils.common.sys_info import get_distribution

        with patch('platform.system', return_value='Foo'):
            self.assertEqual(get_distribution(), None)

        with patch('platform.system', return_value='Linux'):
            with patch('platform.linux_distribution', return_value=["foo"]):
                self.assertEqual(get_distribution(), "Foo")

            with patch('os.path.isfile', return_value=True):
                with patch('platform.linux_distribution',
                           side_effect=[("AmazonFooBar", )]):
                    self.assertEqual(get_distribution(), "Amazonfoobar")

                with patch('platform.linux_distribution',
                           side_effect=(("", ), ("AmazonFooBam", ))):
                    self.assertEqual(get_distribution(), "Amazon")

                with patch('platform.linux_distribution',
                           side_effect=[("", ), ("", )]):
                    self.assertEqual(get_distribution(), "OtherLinux")

                def _dist(distname='',
                          version='',
                          id='',
                          supported_dists=(),
                          full_distribution_name=1):
                    if supported_dists != ():
                        return ("Bar", "2", "Two")
                    else:
                        return ("", "", "")

                with patch('platform.linux_distribution', side_effect=_dist):
                    self.assertEqual(get_distribution(), "Bar")

            with patch('platform.linux_distribution',
                       side_effect=Exception("boo")):
                with patch('platform.dist', return_value=("bar", "2", "Two")):
                    self.assertEqual(get_distribution(), "Bar")
Beispiel #6
0
 def test_distro_amazon_linux_long(self):
     with patch('ansible.module_utils.distro.id', return_value="amazon"):
         assert get_distribution() == "Amazon"
Beispiel #7
0
 def test_distro_unknown(self):
     with patch('ansible.module_utils.distro.id', return_value=""):
         assert get_distribution() == "OtherLinux"
Beispiel #8
0
    def test_distro_known(self):
        with patch('ansible.module_utils.distro.id', return_value="alpine"):
            assert get_distribution() == "Alpine"

        with patch('ansible.module_utils.distro.id', return_value="arch"):
            assert get_distribution() == "Arch"

        with patch('ansible.module_utils.distro.id', return_value="centos"):
            assert get_distribution() == "Centos"

        with patch('ansible.module_utils.distro.id',
                   return_value="clear-linux-os"):
            assert get_distribution() == "Clear-linux-os"

        with patch('ansible.module_utils.distro.id', return_value="coreos"):
            assert get_distribution() == "Coreos"

        with patch('ansible.module_utils.distro.id', return_value="debian"):
            assert get_distribution() == "Debian"

        with patch('ansible.module_utils.distro.id', return_value="flatcar"):
            assert get_distribution() == "Flatcar"

        with patch('ansible.module_utils.distro.id', return_value="linuxmint"):
            assert get_distribution() == "Linuxmint"

        with patch('ansible.module_utils.distro.id', return_value="opensuse"):
            assert get_distribution() == "Opensuse"

        with patch('ansible.module_utils.distro.id', return_value="oracle"):
            assert get_distribution() == "Oracle"

        with patch('ansible.module_utils.distro.id', return_value="raspian"):
            assert get_distribution() == "Raspian"

        with patch('ansible.module_utils.distro.id', return_value="rhel"):
            assert get_distribution() == "Redhat"

        with patch('ansible.module_utils.distro.id', return_value="ubuntu"):
            assert get_distribution() == "Ubuntu"

        with patch('ansible.module_utils.distro.id', return_value="virtuozzo"):
            assert get_distribution() == "Virtuozzo"

        with patch('ansible.module_utils.distro.id', return_value="foo"):
            assert get_distribution() == "Foo"
Beispiel #9
0
def test_get_distribution_not_linux():
    """If it's not Linux, then it has no distribution"""
    with patch('platform.system', return_value='Foo'):
        assert get_distribution() is None
Beispiel #10
0
 def test_distro_amazon_linux(self):
     with patch('ansible.module_utils.distro.name',
                return_value="Amazon Linux AMI"):
         assert get_distribution() == "Amazon"
Beispiel #11
0
 def test_distro_amazon_part_of_another_name(self):
     with patch('ansible.module_utils.distro.name',
                return_value="AmazonFooBar"):
         assert get_distribution() == "Amazonfoobar"
Beispiel #12
0
 def test_distro_known(self):
     with patch('ansible.module_utils.distro.name', return_value="foo"):
         assert get_distribution() == "Foo"
Beispiel #13
0
def test_get_distribution_not_linux(system, dist, mocker):
    """For platforms other than Linux, return the distribution"""
    mocker.patch('platform.system', return_value=system)
    mocker.patch('ansible.module_utils.common.sys_info.distro.id',
                 return_value=dist)
    assert get_distribution() == dist