Make the config generation private method more generic 91/109091/2
authorBartek Grzybowski <b.grzybowski@partner.samsung.com>
Fri, 12 Jun 2020 08:45:42 +0000 (10:45 +0200)
committerMorgan Richomme <morgan.richomme@orange.com>
Mon, 15 Jun 2020 14:09:30 +0000 (14:09 +0000)
Change-Id: I2a0b9ef8eb304cfd088cd3a7dc4c61aaf54504c4
Issue-ID: INT-1621
Signed-off-by: Bartek Grzybowski <b.grzybowski@partner.samsung.com>
test/mocks/mass-pnf-sim/MassPnfSim.py

index fbeec1d..9801d8e 100755 (executable)
@@ -204,19 +204,18 @@ class MassPnfSim:
         with open(f'{self.sim_dirname_pattern}{i}/{self.sim_config}', 'w') as fout:
             fout.write(dump(yml))
 
-    def _generate_docker_compose_file(self, **kwargs):
-        '''Helper private method to generate the Docker Compose file
-        for a given simulator instance'''
+    def _generate_config_file(self, source, dest, **kwargs):
+        '''Helper private method to generate a file based on a template'''
         old_pwd = getcwd()
         chdir(self.sim_dirname_pattern + str(kwargs['I']))
-        # Read the docker compose template file
-        with open(self.sim_compose_template, 'r') as f:
+        # Read the template file
+        with open(source, 'r') as f:
             template = f.read()
         # Replace all occurences of env like variable with it's
         # relevant value from a corresponding key form kwargs
         for (k,v) in kwargs.items():
             template = template.replace('${' + k + '}', str(v))
-        with open('docker-compose.yml', 'w') as f:
+        with open(dest, 'w') as f:
             f.write(template)
         chdir(old_pwd)
 
@@ -278,14 +277,16 @@ class MassPnfSim:
             self.logger.info(f"\tCreating instance #{i} configuration ")
             self._run_cmd(composercmd, f"{self.sim_dirname_pattern}{i}")
             self._generate_pnf_sim_config(i, PortSftp, PortFtps, ip['PnfSim'])
-            self._generate_docker_compose_file(IPGW = ip['gw'], IPSUBNET = ip['subnet'],
-                                               I = i, IPPNFSIM = ip['PnfSim'],
-                                               PORTSFTP = str(PortSftp),
-                                               PORTFTPS = str(PortFtps),
-                                               IPFTPS = ip['ftps'], IPSFTP = ip['sftp'],
-                                               FTPS_PASV_MIN = str(ftps_pasv_port_start),
-                                               FTPS_PASV_MAX = str(ftps_pasv_port_end),
-                                               TIMEZONE = tzname[daylight])
+            # generate docker-compose for the simulator instance
+            self._generate_config_file(self.sim_compose_template, 'docker-compose.yml',
+                                       IPGW = ip['gw'], IPSUBNET = ip['subnet'],
+                                       I = i, IPPNFSIM = ip['PnfSim'],
+                                       PORTSFTP = str(PortSftp),
+                                       PORTFTPS = str(PortFtps),
+                                       IPFTPS = ip['ftps'], IPSFTP = ip['sftp'],
+                                       FTPS_PASV_MIN = str(ftps_pasv_port_start),
+                                       FTPS_PASV_MAX = str(ftps_pasv_port_end),
+                                       TIMEZONE = tzname[daylight])
 
             ftps_pasv_port_start += ftps_pasv_port_num_of_ports + 1
             ftps_pasv_port_end += ftps_pasv_port_num_of_ports + 1