Skip to content

Commit da61bb8

Browse files
Add DryCLI status comparison
Ref: https://issues.redhat.com/browse/ACM-17517 Signed-off-by: yiraeChristineKim <[email protected]>
1 parent c3c1716 commit da61bb8

File tree

33 files changed

+878
-7
lines changed

33 files changed

+878
-7
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ require (
116116
google.golang.org/protobuf v1.34.2 // indirect
117117
gopkg.in/evanphx/json-patch.v4 v4.12.0 // indirect
118118
gopkg.in/inf.v0 v0.9.1 // indirect
119-
gopkg.in/yaml.v2 v2.4.0 // indirect
119+
gopkg.in/yaml.v2 v2.4.0
120120
k8s.io/apiserver v0.31.0 // indirect
121121
k8s.io/cli-runtime v0.28.10 // indirect
122122
k8s.io/component-base v0.31.0 // indirect

pkg/dryrun/cmd.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@ import (
1212
)
1313

1414
type DryRunner struct {
15-
policyPath string
16-
messagesPath string
17-
printDiffs bool
18-
statusPath string
19-
mappingsPath string
20-
logPath string
15+
policyPath string
16+
messagesPath string
17+
printDiffs bool
18+
statusPath string
19+
desiredStatus string
20+
mappingsPath string
21+
logPath string
2122
}
2223

2324
var ErrNonCompliant = errors.New("policy is NonCompliant")
@@ -60,6 +61,14 @@ func (d *DryRunner) GetCmd() *cobra.Command {
6061
"Set to false to omit any diffs generated by the policy.",
6162
)
6263

64+
cmd.Flags().StringVarP(
65+
&d.desiredStatus,
66+
"desired-status",
67+
"c",
68+
"",
69+
"The desired Configuration Policy status to compare with result of the dryrun",
70+
)
71+
6372
cmd.Flags().StringVar(
6473
&d.statusPath,
6574
"status-path",

pkg/dryrun/dryrun.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,14 @@ func (d *DryRunner) dryRun(cmd *cobra.Command, args []string) error {
110110
return fmt.Errorf("unable to get the resulting policy state: %w", err)
111111
}
112112

113+
if d.desiredStatus != "" {
114+
if err := d.compareStatus(cmd, cfgPolicy.Status); err != nil {
115+
return fmt.Errorf("unable to compare configuration policy status: %w", err)
116+
}
117+
118+
cmd.Print("\n")
119+
}
120+
113121
if d.statusPath != "" {
114122
if err := d.saveStatus(cfgPolicy.Status); err != nil {
115123
return fmt.Errorf("unable to save the resulting policy state: %w", err)
@@ -467,6 +475,33 @@ func (d *DryRunner) setupReconciler(
467475
return &rec, nil
468476
}
469477

478+
func (d *DryRunner) compareStatus(cmd *cobra.Command, status policyv1.ConfigurationPolicyStatus) error {
479+
reader, err := os.Open(d.desiredStatus)
480+
if err != nil {
481+
return err
482+
}
483+
484+
configBytes, err := io.ReadAll(reader)
485+
if err != nil {
486+
return err
487+
}
488+
489+
inputMap := map[string]interface{}{}
490+
491+
if err := yaml.Unmarshal(configBytes, &inputMap); err != nil {
492+
return err
493+
}
494+
495+
resultMap := toMap(status)
496+
if resultMap == nil {
497+
return errors.New("unable to convert ConfigurationPolicyStatus to unstructured")
498+
}
499+
500+
compareStatus(cmd, inputMap, resultMap)
501+
502+
return nil
503+
}
504+
470505
func (d *DryRunner) saveStatus(status policyv1.ConfigurationPolicyStatus) error {
471506
f, err := os.Create(d.statusPath)
472507
if err != nil {

pkg/dryrun/dryrun_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"bytes"
77
"embed"
88
"errors"
9+
"os"
910
"path"
1011
"strings"
1112
"testing"
@@ -83,6 +84,14 @@ func cliTest(scenarioPath string) func(t *testing.T) {
8384
t.Fatal(err)
8485
}
8586

87+
desiredStatusPath := path.Join(scenarioPath, "desired_status.yaml")
88+
if pathExists(desiredStatusPath) {
89+
err = cmd.Flags().Set("desired-status", desiredStatusPath)
90+
if err != nil {
91+
t.Fatal(err)
92+
}
93+
}
94+
8695
err = cmd.Execute()
8796
if err != nil && !errors.Is(err, ErrNonCompliant) {
8897
t.Fatal(err)
@@ -118,3 +127,14 @@ func cliTest(scenarioPath string) func(t *testing.T) {
118127
}
119128
}
120129
}
130+
131+
func pathExists(path string) bool {
132+
f, err := os.Open(path)
133+
if err != nil {
134+
return false
135+
}
136+
137+
f.Close()
138+
139+
return true
140+
}

0 commit comments

Comments
 (0)