move files to hrp

This commit is contained in:
debugtalk
2022-03-23 15:10:23 +08:00
parent fa80e70f63
commit 79c25fc475
125 changed files with 0 additions and 0 deletions

1
hrp2/cli/README.md Normal file
View File

@@ -0,0 +1 @@
# hrp cli

83
hrp2/cli/hrp/cmd/boom.go Normal file
View File

@@ -0,0 +1,83 @@
package cmd
import (
"time"
"github.com/spf13/cobra"
"github.com/httprunner/hrp"
"github.com/httprunner/hrp/internal/boomer"
)
// boomCmd represents the boom command
var boomCmd = &cobra.Command{
Use: "boom",
Short: "run load test with boomer",
Long: `run yaml/json testcase files for load test`,
Example: ` $ hrp boom demo.json # run specified json testcase file
$ hrp boom demo.yaml # run specified yaml testcase file
$ hrp boom examples/ # run testcases in specified folder`,
Args: cobra.MinimumNArgs(1),
PreRun: func(cmd *cobra.Command, args []string) {
boomer.SetUlimit(10240) // ulimit -n 10240
setLogLevel("WARN") // disable info logs for load testing
},
Run: func(cmd *cobra.Command, args []string) {
var paths []hrp.ITestCase
for _, arg := range args {
path := hrp.TestCasePath(arg)
paths = append(paths, &path)
}
hrpBoomer := hrp.NewBoomer(spawnCount, spawnRate)
hrpBoomer.SetRateLimiter(maxRPS, requestIncreaseRate)
if loopCount > 0 {
hrpBoomer.SetLoopCount(loopCount)
}
if !disableConsoleOutput {
hrpBoomer.AddOutput(boomer.NewConsoleOutput())
}
if prometheusPushgatewayURL != "" {
hrpBoomer.AddOutput(boomer.NewPrometheusPusherOutput(prometheusPushgatewayURL, "hrp"))
}
hrpBoomer.SetDisableKeepAlive(disableKeepalive)
hrpBoomer.SetDisableCompression(disableCompression)
hrpBoomer.EnableCPUProfile(cpuProfile, cpuProfileDuration)
hrpBoomer.EnableMemoryProfile(memoryProfile, memoryProfileDuration)
hrpBoomer.EnableGracefulQuit()
hrpBoomer.Run(paths...)
},
}
var (
spawnCount int
spawnRate float64
maxRPS int64
loopCount int64
requestIncreaseRate string
memoryProfile string
memoryProfileDuration time.Duration
cpuProfile string
cpuProfileDuration time.Duration
prometheusPushgatewayURL string
disableConsoleOutput bool
disableCompression bool
disableKeepalive bool
)
func init() {
rootCmd.AddCommand(boomCmd)
boomCmd.Flags().Int64Var(&maxRPS, "max-rps", 0, "Max RPS that boomer can generate, disabled by default.")
boomCmd.Flags().StringVar(&requestIncreaseRate, "request-increase-rate", "-1", "Request increase rate, disabled by default.")
boomCmd.Flags().IntVar(&spawnCount, "spawn-count", 1, "The number of users to spawn for load testing")
boomCmd.Flags().Float64Var(&spawnRate, "spawn-rate", 1, "The rate for spawning users")
boomCmd.Flags().Int64Var(&loopCount, "loop-count", -1, "The specify running cycles for load testing")
boomCmd.Flags().StringVar(&memoryProfile, "mem-profile", "", "Enable memory profiling.")
boomCmd.Flags().DurationVar(&memoryProfileDuration, "mem-profile-duration", 30*time.Second, "Memory profile duration.")
boomCmd.Flags().StringVar(&cpuProfile, "cpu-profile", "", "Enable CPU profiling.")
boomCmd.Flags().DurationVar(&cpuProfileDuration, "cpu-profile-duration", 30*time.Second, "CPU profile duration.")
boomCmd.Flags().StringVar(&prometheusPushgatewayURL, "prometheus-gateway", "", "Prometheus Pushgateway url.")
boomCmd.Flags().BoolVar(&disableConsoleOutput, "disable-console-output", false, "Disable console output.")
boomCmd.Flags().BoolVar(&disableCompression, "disable-compression", false, "Disable compression")
boomCmd.Flags().BoolVar(&disableKeepalive, "disable-keepalive", false, "Disable keepalive")
}

View File

