RS UnitTest
RS UnitTest
anqiyouyou 发表于5个月前
RS UnitTest
  • 发表于 5个月前
  • 阅读 0
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 学生专属云服务套餐 10元起购>>>   

import os

import unittest

import tempfile

import unittest.mock as mock

from xml.dom.minidom import Document

from revitfarm.tests.basetest import BaseTest

from revitfarm.core.util.const import Const

from revitfarm.plugin.registry.autodesk.revit.regression.revitservertools import RevitServerTools

 

 

class revitservertools(BaseTest):

 

def __init__(self, *args, **kwargs):

super().__init__(*args, **kwargs)

self.logger = mock.MagicMock()

 

def setUp(self,):

with tempfile.TemporaryDirectory() as tmp_dir:

os.environ[Const.WORKER_APP_ROOT] = tmp_dir

local_job_package_root = os.path.join(tmp_dir, "GEN")

os.makedirs(local_job_package_root)

rs_onIIS_exe_path = os.path.join(local_job_package_root, "Regression", "_RegressionTools", "RSInstallUtils", "rsOnIIS.exe")

rs_path = os.path.join(local_job_package_root, "RevitServer", "Enterprise", "Releasex64")

rs_program = os.path.join(local_job_package_root, "Program")

version_id = "2018"

self.revitservertools = RevitServerTools(version_id, rs_onIIS_exe_path, rs_path, rs_program, logger=self.logger)

 

@mock.patch("revitfarm.plugin.registry.autodesk.revit.regression.revitservertools.gr")

def test_set_env(self, gr):

self.revitservertools.set_env("2018")

setEnv_cmd = '@reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v RSROLE' + '2018' + ' /t reg_expand_sz /d "Accelerator,Admin,Host" /f'

gr.run_simple_cmd.assert_called_once_with(setEnv_cmd, input=None, success_code=0, logger=self.logger)

 

@mock.patch("revitfarm.plugin.registry.autodesk.revit.regression.revitservertools.gr")

def test_rm_env(self, gr):

self.revitservertools.rm_env("2018")

rmEnv_cmd = '@reg delete "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v RSROLE' + '2018' + ' /f'

gr.run_simple_cmd.assert_called_once_with(rmEnv_cmd, input=None, success_code=0, logger=self.logger)

 

@mock.patch("revitfarm.plugin.registry.autodesk.revit.regression.revitservertools.gr")

def test_rsOnIIS_install(self, gr):

with tempfile.TemporaryDirectory() as tmp:

local_work_root = os.path.join(tmp, 'test_reg_job')

# rsOnIIS_exe_path = os.path.join(local_work_root, "Regression", "_RegressionTools", "RSInstallUtils", "rsOnIIS.exe")

rsOnIIS_exe_path = os.path.join(self.test_data_path, 'revitjob', 'test_revit_job.zip.rsOnIIS.exe')

rs_path = os.path.join(local_work_root, "RevitServer", "Enterprise", "Releasex64")

self.revitservertools.rsOnIIS_install('2018', rsOnIIS_exe_path, rs_path)

cmd_line = rsOnIIS_exe_path + " i=\"" + rs_path + "\" r=" + '2018' + " e"

gr.run_simple_cmd.assert_called_once_with(cmd_line, input=None, success_code=0, logger=self.logger)

 

@mock.patch("revitfarm.plugin.registry.autodesk.revit.regression.revitservertools.gr")

def test_rsOnIIS_uninstall(self, gr):

rsOnIIS_exe_path = os.path.join(self.test_data_path, 'revitjob', 'test_revit_job.zip.rsOnIIS.exe')

self.revitservertools.rsOnIIS_uninstall('2018', rsOnIIS_exe_path)

cmd_line = rsOnIIS_exe_path + " u"

gr.run_simple_cmd.assert_called_once_with(cmd_line, input=None, success_code=0, logger=self.logger)

 

def test_analyze_virtual_disk(self):

cmd = "R:\\APP\\RVT\\ECS\\pkg\\Regression\\RegressionTools\\RSInstallUtils\\rsOnIIS.exe u"

subst_result = ['R:\\: => C:\\Regfarm']

expect_result = 'C:\\Regfarm\\APP\RVT\\ECS\\pkg\\Regression\\RegressionTools\\RSInstallUtils\\rsOnIIS.exe u'

#self.revitservertools.analyze_virtual_disk(subst_result, cmd)

self.assertEqual(self.revitservertools.analyze_virtual_disk(subst_result, cmd), expect_result)

 

def test_find_word(self):

with tempfile.TemporaryDirectory() as tmp_dir:

rs_program = os.path.join(tmp_dir, "Program")

os.makedirs(rs_program)

debug_ini = os.path.join(rs_program,"Debug.ini")

word = "RegressionTestServerBasedworkflow=1"

with open(debug_ini, "w+") as file:

file.write("[DebugModes]\nRegressionTestServerBasedworkflow=1\n[DebugTests]\nRegressionTest=2")

self.assertEqual(self.revitservertools.find_word(debug_ini, word), True)

 

def test_find_word_location(self):

with tempfile.TemporaryDirectory() as tmp_dir:

rs_program = os.path.join(tmp_dir, "Program")

os.makedirs(rs_program)

debug_ini = os.path.join(rs_program, "Debug.ini")

with open(debug_ini, "w+") as file:

