support using curl as subcommand

This commit is contained in:
buyuxiang
2022-07-06 13:46:12 +08:00
parent 100c22b81f
commit f4aa4ffb28
8 changed files with 347 additions and 210 deletions

View File

@@ -35,35 +35,7 @@ var boomCmd = &cobra.Command{
path := hrp.TestCasePath(arg)
paths = append(paths, &path)
}
// if set profile, the priority is higher than the other commands
if boomArgs.profile != "" {
err := builtin.LoadFile(boomArgs.profile, &boomArgs)
if err != nil {
log.Error().Err(err).Msg("failed to load profile")
os.Exit(1)
}
}
hrpBoomer := hrp.NewBoomer(boomArgs.SpawnCount, boomArgs.SpawnRate)
hrpBoomer.SetRateLimiter(boomArgs.MaxRPS, boomArgs.RequestIncreaseRate)
if boomArgs.LoopCount > 0 {
hrpBoomer.SetLoopCount(boomArgs.LoopCount)
}
if !boomArgs.DisableConsoleOutput {
hrpBoomer.AddOutput(boomer.NewConsoleOutput())
}
if boomArgs.PrometheusPushgatewayURL != "" {
hrpBoomer.AddOutput(boomer.NewPrometheusPusherOutput(boomArgs.PrometheusPushgatewayURL, "hrp", hrpBoomer.GetMode()))
}
hrpBoomer.SetDisableKeepAlive(boomArgs.DisableKeepalive)
hrpBoomer.SetDisableCompression(boomArgs.DisableCompression)
hrpBoomer.SetClientTransport()
if venv != "" {
hrpBoomer.SetPython3Venv(venv)
}
hrpBoomer.EnableCPUProfile(boomArgs.CPUProfile, boomArgs.CPUProfileDuration)
hrpBoomer.EnableMemoryProfile(boomArgs.MemoryProfile, boomArgs.MemoryProfileDuration)
hrpBoomer.EnableGracefulQuit()
hrpBoomer := makeHRPBoomer()
hrpBoomer.Run(paths...)
},
}
@@ -105,3 +77,36 @@ func init() {
boomCmd.Flags().BoolVar(&boomArgs.DisableKeepalive, "disable-keepalive", false, "Disable keepalive")
boomCmd.Flags().StringVar(&boomArgs.profile, "profile", "", "profile for load testing")
}
func makeHRPBoomer() *hrp.HRPBoomer {
// if set profile, the priority is higher than the other commands
if boomArgs.profile != "" {
err := builtin.LoadFile(boomArgs.profile, &boomArgs)
if err != nil {
log.Error().Err(err).Msg("failed to load profile")
os.Exit(1)
}
}
hrpBoomer := hrp.NewBoomer(boomArgs.SpawnCount, boomArgs.SpawnRate)
hrpBoomer.SetRateLimiter(boomArgs.MaxRPS, boomArgs.RequestIncreaseRate)
if boomArgs.LoopCount > 0 {
hrpBoomer.SetLoopCount(boomArgs.LoopCount)
}
if !boomArgs.DisableConsoleOutput {
hrpBoomer.AddOutput(boomer.NewConsoleOutput())
}
if boomArgs.PrometheusPushgatewayURL != "" {
hrpBoomer.AddOutput(boomer.NewPrometheusPusherOutput(boomArgs.PrometheusPushgatewayURL, "hrp", hrpBoomer.GetMode()))
}
hrpBoomer.SetDisableKeepAlive(boomArgs.DisableKeepalive)
hrpBoomer.SetDisableCompression(boomArgs.DisableCompression)
hrpBoomer.SetClientTransport()
if venv != "" {
hrpBoomer.SetPython3Venv(venv)
}
hrpBoomer.EnableCPUProfile(boomArgs.CPUProfile, boomArgs.CPUProfileDuration)
hrpBoomer.EnableMemoryProfile(boomArgs.MemoryProfile, boomArgs.MemoryProfileDuration)
hrpBoomer.EnableGracefulQuit()
return hrpBoomer
}

View File

