def test_read(configs):
            # Test all read methods
            # Check the result of the read against the 'written' config set.

            which = random.randint(0, 3)
            present = random.randint(0, 1) == 1
            if which == 0:
                # test get_config_value

                key = choose_key(present, configs['written'].values)

                # test case when key is present and not present

                v = boot_config.get_config_value(key)
                print "testing get_config_value({}) => {}".format(key, v)

                if present:
                    self.assertEqual(v, configs['written'].values[key])
                else:
                    self.assertEqual(v, 0)

            elif which == 1:
                # test get_config_comment

                # test case when key is present and not present
                key = choose_key(present, configs['written'].comment_values)
                if present:
                    correct = random.randint(0, 1)
                    value = configs['written'].comment_values[key]
                    if not correct:
                        value = value + '_wrong'
                else:
                    correct = False
                    value = str(random.randint(0, 10))

                v = boot_config.get_config_comment(key, value)
                print "testing get_config_comment({},{}) => {}".format(key, value, v)

                # get_config comment
                self.assertTrue(v == (present and correct))

            elif which == 2:
                # test has_config_comment

                # test case when key is present and not present
                key = choose_key(present, configs['written'].comment_values)

                v = boot_config.has_config_comment(key)
                print "testing has_config_comment({}) => {}".format(key, v)

                self.assertTrue(v == present)

            elif which == 3:
                # test copy_to

                print "testing safe_mode_config_backup"
                boot_config.safe_mode_backup_config()

                # enable testing of restore
                self.backup_config_exists = True

                configs['backup'] = read_config(boot_config.boot_config_safemode_backup_path)

                self.assertTrue(compare(configs['backup'], configs['written']))

            # after a read, state should be at least locked.
            self.assertTrue(boot_config._trans().state >= 1)

            # after a read, config should be unmodified
            after_read = read_config()

            self.assertTrue(compare(configs['current'], after_read))
            self.assertTrue(is_locked())
Exemplo n.º 2
0
        def test_read(configs):
            # Test all read methods
            # Check the result of the read against the 'written' config set.

            which = random.randint(0, 3)
            present = random.randint(0, 1) == 1
            if which == 0:
                # test get_config_value

                key = choose_key(present, configs['written'].values)

                # test case when key is present and not present

                v = boot_config.get_config_value(key)
                print "testing get_config_value({}) => {}".format(key, v)

                if present:
                    self.assertEqual(v, configs['written'].values[key])
                else:
                    self.assertEqual(v, 0)

            elif which == 1:
                # test get_config_comment

                # test case when key is present and not present
                key = choose_key(present, configs['written'].comment_values)
                if present:
                    correct = random.randint(0, 1)
                    value = configs['written'].comment_values[key]
                    if not correct:
                        value = value + '_wrong'
                else:
                    correct = False
                    value = str(random.randint(0, 10))

                v = boot_config.get_config_comment(key, value)
                print "testing get_config_comment({},{}) => {}".format(
                    key, value, v)

                # get_config comment
                self.assertTrue(v == (present and correct))

            elif which == 2:
                # test has_config_comment

                # test case when key is present and not present
                key = choose_key(present, configs['written'].comment_values)

                v = boot_config.has_config_comment(key)
                print "testing has_config_comment({}) => {}".format(key, v)

                self.assertTrue(v == present)

            elif which == 3:
                # test copy_to

                print "testing safe_mode_config_backup"
                boot_config.safe_mode_backup_config()

                # enable testing of restore
                self.backup_config_exists = True

                configs['backup'] = read_config(
                    boot_config.boot_config_safemode_backup_path)

                self.assertTrue(compare(configs['backup'], configs['written']))

            # after a read, state should be at least locked.
            self.assertTrue(boot_config._trans().state >= 1)

            # after a read, config should be unmodified
            after_read = read_config()

            self.assertTrue(compare(configs['current'], after_read))
            self.assertTrue(is_locked())
Exemplo n.º 3
0
#
# test_config.py
#
# Copyright (C) 2016 Kano Computing Ltd.
# License: http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
#
# This script returns a value depending on whether the config.txt lock is available.
# 0 for no, 1 for yes

from kano_settings import boot_config
from kano.utils.file_operations import TimeoutException
import sys

boot_config.lock_timeout = 0.2
try:
    boot_config.has_config_comment("foo")
    print "Lock was unlocked"

    sys.exit(0)
except TimeoutException:
    print "Lock was locked"
    sys.exit(1)