示例#1
0
class TestFileSystem(unittest.TestCase):

    def setUp(self):
        self.fsm = FilesystemModule()
        self.fsm.mount('/', XJFileSystem(1024*1024*10)) # 10 mb partition
        self.fs = Filesystem(self.fsm)
        self.fsm.creat('/prueba.txt', 1,1,0777)

    def test_when_file_exists_for_invalid_path_then_returns_false(self):
        self.assertFalse(self.fs.file_exists('/prueba'))

    def test_when_file_exists_for_valid_path_then_returns_true(self):
        self.assertTrue(self.fs.file_exists('/prueba.txt'))

    def test_when_put_file_binfile_then_binfile_exists(self):
        program = Program()
        program.set_name('binfile')
        self.fs.put_file('/binfile', program)
        self.assertTrue(self.fs.file_exists('/binfile'))

    def test_when_get_file_then_returns_file(self):
        program = Program()
        program.set_name('binfile')
        self.fs.put_file('/binfile', program)
        file = self.fs.get_file('/binfile')
        self.assertEqual(program,file)

    def test_when_mkpath_with_long_path_then_it_should_create_all_intermediate_directories(self):
        path = '/var/log/apache2/errors'
        self.fs.mkpath(path,1,1,0755)

        curr_path = '/'
        traversable_path = path.split('/')
        traversable_path.pop(0)
        for dir_name in traversable_path:
            fd = self.fsm.opendir(curr_path)
            dentries = self.fsm.readdir(fd)
            self.assertTrue(dentries.has_key(dir_name))
            self.fsm.close(fd)
            if curr_path != '/':
                sep = '/'
            else :
                sep = ''
            curr_path = curr_path + sep + dir_name
            print curr_path


    def test_when_list_file_then_returns_a_list_of_all_files_in_fs(self):
        self.fs.put_file('/root/.bashrc', 1)
        self.fs.put_file('/etc/apache/httpd.conf', 2)
        self.fs.put_file('/sbin/ifconfig', 3)
class TestFileSystemModule_MultipleMountFs(unittest.TestCase):

    def setUp(self):
        self.fsm = FilesystemModule()
        self.root_fs     = XJFileSystem(1024*1024*10)
        self.var_fs      = XJFileSystem(1024*1024*10)
        self.home_fs     = XJFileSystem(1024*1024*10)

        self.fsm.mount("/", self.root_fs)
        self.fsm.mount("/var", self.var_fs)
        self.fsm.mount("/home", self.home_fs)

    def test_when_mkdir_etc_then_directory_is_created_under_root_fs(self):
        self.fsm.mkdir("/etc",1,1,0777)
        self.assertTrue(self.root_fs.opendir('/').has_dir('etc'))
        self.assertFalse(self.var_fs.opendir('/').has_dir('etc'))

    def test_when_mkdir_var_log_then_it_is_created_under_var_fs(self):
        self.fsm.mkdir("/var/log",1,1,0777)
        self.assertFalse(self.root_fs.opendir('/').has_dir('log'))
        self.assertTrue(self.var_fs.opendir('/').has_dir('log'))

    def test_when_opendir_var_then_readdir_var_returns_dir_entries_for_root_dir_on_var_fs(self):
        self.fsm.mkdir("/var/log",1,1,0777)

        fd = self.fsm.opendir('/var')
        dentries = self.fsm.readdir(fd)

        self.assertTrue(dentries.has_key('log'))
        self.assertTrue(self.var_fs.opendir('/').has_dir('log'))
class TestFileSystemModule(unittest.TestCase):

    def setUp(self):
        self.fsm = FilesystemModule()

    def test_when_mount_fs_in_root_path_then_select_fs_from_path_returns_corresponding_fs(self):
        self.fsm.mount("/", 1)
        self.assertEqual(self.fsm.select_fs_from_path('/'), 1)

    def test_when_mount_more_than_one_filesystems_then_select_fs_from_path_matches_the_longest_match(self):
        self.fsm.mount("/", 1)
        self.fsm.mount("/var", 2)

        self.assertEqual(self.fsm.select_fs_from_path('/'), 1)
        self.assertEqual(self.fsm.select_fs_from_path('/var/'), 2)
        self.assertEqual(self.fsm.select_fs_from_path('/var/log'), 2)

        self.fsm.mount("/var/log", 3)
        self.assertEqual(self.fsm.select_fs_from_path('/var/log'), 3)

    def test_when_mount_select_fs_from_path_non_mounted_path_then_matches_root_dir(self):
        self.fsm.mount("/", 1)
        self.fsm.mount("/var", 2)
        self.fsm.mount("/var/log", 3)
        self.assertEqual(self.fsm.select_fs_from_path('/home/'), 1)

    def test_when_strip_mount_point_to_var_log_then_its_path_becomes_log(self):
        self.fsm.mount("/", 1)
        self.fsm.mount("/var", 2)
        self.assertEqual(self.fsm.relative_path('/var/log'), '/log')
        self.assertEqual(self.fsm.relative_path('/etc/init.d'), '/etc/init.d')
