def test_json_to_yaml(self): """ Running `crossbar convert` with a YAML config file will convert it to JSON. """ cbdir = self.mktemp() os.makedirs(cbdir) config_file = os.path.join(cbdir, "config.json") with open(config_file, 'w') as f: f.write("""{ "foo": { "bar": "spam", "baz": { "foo": "cat" } } }""") cli.run("crossbar", ["convert", "--config={}".format(config_file)]) self.assertIn( ("YAML formatted configuration written"), self.stdout.getvalue()) with open(os.path.join(cbdir, "config.yaml"), 'r') as f: self.assertEqual(f.read(), """foo: bar: spam baz: foo: cat """)
def _start_run(self, config, app, stdout_expected, stderr_expected, end_on): with open(self.config, "wb") as f: f.write(json.dumps(config, ensure_ascii=False).encode('utf8')) with open(self.code_location + "/myapp.py", "w") as f: f.write(app) reactor = SelectReactor() make_lc(self, reactor, end_on) # In case it hard-locks reactor.callLater(self._subprocess_timeout, reactor.stop) cli.run("crossbar", ["start", "--cbdir={}".format(self.cbdir), "--logformat=syslogd"], reactor=reactor) out = self.stdout.getvalue() err = self.stderr.getvalue() for i in stdout_expected: if i not in out: self.fail(u"Error: '{}' not in:\n{}".format(i, out)) for i in stderr_expected: if i not in err: self.fail(u"Error: '{}' not in:\n{}".format(i, err))
def _start_run(self, config, app, stdout_expected, stderr_expected, end_on): with open(self.config, "wb") as f: f.write(json.dumps(config, ensure_ascii=False).encode("utf8")) with open(self.code_location + "/myapp.py", "w") as f: f.write(app) reactor = SelectReactor() make_lc(self, reactor, end_on) # In case it hard-locks reactor.callLater(self._subprocess_timeout, reactor.stop) cli.run("crossbar", ["start", "--cbdir={}".format(self.cbdir), "--logformat=syslogd"], reactor=reactor) out = self.stdout.getvalue() err = self.stderr.getvalue() for i in stdout_expected: if i not in out: self.fail(u"Error: '{}' not in:\n{}".format(i, out)) for i in stderr_expected: if i not in err: self.fail(u"Error: '{}' not in:\n{}".format(i, err))
def test_json_to_yaml(self): """ Running `crossbar convert` with a YAML config file will convert it to JSON. """ cbdir = FilePath(self.mktemp()) cbdir.makedirs() config_file = cbdir.child("config.json") config_file.setContent(b"""{ "foo": { "bar": "spam", "baz": { "foo": "cat" } } }""") cli.run("crossbar", ["convert", "--config={}".format(config_file.path)]) self.assertIn(("YAML formatted configuration written"), self.stdout.getvalue()) with open(cbdir.child("config.yaml").path) as f: self.assertEqual(f.read(), """foo: bar: spam baz: foo: cat """)
def test_json_to_yaml(self): """ Running `crossbar convert` with a YAML config file will convert it to JSON. """ cbdir = FilePath(self.mktemp()) cbdir.makedirs() config_file = cbdir.child("config.json") config_file.setContent(b"""{ "foo": { "bar": "spam", "baz": { "foo": "cat" } } }""") cli.run("crossbar", ["convert", "--config={}".format(config_file.path)]) self.assertIn( ("YAML formatted configuration written"), self.stdout.getvalue()) with open(cbdir.child("config.yaml").path) as f: self.assertEqual(f.read(), """foo: bar: spam baz: foo: cat """)
def test_hello(self): def _check(lc, reactor): if "published to 'oncounter'" in self.stdout.getvalue(): lc.stop() try: reactor.stop() except: pass appdir = self.mktemp() cbdir = os.path.join(appdir, ".crossbar") reactor = SelectReactor() cli.run("crossbar", ["init", "--appdir={}".format(appdir), "--template=hello:python"], reactor=reactor) self.assertIn("Application template initialized", self.stdout.getvalue()) reactor = SelectReactor() make_lc(self, reactor, _check) # In case it hard-locks reactor.callLater(self._subprocess_timeout, reactor.stop) cli.run("crossbar", ["start", "--cbdir={}".format(cbdir.path), "--logformat=syslogd"], reactor=reactor) stdout_expected = ["published to 'oncounter'"] for i in stdout_expected: self.assertIn(i, self.stdout.getvalue())
def _start_run(self, config, app, stdout_expected, stderr_expected, end_on): with open(self.config, "wb") as f: f.write(json.dumps(config, ensure_ascii=False).encode('utf8')) with open(self.code_location + "/myapp.py", "w") as f: f.write(app) reactor = SelectReactor() make_lc(self, reactor, end_on) # In case it hard-locks reactor.callLater(self._subprocess_timeout, reactor.stop) cli.run("crossbar", ["start", "--cbdir={}".format(self.cbdir), "--logformat=syslogd"], reactor=reactor) for i in stdout_expected: self.assertIn(i, self.stdout.getvalue()) for i in stderr_expected: self.assertIn(i, self.stderr.getvalue())
def test_basic(self): """ Just running `crossbar version` gets us the versions. """ reactor = SelectReactor() cli.run("crossbar", ["version"], reactor=reactor) self.assertIn("Crossbar.io", self.stdout.getvalue()) self.assertIn( ("Twisted : \x1b[33m\x1b[1m" + twisted.version.short() + "-SelectReactor"), self.stdout.getvalue())
def test_start(self): """ A basic start, that doesn't actually enter the reactor. """ with open(self.config, "w") as f: f.write("""{"controller": {}}""") reactor = SelectReactor() reactor.run = lambda: False cli.run("crossbar", ["start", "--cbdir={}".format(self.cbdir), "--logformat=syslogd"], reactor=reactor) self.assertIn("Entering reactor event loop", self.stdout.getvalue())
def test_invalid_json_to_yaml(self): """ Running `crossbar convert` with an invalid JSON config file will error saying it is invalid. """ cbdir = FilePath(self.mktemp()) cbdir.makedirs() config_file = cbdir.child("config.json") config_file.setContent(b"""{{{{{{{{""") with self.assertRaises(SystemExit) as e: cli.run("crossbar", ["convert", "--config={}".format(config_file.path)]) self.assertEqual(e.exception.args[0], 1) self.assertIn(("not seem to be proper JSON"), self.stdout.getvalue())
def test_unknown_format(self): """ Running `crossbar convert` with an unknown config file produces an error. """ cbdir = self.mktemp() os.makedirs(cbdir) config_file = os.path.join(cbdir, "config.blah") open(config_file, 'wb').close() with self.assertRaises(SystemExit) as e: cli.run("crossbar", ["convert", "--config={}".format(config_file)]) self.assertEqual(e.exception.args[0], 1) self.assertIn( ("Error: configuration file needs to be '.json' or '.yaml'."), self.stdout.getvalue())
def test_unknown_format(self): """ Running `crossbar convert` with an unknown config file produces an error. """ cbdir = FilePath(self.mktemp()) cbdir.makedirs() config_file = cbdir.child("config.blah") config_file.setContent(b'') with self.assertRaises(SystemExit) as e: cli.run("crossbar", ["convert", "--config={}".format(config_file.path)]) self.assertEqual(e.exception.args[0], 1) self.assertIn( ("Error: configuration file needs to be '.json' or '.yaml'."), self.stdout.getvalue())
def test_debug(self): """ Running `crossbar version` will give us the versions, plus the locations of some of them. """ reactor = SelectReactor() cli.run("crossbar", ["version", "--loglevel=debug"], reactor=reactor) self.assertIn("Crossbar.io", self.stdout.getvalue()) self.assertIn( ("Twisted : \x1b[33m\x1b[1m" + twisted.version.short() + "-SelectReactor"), self.stdout.getvalue()) self.assertIn( ("[twisted.internet.selectreactor.SelectReactor]"), self.stdout.getvalue())
def test_invalid_json_to_yaml(self): """ Running `crossbar convert` with an invalid JSON config file will error saying it is invalid. """ cbdir = FilePath(self.mktemp()) cbdir.makedirs() config_file = cbdir.child("config.json") config_file.setContent(b"""{{{{{{{{""") with self.assertRaises(SystemExit) as e: cli.run("crossbar", ["convert", "--config={}".format(config_file.path)]) self.assertEqual(e.exception.args[0], 1) self.assertIn( ("not seem to be proper JSON"), self.stdout.getvalue())
def test_invalid_json_to_yaml(self): """ Running `crossbar convert` with an invalid JSON config file will error saying it is invalid. """ cbdir = self.mktemp() os.makedirs(cbdir) config_file = os.path.join(cbdir, "config.json") with open(config_file, 'w') as f: f.write("""{{{{{{{{""") with self.assertRaises(SystemExit) as e: cli.run("crossbar", ["convert", "--config={}".format(config_file)]) self.assertEqual(e.exception.args[0], 1) self.assertIn( ("not seem to be proper JSON"), self.stdout.getvalue())
def test_fileLogging(self): """ Running `crossbar start --logtofile` will log to cbdir/node.log. """ with open(self.config, "w") as f: f.write("""{"controller": {}}""") reactor = SelectReactor() reactor.run = lambda: None cli.run("crossbar", ["start", "--cbdir={}".format(self.cbdir), "--logtofile"], reactor=reactor) with open(os.path.join(self.cbdir, "node.log"), "r") as f: logFile = f.read() self.assertIn("Entering reactor event loop", logFile) self.assertEqual("", self.stderr.getvalue()) self.assertEqual("", self.stdout.getvalue())
def test_stalePID(self): with open(self.config, "w") as f: f.write("""{"controller": {}}""") with open(os.path.join(self.cbdir, "node.pid"), "w") as f: f.write("""{"pid": 9999999}""") reactor = SelectReactor() reactor.run = lambda: None cli.run("crossbar", ["start", "--cbdir={}".format(self.cbdir), "--logformat=syslogd"], reactor=reactor) self.assertIn( ("Stale Crossbar.io PID file (pointing to non-existing process " "with PID {pid}) {fp} removed").format( fp=os.path.abspath(os.path.join(self.cbdir, "node.pid")), pid=9999999), self.stdout.getvalue())
def test_configValidationFailure(self): """ Running `crossbar start` with an invalid config will print a warning. """ with open(self.config, "w") as f: f.write("") reactor = SelectReactor() with self.assertRaises(SystemExit) as e: cli.run("crossbar", ["start", "--cbdir={}".format(self.cbdir), "--logformat=syslogd"], reactor=reactor) # Exit with code 1 self.assertEqual(e.exception.args[0], 1) # The proper warning should be emitted self.assertIn("*** Configuration validation failed ***", self.stderr.getvalue()) self.assertIn(("configuration file does not seem to be proper JSON "), self.stderr.getvalue())
def _test_start_run(self): """ A basic start, that enters the reactor. """ code_location = os.path.abspath(self.mktemp()) os.mkdir(code_location) with open(self.config, "w") as f: f.write("""{ "controller": { }, "workers": [ { "type": "router", "options": { "pythonpath": ["."] }, "realms": [ { "name": "realm1", "roles": [ { "name": "anonymous", "permissions": [ { "uri": "*", "publish": true, "subscribe": true, "call": true, "register": true } ] } ] } ], "transports": [ { "type": "web", "endpoint": { "type": "tcp", "port": 8080 }, "paths": { "/": { "directory": ".", "type": "static" }, "ws": { "type": "websocket" } } } ] }, { "type": "container", "options": { "pythonpath": ["%s"] }, "components": [ { "type": "class", "classname": "test.AppSession", "realm": "realm1", "transport": { "type": "websocket", "endpoint": { "type": "tcp", "host": "127.0.0.1", "port": 8080 }, "url": "ws://127.0.0.1:8080/ws" } } ] } ] } """ % ("/".join(code_location.split(os.sep),))) with open(code_location + "/test.py", "w") as f: f.write("""#!/usr/bin/env python from twisted.internet.defer import inlineCallbacks from twisted.logger import Logger from autobahn.twisted.wamp import ApplicationSession from autobahn.wamp.exception import ApplicationError class AppSession(ApplicationSession): log = Logger() @inlineCallbacks def onJoin(self, details): self.log.info("Loaded the component!") yield self.publish("com.bar", "test") """) reactor = SelectReactor() def _check(lc): if "Loaded the component!" in self.stdout.getvalue(): if reactor.running: reactor.stop() lc.stop() lc = LoopingCall(_check) lc.a = (lc,) lc.clock = reactor # In case it hard-locks reactor.callLater(self._subprocess_timeout, reactor.stop) lc.start(0.1) cli.run("crossbar", ["start", "--cbdir={}".format(self.cbdir), "--logformat=syslogd"], reactor=reactor) self.assertIn("Entering reactor event loop", self.stdout.getvalue()) self.assertIn("Loaded the component!", self.stdout.getvalue())