~rsalveti/linaro-image-tools/generic-hwpack-oe

« back to all changes in this revision

Viewing changes to linaro_image_tools/hwpack/tests/test_hwpack_converter.py

MergeĀ lp:~milo/linaro-image-tools/hwpack-format-converter.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2010, 2011, 2012 Linaro
 
2
#
 
3
# Author: Milo Casagrande <milo.casagrande@linaro.org>
 
4
#
 
5
# This file is part of Linaro Image Tools.
 
6
#
 
7
# Linaro Image Tools is free software; you can redistribute it and/or
 
8
# modify it under the terms of the GNU General Public License
 
9
# as published by the Free Software Foundation; either version 2
 
10
# of the License, or (at your option) any later version.
 
11
#
 
12
# Linaro Image Tools is distributed in the hope that it will be useful,
 
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
# GNU General Public License for more details.
 
16
#
 
17
# You should have received a copy of the GNU General Public License
 
18
# along with Linaro Image Tools; if not, write to the Free Software
 
19
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
 
20
# USA.
 
21
 
 
22
import tempfile
 
23
from linaro_image_tools.testing import TestCaseWithFixtures
 
24
from linaro_image_tools.tests.fixtures import (
 
25
    CreateTempDirFixture,
 
26
    CreateTempFileFixture,
 
27
    )
 
28
 
 
29
from linaro_image_tools.hwpack.hwpack_convert import (
 
30
    HwpackConverter,
 
31
    HwpackConverterException,
 
32
    check_and_validate_args,
 
33
    )
 
34
 
 
35
 
 
36
class Args():
 
37
    """Defines the args for the command line options."""
 
38
    def __init__(self, input_file, output_file=None):
 
39
        self.CONFIG_FILE = input_file
 
40
        self.out = output_file
 
41
 
 
42
 
 
43
class HwpackConverterTests(TestCaseWithFixtures):
 
44
    """Test class for the hwpack converter."""
 
45
 
 
46
    def setUp(self):
 
47
        super(HwpackConverterTests, self).setUp()
 
48
 
 
49
    def test_wrong_input_file(self):
 
50
        """Pass a non-existing file."""
 
51
        input_file = '/tmp/foobaz'
 
52
        self.assertRaises(
 
53
            HwpackConverterException, check_and_validate_args,
 
54
            Args(input_file=input_file))
 
55
 
 
56
    def test_wrong_input_dir(self):
 
57
        """Pass a directory instead of file."""
 
58
        temp_file = tempfile.NamedTemporaryFile()
 
59
        temp_dir = self.useFixture(CreateTempDirFixture()).get_temp_dir()
 
60
        self.assertRaises(
 
61
            HwpackConverterException, check_and_validate_args,
 
62
            Args(input_file=temp_file.name, output_file=temp_dir))
 
63
 
 
64
    def test_same_input_output_file(self):
 
65
        """Pass the same existing file path to the two arguments."""
 
66
        temp_file = self.useFixture(CreateTempFileFixture()).get_file_name()
 
67
        self.assertRaises(
 
68
            HwpackConverterException, check_and_validate_args,
 
69
            Args(input_file=temp_file, output_file=temp_file))
 
70
 
 
71
    def test_basic_parse(self):
 
72
        ini_format = '[hwpack]\nformat=2.0\nsupport=supported'
 
73
        output_format = "format: '3.0'\nsupport: supported\n"
 
74
        input_file = self.useFixture(CreateTempFileFixture(ini_format)).\
 
75
                                                                get_file_name()
 
76
        output_file = self.useFixture(CreateTempFileFixture()).get_file_name()
 
77
        converter = HwpackConverter(input_file, output_file)
 
78
        converter._parse()
 
79
        self.assertEqual(output_format, str(converter))
 
80
 
 
81
    def test_architectures_section_creation(self):
 
82
        """Tests that we create the correct architectures list in the
 
83
        converted file.
 
84
        """
 
85
        ini_format = '[hwpack]\nformat=2.0\narchitectures=armhf armel'
 
86
        output_format = "format: '3.0'\narchitectures:\n- armhf\n- armel\n"
 
87
        input_file = self.useFixture(CreateTempFileFixture(ini_format)).\
 
88
                                                                get_file_name()
 
89
        output_file = self.useFixture(CreateTempFileFixture()).get_file_name()
 
90
        converter = HwpackConverter(input_file, output_file)
 
91
        converter._parse()
 
92
        self.assertEqual(output_format, str(converter))
 
93
 
 
94
    def test_bootloaders(self):
 
95
        """Tests the correct creation of the bootloaders part."""
 
96
        ini_format = ("[hwpack]\nformat=2.0\nu_boot_package=a_package\n"
 
97
                        "u_boot_file=a_file\nu_boot_in_boot_part=Yes\n"
 
98
                        "u_boot_dd=33")
 
99
        out_format = ("format: '3.0'\nbootloaders:\n  u_boot:\n    dd: '33'"
 
100
                        "\n    file: a_file\n    in_boot_part: true\n"
 
101
                        "    package: a_package\n")
 
102
        input_file = self.useFixture(CreateTempFileFixture(ini_format)).\
 
103
                                                                get_file_name()
 
104
        output_file = self.useFixture(CreateTempFileFixture()).get_file_name()
 
105
        converter = HwpackConverter(input_file, output_file)
 
106
        converter._parse()
 
107
        self.assertEqual(out_format, str(converter))
 
108
 
 
109
    def test_extra_boot_options(self):
 
110
        """Tests the correct creation of the extra_boot_options part."""
 
111
        ini_format = ("[hwpack]\nformat=2.0\nu_boot_package=a_package\n"
 
112
                        "extra_boot_options=opt1 opt2")
 
113
        out_format = ("format: '3.0'\nbootloaders:\n  u_boot:\n "
 
114
                        "   extra_boot_options:\n    - opt1\n    "
 
115
                        "- opt2\n    package: a_package\n")
 
116
        input_file = self.useFixture(CreateTempFileFixture(ini_format)).\
 
117
                                                                get_file_name()
 
118
        output_file = self.useFixture(CreateTempFileFixture()).get_file_name()
 
119
        converter = HwpackConverter(input_file, output_file)
 
120
        converter._parse()
 
121
        self.assertEqual(out_format, str(converter))
 
122
 
 
123
    def test_extra_serial_options(self):
 
124
        """Tests the correct creation of the extra_serial_options part."""
 
125
        ini_format = ("[hwpack]\nformat=2.0\nextra_serial_options=opt1 opt2")
 
126
        out_format = ("format: '3.0'\nextra_serial_options:\n- opt1\n- opt2\n")
 
127
        input_file = self.useFixture(CreateTempFileFixture(ini_format)).\
 
128
                                                                get_file_name()
 
129
        output_file = self.useFixture(CreateTempFileFixture()).get_file_name()
 
130
        converter = HwpackConverter(input_file, output_file)
 
131
        converter._parse()
 
132
        self.assertEqual(out_format, str(converter))