def test_connect(self, UJS): UJS().recv.side_effect = [ {"protocol": 1, "service": "ClearSkies Control", "software": "test"}, ] c = ClearSkies() c.connect()
def test_stop__error(self, UJS): UJS().recv.side_effect = [ {"protocol": 1, "service": "ClearSkies Control", "software": "test"}, TransportException("Could not decode JSON"), ] c = ClearSkies() c.connect() self.assertRaises(TransportException, c.stop)
def test_create_access_code__invalid_mode(self, UJS): UJS().recv.side_effect = [ {"protocol": 1, "service": "ClearSkies Control", "software": "test"}, {}, ] c = ClearSkies() c.connect() self.assertRaises(ValueError, c.create_access_code, "/home/foo/Shared", "not_a_real_mode")
def test_set_config_value(self, UJS): UJS().recv.side_effect = [ {"protocol": 1, "service": "ClearSkies Control", "software": "test"}, {}, ] c = ClearSkies() c.connect() c.set_config_value("foo", "bar")
def test_resume(self, UJS): UJS().recv.side_effect = [ {"protocol": 1, "service": "ClearSkies Control", "software": "test"}, {}, ] c = ClearSkies() c.connect() c.resume() UJS().send.assert_called_with({ "type": "resume", })
def test_get_log_data__io_error(self, UJS): UJS().recv.side_effect = [ {"protocol": 1, "service": "ClearSkies Control", "software": "test"}, # get_log_data bypasses the server and gets data straight from the file # possibly this should change in future ] c = ClearSkies() c.connect() with patch(_open) as mock_open: mock_open.side_effect = IOError("File not found") self.assertRaises(ProtocolException, c.get_log_data)
def test_list_shares(self, UJS): UJS().recv.side_effect = [ {"protocol": 1, "service": "ClearSkies Control", "software": "test"}, {"shares": [{"path": "/home/foo/Shared", "status": "N/A"}]}, ] c = ClearSkies() c.connect() c.list_shares() UJS().send.assert_called_with({ "type": "list_shares", })
def test_remove_share(self, UJS): UJS().recv.side_effect = [ {"protocol": 1, "service": "ClearSkies Control", "software": "test"}, {}, ] c = ClearSkies() c.connect() c.remove_share("/home/foo/Shared") UJS().send.assert_called_with({ "type": "remove_share", "path": "/home/foo/Shared", })
def test_create_access_code(self, UJS): UJS().recv.side_effect = [ {"protocol": 1, "service": "ClearSkies Control", "software": "test"}, {"access_code": "SYNC123ABC"}, ] c = ClearSkies() c.connect() code = c.create_access_code("/home/foo/Shared", "read_write") self.assertEqual(code, "SYNC123ABC") UJS().send.assert_called_with({ "type": "create_access_code", "path": "/home/foo/Shared", "mode": "read_write", })
def test_get_log_data(self, UJS): UJS().recv.side_effect = [ {"protocol": 1, "service": "ClearSkies Control", "software": "test"}, # get_log_data bypasses the server and gets data straight from the file # possibly this should change in future ] c = ClearSkies() c.connect() with patch(_open) as mock_open: mock_open.return_value.read.return_value = "some\nlog\ndata\n" # get all the log data self.assertEqual(c.get_log_data(), "some\nlog\ndata\n") # get the bottom two lines self.assertEqual(c.get_log_data(2), "log\ndata\n")
def test_get_config_value(self, UJS): UJS().recv.side_effect = [ {"protocol": 1, "service": "ClearSkies Control", "software": "test"}, {"value": "bar"}, ] c = ClearSkies() c.connect() c.set_config({"foo": "bar"}) # hack self.assertEqual( c.get_config_value("foo"), "bar" )
def test_get_config(self, UJS): UJS().recv.side_effect = [ {"protocol": 1, "service": "ClearSkies Control", "software": "test"}, {"config": {"key": "value"}}, ] c = ClearSkies() c.connect() c.set_config({"key": "value"}) # hack self.assertDictEqual( c.get_config(), {"key": "value"} )
def main(self, args): logging.basicConfig(level=logging.DEBUG, format="%(asctime)19.19s %(levelname)4.4s %(message)s") module_log = logging.getLogger("clearskies") parser = argparse.ArgumentParser(description='ClearSkies python interface demo') parser.add_argument('-v', '--verbose', action="store_true", default=False) subparsers = parser.add_subparsers() parser_stop = subparsers.add_parser('stop') parser_stop.set_defaults(func=self.stop) parser_pause = subparsers.add_parser('pause') parser_pause.set_defaults(func=self.pause) parser_resume = subparsers.add_parser('resume') parser_resume.set_defaults(func=self.resume) parser_status = subparsers.add_parser('status', help="Give program status") parser_status.set_defaults(func=self.status) parser_create_share = subparsers.add_parser('create', help="Create new share") parser_create_share.add_argument('path') parser_create_share.set_defaults(func=self.create_share) parser_list_shares = subparsers.add_parser('list', help="List all shares and sync status") parser_list_shares.set_defaults(func=self.list_shares) parser_create_access_code = subparsers.add_parser('share', help="Make access code to be given to others") parser_create_access_code.add_argument('path') parser_create_access_code.add_argument('mode') parser_create_access_code.set_defaults(func=self.create_access_code) parser_add_share = subparsers.add_parser( 'attach', help="Add access code from someone else, creating new share at [path]" ) parser_add_share.add_argument('code') parser_add_share.add_argument('path') parser_add_share.set_defaults(func=self.add_share) parser_remove_share = subparsers.add_parser('detach', help="Stop syncing path") parser_remove_share.add_argument('path') parser_remove_share.set_defaults(func=self.remove_share) args = parser.parse_args(args[1:]) self.cs = ClearSkies() if args.verbose: module_log.setLevel(logging.DEBUG) else: module_log.setLevel(logging.INFO) try: self.cs.connect() except ClientException as e: log.error("Couldn't connect to daemon: %s" % e) log.error("Is the daemon running?") return if hasattr(args, "func"): args.func(args) else: log.error("No command specified, use --help for a list")