Merge pull request #91 from mattmoyer/prefactor-api-resource-test

Refactor GetAPIResourceList test a bit to prep for IDP CRD changes.
This commit is contained in:
Matt Moyer 2020-09-09 10:46:13 -05:00 committed by GitHub
commit 7207041c37
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -22,11 +22,8 @@ func TestGetAPIResourceList(t *testing.T) {
groups, resources, err := client.Discovery().ServerGroupsAndResources() groups, resources, err := client.Discovery().ServerGroupsAndResources()
require.NoError(t, err) require.NoError(t, err)
groupName := "pinniped.dev" t.Run("has group", func(t *testing.T) {
actualGroup := findGroup(groupName, groups) require.Contains(t, groups, &metav1.APIGroup{
require.NotNil(t, actualGroup)
expectedGroup := &metav1.APIGroup{
Name: "pinniped.dev", Name: "pinniped.dev",
Versions: []metav1.GroupVersionForDiscovery{ Versions: []metav1.GroupVersionForDiscovery{
{ {
@ -38,65 +35,52 @@ func TestGetAPIResourceList(t *testing.T) {
GroupVersion: "pinniped.dev/v1alpha1", GroupVersion: "pinniped.dev/v1alpha1",
Version: "v1alpha1", Version: "v1alpha1",
}, },
} })
require.Equal(t, expectedGroup, actualGroup) })
actualPinnipedResources := findResources("pinniped.dev/v1alpha1", resources) t.Run("has non-CRD APIs", func(t *testing.T) {
require.NotNil(t, actualPinnipedResources) expectResources(t, "pinniped.dev/v1alpha1", resources, []metav1.APIResource{
actualCrdPinnipedResources := findResources("crd.pinniped.dev/v1alpha1", resources) {
require.NotNil(t, actualPinnipedResources)
expectedCredentialRequestAPIResource := metav1.APIResource{
Name: "credentialrequests", Name: "credentialrequests",
Kind: "CredentialRequest", Kind: "CredentialRequest",
Verbs: metav1.Verbs([]string{ Verbs: []string{"create"},
"create",
}),
Namespaced: false, Namespaced: false,
// This is currently an empty string in the response; maybe it should not be // This is currently an empty string in the response; maybe it should not be
// empty? Seems like no harm in keeping it like this for now, but feel free // empty? Seems like no harm in keeping it like this for now, but feel free
// to update in the future if there is a compelling reason to do so. // to update in the future if there is a compelling reason to do so.
SingularName: "", SingularName: "",
} },
})
})
expectedCredentialIssuerConfigResource := metav1.APIResource{ t.Run("has CRD APIs", func(t *testing.T) {
expectResources(t, "crd.pinniped.dev/v1alpha1", resources, []metav1.APIResource{
{
Name: "credentialissuerconfigs", Name: "credentialissuerconfigs",
SingularName: "credentialissuerconfig", SingularName: "credentialissuerconfig",
Namespaced: true, Namespaced: true,
Kind: "CredentialIssuerConfig", Kind: "CredentialIssuerConfig",
Verbs: metav1.Verbs([]string{ Verbs: []string{"delete", "deletecollection", "get", "list", "patch", "create", "update", "watch"},
"delete", "deletecollection", "get", "list", "patch", "create", "update", "watch",
}),
ShortNames: []string{"cic"}, ShortNames: []string{"cic"},
StorageVersionHash: "unknown: to be filled in automatically below", },
})
})
} }
require.Len(t, actualPinnipedResources.APIResources, 1) func expectResources(t *testing.T, groupVersion string, resources []*metav1.APIResourceList, expected []metav1.APIResource) {
require.Equal(t, expectedCredentialRequestAPIResource, actualPinnipedResources.APIResources[0]) var actualResourceList *metav1.APIResourceList
require.Len(t, actualCrdPinnipedResources.APIResources, 1)
actualAPIResource := actualCrdPinnipedResources.APIResources[0]
// workaround because its hard to predict the storage version hash (e.g. "t/+v41y+3e4=")
// so just don't worry about comparing that field
expectedCredentialIssuerConfigResource.StorageVersionHash = actualAPIResource.StorageVersionHash
require.Equal(t, expectedCredentialIssuerConfigResource, actualAPIResource)
}
func findGroup(name string, groups []*metav1.APIGroup) *metav1.APIGroup {
for _, group := range groups {
if group.Name == name {
return group
}
}
return nil
}
func findResources(groupVersion string, resources []*metav1.APIResourceList) *metav1.APIResourceList {
for _, resource := range resources { for _, resource := range resources {
if resource.GroupVersion == groupVersion { if resource.GroupVersion == groupVersion {
return resource actualResourceList = resource.DeepCopy()
} }
} }
return nil require.NotNilf(t, actualResourceList, "could not find groupVersion %s", groupVersion)
// Because its hard to predict the storage version hash (e.g. "t/+v41y+3e4="), we just don't
// worry about comparing that field.
for i := range actualResourceList.APIResources {
actualResourceList.APIResources[i].StorageVersionHash = ""
}
require.EqualValues(t, expected, actualResourceList.APIResources, "unexpected API resources")
} }