Exemple #1
0
    def _validate_miimon_conf(self, kwargs, required=True):
        """
        Validate miimon configuration
        """
        # Make copy of kwargs so we don't modify what was passed in
        kwargs = copy.copy(kwargs)

        # Remove miimon and downdelay (if present) to test invalid input
        for key in ("miimon", "downdelay"):
            kwargs.pop(key, None)

        if required:
            # Leaving out miimon should raise an error
            try:
                rh_ip.build_interface(
                    "bond0",
                    "bond",
                    enabled=True,
                    **kwargs,
                )
            except AttributeError as exc:
                assert "miimon" in str(exc)
            else:
                raise Exception("AttributeError was not raised")

        self._validate_miimon_downdelay(kwargs)
Exemple #2
0
    def test_build_interface(self):
        '''
        Test to build an interface script for a network interface.
        '''
        with patch.dict(rh_ip.__grains__, {
                'os': 'Fedora',
                'osmajorrelease': 26
        }):
            with patch.object(rh_ip, '_raise_error_iface', return_value=None):

                self.assertRaises(AttributeError, rh_ip.build_interface,
                                  'iface', 'slave', True)

                with patch.dict(rh_ip.__salt__,
                                {'network.interfaces': lambda: {
                                    'eth': True
                                }}):
                    self.assertRaises(AttributeError,
                                      rh_ip.build_interface,
                                      'iface',
                                      'eth',
                                      True,
                                      netmask='255.255.255.255',
                                      prefix=32,
                                      test=True)

                with patch.object(rh_ip, '_parse_settings_bond', MagicMock()):
                    mock = jinja2.exceptions.TemplateNotFound('foo')
                    with patch.object(jinja2.Environment, 'get_template',
                                      MagicMock(side_effect=mock)):
                        self.assertEqual(
                            rh_ip.build_interface('iface', 'vlan', True), '')

                    with patch.object(rh_ip, '_read_temp', return_value='A'):
                        with patch.object(jinja2.Environment, 'get_template',
                                          MagicMock()):
                            self.assertEqual(
                                rh_ip.build_interface('iface',
                                                      'vlan',
                                                      True,
                                                      test='A'), 'A')

                            with patch.object(rh_ip,
                                              '_write_file_iface',
                                              return_value=None):
                                with patch.object(os.path,
                                                  'join',
                                                  return_value='A'):
                                    with patch.object(rh_ip,
                                                      '_read_file',
                                                      return_value='A'):
                                        self.assertEqual(
                                            rh_ip.build_interface(
                                                'iface', 'vlan', True), 'A')
Exemple #3
0
 def test_build_interface_bond_slave(self):
     """
     Test that bond slave interfaces are properly built
     """
     for version in range(7, 8):
         with patch.dict(
             rh_ip.__grains__, {"osmajorrelease": version, "osrelease": str(version)}
         ):
             results = sorted(
                 rh_ip.build_interface(
                     "eth1",
                     "slave",
                     enabled=True,
                     test=True,
                     master="bond0",
                 )
             )
             expected = [
                 'BOOTPROTO="none"',
                 'DEVICE="eth1"',
                 'MASTER="bond0"',
                 'NM_CONTROLLED="no"',
                 'ONBOOT="yes"',
                 'SLAVE="yes"',
                 'USERCTL="no"',
             ]
             assert results == expected, results
Exemple #4
0
    def test_build_interface_team(self):
        """
        Test that team interfaces are properly built
        """
        dunder_salt = {"pkg.version": MagicMock(return_value="1.29-1.el7")}
        for version in range(7, 8):
            with patch.dict(
                rh_ip.__grains__, {"osmajorrelease": version, "osrelease": str(version)}
            ), patch.dict(rh_ip.__salt__, dunder_salt):
                ret = sorted(
                    rh_ip.build_interface(
                        "team0",
                        "team",
                        enabled=True,
                        test=True,
                        ipaddr="1.2.3.4",
                        team_config={"foo": "bar"},
                    )
                )

            expected = [
                'DEVICE="team0"',
                'DEVICETYPE="Team"',
                'IPADDR="1.2.3.4"',
                'NM_CONTROLLED="no"',
                'ONBOOT="yes"',
                'TEAM_CONFIG=\'{"foo": "bar"}\'',
                'USERCTL="no"',
            ]
            assert ret == expected
