def test_get_vf_count_and_functions_many_match(self, gim, gidn):
        # we mock-out get_interface_driver_name and get_interface_macaddress
        # to return useful values for the test
        gim.side_effect = lambda x: '00:01:02:03:04:05' if x == 'enp3' else '00:00:00:00:00:00'
        gidn.side_effect = lambda x: 'foo' if x == 'enp2' else 'bar'
        with open(os.path.join(self.workdir.name, "etc/netplan/test.yaml"),
                  'w') as fd:
            print('''network:
  version: 2
  renderer: networkd
  ethernets:
    renderer: networkd
    enpx:
      match:
        name: enp*
      mtu: 9000
    enpxs16f1:
      link: enpx
''',
                  file=fd)
        self.configmanager.parse()
        interfaces = ['enp1', 'wlp6s0', 'enp2', 'enp3']
        vf_counts = defaultdict(int)
        vfs = {}
        pfs = {}

        # call the function under test
        with self.assertRaises(ConfigurationError) as e:
            sriov.get_vf_count_and_functions(interfaces, self.configmanager,
                                             vf_counts, vfs, pfs)

        self.assertIn('matched more than one interface for a PF device: enpx',
                      str(e.exception))
Пример #2
0
    def test_get_vf_count_and_functions_set_name(self, gim, gidn):
        # we mock-out get_interface_driver_name and get_interface_macaddress
        # to return useful values for the test
        gim.side_effect = lambda x: '00:01:02:03:04:05' if x == 'enp3' else '00:00:00:00:00:00'
        gidn.side_effect = lambda x: 'foo' if x == 'enp1' else 'bar'
        with open(os.path.join(self.workdir.name, "etc/netplan/test.yaml"),
                  'w') as fd:
            print('''network:
  version: 2
  renderer: networkd
  ethernets:
    renderer: networkd
    enp1:
      match:
        driver: foo
      set-name: pf1
    enp8:
      match:
        name: enp[3-8]
      set-name: pf2
      virtual-function-count: 7
    enp1s16f1:
      link: enp1
      macaddress: 01:02:03:04:05:00
''',
                  file=fd)
        self.configmanager.parse()
        interfaces = ['pf1', 'enp8']
        vf_counts = defaultdict(int)
        vfs = {}
        pfs = {}

        # call the function under test
        sriov.get_vf_count_and_functions(interfaces, self.configmanager,
                                         vf_counts, vfs, pfs)
        # check if the right vf counts have been recorded in vf_counts -
        # we expect netplan to take into consideration the renamed interface
        # names here
        self.assertDictEqual(vf_counts, {'pf1': 1, 'enp8': 7})
        # also check if the vfs and pfs dictionaries got properly set
        self.assertDictEqual(vfs, {'enp1s16f1': None})
        self.assertDictEqual(pfs, {'enp1': 'pf1', 'enp8': 'enp8'})
    def test_get_vf_count_and_functions_not_enough_explicit(self, gim, gidn):
        # we mock-out get_interface_driver_name and get_interface_macaddress
        # to return useful values for the test
        gim.side_effect = lambda x: '00:01:02:03:04:05' if x == 'enp3' else '00:00:00:00:00:00'
        gidn.side_effect = lambda x: 'foo' if x == 'enp2' else 'bar'
        with open(os.path.join(self.workdir.name, "etc/netplan/test.yaml"),
                  'w') as fd:
            print('''network:
  version: 2
  renderer: networkd
  ethernets:
    renderer: networkd
    enp1:
      virtual-function-count: 2
      mtu: 9000
    enp1s16f1:
      link: enp1
    enp1s16f2:
      link: enp1
    enp1s16f3:
      link: enp1
''',
                  file=fd)
        self.configmanager.parse()
        interfaces = ['enp1', 'wlp6s0']
        vf_counts = defaultdict(int)
        vfs = {}
        pfs = {}

        # call the function under test
        with self.assertRaises(ConfigurationError) as e:
            sriov.get_vf_count_and_functions(interfaces, self.configmanager,
                                             vf_counts, vfs, pfs)

        self.assertIn(
            'more VFs allocated than the explicit size declared: 3 > 2',
            str(e.exception))
    def test_get_vf_count_and_functions(self, gim, gidn):
        # we mock-out get_interface_driver_name and get_interface_macaddress
        # to return useful values for the test
        gim.side_effect = lambda x: '00:01:02:03:04:05' if x == 'enp3' else '00:00:00:00:00:00'
        gidn.side_effect = lambda x: 'foo' if x == 'enp2' else 'bar'
        with open(os.path.join(self.workdir.name, "etc/netplan/test.yaml"),
                  'w') as fd:
            print('''network:
  version: 2
  renderer: networkd
  ethernets:
    renderer: networkd
    enp1:
      mtu: 9000
    enp2:
      match:
        driver: foo
    enp3:
      match:
        macaddress: 00:01:02:03:04:05
    enpx:
      match:
        name: enp[4-5]
    enp0:
      mtu: 9000
    enp8:
      virtual-function-count: 7
    enp9: {}
    wlp6s0: {}
    enp1s16f1:
      link: enp1
      macaddress: 01:02:03:04:05:00
    enp1s16f2:
      link: enp1
      macaddress: 01:02:03:04:05:01
    enp2s16f1:
      link: enp2
    enp2s16f2: {link: enp2}
    enp3s16f1:
      link: enp3
    enpxs16f1:
      match:
        name: enp[4-5]s16f1
      link: enpx
    enp9s16f1:
      link: enp9
''',
                  file=fd)
        self.configmanager.parse()
        interfaces = ['enp1', 'enp2', 'enp3', 'enp5', 'enp0', 'enp8']
        vf_counts = defaultdict(int)
        vfs = {}
        pfs = {}

        # call the function under test
        sriov.get_vf_count_and_functions(interfaces, self.configmanager,
                                         vf_counts, vfs, pfs)
        # check if the right vf counts have been recorded in vf_counts
        self.assertDictEqual(vf_counts, {
            'enp1': 2,
            'enp2': 2,
            'enp3': 1,
            'enp5': 1,
            'enp8': 7
        })
        # also check if the vfs and pfs dictionaries got properly set
        self.assertDictEqual(
            vfs, {
                'enp1s16f1': None,
                'enp1s16f2': None,
                'enp2s16f1': None,
                'enp2s16f2': None,
                'enp3s16f1': None,
                'enpxs16f1': None
            })
        self.assertDictEqual(
            pfs, {
                'enp1': 'enp1',
                'enp2': 'enp2',
                'enp3': 'enp3',
                'enpx': 'enp5',
                'enp8': 'enp8'
            })