1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-29 02:45:06 +00:00

chore: improve unit tests in cli (#8301)

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
This commit is contained in:
Charles-Edouard Brétéché 2023-09-07 04:53:37 +02:00 committed by GitHub
parent bbd137db24
commit b2d29886c1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 374 additions and 28 deletions

View file

@ -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),

View file

@ -0,0 +1,6 @@
package table
type Row struct {
RowCompact `header:"inline"`
Message string `header:"message"`
}

View file

@ -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"`
}

View file

@ -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
}

View file

@ -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)
}
})
}
}

View file

@ -9,8 +9,6 @@ import (
)
func TestNew(t *testing.T) {
type args struct {
}
tests := []struct {
name string
fs billy.Filesystem

View file

@ -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)
}
})
}
}