~smoser/curtin/yakkety.lp1666986

« back to all changes in this revision

Viewing changes to tests/vmtests/helpers.py

  • Committer: Scott Moser
  • Date: 2016-07-12 16:28:59 UTC
  • mto: (58.1.1 pkg)
  • mto: This revision was merged to the branch mainline in revision 54.
  • Revision ID: smoser@ubuntu.com-20160712162859-f9jmixbnjsn3keu9
Tags: upstream-0.1.0~bzr399
ImportĀ upstreamĀ versionĀ 0.1.0~bzr399

Show diffs side-by-side

added added

removed removed

Lines of Context:
117
117
                if getattr(test_case, 'release', ''):
118
118
                    releases.add(getattr(test_case, 'release'))
119
119
    return sorted(releases)
 
120
 
 
121
 
 
122
def _parse_ifconfig_xenial(ifconfig_out):
 
123
    """Parse ifconfig output from xenial or earlier and return a dictionary.
 
124
    given content like below, return:
 
125
    {'eth0': {'address': '10.8.1.78', 'broadcast': '10.8.1.255',
 
126
              'inet6': [{'address': 'fe80::216:3eff:fe63:c05d',
 
127
                         'prefixlen': '64', 'scope': 'Link'},
 
128
                        {'address': 'fdec:2922:2f07:0:216:3eff:fe63:c05d',
 
129
                         'prefixlen': '64', 'scope': 'Global'}],
 
130
              'interface': 'eth0', 'link_encap': 'Ethernet',
 
131
              'mac_address': '00:16:3e:63:c0:5d', 'mtu': 1500,
 
132
              'multicast': True, 'netmask': '255.255.255.0',
 
133
              'running': True, 'up': True}}
 
134
 
 
135
    eth0  Link encap:Ethernet  HWaddr 00:16:3e:63:c0:5d
 
136
          inet addr:10.8.1.78  Bcast:10.8.1.255  Mask:255.255.255.0
 
137
          inet6 addr: fe80::216:3eff:fe63:c05d/64 Scope:Link
 
138
          inet6 addr: fdec:2922:2f07:0:216:3eff:fe63:c05d/64 Scope:Global
 
139
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
 
140
          RX packets:21503 errors:0 dropped:0 overruns:0 frame:0
 
141
          TX packets:11346 errors:0 dropped:0 overruns:0 carrier:0
 
142
          collisions:0 txqueuelen:1000
 
143
          RX bytes:31556357 (31.5 MB)  TX bytes:870943 (870.9 KB)
 
144
    """
 
145
    ifaces = {}
 
146
    combined_fields = {'addr': 'address', 'Bcast': 'broadcast',
 
147
                       'Mask': 'netmask', 'MTU': 'mtu',
 
148
                       'encap': 'link_encap'}
 
149
    boolmap = {'RUNNING': 'running', 'UP': 'up', 'MULTICAST': 'multicast'}
 
150
 
 
151
    for line in ifconfig_out.splitlines():
 
152
        if not line:
 
153
            continue
 
154
        if not line.startswith(" "):
 
155
            cur_iface = line.split()[0].rstrip(":")
 
156
            cur_data = {'inet6': [], 'interface': cur_iface}
 
157
            for t in boolmap.values():
 
158
                cur_data[t] = False
 
159
            ifaces[cur_iface] = cur_data
 
160
 
 
161
        toks = line.split()
 
162
 
 
163
        if toks[0] == "inet6":
 
164
            cidr = toks[2]
 
165
            address, prefixlen = cidr.split("/")
 
166
            scope = toks[3].split(":")[1]
 
167
            cur_ipv6 = {'address': address, 'scope': scope,
 
168
                        'prefixlen': prefixlen}
 
169
            cur_data['inet6'].append(cur_ipv6)
 
170
            continue
 
171
 
 
172
        for i in range(0, len(toks)):
 
173
            cur_tok = toks[i]
 
174
            try:
 
175
                next_tok = toks[i+1]
 
176
            except IndexError:
 
177
                next_tok = None
 
178
 
 
179
            if cur_tok == "HWaddr":
 
180
                cur_data['mac_address'] = next_tok
 
181
            elif ":" in cur_tok:
 
182
                key, _colon, val = cur_tok.partition(":")
 
183
                if key in combined_fields:
 
184
                    cur_data[combined_fields[key]] = val
 
185
            elif cur_tok in boolmap:
 
186
                cur_data[boolmap[cur_tok]] = True
 
187
 
 
188
        if 'mtu' in cur_data:
 
189
            cur_data['mtu'] = int(cur_data['mtu'])
 
190
 
 
191
    return ifaces
 
192
 
 
193
 
 
194
def _parse_ifconfig_yakkety(ifconfig_out):
 