file.write("[DebugModes]\nRegressionTestServerBasedWorkflow=1\n[DebugTests]\nRegressionTest=2")

self.assertEqual(self.revitservertools.find_word_location(debug_ini, "[DebugModes]"), 0)

self.assertEqual(self.revitservertools.find_word_location(debug_ini, "[DebugTests]"), 49)

 

def test_insert_text(self):

with tempfile.TemporaryDirectory() as tmp_dir:

rs_program = os.path.join(tmp_dir, "Program")

os.makedirs(rs_program)

debug_ini = os.path.join(rs_program, "Debug.ini")

word = "RegressionTestServerBasedworkflow=1"

expect_word = "[DebugMode]\n"+'RegressionTestServerBasedworkflow=1'+"\n"

with open(debug_ini,"w+") as file:

file.write("[DebugMode]\n")

self.assertEqual(self.revitservertools.insert_text(debug_ini,12,word),expect_word)

 

def test_remove_text(self):

with tempfile.TemporaryDirectory() as tmp_dir:

rs_program = os.path.join(tmp_dir, "Program")

os.makedirs(rs_program)

debug_ini = os.path.join(rs_program, "Debug.ini")

word = "RegressionTestServerBasedworkflow=1"

expect = "[DebugModes]\n\n[DebugTests]\nRegressionTest=2"

with open(debug_ini, "w+") as file:

file.write("[DebugModes]\nRegressionTestServerBasedworkflow=1\n[DebugTests]\nRegressionTest=2")

self.assertEqual(self.revitservertools.remove_text(debug_ini, word), expect)

 

def test_check_create_rsn(self):

with tempfile.TemporaryDirectory() as tmp_dir:

local_work_root = os.path.join(tmp_dir, 'test_reg_job')

rs_program = os.path.join(tmp_dir, "Program")

os.makedirs(rs_program)

rsn_ini = os.path.join(rs_program, "RSN.ini")

self.revitservertools.check_create_rsn(rsn_ini)

self.assertEqual(os.path.exists(rsn_ini), True)

with open(rsn_ini, "r") as rsn:

all_text = rsn.read()

self.assertEqual(all_text, "localhost")

 

def test_check_create_debug(self):

with tempfile.TemporaryDirectory() as tmp_dir:

rs_program = os.path.join(tmp_dir,"Program")

os.makedirs(rs_program)

debug_ini = os.path.join(rs_program, "Debug.ini")

self.revitservertools.check_create_debug(debug_ini)

with open(debug_ini, "r") as f:

all_text = f.read()

expect_result = "RegressionTestServerBasedWorkflow=1" in all_text

self.assertTrue(expect_result)

 

def test_get_version_id(self):

with tempfile.TemporaryDirectory() as tmp_dir:

service_path = os.path.join(tmp_dir, "Enterprise", "Release64", "Service", "RS.Service.Protected.Model")

os.makedirs(service_path)

web_config = os.path.join(service_path, "web.config")

doc = Document()

configuration = doc.createElement("configuration")

doc.appendChild(configuration)

runtime = doc.createElement("runtime")

configuration.appendChild(runtime)

assemblyBinding = doc.createElement("assemblyBinding")

assemblyBinding.setAttribute("xmlns", "urn:schemas-microsoft-com:asm.v1")

runtime.appendChild(assemblyBinding)

dependentAssembly = doc.createElement("dependentAssembly")

assemblyBinding.appendChild(dependentAssembly)

assemblyIdentity = doc.createElement("assemblyIdentity")

assemblyIdentity.setAttribute("name", "System.Web.Extensions")

assemblyIdentity.setAttribute("publicKeyToken", "31bf3856ad364e35")

assemblyBinding.appendChild(assemblyIdentity)

system_serviceModel = doc.createElement("system.serviceModel")

configuration.appendChild(system_serviceModel)

diagnostics = doc.createElement("diagnostics")

diagnostics.setAttribute("performanceCounters", "Off")

system_serviceModel.appendChild(diagnostics)

client = doc.createElement("client")

diagnostics.appendChild(client)

endpoint1 = doc.createElement("endpoint")

endpoint1.setAttribute("address", "net.tcp://localhost/ModelService2018/Files.svc/netTcp")

endpoint1.setAttribute("binding", "TcpStreamed")

endpoint2 = doc.createElement("endpoint")

endpoint2.setAttribute("address", "net.tcp://localhost/ModelService2018/Files.svc/upload")

endpoint2.setAttribute("binding", "TcpStreamed")

endpoint3 = doc.createElement("endpoint")

endpoint3.setAttribute("address", "net.tcp://localhost/ModelService2018/Files.svc/download")

endpoint3.setAttribute("binding", "TcpStreamed")

client.appendChild(endpoint1)

client.appendChild(endpoint2)

client.appendChild(endpoint3)

bindings = doc.createElement("bindings")

system_serviceModel.appendChild(bindings)

netTcpBinding = doc.createElement("netTcpBinding")

bindings.appendChild(netTcpBinding)

with open(web_config, "w+") as file:

file.write(doc.toprettyxml(indent=" "))

result = self.revitservertools.get_version_id(web_config)

self.assertEqual(result, "2018")

 

if __name__ == "__main__":

unittest.main()

共有 人打赏支持
粉丝 0
博文 3
码字总数 1474
×
anqiyouyou
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: