File: test_task.py

package info (click to toggle)
rally 5.0.0-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,368 kB
  • sloc: python: 42,541; javascript: 487; sh: 198; makefile: 192; xml: 43
file content (1123 lines) | stat: -rw-r--r-- 50,080 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
# Copyright 2013: Mirantis Inc.
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import datetime as dt
import io
import json
import os.path
import sys
from unittest import mock

import ddt

import rally
from rally import api
from rally.cli import cliutils
from rally.cli.commands import task
from rally.cli import yamlutils as yaml
from rally import consts
from rally import exceptions
from tests.unit import fakes
from tests.unit import test


@ddt.ddt
class TaskCommandsTestCase(test.TestCase):

    def setUp(self):
        super(TaskCommandsTestCase, self).setUp()
        self.task = task.TaskCommands()
        self.fake_api = fakes.FakeAPI()

        with mock.patch("rally.api.API.check_db_revision"):
            self.real_api = api.API()

    @mock.patch("rally.cli.commands.task.open", create=True)
    def test__load_and_validate_task(self, mock_open):
        input_task = "{'ab': {{test}}}"
        input_args = "{'test': 2}"

        mock_open.side_effect = [
            mock.mock_open(read_data=input_task).return_value,
            mock.mock_open(read_data="{'test': 1}").return_value
        ]
        task_conf = self.task._load_and_validate_task(
            self.real_api, "in_task", args_file="in_args_path")
        self.assertEqual({"ab": 1}, task_conf)

        mock_open.side_effect = [
            mock.mock_open(read_data=input_task).return_value
        ]
        task_conf = self.task._load_and_validate_task(
            self.real_api, "in_task", raw_args=input_args)
        self.assertEqual({"ab": 2}, task_conf)

        mock_open.side_effect = [
            mock.mock_open(read_data=input_task).return_value,
            mock.mock_open(read_data="{'test': 1}").return_value
        ]
        task_conf = self.task._load_and_validate_task(
            self.real_api, "in_task", raw_args=input_args,
            args_file="any_file")
        self.assertEqual({"ab": 2}, task_conf)

        mock_open.side_effect = [
            mock.mock_open(read_data=input_task).return_value,
            mock.mock_open(read_data="{'test': 1}").return_value
        ]
        task_conf = self.task._load_and_validate_task(
            self.real_api, "in_task", raw_args="test=2",
            args_file="any_file")
        self.assertEqual({"ab": 2}, task_conf)

    @mock.patch("rally.cli.commands.task.open", create=True)
    def test__load_task_wrong_task_args_file(self, mock_open):

        def open_return_value(filename):
            if filename == "in_task":
                m = mock.MagicMock()
                m.__enter__.return_value = mock.Mock(read=lambda: "{}")
                return m
            else:
                raise IOError()

        mock_open.side_effect = open_return_value

        e = self.assertRaises(task.FailedToLoadTask,
                              self.task._load_and_validate_task,
                              self.fake_api, task_file="in_task",
                              args_file="in_args_path")
        self.assertEqual("Invalid --task-args-file passed:\n\n\t Error "
                         "reading in_args_path: ", e.format_message())

    @mock.patch("rally.cli.commands.task.yaml.safe_load")
    def test__load_task_wrong_input_task_args(self, mock_safe_load):
        mock_safe_load.side_effect = yaml.ParserError("foo")
        # use real file to avoid mocking open
        task_file = __file__

        e = self.assertRaises(task.FailedToLoadTask,
                              self.task._load_and_validate_task, self.real_api,
                              task_file, raw_args="{'test': {}")
        self.assertEqual("Invalid --task-args passed:\n\n\t Value has to be "
                         "YAML or JSON. Details:\n\nfoo", e.format_message())
        mock_safe_load.assert_called_once_with("{'test': {}")

        # the case #2
        mock_safe_load.reset_mock()
        e = self.assertRaises(task.FailedToLoadTask,
                              self.task._load_and_validate_task, self.real_api,
                              task_file, raw_args="[]")
        self.assertEqual("Invalid --task-args passed:\n\n\t Value has to be "
                         "YAML or JSON. Details:\n\nfoo", e.format_message())
        mock_safe_load.assert_called_once_with("[]")

        # the case #3
        mock_safe_load.reset_mock()
        e = self.assertRaises(task.FailedToLoadTask,
                              self.task._load_and_validate_task, self.real_api,
                              task_file, raw_args="foo")
        self.assertEqual("Invalid --task-args passed:\n\n\t Value has to be "
                         "YAML or JSON. Details:\n\nfoo", e.format_message())
        mock_safe_load.assert_called_once_with("foo")

    @mock.patch("rally.cli.commands.task.open", create=True)
    def test__load_task_task_render_raise_exc(self, mock_open):
        mock_open.side_effect = [
            mock.mock_open(read_data="{'test': {{t}}}").return_value
        ]
        e = self.assertRaises(task.FailedToLoadTask,
                              self.task._load_and_validate_task, self.real_api,
                              "in_task")
        self.assertEqual("Invalid --task passed:\n\n\t Failed to render task "
                         "template.\n\nPlease specify template task argument: "
                         "t", e.format_message())

    @mock.patch("rally.cli.commands.task.yaml")
    @mock.patch("rally.cli.commands.task.open", create=True)
    def test__load_task_task_not_in_yaml(self, mock_open, mock_yaml):
        mock_open.side_effect = [
            mock.mock_open(read_data="{'test': {}").return_value
        ]
        mock_yaml.safe_load.side_effect = Exception("ERROR!!!PANIC!!!")

        e = self.assertRaises(task.FailedToLoadTask,
                              self.task._load_and_validate_task, self.fake_api,
                              "in_task")
        self.assertEqual("Invalid --task passed:\n\n\t Wrong format of "
                         "rendered input task. It should be YAML or JSON. "
                         "Details:\n\nERROR!!!PANIC!!!", e.format_message())

    def test_load_task_including_other_template(self):
        other_template_path = os.path.join(
            os.path.dirname(rally.__file__), os.pardir,
            "samples/tasks/scenarios/dummy/dummy.json")
        input_task = "{%% include \"%s\" %%}" % os.path.basename(
            other_template_path)
        expect = self.task._load_and_validate_task(self.real_api,
                                                   other_template_path)

        with mock.patch("rally.cli.commands.task.open",
                        create=True) as mock_open:
            mock_open.side_effect = [
                mock.mock_open(read_data=input_task).return_value
            ]
            input_task_file = os.path.join(
                os.path.dirname(other_template_path), "input_task.json")
            actual = self.task._load_and_validate_task(self.real_api,
                                                       input_task_file)
        self.assertEqual(expect, actual)

    @mock.patch("rally.cli.commands.task.open", create=True)
    def test__load_and_validate_file_failed(self, mock_open):
        mock_open.side_effect = IOError

        e = self.assertRaises(task.FailedToLoadTask,
                              self.task._load_and_validate_task,
                              api=self.fake_api, task_file="some_task",
                              raw_args="task_args", args_file="task_args_file")
        self.assertEqual(
            "Invalid --task passed:\n\n\t Error reading some_task: ",
            e.format_message())

    @mock.patch("rally.cli.commands.task.version")
    @mock.patch("rally.cli.commands.task.TaskCommands.use")
    @mock.patch("rally.cli.commands.task.TaskCommands._detailed")
    @mock.patch("rally.cli.commands.task.TaskCommands._load_and_validate_task",
                return_value={"some": "json"})
    def test_start(self, mock__load_and_validate_task, mock__detailed,
                   mock_use, mock_version):
        deployment_id = "e0617de9-77d1-4875-9b49-9d5789e29f20"
        task_path = "path_to_config.json"
        fake_task = fakes.FakeTask(uuid="some_new_uuid", tags=["tag"])
        mock__detailed.return_value = 1
        self.fake_api.task.create.return_value = fake_task
        self.fake_api.task.validate.return_value = fakes.FakeTask(
            some="json", uuid="some_uuid", temporary=True)

        val = self.task.start(self.fake_api, task_path,
                              deployment_id, do_use=True)
        self.assertEqual(2, val)
        mock_version.version_string.assert_called_once_with()
        self.fake_api.task.create.assert_called_once_with(
            deployment=deployment_id, tags=None)
        self.fake_api.task.start.assert_called_once_with(
            deployment=deployment_id,
            config=mock__load_and_validate_task.return_value,
            task=fake_task["uuid"],
            abort_on_sla_failure=False)
        mock__load_and_validate_task.assert_called_once_with(
            self.fake_api, task_path, args_file=None, raw_args=None)
        mock_use.assert_called_once_with(self.fake_api, "some_new_uuid")
        mock__detailed.assert_called_once_with(self.fake_api,
                                               task_id=fake_task["uuid"])
        mock__detailed.return_value = 0
        val1 = self.task.start(self.fake_api, task_path,
                               deployment_id, do_use=True)
        self.assertEqual(0, val1)

    @mock.patch("rally.cli.commands.task.TaskCommands._detailed")
    @mock.patch("rally.cli.commands.task.TaskCommands._load_and_validate_task",
                return_value="some_config")
    def test_start_on_unfinished_deployment(self, mock__load_and_validate_task,
                                            mock__detailed):
        deployment_id = "e0617de9-77d1-4875-9b49-9d5789e29f20"
        deployment_name = "xxx_name"
        task_path = "path_to_config.json"
        fake_task = fakes.FakeTask(uuid="some_new_uuid", tag="tag")
        self.fake_api.task.create.return_value = fake_task

        exc = exceptions.DeploymentNotFinishedStatus(
            name=deployment_name,
            uuid=deployment_id,
            status=consts.DeployStatus.DEPLOY_INIT)
        self.fake_api.task.create.side_effect = exc
        self.assertEqual(1, self.task.start(self.fake_api, task_path,
                                            deployment="any",
                                            tags=["some_tag"]))
        self.assertFalse(mock__detailed.called)

    @mock.patch("rally.cli.commands.task.TaskCommands._detailed")
    @mock.patch("rally.cli.commands.task.TaskCommands._load_and_validate_task",
                return_value="some_config")
    def test_start_with_task_args(self, mock__load_and_validate_task,
                                  mock__detailed):
        fake_task = fakes.FakeTask(uuid="new_uuid", tags=["some_tag"])
        self.fake_api.task.create.return_value = fakes.FakeTask(
            uuid="new_uuid", tags=["some_tag"])
        self.fake_api.task.validate.return_value = fakes.FakeTask(
            uuid="some_id")

        task_path = "path_to_config.json"
        task_args = "task_args"
        task_args_file = "task_args_file"
        self.task.start(self.fake_api, task_path, deployment="any",
                        task_args=task_args, task_args_file=task_args_file,
                        tags=["some_tag"])

        mock__load_and_validate_task.assert_called_once_with(
            self.fake_api, task_path, raw_args=task_args,
            args_file=task_args_file)

        self.fake_api.task.start.assert_called_once_with(
            deployment="any",
            config=mock__load_and_validate_task.return_value,
            task=fake_task["uuid"],
            abort_on_sla_failure=False)
        mock__detailed.assert_called_once_with(
            self.fake_api,
            task_id=fake_task["uuid"])
        self.fake_api.task.create.assert_called_once_with(
            deployment="any", tags=["some_tag"])

    @mock.patch("rally.cli.commands.task.envutils.get_global")
    def test_start_no_deployment_id(self, mock_get_global):
        mock_get_global.side_effect = exceptions.InvalidArgumentsException
        self.assertRaises(exceptions.InvalidArgumentsException,
                          self.task.start, "path_to_config.json", None)

    @mock.patch("rally.cli.commands.task.TaskCommands._detailed")
    @mock.patch("rally.cli.commands.task.TaskCommands._load_and_validate_task")
    def test_start_invalid_task(self, mock__load_and_validate_task,
                                mock__detailed):
        task_obj = fakes.FakeTask(temporary=False, tag="tag", uuid="uuid")
        self.fake_api.task.create.return_value = task_obj
        exc = exceptions.InvalidTaskException("foo")

        mock__load_and_validate_task.side_effect = exc

        self.assertRaises(exceptions.InvalidTaskException,
                          self.task.start, self.fake_api, "task_path",
                          "deployment", tags=["tag"])

        self.assertFalse(self.fake_api.task.create.called)
        self.assertFalse(self.fake_api.task.start.called)

        # the case 2
        task_cfg = {"some": "json"}
        mock__load_and_validate_task.side_effect = (task_cfg, )
        self.fake_api.task.start.side_effect = KeyError()

        self.assertRaises(KeyError,
                          self.task.start, self.fake_api, "task_path",
                          "deployment", tags=["tag"])

        self.fake_api.task.create.assert_called_once_with(
            deployment="deployment", tags=["tag"])

        self.fake_api.task.start.assert_called_once_with(
            deployment="deployment", config=task_cfg,
            task=task_obj["uuid"],
            abort_on_sla_failure=False)

        self.assertFalse(mock__detailed.called)

    @mock.patch("rally.cli.commands.task.TaskCommands._start_task")
    @ddt.data({"scenario": None},
              {"scenario": "scenario_name"},
              {"scenario": "none_name"})
    @ddt.unpack
    def test_restart(self, mock__start_task, scenario):
        self.fake_api.task.get.return_value = {
            "status": "finished",
            "title": "fake_task",
            "description": "this is a test",
            "tags": [],
            "subtasks": [
                {"title": "subtask", "description": "",
                 "workloads": [
                     {
                         "name": "scenario_name",
                         "args": {},
                         "contexts": {},
                         "runner": {"times": 20, "concurrency": 5},
                         "runner_type": "constant",
                         "hooks": [],
                         "sla": {}
                     }]}
            ]
        }
        if scenario == "none_name":
            self.assertEqual(
                1,
                self.task.restart(self.fake_api, "deployment_uuid",
                                  "task_uuid", scenarios=scenario)
            )
        else:
            self.assertEqual(
                mock__start_task.return_value,
                self.task.restart(self.fake_api, "deployment_uuid",
                                  "task_uuid", scenarios=scenario)
            )
        self.fake_api.task.get.assert_called_once_with(task_id="task_uuid",
                                                       detailed=True)

    def test_restart_by_crashed_task(self):
        self.fake_api.task.get.return_value = {
            "uuid": "task_uuid",
            "status": "crashed",
            "title": "fake_task",
            "description": "this is a test",
            "tags": [],
            "subtasks": [],
            "validation_result": {
                "trace": {},
                "etype": "",
                "msg": ""
            }
        }
        self.assertEqual(
            1,
            self.task.restart(self.fake_api, "deployment_uuid", "task_uuid")
        )
        self.fake_api.task.get.assert_called_once_with(task_id="task_uuid",
                                                       detailed=True)

    def test_abort(self):
        test_uuid = "17860c43-2274-498d-8669-448eff7b073f"
        self.task.abort(self.fake_api, test_uuid)
        self.fake_api.task.abort.assert_called_once_with(
            task_uuid=test_uuid, soft=False, wait=True)

    @mock.patch("rally.cli.commands.task.envutils.get_global")
    def test_abort_no_task_id(self, mock_get_global):
        mock_get_global.side_effect = exceptions.InvalidArgumentsException
        self.assertRaises(exceptions.InvalidArgumentsException,
                          self.task.abort, self.fake_api, None)

    def test_status(self):
        test_uuid = "a3e7cefb-bec2-4802-89f6-410cc31f71af"
        value = {"task_id": "task", "status": "status"}
        self.fake_api.task.get.return_value = value
        self.task.status(self.fake_api, test_uuid)
        self.fake_api.task.get.assert_called_once_with(task_id=test_uuid)

    @mock.patch("rally.cli.commands.task.envutils.get_global")
    def test_status_no_task_id(self, mock_get_global):
        mock_get_global.side_effect = exceptions.InvalidArgumentsException
        self.assertRaises(exceptions.InvalidArgumentsException,
                          self.task.status, None)

    @ddt.data({"iterations_data": False, "has_output": True,
               "filters": None},
              {"iterations_data": True, "has_output": False,
               "filters": ["scenario=fake_name", "sla_failures"]})
    @ddt.unpack
    def test_detailed(self, iterations_data, has_output, filters):
        test_uuid = "c0d874d4-7195-4fd5-8688-abe82bfad36f"
        detailed_value = {
            "id": "task", "uuid": test_uuid,
            "pass_sla": False, "status": "finished",
            "subtasks": [{"workloads": [{
                "name": "fake_name", "position": "fake_pos",
                "args": "args", "contexts": "context", "sla": "sla",
                "runner": "runner", "hooks": [],
                "statistics": {
                    "durations": {
                        "atomics": [
                            {
                                "name": "foo",
                                "display_name": "foo (x2)",
                                "count_per_iteration": 2,
                                "children": [
                                    {"name": "inner_foo",
                                     "display_name": "inner_foo",
                                     "count_per_iteration": 1,
                                     "children": [],
                                     "data": {
                                         "min": 1,
                                         "median": 2,
                                         "90%ile": 1.5,
                                         "95%ile": 1.6,
                                         "max": 3,
                                         "avg": 1.4,
                                         "success": 3,
                                         "iteration_count": 3}
                                     }
                                ],
                                "data": {
                                    "min": 1,
                                    "median": 2,
                                    "90%ile": 1.5,
                                    "95%ile": 1.6,
                                    "max": 3,
                                    "avg": 1.4,
                                    "success": 3,
                                    "iteration_count": 3}},
                            {
                                "name": "bar",
                                "display_name": "bar",
                                "count_per_iteration": 1,
                                "children": [],
                                "data": {
                                    "min": 1.1,
                                    "median": 2.2,
                                    "90%ile": 1.6,
                                    "95%ile": 1.65,
                                    "max": 3,
                                    "avg": 1.5,
                                    "success": 3,
                                    "iteration_count": 3}
                            }],
                        "total": {
                            "name": "total",
                            "display_name": "bar",
                            "count_per_iteration": 1,
                            "children": [],
                            "data": {
                                "min": 1,
                                "median": 2.1,
                                "90%ile": 1.55,
                                "95%ile": 1.62,
                                "max": 3,
                                "avg": 1.45,
                                "success": 6,
                                "iteration_count": 6}}}},
                "load_duration": 3.2,
                "full_duration": 3.5,
                "total_iteration_count": 4,
                "data": [
                    {
                        "duration": 0.9,
                        "idle_duration": 0.1,
                        "output": {"additive": [], "complete": []},
                        "atomic_actions": [
                            {"name": "foo", "started_at": 0.0,
                             "finished_at": 0.6, "children": []},
                            {"name": "bar", "started_at": 0.6,
                             "finished_at": 1.3, "children": []}
                        ],
                        "error": ["type", "message", "traceback"]
                    },
                    {
                        "duration": 1.2,
                        "idle_duration": 0.3,
                        "output": {"additive": [], "complete": []},
                        "atomic_actions": [
                            {"name": "foo", "started_at": 0.0,
                             "finished_at": 0.6, "children": []},
                            {"name": "bar", "started_at": 0.6,
                             "finished_at": 1.3, "children": []}
                        ],
                        "error": ["type", "message", "traceback"]
                    },
                    {
                        "duration": 0.7,
                        "idle_duration": 0.5,
                        "output": {
                            "additive": [
                                {"data": [("foo", 0.6), ("bar", 0.7)],
                                 "title": "Scenario output",
                                 "description": "",
                                 "chart_plugin": "StackedArea"}
                            ],
                            "complete": []
                        },
                        "atomic_actions": [
                            {"name": "foo", "started_at": 0.0,
                             "finished_at": 0.6, "children": []},
                            {"name": "bar", "started_at": 0.6,
                             "finished_at": 1.3, "children": []}
                        ],
                        "error": ["type", "message", "traceback"]
                    },
                    {
                        "duration": 0.5,
                        "idle_duration": 0.5,
                        "atomic_actions": [
                            {"name": "foo", "started_at": 0.0,
                             "finished_at": 0.6, "children": []},
                            {"name": "bar", "started_at": 0.6,
                             "finished_at": 1.3, "children": []}
                        ],
                        "error": ["type", "message", "traceback"]
                    }],
            }]}]}
        if has_output:
            detailed_value["subtasks"][0]["workloads"][0]["output"] = {
                "additive": [], "complete": []}
        self.fake_api.task.get.return_value = detailed_value
        self.task.detailed(self.fake_api, test_uuid,
                           iterations_data=iterations_data,
                           filters=filters)
        self.fake_api.task.get.assert_called_once_with(
            task_id=test_uuid, detailed=True)

    @mock.patch("rally.cli.commands.task.sys.stdout")
    @mock.patch("rally.cli.commands.task.logging")
    @ddt.data({"debug": True},
              {"debug": False})
    @ddt.unpack
    def test_detailed_task_failed(self, mock_logging, mock_stdout, debug):
        test_uuid = "test_task_id"
        value = {
            "id": "task",
            "uuid": test_uuid,
            "status": consts.TaskStatus.CRASHED,
            "results": [],
            "validation_result": {"etype": "error_type",
                                  "msg": "error_message",
                                  "trace": "error_traceback"}
        }
        self.fake_api.task.get.return_value = value

        mock_logging.is_debug.return_value = debug
        self.task.detailed(self.fake_api, test_uuid)
        if debug:
            expected_calls = [
                mock.call("Task test_task_id: crashed"),
                mock.call("%s" % value["validation_result"]["trace"])]
            mock_stdout.write.assert_has_calls(expected_calls, any_order=True)
        else:
            expected_calls = [
                mock.call("Task test_task_id: crashed"),
                mock.call("%s" % value["validation_result"]["etype"]),
                mock.call("%s" % value["validation_result"]["msg"]),
                mock.call("\nFor more details run:\n"
                          "rally -d task detailed %s" % test_uuid)]
            mock_stdout.write.assert_has_calls(expected_calls, any_order=True)

    @mock.patch("rally.cli.commands.task.sys.stdout")
    def test_detailed_task_status_not_in_finished_abort(self, mock_stdout):
        test_uuid = "test_task_id"
        value = {
            "id": "task",
            "uuid": test_uuid,
            "status": consts.TaskStatus.INIT,
            "results": []
        }
        self.fake_api.task.get.return_value = value
        self.task.detailed(self.fake_api, test_uuid)
        expected_calls = [mock.call("Task test_task_id: init"),
                          mock.call("\nThe task test_task_id marked as "
                                    "'init'. Results available when it "
                                    "is 'finished'.")]
        mock_stdout.write.assert_has_calls(expected_calls, any_order=True)

    @mock.patch("rally.cli.commands.task.envutils.get_global")
    def test_detailed_no_task_id(self, mock_get_global):
        mock_get_global.side_effect = exceptions.InvalidArgumentsException
        self.assertRaises(exceptions.InvalidArgumentsException,
                          self.task.detailed, None)

    def test_detailed_wrong_id(self):
        test_uuid = "eb290c30-38d8-4c8f-bbcc-fc8f74b004ae"
        self.fake_api.task.get.side_effect = None
        self.task.detailed(self.fake_api, test_uuid)
        self.fake_api.task.get.assert_called_once_with(
            task_id=test_uuid, detailed=True)

    def _make_task(self, status=None, data=None):
        return {
            "status": status or consts.TaskStatus.FINISHED,
            "subtasks": [{"workloads": [{
                "full_duration": 1, "load_duration": 2,
                "created_at": "2017-09-27T07:22:55",
                "name": "Foo.bar", "description": "descr",
                "position": 2,
                "args": {"key1": "value1"},
                "runner_type": "rruunneerr",
                "runner": {"arg1": "args2"},
                "hooks": [],
                "sla": {"failure_rate": {"max": 0}},
                "sla_results": {"sla": [{"success": True}]},
                "contexts": {"users": {}},
                "data": data or []}]}]}

    @mock.patch("rally.plugins.task.exporters.old_json_results.json.dumps")
    def test_results(self, mock_json_dumps):

        mock_json_dumps.return_value = ""

        def fake_export(tasks, output_type, output_dest=None):
            tasks = [self.fake_api.task.get(u, detailed=True) for u in tasks]
            return self.real_api.task.export(tasks, output_type, output_dest)

        self.fake_api.task.export.side_effect = fake_export

        task_id = "foo_task_id"

        task_obj = self._make_task(
            data=[{"atomic_actions": [{"name": "foo",
                                       "started_at": 0,
                                       "finished_at": 1.1}]}]
        )
        task_obj["subtasks"][0]["workloads"][0]["hooks"] = [{
            "config": {
                "action": ("foo", "arg"),
                "trigger": ("bar", "arg2")
            },
            "summary": {"success": 1}}
        ]
        task_obj["uuid"] = task_id

        def fix_r(workload):
            cfg = workload["runner"]
            cfg["type"] = workload["runner_type"]
            return cfg

        result = map(
            lambda x: {
                "key": {"kw": {"sla": x["sla"],
                               "args": x["args"],
                               "context": x["contexts"],
                               "runner": fix_r(x),
                               "hooks": [{"description": "",
                                          "name": "foo",
                                          "args": "arg",
                                          "trigger": {"name": "bar",
                                                      "args": "arg2"}}]},
                        "pos": x["position"],
                        "name": x["name"],
                        "description": x["description"]},
                "result": x["data"],
                "load_duration": x["load_duration"],
                "full_duration": x["full_duration"],
                "created_at": dt.datetime.strptime(
                    x["created_at"], "%Y-%m-%dT%H:%M:%S").strftime(
                    "%Y-%d-%mT%H:%M:%S"),
                "hooks": [{
                    "config": {"description": "",
                               "name": "foo",
                               "args": "arg",
                               "trigger": {"name": "bar", "args": "arg2"}},
                    "summary": {"success": 1}}],
                "sla": x["sla_results"]["sla"]},
            task_obj["subtasks"][0]["workloads"])

        self.fake_api.task.get.return_value = task_obj

        self.assertIsNone(self.task.results(self.fake_api, task_id))
        self.assertEqual(1, mock_json_dumps.call_count)
        self.assertEqual(1, len(mock_json_dumps.call_args[0]))
        self.assertEqual(list(result), mock_json_dumps.call_args[0][0])
        self.assertEqual({"sort_keys": False, "indent": 4},
                         mock_json_dumps.call_args[1])
        self.fake_api.task.get.assert_called_once_with(
            task_id=task_id, detailed=True)

    @mock.patch("rally.cli.commands.task.sys.stdout")
    def test_results_no_data(self, mock_stdout):
        def fake_export(tasks, output_type, output_dest=None):
            tasks = [self.fake_api.task.get(u, detailed=True) for u in tasks]
            return self.real_api.task.export(tasks, output_type, output_dest)

        self.fake_api.task.export.side_effect = fake_export

        task_id = "foo"
        task_obj = self._make_task(status=consts.TaskStatus.CRASHED)
        task_obj["uuid"] = task_id
        self.fake_api.task.get.return_value = task_obj

        self.assertEqual(1, self.task.results(self.fake_api, task_id))

        self.fake_api.task.get.assert_called_once_with(
            task_id=task_id, detailed=True)

        expected_out = ("Task status is %s. Results "
                        "available when it is one of %s.") % (
            consts.TaskStatus.CRASHED,
            ", ".join((consts.TaskStatus.FINISHED,
                       consts.TaskStatus.ABORTED)))
        mock_stdout.write.assert_has_calls([mock.call(expected_out)])

    def test_trends(self):
        self.task.export = mock.MagicMock()
        self.task.trends(self.fake_api,
                         tasks=["uuid"],
                         out="output.html")
        self.task.export.assert_called_once_with(
            self.fake_api, tasks=["uuid"], output_type="trends-html",
            output_dest="output.html", open_it=False)

    def test_trends_no_tasks_given(self):
        ret = self.task.trends(self.fake_api, tasks=[],
                               out="output.html", out_format="html")
        self.assertEqual(1, ret)

    def test_report(self):
        self.task.export = mock.MagicMock()
        self.task.report(self.fake_api, tasks="uuid",
                         out="out", open_it=False, out_format="junit-xml")
        self.task.export.assert_called_once_with(
            self.fake_api, tasks="uuid", output_type="junit-xml",
            output_dest="out", open_it=False, deployment=None
        )

    @mock.patch("rally.cli.commands.task.cliutils.print_list")
    @mock.patch("rally.cli.commands.task.envutils.get_global",
                return_value="123456789")
    def test_list(self, mock_get_global, mock_print_list):
        self.fake_api.task.list.return_value = [
            {"uuid": "a",
             "created_at": "2007-01-01T00:00:01",
             "updated_at": "2007-01-01T00:00:03",
             "status": consts.TaskStatus.RUNNING,
             "tags": ["d"],
             "deployment_name": "some_name"}]
        self.task.list(self.fake_api, status="running")
        self.fake_api.task.list.assert_called_once_with(
            deployment=mock_get_global.return_value,
            status=consts.TaskStatus.RUNNING)

        headers = ["UUID", "Deployment name", "Created at", "Load duration",
                   "Status", "Tag(s)"]

        mock_print_list.assert_called_once_with(
            self.fake_api.task.list.return_value, fields=headers,
            normalize_field_names=True,
            sortby_index=headers.index("Created at"),
            formatters=mock.ANY)

    @mock.patch("rally.cli.commands.task.envutils.get_global",
                return_value="123456789")
    def test_list_uuids_only(self, mock_get_global):
        self.fake_api.task.list.return_value = [
            {"uuid": "a",
             "created_at": "2007-01-01T00:00:01",
             "updated_at": "2007-01-01T00:00:03",
             "status": consts.TaskStatus.RUNNING,
             "tags": ["d"],
             "deployment_name": "some_name"}]
        out = io.StringIO()
        with mock.patch.object(sys, "stdout", new=out):
            self.task.list(self.fake_api, status="running", uuids_only=True)
            self.assertEqual("a\n", out.getvalue())
        self.fake_api.task.list.assert_called_once_with(
            deployment=mock_get_global.return_value,
            status=consts.TaskStatus.RUNNING)

    def test_list_wrong_status(self):
        self.assertEqual(1, self.task.list(self.fake_api, deployment="fake",
                                           status="wrong non existing status"))

    def test_list_no_results(self):
        self.fake_api.task.list.return_value = []
        self.assertIsNone(self.task.list(self.fake_api, deployment="fake",
                                         all_deployments=True))
        self.fake_api.task.list.assert_called_once_with()
        self.fake_api.task.list.reset_mock()

        self.assertIsNone(self.task.list(self.fake_api, deployment="d",
                                         status=consts.TaskStatus.RUNNING))
        self.fake_api.task.list.assert_called_once_with(
            deployment="d", status=consts.TaskStatus.RUNNING)

    @mock.patch("rally.cli.commands.task.envutils.get_global",
                return_value="123456789")
    def test_list_output(self, mock_get_global):
        self.fake_api.task.list.return_value = [
            {"uuid": "UUID-1",
             "created_at": "2007-01-01T00:00:01",
             "task_duration": 0.0000009,
             "status": consts.TaskStatus.INIT,
             "tags": [],
             "deployment_name": "some_name"},
            {"uuid": "UUID-2",
             "created_at": "2007-02-01T00:00:01",
             "task_duration": 123.99992,
             "status": consts.TaskStatus.FINISHED,
             "tags": ["tag-1", "tag-2"],
             "deployment_name": "some_name"}]

        # It is a hard task to mock default value of function argument, so we
        # need to apply this workaround
        original_print_list = cliutils.print_list
        print_list_calls = []

        def print_list(*args, **kwargs):
            print_list_calls.append(io.StringIO())
            kwargs["out"] = print_list_calls[-1]
            original_print_list(*args, **kwargs)

        with mock.patch.object(task.cliutils, "print_list",
                               new=print_list):
            self.task.list(self.fake_api, status="running")

        self.assertEqual(1, len(print_list_calls))

        self.assertEqual(
            "+--------+-----------------+---------------------"
            "+---------------+----------+------------------+\n"
            "| UUID   | Deployment name | Created at          "
            "| Load duration | Status   | Tag(s)           |\n"
            "+--------+-----------------+---------------------"
            "+---------------+----------+------------------+\n"
            "| UUID-1 | some_name       | 2007-01-01 00:00:01 "
            "| 0.0           | init     |                  |\n"
            "| UUID-2 | some_name       | 2007-02-01 00:00:01 "
            "| 124.0         | finished | 'tag-1', 'tag-2' |\n"
            "+--------+-----------------+---------------------"
            "+---------------+----------+------------------+\n",
            print_list_calls[0].getvalue())

    def test_delete(self):
        task_uuid = "8dcb9c5e-d60b-4022-8975-b5987c7833f7"
        force = False
        self.task.delete(self.fake_api, task_uuid, force=force)
        self.fake_api.task.delete.assert_called_once_with(
            task_uuid=task_uuid, force=force)

    def test_delete_multiple_uuid(self):
        task_uuids = ["4bf35b06-5916-484f-9547-12dce94902b7",
                      "52cad69d-d3e4-47e1-b445-dec9c5858fe8",
                      "6a3cb11c-ac75-41e7-8ae7-935732bfb48f",
                      "018af931-0e5a-40d5-9d6f-b13f4a3a09fc"]
        force = False
        self.task.delete(self.fake_api, task_uuids, force=force)
        self.assertTrue(
            self.fake_api.task.delete.call_count == len(task_uuids))
        expected_calls = [mock.call(task_uuid=task_uuid,
                                    force=force) for task_uuid in task_uuids]
        self.assertTrue(self.fake_api.task.delete.mock_calls == expected_calls)

    @mock.patch("rally.cli.commands.task.cliutils.print_list")
    def test_sla_check(self, mock_print_list):
        task_obj = self._make_task()
        task_obj["subtasks"][0]["workloads"][0]["sla_results"]["sla"] = [
            {"benchmark": "KeystoneBasic.create_user",
             "criterion": "max_seconds_per_iteration",
             "pos": 0, "success": False, "detail": "Max foo, actually bar"}]

        self.fake_api.task.get.return_value = task_obj
        result = self.task.sla_check(self.fake_api, task_id="fake_task_id")
        self.assertEqual(1, result)
        self.fake_api.task.get.assert_called_with(
            task_id="fake_task_id", detailed=True)

        task_obj["subtasks"][0]["workloads"][0]["sla_results"]["sla"][0][
            "success"] = True

        result = self.task.sla_check(self.fake_api, task_id="fake_task_id",
                                     tojson=True)
        self.assertEqual(0, result)

    @mock.patch("rally.cli.commands.task.os.path.isfile", return_value=True)
    @mock.patch("rally.cli.commands.task.open",
                side_effect=mock.mock_open(read_data="{\"some\": \"json\"}"),
                create=True)
    def test_validate(self, mock_open, mock_os_path_isfile):
        self.fake_api.task.render_template = self.real_api.task.render_template

        self.task.validate(self.fake_api, "path_to_config.json", "fake_id")

        self.fake_api.task.validate.assert_called_once_with(
            deployment="fake_id", config={"some": "json"})

    @mock.patch("rally.cli.commands.task.TaskCommands._load_and_validate_task",
                side_effect=task.FailedToLoadTask)
    def test_validate_failed_to_load_task(self, mock__load_and_validate_task):
        args = "args"
        args_file = "args_file"

        mock__load_and_validate_task.side_effect = KeyError("foo")

        self.assertRaises(KeyError, self.task.validate, self.real_api,
                          "path_to_task", "fake_deployment_id",
                          task_args=args, task_args_file=args_file)
        self.assertFalse(self.fake_api.task.validate.called)

        mock__load_and_validate_task.assert_called_once_with(
            self.real_api, "path_to_task", raw_args=args, args_file=args_file)

    @mock.patch("rally.cli.commands.task.TaskCommands._load_and_validate_task")
    def test_validate_invalid(self, mock__load_and_validate_task):
        exc = exceptions.InvalidTaskException("foo")
        self.fake_api.task.validate.side_effect = exc
        self.assertRaises(exceptions.InvalidTaskException,
                          self.task.validate, self.fake_api, "path_to_task",
                          "deployment")
        self.fake_api.task.validate.assert_called_once_with(
            deployment="deployment",
            config=mock__load_and_validate_task.return_value)

    @mock.patch("rally.cli.envutils._rewrite_env_file")
    def test_use(self, mock__rewrite_env_file):
        task_id = "80422553-5774-44bd-98ac-38bd8c7a0feb"
        self.task.use(self.fake_api, task_id)
        mock__rewrite_env_file.assert_called_once_with(
            os.path.expanduser("~/.rally/globals"),
            ["RALLY_TASK=%s\n" % task_id])

    def test_use_not_found(self):
        task_id = "ddc3f8ba-082a-496d-b18f-72cdf5c10a14"
        exc = exceptions.DBRecordNotFound(criteria="uuid: %s" % task_id,
                                          table="tasks")
        self.fake_api.task.get.side_effect = exc
        self.assertRaises(exceptions.DBRecordNotFound, self.task.use,
                          self.fake_api, task_id)

    @mock.patch("rally.cli.task_results_loader.load")
    @mock.patch("rally.cli.commands.task.os.path")
    @mock.patch("rally.cli.commands.task.webbrowser.open_new_tab")
    @mock.patch("rally.cli.commands.task.open", create=True)
    @mock.patch("rally.cli.commands.task.print")
    def test_export(self, mock_print, mock_open, mock_open_new_tab,
                    mock_path, mock_load):

        # file
        self.fake_api.task.export.return_value = {
            "files": {"output_dest": "content"}, "open": "output_dest"}
        mock_path.exists.side_effect = [False, True, False]
        mock_path.expanduser.return_value = "output_file"
        mock_path.realpath.return_value = "real_path"
        mock_fd = mock.mock_open()
        mock_open.side_effect = mock_fd
        mock_load.return_value = [{"task": "task_1"}, {"task": "task2"}]

        self.task.export(self.fake_api, tasks=["uuid", "file"],
                         output_type="json", output_dest="output_dest",
                         open_it=True)

        self.fake_api.task.export.assert_called_once_with(
            tasks=["uuid"] + mock_load.return_value,
            output_type="json",
            output_dest="output_dest"
        )
        mock_open.assert_called_once_with("output_file", "w+")
        mock_fd.return_value.write.assert_called_once_with("content")

        # print
        self.fake_api.task.export.reset_mock()
        self.fake_api.task.export.return_value = {"print": "content"}
        self.task.export(self.fake_api, tasks="uuid", output_type="json")
        self.fake_api.task.export.assert_called_once_with(
            tasks=["uuid"],
            output_type="json", output_dest=None
        )
        mock_print.assert_called_once_with("content")

    @mock.patch("rally.cli.commands.task.sys.stdout")
    @ddt.data({"error_type": "test_no_trace_type",
               "error_message": "no_trace_error_message",
               "error_traceback": None,
               },
              {"error_type": "test_error_type",
               "error_message": "test_error_message",
               "error_traceback": "test\nerror\ntraceback",
               })
    @ddt.unpack
    def test_show_task_errors_no_trace(self, mock_stdout,
                                       error_type, error_message,
                                       error_traceback=None):
        test_uuid = "test_task_id"
        error_data = [error_type, error_message]
        if error_traceback:
            error_data.append(error_traceback)
        self.fake_api.task.get.return_value = {
            "id": "task",
            "uuid": test_uuid,
            "status": "finished",
            "pass_sla": True,
            "subtasks": [{"workloads": [{
                "name": "fake_name",
                "position": "fake_pos",
                "args": {}, "runner_type": "foo",
                "runner": {}, "contexts": {}, "sla": {},
                "hooks": {},
                "load_duration": 3.2,
                "full_duration": 3.5,
                "statistics": {
                    "durations": {
                        "atomics": [
                            {
                                "name": "foo",
                                "display_name": "foo (x2)",
                                "count_per_iteration": 2,
                                "children": [],
                                "data": {
                                    "min": 1,
                                    "median": 2,
                                    "90%ile": 1.5,
                                    "95%ile": 1.6,
                                    "max": 3,
                                    "avg": 1.4,
                                    "success": 3,
                                    "iteration_count": 3}},
                            {
                                "name": "bar",
                                "display_name": "bar",
                                "count_per_iteration": 1,
                                "children": [],
                                "data": {
                                    "min": 1.1,
                                    "median": 2.2,
                                    "90%ile": 1.6,
                                    "95%ile": 1.65,
                                    "max": 3,
                                    "avg": 1.5,
                                    "success": 3,
                                    "iteration_count": 3}
                            }],
                        "total": {
                            "name": "total",
                            "display_name": "bar",
                            "count_per_iteration": 1,
                            "children": [],
                            "data": {
                                "min": 1,
                                "median": 2.1,
                                "90%ile": 1.55,
                                "95%ile": 1.62,
                                "max": 3,
                                "avg": 1.45,
                                "success": 6,
                                "iteration_count": 6}}}
                },
                "total_iteration_count": 1,
                "total_iteration_failed": 1,
                "data": [
                    {"duration": 0.9,
                     "idle_duration": 0.1,
                     "output": {"additive": [], "complete": []},
                     "atomic_actions": {"foo": 0.6, "bar": 0.7},
                     "error": error_data
                     },
                ]},
            ]}],
            "validation_result": json.dumps([error_type, error_message,
                                             error_traceback])
        }
        self.task.detailed(self.fake_api, test_uuid)
        self.fake_api.task.get.assert_called_once_with(
            task_id=test_uuid, detailed=True)
        mock_stdout.write.assert_has_calls([
            mock.call(error_traceback or "No traceback available.")
        ], any_order=False)

    @mock.patch("rally.cli.task_results_loader.load")
    @mock.patch("rally.cli.commands.task.os.path")
    def test_import_results(self, mock_os_path, mock_load):
        mock_os_path.exists.return_value = True
        mock_os_path.expanduser = lambda path: path
        mock_load.return_value = ["results"]

        self.task.import_results(self.fake_api,
                                 "deployment_uuid",
                                 "task_file", tags=["tag"])

        mock_load.assert_called_once_with("task_file")
        self.fake_api.task.import_results.assert_called_once_with(
            deployment="deployment_uuid", task_results="results",
            tags=["tag"])

        # not exist
        mock_os_path.exists.return_value = False
        self.assertEqual(
            1,
            self.task.import_results(self.fake_api,
                                     "deployment_uuid",
                                     "task_file", ["tag"])
        )