def test_set_locale(self): with mock_file(locale.XSESSION_RC_FILE): params = [ 'LANGUAGE', 'LC_ADDRESS', 'LC_COLLATE', 'LC_CTYPE', 'LC_MONETARY', 'LC_MEASUREMENT', 'LC_MESSAGES', 'LC_NUMERIC', 'LC_PAPER', 'LC_RESPONSE', 'LC_TELEPHONE', 'LC_TIME' ] test_locale = 'en_US.UTF-8' config_lines = [ 'export {}={}'.format(param, test_locale) for param in params ] locale.set_locale(test_locale) config = utils.read_file_contents_as_lines(locale.XSESSION_RC_FILE) for line in config_lines: if line not in config: self.assertFalse(True) self.assertTrue(True)
def run_test_for_param(self, param): with mock_file(locale.XSESSION_RC_FILE): test_locale = 'en_US.UTF-8' config_line = 'export {}={}'.format(param, test_locale) locale.set_locale_param(param, test_locale) config = utils.read_file_contents_as_lines(locale.XSESSION_RC_FILE) self.assertTrue(config_line in config)
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_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')