@@ -19,39 +19,7 @@ var convertCmd = &cobra.Command{
PreRun: func(cmd *cobra.Command, args []string) {
setLogLevel(logLevel)
},
RunE: func(cmd *cobra.Command, args []string) error {
var flagCount int
var outputType convert.OutputType
if toJSONFlag {
flagCount++
}
if toYAMLFlag {
flagCount++
outputType = convert.OutputTypeYAML
}
if toGoTestFlag {
flagCount++
outputType = convert.OutputTypeGoTest
}
if toPyTestFlag {
flagCount++
outputType = convert.OutputTypePyTest
packages := []string{
fmt.Sprintf("httprunner==%s", version.VERSION),
}
_, err := builtin.EnsurePython3Venv(venv, packages...)
if err != nil {
log.Error().Err(err).Msg("python3 venv is not ready")
return err
}
}
if flagCount > 1 {
return errors.New("please specify at most one conversion flag")
}
convert.Run(outputType, outputDir, profilePath, args)
return nil
},
RunE: convertRun,
}
var (
@@ -61,6 +29,8 @@ var (
toPyTestFlag bool
outputDir string
profilePath string
outputType convert.OutputType
)
func init() {
@@ -72,3 +42,36 @@ func init() {
convertCmd.Flags().StringVarP(&outputDir, "output-dir", "d", "", "specify output directory, default to the same dir with har file")
convertCmd.Flags().StringVarP(&profilePath, "profile", "p", "", "specify profile path to override headers and cookies")
}
func convertRun(cmd *cobra.Command, args []string) error {
var flagCount int
if toJSONFlag {
flagCount++
}
if toYAMLFlag {
flagCount++
outputType = convert.OutputTypeYAML
}
if toGoTestFlag {
flagCount++
outputType = convert.OutputTypeGoTest
}
if toPyTestFlag {
flagCount++
outputType = convert.OutputTypePyTest
packages := []string{
fmt.Sprintf("httprunner==%s", version.VERSION),
}
_, err := builtin.EnsurePython3Venv(venv, packages...)
if err != nil {
log.Error().Err(err).Msg("python3 venv is not ready")
return err
}
}
if flagCount > 1 {
return errors.New("please specify at most one conversion flag")
}
convert.Run(outputType, outputDir, profilePath, args)
return nil
}

135
hrp/cmd/curl.go Normal file
View File

@@ -0,0 +1,135 @@
package cmd
import (
"os"
"strings"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/httprunner/httprunner/v4/hrp"
"github.com/httprunner/httprunner/v4/hrp/internal/boomer"
"github.com/httprunner/httprunner/v4/hrp/internal/convert"
)
var runCurlCmd = &cobra.Command{
Use: "curl URLs",
Short: "run API test with go engine using converted curl testcase",
Args: cobra.MinimumNArgs(1),
PreRun: func(cmd *cobra.Command, args []string) {
setLogLevel(logLevel)
},
Run: func(cmd *cobra.Command, args []string) {
runner := makeHRPRunner()
if runner.Run(makeCurlTestCase(args)) != nil {
os.Exit(1)
}
},
}
var boomCurlCmd = &cobra.Command{
Use: "curl URLs",
Short: "run load test with boomer using converted curl testcase",
Args: cobra.MinimumNArgs(1),
PreRun: func(cmd *cobra.Command, args []string) {
boomer.SetUlimit(10240) // ulimit -n 10240
if !strings.EqualFold(logLevel, "DEBUG") {
logLevel = "WARN" // disable info logs for load testing
}
setLogLevel(logLevel)
},
Run: func(cmd *cobra.Command, args []string) {
boomer := makeHRPBoomer()
boomer.Run(makeCurlTestCase(args))
},
}
var convertCurlCmd = &cobra.Command{
Use: "curl URLs",
Short: "convert curl command(s) to httprunner testcase",
Args: cobra.MinimumNArgs(1),
PreRun: func(cmd *cobra.Command, args []string) {
setLogLevel(logLevel)
},
RunE: func(cmd *cobra.Command, args []string) error {
curlCommand := makeCurlCommand(args)
return convertRun(cmd, []string{curlCommand})
},
}
var (
cookieSlice []string
dataSlice []string
formSlice []string
get bool
head bool
headerSlice []string
request string
)
func init() {
runCmd.AddCommand(runCurlCmd)
addCurlFlags(runCurlCmd)
boomCmd.AddCommand(boomCurlCmd)
addCurlFlags(boomCurlCmd)
convertCmd.AddCommand(convertCurlCmd)
addCurlFlags(convertCurlCmd)
}
func addCurlFlags(cmd *cobra.Command) {
cmd.Flags().StringSliceVarP(&cookieSlice, "cookie", "b", nil, "-b, --cookie in curl")
cmd.Flags().StringSliceVarP(&dataSlice, "data", "d", nil, "-d, --data in curl")
cmd.Flags().StringSliceVarP(&formSlice, "form", "F", nil, "-F, --form in curl")
cmd.Flags().BoolVarP(&get, "get", "G", false, "-G, --get in curl")
cmd.Flags().BoolVarP(&head, "head", "I", false, "-I, --head in curl")
cmd.Flags().StringSliceVarP(&headerSlice, "header", "H", nil, "-H, --header in curl")
cmd.Flags().StringVarP(&request, "request", "X", "", "-X, --request in curl")
}
func makeCurlTestCase(args []string) *hrp.TestCase {
curlCommand := makeCurlCommand(args)
tCase, err := convert.LoadSingleCurlCase(curlCommand)
if err != nil {
log.Error().Err(err).Msg("convert curl command failed")
os.Exit(1)
}
casePath, err := os.Getwd()
if err != nil {
}
testCase, err := tCase.ToTestCase(casePath)
if err != nil {
log.Error().Err(err).Msg("convert testcase failed")
os.Exit(1)
}
return testCase
}
func makeCurlCommand(args []string) string {
var cmdList []string
cmdList = append(cmdList, "curl")
for _, c := range cookieSlice {
cmdList = append(cmdList, "--cookie", c)
}
for _, d := range dataSlice {
cmdList = append(cmdList, "--data", d)
}
for _, f := range formSlice {
cmdList = append(cmdList, "--form", f)
}
if get {
cmdList = append(cmdList, "--get")
}
if head {
cmdList = append(cmdList, "--head")
}
for _, h := range headerSlice {
cmdList = append(cmdList, "--header", h)
}
if request != "" {
cmdList = append(cmdList, "--request", request)
}
cmdList = append(cmdList, args...)
return strings.Join(cmdList, " ")
}

View File

@@ -42,7 +42,8 @@ Copyright 2017 debugtalk`,
log.Info().Msg("Set log to color console other than JSON format.")
}
},
Version: version.VERSION,
Version: version.VERSION,
TraverseChildren: true,
}
var (

View File

@@ -26,27 +26,7 @@ var runCmd = &cobra.Command{
path := hrp.TestCasePath(arg)
paths = append(paths, &path)
}
runner := hrp.NewRunner(nil).
SetFailfast(!continueOnFailure).
SetSaveTests(saveTests)
if genHTMLReport {
runner.GenHTMLReport()
}
if !requestsLogOff {
runner.SetRequestsLogOn()
}
if httpStatOn {
runner.SetHTTPStatOn()
}
if pluginLogOn {
runner.SetPluginLogOn()
}
if venv != "" {
runner.SetPython3Venv(venv)
}
if proxyUrl != "" {
runner.SetProxyUrl(proxyUrl)
}
runner := makeHRPRunner()
err := runner.Run(paths...)
if err != nil {
os.Exit(1)
@@ -74,3 +54,28 @@ func init() {
runCmd.Flags().BoolVarP(&saveTests, "save-tests", "s", false, "save tests summary")
runCmd.Flags().BoolVarP(&genHTMLReport, "gen-html-report", "g", false, "generate html report")
}
func makeHRPRunner() *hrp.HRPRunner {
runner := hrp.NewRunner(nil).
SetFailfast(!continueOnFailure).
SetSaveTests(saveTests)
if genHTMLReport {
runner.GenHTMLReport()
}
if !requestsLogOff {
runner.SetRequestsLogOn()
}
if httpStatOn {
runner.SetHTTPStatOn()
}
if pluginLogOn {
runner.SetPluginLogOn()
}
if venv != "" {
runner.SetPython3Venv(venv)
}
if proxyUrl != "" {
runner.SetProxyUrl(proxyUrl)
}
return runner
}