Example #1
0
def get_by_ref(**kwargs):
    ref = kwargs.get('ref_or_id', None)

    if not ref:
        raise Exception('Actions must be referred to by "ref".')

    if ref == 'mockety.mock1':
        return models.Action(**ACTION1)
    if ref == 'mockety.mock2':
        return models.Action(**ACTION2)
Example #2
0
class TestExecutionResourceManager(unittest2.TestCase):
    @classmethod
    def setUpClass(cls):
        super(TestExecutionResourceManager, cls).setUpClass()
        cls.client = client.Client()

    @mock.patch.object(
        models.ResourceManager, 'get_by_id',
        mock.MagicMock(return_value=models.Execution(**EXECUTION)))
    @mock.patch.object(models.ResourceManager, 'get_by_ref_or_id',
                       mock.MagicMock(return_value=models.Action(**ACTION)))
    @mock.patch.object(models.ResourceManager, 'get_by_name',
                       mock.MagicMock(return_value=models.RunnerType(**RUNNER))
                       )
    @mock.patch.object(
        httpclient.HTTPClient, 'post',
        mock.MagicMock(
            return_value=base.FakeResponse(json.dumps(EXECUTION), 200, 'OK')))
    def test_rerun_with_no_params(self):
        self.client.executions.re_run(EXECUTION['id'], tasks=['foobar'])

        endpoint = '/executions/%s/re_run' % EXECUTION['id']

        data = {
            'tasks': ['foobar'],
            'reset': ['foobar'],
            'parameters': {},
            'delay': 0
        }

        httpclient.HTTPClient.post.assert_called_with(endpoint, data)

    @mock.patch.object(
        models.ResourceManager, 'get_by_id',
        mock.MagicMock(return_value=models.Execution(**EXECUTION)))
    @mock.patch.object(models.ResourceManager, 'get_by_ref_or_id',
                       mock.MagicMock(return_value=models.Action(**ACTION)))
    @mock.patch.object(models.ResourceManager, 'get_by_name',
                       mock.MagicMock(return_value=models.RunnerType(**RUNNER))
                       )
    @mock.patch.object(
        httpclient.HTTPClient, 'post',
        mock.MagicMock(
            return_value=base.FakeResponse(json.dumps(EXECUTION), 200, 'OK')))
    def test_rerun_with_params(self):
        params = {'var1': 'testing...'}

        self.client.executions.re_run(EXECUTION['id'],
                                      tasks=['foobar'],
                                      parameters=params)

        endpoint = '/executions/%s/re_run' % EXECUTION['id']

        data = {
            'tasks': ['foobar'],
            'reset': ['foobar'],
            'parameters': params,
            'delay': 0
        }

        httpclient.HTTPClient.post.assert_called_with(endpoint, data)

    @mock.patch.object(
        models.ResourceManager, 'get_by_id',
        mock.MagicMock(return_value=models.Execution(**EXECUTION)))
    @mock.patch.object(models.ResourceManager, 'get_by_ref_or_id',
                       mock.MagicMock(return_value=models.Action(**ACTION)))
    @mock.patch.object(models.ResourceManager, 'get_by_name',
                       mock.MagicMock(return_value=models.RunnerType(**RUNNER))
                       )
    @mock.patch.object(
        httpclient.HTTPClient, 'post',
        mock.MagicMock(
            return_value=base.FakeResponse(json.dumps(EXECUTION), 200, 'OK')))
    def test_rerun_with_delay(self):
        self.client.executions.re_run(EXECUTION['id'],
                                      tasks=['foobar'],
                                      delay=100)

        endpoint = '/executions/%s/re_run' % EXECUTION['id']

        data = {
            'tasks': ['foobar'],
            'reset': ['foobar'],
            'parameters': {},
            'delay': 100
        }

        httpclient.HTTPClient.post.assert_called_with(endpoint, data)

    @mock.patch.object(
        models.ResourceManager, 'get_by_id',
        mock.MagicMock(return_value=models.Execution(**EXECUTION)))
    @mock.patch.object(models.ResourceManager, 'get_by_ref_or_id',
                       mock.MagicMock(return_value=models.Action(**ACTION)))
    @mock.patch.object(models.ResourceManager, 'get_by_name',
                       mock.MagicMock(return_value=models.RunnerType(**RUNNER))
                       )
    @mock.patch.object(
        httpclient.HTTPClient, 'put',
        mock.MagicMock(
            return_value=base.FakeResponse(json.dumps(EXECUTION), 200, 'OK')))
    def test_pause(self):
        self.client.executions.pause(EXECUTION['id'])

        endpoint = '/executions/%s' % EXECUTION['id']

        data = {'status': 'pausing'}

        httpclient.HTTPClient.put.assert_called_with(endpoint, data)

    @mock.patch.object(
        models.ResourceManager, 'get_by_id',
        mock.MagicMock(return_value=models.Execution(**EXECUTION)))
    @mock.patch.object(models.ResourceManager, 'get_by_ref_or_id',
                       mock.MagicMock(return_value=models.Action(**ACTION)))
    @mock.patch.object(models.ResourceManager, 'get_by_name',
                       mock.MagicMock(return_value=models.RunnerType(**RUNNER))
                       )
    @mock.patch.object(
        httpclient.HTTPClient, 'put',
        mock.MagicMock(
            return_value=base.FakeResponse(json.dumps(EXECUTION), 200, 'OK')))
    def test_resume(self):
        self.client.executions.resume(EXECUTION['id'])

        endpoint = '/executions/%s' % EXECUTION['id']

        data = {'status': 'resuming'}

        httpclient.HTTPClient.put.assert_called_with(endpoint, data)

    @mock.patch.object(models.core.Resource, 'get_url_path_name',
                       mock.MagicMock(return_value='executions'))
    @mock.patch.object(
        httpclient.HTTPClient, 'get',
        mock.MagicMock(
            return_value=base.FakeResponse(json.dumps([EXECUTION]), 200, 'OK'))
    )
    def test_get_children(self):
        self.client.executions.get_children(EXECUTION['id'])

        endpoint = '/executions/%s/children' % EXECUTION['id']

        data = {'depth': -1}

        httpclient.HTTPClient.get.assert_called_with(url=endpoint, params=data)

    @mock.patch.object(
        models.ResourceManager, 'get_all',
        mock.MagicMock(return_value=[models.Execution(**EXECUTION)]))
    @mock.patch.object(warnings, 'warn')
    def test_st2client_liveactions_has_been_deprecated_and_emits_warning(
            self, mock_warn):
        self.assertEqual(mock_warn.call_args, None)

        self.client.liveactions.get_all()

        expected_msg = 'st2client.liveactions has been renamed'
        self.assertTrue(len(mock_warn.call_args_list) >= 1)
        self.assertTrue(expected_msg in mock_warn.call_args_list[0][0][0])
        self.assertEqual(mock_warn.call_args_list[0][0][1], DeprecationWarning)
