@@ -13,108 +13,8 @@ import (
1313 cranevalidate "github.com/konveyor/crane/internal/validate"
1414 . "github.com/onsi/ginkgo/v2"
1515 . "github.com/onsi/gomega"
16- "gopkg.in/yaml.v3"
1716)
1817
19- func runValidateAndParseReport (runner CraneRunner , inputDir string , validateDir string ,
20- apiSurfaceFile string , outputFormat string ) (cranevalidate.ValidationReport , error ) {
21- // Ran crane validate command and parse the report
22- stdout , err := runner .Validate (ValidateOptions {
23- InputDir : inputDir ,
24- ValidateDir : validateDir ,
25- APIResourcesFile : apiSurfaceFile ,
26- OutputFormat : outputFormat ,
27- })
28- if err != nil {
29- return cranevalidate.ValidationReport {}, err
30- }
31- log .Printf ("Validate %s stdout: %s" , outputFormat , stdout )
32-
33- By ("Verify " + outputFormat + " validation report exists" )
34- reportExt := "." + outputFormat
35- reportPath := filepath .Join (validateDir , "report" + reportExt )
36- Expect (reportPath ).To (BeAnExistingFile (), "expected report%s at %s" , reportExt , reportPath )
37-
38- By ("Parse and verify " + outputFormat + " validation report" )
39- reportData , err := os .ReadFile (reportPath )
40- if err != nil {
41- return cranevalidate.ValidationReport {}, err
42- }
43-
44- var report cranevalidate.ValidationReport
45- if outputFormat == "yaml" {
46- err = yaml .Unmarshal (reportData , & report )
47- } else {
48- err = json .Unmarshal (reportData , & report )
49- }
50- if err != nil {
51- return cranevalidate.ValidationReport {}, err
52- }
53-
54- return report , nil
55- }
56-
57- func verifyValidateResults (report cranevalidate.ValidationReport , namespace string ,
58- validateDir string , apiSurfaceFile string , outputFormat string ) {
59- // Verify report data and creation of failures directory when required
60- By (outputFormat + " report: Verify report shows offline mode" )
61- Expect (report .Mode ).To (Equal ("offline" ), "expected validation mode to be 'offline' in %s report" , outputFormat )
62- log .Printf ("%s validation mode: %s" , outputFormat , report .Mode )
63-
64- By (outputFormat + " report: Verify apiResourcesSource is set to the captured API surface file" )
65- Expect (report .APIResourcesSource ).NotTo (BeEmpty (), "expected apiResourcesSource to be set in offline mode" )
66- Expect (report .APIResourcesSource ).To (Equal (apiSurfaceFile ), "expected apiResourcesSource to match API surface file path" )
67- log .Printf ("API resources source: %s" , report .APIResourcesSource )
68-
69- By (outputFormat + " report: Verify resource count" )
70- Expect (report .TotalScanned ).To (Equal (5 ), "expected exactly 5 resources scanned in %s report" , outputFormat )
71- Expect (report .Compatible ).To (Equal (5 ), "expected all 5 resources to be compatible in %s report" , outputFormat )
72- Expect (report .Incompatible ).To (Equal (0 ), "expected no incompatible resources in %s report" , outputFormat )
73- log .Printf ("%s report - Total: %d, Compatible: %d, Incompatible: %d" ,
74- outputFormat , report .TotalScanned , report .Compatible , report .Incompatible )
75-
76- By (outputFormat + " report: Verify resource API version, namespace, status" )
77- expectedResources := map [string ]string {
78- "Deployment" : "apps/v1" ,
79- "Service" : "v1" ,
80- "ConfigMap" : "v1" ,
81- "Secret" : "v1" ,
82- "RoleBinding" : "rbac.authorization.k8s.io/v1" ,
83- }
84-
85- foundResources := make (map [string ]bool )
86- for _ , result := range report .Results {
87- if expectedAPIVersion , expected := expectedResources [result .Kind ]; expected {
88- foundResources [result .Kind ] = true
89- Expect (result .APIVersion ).To (Equal (expectedAPIVersion ),
90- "expected %s to have apiVersion %s in %s report" , result .Kind , expectedAPIVersion , outputFormat )
91- Expect (result .Status ).To (Equal (cranevalidate .StatusOK ),
92- "expected %s to have status OK in %s report" , result .Kind , outputFormat )
93- Expect (result .Namespace ).To (Equal (namespace ),
94- "expected %s to be in namespace %s in %s report" , result .Kind , namespace , outputFormat )
95- }
96- log .Printf ("✓ %s report: Found %s with apiVersion %s in namespace %s with status %s" ,
97- outputFormat , result .Kind , result .APIVersion , result .Namespace , result .Status )
98- }
99-
100- By (outputFormat + " report: Verify all expected resources were found" )
101- var missingResources []string
102- for kind := range expectedResources {
103- if ! foundResources [kind ] {
104- missingResources = append (missingResources , kind )
105- }
106- }
107- Expect (missingResources ).To (BeEmpty (),
108- "expected to find all resources in %s validation results, missing: %v" , outputFormat , missingResources )
109-
110- By (outputFormat + " output: Verify no failures directory was created" )
111- failuresDir := filepath .Join (validateDir , "failures" )
112- _ , err := os .Stat (failuresDir )
113- Expect (os .IsNotExist (err )).To (BeTrue (),
114- "expected no failures/ directory for all compatible resources" )
115- log .Printf ("No failures directory created" )
116- }
117-
11818var _ = Describe ("Crane validate: all compatible standard resources in offline mode in JSON and YAML formats" , func () {
11919 It ("[MTA-831][MTA-848] Generate and validate crane validate report in JSON and YAML formats" ,
12020 Label ("tier0" , "validate" ), func () {
@@ -218,10 +118,44 @@ var _ = Describe("Crane validate: all compatible standard resources in offline m
218118 for _ , ft := range formats {
219119 By ("Run crane validate in offline mode with output in " + ft .label + " format" )
220120 validateDir := filepath .Join (paths .TempDir , ft .dirSuffix )
221- report , err := runValidateAndParseReport (runner , paths .OutputDir , validateDir , apiSurfaceFile , ft .format )
222- Expect (err ).NotTo (HaveOccurred (), "validate with %s output should succeed for all compatible resources" , ft .label )
223121
224- verifyValidateResults (report , namespace , validateDir , apiSurfaceFile , ft .label )
122+ // Run crane validate command
123+ stdout , err := runner .Validate (ValidateOptions {
124+ InputDir : paths .OutputDir ,
125+ ValidateDir : validateDir ,
126+ APIResourcesFile : apiSurfaceFile ,
127+ OutputFormat : ft .format ,
128+ })
129+ Expect (err ).NotTo (HaveOccurred (), "crane validate should succeed" )
130+ log .Printf ("Validate %s stdout: %s" , ft .format , stdout )
131+
132+ By ("Parse " + ft .label + " validation report" )
133+ var report cranevalidate.ValidationReport
134+ err = utils .ParseValidationReport (validateDir , ft .format , & report )
135+ Expect (err ).NotTo (HaveOccurred (), "should parse %s report" , ft .label )
136+
137+ // Define expectations for this test
138+ expectations := utils.ValidationExpectations {
139+ ValidationReport : cranevalidate.ValidationReport {
140+ Mode : "offline" ,
141+ APIResourcesSource : apiSurfaceFile ,
142+ TotalScanned : 5 ,
143+ Compatible : 5 ,
144+ Incompatible : 0 ,
145+ },
146+ ExpectedResources : map [string ]string {
147+ "Deployment" : "apps/v1" ,
148+ "Service" : "v1" ,
149+ "ConfigMap" : "v1" ,
150+ "Secret" : "v1" ,
151+ "RoleBinding" : "rbac.authorization.k8s.io/v1" ,
152+ },
153+ ExpectedStatus : cranevalidate .StatusOK ,
154+ Namespace : namespace ,
155+ ExpectFailuresDir : false ,
156+ }
157+
158+ utils .VerifyValidateResults (report , validateDir , ft .label , expectations )
225159
226160 log .Printf ("\n " +
227161 "========================================\n " +
0 commit comments