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"""
32Creation, renaming and deletion configuration testcases
33
34List of tested functions :
35--------------------------
36    - [createDomain]  function
37    - [deleteDomain] function
38
39Test cases :
40------------
41    - Testing nominal cases
42    - Testing domain creation error
43    - Testing domain deletion error
44"""
45import os
46from Util.PfwUnitTestLib import PfwTestCase
47from Util import ACTLogging
48log=ACTLogging.Logger()
49
50# Test of Domains - Basic operations (creations/deletions)
51class TestCases(PfwTestCase):
52    def setUp(self):
53        self.pfw.sendCmd("setTuningMode", "on")
54        self.new_domains_number = 4
55        self.new_domain_name = "Domain"
56
57    def tearDown(self):
58        self.pfw.sendCmd("setTuningMode", "off")
59
60    def test_Domain_Creation_Error(self):
61        """
62        Testing domain creation error
63        -----------------------------
64            Test case description :
65            ~~~~~~~~~~~~~~~~~~~~~~~
66                - Create an already existent domain
67            Tested commands :
68            ~~~~~~~~~~~~~~~~~
69                - [createDomain] function
70                - [listDomains] function
71            Expected result :
72            ~~~~~~~~~~~~~~~~~
73                - Error detected when creating an already existent domain
74                - No domains list update
75        """
76        log.D(self.test_Domain_Creation_Error.__doc__)
77        # New domain creation
78        log.I("New domain creation")
79        log.I("command [createDomain]")
80        domain_name = 'Test_Domain'
81        out, err = self.pfw.sendCmd("createDomain",domain_name, "")
82        assert out == "Done", out
83        assert err == None, "ERROR : command [createDomain] - Error while creating domain %s" % (domain_name)
84        log.I("command [createDomain] correctly executed")
85
86        # Domains listing using "listDomains" command
87        log.I("Current domains listing")
88        log.I("command [listDomains]")
89        out, err = self.pfw.sendCmd("listDomains","","")
90        assert err == None, "ERROR : command [listDomains] - Error while listing domains"
91        log.I("command [listDomains] - correctly executed")
92
93        # Domains listing backup
94        f_Domains_Backup = open("f_Domains_Backup", "w")
95        f_Domains_Backup.write(out)
96        f_Domains_Backup.close()
97        f_Domains_Backup = open("f_Domains_Backup", "r")
98        domains_nbr_init = 0
99        line=f_Domains_Backup.readline()
100        while line!="":
101            line=f_Domains_Backup.readline()
102            domains_nbr_init+=1
103        f_Domains_Backup.close()
104        log.I("Actual domains number : %s" % domains_nbr_init)
105
106        # Trying to add an existent domain name
107        log.I("Adding an already existent domain name")
108        log.I("command [createDomain]")
109        domain_name = 'Test_Domain'
110        out, err = self.pfw.sendCmd("createDomain",domain_name, "", expectSuccess=False)
111        assert out != "Done", "ERROR : command [createDomain] - Error not detected when creating an already existent domain"
112        assert err == None, err
113        log.I("command [createDomain] - error correctly detected")
114
115        # Checking domains list integrity
116        log.I("Checking domains listing integrity after domain creation error")
117        ## Domains listing using "listDomains" command
118        out, err = self.pfw.sendCmd("listDomains","","")
119        assert err == None, "ERROR : command [listDomains] - Error while listing domains"
120        f_Domains = open("f_Domains", "w")
121        f_Domains.write(out)
122        f_Domains.close()
123        ## Domains listing integrity check
124        f_Domains = open("f_Domains", "r")
125        domains_nbr = 0
126        line=f_Domains.readline()
127        while line!="":
128            line=f_Domains.readline()
129            domains_nbr+=1
130        f_Domains.close()
131        assert domains_nbr == domains_nbr_init, "ERROR : Domains number error, expected %s, found %s" % (domains_nbr_init,domains_nbr)
132        log.I("Test OK - Domains number not updated")
133        f_Domains = open("f_Domains", "r")
134        f_Domains_Backup = open("f_Domains_Backup", "r")
135        for line in range(domains_nbr):
136            domain_backup_name = f_Domains_Backup.readline().strip('\r\n'),
137            domain_name = f_Domains.readline().strip('\r\n'),
138            assert domain_backup_name==domain_name, "ERROR : Error while reading domain %s" % (domain_backup_name)
139        log.I("Test OK - Domains listing not affected by domain creation error")
140
141        # Closing and deleting temp files
142        f_Domains_Backup.close()
143        f_Domains.close()
144        os.remove("f_Domains_Backup")
145        os.remove("f_Domains")
146
147    def test_Domain_Deletion_Error(self):
148        """
149        Testing domain deletion error
150        -----------------------------
151            Test case description :
152            ~~~~~~~~~~~~~~~~~~~~~~~
153                - Delete a non existent domain
154            Tested commands :
155            ~~~~~~~~~~~~~~~~~
156                - [deleteDomain] function
157                - [listDomains] function
158            Expected result :
159            ~~~~~~~~~~~~~~~~~
160                - Error detected when deleting a non-existent domain
161                - No domains list update
162        """
163        log.D(self.test_Domain_Deletion_Error.__doc__)
164        # Domains listing using "listDomains" command
165        log.I("Current domains listing")
166        log.I("command [listDomains]")
167        out, err = self.pfw.sendCmd("listDomains","","")
168        assert err == None, "ERROR : command [listDomains] - Error while listing domains"
169        log.I("command [listDomains] correctly executed")
170
171        # Domains listing backup
172        f_Domains_Backup = open("f_Domains_Backup", "w")
173        f_Domains_Backup.write(out)
174        f_Domains_Backup.close()
175        f_Domains_Backup = open("f_Domains_Backup", "r")
176        domains_nbr_init = 0
177        line=f_Domains_Backup.readline()
178        while line!="":
179            line=f_Domains_Backup.readline()
180            domains_nbr_init+=1
181        f_Domains_Backup.close()
182        log.I("Actual domains number : %s" % domains_nbr_init)
183
184        # Trying to delete a non-existent domain name
185        log.I("Deleting a non-existent domain name")
186        log.I("command [deleteDomain]")
187        domain_name = 'Wrong_Domain_Name'
188        out, err = self.pfw.sendCmd("deleteDomain",domain_name, "", expectSuccess=False)
189        assert out != "Done", "ERROR : command [deleteDomain] - Error not detected when deleting a non-existent domain"
190        assert err == None, err
191        log.I("command [deleteDomain] - error correctly detected")
192
193        # Checking domains list integrity
194        log.I("Checking domains listing integrity after domain deletion error")
195        ## Domains listing using "listDomains" command
196        out, err = self.pfw.sendCmd("listDomains","","")
197        assert err == None, "ERROR : command [listDomains] - Error while listing domains"
198        f_Domains = open("f_Domains", "w")
199        f_Domains.write(out)
200        f_Domains.close()
201        ## Domains listing integrity check
202        f_Domains = open("f_Domains", "r")
203        domains_nbr = 0
204        line=f_Domains.readline()
205        while line!="":
206            line=f_Domains.readline()
207            domains_nbr+=1
208        f_Domains.close()
209        assert domains_nbr == domains_nbr_init, "ERROR : Domains number error, expected %s, found %s" % (domains_nbr_init,domains_nbr)
210        log.I("Test OK - Domains number not updated")
211        f_Domains = open("f_Domains", "r")
212        f_Domains_Backup = open("f_Domains_Backup", "r")
213        for line in range(domains_nbr):
214            domain_backup_name = f_Domains_Backup.readline().strip('\r\n'),
215            domain_name = f_Domains.readline().strip('\r\n'),
216            assert domain_backup_name==domain_name, "Error while reading domain %s" % (domain_backup_name)
217        log.I("Test OK - Domains listing not affected by domain deletion error")
218
219        # Closing and deleting temp files
220        f_Domains_Backup.close()
221        f_Domains.close()
222        os.remove("f_Domains_Backup")
223        os.remove("f_Domains")
224
225    def test_Nominal_Case(self):
226        """
227        Testing nominal cases
228        ---------------------
229            Test case description :
230            ~~~~~~~~~~~~~~~~~~~~~~~
231                - Create X new domains
232                - Delete X domains
233            Tested commands :
234            ~~~~~~~~~~~~~~~~~
235                - [createDomain] function
236                - [deleteDomain] function
237                - [listDomains] function
238            Expected result :
239            ~~~~~~~~~~~~~~~~~
240                - X new domains created
241                - X domains deleted
242        """
243        log.D(self.test_Nominal_Case.__doc__)
244        # Initial domains listing using "listDomains" command
245        log.I("Initial domains listing")
246        log.I("command [listDomains]")
247        out, err = self.pfw.sendCmd("listDomains","","")
248        assert err == None, "ERROR : command [listDomains] - Error while listing domains"
249        log.I("command [listDomains] correctly executed")
250
251        # Initial domains number count
252        f_init_domains = open("f_init_domains", "w")
253        f_init_domains.write(out)
254        f_init_domains.close()
255        init_domains_nbr = 0
256        f_init_domains = open("f_init_domains", "r")
257        line=f_init_domains.readline()
258        while line!="":
259            line=f_init_domains.readline()
260            init_domains_nbr+=1
261        f_init_domains.close()
262        log.I("Initial domains number : %s" % (init_domains_nbr))
263
264        # New domains creation
265        log.I("New domains creation")
266        log.I("PFW command : [createDomain]")
267        for index in range (self.new_domains_number):
268            domain_name = "".join([self.new_domain_name, "_", str(index+init_domains_nbr)])
269            out, err = self.pfw.sendCmd("createDomain",domain_name, "")
270            assert out == "Done", "ERROR : %s" % (out)
271            assert err == None, "ERROR : command [createDomain] - Error while creating domain %s" % (domain_name)
272        log.I("command [createDomain] correctly executed")
273
274        # New domain creation check
275        log.I("New domains creation check :")
276        log.I("command [listDomains]")
277        out, err = self.pfw.sendCmd("listDomains","","")
278        assert err == None, "ERROR : command [listDomains] - Error while listing new domains"
279        log.I("command [listDomains] correctly executed")
280
281        # Working on a temporary files to record domains listing
282        tempfile = open("tempfile", "w")
283        tempfile.write(out)
284        tempfile.close()
285
286        # Checking last added entries in the listing
287        tempfile = open("tempfile", "r")
288        domains_nbr = 0
289        line=tempfile.readline()
290        while line!="":
291            line=tempfile.readline()
292            domains_nbr+=1
293        tempfile.close()
294        log.I("New domains conformity check")
295        tempfile = open("tempfile", "r")
296        for line in range(domains_nbr):
297            if (line >= (domains_nbr - self.new_domains_number)):
298                domain_name = "".join([self.new_domain_name,"_",str(line)]),
299                domain_created = tempfile.readline().strip('\n\r'),
300                assert domain_name==domain_created, "ERROR : Error while creating domain %s %s" % (domain_created, domain_name)
301            else:
302                domain_created = tempfile.readline()
303        log.I("New domains conform to expected values")
304        created_domains_number = domains_nbr - init_domains_nbr
305        log.I("%s new domains created" % created_domains_number)
306        tempfile.close()
307        os.remove("tempfile")
308
309        # New domains deletion
310        log.I("New domains deletion")
311        log.I("command [deleteDomain]")
312        for index in range (self.new_domains_number):
313            domain_name = "".join([self.new_domain_name, "_", str(index+init_domains_nbr)])
314            out, err = self.pfw.sendCmd("deleteDomain",domain_name, "")
315            assert out == "Done", "ERROR : %s" % (out)
316            assert err == None, "ERROR : command [deleteDomain] - Error while deleting domain %s" % (domain_name)
317        log.I("command [deleteDomain] correctly executed")
318
319        # New domains deletion check
320        f_init_domains = open("f_init_domains", "r")
321        tempfile = open("tempfile", "w")
322        log.I("New domains deletion check :")
323        log.I("command [listDomains]")
324        out, err = self.pfw.sendCmd("listDomains","","")
325        assert err == None, "ERROR : command [listDomains] - Error while listing domains"
326        log.I("command [listDomains] correctly executed")
327        tempfile.write(out)
328        tempfile.close()
329        tempfile = open("tempfile", "r")
330        line=tempfile.readline()
331        line_init=f_init_domains.readline()
332        while line!="":
333            line=tempfile.readline()
334            line_init=f_init_domains.readline()
335            assert line == line_init, "ERROR : Domain deletion error"
336        if line=="":
337            assert line_init == "", "ERROR : Wrong domains deletion number"
338            log.I("Deletion completed - %s domains deleted" % created_domains_number)
339
340        # Temporary files deletion
341        tempfile.close()
342        f_init_domains.close()
343        os.remove("tempfile")
344        os.remove("f_init_domains")
345