Example #3
0
class TestLiveActionResourceManager(unittest2.TestCase):
    @classmethod
    def setUpClass(cls):
        super(TestLiveActionResourceManager, cls).setUpClass()
        cls.client = client.Client()

    @mock.patch.object(
        models.ResourceManager, 'get_by_id',
        mock.MagicMock(return_vaue=models.LiveAction(**LIVE_ACTION)))
    @mock.patch.object(models.ResourceManager, 'get_by_ref_or_id',
                       mock.MagicMock(return_value=models.Action(**ACTION)))
    @mock.patch.object(models.ResourceManager, 'get_by_name',
                       mock.MagicMock(return_value=models.RunnerType(**RUNNER))
                       )
    @mock.patch.object(
        httpclient.HTTPClient, 'post',
        mock.MagicMock(
            return_value=base.FakeResponse(json.dumps(LIVE_ACTION), 200, 'OK'))
    )
    def test_rerun_with_no_params(self):
        self.client.liveactions.re_run(LIVE_ACTION['id'], tasks=['foobar'])

        endpoint = '/executions/%s/re_run' % LIVE_ACTION['id']

        data = {'tasks': ['foobar'], 'reset': ['foobar'], 'parameters': {}}

        httpclient.HTTPClient.post.assert_called_with(endpoint, data)

    @mock.patch.object(
        models.ResourceManager, 'get_by_id',
        mock.MagicMock(return_vaue=models.LiveAction(**LIVE_ACTION)))
    @mock.patch.object(models.ResourceManager, 'get_by_ref_or_id',
                       mock.MagicMock(return_value=models.Action(**ACTION)))
    @mock.patch.object(models.ResourceManager, 'get_by_name',
                       mock.MagicMock(return_value=models.RunnerType(**RUNNER))
                       )
    @mock.patch.object(
        httpclient.HTTPClient, 'post',
        mock.MagicMock(
            return_value=base.FakeResponse(json.dumps(LIVE_ACTION), 200, 'OK'))
    )
    def test_rerun_with_params(self):
        params = {'var1': 'testing...'}

        self.client.liveactions.re_run(LIVE_ACTION['id'],
                                       tasks=['foobar'],
                                       parameters=params)

        endpoint = '/executions/%s/re_run' % LIVE_ACTION['id']

        data = {'tasks': ['foobar'], 'reset': ['foobar'], 'parameters': params}

        httpclient.HTTPClient.post.assert_called_with(endpoint, data)

    @mock.patch.object(
        models.ResourceManager, 'get_by_id',
        mock.MagicMock(return_vaue=models.LiveAction(**LIVE_ACTION)))
    @mock.patch.object(models.ResourceManager, 'get_by_ref_or_id',
                       mock.MagicMock(return_value=models.Action(**ACTION)))
    @mock.patch.object(models.ResourceManager, 'get_by_name',
                       mock.MagicMock(return_value=models.RunnerType(**RUNNER))
                       )
    @mock.patch.object(
        httpclient.HTTPClient, 'put',
        mock.MagicMock(
            return_value=base.FakeResponse(json.dumps(LIVE_ACTION), 200, 'OK'))
    )
    def test_pause(self):
        self.client.liveactions.pause(LIVE_ACTION['id'])

        endpoint = '/executions/%s' % LIVE_ACTION['id']

        data = {'status': 'pausing'}

        httpclient.HTTPClient.put.assert_called_with(endpoint, data)

    @mock.patch.object(
        models.ResourceManager, 'get_by_id',
        mock.MagicMock(return_vaue=models.LiveAction(**LIVE_ACTION)))
    @mock.patch.object(models.ResourceManager, 'get_by_ref_or_id',
                       mock.MagicMock(return_value=models.Action(**ACTION)))
    @mock.patch.object(models.ResourceManager, 'get_by_name',
                       mock.MagicMock(return_value=models.RunnerType(**RUNNER))
                       )
    @mock.patch.object(
        httpclient.HTTPClient, 'put',
        mock.MagicMock(
            return_value=base.FakeResponse(json.dumps(LIVE_ACTION), 200, 'OK'))
    )
    def test_resume(self):
        self.client.liveactions.resume(LIVE_ACTION['id'])

        endpoint = '/executions/%s' % LIVE_ACTION['id']

        data = {'status': 'resuming'}

        httpclient.HTTPClient.put.assert_called_with(endpoint, data)
