diff --git a/cmd/cli/kubectl-kyverno/commands/test/output.go b/cmd/cli/kubectl-kyverno/commands/test/output.go index 5aebbc302d..65b247b2fa 100644 --- a/cmd/cli/kubectl-kyverno/commands/test/output.go +++ b/cmd/cli/kubectl-kyverno/commands/test/output.go @@ -48,7 +48,7 @@ func printTestResult( // if checks failed but we were expecting a fail it's considered a success success := ok || (!ok && test.Result == policyreportv1alpha2.StatusFail) row := table.Row{ - CompactRow: table.CompactRow{ + RowCompact: table.RowCompact{ ID: testCount, Policy: color.Policy("", test.Policy), Rule: color.Rule(test.Rule), @@ -76,7 +76,7 @@ func printTestResult( // if not found if len(rows) == 0 { row := table.Row{ - CompactRow: table.CompactRow{ + RowCompact: table.RowCompact{ ID: testCount, Policy: color.Policy("", test.Policy), Rule: color.Rule(test.Rule), diff --git a/cmd/cli/kubectl-kyverno/output/table/row.go b/cmd/cli/kubectl-kyverno/output/table/row.go new file mode 100644 index 0000000000..cacdbe48f5 --- /dev/null +++ b/cmd/cli/kubectl-kyverno/output/table/row.go @@ -0,0 +1,6 @@ +package table + +type Row struct { + RowCompact `header:"inline"` + Message string `header:"message"` +} diff --git a/cmd/cli/kubectl-kyverno/output/table/row_compact.go b/cmd/cli/kubectl-kyverno/output/table/row_compact.go new file mode 100644 index 0000000000..89a783a0a9 --- /dev/null +++ b/cmd/cli/kubectl-kyverno/output/table/row_compact.go @@ -0,0 +1,11 @@ +package table + +type RowCompact struct { + IsFailure bool + ID int `header:"id"` + Policy string `header:"policy"` + Rule string `header:"rule"` + Resource string `header:"resource"` + Result string `header:"result"` + Reason string `header:"reason"` +} diff --git a/cmd/cli/kubectl-kyverno/output/table/table.go b/cmd/cli/kubectl-kyverno/output/table/table.go index 00ce6e8074..7c2f340339 100644 --- a/cmd/cli/kubectl-kyverno/output/table/table.go +++ b/cmd/cli/kubectl-kyverno/output/table/table.go @@ -4,17 +4,6 @@ type Table struct { RawRows []Row } -func (t *Table) Rows(detailed bool) interface{} { - if detailed { - return t.RawRows - } - var rows []CompactRow - for _, row := range t.RawRows { - rows = append(rows, row.CompactRow) - } - return rows -} - func (t *Table) AddFailed(rows ...Row) { for _, row := range rows { if row.IsFailure { @@ -27,17 +16,13 @@ func (t *Table) Add(rows ...Row) { t.RawRows = append(t.RawRows, rows...) } -type CompactRow struct { - IsFailure bool - ID int `header:"id"` - Policy string `header:"policy"` - Rule string `header:"rule"` - Resource string `header:"resource"` - Result string `header:"result"` - Reason string `header:"reason"` -} - -type Row struct { - CompactRow `header:"inline"` - Message string `header:"message"` +func (t *Table) Rows(detailed bool) interface{} { + if detailed { + return t.RawRows + } + var rows []RowCompact + for _, row := range t.RawRows { + rows = append(rows, row.RowCompact) + } + return rows } diff --git a/cmd/cli/kubectl-kyverno/output/table/table_test.go b/cmd/cli/kubectl-kyverno/output/table/table_test.go new file mode 100644 index 0000000000..2151cbad4a --- /dev/null +++ b/cmd/cli/kubectl-kyverno/output/table/table_test.go @@ -0,0 +1,201 @@ +package table + +import ( + "reflect" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestTable_Add(t *testing.T) { + tests := []struct { + name string + RawRows []Row + rows []Row + want int + }{{ + name: "nil", + RawRows: nil, + rows: nil, + want: 0, + }, { + name: "empty", + RawRows: nil, + rows: []Row{}, + want: 0, + }, { + name: "two", + RawRows: nil, + rows: []Row{{}, {}}, + want: 2, + }, { + name: "two + two", + RawRows: []Row{{}, {}}, + rows: []Row{{}, {}}, + want: 4, + }} + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + tr := &Table{ + RawRows: tt.RawRows, + } + tr.Add(tt.rows...) + assert.Equal(t, len(tr.RawRows), tt.want) + }) + } +} + +func TestTable_AddFailed(t *testing.T) { + tests := []struct { + name string + RawRows []Row + rows []Row + want int + }{{ + name: "nil", + RawRows: nil, + rows: nil, + want: 0, + }, { + name: "empty", + RawRows: nil, + rows: []Row{}, + want: 0, + }, { + name: "one", + RawRows: nil, + rows: []Row{{}, {RowCompact: RowCompact{IsFailure: true}}}, + want: 1, + }, { + name: "two + one", + RawRows: []Row{{}, {}}, + rows: []Row{{RowCompact: RowCompact{IsFailure: true}}, {}}, + want: 3, + }} + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + tr := &Table{ + RawRows: tt.RawRows, + } + tr.AddFailed(tt.rows...) + assert.Equal(t, len(tr.RawRows), tt.want) + }) + } +} + +func TestTable_Rows(t *testing.T) { + var nilRows []Row + var nilCompactRows []RowCompact + tests := []struct { + name string + RawRows []Row + detailed bool + want interface{} + }{{ + name: "nil", + want: nilCompactRows, + }, { + name: "nil - detailed", + detailed: true, + want: nilRows, + }, { + name: "compact", + RawRows: []Row{{ + RowCompact: RowCompact{ + ID: 1, + Policy: "policy1", + Rule: "rule1", + Resource: "resource1", + Result: "result1", + Reason: "reason1", + }, + Message: "message1", + }, { + RowCompact: RowCompact{ + IsFailure: true, + ID: 2, + Policy: "policy2", + Rule: "rule2", + Resource: "resource2", + Result: "result2", + Reason: "reason2", + }, + Message: "message2", + }}, + want: []RowCompact{{ + ID: 1, + Policy: "policy1", + Rule: "rule1", + Resource: "resource1", + Result: "result1", + Reason: "reason1", + }, { + IsFailure: true, + ID: 2, + Policy: "policy2", + Rule: "rule2", + Resource: "resource2", + Result: "result2", + Reason: "reason2", + }}, + }, { + name: "detailed", + detailed: true, + RawRows: []Row{{ + RowCompact: RowCompact{ + ID: 1, + Policy: "policy1", + Rule: "rule1", + Resource: "resource1", + Result: "result1", + Reason: "reason1", + }, + Message: "message1", + }, { + RowCompact: RowCompact{ + IsFailure: true, + ID: 2, + Policy: "policy2", + Rule: "rule2", + Resource: "resource2", + Result: "result2", + Reason: "reason2", + }, + Message: "message2", + }}, + want: []Row{{ + RowCompact: RowCompact{ + ID: 1, + Policy: "policy1", + Rule: "rule1", + Resource: "resource1", + Result: "result1", + Reason: "reason1", + }, + Message: "message1", + }, { + RowCompact: RowCompact{ + IsFailure: true, + ID: 2, + Policy: "policy2", + Rule: "rule2", + Resource: "resource2", + Result: "result2", + Reason: "reason2", + }, + Message: "message2", + }}, + }, + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + tr := &Table{ + RawRows: tt.RawRows, + } + if got := tr.Rows(tt.detailed); !reflect.DeepEqual(got, tt.want) { + t.Errorf("Table.Rows() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/cmd/cli/kubectl-kyverno/variables/new_test.go b/cmd/cli/kubectl-kyverno/variables/new_test.go index f4d0ade618..116bdff47e 100644 --- a/cmd/cli/kubectl-kyverno/variables/new_test.go +++ b/cmd/cli/kubectl-kyverno/variables/new_test.go @@ -9,8 +9,6 @@ import ( ) func TestNew(t *testing.T) { - type args struct { - } tests := []struct { name string fs billy.Filesystem diff --git a/cmd/cli/kubectl-kyverno/variables/variables_test.go b/cmd/cli/kubectl-kyverno/variables/variables_test.go index 66ec1b4a3d..67251eeff2 100644 --- a/cmd/cli/kubectl-kyverno/variables/variables_test.go +++ b/cmd/cli/kubectl-kyverno/variables/variables_test.go @@ -5,6 +5,8 @@ import ( "testing" valuesapi "github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/apis/values" + "github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/values" + "github.com/stretchr/testify/assert" ) func TestVariables_HasVariables(t *testing.T) { @@ -89,3 +91,146 @@ func TestVariables_Subresources(t *testing.T) { }) } } + +func TestVariables_NamespaceSelectors(t *testing.T) { + vals, err := values.Load(nil, "../_testdata/values/valid.yaml") + assert.NoError(t, err) + tests := []struct { + name string + values *valuesapi.Values + variables map[string]string + want map[string]Labels + }{{ + name: "nil", + values: nil, + variables: nil, + want: nil, + }, { + name: "empty", + values: &valuesapi.Values{}, + variables: nil, + want: nil, + }, { + name: "values", + values: vals, + variables: nil, + want: map[string]map[string]string{ + "test1": { + "foo.com/managed-state": "managed", + }, + }, + }} + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + v := Variables{ + values: tt.values, + variables: tt.variables, + } + if got := v.NamespaceSelectors(); !reflect.DeepEqual(got, tt.want) { + t.Errorf("Variables.NamespaceSelectors() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestVariables_SetInStore(t *testing.T) { + vals, err := values.Load(nil, "../_testdata/values/valid.yaml") + assert.NoError(t, err) + vals.Policies = append(vals.Policies, valuesapi.Policy{ + Name: "limit-configmap-for-sa", + Rules: []valuesapi.Rule{{ + Name: "rule", + Values: map[string]interface{}{ + "foo": "bar", + }, + ForeachValues: map[string][]interface{}{ + "baz": nil, + }, + }}, + }) + tests := []struct { + name string + values *valuesapi.Values + variables map[string]string + }{{ + name: "nil", + values: nil, + variables: nil, + }, { + name: "empty", + values: &valuesapi.Values{}, + variables: nil, + }, { + name: "values", + values: vals, + variables: nil, + }} + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + v := Variables{ + values: tt.values, + variables: tt.variables, + } + v.SetInStore() + }) + } +} + +func TestVariables_HasPolicyVariables(t *testing.T) { + vals, err := values.Load(nil, "../_testdata/values/valid.yaml") + assert.NoError(t, err) + vals.Policies = append(vals.Policies, valuesapi.Policy{ + Name: "limit-configmap-for-sa", + Rules: []valuesapi.Rule{{ + Name: "rule", + Values: map[string]interface{}{ + "foo": "bar", + }, + ForeachValues: map[string][]interface{}{ + "baz": nil, + }, + }}, + }) + tests := []struct { + name string + values *valuesapi.Values + variables map[string]string + policy string + want bool + }{{ + name: "nil", + values: nil, + variables: nil, + policy: "test", + want: false, + }, { + name: "empty", + values: &valuesapi.Values{}, + variables: nil, + policy: "test", + want: false, + }, { + name: "values - test", + values: vals, + variables: nil, + policy: "test", + want: false, + }, { + name: "values - limit-configmap-for-sa", + values: vals, + variables: nil, + policy: "limit-configmap-for-sa", + want: true, + }} + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + v := Variables{ + values: tt.values, + variables: tt.variables, + } + if got := v.HasPolicyVariables(tt.policy); got != tt.want { + t.Errorf("Variables.HasPolicyVariables() = %v, want %v", got, tt.want) + } + }) + } +}