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
31"""
32Split elements from domains testcases
33
34List of tested functions :
35--------------------------
36    - [splitDomain]  function
37    - [listBelongingDomains] function
38    - [listAssociatedDomains] function
39    - [listAssociatedElements] function
40    - [listConflictingElements] function
41    - [listRogueElements] function
42Test cases :
43------------
44    - Testing nominal case
45"""
46import os.path
47from Util.PfwUnitTestLib import PfwTestCase
48from Util import ACTLogging
49log=ACTLogging.Logger()
50
51class TestCases(PfwTestCase):
52
53    def setUp(self):
54        self.pfw.sendCmd("setTuningMode", "on")
55        self.reference_xml = "$PFW_TEST_TOOLS/xml/XML_Test/Reference_Split_Domain.xml"
56
57        self.temp_domain="f_Domains_Backup"
58        self.temp_status="f_Config_Status"
59
60        self.path_main = "/Test/Test/TEST_MAIN/"
61        self.path_dir_0 = "/Test/Test/TEST_MAIN/TEST_DIR_0"
62        self.path_dir_1 = "/Test/Test/TEST_MAIN/TEST_DIR_1"
63        self.path_dir_2 = "/Test/Test/TEST_MAIN/TEST_DIR_2"
64        self.dir_nbr = 3
65        self.element_name = "TEST_DIR"
66
67        self.domain_1 = "Domain_1"
68        self.domain_2 = "Domain_2"
69        self.domain_3 = "Domain_3"
70
71        self.temp_file="f_temp_file"
72
73    def tearDown(self):
74        self.pfw.sendCmd("setTuningMode", "off")
75        if os.path.exists(self.temp_file):
76            os.remove(self.temp_file)
77    def test_Combinatorial_Criteria(self):
78        """
79        Testing combinatorial criteria
80        ------------------------------
81            Test case description :
82            ~~~~~~~~~~~~~~~~~~~~~~~
83                - Split a configuration element associated to a domain
84                - Check that the configuration element children are associated to the domain
85                - Pass a configuration element to rogue element
86                - Add a configuration element to another domain and heck that this element is
87                conflicting while not removed from original domain.
88
89            Tested commands :
90           ~~~~~~~~~~~~~~~~~
91                - [splitDomain]  function
92                - [listBelongingDomains] function
93                - [listAssociatedDomains] function
94                - [listAssociatedElements] function
95                - [listConflictingElements] function
96                - [listRogueElements] function
97            Expected result :
98            ~~~~~~~~~~~~~~~~~
99                - Conform to expected behavior
100        """
101        log.D(self.test_Combinatorial_Criteria.__doc__)
102
103        # Import a reference XML file
104        log.I("Import Domains with settings from %s"%(self.reference_xml))
105        out, err = self.pfw.sendCmd("importDomainsWithSettingsXML",self.reference_xml, "")
106        assert err == None, log.E("Command [importDomainsWithSettingsXML %s] : %s"%(self.reference_xml,err))
107        assert out == "Done", log.F("When using function importDomainsWithSettingsXML %s]"%(self.reference_xml))
108
109        # Checking initial state
110        # Checking domain integrity
111        log.I("Checking initial conditions :")
112        log.I("Checking that %s configurable element is associated to %s :" % (self.path_main,self.domain_1))
113        out, err = self.pfw.sendCmd("listAssociatedDomains", self.path_main)
114        assert err == None, log.E("Command [listAssociatedDomains] : error when listing domain name")
115        f_temp_file = open(self.temp_file, "w")
116        f_temp_file.write(out)
117        f_temp_file.close()
118        element_name = self.domain_1
119        element_found = 0
120        for line in open(self.temp_file, "r"):
121            if element_name in line:
122                element_found = 1
123        assert element_found==1, log.F("configurable element %s not correctly associated to domain %s" % (self.path_main, self.domain_1))
124        log.I("configurable element %s correctly associated to domain %s" % (self.path_main, self.domain_1))
125        # Deleting temp file
126        os.remove(self.temp_file)
127
128        # Checking children integrity
129        log.I("Checking that %s children configurable elements are correctly set for the test" % (self.path_main))
130        out, err = self.pfw.sendCmd("listElements", self.path_main)
131        assert err == None, log.E("Command [listElements] : listing error")
132        f_temp_file = open(self.temp_file, "w")
133        f_temp_file.write(out)
134        f_temp_file.close()
135        for index in range (self.dir_nbr):
136            element_name = "".join([self.element_name, "_", str(index)])
137            element_found = 0
138            for line in open(self.temp_file, "r"):
139                if element_name in line:
140                    element_found = 1
141            assert element_found==1, log.F("Element %s not found in %s" % (element_name, self.path_main))
142            log.I("Element %s found in %s" % (element_name, self.path_main))
143            # Checking that child element belong to domain
144            element_path = "".join([self.path_main, element_name])
145            out, err = self.pfw.sendCmd("listBelongingDomains", element_path)
146            assert err == None, log.E("Command [listBelongingDomains] : listing error")
147            assert out == self.domain_1, log.F("Wrong behavior : %s not belonging to %s " % (element_name, self.domain_1))
148            # Checking that child element is not associated to domain, and only belong to it
149            out, err = self.pfw.sendCmd("listAssociatedDomains", element_path)
150            assert err == None, log.E("Command [listAssociatedDomains] : listing error")
151            assert out == '', log.F("Wrong behavior : configurable element %s not associated to %s" % (element_name, self.domain_1))
152            log.I("configurable element %s is belonging to %s" % (element_name, self.domain_1))
153        log.I("Configurable elements : check OK")
154        # Deleting temp file
155        os.remove(self.temp_file)
156
157        # Split domain_0
158        log.I("Splitting configurable element %s from %s" % (self.path_main, self.domain_1))
159        out, err = self.pfw.sendCmd("splitDomain", self.domain_1, self.path_main)
160        assert err == None, log.E("Command [splitDomain] : %s" % (err))
161        assert out == 'Done', log.F("Wrong behavior : configurable element %s not splitted correctly" % (self.path_main))
162        log.I("Splitting done")
163
164        # check that the configurable element splitted is neither associated nor belonging to the domain
165        log.I("Checking that %s is neither associated nor belonging to %s" % (self.path_main, self.domain_1))
166        out, err = self.pfw.sendCmd("listBelongingDomains", self.path_main)
167        assert err == None, log.E("Command [listBelongingDomains] : listing error")
168        assert out != self.domain_1, log.F("Wrong behavior : %s still belonging to %s" % (self.path_main, self.domain_1))
169        out, err = self.pfw.sendCmd("listAssociatedDomains", self.path_main)
170        assert err == None, log.E("Command [listAssociatedDomains] : listing error")
171        assert out == '', log.F("Wrong behavior : configurable element %s still associated to %s" % (self.path_main, self.domain_1))
172        log.I("Configurable element %s is no longer associated to %s" % (self.path_main, self.domain_1))
173
174        # Checking that children configurable elements are now associated to domain
175        log.I("Checking that %s children configurable elements are now associated to %s" % (self.path_main, self.domain_1))
176        out, err = self.pfw.sendCmd("listElements", self.path_main)
177        assert err == None, log.E("Command [listElements] : listing error")
178        f_temp_file = open(self.temp_file, "w")
179        f_temp_file.write(out)
180        f_temp_file.close()
181        for index in range (self.dir_nbr):
182            element_name = "".join([self.element_name, "_", str(index)])
183            element_found = 0
184            for line in open(self.temp_file, "r"):
185                if element_name in line:
186                    element_found = 1
187            assert element_found==1, log.F("Element %s not found in %s" % (element_name, self.path_main))
188            log.I("Element %s found in %s" % (element_name, self.path_main))
189            # Checking that child element is associated to domain
190            element_path = "".join([self.path_main, element_name])
191            out, err = self.pfw.sendCmd("listAssociatedDomains", element_path)
192            assert err == None, log.E("Command [listAssociatedDomains] : listing error")
193            assert out == self.domain_1, log.F("Wrong behavior : configurable element %s not associated to %s" % (element_name, self.domain_1))
194            log.I("configurable element %s is associated to %s" % (element_name, self.domain_1))
195        log.I("Configurable elements : check OK")
196        # Deleting temp file
197        os.remove(self.temp_file)
198
199        # Removing one element from domain and checking that it becomes a rogue element
200        log.I("Removing domain element %s from domain %s" % (self.path_dir_0, self.domain_1))
201        out, err = self.pfw.sendCmd("removeElement", str(self.domain_1), str(self.path_dir_0))
202        assert err == None, log.E("ERROR : command [removeElement] - Error while removing domain element %s" % (self.path_dir_0))
203        assert out == "Done", log.F("Domain element %s not correctly removed" % (self.path_dir_0))
204        log.I("Domain element %s correctly removed from domain %s" % (self.path_dir_0, self.domain_1))
205        log.I("Checking that %s is a rogue element" % (self.path_dir_0))
206        out, err = self.pfw.sendCmd("listRogueElements")
207        assert err == None, log.E("command [listRogueElements] - Error while listing rogue elements")
208        f_temp_file = open(self.temp_file, "w")
209        f_temp_file.write(out)
210        f_temp_file.close()
211        element_found = 0
212        for line in open(self.temp_file, "r"):
213            if self.path_dir_0 in line:
214                element_found = 1
215        assert element_found==1, log.F("Configurable element %s not found in rogue elements" % (self.path_dir_0))
216        log.I("Element %s found in rogue elements" % (self.path_dir_0))
217
218        # Moving one configuration element to another domain
219        log.I("Moving configurable element %s from domain %s to domain %s" % (self.path_dir_1, self.domain_1, self.domain_2))
220        log.I("Adding %s to domain %s" % (self.path_dir_1, self.domain_2))
221        out, err = self.pfw.sendCmd("addElement", self.domain_2, self.path_dir_1)
222        assert err == None, log.E("ERROR : command [addElement] - Error while adding element %s to domain %s" % (self.path_dir_1, self.domain_2))
223        out, err = self.pfw.sendCmd("listConflictingElements")
224        assert err == None, log.E("command [listConflictingElements] - Error while listing conflicting elements")
225        f_temp_file = open(self.temp_file, "w")
226        f_temp_file.write(out)
227        f_temp_file.close()
228        element_found = 0
229        for line in open(self.temp_file, "r"):
230            if self.path_dir_1 in line:
231                element_found = 1
232        assert element_found==1, log.F("Configurable element %s not found in conflicting elements" % (self.path_dir_1))
233        log.I("Element %s found in conflicting elements" % (self.path_dir_1))
234        log.I("Removing %s from domain %s" % (self.path_dir_1, self.domain_1))
235        out, err = self.pfw.sendCmd("removeElement", self.domain_1, self.path_dir_1)
236        assert err == None, log.E("ERROR : command [removeElement] - Error while removing element %s from domain %s" % (self.path_dir_1, self.domain_2))
237        out, err = self.pfw.sendCmd("listConflictingElements")
238        assert err == None, log.E("command [listConflictingElements] - Error while listing conflicting elements")
239        f_temp_file = open(self.temp_file, "w")
240        f_temp_file.write(out)
241        f_temp_file.close()
242        element_found = 0
243        for line in open(self.temp_file, "r"):
244            if self.path_dir_1 in line:
245                element_found = 1
246        assert element_found!=1, log.F("Configurable element %s still found in conflicting elements" % (self.path_dir_1))
247        log.I("Element %s no longer found in conflicting elements" % (self.path_dir_1))
248