Example #1
0
 def test_listeners(self):
     listener = DummyStatusListener()
     listener.notify = MagicMock()
     status = Status()
     status.add_listener(listener)
     status.server.up = False
     listener.notify.assert_called_once_with()
     listener.notify.reset_mock()
     status.server.error_msg = "Everything's good"
     listener.notify.assert_called_once_with()
 def read(self, chunk_handle, start_offset, numbytes):
     start_offset = int(start_offset)
     numbytes = int(numbytes)
     try:
         with open(os.path.join(self.root, chunk_handle), "r") as f:
             f.seek(start_offset)
             ret = f.read(numbytes)
     except Exception as e:
         return Status(-1, "ERROR: " + str(e))
     else:
         return Status(0, ret)
    def delete_file(self, file_path):
        status = self.check_valid_file(file_path)
        if status.v != 0:
            return status

        try:
            self.meta.mark_delete(file_path)
        except Exception as e:
            return Status(-1, "ERROR: " + str(e))
        else:
            return Status(
                0, "SUCCESS: file {} is marked deleted".format(file_path))
Example #4
0
    def test_copy_doesnt_copy_listeners(self):
        status = Status()
        listener = DummyStatusListener()
        listener.notify = MagicMock()
        status.add_listener(listener)
        copy = status.copy()

        status.server.error_msg = "a"
        listener.notify.assert_called_once_with()
        listener.notify.reset_mock()

        copy.server.error_msg = "b"
        listener.notify.assert_not_called()
    def undelete_file(self, file_path):
        if file_path not in self.meta.files:
            return Status(
                -1,
                "ERROR: file {} doesn't exist, already garbage collected or never created"
                .format(file_path))
        elif self.meta.files[file_path].delete is not True:
            return Status(
                -1, "ERROR: file {} is not marked deleted".format(file_path))

        try:
            self.meta.unmark_delete(file_path)
        except Exception as e:
            return Status(-1, "ERROR: " + str(e))
        else:
            return Status(0, "SUCCESS: file {} is restored".format(file_path))
def post_status():
  global status
  action = request.args.get('action')    # query
  print(f'post_status, action: {action}')
  status = Status(int(action))
  pub_web.publish(status.value)
  return 'POST'
 def create_new_file(self, file_path, chunk_handle):
     if file_path in self.files:
         return Status(-1,
                       "ERROR: File exists already: {}".format(file_path))
     fl = File(file_path)
     self.files[file_path] = fl
     status = self.create_new_chunk(file_path, -1, chunk_handle)
     return status
Example #8
0
    def test_components_registered(self):
        # Test that all components were registered
        # This is done through the copy method
        status = Status()

        status.server.up = False
        status.server.error_msg = "an error message"
        copy = status.copy()
        self.assertEqual(False, copy.server.up)
        self.assertEqual("an error message", copy.server.error_msg)

        time1 = datetime.now()
        time2 = datetime.now()
        status.controller.latest_local_scan_time = time1
        status.controller.latest_remote_scan_time = time2
        copy = status.copy()
        self.assertEqual(time1, copy.controller.latest_local_scan_time)
        self.assertEqual(time2, copy.controller.latest_remote_scan_time)
    def append_file(self, file_path):
        status = self.check_valid_file(file_path)
        if status.v != 0:
            return None, None, status

        latest_chunk_handle = self.meta.get_latest_chunk(file_path)
        locs = self.meta.get_chunk_locs(latest_chunk_handle)
        status = Status(0, "Append handled")
        return latest_chunk_handle, locs, status
    def test_event_names(self):
        serialize = SerializeStatus()
        status = Status()
        out = parse_stream(serialize.status(status))
        self.assertEqual("status", out["event"])

        status.server.up = False
        status.server.error_msg = "Bad stuff happened"
        out = parse_stream(serialize.status(status))
        self.assertEqual("status", out["event"])
Example #11
0
    def test_controller_status_latest_remote_scan_failed(self):
        serialize = SerializeStatus()
        status = Status()
        out = parse_stream(serialize.status(status))
        data = json.loads(out["data"])
        self.assertIsNone(data["controller"]["latest_remote_scan_failed"])

        status.controller.latest_remote_scan_failed = True
        out = parse_stream(serialize.status(status))
        data = json.loads(out["data"])
        self.assertEqual(True, data["controller"]["latest_remote_scan_failed"])
    def test_server_status_error_msg(self):
        serialize = SerializeStatus()
        status = Status()
        out = parse_stream(serialize.status(status))
        data = json.loads(out["data"])
        self.assertEqual(None, data["server"]["error_msg"])

        status.server.up = False
        status.server.error_msg = "Bad stuff happened"
        out = parse_stream(serialize.status(status))
        data = json.loads(out["data"])
        self.assertEqual("Bad stuff happened", data["server"]["error_msg"])
