1#
2# Copyright (C) 2017 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#      http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16
17import logging
18from vts.runners.host import const
19from vts.testcases.kernel.api.proc import KernelProcFileTestBase
20from vts.testcases.kernel.api.proc.KernelProcFileTestBase import repeat_rule, literal_token
21
22
23class ProcDiskstatsTest(KernelProcFileTestBase.KernelProcFileTestBase):
24    '''/proc/diskstats displays I/O statistics of block devices.'''
25
26    t_ignore = ' '
27    start = 'lines'
28    p_lines = repeat_rule('line')
29
30    def p_line(self, p):
31        '''line : NUMBER NUMBER STRING NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NEWLINE
32                | NUMBER NUMBER STRING NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NEWLINE
33                | NUMBER NUMBER STRING NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NEWLINE'''
34        p[0] = p[1:]
35
36    def get_path(self):
37        return "/proc/diskstats"
38
39class ProcMountsTest(KernelProcFileTestBase.KernelProcFileTestBase):
40    '''/proc/self/mounts lists the mounted filesystems.
41
42    /proc/mounts must symlink to this file.'''
43
44    def parse_contents(self, contents):
45        if len(contents) == 0 or contents[-1] != '\n':
46            raise SyntaxError('Missing final newline')
47        result = []
48        for line in contents.split('\n')[:-1]:
49            parsed = line.split(' ')
50            parsed[3] = parsed[3].split(',')
51            result.append(parsed)
52        return result
53
54    def result_correct(self, parse_results):
55        for line in parse_results:
56            if len(line[3]) < 1 or line[3][0] not in {'rw', 'ro'}:
57                logging.error("First attribute must be rw or ro")
58                return False
59            if line[4] != '0' or line[5] != '0':
60                logging.error("Last 2 columns must be 0")
61                return False
62        return True
63
64    def prepare_test(self, shell, dut):
65        # Follow the symlink
66        results = shell.Execute('readlink /proc/mounts')
67        if results[const.EXIT_CODE][0] != 0:
68            return False
69        return results[const.STDOUT][0] == 'self/mounts\n'
70
71    def get_path(self):
72        return "/proc/self/mounts"
73
74class ProcFilesystemsTest(KernelProcFileTestBase.KernelProcFileTestBase):
75    '''/proc/filesystems lists filesystems currently supported by kernel.'''
76
77    def parse_contents(self, contents):
78        if len(contents) == 0 or contents[-1] != '\n':
79            raise SyntaxError('Missing final newline')
80
81        result = []
82        for line in contents.split('\n')[:-1]:
83            parsed = line.split('\t')
84            num_columns = len(parsed)
85            if num_columns != 2:
86                raise SyntaxError('Wrong number of columns.')
87            result.append(parsed)
88        return result
89
90    def get_path(self):
91        return "/proc/filesystems"
92
93class ProcSwapsTest(KernelProcFileTestBase.KernelProcFileTestBase):
94    '''/proc/swaps measures swap space utilization.'''
95
96    REQUIRED_COLUMNS = {
97        'Filename',
98        'Type',
99        'Size',
100        'Used',
101        'Priority'
102    }
103
104    def parse_contents(self, contents):
105        if len(contents) == 0 or contents[-1] != '\n':
106            raise SyntaxError('Missing final newline')
107        return map(lambda x: x.split(), contents.split('\n')[:-1])
108
109    def result_correct(self, result):
110        return self.REQUIRED_COLUMNS.issubset(result[0])
111
112    def get_path(self):
113        return "/proc/swaps"
114
115    def file_optional(self, shell=None, dut=None):
116        # It is not mandatory to have this file present
117        return True
118