Skip to content
Snippets Groups Projects
Commit e4dd5f73 authored by Nabywaniec's avatar Nabywaniec
Browse files

Update tests

parent 70b063ae
No related branches found
No related tags found
No related merge requests found
<?xml version="1.0" encoding="UTF-8"?>
<SILECS-Deploy
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
silecs-version="2.3.0" created="08/16/2022" updated="08/16/2022"
xsi:noNamespaceSchemaLocation="/common/usr/cscofe/silecs/2.3.0/silecs-model/xml/DeploySchema.xsd">
<Information>
<Owner user-login="mnabywan" />
<Editor user-login="mnabywan" />
</Information>
<Deploy-Unit name="SilecsTestClassDU" version="0.1.0"></Deploy-Unit>
<SilecsDesign silecs-design-name="SilecsTestClass"
silecs-design-version="0.1.0" />
<Controller host-name="tsts7001">
<Siemens-PLC system="TIA-PORTAL" model="SIMATIC_S7-1500"
protocol="DEVICE_MODE" base-DB-number="1">
<Device silecs-device-label="dev0"
silecs-design-ref="SilecsTestClass" fesa-device-name="mySilecsTest"
fesa-fec-name="asl751"></Device>
</Siemens-PLCerror>
</Controller>
</SILECS-Deploy>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?><deploy-unit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="file:/opt/fesa/fesa-model-gsi/7.4.0/xml/deployment/deployment-gsi.xsd">
<information>
<deploy-unit-name>SilecsTestClass2_DU</deploy-unit-name>
<deploy-unit-major-version>0</deploy-unit-major-version>
<deploy-unit-minor-version>1</deploy-unit-minor-version>
<deploy-unit-tiny-version>0</deploy-unit-tiny-version>
<description>Empty Template description</description>
<fesa-version>7.4.0</fesa-version>
</information>
<ownership>
<responsible name="Undefined"/>
<creator login="mnabywan"/>
</ownership>
<class>
<class-name>class-name</class-name>
<class-major-version>0</class-major-version>
<class-minor-version>1</class-minor-version>
<class-tiny-version>0</class-tiny-version>
<device-instance>required</device-instance>
</class>
<executable>
<server extension="_S"/>
</executable>
</deploy-unit>
import os
import sys
import sys
import unittest
sys.path.append('../')
from silecs_cli import silecs
filenames_extensions = [("test1.design", "design"), ("test2.deploy", "deploy")]
filenames_project_names = [("test1.design", "test1"), ("test2.deploy", "test2")]
filenames_dirs = [("/home/test/example/test1.design", "/home/test/example"), ("/home/test/example/test2.deploy", "/home/test/example")]
filenames_schema_paths = [("/home/test/example/test1.silecsdesign", "../silecs-model/src/xml/DesignSchema.xsd"), ("/home/test/example/test2.silecsdeploy", "../silecs-model/src/xml/DeploySchema.xsd")]
filenames_extensions = [("test1.design", "design"), ("test2.deploy", "deploy")]
filenames_project_names = [("test1.design", "test1"), ("test2.deploy", "test2")]
filenames_dirs = [("/home/test/example/test1.design", "/home/test/example"),
("/home/test/example/test2.deploy", "/home/test/example")]
filenames_schema_paths = [("/home/test/example/test1.silecsdesign",
"../silecs-model/src/xml/DesignSchema.xsd"),
("/home/test/example/test2.silecsdeploy",
"../silecs-model/src/xml/DeploySchema.xsd")]
BASE_FOR_BACKUP = os.path.join("tests", "examples", "SilecsTestClass.design")
silecs_design_deploy_paths = [os.path.join("tests", "examples", "SilecsTestClass.silecsdesign"), \
os.path.join("tests", "examples", "SilecsTestClassDU.silecsdeploy")]
silecs_design_deplov_paths = [os.path.join("tests", "examples", "SilecsTestClass.silecsdesign"), \
os.path.join("tests", "examples", "SilecsTestClassDU.silecsdeploy")]
invalid_silecs_deploy_path = os.path.join("tests", "examples", "SilecsTestClassDU_invalid.silecsdeploy")
BACKUP = BASE_FOR_BACKUP + ".backup"
DESIGN_PATH = os.path.join("tests", "examples", "SilecsTestClass2.design")
SILECS_DESIGN_PATH = os.path.join("tests", "examples", "SilecsTestClass2.silecsdesign")
DEPLOY_PATH = os.path.join("tests", "examples", "SilecsTestClass_2_DU.deploy")
SILECS_DESIGN_PATH = DESIGN_PATH.split(".")[0] + ".silecsdesign"
SILECS_DEPLOY_PATH = DEPLOY_PATH.split(".")[0] + ".silecsdeploy"
class SilecsTest(unittest.TestCase):
"""Class to test silecs.py script"""
@classmethod
def tearDownClass(cls):
"""Remove created files during tests"""
if os.path.isfile(BACKUP):
os.remove(BACKUP)
os.remove(BACKUP)
if os.path.isfile(SILECS_DESIGN_PATH):
os.remove(SILECS_DESIGN_PATH)
os.remove(SILECS_DESIGN_PATH)
def test_get_extension(self):
if os.path.isfile(SILECS_DEPLOY_PATH):
os.remove(SILECS_DEPLOY_PATH)
def test_get_extension(self):
"""Test getting extension from file"""
for filename, expected in filenames_extensions:
with self.subTest():
self.assertEqual(silecs.get_extension(filename), expected)
def test_get_project_name(self):
def test_get_project_name(self):
"""Test getting extension from filepath"""
for filename, project_name in filenames_project_names:
with self.subTest():
self.assertEqual(silecs.get_project_name(filename), project_name)
def test_get_project_dir(self):
def test_get_project_dir(self):
"""Test getting project directory from filepath"""
for filename, directory in filenames_dirs:
with self.subTest():
self.assertEqual(silecs.get_project_dir(filename), directory)
def test_get_schema_path(self):
"""Test getting xml schema path for silecs design deploy files"""
for filename, schema_path in filenames_schema_paths:
with self.subTest():
self.assertEqual(silecs.get_schema_path(filename), schema_path)
def test_get_schema_path_invalid(self):
"""Test getting xml schema path for invalid path (passed file path
must have the extension '.silecsdesign' or '.silecsdeploy')"""
try:
silecs.get_schema_path("/home/test/example/test1.design")
self.assertFail()
except Exception as e:
self.assertEqual("Error: Passed file for validation needs to have the extension '.silecsdesign' or '.silecsdeploy'", str(e))
def test_get_silecs_new_file_path(self):
self.assertEqual(silecs.get_silecs_new_file_path("../test1.design"), os.path.join("..", "test1.silecsdesign"))
def test_get_silecs_file_path_from_fesa(self):
"""Test getting xml schema path for silecs design deploy files"""
self.assertEqual(silecs.get_silecs_file_path_from_fesa("../test1.design"), os.path.join("..", "test1.silecsdesign"))
def test_get_silecs_new_file_path_invalid(self):
def test_get_silecs_file_path_from_fesa_invalid(self):
"""Test getting xml schema path for invalid file (passed file path
must have the extension '.design' or '.deploy')
"""
try:
silecs.get_silecs_new_file_path("../test1.silecsdesign")
self.assertFail()
silecs.get_silecs_file_path_from_fesa("../test1.silecsdesign")
except Exception as e:
self.assertEqual("Error: Passed FESA file needs to have the extension '.design' or '.deploy'", str(e))
def test_create_backup_file(self):
silecs.create_backup_file(BASE_FOR_BACKUP)
"""Test creating backup file"""
silecs.create_backup_file(BASE_FOR_BACKUP)
self.assertNotEqual(os.stat(BACKUP).st_size, 0)
def test_silecs_validate(self):
"""Test validating silecs design/deploy files"""
for path in silecs_design_deploy_paths:
xsd_path = silecs.get_schema_path(path)
self.assertEqual(silecs.validate(path, xsd_path), True)
def test_silecs_validate_invalid(self):
"""Test validating invalid silecs file"""
xsd_path = silecs.get_schema_path(invalid_silecs_deploy_path)
self.assertEqual(silecs.validate(invalid_silecs_deploy_path, xsd_path), False)
def test_create_silecs_design(self):
"""Test creating silecs design files from design file"""
path = os.path.abspath(DESIGN_PATH)
new_file_path = silecs.get_silecs_new_file_path(path)
new_file_path = silecs.get_silecs_file_path_from_fesa(path)
silecs.create(new_file_path)
print(new_file_path)
self.assertEqual(os.path.exists(new_file_path), True)
self.assertNotEqual(os.stat(new_file_path).st_size, 0)
try:
silecs.create(new_file_path)
except Exception as e:
print(str(e))
self.assertIn("There is already a .silecsdesign file available:", str(e))
def test_create_silecs_deploy(self):
"""Test creating silecs deploy files from deploy file"""
path = os.path.abspath(DEPLOY_PATH)
new_file_path = silecs.get_silecs_file_path_from_fesa(path)
silecs.create(new_file_path)
print(new_file_path)
self.assertEqual(os.path.exists(new_file_path), True)
self.assertNotEqual(os.stat(new_file_path).st_size, 0)
try:
silecs.create(new_file_path)
except Exception as e:
print(str(e))
self.assertIn("There is already a .silecsdeploy file available:", str(e))
if __name__ == "_main__":
unittest.main()
\ No newline at end of file
unittest.main()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment