def _add_boot_config_options(self, extra_config): """ Helper function to add a block of options (text) to the boot/config.txt Args: extra_config - unindedted multiline or not str as it would go into the .txt """ config_path = '/boot/config.txt' use_transactions = False # if we can't use transactions, fall back to editting the file directly tmp_path = config_path try: try: # append uart config to config.txt from kano_settings.boot_config import _trans, \ end_config_transaction use_transactions = True tmp_path = '/tmp/config.tmp' _trans().copy_to(tmp_path) except ImportError: pass with open(tmp_path, 'a') as tmp_config: tmp_config.write(extra_config) if use_transactions: _trans().copy_from(tmp_path) end_config_transaction() except: logger.error("failed to update config")
def test_write(configs): if self.backup_config_exists: which = random.randint(0, 2) else: which = random.randint(0, 1) present = random.randint(0, 1) == 1 before_write = configs['current'].copy() if which == 0: # test set_config_value key = choose_key(present, configs['current'].values) if random.randint(0, 1): value = None print "testing set_config_value({},{})".format(key, value) boot_config.set_config_value(key, value) configs['written'].values[key] = 0 configs['written'].comment_keys[key] = True else: value = random.randint(0, 1000) print "testing set_config_value({},{})".format(key, value) boot_config.set_config_value(key, value) configs['written'].values[key] = value configs['written'].comment_keys[key] = False elif which == 1: # test set_config_comment key = choose_key(present, configs['current'].comment_values) value = str(random.randint(0, 1000)) print "testing set_config_comment({},{})".format(key, value) boot_config.set_config_comment(key, value) configs['written'].comment_values[key] = value elif which == 2: # test restore_backup self.assertTrue( self.backup_config_exists) # bug in test if triggered print "testing safe_mode_restore_backup" boot_config.safe_mode_restore_config() configs['written'] = read_config( boot_config._trans().temp_path) self.backup_config_exists = False self.assertTrue(compare(configs['backup'], configs['written'])) after_write = read_config() self.assertTrue(compare(before_write, after_write)) self.assertTrue(boot_config._trans().state == 2) self.assertTrue(is_locked())
def test_write(configs): if self.backup_config_exists: which = random.randint(0, 2) else: which = random.randint(0, 1) present = random.randint(0, 1) == 1 before_write = configs['current'].copy() if which == 0: # test set_config_value key = choose_key(present, configs['current'].values) if random.randint(0, 1): value = None print "testing set_config_value({},{})".format(key, value) boot_config.set_config_value(key, value) configs['written'].values[key] = 0 configs['written'].comment_keys[key] = True else: value = random.randint(0, 1000) print "testing set_config_value({},{})".format(key, value) boot_config.set_config_value(key, value) configs['written'].values[key] = value configs['written'].comment_keys[key] = False elif which == 1: # test set_config_comment key = choose_key(present, configs['current'].comment_values) value = str(random.randint(0, 1000)) print "testing set_config_comment({},{})".format(key, value) boot_config.set_config_comment(key, value) configs['written'].comment_values[key] = value elif which == 2: # test restore_backup self.assertTrue(self.backup_config_exists) # bug in test if triggered print "testing safe_mode_restore_backup" boot_config.safe_mode_restore_config() configs['written'] = read_config(boot_config._trans().temp_path) self.backup_config_exists = False self.assertTrue(compare(configs['backup'], configs['written'])) after_write = read_config() self.assertTrue(compare(before_write, after_write)) self.assertTrue(boot_config._trans().state == 2) self.assertTrue(is_locked())
def test_remove_noobs(configs): # FIXME this is not properly tested yet before_write = configs['current'].copy() present = boot_config.noobs_line in before_write.comments # test remove_noobs_defaults boot_config.remove_noobs_defaults() print "testing remove_noobs_defaults" after_write = read_config() self.assertTrue(compare(before_write, after_write)) if present: self.assertTrue(boot_config._trans().state == 2) else: self.assertTrue(boot_config._trans().state >= 1) self.assertTrue(is_locked())
def test_close(configs): boot_config.end_config_transaction() configs['current'] = read_config() print "testing close" # all written items should now be present self.assertTrue(compare(configs['current'], configs['written'])) self.assertTrue(boot_config._trans().state == 0) self.assertTrue(not is_locked())
def beta_310_to_beta_320(self): config_path = '/boot/config.txt' use_transactions = False # if we can't use transactions, fall back to editting the file directly tmp_path = config_path try: try: # append uart config to config.txt from kano_settings.boot_config import _trans, \ end_config_transaction use_transactions = True tmp_path = '/tmp/config.tmp' _trans().copy_to(tmp_path) except ImportError: pass from textwrap import dedent extra_config = dedent(""" [pi3] # for light board enable_uart=1 [all] """) with open(tmp_path, 'a') as tmp_config: tmp_config.write(extra_config) if use_transactions: _trans().copy_from(tmp_path) end_config_transaction() except: logger.error("failed to update config")
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())
def test_set_config(self): from kano_settings import boot_config def choose_key(present, data): # Choose either a key of a dictionary, or a key not present if present: key = random.choice(data.keys()) else: # choose a key that is not present key = str(random.randint(0, 10000000)) while key in data.keys(): key = str(random.randint(0, 10000000)) return key def is_locked(): # Check whether the lock is effective. Return true if the lock is held import os py = os.path.join(os.path.dirname(__file__), 'try_lock.py') return os.system('python ' + py) != 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()) def test_write(configs): if self.backup_config_exists: which = random.randint(0, 2) else: which = random.randint(0, 1) present = random.randint(0, 1) == 1 before_write = configs['current'].copy() if which == 0: # test set_config_value key = choose_key(present, configs['current'].values) if random.randint(0, 1): value = None print "testing set_config_value({},{})".format(key, value) boot_config.set_config_value(key, value) configs['written'].values[key] = 0 configs['written'].comment_keys[key] = True else: value = random.randint(0, 1000) print "testing set_config_value({},{})".format(key, value) boot_config.set_config_value(key, value) configs['written'].values[key] = value configs['written'].comment_keys[key] = False elif which == 1: # test set_config_comment key = choose_key(present, configs['current'].comment_values) value = str(random.randint(0, 1000)) print "testing set_config_comment({},{})".format(key, value) boot_config.set_config_comment(key, value) configs['written'].comment_values[key] = value elif which == 2: # test restore_backup self.assertTrue( self.backup_config_exists) # bug in test if triggered print "testing safe_mode_restore_backup" boot_config.safe_mode_restore_config() configs['written'] = read_config( boot_config._trans().temp_path) self.backup_config_exists = False self.assertTrue(compare(configs['backup'], configs['written'])) after_write = read_config() self.assertTrue(compare(before_write, after_write)) self.assertTrue(boot_config._trans().state == 2) self.assertTrue(is_locked()) def test_remove_noobs(configs): # FIXME this is not properly tested yet before_write = configs['current'].copy() present = boot_config.noobs_line in before_write.comments # test remove_noobs_defaults boot_config.remove_noobs_defaults() print "testing remove_noobs_defaults" after_write = read_config() self.assertTrue(compare(before_write, after_write)) if present: self.assertTrue(boot_config._trans().state == 2) else: self.assertTrue(boot_config._trans().state >= 1) self.assertTrue(is_locked()) def test_close(configs): boot_config.end_config_transaction() configs['current'] = read_config() print "testing close" # all written items should now be present self.assertTrue(compare(configs['current'], configs['written'])) self.assertTrue(boot_config._trans().state == 0) self.assertTrue(not is_locked()) with mock_file(config_path, dummy_config_data): self.assertTrue(not is_locked()) orig = read_config() self.assertTrue(boot_config._trans().valid_state()) self.assertTrue(boot_config._trans().state == 0) numtests = random.randint(0, 1000) configs = {} configs['current'] = orig configs['written'] = orig.copy() for trial in range(numtests): print "test number ", trial # os.system('cat /boot/config.txt') # print "___" # os.system('cat '+boot_config._trans().temp_config.path) # print "___" rwc = random.randint(0, 3) if rwc == 0: test_read(configs) elif rwc == 1: test_write(configs) elif rwc == 2: test_remove_noobs(configs) else: test_close(configs) # after each operation, state shuold be valid self.assertTrue(boot_config._trans().valid_state()) # Sanity check - did we actually test anything? os.system('cat /boot/config.txt')
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())
def test_set_config(self): from kano_settings import boot_config def choose_key(present, data): # Choose either a key of a dictionary, or a key not present if present: key = random.choice(data.keys()) else: # choose a key that is not present key = str(random.randint(0, 10000000)) while key in data.keys(): key = str(random.randint(0, 10000000)) return key def is_locked(): # Check whether the lock is effective. Return true if the lock is held import os py = os.path.join(os.path.dirname(__file__), 'try_lock.py') return os.system('python ' + py) != 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()) def test_write(configs): if self.backup_config_exists: which = random.randint(0, 2) else: which = random.randint(0, 1) present = random.randint(0, 1) == 1 before_write = configs['current'].copy() if which == 0: # test set_config_value key = choose_key(present, configs['current'].values) if random.randint(0, 1): value = None print "testing set_config_value({},{})".format(key, value) boot_config.set_config_value(key, value) configs['written'].values[key] = 0 configs['written'].comment_keys[key] = True else: value = random.randint(0, 1000) print "testing set_config_value({},{})".format(key, value) boot_config.set_config_value(key, value) configs['written'].values[key] = value configs['written'].comment_keys[key] = False elif which == 1: # test set_config_comment key = choose_key(present, configs['current'].comment_values) value = str(random.randint(0, 1000)) print "testing set_config_comment({},{})".format(key, value) boot_config.set_config_comment(key, value) configs['written'].comment_values[key] = value elif which == 2: # test restore_backup self.assertTrue(self.backup_config_exists) # bug in test if triggered print "testing safe_mode_restore_backup" boot_config.safe_mode_restore_config() configs['written'] = read_config(boot_config._trans().temp_path) self.backup_config_exists = False self.assertTrue(compare(configs['backup'], configs['written'])) after_write = read_config() self.assertTrue(compare(before_write, after_write)) self.assertTrue(boot_config._trans().state == 2) self.assertTrue(is_locked()) def test_remove_noobs(configs): # FIXME this is not properly tested yet before_write = configs['current'].copy() present = boot_config.noobs_line in before_write.comments # test remove_noobs_defaults boot_config.remove_noobs_defaults() print "testing remove_noobs_defaults" after_write = read_config() self.assertTrue(compare(before_write, after_write)) if present: self.assertTrue(boot_config._trans().state == 2) else: self.assertTrue(boot_config._trans().state >= 1) self.assertTrue(is_locked()) def test_close(configs): boot_config.end_config_transaction() configs['current'] = read_config() print "testing close" # all written items should now be present self.assertTrue(compare(configs['current'], configs['written'])) self.assertTrue(boot_config._trans().state == 0) self.assertTrue(not is_locked()) with mock_file(config_path, dummy_config_data): self.assertTrue(not is_locked()) orig = read_config() self.assertTrue(boot_config._trans().valid_state()) self.assertTrue(boot_config._trans().state == 0) numtests = random.randint(0, 1000) configs = {} configs['current'] = orig configs['written'] = orig.copy() for trial in range(numtests): print "test number ", trial # os.system('cat /boot/config.txt') # print "___" # os.system('cat '+boot_config._trans().temp_config.path) # print "___" rwc = random.randint(0, 3) if rwc == 0: test_read(configs) elif rwc == 1: test_write(configs) elif rwc == 2: test_remove_noobs(configs) else: test_close(configs) # after each operation, state shuold be valid self.assertTrue(boot_config._trans().valid_state()) # Sanity check - did we actually test anything? os.system('cat /boot/config.txt')