def test_validate_methods(self, m): m.register_uri('GET', self.MIST_APP_URL + 'artifacts/test-name.jar/sha', text="SOME_CONTENT") m.register_uri('GET', self.MIST_APP_URL + 'artifacts/unknown.jar/sha', status_code=404) m.register_uri('GET', self.MIST_APP_URL + 'contexts/test-ctx', text=""" { "name": "simple", "maxJobs": 20, "workerMode": "shared", "precreated": false, "sparkConf": { "spark.executor.memory": "256m", "spark.driver.memory": "512m" }, "runOptions": "", "downtime": "Inf", "streamingDuration": "1s" } """) m.register_uri('GET', self.MIST_APP_URL + 'contexts/unknown-ctx', status_code=404) mist = MistApp() with self.assertRaises(ValueError): mist._validate_artifact(models.Artifact('test-name', 'test-name.jar')) mist._validate_artifact(models.Artifact('unknown', 'test-name.jar')) mist._validate_context(models.Context('test')) fn1 = models.Function('test', 'Test', 'test-ctx', path='test-name.jar') mist._validate_function(fn1) fn2 = models.Function('test', 'Test', 'unknown-ctx', path='unknown.jar') with self.assertRaises(ValueError): mist._validate_function(fn2) fn3 = models.Function('test', 'Test', 'test-ctx', path='unknown.jar') with self.assertRaises(ValueError): mist._validate_function(fn3)
def test_function_create(self): fn = models.Function('test', 'Test', 'test-context', 'test-path') self.assertEqual(fn.name, 'test') self.assertEqual(fn.class_name, 'Test') self.assertIsInstance(fn.default_context, models.Context) self.assertEqual(fn.default_context.name, 'test-context') self.assertEqual(fn.path, 'test-path') fn = models.Function('test') self.assertIsNotNone(fn.default_context) self.assertEqual(fn.default_context.name, 'default')
def test_mist_cli_list_subcommands(self): mist_app = app.MistApp() mist_app.workers = MagicMock(return_value=[ models.Worker('test-worker-id', 'localhost:0', 'spark-ui') ]) mist_app.contexts = MagicMock(return_value=[models.Context('foo')]) mist_app.jobs = MagicMock(return_value=[ models.Job('test-job-id', 'test', 'foo', 'cli', 'started') ]) mist_app.functions = MagicMock( return_value=[models.Function('test', 'Test', 'foo')]) def invoke_cmd(cmd): return self.runner.invoke(cmd, catch_exceptions=True, obj=mist_app) w_res = invoke_cmd(cli.list_workers) e_res = invoke_cmd(cli.list_functions) j_res = invoke_cmd(cli.list_jobs) c_res = invoke_cmd(cli.list_contexts) self.assertEqual(w_res.exit_code, 0) self.assertEqual(j_res.exit_code, 0) self.assertEqual(c_res.exit_code, 0) self.assertEqual(e_res.exit_code, 0) self.assertTrue('localhost:0' in w_res.output) self.assertTrue('test-job-id' in j_res.output) self.assertTrue('Test' in e_res.output) self.assertTrue('foo' in c_res.output)
def test_update_deployment(self, m): mist = MistApp() artifact = models.Artifact('test-artifact.py', 'test-artifact.py') context = models.Context('test-context') fn = models.Function('test-fn', 'Test', 'test-context', 'test-path.py') mist.artifact_parser.parse = MagicMock(return_value=artifact) mist.context_parser.parse = MagicMock(return_value=context) mist.function_parser.parse = MagicMock(return_value=fn) mist._MistApp__upload_artifact = MagicMock(return_value=artifact) mist.update_context = MagicMock(return_value=context) mist.update_function = MagicMock(return_value=fn) mist._validate_artifact = MagicMock(return_value=None) mist._validate_function = MagicMock(return_value=None) mist._validate_context = MagicMock(return_value=None) mist.update( models.Deployment('test-artifact.py', 'Artifact', ConfigTree(**{'file-path': 'test-path.py'}))) mist.update(models.Deployment('test-context', 'Context', ConfigTree())) mist.update(models.Deployment('test-fn', 'Function', ConfigTree())) call_artifact = mist._MistApp__upload_artifact.call_args[0][0] call_fn = mist.update_function.call_args[0][0] call_context = mist.update_context.call_args[0][0] self.assertEqual(call_artifact.name, 'test-artifact.py') self.assertEqual(call_context.name, 'test-context') self.assertEqual(call_fn.name, 'test-fn')
def test_deploy_function_exists(self, m): m.register_uri('PUT', self.MIST_APP_URL + 'functions', text=""" { "name": "test-fn", "className": "Test", "path": "test-path.py", "defaultContext": "foo" } """) # returns something by with addr m.register_uri('GET', self.MIST_APP_URL + 'functions/test-fn', text=""" { "name": "test-fn", "className": "Test", "path": "test-path.py", "defaultContext": "foo" } """) fn = models.Function('test-fn', 'Test', 'foo', 'test-path.py') mist = MistApp() res = mist.update_function(fn) self.assertIsInstance(res, models.Function) self.assertEqual(res.name, 'test-fn') self.assertEqual(res.class_name, 'Test') self.assertEqual(res.path, 'test-path.py') self.assertEqual(res.default_context.name, 'foo')
def test_to_json(self): fn = models.Function('test', 'Test', 'test-context', 'test-path') res = fn.to_json() self.assertTrue('name' in res) self.assertTrue('defaultContext' in res) self.assertTrue('path' in res) self.assertTrue('className' in res)
def test_update_deployments_should_catch_exceptions(self, m): mist = MistApp(validate=False) context = models.Context('test-context') fn = models.Function('test-fn', 'Test', 'test-context', 'test-path.py') mist.update_function = MagicMock(return_value=fn) mist.update_context = MagicMock(return_value=context) mist.context_parser.parse = MagicMock(return_value=context) mist.function_parser.parse = MagicMock(return_value=fn) depls = [ models.Deployment('simple', 'Function', ConfigTree()), models.Deployment('simple-ctx', 'Context', ConfigTree()) ] mist.update_deployments(depls)
def test_deploy_function(self, m): m.register_uri('POST', self.MIST_APP_URL + 'endpoints', text=""" { "name": "test-fn", "className": "Test", "path": "test-path.py", "defaultContext": "foo" } """) fn = models.Function('test-fn', 'Test', 'foo', 'test-path.py') mist = MistApp() res = mist.update_function(fn) self.assertIsInstance(res, models.Function) self.assertEqual(res.name, 'test-fn') self.assertEqual(res.class_name, 'Test') self.assertEqual(res.path, 'test-path.py') self.assertEqual(res.default_context.name, 'foo')
def test_to_row(self): row = models.Function.to_row(models.Function('test', 'Test', path='test-path')) self.assertListEqual(row, ['test', 'default', 'test-path', 'Test'])