Example #4
0
def get_by_ref(**kwargs):
    return models.Action(**MOCK_ACTION)
Example #5
0
class TestExecutionResourceManager(unittest2.TestCase):
    @classmethod
    def setUpClass(cls):
        super(TestExecutionResourceManager, cls).setUpClass()
        cls.client = client.Client()

    @mock.patch.object(
        models.ResourceManager,
        "get_by_id",
        mock.MagicMock(return_value=models.Execution(**EXECUTION)),
    )
    @mock.patch.object(
        models.ResourceManager,
        "get_by_ref_or_id",
        mock.MagicMock(return_value=models.Action(**ACTION)),
    )
    @mock.patch.object(
        models.ResourceManager,
        "get_by_name",
        mock.MagicMock(return_value=models.RunnerType(**RUNNER)),
    )
    @mock.patch.object(
        httpclient.HTTPClient,
        "post",
        mock.MagicMock(
            return_value=base.FakeResponse(json.dumps(EXECUTION), 200, "OK")),
    )
    def test_rerun_with_no_params(self):
        self.client.executions.re_run(EXECUTION["id"], tasks=["foobar"])

        endpoint = "/executions/%s/re_run" % EXECUTION["id"]

        data = {
            "tasks": ["foobar"],
            "reset": ["foobar"],
            "parameters": {},
            "delay": 0
        }

        httpclient.HTTPClient.post.assert_called_with(endpoint, data)

    @mock.patch.object(
        models.ResourceManager,
        "get_by_id",
        mock.MagicMock(return_value=models.Execution(**EXECUTION)),
    )
    @mock.patch.object(
        models.ResourceManager,
        "get_by_ref_or_id",
        mock.MagicMock(return_value=models.Action(**ACTION)),
    )
    @mock.patch.object(
        models.ResourceManager,
        "get_by_name",
        mock.MagicMock(return_value=models.RunnerType(**RUNNER)),
    )
    @mock.patch.object(
        httpclient.HTTPClient,
        "post",
        mock.MagicMock(
            return_value=base.FakeResponse(json.dumps(EXECUTION), 200, "OK")),
    )
    def test_rerun_with_params(self):
        params = {"var1": "testing..."}

        self.client.executions.re_run(EXECUTION["id"],
                                      tasks=["foobar"],
                                      parameters=params)

        endpoint = "/executions/%s/re_run" % EXECUTION["id"]

        data = {
            "tasks": ["foobar"],
            "reset": ["foobar"],
            "parameters": params,
            "delay": 0,
        }

        httpclient.HTTPClient.post.assert_called_with(endpoint, data)

    @mock.patch.object(
        models.ResourceManager,
        "get_by_id",
        mock.MagicMock(return_value=models.Execution(**EXECUTION)),
    )
    @mock.patch.object(
        models.ResourceManager,
        "get_by_ref_or_id",
        mock.MagicMock(return_value=models.Action(**ACTION)),
    )
    @mock.patch.object(
        models.ResourceManager,
        "get_by_name",
        mock.MagicMock(return_value=models.RunnerType(**RUNNER)),
    )
    @mock.patch.object(
        httpclient.HTTPClient,
        "post",
        mock.MagicMock(
            return_value=base.FakeResponse(json.dumps(EXECUTION), 200, "OK")),
    )
    def test_rerun_with_delay(self):
        self.client.executions.re_run(EXECUTION["id"],
                                      tasks=["foobar"],
                                      delay=100)

        endpoint = "/executions/%s/re_run" % EXECUTION["id"]

        data = {
            "tasks": ["foobar"],
            "reset": ["foobar"],
            "parameters": {},
            "delay": 100,
        }

        httpclient.HTTPClient.post.assert_called_with(endpoint, data)

    @mock.patch.object(
        models.ResourceManager,
        "get_by_id",
        mock.MagicMock(return_value=models.Execution(**EXECUTION)),
    )
    @mock.patch.object(
        models.ResourceManager,
        "get_by_ref_or_id",
        mock.MagicMock(return_value=models.Action(**ACTION)),
    )
    @mock.patch.object(
        models.ResourceManager,
        "get_by_name",
        mock.MagicMock(return_value=models.RunnerType(**RUNNER)),
    )
    @mock.patch.object(
        httpclient.HTTPClient,
        "put",
        mock.MagicMock(
            return_value=base.FakeResponse(json.dumps(EXECUTION), 200, "OK")),
    )
    def test_pause(self):
        self.client.executions.pause(EXECUTION["id"])

        endpoint = "/executions/%s" % EXECUTION["id"]

        data = {"status": "pausing"}

        httpclient.HTTPClient.put.assert_called_with(endpoint, data)

    @mock.patch.object(
        models.ResourceManager,
        "get_by_id",
        mock.MagicMock(return_value=models.Execution(**EXECUTION)),
    )
    @mock.patch.object(
        models.ResourceManager,
        "get_by_ref_or_id",
        mock.MagicMock(return_value=models.Action(**ACTION)),
    )
    @mock.patch.object(
        models.ResourceManager,
        "get_by_name",
        mock.MagicMock(return_value=models.RunnerType(**RUNNER)),
    )
    @mock.patch.object(
        httpclient.HTTPClient,
        "put",
        mock.MagicMock(
            return_value=base.FakeResponse(json.dumps(EXECUTION), 200, "OK")),
    )
    def test_resume(self):
        self.client.executions.resume(EXECUTION["id"])

        endpoint = "/executions/%s" % EXECUTION["id"]

        data = {"status": "resuming"}

        httpclient.HTTPClient.put.assert_called_with(endpoint, data)

    @mock.patch.object(
        models.core.Resource,
        "get_url_path_name",
        mock.MagicMock(return_value="executions"),
    )
    @mock.patch.object(
        httpclient.HTTPClient,
        "get",
        mock.MagicMock(return_value=base.FakeResponse(json.dumps([EXECUTION]),
                                                      200, "OK")),
    )
    def test_get_children(self):
        self.client.executions.get_children(EXECUTION["id"])

        endpoint = "/executions/%s/children" % EXECUTION["id"]

        data = {"depth": -1}

        httpclient.HTTPClient.get.assert_called_with(url=endpoint, params=data)

    @mock.patch.object(
        models.ResourceManager,
        "get_all",
        mock.MagicMock(return_value=[models.Execution(**EXECUTION)]),
    )
    @mock.patch.object(warnings, "warn")
    def test_st2client_liveactions_has_been_deprecated_and_emits_warning(
            self, mock_warn):
        self.assertEqual(mock_warn.call_args, None)

        self.client.liveactions.get_all()

        expected_msg = "st2client.liveactions has been renamed"
        self.assertTrue(len(mock_warn.call_args_list) >= 1)
        self.assertIn(expected_msg, mock_warn.call_args_list[0][0][0])
        self.assertEqual(mock_warn.call_args_list[0][0][1], DeprecationWarning)