示例#4
0
class Kernel(object):

    def __init__(self, malloc_strategy, scheduling_policy_class, round_robin_policy_on=True, round_robin_quantum=5):

        self.__mutex = RLock()
        self.__scheduling_policy_class = scheduling_policy_class
        self.__round_robin_policy_on = round_robin_policy_on
        self.__malloc_strategy = malloc_strategy
        self.__pcb_table = PcbTable()
        self.__quantum = round_robin_quantum
        self.g_mode = False

    def set_cpu(self, a_cpu):
        a_cpu.set_kernel(self)
        if self.__round_robin_policy_on:
            a_cpu.enable_round_robin(self.__quantum)
        self.__cpu = a_cpu

    def set_memory(self, a_memory_ram):
        self.__memory_ram = a_memory_ram

    def set_hard_drive(self, a_hard_drive):
        self.__hard_drive = a_hard_drive

    def file_exists(self, path):
        return self.__filesystem.file_exists( path )

    def install_program(self, path, program):
        program.append_instruction(Instruction("Close the program on memory.", "KILL"))
        self.__filesystem.put_file('/bin/' + path, program)

    def get_process_list(self):
        result = {}
        for id, pcb in self.__pcb_table.get_pcb_list().iteritems():
            result[id] = { 'name': pcb.get_program_name() , 'state': pcb.get_state() }
        return result

    def boot(self):
        self.__validate_kernel_setup()
        self.__setup_components()
#        self.get_cpu().start()
        self.__clock.start()

    def fork_execve(self,  path):
        with self.__cpu.pcb_not_set():
            with self.__mutex:
                program = self.__filesystem.get_file(path)
                pcb     = self.__loader.load(program)
                self.get_irq_manager().handle( Irq(NEW_INTERRUPT, pcb) )
            self.__cpu.pcb_not_set().notify()

    def get_mem_manager(self):
        return self.__mem_manager

    def get_filesystem_module(self):
        return self.__fs_module

    def get_partitions(self):
        return self.__hard_drive.partitions()


    def __validate_kernel_setup(self):

        if self.__cpu == None:
            raise NoDeviceException("CPU")

        if self.__hard_drive == None:
            raise NoDeviceException("Hard Drive")

        if self.__memory_ram == None:
            raise NoDeviceException("Memory")

    def __setup_components(self):
        self.__setup_memory()
        self.__setup_scheduling()
        self.__setup_irq_manager()
        self.__setup_clock()
        self.__setup_filesystems()
        self.__setup_program_loader()
        self.__setup_authentication()

    def __setup_memory(self):
        self.__mem_manager = MemoryManager(self.__memory_ram, self.__malloc_strategy)

    def __setup_filesystems(self):
        self.__mount_root()
        self.__filesystem = Filesystem(self.__fs_module) # For backwards compatibility only

    def __mount_root(self):
        self.__fs_module = FilesystemModule()
        self.__fs_module.mount('/', self.__hard_drive.partition(0)) # we assume root partition is the first partition

    def __setup_authentication(self):
        self.__authentication = UsersAccountService(self.__filesystem)

    def __setup_scheduling(self):
        self.set_scheduler(self.__scheduling_policy_class())

    def __setup_clock(self):
        self.__clock = Clock()
        self.__clock.add_listener( self.get_cpu() )


    def __setup_program_loader(self):
        self.__loader = ProgramLoader(self.__mem_manager, self.__pcb_table)

    def get_cpu(self):
        return self.__cpu

    def set_scheduler(self, a_scheduler):
        self.scheduler = a_scheduler
        
    def get_scheduler(self):
        return self.scheduler
        
    def get_memory_ram(self):
        return self.__memory_ram
    
    def get_hard_drive(self):
        return self.__hard_drive
    
    def set_irq_manager(self, a_irq_manager):
        self.irq_manager = a_irq_manager
        
    def get_irq_manager(self):
        return self.irq_manager
    
    def set_ready_queue(self, a_queue):
        self.ready_queue = a_queue
        
    def get_ready_queue(self):
        return self.ready_queue

    def get_fileSystem(self):
        return self.__filesystem

    def get_authentication(self):
        return self.__authentication

    def __setup_irq_manager(self):
        self.irq_manager = IrqManager()
        self.__setup_new_handler()
        self.__setup_io_handler()
        self.__setup_io_end_handler()
        self.__setup_kill_handler()
        self.__setup_timeout_handler()

    def __setup_timeout_handler(self):
        timeout_handler = TimeoutInterruptionHandler(self.get_scheduler(), self.get_cpu())
        self.get_irq_manager().reference_interruption_to_handler(TIMEOUT_INTERRUPT, timeout_handler)

    def __setup_kill_handler(self):
        kill_int_handler = KillInterruptionHandler(self.get_scheduler(), self.get_cpu(), self.__pcb_table, self.__mem_manager)
        self.get_irq_manager().reference_interruption_to_handler(KILL_INTERRUPT, kill_int_handler)

    def __setup_io_end_handler(self):
        io_end_interruption_handler = IOEndInterruptionHandler(self.get_scheduler(), self.get_cpu(), self.get_mem_manager())
        self.get_irq_manager().reference_interruption_to_handler(IO_END_INTERRUPT, io_end_interruption_handler)

    def __setup_io_handler(self):
        io_int_handler = IOInterruptionHandler(self.get_scheduler(), self.get_cpu(), IOManager(self), self.get_mem_manager())
        self.get_irq_manager().reference_interruption_to_handler(IO_INTERRUPT, io_int_handler)

    def __setup_new_handler(self):
        new_int_handler = NewInterruptionHandler(self.get_scheduler(), self.get_cpu())
        self.get_irq_manager().reference_interruption_to_handler(NEW_INTERRUPT, new_int_handler)

    def login(self):
        answer = raw_input('enable operative system with graphical environment? (yes or no)\n')

        if (answer == 'yes') | (answer =='y'):
            self.__log_with_graphical_environment()

        elif (answer == 'no') | (answer =='n'):
            self.__log_with_bash_environment()
        else:
            self.login()

    def __log_with_graphical_environment(self):
        self.g_mode = True
        self.get_authentication().graphical_login()

    def __log_with_bash_environment(self):
        self.get_authentication().login()