Skip to content

Commit 6e3b131

Browse files
authored
Add rds-variant field to rules files and report outputs (openshift-kni#11)
1 parent f5d9cbd commit 6e3b131

6 files changed

Lines changed: 109 additions & 5 deletions

File tree

internal/report/html.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ func escapeHTML(s string) template.HTML {
2626
type HTMLReport struct {
2727
GeneratedAt string
2828
OCPVersion string
29+
RDSVariant string
2930
Summary SummaryData
3031
MissingCRs []MissingCRGroup
3132
Diffs []DiffData
@@ -172,6 +173,8 @@ func (g *HTMLGenerator) buildHTMLReport(report types.ValidationReport) HTMLRepor
172173
htmlReport.OCPVersion = targetVersion.String()
173174
}
174175

176+
htmlReport.RDSVariant = g.ruleEngine.GetRDSVariant()
177+
175178
htmlReport.MissingCRs, htmlReport.ImpactStats = g.processMissingCRs(report.Summary.ValidationIssues, report.Diffs)
176179
htmlReport.Summary.TotalMissing = htmlReport.ImpactStats.RequiredCRCount + htmlReport.ImpactStats.OptionalCRCount
177180
htmlReport.Diffs, htmlReport.CountViolations = g.processDiffs(report.Diffs, &htmlReport.ImpactStats)
@@ -1341,7 +1344,7 @@ const htmlTemplate = `<!DOCTYPE html>
13411344
<header>
13421345
<h1>RDS Validation Report</h1>
13431346
<div class="meta">
1344-
<span>Generated at {{.GeneratedAt}}{{if .OCPVersion}}. Using target OCP Version: {{.OCPVersion}}{{end}} </span>
1347+
<span>Generated at {{.GeneratedAt}}{{if .RDSVariant}}. Using "{{.RDSVariant}}" RDS rules{{if .OCPVersion}} and target OCP Version: {{.OCPVersion}}{{end}}{{else if .OCPVersion}}. Using target OCP Version: {{.OCPVersion}}{{end}} </span>
13451348
</div>
13461349
</header>
13471350

internal/report/reporting.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ func (g *ReportingGenerator) printHeader() {
8585
fmt.Fprintf(g.writer, "Used target OCP version: %s\n", targetVersion)
8686
}
8787

88+
if variant := g.ruleEngine.GetRDSVariant(); variant != "" {
89+
fmt.Fprintf(g.writer, "Used RDS Variant: %s\n", variant)
90+
}
91+
8892
fmt.Fprintf(g.writer, "Generated: %s\n", time.Now().Format("2006-01-02"))
8993
fmt.Fprintln(g.writer)
9094
}

internal/report/text.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,14 @@ func NewTextGenerator(ruleEngine *rules.Engine) *TextGenerator {
3232
func (g *TextGenerator) Generate(w io.Writer, report types.ValidationReport) error {
3333
g.writer = w
3434

35-
// Show target version if set.
36-
if targetVersion := g.ruleEngine.GetTargetVersion(); !targetVersion.IsZero() {
35+
// Show RDS variant and/or target version if set.
36+
variant := g.ruleEngine.GetRDSVariant()
37+
targetVersion := g.ruleEngine.GetTargetVersion()
38+
if variant != "" && !targetVersion.IsZero() {
39+
fmt.Fprintf(w, "Analyzing using %q RDS rules and target OCP version: %s\n\n", variant, targetVersion)
40+
} else if variant != "" {
41+
fmt.Fprintf(w, "Analyzing using %q RDS rules\n\n", variant)
42+
} else if !targetVersion.IsZero() {
3743
fmt.Fprintf(w, "Analyzing using target OCP version: %s\n\n", targetVersion)
3844
}
3945

internal/rules/engine.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,12 @@ func (e *Engine) GetTargetVersion() OCPVersion {
109109
return e.targetVersion
110110
}
111111

112+
// GetRDSVariant returns the RDS variant declared in the rules file (e.g., "Core", "RAN", "Hub").
113+
// Returns an empty string if the rds-variant field is not set.
114+
func (e *Engine) GetRDSVariant() string {
115+
return e.config.RDSVariant
116+
}
117+
112118
// Evaluate evaluates a DiffCheck against all applicable rules.
113119
// It returns an EvaluationResult containing the overall impact and
114120
// details about which conditions matched.

internal/rules/engine_test.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1458,6 +1458,89 @@ func TestGetters(t *testing.T) {
14581458
t.Error("Expected non-zero target version")
14591459
}
14601460
})
1461+
1462+
t.Run("GetRDSVariant_absent", func(t *testing.T) {
1463+
// testRulesYAML does not include rds-variant; should return empty string.
1464+
if got := engine.GetRDSVariant(); got != "" {
1465+
t.Errorf("expected empty variant, got %q", got)
1466+
}
1467+
})
1468+
}
1469+
1470+
// TestGetRDSVariant tests that the rds-variant YAML field is parsed and returned correctly.
1471+
func TestGetRDSVariant(t *testing.T) {
1472+
tests := []struct {
1473+
name string
1474+
yaml string
1475+
want string
1476+
}{
1477+
{
1478+
name: "variant set to Hub",
1479+
yaml: `
1480+
version: "1.0"
1481+
description: "Hub Rules"
1482+
rds-variant: "Hub"
1483+
settings:
1484+
default_impact: "NeedsReview"
1485+
rules: []
1486+
`,
1487+
want: "Hub",
1488+
},
1489+
{
1490+
name: "variant set to RAN",
1491+
yaml: `
1492+
version: "1.0"
1493+
description: "RAN Rules"
1494+
rds-variant: "RAN"
1495+
settings:
1496+
default_impact: "NeedsReview"
1497+
rules: []
1498+
`,
1499+
want: "RAN",
1500+
},
1501+
{
1502+
name: "variant set to Core",
1503+
yaml: `
1504+
version: "1.0"
1505+
description: "Core Rules"
1506+
rds-variant: "Core"
1507+
settings:
1508+
default_impact: "NeedsReview"
1509+
rules: []
1510+
`,
1511+
want: "Core",
1512+
},
1513+
{
1514+
name: "variant absent",
1515+
yaml: `
1516+
version: "1.0"
1517+
description: "Rules without variant"
1518+
settings:
1519+
default_impact: "NeedsReview"
1520+
rules: []
1521+
`,
1522+
want: "",
1523+
},
1524+
}
1525+
1526+
for _, tt := range tests {
1527+
t.Run(tt.name, func(t *testing.T) {
1528+
tmpDir := t.TempDir()
1529+
rulesPath := filepath.Join(tmpDir, "rules.yaml")
1530+
if err := os.WriteFile(rulesPath, []byte(tt.yaml), 0644); err != nil {
1531+
t.Fatalf("failed to write rules file: %v", err)
1532+
}
1533+
1534+
engine, err := NewEngine(rulesPath)
1535+
if err != nil {
1536+
t.Fatalf("NewEngine failed: %v", err)
1537+
}
1538+
1539+
if got := engine.GetRDSVariant(); got != tt.want {
1540+
t.Errorf("GetRDSVariant() = %q, want %q", got, tt.want)
1541+
}
1542+
})
1543+
}
14611544
}
14621545

14631546
// TestConditionTypes tests different condition types (Any, FoundNotExpected, etc).

internal/rules/types.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ import (
1010
// It contains global settings, rules that match specific CRs, global rules
1111
// that apply to all CRs, and count rules for aggregate checks.
1212
type RulesConfig struct {
13-
Version string `yaml:"version"`
14-
Description string `yaml:"description"`
13+
Version string `yaml:"version"`
14+
Description string `yaml:"description"`
15+
// RDSVariant identifies which RDS variant these rules apply to (e.g., "Core", "RAN", "Hub").
16+
RDSVariant string `yaml:"rds-variant"`
1517
Settings Settings `yaml:"settings"`
1618
LabelAnnotationRules LabelAnnotationRules `yaml:"label_annotation_rules"`
1719
GlobalRules []Rule `yaml:"global_rules"`

0 commit comments

Comments
 (0)