Example #13
0
    def test_controller_status_latest_remote_scan_error(self):
        serialize = SerializeStatus()
        status = Status()
        out = parse_stream(serialize.status(status))
        data = json.loads(out["data"])
        self.assertIsNone(data["controller"]["latest_remote_scan_error"])

        status.controller.latest_remote_scan_error = "remote server went boom"
        out = parse_stream(serialize.status(status))
        data = json.loads(out["data"])
        self.assertEqual("remote server went boom",
                         data["controller"]["latest_remote_scan_error"])
    def test_controller_status_latest_remote_scan_time(self):
        serialize = SerializeStatus()
        status = Status()
        out = parse_stream(serialize.status(status))
        data = json.loads(out["data"])
        self.assertIsNone(data["controller"]["latest_remote_scan_time"])

        timestamp = datetime(2018, 11, 9, 21, 40, 18, tzinfo=timezone('UTC'))
        status.controller.latest_remote_scan_time = timestamp
        out = parse_stream(serialize.status(status))
        data = json.loads(out["data"])
        self.assertEqual(str(1541799618.0), data["controller"]["latest_remote_scan_time"])
    def read_file(self, file_path, offset, numbytes):
        status = self.check_valid_file(file_path)
        if status.v != 0:
            return status

        chunk_size = cfg.chunk_size
        start_chunk = offset // chunk_size
        all_chunks = list(self.meta.files[file_path].chunks.keys())
        if start_chunk > len(all_chunks):
            return Status(-1, "ERROR: Offset is too large")

        start_offset = offset % chunk_size

        if numbytes == -1:
            end_offset = chunk_size
            end_chunk = len(all_chunks) - 1
        else:
            end_offset = offset + numbytes - 1
            end_chunk = end_offset // chunk_size
            end_offset = end_offset % chunk_size

        all_chunk_handles = all_chunks[start_chunk:end_chunk + 1]
        ret = []
        for idx, chunk_handle in enumerate(all_chunk_handles):
            if idx == 0:
                stof = start_offset
            else:
                stof = 0
            if idx == len(all_chunk_handles) - 1:
                enof = end_offset
            else:
                enof = chunk_size - 1
            loc = self.meta.files[file_path].chunks[chunk_handle].locs[0]
            ret.append(chunk_handle + "*" + loc + "*" + str(stof) + "*" +
                       str(enof - stof + 1))
        ret = "|".join(ret)
        return Status(0, ret)
    def test_controller_status_latest_remote_scan_time(self):
        serialize = SerializeStatus()
        status = Status()
        out = parse_stream(serialize.status(status))
        data = json.loads(out["data"])
        self.assertIsNone(data["controller"]["latest_remote_scan_time"])

        timestamp = datetime.now()
        time_float = time.mktime(
            timestamp.timetuple()) + timestamp.microsecond / 1E6
        status.controller.latest_remote_scan_time = timestamp
        out = parse_stream(serialize.status(status))
        data = json.loads(out["data"])
        self.assertEqual(str(time_float),
                         data["controller"]["latest_remote_scan_time"])
Example #17
0
    def test_copy_values(self):
        status = Status()
        status.server.up = False
        status.server.error_msg = "Bad error"

        copy = status.copy()
        self.assertEqual(False, copy.server.up)
        self.assertEqual("Bad error", copy.server.error_msg)

        # Modifying original doesn't touch copy
        status.server.up = True
        status.server.error_msg = "No error"
        self.assertEqual(True, status.server.up)
        self.assertEqual("No error", status.server.error_msg)
        self.assertEqual(False, copy.server.up)
        self.assertEqual("Bad error", copy.server.error_msg)

        # Modifying copy doesn't touch original
        copy.server.up = False
        copy.server.error_msg = "Worse error"
        self.assertEqual(True, status.server.up)
        self.assertEqual("No error", status.server.error_msg)
        self.assertEqual(False, copy.server.up)
        self.assertEqual("Worse error", copy.server.error_msg)
Example #18
0
    def setUp(self):
        self.context = MagicMock()
        self.controller = MagicMock()

        # Mock the base logger
        logger = logging.getLogger()
        handler = logging.StreamHandler(sys.stdout)
        logger.addHandler(handler)
        logger.setLevel(logging.DEBUG)
        formatter = logging.Formatter(
            "%(asctime)s - %(levelname)s - %(name)s - %(message)s")
        handler.setFormatter(formatter)
        self.context.logger = logger

        # Model files
        self.model_files = []

        # Real status
        self.context.status = Status()

        # Real config
        self.context.config = Config()

        # Real auto-queue persist
        self.auto_queue_persist = AutoQueuePersist()

        # Capture the model listener
        def capture_listener(listener):
            self.model_listener = listener
            return self.model_files

        self.model_listener = None
        self.controller.get_model_files_and_add_listener = MagicMock()
        self.controller.get_model_files_and_add_listener.side_effect = capture_listener
        self.controller.remove_model_listener = MagicMock()

        # noinspection PyTypeChecker
        self.web_app_builder = WebAppBuilder(self.context, self.controller,
                                             self.auto_queue_persist)
        self.web_app = self.web_app_builder.build()
        self.test_app = TestApp(self.web_app)
