github.com/redhat-appstudio/e2e-tests@v0.0.0-20230619105049-9a422b2094d7/pkg/framework/polarion_reporter.go (about)

     1  package framework
     2  
     3  import (
     4  	"flag"
     5  	"strings"
     6  
     7  	types "github.com/onsi/ginkgo/v2/types"
     8  	polarion_xml "kubevirt.io/qe-tools/pkg/polarion-xml"
     9  )
    10  
    11  func GeneratePolarionReport(report types.Report, outputFile string, polarionProjectID string) {
    12  	dryRun := flag.String("dry-run", "false", "Dry-run property")
    13  
    14  	var testCases = &polarion_xml.TestCases{
    15  		ProjectID: polarionProjectID,
    16  		Properties: polarion_xml.PolarionProperties{
    17  			Property: []polarion_xml.PolarionProperty{
    18  				{
    19  					Name:  "lookup-method",
    20  					Value: "custom",
    21  				},
    22  				{
    23  					Name:  "custom-lookup-method-field-id",
    24  					Value: "customId",
    25  				},
    26  				{
    27  					Name:  "dry-run",
    28  					Value: *dryRun,
    29  				},
    30  			},
    31  		},
    32  	}
    33  
    34  	type PolarionCase struct {
    35  		testCase string
    36  		filename string
    37  		labels   string
    38  		steps    []string
    39  	}
    40  
    41  	polarionCaseMap := make(map[string]PolarionCase)
    42  	for i := range report.SpecReports {
    43  		specReport := report.SpecReports[i]
    44  		if specReport.LeafNodeType == types.NodeTypeIt {
    45  			if polarionCaseMod, found := polarionCaseMap[specReport.ContainerHierarchyTexts[0]]; found {
    46  				polarionCaseMod.steps = append(polarionCaseMod.steps, strings.Join(append(specReport.ContainerHierarchyTexts[1:], specReport.LeafNodeText, ""), ""))
    47  				polarionCaseMap[specReport.ContainerHierarchyTexts[0]] = polarionCaseMod
    48  			} else {
    49  				polarionCase := PolarionCase{specReport.ContainerHierarchyTexts[0], specReport.LeafNodeLocation.FileName, strings.Join(specReport.ContainerHierarchyLabels[0], ","), append([]string{}, strings.Join(append(specReport.ContainerHierarchyTexts[1:], specReport.LeafNodeText, ""), ""))}
    50  				polarionCaseMap[specReport.ContainerHierarchyTexts[0]] = polarionCase
    51  			}
    52  		}
    53  	}
    54  
    55  	for testCaseSpec, testPolarionCase := range polarionCaseMap {
    56  		testCase := &polarion_xml.TestCase{
    57  			Title:       polarion_xml.Title{Content: testCaseSpec},
    58  			Description: polarion_xml.Description{Content: testCaseSpec},
    59  		}
    60  
    61  		customFields := polarion_xml.TestCaseCustomFields{}
    62  		addCustomField(&customFields, "caseautomation", "automated")
    63  		addCustomField(&customFields, "testtype", "functional")
    64  		addCustomField(&customFields, "automation_script", parseTestSourceFromFilename(testPolarionCase.filename))
    65  		addCustomField(&customFields, "tags", testPolarionCase.labels)
    66  		testCase.TestCaseCustomFields = customFields
    67  
    68  		for _, testStep := range testPolarionCase.steps {
    69  			if testCase.TestCaseSteps == nil {
    70  				testCase.TestCaseSteps = &polarion_xml.TestCaseSteps{}
    71  			}
    72  			addTestStep(testCase.TestCaseSteps, testStep, true)
    73  		}
    74  		parseTagsFromTitle(testCase, testCaseSpec, parseComponentFromFilename(testPolarionCase.filename), testCases.ProjectID)
    75  		testCases.TestCases = append(testCases.TestCases, *testCase)
    76  	}
    77  	// generate polarion test cases XML file
    78  	polarion_xml.GeneratePolarionXmlFile(outputFile, testCases)
    79  }
    80  
    81  func addCustomField(customFields *polarion_xml.TestCaseCustomFields, id, content string) {
    82  	customFields.CustomFields = append(
    83  		customFields.CustomFields, polarion_xml.TestCaseCustomField{
    84  			Content: content,
    85  			ID:      id,
    86  		})
    87  }
    88  
    89  func addTestStep(testCaseSteps *polarion_xml.TestCaseSteps, content string, prepend bool) {
    90  	testCaseStep := polarion_xml.TestCaseStep{
    91  		StepColumn: []polarion_xml.TestCaseStepColumn{
    92  			{
    93  				Content: content,
    94  				ID:      "step",
    95  			},
    96  			{
    97  				Content: "Succeeded",
    98  				ID:      "expectedResult",
    99  			},
   100  		},
   101  	}
   102  	if prepend {
   103  		testCaseSteps.Steps = append([]polarion_xml.TestCaseStep{testCaseStep}, testCaseSteps.Steps...)
   104  	} else {
   105  		testCaseSteps.Steps = append(testCaseSteps.Steps, testCaseStep)
   106  	}
   107  }
   108  
   109  // How to use these tags is described here: https://github.com/redhat-appstudio/e2e-tests/blob/main/docs/LabelsNaming.md#tests-naming
   110  func parseTagsFromTitle(testCase *polarion_xml.TestCase, title string, component string, projectID string) {
   111  	posneg := "positive"
   112  	caselevel := "component"
   113  	criticality := "medium"
   114  	rfeID := ""
   115  	customID := ""
   116  	title = strings.Replace(title, "]", ",", -1)
   117  	title = strings.Replace(title, "[", ",", -1)
   118  	attrList := strings.Split(title, ",")
   119  
   120  	for i := 0; i < len(attrList); i++ {
   121  		attrList[i] = strings.Trim(attrList[i], " ")
   122  		if strings.Contains(attrList[i], "test_id:") {
   123  			testID := strings.Split(strings.Trim(strings.Split(attrList[i], "test_id:")[1], " "), " ")[0]
   124  			testCase.ID = projectID + "-" + testID
   125  			customID = projectID + "-" + testID
   126  		} else if strings.Contains(attrList[i], "rfe_id:") {
   127  			rfeID = projectID + "-" + strings.Split(strings.Trim(strings.Split(attrList[i], "rfe_id:")[1], " "), " ")[0]
   128  		} else if strings.Contains(attrList[i], "crit:") {
   129  			criticality = strings.Split(strings.Trim(strings.Split(attrList[i], "crit:")[1], " "), " ")[0]
   130  
   131  		} else if strings.Contains(attrList[i], "posneg:") {
   132  			posneg = strings.Split(strings.Trim(strings.Split(attrList[i], "posneg:")[1], " "), " ")[0]
   133  
   134  		} else if strings.Contains(attrList[i], "level:") {
   135  			caselevel = strings.Split(strings.Trim(strings.Split(attrList[i], "level:")[1], " "), " ")[0]
   136  
   137  		} else if strings.Contains(attrList[i], "component:") {
   138  			component = strings.Split(strings.Trim(strings.Split(attrList[i], "component:")[1], " "), " ")[0]
   139  		}
   140  	}
   141  
   142  	addCustomField(&testCase.TestCaseCustomFields, "customId", customID)
   143  	addCustomField(&testCase.TestCaseCustomFields, "caseimportance", criticality)
   144  	addCustomField(&testCase.TestCaseCustomFields, "caseposneg", posneg)
   145  	addCustomField(&testCase.TestCaseCustomFields, "caselevel", caselevel)
   146  	addLinkedWorkItem(&testCase.TestCaseLinkedWorkItems, rfeID)
   147  	if component != "" {
   148  		addCustomField(&testCase.TestCaseCustomFields, "casecomponent", component)
   149  	}
   150  }
   151  
   152  func addLinkedWorkItem(linkedWorkItems *polarion_xml.TestCaseLinkedWorkItems, id string) {
   153  	linkedWorkItems.LinkedWorkItems = append(
   154  		linkedWorkItems.LinkedWorkItems, polarion_xml.TestCaseLinkedWorkItem{
   155  			ID:   id,
   156  			Role: "verifies",
   157  		})
   158  }
   159  
   160  func parseComponentFromFilename(filename string) string {
   161  	return strings.Split(parseTestSourceFromFilename(filename), "/")[3]
   162  }
   163  
   164  func parseTestSourceFromFilename(filename string) string {
   165  	n := strings.LastIndex(filename, "/e2e-tests/tests/")
   166  	return filename[n:]
   167  }