2023-08-31 08:35:53 +00:00
|
|
|
package modules
|
|
|
|
|
|
|
|
import (
|
|
|
|
_ "embed"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2023-09-01 09:08:49 +00:00
|
|
|
"sort"
|
|
|
|
"strings"
|
2023-08-31 08:35:53 +00:00
|
|
|
|
2023-09-01 09:08:49 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/s3-tests-parser/internal/parser"
|
|
|
|
"git.frostfs.info/TrueCloudLab/s3-tests-parser/internal/s3"
|
|
|
|
"git.frostfs.info/TrueCloudLab/s3-tests-parser/internal/templates"
|
2023-08-31 08:35:53 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
var compatibilityCmd = &cobra.Command{
|
|
|
|
Use: "compatibility",
|
|
|
|
Short: "Shows compatibility results",
|
|
|
|
Long: "Form compatibility table based on passed s3 tests",
|
|
|
|
Example: `s3-tests-parser compatibility suite.csv
|
2023-09-01 09:08:49 +00:00
|
|
|
s3-tests-parser compatibility suite.json --format json
|
|
|
|
s3-tests-parser compatibility suite.json --format json --output-format md
|
|
|
|
s3-tests-parser compatibility suite.json --format json --output-format md --output result.md`,
|
2023-08-31 08:35:53 +00:00
|
|
|
RunE: runCompatibilityCmd,
|
|
|
|
}
|
|
|
|
|
|
|
|
type (
|
2023-09-01 09:08:49 +00:00
|
|
|
Results struct {
|
|
|
|
Legend []Status
|
|
|
|
TagGroups []TagGroup
|
2023-08-31 08:35:53 +00:00
|
|
|
}
|
|
|
|
|
2023-09-01 09:08:49 +00:00
|
|
|
TagGroup struct {
|
|
|
|
Name string
|
|
|
|
Tests []TestResult
|
2023-08-31 08:35:53 +00:00
|
|
|
}
|
|
|
|
|
2023-09-01 09:08:49 +00:00
|
|
|
Status struct {
|
|
|
|
Color string
|
|
|
|
Description string
|
|
|
|
}
|
|
|
|
|
|
|
|
TestResult struct {
|
|
|
|
Color string
|
|
|
|
Name string
|
|
|
|
Comment string
|
|
|
|
Passed int
|
|
|
|
Total int
|
|
|
|
}
|
2023-08-31 08:35:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2023-09-01 09:08:49 +00:00
|
|
|
formatFlag = "format"
|
|
|
|
outputFlag = "output"
|
|
|
|
outputFormatFlag = "output-format"
|
2023-08-31 08:35:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func initCompatibilityCmd() {
|
|
|
|
compatibilityCmd.Flags().String(formatFlag, "csv", "format of input test suite file")
|
2023-09-01 09:08:49 +00:00
|
|
|
compatibilityCmd.Flags().String(outputFlag, "", "file to write output, if missed the stdout is used")
|
|
|
|
compatibilityCmd.Flags().String(outputFormatFlag, "txt", "format of output")
|
2023-08-31 08:35:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func runCompatibilityCmd(cmd *cobra.Command, args []string) error {
|
|
|
|
if len(args) != 1 {
|
|
|
|
return fmt.Errorf("expected exactly one arg, got: %v", args)
|
|
|
|
}
|
|
|
|
|
2023-09-01 09:08:49 +00:00
|
|
|
testStruct, err := s3.ParseTestsStruct()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-08-31 08:35:53 +00:00
|
|
|
}
|
|
|
|
|
2023-09-01 09:08:49 +00:00
|
|
|
testsMap, err := parser.ParseSuite(args[0], viper.GetString(formatFlag))
|
2023-08-31 08:35:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("parse tests: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-09-01 09:08:49 +00:00
|
|
|
res := formResults(testStruct, testsMap)
|
|
|
|
return printResults(cmd, res)
|
|
|
|
}
|
|
|
|
|
|
|
|
var legend = []Status{
|
|
|
|
{
|
|
|
|
Color: templates.GreenColor,
|
|
|
|
Description: "Supported",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Color: templates.YellowColor,
|
|
|
|
Description: "Partially supported",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Color: templates.RedColor,
|
|
|
|
Description: "Badly supported",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Color: templates.BlueColor,
|
|
|
|
Description: "Not supported yet, but will be in future",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Color: templates.BlackColor,
|
|
|
|
Description: "Not applicable or will never be supported",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func formResults(testStruct s3.TestsStructure, testsMap map[string]bool) Results {
|
|
|
|
tagGroups := make(map[string]TagGroup)
|
2023-08-31 08:35:53 +00:00
|
|
|
for _, group := range testStruct.Groups {
|
2023-09-01 09:08:49 +00:00
|
|
|
tagGroup, ok := tagGroups[group.Tag]
|
|
|
|
if !ok {
|
|
|
|
tagGroup.Name = group.Tag
|
2023-08-31 08:35:53 +00:00
|
|
|
}
|
|
|
|
|
2023-09-01 09:08:49 +00:00
|
|
|
tagGroup.Tests = append(tagGroup.Tests, formTestResult(group, testsMap))
|
|
|
|
tagGroups[group.Tag] = tagGroup
|
|
|
|
}
|
|
|
|
|
|
|
|
res := Results{Legend: legend}
|
|
|
|
for _, group := range tagGroups {
|
|
|
|
res.TagGroups = append(res.TagGroups, group)
|
2023-08-31 08:35:53 +00:00
|
|
|
}
|
|
|
|
|
2023-09-01 09:08:49 +00:00
|
|
|
sort.Slice(res.TagGroups, func(i, j int) bool {
|
|
|
|
return res.TagGroups[i].Name < res.TagGroups[j].Name
|
|
|
|
})
|
|
|
|
|
|
|
|
return res
|
2023-08-31 08:35:53 +00:00
|
|
|
}
|
|
|
|
|
2023-09-01 09:08:49 +00:00
|
|
|
func formTestResult(group s3.Group, testsMap map[string]bool) TestResult {
|
|
|
|
ln := len(group.Tests)
|
|
|
|
pass := 0
|
2023-08-31 08:35:53 +00:00
|
|
|
|
|
|
|
for _, test := range group.Tests {
|
|
|
|
if testsMap[test] {
|
|
|
|
pass++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-01 09:08:49 +00:00
|
|
|
var color string
|
|
|
|
if strings.Contains(group.Comment, "Not supported yet") {
|
|
|
|
color = templates.BlueColor
|
|
|
|
} else if strings.Contains(group.Comment, "Not applicable") {
|
|
|
|
color = templates.BlackColor
|
2023-08-31 08:35:53 +00:00
|
|
|
}
|
|
|
|
|
2023-09-01 09:08:49 +00:00
|
|
|
if color == "" {
|
|
|
|
color = templates.RedColor
|
|
|
|
rate := float64(pass) / float64(ln)
|
|
|
|
if rate > 0.9 {
|
|
|
|
color = templates.GreenColor
|
|
|
|
} else if rate > 0.5 {
|
|
|
|
color = templates.YellowColor
|
|
|
|
}
|
2023-08-31 08:35:53 +00:00
|
|
|
}
|
|
|
|
|
2023-09-01 09:08:49 +00:00
|
|
|
return TestResult{
|
|
|
|
Color: color,
|
|
|
|
Name: group.Name,
|
|
|
|
Comment: group.Comment,
|
|
|
|
Passed: pass,
|
|
|
|
Total: ln,
|
2023-08-31 08:35:53 +00:00
|
|
|
}
|
2023-09-01 09:08:49 +00:00
|
|
|
}
|
2023-08-31 08:35:53 +00:00
|
|
|
|
2023-09-01 09:08:49 +00:00
|
|
|
func printResults(cmd *cobra.Command, res Results) error {
|
|
|
|
w := cmd.OutOrStdout()
|
|
|
|
if outFile := viper.GetString(outputFlag); outFile != "" {
|
|
|
|
f, err := os.Create(outFile)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("create out file: %w", err)
|
2023-08-31 08:35:53 +00:00
|
|
|
}
|
2023-09-01 09:08:49 +00:00
|
|
|
w = f
|
|
|
|
defer f.Close()
|
2023-08-31 08:35:53 +00:00
|
|
|
}
|
|
|
|
|
2023-09-01 09:08:49 +00:00
|
|
|
outTemplate, err := templates.GetTemplate(viper.GetString(outputFormatFlag))
|
2023-08-31 08:35:53 +00:00
|
|
|
if err != nil {
|
2023-09-01 09:08:49 +00:00
|
|
|
return fmt.Errorf("form out template: %w", err)
|
2023-08-31 08:35:53 +00:00
|
|
|
}
|
|
|
|
|
2023-09-01 09:08:49 +00:00
|
|
|
return outTemplate.Execute(w, res)
|
2023-08-31 08:35:53 +00:00
|
|
|
}
|