Example #19
0
def parse(b: bytes) -> Status:
    # print(" ".join(f"{int(b_):x}" for b_ in b))
    model_code = int.from_bytes(b[3:5], "big")
    errors = int.from_bytes(b[8:10], "big")
    media_width = int(b[10])
    media_type = int(b[11])
    media_length = int(b[17])
    status_type = int(b[18])
    phase = int(b[19])
    notification = int(b[22])
    text_color = int(b[25])

    # print(f"0x{text_color:x}")
    return Status(model=models.get(model_code, "Unknown"),
                  media_width=media_width,
                  media_length=media_length,
                  media_type=media_types.get(media_type, "Unknown"),
                  errors=errors,
                  status_type=StatusType(status_type),
                  phase=PhaseState(phase),
                  notification=notification)
Example #20
0
 def test_property_values(self):
     status = Status()
     status.server.up = True
     status.server.error_msg = "Everything's good"
     self.assertEqual(True, status.server.up)
     self.assertEqual("Everything's good", status.server.error_msg)
Example #21
0
 def get_status(self):
     self._status = Status.from_dict(api.get_status(self._token))
     return self._status
Example #22
0
    def __init__(self):
        # Parse the args
        args = self._parse_args()

        # Create/load config
        config = None
        self.config_path = os.path.join(args.config_dir,
                                        Seedsync.__FILE_CONFIG)
        create_default_config = False
        if os.path.isfile(self.config_path):
            try:
                config = Config.from_file(self.config_path)
            except (ConfigError, PersistError):
                Seedsync.__backup_file(self.config_path)
                # set config to default
                create_default_config = True
        else:
            create_default_config = True

        if create_default_config:
            # Create default config
            config = Seedsync._create_default_config()
            config.to_file(self.config_path)

        # Determine the true value of debug
        is_debug = args.debug or config.general.debug

        # Create context args
        ctx_args = Args()
        ctx_args.local_path_to_scanfs = args.scanfs
        ctx_args.html_path = args.html
        ctx_args.debug = is_debug
        ctx_args.exit = args.exit

        # Logger setup
        # We separate the main log from the web-access log
        logger = self._create_logger(name=Constants.SERVICE_NAME,
                                     debug=is_debug,
                                     logdir=args.logdir)
        Seedsync.logger = logger
        web_access_logger = self._create_logger(
            name=Constants.WEB_ACCESS_LOG_NAME,
            debug=is_debug,
            logdir=args.logdir)
        logger.info(
            "Debug mode is {}.".format("enabled" if is_debug else "disabled"))

        # Create status
        status = Status()

        # Create context
        self.context = Context(logger=logger,
                               web_access_logger=web_access_logger,
                               config=config,
                               args=ctx_args,
                               status=status)

        # Register the signal handlers
        signal.signal(signal.SIGTERM, self.signal)
        signal.signal(signal.SIGINT, self.signal)

        # Print context to log
        self.context.print_to_log()

        # Load the persists
        self.controller_persist_path = os.path.join(
            args.config_dir, Seedsync.__FILE_CONTROLLER_PERSIST)
        self.controller_persist = self._load_persist(
            ControllerPersist, self.controller_persist_path)

        self.auto_queue_persist_path = os.path.join(
            args.config_dir, Seedsync.__FILE_AUTO_QUEUE_PERSIST)
        self.auto_queue_persist = self._load_persist(
            AutoQueuePersist, self.auto_queue_persist_path)
Example #23
0
 def test_cannot_replace_component(self):
     status = Status()
     new_server = Status.ServerStatus()
     with self.assertRaises(ValueError) as e:
         status.server = new_server
     self.assertEqual("Cannot reassign component", str(e.exception))
Example #24
0
 def test_default_values(self):
     status = Status()
     self.assertEqual(True, status.server.up)
     self.assertEqual(None, status.server.error_msg)
     self.assertEqual(None, status.controller.latest_local_scan_time)
     self.assertEqual(None, status.controller.latest_remote_scan_time)
def callback(status):
    global STATUS
    print(f'[main.py]: got status {status}')
    STATUS = Status(status.data)
    publishers.status.publish(STATUS.value)