change ci from base-on-pod to base-on-scenario (in progress)
[yardstick.git] / tests / unit / cmd / commands / test_task.py
1 #!/usr/bin/env python
2
3 ##############################################################################
4 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
5 #
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Apache License, Version 2.0
8 # which accompanies this distribution, and is available at
9 # http://www.apache.org/licenses/LICENSE-2.0
10 ##############################################################################
11
12 # Unittest for yardstick.cmd.commands.task
13
14 import mock
15 import unittest
16
17 from yardstick.cmd.commands import task
18
19
20 class TaskCommandsTestCase(unittest.TestCase):
21
22     @mock.patch('yardstick.cmd.commands.task.Context')
23     def test_parse_nodes_host_target_same_context(self, mock_context):
24         nodes = {
25             "host": "node1.LF",
26             "target": "node2.LF"
27         }
28         scenario_cfg = {"nodes": nodes}
29         server_info = {
30            "ip": "10.20.0.3",
31            "user": "root",
32            "key_filename": "/root/.ssh/id_rsa"
33         }
34         mock_context.get_server.return_value = server_info
35         context_cfg = task.parse_nodes_with_context(scenario_cfg)
36
37         self.assertEqual(context_cfg["host"], server_info)
38         self.assertEqual(context_cfg["target"], server_info)
39
40     @mock.patch('yardstick.cmd.commands.task.Context')
41     @mock.patch('yardstick.cmd.commands.task.base_runner')
42     def test_run(self, mock_base_runner, mock_ctx):
43         scenario = \
44             {'host': 'athena.demo',
45              'target': 'ares.demo',
46              'runner':
47                  {'duration': 60,
48                   'interval': 1,
49                   'type': 'Duration'
50                  },
51                  'type': 'Ping'}
52
53         t = task.TaskCommands()
54         runner = mock.Mock()
55         runner.join.return_value = 0
56         mock_base_runner.Runner.get.return_value = runner
57         t._run([scenario], False, "yardstick.out")
58         self.assertTrue(runner.run.called)
59
60     @mock.patch('yardstick.cmd.commands.task.os')
61     def test_check_precondition(self, mock_os):
62         cfg = \
63             {'precondition':
64                  {'installer_type': 'compass',
65                   'deploy_scenarios': 'os-nosdn',
66                   'pod_name': 'huawei-pod1'
67                  }
68             }
69
70         t = task.TaskParser('/opt')
71         mock_os.environ.get.side_effect = ['compass', 'os-nosdn', 'huawei-pod1']
72         result = t._check_precondition(cfg)
73         self.assertTrue(result)