Exemple #5
0
    def test_build_interface_teamport(self):
        """
        Test that teamport interfaces are properly built
        """
        ifaces = MagicMock(return_value={"eth1": {"hwaddr": "02:42:ac:11:00:02"}})
        dunder_salt = {"network.interfaces": ifaces}
        for version in range(7, 8):
            with patch.dict(
                rh_ip.__grains__, {"osmajorrelease": version, "osrelease": str(version)}
            ), patch.dict(rh_ip.__salt__, dunder_salt):
                ret = sorted(
                    rh_ip.build_interface(
                        "eth1",
                        "teamport",
                        enabled=True,
                        test=True,
                        team_port_config={"prio": 100},
                        team_master="team0",
                    )
                )

            expected = [
                'DEVICE="eth1"',
                'DEVICETYPE="TeamPort"',
                'HWADDR="02:42:ac:11:00:02"',
                'NM_CONTROLLED="no"',
                'ONBOOT="yes"',
                'TEAM_MASTER="team0"',
                "TEAM_PORT_CONFIG='{\"prio\": 100}'",
                'USERCTL="no"',
            ]
            assert ret == expected, ret
Exemple #6
0
    def test_build_interface(self):
        '''
        Test to build an interface script for a network interface.
        '''
        with patch.dict(rh_ip.__grains__, {'os': 'Fedora'}):
            with patch.object(rh_ip, '_raise_error_iface', return_value=None):

                self.assertRaises(AttributeError,
                                  rh_ip.build_interface,
                                  'iface', 'slave', True)

                with patch.dict(rh_ip.__salt__, {'network.interfaces': lambda: {'eth': True}}):
                    self.assertRaises(AttributeError,
                                      rh_ip.build_interface,
                                      'iface', 'eth', True, netmask='255.255.255.255', prefix=32,
                                      test=True)

                with patch.object(rh_ip, '_parse_settings_bond', MagicMock()):
                    mock = jinja2.exceptions.TemplateNotFound('foo')
                    with patch.object(jinja2.Environment,
                                      'get_template',
                                      MagicMock(side_effect=mock)):
                        self.assertEqual(rh_ip.build_interface('iface',
                                                               'vlan',
                                                               True), '')

                    with patch.object(rh_ip, '_read_temp', return_value='A'):
                        with patch.object(jinja2.Environment,
                                          'get_template', MagicMock()):
                            self.assertEqual(rh_ip.build_interface('iface',
                                                                   'vlan',
                                                                   True,
                                                                   test='A'),
                                             'A')

                            with patch.object(rh_ip, '_write_file_iface',
                                              return_value=None):
                                with patch.object(os.path, 'join',
                                                  return_value='A'):
                                    with patch.object(rh_ip, '_read_file',
                                                      return_value='A'):
                                        self.assertEqual(rh_ip.build_interface
                                                         ('iface', 'vlan',
                                                          True), 'A')
Exemple #7
0
    def _get_bonding_opts(self, kwargs):
        results = rh_ip.build_interface(
            "bond0",
            "bond",
            enabled=True,
            **kwargs,
        )
        self._check_common_opts_bond(results)

        for line in results:
            if line.startswith("BONDING_OPTS="):
                return sorted(line.split("=", 1)[-1].strip('"').split())
        raise Exception("BONDING_OPTS not found")