195
    """Parse ifconfig output from yakkety or later(?) and return a dictionary.
 
196
 
 
197
    given ifconfig output like below, return:
 
198
    {'ens2': {'address': '10.5.0.78',
 
199
              'broadcast': '10.5.255.255',
 
200
              'broadcast_flag': True,
 
201
              'inet6': [{'address': 'fe80::f816:3eff:fe05:9673',
 
202
                         'prefixlen': '64', 'scopeid': '0x20<link>'},
 
203
                        {'address': 'fe80::f816:3eff:fe05:9673',
 
204
                         'prefixlen': '64', 'scopeid': '0x20<link>'}],
 
205
              'interface': 'ens2', 'link_encap': 'Ethernet',
 
206
              'mac_address': 'fa:16:3e:05:96:73', 'mtu': 1500,
 
207
              'multicast': True, 'netmask': '255.255.0.0',
 
208
              'running': True, 'up': True}}
 
209
 
 
210
    ens2: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
 
211
            inet 10.5.0.78  netmask 255.255.0.0  broadcast 10.5.255.255
 
212
            inet6 fe80::f816:3eff:fe05:9673  prefixlen 64  scopeid 0x20<link>
 
213
            inet6 fe80::f816:3eff:fe05:9673  prefixlen 64  scopeid 0x20<link>
 
214
            ether fa:16:3e:05:96:73  txqueuelen 1000  (Ethernet)
 
215
            RX packets 33196  bytes 48916947 (48.9 MB)
 
216
            RX errors 0  dropped 0  overruns 0  frame 0
 
217
            TX packets 5458  bytes 411486 (411.4 KB)
 
218
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
 
219
    """
 
220
    fmap = {'mtu': 'mtu', 'inet': 'address',
 
221
            'netmask': 'netmask', 'broadcast': 'broadcast',
 
222
            'ether': 'mac_address'}
 
223
    boolmap = {'RUNNING': 'running', 'UP': 'up', 'MULTICAST': 'multicast',
 
224
               'BROADCAST': 'broadcast_flag'}
 
225
 
 
226
    ifaces = {}
 
227
    for line in ifconfig_out.splitlines():
 
228
        if not line:
 
229
            continue
 
230
        if not line.startswith(" "):
 
231
            cur_iface = line.split()[0].rstrip(":")
 
232
            cur_data = {'inet6': [], 'interface': cur_iface}
 
233
            for t in boolmap.values():
 
234
                cur_data[t] = False
 
235
            ifaces[cur_iface] = cur_data
 
236
 
 
237
        toks = line.split()
 
238
        if toks[0] == "inet6":
 
239
            cur_ipv6 = {'address': toks[1]}
 
240
            cur_data['inet6'].append(cur_ipv6)
 
241
 
 
242
        for i in range(0, len(toks)):
 
243
            cur_tok = toks[i]
 
244
            try:
 
245
                next_tok = toks[i+1]
 
246
            except IndexError:
 
247
                next_tok = None
 
248
            if cur_tok in fmap:
 
249
                cur_data[fmap[cur_tok]] = next_tok
 
250
            elif cur_tok in ('prefixlen', 'scopeid'):
 
251
                cur_ipv6[cur_tok] = next_tok
 
252
                cur_data['inet6'].append
 
253
            elif cur_tok.startswith("flags="):
 
254
                # flags=4163<UP,BROADCAST,RUNNING,MULTICAST>
 
255
                flags = cur_tok[cur_tok.find("<") + 1:
 
256
                                cur_tok.rfind(">")].split(",")
 
257
                for flag in flags:
 
258
                    if flag in boolmap:
 
259
                        cur_data[boolmap[flag]] = True
 
260
            elif cur_tok == "(Ethernet)":
 
261
                cur_data['link_encap'] = 'Ethernet'
 
262
 
 
263
        if 'mtu' in cur_data:
 
264
            cur_data['mtu'] = int(cur_data['mtu'])
 
265
 
 
266
    return ifaces
 
267
 
 
268
 
 
269
def ifconfig_to_dict(ifconfig_a):
 
270
    # if the first token of the first line ends in a ':' then assume yakkety
 
271
    # parse ifconfig output and return a dictionary.
 
272
    #
 
273
    # return a dictionary of network information like:
 
274
    #  {'ens2': {'address': '10.5.0.78', 'broadcast': '10.5.255.255',
 
275
    #         'broadcast_flag': True,
 
276
    #         'inet6': [{'address': 'fe80::f816:3eff:fe05:9673',
 
277
    #                    'prefixlen': '64', 'scopeid': '0x20<link>'},
 
278
    #                   {'address': 'fe80::f816:3eff:fe05:9673',
 
279
    #                    'prefixlen': '64', 'scopeid': '0x20<link>'}],
 
280
    #         'interface': 'ens2', 'link_encap': 'Ethernet',
 
281
    #         'mac_address': 'fa:16:3e:05:96:73', 'mtu': 1500,
 
282
    #         'multicast': True, 'netmask': '255.255.0.0',
 
283
    #         'running': True, 'up': True}}
 
284
    line = ifconfig_a.lstrip().splitlines()[0]
 
285
    if line.split()[0].endswith(":"):
 
286
        return _parse_ifconfig_yakkety(ifconfig_a)
 
287
    else:
 
288
        return _parse_ifconfig_xenial(ifconfig_a)