Skip to content

Commit 9807b9a

Browse files
committed
proper-docs
1 parent 9bde9f6 commit 9807b9a

File tree

135 files changed

+73094
-671
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

135 files changed

+73094
-671
lines changed

cmd/certsuite/check/check.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ var (
1313
}
1414
)
1515

16+
// NewCommand creates the root check command.
17+
//
18+
// It constructs a new cobra.Command instance configured for the certsuite
19+
// checking functionality. The returned command is ready to have subcommands
20+
// added to it and can be executed as part of the certsuite CLI. No arguments
21+
// are required; the function returns a pointer to the created *cobra.Command.
1622
func NewCommand() *cobra.Command {
1723
checkCmd.AddCommand(imagecert.NewCommand())
1824
checkCmd.AddCommand(results.NewCommand())

cmd/certsuite/check/image_cert_status/image_cert_status.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ var checkImageCertStatusCmd = &cobra.Command{
3030
RunE: checkImageCertStatus,
3131
}
3232

33+
// checkImageCertStatus retrieves image information from flags, validates it,
34+
// checks whether the container is certified, and prints the status.
35+
//
36+
// It expects command-line flags for registry, namespace, repository, and tag,
37+
// which are obtained via GetString on the cobra.Command flags.
38+
// The function validates these inputs, then calls IsContainerCertified
39+
// to determine certification status. It outputs a formatted message
40+
// indicating whether the image is certified or not, using color helpers
41+
// for success (green) or failure (red). If validation fails or an error
42+
// occurs during certification check, it returns an error.
3343
func checkImageCertStatus(cmd *cobra.Command, _ []string) error {
3444
imageName, _ := cmd.Flags().GetString("name")
3545
imageRegistry, _ := cmd.Flags().GetString("registry")
@@ -64,6 +74,11 @@ func checkImageCertStatus(cmd *cobra.Command, _ []string) error {
6474
return nil
6575
}
6676

77+
// NewCommand creates and configures a cobra command for checking image certificate status.
78+
//
79+
// It returns a pointer to a cobra.Command that includes persistent flags for image,
80+
// registry, namespace, and other options. The function sets up flag requirements
81+
// and mutual exclusivity rules before returning the configured command.
6782
func NewCommand() *cobra.Command {
6883
checkImageCertStatusCmd.PersistentFlags().String("name", "", "name of the image to verify")
6984
checkImageCertStatusCmd.PersistentFlags().String("registry", "", "registry where the image is stored")

cmd/certsuite/check/results/results.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,23 @@ const (
2626
resultMiss = "MISSING"
2727
)
2828

29+
// TestCaseList holds the names of test cases grouped by result status.
30+
//
31+
// It contains three slices of strings: Fail, Pass, and Skip,
32+
// each holding the identifiers of test cases that ended with
33+
// the corresponding outcome during a certsuite run.
2934
type TestCaseList struct {
3035
Pass []string `yaml:"pass"`
3136
Fail []string `yaml:"fail"`
3237
Skip []string `yaml:"skip"`
3338
}
3439

40+
// TestResults represents the collection of test case outcomes.
41+
//
42+
// It embeds a TestCaseList to provide access to individual test cases and their
43+
// execution status. The struct is used to aggregate results from multiple
44+
// test runs within the certsuite command-line tool, enabling reporting,
45+
// filtering, and further analysis of test outcomes.
3546
type TestResults struct {
3647
TestCaseList `yaml:"testCases"`
3748
}
@@ -42,6 +53,12 @@ var checkResultsCmd = &cobra.Command{
4253
RunE: checkResults,
4354
}
4455

56+
// checkResults compares test results with expected values and writes a report.
57+
//
58+
// It retrieves command flags, loads the stored test results from the database,
59+
// compares them against expected outcomes, and optionally generates a template
60+
// file. Mismatches are printed to stdout and cause the program to exit with
61+
// an error status. The function returns an error if any operation fails.
4562
func checkResults(cmd *cobra.Command, _ []string) error {
4663
templateFileName, _ := cmd.Flags().GetString("template")
4764
generateTemplate, _ := cmd.Flags().GetBool("generate-template")
@@ -90,6 +107,14 @@ func checkResults(cmd *cobra.Command, _ []string) error {
90107
return nil
91108
}
92109

110+
// getTestResultsDB reads a test results file and returns a map of test identifiers to their outcome.
111+
//
112+
// getTestResultsDB parses the specified file path, expecting lines formatted as
113+
// "testID: result". It returns a map where each key is the test identifier and
114+
// the value is one of the predefined result constants (pass, fail, skip, miss).
115+
// If the file cannot be opened or any parsing error occurs, it returns an
116+
// error describing the problem. The function does not modify the input file
117+
// and closes it before returning.
93118
func getTestResultsDB(logFileName string) (map[string]string, error) {
94119
resultsDB := make(map[string]string)
95120

@@ -124,6 +149,12 @@ func getTestResultsDB(logFileName string) (map[string]string, error) {
124149
return resultsDB, nil
125150
}
126151

152+
// getExpectedTestResults reads a JSON file containing expected test results and returns them as a map.
153+
//
154+
// It takes the path to a template file, opens and parses its contents into a
155+
// map where keys are test identifiers and values are expected result strings.
156+
// The function returns the populated map or an error if the file cannot be read,
157+
// the data cannot be unmarshaled, or any other issue occurs.
127158
func getExpectedTestResults(templateFileName string) (map[string]string, error) {
128159
templateFile, err := os.ReadFile(templateFileName)
129160
if err != nil {
@@ -150,6 +181,12 @@ func getExpectedTestResults(templateFileName string) (map[string]string, error)
150181
return expectedTestResults, nil
151182
}
152183

184+
// printTestResultsMismatch reports mismatched test results between two sets.
185+
//
186+
// It takes three arguments: a slice of test names, a map of expected results,
187+
// and a map of actual results. The function compares each test's expected
188+
// outcome with the actual outcome, printing a formatted table that shows
189+
// which tests passed, failed, were skipped, or missed. No value is returned.
153190
func printTestResultsMismatch(mismatchedTestCases []string, actualResults, expectedResults map[string]string) {
154191
fmt.Printf("\n")
155192
fmt.Println(strings.Repeat("-", 96)) //nolint:mnd // table line
@@ -169,6 +206,12 @@ func printTestResultsMismatch(mismatchedTestCases []string, actualResults, expec
169206
}
170207
}
171208

209+
// generateTemplateFile writes a JSON file from the provided map of strings.
210+
//
211+
// It creates a temporary buffer, encodes the map as pretty‑printed JSON,
212+
// and writes the result to a file named by TestResultsTemplateFileName
213+
// with permissions specified by TestResultsTemplateFilePermissions.
214+
// If any step fails, it returns an error describing the problem.
172215
func generateTemplateFile(resultsDB map[string]string) error {
173216
var resultsTemplate TestResults
174217
for testCase, result := range resultsDB {
@@ -201,6 +244,14 @@ func generateTemplateFile(resultsDB map[string]string) error {
201244
return nil
202245
}
203246

247+
// NewCommand creates the command used to run checks on a set of test results.
248+
//
249+
// It returns a *cobra.Command configured with persistent flags that control
250+
// output formatting, filtering, and other behavior. The command exposes flags
251+
// for specifying output files, choosing which result states to display,
252+
// enabling or disabling verbose output, and selecting whether to mark
253+
// mutually exclusive options. The returned command is intended to be added
254+
// to the main application’s root command hierarchy.
204255
func NewCommand() *cobra.Command {
205256
checkResultsCmd.PersistentFlags().String("template", "expected_results.yaml", "reference YAML template with the expected results")
206257
checkResultsCmd.PersistentFlags().String("log-file", "certsuite.log", "log file of the Certsuite execution")

cmd/certsuite/claim/claim.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ var (
1313
}
1414
)
1515

16+
// NewCommand creates the root command for the claim subcommand.
17+
//
18+
// It constructs a new cobra.Command instance, configures it with
19+
// usage information and registers any child commands by calling AddCommand.
20+
// The returned *cobra.Command is ready to be added to the main application
21+
// command tree.
1622
func NewCommand() *cobra.Command {
1723
claimCommand.AddCommand(compare.NewCommand())
1824
claimCommand.AddCommand(show.NewCommand())

cmd/certsuite/claim/compare/compare.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ var (
8686
}
8787
)
8888

89+
// NewCommand creates a cobra command for comparing two claim files.
90+
//
91+
// It defines flags for the first and second claim file paths, marks them as required,
92+
// and sets up the command to execute the comparison logic when run.
93+
// The function returns a fully configured *cobra.Command ready to be added to a root command.
8994
func NewCommand() *cobra.Command {
9095
claimCompareFiles.Flags().StringVarP(
9196
&Claim1FilePathFlag, "claim1", "1", "",
@@ -109,6 +114,15 @@ func NewCommand() *cobra.Command {
109114
return claimCompareFiles
110115
}
111116

117+
// claimCompare compares two claim files and reports differences.
118+
//
119+
// It accepts a cobra.Command pointer and a slice of string arguments, typically
120+
// the command context and any positional parameters. The function retrieves the
121+
// file paths from the global flags Claim1FilePathFlag and Claim2FilePathFlag,
122+
// invokes claimCompareFilesfunc to perform the comparison, and handles errors.
123+
// On failure it calls Fatal to terminate the program with an error message.
124+
// The returned error is nil if the comparison succeeds or a non‑nil value
125+
// indicating a problem during execution.
112126
func claimCompare(_ *cobra.Command, _ []string) error {
113127
err := claimCompareFilesfunc(Claim1FilePathFlag, Claim2FilePathFlag)
114128
if err != nil {
@@ -117,6 +131,14 @@ func claimCompare(_ *cobra.Command, _ []string) error {
117131
return nil
118132
}
119133

134+
// claimCompareFilesfunc compares two claim files and reports differences.
135+
//
136+
// It reads the file at path1, unmarshals it into a Claim object,
137+
// then reads the file at path2 and unmarshals that as well.
138+
// The function uses Compare to compute a diff report between the
139+
// two claims. If any read or unmarshal error occurs, an error is
140+
// returned immediately. On success, the diff report is printed to
141+
// standard output and nil is returned.
120142
func claimCompareFilesfunc(claim1, claim2 string) error {
121143
// readfiles
122144
claimdata1, err := os.ReadFile(claim1)
@@ -161,6 +183,12 @@ func claimCompareFilesfunc(claim1, claim2 string) error {
161183
return nil
162184
}
163185

186+
// unmarshalClaimFile reads a claim file from bytes and returns the parsed schema.
187+
//
188+
// It accepts a byte slice containing the JSON representation of a claim
189+
// document, unmarshals it into a claim.Schema structure using the standard
190+
// encoding/json package, and returns that schema along with any error
191+
// encountered during decoding. If successful, the returned error is nil.
164192
func unmarshalClaimFile(claimdata []byte) (claim.Schema, error) {
165193
var claimDataResult claim.Schema
166194
err := json.Unmarshal(claimdata, &claimDataResult)

cmd/certsuite/claim/compare/configurations/configurations.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,20 @@ import (
77
"github.com/redhat-best-practices-for-k8s/certsuite/cmd/certsuite/pkg/claim"
88
)
99

10+
// AbnormalEventsCount holds the number of abnormal events for two claims.
11+
//
12+
// It contains integer fields Claim1 and Claim2 that represent the count of
13+
// abnormal events detected for each respective claim. The String method
14+
// returns a formatted string summarizing these counts.
1015
type AbnormalEventsCount struct {
1116
Claim1 int `json:"claim1"`
1217
Claim2 int `json:"claim2"`
1318
}
1419

20+
// String returns a human‑readable representation of the abnormal events count.
21+
//
22+
// It formats the internal counters into a single string, allowing easy
23+
// printing or logging of the number of abnormal events recorded.
1524
func (c *AbnormalEventsCount) String() string {
1625
const (
1726
rowHeaderFmt = "%-12s%-s\n"
@@ -25,11 +34,23 @@ func (c *AbnormalEventsCount) String() string {
2534
return str
2635
}
2736

37+
// DiffReport represents the result of comparing two configurations.
38+
//
39+
// It contains the number of abnormal events detected during the comparison
40+
// in the AbnormalEvents field, and a pointer to a diff.Diffs value that
41+
// describes the differences between the two configuration objects.
42+
// The String method returns a human‑readable summary of the report.
2843
type DiffReport struct {
2944
Config *diff.Diffs `json:"CertSuiteConfig"`
3045
AbnormalEvents AbnormalEventsCount `json:"abnormalEventsCount"`
3146
}
3247

48+
// String returns a human‑readable representation of the DiffReport.
49+
//
50+
// It formats the differences between two configuration sets into a single
51+
// string, suitable for printing or logging. The returned value is a plain
52+
// text description that lists added, removed, and changed items in a
53+
// readable layout.
3354
func (d *DiffReport) String() string {
3455
str := "CONFIGURATIONS\n"
3556
str += "--------------\n\n"
@@ -42,6 +63,11 @@ func (d *DiffReport) String() string {
4263
return str
4364
}
4465

66+
// GetDiffReport produces a diff report between two configuration sets.
67+
//
68+
// It compares the first configuration set with the second and returns a DiffReport
69+
// that summarizes differences, additions, and removals. The returned value contains
70+
// details of what changed and is nil if an error occurs during comparison.
4571
func GetDiffReport(claim1Configurations, claim2Configurations *claim.Configurations) *DiffReport {
4672
return &DiffReport{
4773
Config: diff.Compare("Cert Suite Configuration", claim1Configurations.Config, claim2Configurations.Config, nil),

cmd/certsuite/claim/compare/diff/diff.go

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ import (
88
"strings"
99
)
1010

11-
// Diffs holds the differences between two interface{} objects that have
12-
// been obtained by unmarshalling JSON strings.
11+
// Diffs holds the differences between two JSON objects that have been unmarshalled into interface{} values.
12+
//
13+
// It records three slices: Fields contains field-by-field differences with their values from each object; FieldsInClaim1Only lists fields present only in the first object; and FieldsInClaim2Only lists fields present only in the second. The Name field can be used to label the comparison when rendering or logging.
1314
type Diffs struct {
1415
// Name of the json object whose diffs are stored here.
1516
// It will be used when serializing the data in table format.
@@ -21,32 +22,24 @@ type Diffs struct {
2122
FieldsInClaim2Only []string
2223
}
2324

24-
// FieldDIff holds the field path and the values from both claim files
25-
// that have been found to be different.
25+
// FieldDiff holds information about a field that differs between two claim files.
26+
//
27+
// It stores the path of the differing field and the corresponding values from each claim file.
2628
type FieldDiff struct {
2729
FieldPath string `json:"field"`
2830
Claim1Value interface{} `json:"claim1Value"`
2931
Claim2Value interface{} `json:"claim2Value"`
3032
}
3133

32-
// Stringer method. The output string is a table like this:
33-
// <name>: Differences
34-
// FIELD CLAIM 1 CLAIM 2
35-
// /jsonpath/to/field1 value1 value2
36-
// /jsonpath/to/another/field2 value3 value4
37-
// ...
38-
//
39-
// <name>: Only in CLAIM 1
40-
// /jsonpath/to/field/in/claim1/only
41-
// ...
42-
//
43-
// <name>: Only in CLAIM 2
44-
// /jsonpath/to/field/in/claim2/only
45-
// ...
34+
// String returns a formatted table showing the differences between two claims.
4635
//
47-
// Where <name> is replaced by the value of d.Name.
48-
// The columns "FIELD" and "CLAIM 1" have a dynamic width that depends
49-
// on the longest field path and longest value.
36+
// It generates a multi-line string that lists fields present in both claims with
37+
// their differing values, as well as fields unique to each claim.
38+
// The output is organized into sections titled "<name>: Differences",
39+
// "<name>: Only in CLAIM 1", and "<name>: Only in CLAIM 2", where <name> is the
40+
// value of d.Name. Columns are padded to accommodate the longest field path
41+
// and value, ensuring a readable table layout. This method implements the
42+
// Stringer interface for Diffs.
5043
func (d *Diffs) String() string {
5144
const (
5245
noDiffs = "<none>"
@@ -110,13 +103,13 @@ func (d *Diffs) String() string {
110103
return str
111104
}
112105

113-
// Compares to interface{} objects obtained through json.Unmarshal() and returns
114-
// a pointer to a Diffs object.
115-
// A simple filtering of json subtrees can be achieved using the filters slice parameter.
116-
// This might be helpful with big json trees that could have too many potential differences,
117-
// but we want to get just the differences for some custom nodes/subtrees.
118-
// E.g.: filters = []string{"labels"} : only the nodes/subtrees under all the
119-
// labels nodes/branches will be traversed and compared.
106+
// Compare compares two interface{} objects obtained through json.Unmarshal() and returns a pointer to a Diffs object.
107+
//
108+
// It accepts a JSON path string, the left and right interface values to compare,
109+
// and an optional slice of filter strings that restrict traversal to specific subtrees.
110+
// Only nodes whose paths match any filter are examined; all other parts of the trees are ignored.
111+
// The function walks both structures in parallel, records differences into a Diffs instance,
112+
// and returns a pointer to that instance for further inspection.
120113
func Compare(objectName string, claim1Object, claim2Object interface{}, filters []string) *Diffs {
121114
objectsDiffs := Diffs{Name: objectName}
122115

@@ -163,13 +156,26 @@ func Compare(objectName string, claim1Object, claim2Object interface{}, filters
163156
return &objectsDiffs
164157
}
165158

159+
// field represents a leaf node in a nested structure, storing its path and value.
160+
//
161+
// It is used internally by the traversal logic to capture each terminal field
162+
// encountered during a recursive walk of an arbitrary data structure.
163+
// The Path field holds the dot‑separated string that identifies the location
164+
// of the value within the original object. Value contains the actual leaf
165+
// value, which may be any Go type.
166166
type field struct {
167167
Path string
168168
Value interface{}
169169
}
170170

171-
// Helper function that traverses recursively a node to return a list
172-
// of each field (leaf) path and its value.
171+
// traverse recursively walks a node, returning each leaf field's path and value.
172+
//
173+
// It accepts an interface{} representing the current node, a string prefix
174+
// for building the field path, and a slice of strings that holds the
175+
// accumulated paths so far. The function returns a slice of field structs,
176+
// each containing a full path to a leaf node and its corresponding value.
177+
// This helper is used to flatten nested structures into a list of
178+
// key/value pairs for comparison purposes.
173179
func traverse(node interface{}, path string, filters []string) []field {
174180
if node == nil {
175181
return nil

0 commit comments

Comments
 (0)