@@ -0,0 +1,15 @@
package cmd
import (
"testing"
"github.com/spf13/cobra/doc"
)
// run this test to generate markdown docs for hrp command
func TestGenMarkdownTree(t *testing.T) {
err := doc.GenMarkdownTree(rootCmd, "../../../docs/cmd")
if err != nil {
t.Fatal(err)
}
}

View File

@@ -0,0 +1,64 @@
package cmd
import (
"errors"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/httprunner/hrp/internal/har2case"
)
// har2caseCmd represents the har2case command
var har2caseCmd = &cobra.Command{
Use: "har2case $har_path...",
Short: "convert HAR to json/yaml testcase files",
Long: `convert HAR to json/yaml testcase files`,
Args: cobra.MinimumNArgs(1),
PreRun: func(cmd *cobra.Command, args []string) {
setLogLevel(logLevel)
},
RunE: func(cmd *cobra.Command, args []string) error {
var outputFiles []string
for _, arg := range args {
// must choose one
if !genYAMLFlag && !genJSONFlag {
return errors.New("please select convert format type")
}
var outputPath string
var err error
har := har2case.NewHAR(arg)
// specify output dir
if outputDir != "" {
har.SetOutputDir(outputDir)
}
// generate json/yaml files
if genYAMLFlag {
outputPath, err = har.GenYAML()
} else {
outputPath, err = har.GenJSON() // default
}
if err != nil {
return err
}
outputFiles = append(outputFiles, outputPath)
}
log.Info().Strs("output", outputFiles).Msg("convert testcase success")
return nil
},
}
var (
genJSONFlag bool
genYAMLFlag bool
outputDir string
)
func init() {
rootCmd.AddCommand(har2caseCmd)
har2caseCmd.Flags().BoolVarP(&genJSONFlag, "to-json", "j", true, "convert to JSON format")
har2caseCmd.Flags().BoolVarP(&genYAMLFlag, "to-yaml", "y", false, "convert to YAML format")
har2caseCmd.Flags().StringVarP(&outputDir, "output-dir", "d", "", "specify output directory, default to the same dir with har file")
}

80
hrp2/cli/hrp/cmd/root.go Normal file
View File

@@ -0,0 +1,80 @@
package cmd
import (
"os"
"runtime"
"strings"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/httprunner/hrp/internal/version"
)
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "hrp",
Short: "One-stop solution for HTTP(S) testing.",
Long: `
██╗ ██╗████████╗████████╗██████╗ ██████╗ ██╗ ██╗███╗ ██╗███╗ ██╗███████╗██████╗
██║ ██║╚══██╔══╝╚══██╔══╝██╔══██╗██╔══██╗██║ ██║████╗ ██║████╗ ██║██╔════╝██╔══██╗
███████║ ██║ ██║ ██████╔╝██████╔╝██║ ██║██╔██╗ ██║██╔██╗ ██║█████╗ ██████╔╝
██╔══██║ ██║ ██║ ██╔═══╝ ██╔══██╗██║ ██║██║╚██╗██║██║╚██╗██║██╔══╝ ██╔══██╗
██║ ██║ ██║ ██║ ██║ ██║ ██║╚██████╔╝██║ ╚████║██║ ╚████║███████╗██║ ██║
╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═══╝╚═╝ ╚═══╝╚══════╝╚═╝ ╚═╝
hrp (HttpRunner+) aims to be a one-stop solution for HTTP(S) testing, covering API testing,
load testing and digital experience monitoring (DEM). Enjoy! ✨ 🚀 ✨
License: Apache-2.0
Website: https://httprunner.com
Github: https://github.com/httprunner/hrp
Copyright 2021 debugtalk`,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
var noColor = false
if runtime.GOOS == "windows" {
noColor = true
}
if !logJSON {
log.Logger = zerolog.New(zerolog.ConsoleWriter{NoColor: noColor, Out: os.Stderr}).With().Timestamp().Logger()
log.Info().Msg("Set log to color console other than JSON format.")
}
},
Version: version.VERSION,
}
var (
logLevel string
logJSON bool
)
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
rootCmd.PersistentFlags().StringVarP(&logLevel, "log-level", "l", "INFO", "set log level")
rootCmd.PersistentFlags().BoolVar(&logJSON, "log-json", false, "set log to json format")
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
}
func setLogLevel(level string) {
level = strings.ToUpper(level)
log.Info().Str("level", level).Msg("Set log level")
switch level {
case "DEBUG":
zerolog.SetGlobalLevel(zerolog.DebugLevel)
case "INFO":
zerolog.SetGlobalLevel(zerolog.InfoLevel)
case "WARN":
zerolog.SetGlobalLevel(zerolog.WarnLevel)
case "ERROR":
zerolog.SetGlobalLevel(zerolog.ErrorLevel)
case "FATAL":
zerolog.SetGlobalLevel(zerolog.FatalLevel)
case "PANIC":
zerolog.SetGlobalLevel(zerolog.PanicLevel)
}
}