Exemple #8
0
    def _validate_miimon_downdelay(self, kwargs):
        """
        Validate that downdelay that is not a multiple of miimon raises an error
        """
        # Make copy of kwargs so we don't modify what was passed in
        kwargs = copy.copy(kwargs)

        # Remove miimon and downdelay (if present) to test invalid input
        for key in ("miimon", "downdelay"):
            kwargs.pop(key, None)

        kwargs["miimon"] = 100
        kwargs["downdelay"] = 201
        try:
            rh_ip.build_interface(
                "bond0",
                "bond",
                enabled=True,
                **kwargs,
            )
        except AttributeError as exc:
            assert "multiple of miimon" in str(exc)
        else:
            raise Exception("AttributeError was not raised")
Exemple #9
0
    def test_build_interface(self):
        """
        Test to build an interface script for a network interface.
        """
        with patch.dict(rh_ip.__grains__, {"os": "Fedora", "osmajorrelease": 26}):
            with patch.object(rh_ip, "_raise_error_iface", return_value=None):
                self.assertRaises(
                    AttributeError, rh_ip.build_interface, "iface", "slave", True
                )

                with patch.dict(
                    rh_ip.__salt__, {"network.interfaces": lambda: {"eth": True}}
                ):
                    self.assertRaises(
                        AttributeError,
                        rh_ip.build_interface,
                        "iface",
                        "eth",
                        True,
                        netmask="255.255.255.255",
                        prefix=32,
                        test=True,
                    )
                    self.assertRaises(
                        AttributeError,
                        rh_ip.build_interface,
                        "iface",
                        "eth",
                        True,
                        ipaddrs=["A"],
                        test=True,
                    )
                    self.assertRaises(
                        AttributeError,
                        rh_ip.build_interface,
                        "iface",
                        "eth",
                        True,
                        ipv6addrs=["A"],
                        test=True,
                    )

        for osrelease in range(7, 8):
            with patch.dict(
                rh_ip.__grains__,
                {"os": "RedHat", "osrelease": str(osrelease)},
            ):
                with patch.object(rh_ip, "_raise_error_iface", return_value=None):
                    with patch.object(rh_ip, "_parse_settings_bond", MagicMock()):
                        mock = jinja2.exceptions.TemplateNotFound("foo")
                        with patch.object(
                            jinja2.Environment,
                            "get_template",
                            MagicMock(side_effect=mock),
                        ):
                            self.assertEqual(
                                rh_ip.build_interface("iface", "vlan", True), ""
                            )

                        with patch.object(rh_ip, "_read_temp", return_value="A"):
                            with patch.object(
                                jinja2.Environment, "get_template", MagicMock()
                            ):
                                self.assertEqual(
                                    rh_ip.build_interface(
                                        "iface", "vlan", True, test="A"
                                    ),
                                    "A",
                                )

                                with patch.object(
                                    rh_ip, "_write_file_iface", return_value=None
                                ):
                                    with patch.object(
                                        os.path, "join", return_value="A"
                                    ):
                                        with patch.object(
                                            rh_ip, "_read_file", return_value="A"
                                        ):
                                            self.assertEqual(
                                                rh_ip.build_interface(
                                                    "iface", "vlan", True
                                                ),
                                                "A",
                                            )
                                            if osrelease > 6:
                                                with patch.dict(
                                                    rh_ip.__salt__,
                                                    {
                                                        "network.interfaces": lambda: {
                                                            "eth": True
                                                        }
                                                    },
                                                ):
                                                    self.assertEqual(
                                                        rh_ip.build_interface(
                                                            "iface",
                                                            "eth",
                                                            True,
                                                            ipaddrs=["127.0.0.1/8"],
                                                        ),
                                                        "A",
                                                    )
                                                    self.assertEqual(
                                                        rh_ip.build_interface(
                                                            "iface",
                                                            "eth",
                                                            True,
                                                            ipv6addrs=["fc00::1/128"],
                                                        ),
                                                        "A",
                                                    )