提交 96d5837f 编写于 作者: aaronchen2k2k's avatar aaronchen2k2k

plugin communication sample

上级 570d8488
#!/usr/bin/python
# -*- coding: utf-8 -*-
'''
title=Create For Approval (Requester)
timeout=0
cid=5016
pid=0
Click on "New Submission" > "For Approval" >> "New Submission - For Approval" page is displayed
Fill in the fields in metadata, Title, Priority, Classification and CC >>
Metadata default:
- Requester (current user)
- Designation (list user's current designation)
>>
'''
print('''"New Submission - For Approval" page is displayed''')
print('>>')
print('''Metadata default:\n- Requester (current user)\n- Designation (list user's current designation)''')
print('>>')
\ No newline at end of file
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
'''
title=测试返回结果
cid=1
- 返回 one @one
- 返回 two @two
- 返回 three @three
'''
# from selenium import webdriver
# from selenium.webdriver.support.ui import WebDriverWait
# from selenium.webdriver.support import expected_conditions as EC
# from selenium.webdriver.common.by import By
# import sys,io,platform
# import method
#启动浏览器
# driver = method.startChrome()
#登录系统
# returnstr = method.login(driver,'https://partner.dullestest.cn/#/','yy','123456')
# returnstr = method.login(driver,'http://localhost:86/#/',"yy","123456")
#step1
print('one')
#打开菜单
# method.openMenu(driver,"基础物料管理-基础物料维护")
#step2
print('two')
#step3
print('three')
#进行查询测试
# queryStr = "materialName=问问,materialSpecs=1*1*1,materialCode=1*1*1"
# returnStatus = method.query(driver,queryStr)
# if(returnStatus):
# print('yes')
# else:
# print('no')
\ No newline at end of file
......@@ -36,6 +36,11 @@ const (
PathInfo = "PATH_INFO"
Get = "GET"
PthSep = string(os.PathSeparator)
PluginDir = "plugin"
DownloadDir = "download"
BinDir = "bin"
ZapDownloadUrl = "https://dl.cnezsoft.com/ztf/plugin/zap/zap-%s.zip"
)
var (
......
package commDomain
type PluginInstallReq struct {
Name string `json:"name"`
Version string `json:"version"`
}
type PluginStartReq struct {
}
type ZapScanReq struct {
Session string `json:"session"`
}
......@@ -38,7 +38,9 @@ func LoadByWorkspacePath(workspacePath string) (config commDomain.WorkspaceConf)
GetInterpreterConfig(&config)
return
}
pth := filepath.Join(commConsts.ConfigPath, commConsts.ConfigFile)
pth := filepath.Join(workspacePath, "conf", commConsts.ConfigFile)
return LoadByConfigPath(pth)
}
......
......@@ -2,6 +2,7 @@ package controller
import (
commConsts "github.com/easysoft/zentaoatf/internal/pkg/consts"
commDomain "github.com/easysoft/zentaoatf/internal/pkg/domain"
"github.com/easysoft/zentaoatf/internal/server/modules/v1/service"
"github.com/kataras/iris/v12"
)
......@@ -15,6 +16,26 @@ func NewPluginCtrl() *PluginCtrl {
return &PluginCtrl{}
}
func (c *PluginCtrl) Exec(ctx iris.Context) {
err := c.PluginService.Exec()
if err != nil {
ctx.JSON(c.ErrResp(commConsts.CommErr, err.Error()))
return
}
ctx.JSON(c.SuccessResp(nil))
}
func (c *PluginCtrl) Cancel(ctx iris.Context) {
err := c.PluginService.Cancel()
if err != nil {
ctx.JSON(c.ErrResp(commConsts.CommErr, err.Error()))
return
}
ctx.JSON(c.SuccessResp(nil))
}
func (c *PluginCtrl) Start(ctx iris.Context) {
err := c.PluginService.Start()
if err != nil {
......@@ -36,7 +57,13 @@ func (c *PluginCtrl) Stop(ctx iris.Context) {
}
func (c *PluginCtrl) Install(ctx iris.Context) {
err := c.PluginService.Install()
req := commDomain.PluginInstallReq{}
err := ctx.ReadJSON(&req)
if err != nil {
ctx.JSON(c.ErrResp(commConsts.CommErr, err.Error()))
}
err = c.PluginService.Install(req)
if err != nil {
ctx.JSON(c.ErrResp(commConsts.CommErr, err.Error()))
return
......
......@@ -20,11 +20,14 @@ func (m *PluginModule) Party() module.WebModule {
handler := func(index iris.Party) {
index.Use(middleware.InitCheck())
index.Get("/install", m.PluginCtrl.Install).Name = "安装插件"
index.Get("/uninstall", m.PluginCtrl.Uninstall).Name = "卸载插件"
index.Post("/install", m.PluginCtrl.Install).Name = "安装插件"
index.Post("/uninstall", m.PluginCtrl.Uninstall).Name = "卸载插件"
index.Get("/start", m.PluginCtrl.Start).Name = "启动插件"
index.Get("/stop", m.PluginCtrl.Stop).Name = "停止插件"
index.Post("/start", m.PluginCtrl.Start).Name = "启动插件"
index.Post("/stop", m.PluginCtrl.Stop).Name = "停止插件"
index.Post("/exec", m.PluginCtrl.Exec).Name = "执行扫描"
index.Post("/cancel", m.PluginCtrl.Cancel).Name = "取消扫描"
}
return module.NewModule("/plugins", handler)
......
package service
import (
"errors"
"fmt"
commConsts "github.com/easysoft/zentaoatf/internal/pkg/consts"
commDomain "github.com/easysoft/zentaoatf/internal/pkg/domain"
zapPlugin "github.com/easysoft/zentaoatf/internal/pkg/plugin/zap/plugin"
zapService "github.com/easysoft/zentaoatf/internal/pkg/plugin/zap/service"
"github.com/easysoft/zentaoatf/internal/pkg/plugin/zap/shared"
fileUtils "github.com/easysoft/zentaoatf/pkg/lib/file"
shellUtils "github.com/easysoft/zentaoatf/pkg/lib/shell"
"github.com/hashicorp/go-plugin"
"path/filepath"
"strings"
)
const (
......@@ -18,6 +24,14 @@ type PluginService struct {
zapRpcClient plugin.ClientProtocol
}
func (s *PluginService) Exec() (err error) {
return
}
func (s *PluginService) Cancel() (err error) {
return
}
func (s *PluginService) Start() (err error) {
s.zapClient = plugin.NewClient(&plugin.ClientConfig{
Plugins: map[string]plugin.Plugin{
......@@ -62,7 +76,35 @@ func (s *PluginService) Stop() (err error) {
return
}
func (s *PluginService) Install() (err error) {
func (s *PluginService) Install(req commDomain.PluginInstallReq) (err error) {
name := req.Name
version := req.Version
if version == "" {
version = "latest"
}
url := fmt.Sprintf(commConsts.ZapDownloadUrl, version)
zipPath, extractDir := s.getPluginDownloadPath(name, version)
err = fileUtils.Download(url, zipPath)
if err != nil {
return
}
err = fileUtils.Download(url+".md5", zipPath+".md5")
if err != nil {
return
}
success := s.checkMd5(zipPath)
if !success {
err = errors.New("md5 check fail")
return
}
fileUtils.Unzip(zipPath, extractDir)
return
}
......@@ -71,3 +113,25 @@ func (s *PluginService) Uninstall() (err error) {
return
}
func (s *PluginService) getPluginDownloadPath(name, version string) (zipPath, binDir string) {
dir := filepath.Join(commConsts.WorkDir, commConsts.PluginDir, name)
zipPath = filepath.Join(dir, commConsts.DownloadDir, fmt.Sprintf("%s-%s.zip", name, version))
binDir = filepath.Join(dir, commConsts.BinDir)
return
}
func (s *PluginService) checkMd5(filePth string) (pass bool) {
md5Path := filePth + ".md5"
if !fileUtils.FileExist(filePth) {
return false
}
expectVal := fileUtils.ReadFile(md5Path)
actualVal, _ := fileUtils.GetMd5(filePth)
return strings.TrimSpace(actualVal) == strings.TrimSpace(expectVal)
}
package fileUtils
import (
commonUtils "github.com/easysoft/zentaoatf/pkg/lib/common"
shellUtils "github.com/easysoft/zentaoatf/pkg/lib/shell"
"strings"
)
func GetMd5(pth string) (ret string, err error) {
cmdStr := ""
if commonUtils.IsWin() {
cmdStr = "CertUtil -hashfile " + pth + " MD5"
} else {
cmdStr = "md5sum " + pth + " | awk '{print $1}'"
}
ret, _ = shellUtils.ExeSysCmd(cmdStr)
if commonUtils.IsWin() {
arr := strings.Split(ret, "\n")
if len(arr) > 1 {
ret = arr[1]
}
}
ret = strings.TrimSpace(ret)
return
}
......@@ -37,6 +37,8 @@ func ZipFiles(dist string, dir string, files []string) error {
}
func Unzip(zipPath, dstDir string) error {
MkDirIfNeeded(filepath.Dir(dstDir))
// open zip file
reader, err := zip.OpenReader(zipPath)
if err != nil {
......
......@@ -23,8 +23,10 @@ func Run(version string, codeDir string) (err error) {
}
// cmd := exec.Command("docker", "run", "--name", "zentao"+versionNumber, "-p", "8081:80", "-v", codeDir+":/www/zentaopms", "-d", "easysoft/zentao:"+version)
cmd := exec.Command("docker", "run", "--name", "zentao"+versionNumber, "-p", "8081:80", "-d", "easysoft/zentao:"+version)
cmd := exec.Command("docker", "run", "--name", "zentao"+versionNumber, "-p",
fmt.Sprintf("%d:80", constTestHelper.ZentaoPort), "-d", "easysoft/zentao:"+version)
fmt.Println(cmd.String())
output, err := cmd.CombinedOutput()
if err != nil {
return
......@@ -44,7 +46,7 @@ func IsExistContainer(name string) bool {
return strings.Contains(string(output), name)
}
func IsRuning(name string) bool {
func IsRunning(name string) bool {
cmd := exec.Command("docker", "ps", "--format", "'{{.Names}}'")
output, err := cmd.CombinedOutput()
if err != nil {
......@@ -104,11 +106,13 @@ func InitZentao(version string) (err error) {
isExist := IsExistContainer(containerName)
apath, _ := os.Getwd()
codeDir := apath + "/docker/www/zentao" + versionNumber
if runtime.GOOS == "windows" {
codeDir = apath + `\docker\www\zentao` + versionNumber
}
if isExist {
if !IsRuning(containerName) {
if !IsRunning(containerName) {
StopAll()
Start(containerName)
waitZentaoAccessed()
......
......@@ -38,31 +38,7 @@ func TestCli() (err error) {
if report.Fail > 0 {
os.Exit(1)
}
return
}
func TestUi() (err error) {
var screenshotPath = fmt.Sprintf("%stest/screenshot", constTestHelper.RootPath)
os.RemoveAll(screenshotPath)
fileUtils.MkDirIfNeeded(screenshotPath)
testPath := fmt.Sprintf(`%stest`, constTestHelper.RootPath)
if runtime.GOOS == "windows" {
testPath = fmt.Sprintf(`%stest`, constTestHelper.RootPath)
}
req := serverDomain.TestSet{
WorkspacePath: testPath,
Cmd: "go test ./ui -v -timeout 10m",
TestTool: commConsts.GoTest,
}
report := ExecUnit(req, "ui")
config := commDomain.WorkspaceConf{Url: constTestHelper.ZentaoSiteUrl + "/", Password: "Test123456.", Username: "admin"}
err = zentaoHelper.CommitResult(report, 1, 0, config, nil)
if report.Fail > 0 {
os.Exit(1)
}
return
}
......@@ -74,10 +50,14 @@ func ExecUnit(req serverDomain.TestSet, unitType string) (report commDomain.ZtfR
}
pth := filepath.Join(req.WorkspacePath, commConsts.AllureReportDir)
fileUtils.RmDir(pth)
startTime := time.Now()
// run
execHelper.RunUnitTest(nil, req.Cmd, req.WorkspacePath, nil)
entTime := time.Now()
// gen report
req.ResultDir = commConsts.AllureReportDir
req.ZipDir = req.ResultDir
......
package constTestHelper
import (
"fmt"
"os"
"runtime"
"strings"
......@@ -11,10 +12,13 @@ const (
)
var (
NewLine = "\n"
RootPath = ""
ZentaoSiteUrl = "http://127.0.0.1/"
ZtfUrl = "http://127.0.0.1:8000/"
NewLine = "\n"
RootPath = ""
ZentaoPort = 58080
ZentaoSiteUrl = fmt.Sprintf("http://127.0.0.1:%d", ZentaoPort)
ZtfUrl = "http://127.0.0.1:8000/"
)
func init() {
......
package main
import (
"github.com/ozontech/allure-go/pkg/framework/provider"
"github.com/ozontech/allure-go/pkg/framework/suite"
"testing"
)
type AllSuite struct {
suite.Suite
}
func TestRestApiSet(t *testing.T) {
suite.RunSuite(t, new(ProductApiSuite))
}
func (s *AllSuite) BeforeEach(t provider.T) {
t.ID("0")
t.AddSubSuite("TestProductApi")
}
package main
import (
"github.com/ozontech/allure-go/pkg/framework/provider"
"github.com/ozontech/allure-go/pkg/framework/suite"
)
func (s *ProductApiSuite) BeforeEach(t provider.T) {
t.ID("1")
t.AddSubSuite("ProductApi")
}
func (s *ProductApiSuite) TestMe(t provider.T) {
t.Require().Equal("Success", "Success")
}
type ProductApiSuite struct {
suite.Suite
}
package main
import (
"flag"
"fmt"
commDomain "github.com/easysoft/zentaoatf/internal/pkg/domain"
zentaoHelper "github.com/easysoft/zentaoatf/internal/pkg/helper/zentao"
serverDomain "github.com/easysoft/zentaoatf/internal/server/modules/v1/domain"
fileUtils "github.com/easysoft/zentaoatf/pkg/lib/file"
"os"
"path/filepath"
"runtime"
"testing"
"time"
commConsts "github.com/easysoft/zentaoatf/internal/pkg/consts"
execHelper "github.com/easysoft/zentaoatf/internal/pkg/helper/exec"
serverConfig "github.com/easysoft/zentaoatf/internal/server/config"
i118Utils "github.com/easysoft/zentaoatf/pkg/lib/i118"
commonTestHelper "github.com/easysoft/zentaoatf/test/helper/common"
constTestHelper "github.com/easysoft/zentaoatf/test/helper/conf"
uiTest "github.com/easysoft/zentaoatf/test/helper/zentao/ui"
)
var (
runFrom, version, testToRun string
flagSet *flag.FlagSet
)
func main() {
defer func() {
execHelper.KillProcessByUUID("ui_auto_test")
uiTest.Close()
}()
flagSet = flag.NewFlagSet("restapi", flag.ContinueOnError)
flagSet.StringVar(&runFrom, "runFrom", "cmd", "")
flagSet.StringVar(&runFrom, "f", "cmd", "")
flagSet.StringVar(&version, "version", "latest", "")
flagSet.StringVar(&version, "v", "latest", "")
flagSet.StringVar(&testToRun, "testToRun", "all_test.go", "")
flagSet.StringVar(&testToRun, "t", "all_test.go", "")
testing.Init()
flagSet.Parse(os.Args[1:])
initTest(version)
initZentao(runFrom, version)
doTest(testToRun)
}
func initTest(version string) (err error) {
commConsts.ExecFrom = commConsts.FromCmd
serverConfig.InitLog()
serverConfig.InitExecLog(constTestHelper.RootPath)
commConsts.ZtfDir = constTestHelper.RootPath
i118Utils.Init("zh-CN", commConsts.AppServer)
fmt.Println(version)
return
}
func initZentao(runFrom, version string) (err error) {
if runFrom == "jenkins" {
err := commonTestHelper.InitZentaoData()
if err != nil {
fmt.Println("Init zentao data fail ", err)
}
} else {
err := commonTestHelper.InitZentao(version)
if err != nil {
fmt.Println("Init zentao data fail ", err)
}
err = commonTestHelper.BuildCli()
if err != nil {
fmt.Println("Build cli fail ", err)
}
}
return
}
func doTest(testToRun string) (err error) {
testPath := fmt.Sprintf(`%stest`, constTestHelper.RootPath)
if runtime.GOOS == "windows" {
testPath = fmt.Sprintf(`%stest`, constTestHelper.RootPath)
}
req := serverDomain.TestSet{
WorkspacePath: testPath,
Cmd: fmt.Sprintf("go test ./restapi/%s -v", testToRun),
TestTool: commConsts.GoTest,
}
fmt.Println(testPath, req.Cmd)
// exec testing
report := execSuite(req, "api")
// submit result
config := commDomain.WorkspaceConf{Url: constTestHelper.ZentaoSiteUrl + "/", Password: "Test123456.", Username: "admin"}
err = zentaoHelper.CommitResult(report, 1, 0, config, nil)
return
}
func execSuite(req serverDomain.TestSet, unitType string) (report commDomain.ZtfReport) {
commConsts.AllureReportDir = filepath.Join(unitType, "allure-results")
pth := filepath.Join(req.WorkspacePath, commConsts.AllureReportDir)
fileUtils.RmDir(pth)
startTime := time.Now()
// run
execHelper.RunUnitTest(nil, req.Cmd, req.WorkspacePath, nil)
entTime := time.Now()
// gen report
req.ResultDir = commConsts.AllureReportDir
req.ZipDir = req.ResultDir
if !fileUtils.IsAbsolutePath(req.ResultDir) {
req.ResultDir = filepath.Join(req.WorkspacePath, req.ResultDir)
}
if !fileUtils.IsAbsolutePath(req.ZipDir) {
req.ZipDir = filepath.Join(req.WorkspacePath, req.ZipDir)
}
report = execHelper.GenUnitTestReport(req, startTime.Unix(), entTime.Unix(), nil, nil)
fmt.Printf("执行:%v, 成功:%v,失败:%v", report.Total, report.Pass, report.Fail)
return report
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册