68
hrp2/cli/hrp/cmd/run.go Normal file
View File

@@ -0,0 +1,68 @@
package cmd
import (
"os"
"github.com/spf13/cobra"
"github.com/httprunner/hrp"
)
// runCmd represents the run command
var runCmd = &cobra.Command{
Use: "run $path...",
Short: "run API test",
Long: `run yaml/json testcase files for API test`,
Example: ` $ hrp run demo.json # run specified json testcase file
$ hrp run demo.yaml # run specified yaml testcase file
$ hrp run examples/ # run testcases in specified folder`,
Args: cobra.MinimumNArgs(1),
PreRun: func(cmd *cobra.Command, args []string) {
setLogLevel(logLevel)
},
Run: func(cmd *cobra.Command, args []string) {
var paths []hrp.ITestCase
for _, arg := range args {
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 pluginLogOn {
runner.SetPluginLogOn()
}
if proxyUrl != "" {
runner.SetProxyUrl(proxyUrl)
}
err := runner.Run(paths...)
if err != nil {
os.Exit(1)
}
},
}
var (
continueOnFailure bool
requestsLogOff bool
pluginLogOn bool
proxyUrl string
saveTests bool
genHTMLReport bool
)
func init() {
rootCmd.AddCommand(runCmd)
runCmd.Flags().BoolVarP(&continueOnFailure, "continue-on-failure", "c", false, "continue running next step when failure occurs")
runCmd.Flags().BoolVar(&requestsLogOff, "log-requests-off", false, "turn off request & response details logging")
runCmd.Flags().BoolVar(&pluginLogOn, "log-plugin", false, "turn on plugin logging")
runCmd.Flags().StringVarP(&proxyUrl, "proxy-url", "p", "", "set proxy url")
runCmd.Flags().BoolVarP(&saveTests, "save-tests", "s", false, "save tests summary")
runCmd.Flags().BoolVarP(&genHTMLReport, "gen-html-report", "g", false, "generate html report")
}

View File

@@ -0,0 +1,54 @@
package cmd
import (
"errors"
"os"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/httprunner/hrp/internal/scaffold"
)
var scaffoldCmd = &cobra.Command{
Use: "startproject $project_name",
Short: "create a scaffold project",
Args: cobra.ExactValidArgs(1),
PreRun: func(cmd *cobra.Command, args []string) {
setLogLevel(logLevel)
},
RunE: func(cmd *cobra.Command, args []string) error {
if !ignorePlugin && !genPythonPlugin && !genGoPlugin {
return errors.New("please select function plugin type")
}
var pluginType scaffold.PluginType
if ignorePlugin {
pluginType = scaffold.Ignore
} else if genGoPlugin {
pluginType = scaffold.Go
} else {
pluginType = scaffold.Py // default
}
err := scaffold.CreateScaffold(args[0], pluginType)
if err != nil {
log.Error().Err(err).Msg("create scaffold project failed")
os.Exit(1)
}
log.Info().Str("projectName", args[0]).Msg("create scaffold success")
return nil
},
}
var (
ignorePlugin bool
genPythonPlugin bool
genGoPlugin bool
)
func init() {
rootCmd.AddCommand(scaffoldCmd)
scaffoldCmd.Flags().BoolVar(&genPythonPlugin, "py", true, "generate hashicorp python plugin")
scaffoldCmd.Flags().BoolVar(&genGoPlugin, "go", false, "generate hashicorp go plugin")
scaffoldCmd.Flags().BoolVar(&ignorePlugin, "ignore-plugin", false, "ignore function plugin")
}

9
hrp2/cli/hrp/main.go Normal file
View File

@@ -0,0 +1,9 @@
package main
import (
"github.com/httprunner/hrp/cli/hrp/cmd"
)
func main() {
cmd.Execute()
}

23
hrp2/cli/scripts/build.sh Normal file
View File

@@ -0,0 +1,23 @@
#!/bin/bash
# build hrp cli binary for testing
# release will be triggered on github actions, see .github/workflows/release.yml
# Usage:
# $ make build
# or
# $ bash cli/scripts/build.sh
set -e
set -x
# prepare path
mkdir -p "output"
bin_path="output/hrp"
# build
go build -ldflags '-s -w' -o "$bin_path" cli/hrp/main.go
# check output and version
ls -lh "$bin_path"
chmod +x "$bin_path"
./"$bin_path" -v

View File

@@ -0,0 +1,23 @@
#!/bin/bash
# build hrp cli binary for testing
# release will be triggered on github actions, see .github/workflows/release.yml
# Usage:
# $ make bump version=v0.5.2
# or
# $ bash cli/scripts/bump_version.sh v0.5.2
set -e
version=$1
if [ -z "$version" ]; then
echo "version is required"
exit 1
fi
echo "bump hrp version to $version"
sed -i'.bak' "s/\"v.*\"/\"$version\"/g" internal/version/init.go
echo "bump install.sh version to $version"
sed -i'.bak' "s/LATEST_VERSION=\"v.*\"/LATEST_VERSION=\"$version\"/g" cli/scripts/install.sh

124
hrp2/cli/scripts/install.sh Normal file
View File

@@ -0,0 +1,124 @@
#!/bin/bash
# install hrp with one shell command
# bash -c "$(curl -ksSL https://httprunner.oss-cn-beijing.aliyuncs.com/install.sh)"
LATEST_VERSION="v0.8.0"
set -e
function echoError() {
echo -e "\033[31m✘ $1\033[0m" # red
}
export -f echoError
function echoInfo() {
echo -e "\033[32m✔ $1\033[0m" # green
}
export -f echoInfo
function echoWarn() {
echo -e "\033[33m! $1\033[0m" # yellow
}
export -f echoError
function get_latest_version() {
# <title>Release v0.4.0 · httprunner/hrp · GitHub</title>
curl -sL https://github.com/httprunner/hrp/releases/latest | grep '<title>Release' | cut -d" " -f4
}
function get_os() {
os=$(uname -s)
echo "$os" | tr '[:upper:]' '[:lower:]'
}
function get_arch() {
arch=$(uname -m)
if [ "$arch" == "x86_64" ]; then
arch="amd64"
fi
echo "$arch"
}
function get_pkg_suffix() {
os=$1
if [ "$os" == "windows" ]; then
echo ".zip"
else
echo ".tar.gz"
fi
}
function extract_pkg() {
pkg=$1
if [[ $pkg == *.zip ]]; then # windows
echo "$ unzip -o $pkg -d ."
unzip -o $pkg -d .
else
echo "$ tar -xzf $pkg"
tar -xzf "$pkg"
fi
}
function main() {
echoInfo "Detect target hrp package..."
version=$LATEST_VERSION
os=$(get_os)
echo "Current OS: $os"
arch=$(get_arch)
echo "Current ARCH: $arch"
pkg_suffix=$(get_pkg_suffix $os)
pkg="hrp-$version-$os-$arch$pkg_suffix"
# download from aliyun OSS
url="https://httprunner.oss-cn-beijing.aliyuncs.com/$pkg"
if ! curl --output /dev/null --silent --head --fail "$url"; then
# aliyun OSS url is invalid, try to download from github
version=$(get_latest_version)
pkg="hrp-$version-$os-$arch$pkg_suffix"
url="https://github.com/httprunner/hrp/releases/download/$version/$pkg"
fi
echo "Latest version: $version"
echo "Download url: $url"
echo
echoInfo "Created temp dir..."
echo "$ mktemp -d -t hrp.XXXX"
tmp_dir=$(mktemp -d -t hrp.XXXX)
echo "$tmp_dir"
cd "$tmp_dir"
echo
echoInfo "Downloading..."
echo "$ curl -kL $url -o $pkg"
curl -kL $url -o "$pkg"
echo
echoInfo "Extracting..."
extract_pkg "$pkg"
echo "$ ls -lh"
ls -lh
echo
echoInfo "Installing..."
if hrp -v > /dev/null && [ $(command -v hrp) != "./hrp" ]; then
echoWarn "$(hrp -v) exists, remove first !!!"
echo "$ rm -rf $(command -v hrp)"
rm -rf "$(command -v hrp)"
fi
echo "$ chmod +x hrp && mv hrp /usr/local/bin/"
chmod +x hrp
mv hrp /usr/local/bin/
echo
echoInfo "Check installation..."
echo "$ command -v hrp"
command -v hrp
echo "$ hrp -v"
hrp -v
echo "$ hrp -h"
hrp -h
}
main