Skip to content

Commit

Permalink
Convert tests to check.v1 format
Browse files Browse the repository at this point in the history
  • Loading branch information
sukhil-suresh committed Mar 1, 2024
1 parent d71b0c9 commit b8e8f39
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 42 deletions.
2 changes: 1 addition & 1 deletion pkg/ksprig/fipsonly_sprig.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ var fipsNonCompliantFuncs = map[string]interface{}{
}
return fn(typ), nil
}
return "", NewUnsupportedSprigUsageErr(fmt.Sprintf("genPrivateKey for %s key", typ))
return "", NewUnsupportedSprigUsageErr(fmt.Sprintf("genPrivateKey for %s", typ))
},

"htpasswd": func(username string, password string) (string, error) {
Expand Down
86 changes: 45 additions & 41 deletions pkg/ksprig/fipsonly_sprig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,64 +20,74 @@ import (
"testing"
"text/template"

. "gopkg.in/check.v1"

"github.com/kanisterio/kanister/pkg/ksprig"
)

func TestTemplateErrorsForUnsupportedFuncs(t *testing.T) {
type FipsOnlySprigSuite struct{}

var _ = Suite(&FipsOnlySprigSuite{})

func TestFipsOnlySprigSuite(t *testing.T) { TestingT(t) }

func (f *FipsOnlySprigSuite) TestUnsupportedTxtFuncMapUsage(c *C) {
funcMap := ksprig.TxtFuncMap()

testCases := []struct {
function string
templateText string
usageErr string
}{
{
function: "bcrypt",
templateText: "{{bcrypt \"password\"}}",
usageErr: "bcrypt",
},
{
function: "derivePassword",
templateText: "{{derivePassword 1 \"long\" \"password\" \"user\" \"example.com\"}}",
usageErr: "derivePassword",
},
{
function: "genPrivateKey",
templateText: "{{genPrivateKey \"dsa\"}}",
usageErr: "genPrivateKey for dsa",
},
{
function: "htpasswd",
templateText: "{{htpasswd \"username\" \"password\"}}",
usageErr: "htpasswd",
},
}

for _, tc := range testCases {
funcMap := ksprig.TxtFuncMap()

t.Run(tc.function, func(t *testing.T) {
if _, ok := funcMap[tc.function]; !ok {
t.Skipf("Function %s is not supported by sprig.TxtFuncMap()", tc.function)
}

temp, err := template.New("test").Funcs(funcMap).Parse(tc.templateText)
if err != nil {
t.Fatalf("Unexpected template parse error: %s", err)
}

err = temp.Execute(nil, "")
if err == nil {
t.Fatal("Unexpected success for template execution")
}

if !errors.As(err, &ksprig.UnsupportedSprigUsageErr{}) {
t.Fatalf("Expected error of type UnsupportedSprigFuncErr")
}
})
if _, ok := funcMap[tc.function]; !ok {
c.Logf("Skipping test of %s since the tested sprig version does not support it", tc.function)
continue
}
c.Logf("Testing %s", tc.function)

temp, err := template.New("test").Funcs(funcMap).Parse(tc.templateText)
c.Assert(err, IsNil)

err = temp.Execute(nil, "")

var sprigErr ksprig.UnsupportedSprigUsageErr
c.Assert(errors.As(err, &sprigErr), Equals, true)
c.Assert(sprigErr.Usage, Equals, tc.usageErr)
}
}

func TestTemplateWorksForSupportedFuncs(t *testing.T) {
func (f *FipsOnlySprigSuite) TestSupportedTxtFuncMapUsage(c *C) {
funcMap := ksprig.TxtFuncMap()

testCases := []struct {
description string
function string
templateText string
}{
// The supported funcs are not limited to these test cases
// The supported usages are not limited to these test cases
{
description: "genPrivateKey for rsa key",
function: "genPrivateKey",
Expand All @@ -96,22 +106,16 @@ func TestTemplateWorksForSupportedFuncs(t *testing.T) {
}

for _, tc := range testCases {
funcMap := ksprig.TxtFuncMap()

t.Run(tc.description, func(t *testing.T) {
if _, ok := funcMap[tc.function]; !ok {
t.Skipf("Function %s is not supported by sprig.TxtFuncMap()", tc.function)
}

temp, err := template.New("test").Funcs(funcMap).Parse(tc.templateText)
if err != nil {
t.Fatalf("Unexpected template parse error: %s", err)
}

err = temp.Execute(&strings.Builder{}, "")
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
})
if _, ok := funcMap[tc.function]; !ok {
c.Logf("Skipping test of %s since the tested sprig version does not support it", tc.function)
continue
}
c.Logf("Testing %s", tc.description)

temp, err := template.New("test").Funcs(funcMap).Parse(tc.templateText)
c.Assert(err, IsNil)

err = temp.Execute(&strings.Builder{}, "")
c.Assert(err, IsNil)
}
}

0 comments on commit b8e8f39

Please sign in to comment.