Exemple #1
0
def get_by_name(name, **kwargs):
    if name == 'mock-runner1':
        return models.RunnerType(**RUNNER1)
    if name == 'mock-runner2':
        return models.RunnerType(**RUNNER2)
Exemple #2
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)
Exemple #3
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)
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)