Skip to content

Commit

Permalink
add code
Browse files Browse the repository at this point in the history
  • Loading branch information
ms-zhenhua committed Sep 25, 2023
1 parent c4423d4 commit 7d9a6c3
Show file tree
Hide file tree
Showing 5 changed files with 517 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ commands/.temp

# never upload the build to git
armstrong
armstrong.exe

# test output
coverage/test_coverage_report*.md
Expand Down
69 changes: 69 additions & 0 deletions commands/api_test_generate_report.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package commands

import (
"flag"
"fmt"
"github.com/ms-henglu/armstrong/report"
"github.com/sirupsen/logrus"
"log"
"os"
"path/filepath"
"strings"
)

type ApiTestGenerateReportCommand struct {
workingDir string
swaggerPath string
}

func (c *ApiTestGenerateReportCommand) flags() *flag.FlagSet {
fs := defaultFlagSet("api-test-generate-report")
fs.StringVar(&c.workingDir, "working-dir", "", "path that contains all the test cases")
fs.StringVar(&c.swaggerPath, "swagger", "", "path to the .json swagger which is being test")
fs.Usage = func() { logrus.Error(c.Help()) }
return fs
}

func (c ApiTestGenerateReportCommand) Help() string {
helpText := `
Usage: armstrong api-test-generate-report
` + c.Synopsis() + "\n\n" + helpForFlags(c.flags())

return strings.TrimSpace(helpText)
}

func (c ApiTestGenerateReportCommand) Synopsis() string {
return "Generate test report for a set of test results"
}

func (c ApiTestGenerateReportCommand) Run(args []string) int {
f := c.flags()
if err := f.Parse(args); err != nil {
logrus.Error(fmt.Sprintf("Error parsing command-line flags: %s", err))
return 1
}
return c.Execute()
}

func (c ApiTestGenerateReportCommand) Execute() int {
log.Println("[INFO] ----------- generate API Test Report ---------")
wd, err := os.Getwd()
if err != nil {
logrus.Error(fmt.Sprintf("failed to get working directory: %+v", err))
return 1
}

if c.workingDir != "" {
wd, err = filepath.Abs(c.workingDir)
if err != nil {
logrus.Error(fmt.Sprintf("working directory is invalid: %+v", err))
return 1
}
}

if report.GenerateApiTestReports(wd, c.swaggerPath) != nil {
log.Fatalf("[ERROR] failed to generate API Test Report: %+v", err)
}

return 0
}
26 changes: 24 additions & 2 deletions commands/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,18 @@ import (
)

type TestCommand struct {
verbose bool
workingDir string
verbose bool
workingDir string
destroyAfterTest bool
swaggerPath string
}

func (c *TestCommand) flags() *flag.FlagSet {
fs := defaultFlagSet("test")
fs.BoolVar(&c.verbose, "v", false, "whether show terraform logs")
fs.StringVar(&c.workingDir, "working-dir", "", "path to Terraform configuration files")
fs.BoolVar(&c.destroyAfterTest, "destroy-after-test", false, "whether to destroy the created resources after each test")
fs.StringVar(&c.swaggerPath, "swagger", "", "path to the .json swagger which is being test")
fs.Usage = func() { logrus.Error(c.Help()) }
return fs
}
Expand Down Expand Up @@ -140,9 +144,27 @@ func (c TestCommand) Execute() int {
} else {
log.Fatalf("[ERROR] error showing terraform state: %+v", err)
}

return 0
}

if applyErr == nil {
if c.destroyAfterTest {
destroyErr := terraform.Destroy()
if destroyErr != nil {
log.Printf("[ERROR] error running terraform destroy: %+v\n", destroyErr)
} else {
log.Println("[INFO] test resource has been deleted")
}
}
}

if c.swaggerPath != "" {
if report.StoreApiTestReport(wd, c.swaggerPath) != nil {
log.Fatalf("[ERROR] error storing api test report: %+v", err)
}
}

logs, err := report.ParseLogs(path.Join(wd, "log.txt"))
if err != nil {
log.Printf("[ERROR] parsing log.txt: %+v", err)
Expand Down
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ func main() {
"cleanup": func() (cli.Command, error) {
return &commands.CleanupCommand{}, nil
},
"api-test-generate-report": func() (cli.Command, error) {
return &commands.ApiTestGenerateReportCommand{}, nil
},
}

exitStatus, err := c.Run()
Expand Down
Loading

0 comments on commit 7d9a6c3

Please sign in to comment.