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"""
32Adding and Removing elements from domain testcases
33
34List of tested functions :
35--------------------------
36    - [listDomainElements]  function
37    - [addElement] function
38    - [removeElement] function
39
40Test cases :
41------------
42    - Testing nominal case
43    - Testing addElement errors
44    - Testing removeElement errors
45"""
46import os
47from Util.PfwUnitTestLib import PfwTestCase
48from Util import ACTLogging
49log=ACTLogging.Logger()
50
51class TestCases(PfwTestCase):
52    def setUp(self):
53        self.pfw.sendCmd("setTuningMode", "on")
54        self.domain_name = "Domain_0"
55        self.elem_0_path = "/Test/Test/TEST_DIR"
56        self.elem_1_path = "/Test/Test/TEST_DOMAIN_0"
57        self.elem_2_path = "/Test/Test/TEST_DOMAIN_1"
58
59    def tearDown(self):
60        self.pfw.sendCmd("setTuningMode", "off")
61
62    def test_Nominal_Case(self):
63        """
64        Testing nominal case
65        --------------------
66            Test case description :
67            ~~~~~~~~~~~~~~~~~~~~~~~
68                - list and backup initial domain elements
69                - add a domain element
70                - remove a domain element
71                - list and check domains elements
72            Tested commands :
73            ~~~~~~~~~~~~~~~~~
74                - [listDomainElements] function
75                - [addElement] function
76                - [removeElement] function
77            Expected result :
78            ~~~~~~~~~~~~~~~~~
79                - all operations succeed
80        """
81        log.D(self.test_Nominal_Case.__doc__)
82
83        # List and backup initial domain elements
84        log.I("Listing initial domain %s elements" % (self.domain_name))
85        out, err = self.pfw.sendCmd("listDomainElements",str(self.domain_name))
86        assert err == None, "ERROR : command [listDomainElements] - Error while listing domain elements"
87        f_DomainElements_Backup = open("f_DomainElements_Backup", "w")
88        f_DomainElements_Backup.write(out)
89        f_DomainElements_Backup.close()
90        log.I("command [listDomainElements] correctly executed")
91        f_DomainElements_Backup = open("f_DomainElements_Backup", "r")
92        element_nbr_init = 0
93        line=f_DomainElements_Backup.readline()
94        while line!="":
95            line=f_DomainElements_Backup.readline()
96            element_nbr_init+=1
97        f_DomainElements_Backup.close()
98        log.I("Actual domain %s elements number is %s" % (self.domain_name,element_nbr_init))
99
100        # Adding a new domain element
101        log.I("Adding a new domain element to domain %s" % (self.domain_name))
102        out, err = self.pfw.sendCmd("addElement", str(self.domain_name), str(self.elem_1_path))
103        assert err == None, "ERROR : command [addElement] - Error while adding new domain element %s" % (self.elem_1_path)
104        assert out == "Done", "ERROR : command [addElement] - Error while adding new domain element %s" % (self.elem_1_path)
105        log.I("Adding a new domain element to domain %s" % (self.domain_name))
106        out, err = self.pfw.sendCmd("addElement", str(self.domain_name), str(self.elem_2_path))
107        assert err == None, "ERROR : command [addElement] - Error while adding new domain element %s" % (self.elem_2_path)
108        assert out == "Done", "ERROR : command [addElement] - Error while adding new domain element %s" % (self.elem_2_path)
109        log.I("New domain elements %s and %s added to domain %s" % (self.elem_1_path, self.elem_2_path, self.domain_name))
110
111        # Removing a domain element
112        log.I("Removing domain element %s from domain %s" % (self.elem_1_path,self.domain_name))
113        out, err = self.pfw.sendCmd("removeElement", str(self.domain_name), str(self.elem_1_path))
114        assert err == None, "ERROR : command [removeElement] - Error while removing domain element %s" % (self.elem_1_path)
115        assert out == "Done", "ERROR : command [removeElement] - Error while removing domain element %s" % (self.elem_1_path)
116
117        # Checking final domain elements
118        log.I("Listing final domain %s elements" % (self.domain_name))
119        out, err = self.pfw.sendCmd("listDomainElements",str(self.domain_name))
120        assert err == None, "ERROR : command [listDomainElements] - Error while listing domain elements"
121        f_DomainElements = open("f_DomainElements", "w")
122        f_DomainElements.write(out)
123        f_DomainElements.close()
124        log.I("command [listDomainElements] correctly executed")
125        f_DomainElements = open("f_DomainElements", "r")
126        element_nbr = 0
127        line=f_DomainElements.readline()
128        while line!="":
129            line=f_DomainElements.readline()
130            element_nbr+=1
131        f_DomainElements.close()
132        log.I("Actual domain %s elements number is %s" % (self.domain_name,element_nbr))
133        log.I("Checking domain %s elements names conformity" % (self.domain_name))
134        f_DomainElements = open("f_DomainElements", "r")
135        f_DomainElements_Backup = open("f_DomainElements_Backup", "r")
136        for line in range(element_nbr):
137            # initial domain elements shall not have been impacted by current test
138            if (line < element_nbr_init):
139                element_name = f_DomainElements.readline().strip('\r\n')
140                element_name_backup = f_DomainElements_Backup.readline().strip('\r\n')
141                assert element_name==element_name_backup, "ERROR : Error while modifying domain elements on domain %s" % (self.domain_name)
142            # last listed element shall be equal to the only one element added previously
143            else:
144                element_name = f_DomainElements.readline().strip('\r\n')
145                assert element_name==str(self.elem_2_path), "ERROR : Error while modifying domain elements on domain %s" % (self.domain_name)
146        log.I("Actual domain %s elements names conform to expected values" % (self.domain_name))
147        # Temporary files deletion
148        f_DomainElements.close()
149        f_DomainElements_Backup.close()
150        os.remove("f_DomainElements_Backup")
151        os.remove("f_DomainElements")
152        # Removing created domain element
153        out, err = self.pfw.sendCmd("removeElement", str(self.domain_name), str(self.elem_2_path))
154        assert err == None, "ERROR : command [removeElement] - Error while removing domain element %s" % (self.elem_2_path)
155        assert out == "Done", "ERROR : command [removeElement] - Error while removing domain element %s" % (self.elem_2_path)
156
157    def test_addElement_Error(self):
158        """
159        Testing addElement error
160        ------------------------
161            Test case description :
162            ~~~~~~~~~~~~~~~~~~~~~~~
163                - add an already existing domain element
164                - add a non defined domain element
165            Tested commands :
166            ~~~~~~~~~~~~~~~~~
167                - [addElement] function
168                - [listDomainElements] function
169            Expected result :
170            ~~~~~~~~~~~~~~~~~
171                - Errors correctly detected
172                - No side effect
173        """
174        log.D(self.test_addElement_Error.__doc__)
175
176        # List and backup initial domain elements
177        log.I("Listing initial domain %s elements" % (self.domain_name))
178        out, err = self.pfw.sendCmd("listDomainElements",str(self.domain_name))
179        assert err == None, "ERROR : command [listDomainElements] - Error while listing domain elements"
180        f_DomainElements_Backup = open("f_DomainElements_Backup", "w")
181        f_DomainElements_Backup.write(out)
182        f_DomainElements_Backup.close()
183        log.I("command [listDomainElements] correctly executed")
184        f_DomainElements_Backup = open("f_DomainElements_Backup", "r")
185        element_nbr_init = 0
186        line=f_DomainElements_Backup.readline()
187        while line!="":
188            line=f_DomainElements_Backup.readline()
189            element_nbr_init+=1
190        f_DomainElements_Backup.close()
191        log.I("Actual domain %s elements number is %s" % (self.domain_name,element_nbr_init))
192
193        # Adding a new domain element errors
194        log.I("Adding an already existing domain element to domain %s" % (self.domain_name))
195        out, err = self.pfw.sendCmd("addElement", str(self.domain_name), str(self.elem_0_path), expectSuccess=False)
196        assert err == None, "ERROR : command [addElement] - Error while adding new domain element %s" % (self.elem_0_path)
197        assert out != "Done", "ERROR : command [addElement] - Error not detected while adding an already existing domain element to domain %s" % (self.domain_name)
198        log.I("Adding a non defined domain element to domain %s" % (self.domain_name))
199        out, err = self.pfw.sendCmd("addElement", str(self.domain_name), "Non_Defined_Element", expectSuccess=False)
200        assert err == None, "ERROR : command [addElement] - Error while adding new domain element %s" % (self.elem_2_path)
201        assert out != "Done", "ERROR : command [addElement] - Error not detected while adding a non defined domain element to domain %s" % (self.domain_name)
202        log.I("Error when adding elements correctly detected")
203
204        # Checking final domain elements
205        log.I("Listing final domain %s elements" % (self.domain_name))
206        out, err = self.pfw.sendCmd("listDomainElements",str(self.domain_name))
207        assert err == None, "ERROR : command [listDomainElements] - Error while listing domain elements"
208        f_DomainElements = open("f_DomainElements", "w")
209        f_DomainElements.write(out)
210        f_DomainElements.close()
211        log.I("command [listDomainElements] correctly executed")
212        f_DomainElements = open("f_DomainElements", "r")
213        element_nbr = 0
214        line=f_DomainElements.readline()
215        while line!="":
216            line=f_DomainElements.readline()
217            element_nbr+=1
218        f_DomainElements.close()
219        log.I("Actual domain %s elements number is %s" % (self.domain_name,element_nbr))
220        log.I("Checking domain %s elements names conformity" % (self.domain_name))
221        f_DomainElements = open("f_DomainElements", "r")
222        f_DomainElements_Backup = open("f_DomainElements_Backup", "r")
223        for line in range(element_nbr):
224            # initial domain elements shall not have been impacted by current test
225            element_name = f_DomainElements.readline().strip('\r\n')
226            element_name_backup = f_DomainElements_Backup.readline().strip('\r\n')
227            assert element_name==element_name_backup, "ERROR : domain %s elements affected by addElement errors" % (self.domain_name)
228        log.I("Actual domain %s elements names conform to expected values" % (self.domain_name))
229        # Temporary files deletion
230        f_DomainElements.close()
231        f_DomainElements_Backup.close()
232        os.remove("f_DomainElements_Backup")
233        os.remove("f_DomainElements")
234
235    def test_removeElement_Error(self):
236        """
237        Testing removeElement error
238        ---------------------------
239            Test case description :
240            ~~~~~~~~~~~~~~~~~~~~~~~
241                - remove a non defined domain element
242                - remove a domain element on a wrong domain name
243            Tested commands :
244            ~~~~~~~~~~~~~~~~~
245                - [removeElement] function
246                - [listDomainElements] function
247            Expected result :
248            ~~~~~~~~~~~~~~~~~
249                - Errors correctly detected
250                - No side effect
251        """
252        log.D(self.test_removeElement_Error.__doc__)
253
254        # List and backup initial domain elements
255        log.I("Listing initial domain %s elements" % (self.domain_name))
256        out, err = self.pfw.sendCmd("listDomainElements",str(self.domain_name))
257        assert err == None, "ERROR : command [listDomainElements] - Error while listing domain elements"
258        f_DomainElements_Backup = open("f_DomainElements_Backup", "w")
259        f_DomainElements_Backup.write(out)
260        f_DomainElements_Backup.close()
261        log.I("command [listDomainElements] correctly executed")
262        f_DomainElements_Backup = open("f_DomainElements_Backup", "r")
263        element_nbr_init = 0
264        line=f_DomainElements_Backup.readline()
265        while line!="":
266            line=f_DomainElements_Backup.readline()
267            element_nbr_init+=1
268        f_DomainElements_Backup.close()
269        log.I("Actual domain %s elements number is %s" % (self.domain_name,element_nbr_init))
270
271        # Error when removing domain elements
272        log.I("Removing a domain element from a non defined domain")
273        out, err = self.pfw.sendCmd("removeElement", "Wrong_Domain_Name", str(self.elem_0_path), expectSuccess=False)
274        assert err == None, "ERROR : command [removeElement] - Error when removing domain element %s" % (self.elem_0_path)
275        assert out != "Done", "ERROR : command [removeElement] - Error not detected when removing domain element %s from an undefined domain"% (self.elem_0_path)
276        log.I("Removing a non existent domain element from domain %s" % (self.domain_name))
277        out, err = self.pfw.sendCmd("removeElement", str(self.domain_name), "Wrong_Element_Name", expectSuccess=False)
278        assert err == None, "ERROR : command [removeElement] - Error when removing domain element %s" % (self.elem_0_path)
279        assert out != "Done", "ERROR : command [removeElement] - Error not detected when removing a non existent domain element from domain %s" % (self.domain_name)
280        log.I("Error when removing elements correctly detected")
281
282        # Checking final domain elements
283        log.I("Listing final domain %s elements" % (self.domain_name))
284        out, err = self.pfw.sendCmd("listDomainElements",str(self.domain_name))
285        assert err == None, "ERROR : command [listDomainElements] - Error while listing domain elements"
286        f_DomainElements = open("f_DomainElements", "w")
287        f_DomainElements.write(out)
288        f_DomainElements.close()
289        log.I("command [listDomainElements] correctly executed")
290        f_DomainElements = open("f_DomainElements", "r")
291        element_nbr = 0
292        line=f_DomainElements.readline()
293        while line!="":
294            line=f_DomainElements.readline()
295            element_nbr+=1
296        f_DomainElements.close()
297        log.I("Actual domain %s elements number is %s" % (self.domain_name,element_nbr))
298        log.I("Checking domain %s elements names conformity" % (self.domain_name))
299        f_DomainElements = open("f_DomainElements", "r")
300        f_DomainElements_Backup = open("f_DomainElements_Backup", "r")
301        for line in range(element_nbr):
302            # initial domain elements shall not have been impacted by current test
303            element_name = f_DomainElements.readline().strip('\r\n')
304            element_name_backup = f_DomainElements_Backup.readline().strip('\r\n')
305            assert element_name==element_name_backup, "ERROR : domain %s elements affected by addElement errors" % (self.domain_name)
306        log.I("Actual domain %s elements names conform to expected values" % (self.domain_name))
307        # Temporary files deletion
308        f_DomainElements.close()
309        f_DomainElements_Backup.close()
310        os.remove("f_DomainElements_Backup")
311        os.remove("f_DomainElements")
312