1# -*-coding:utf-8 -*
2
3# Copyright (c) 2011-2015, Intel Corporation
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without modification,
7# are permitted provided that the following conditions are met:
8#
9# 1. Redistributions of source code must retain the above copyright notice, this
10# list of conditions and the following disclaimer.
11#
12# 2. Redistributions in binary form must reproduce the above copyright notice,
13# this list of conditions and the following disclaimer in the documentation and/or
14# other materials provided with the distribution.
15#
16# 3. Neither the name of the copyright holder nor the names of its contributors
17# may be used to endorse or promote products derived from this software without
18# specific prior written permission.
19#
20# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
21# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
24# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
27# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31import subprocess
32import unittest
33import time
34
35class RemoteCli(object):
36    def sendCmd(self, cmd, *args):
37        shell_cmd = " ".join([self.platform_command, cmd])
38        if args is not None:
39            shell_cmd += " " + " ".join(args)
40        print "CMD  :",
41        print "[" + shell_cmd + "]"
42        try:
43            p = subprocess.Popen(shell_cmd, shell=True, stdout=subprocess.PIPE)
44        except Exception as (errno, strerror):
45            return None, strerror
46        out, err = p.communicate()
47        if out is not None:
48            out = out.strip()
49        return out, err
50
51class Pfw(RemoteCli):
52    def __init__(self):
53        self.platform_command = "remote-process localhost 5000 "
54
55class Hal(RemoteCli):
56    def __init__(self):
57        self.platform_command = "remote-process localhost 5001 "
58
59    # Starts the HAL exe
60    def startHal(self):
61        cmd= "test-platform $PFW_TEST_CONFIGURATION"
62        subprocess.Popen(cmd, shell=True)
63        pass
64
65    # Send command "stop" to the HAL
66    def stopHal(self):
67        subprocess.call("remote-process localhost 5001 exit", shell=True)
68
69    def createInclusiveCriterion(self, name, nb):
70        self.sendCmd("createInclusiveSelectionCriterion", name, nb)
71
72    def createExclusiveCriterion(self, name, nb):
73        self.sendCmd("createExclusiveSelectionCriterion", name, nb)
74
75    # Starts the Pfw
76    def start(self):
77        self.sendCmd("start")
78
79# A PfwTestCase gather tests performed on one instance of the PFW.
80class PfwTestCase(unittest.TestCase):
81
82    hal = Hal()
83
84    def __init__(self, argv):
85        super(PfwTestCase, self).__init__(argv)
86        self.pfw = Pfw()
87
88    @classmethod
89    def setUpClass(cls):
90        cls.startHal()
91
92    @classmethod
93    def tearDownClass(cls):
94        cls.stopHal()
95
96    @classmethod
97    def startHal(cls):
98        # set up the Hal & pfw
99        cls.hal.startHal()
100        time.sleep(0.1)
101        # create criterions
102        cls.hal.createInclusiveCriterion("Crit_0", "2")
103        cls.hal.createExclusiveCriterion("Crit_1", "2")
104        # start the Pfw
105        cls.hal.start()
106
107    @classmethod
108    def stopHal(cls):
109        cls.hal.stopHal()
110        time.sleep(0.1)
111