2023-09-05 17:10:27 +00:00
|
|
|
package userinfo
|
|
|
|
|
|
|
|
import (
|
2023-09-06 22:02:48 +00:00
|
|
|
"os"
|
2023-09-05 17:10:27 +00:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/go-git/go-billy/v5"
|
2023-09-06 22:02:48 +00:00
|
|
|
"github.com/go-git/go-billy/v5/memfs"
|
2024-06-20 09:44:43 +00:00
|
|
|
kyvernov2 "github.com/kyverno/kyverno/api/kyverno/v2"
|
2023-09-18 10:51:35 +00:00
|
|
|
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/apis/v1alpha1"
|
2023-09-05 17:10:27 +00:00
|
|
|
authenticationv1 "k8s.io/api/authentication/v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestLoad(t *testing.T) {
|
2023-09-06 22:02:48 +00:00
|
|
|
fs := func(path string) billy.Filesystem {
|
|
|
|
t.Helper()
|
|
|
|
f := memfs.New()
|
|
|
|
file, err := f.Create("valid.yaml")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
data, err := os.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if _, err := file.Write(data); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return f
|
|
|
|
}
|
2023-09-05 17:10:27 +00:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fs billy.Filesystem
|
|
|
|
path string
|
|
|
|
resourcePath string
|
2023-09-18 10:51:35 +00:00
|
|
|
want *v1alpha1.UserInfo
|
2023-09-05 17:10:27 +00:00
|
|
|
wantErr bool
|
|
|
|
}{{
|
|
|
|
name: "empty",
|
|
|
|
fs: nil,
|
|
|
|
path: "",
|
|
|
|
resourcePath: "",
|
|
|
|
want: nil,
|
|
|
|
wantErr: true,
|
|
|
|
}, {
|
|
|
|
name: "invalid",
|
|
|
|
fs: nil,
|
2023-09-05 17:50:52 +00:00
|
|
|
path: "../_testdata/user-infos/invalid.yaml",
|
2023-09-05 17:10:27 +00:00
|
|
|
resourcePath: "",
|
|
|
|
want: nil,
|
|
|
|
wantErr: true,
|
|
|
|
}, {
|
|
|
|
name: "valid",
|
|
|
|
fs: nil,
|
2023-09-05 17:50:52 +00:00
|
|
|
path: "../_testdata/user-infos/valid.yaml",
|
2023-09-05 17:10:27 +00:00
|
|
|
resourcePath: "",
|
2023-09-18 10:51:35 +00:00
|
|
|
want: &v1alpha1.UserInfo{
|
2024-06-20 09:44:43 +00:00
|
|
|
RequestInfo: kyvernov2.RequestInfo{
|
2023-09-18 10:51:35 +00:00
|
|
|
ClusterRoles: []string{"cluster-admin"},
|
|
|
|
AdmissionUserInfo: authenticationv1.UserInfo{
|
|
|
|
Username: "molybdenum@somecorp.com",
|
|
|
|
},
|
2023-09-05 17:10:27 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
wantErr: false,
|
2023-09-06 22:02:48 +00:00
|
|
|
}, {
|
|
|
|
name: "empty (billy)",
|
|
|
|
fs: fs("../_testdata/user-infos/valid.yaml"),
|
|
|
|
path: "",
|
|
|
|
resourcePath: "",
|
|
|
|
want: nil,
|
|
|
|
wantErr: true,
|
|
|
|
}, {
|
|
|
|
name: "invalid (billy)",
|
|
|
|
fs: fs("../_testdata/user-infos/valid.yaml"),
|
|
|
|
path: "invalid.yaml",
|
|
|
|
resourcePath: "",
|
|
|
|
want: nil,
|
|
|
|
wantErr: true,
|
|
|
|
}, {
|
|
|
|
name: "valid (billy)",
|
|
|
|
fs: fs("../_testdata/user-infos/valid.yaml"),
|
|
|
|
path: "valid.yaml",
|
|
|
|
resourcePath: "",
|
2023-09-18 10:51:35 +00:00
|
|
|
want: &v1alpha1.UserInfo{
|
2024-06-20 09:44:43 +00:00
|
|
|
RequestInfo: kyvernov2.RequestInfo{
|
2023-09-18 10:51:35 +00:00
|
|
|
ClusterRoles: []string{"cluster-admin"},
|
|
|
|
AdmissionUserInfo: authenticationv1.UserInfo{
|
|
|
|
Username: "molybdenum@somecorp.com",
|
|
|
|
},
|
2023-09-06 22:02:48 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
}}
|
2023-09-05 17:10:27 +00:00
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
got, err := Load(tt.fs, tt.path, tt.resourcePath)
|
|
|
|
if (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("Load() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(got, tt.want) {
|
|
|
|
t.Errorf("Load() = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|