mirror of
https://github.com/vmware-tanzu/pinniped.git
synced 2026-01-07 14:05:50 +00:00
Merge pull request #33 from cfryanr/discovery_doc
Adding discovery document object
This commit is contained in:
100
test/integration/api_discovery_test.go
Normal file
100
test/integration/api_discovery_test.go
Normal file
@@ -0,0 +1,100 @@
|
||||
/*
|
||||
Copyright 2020 VMware, Inc.
|
||||
SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
package integration
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
|
||||
"github.com/suzerain-io/placeholder-name/test/library"
|
||||
)
|
||||
|
||||
func TestGetAPIResourceList(t *testing.T) {
|
||||
client := library.NewPlaceholderNameClientset(t)
|
||||
|
||||
groups, resources, err := client.Discovery().ServerGroupsAndResources()
|
||||
require.NoError(t, err)
|
||||
|
||||
groupName := "placeholder.suzerain-io.github.io"
|
||||
actualGroup := findGroup(groupName, groups)
|
||||
require.NotNil(t, actualGroup)
|
||||
|
||||
expectedGroup := &metav1.APIGroup{
|
||||
Name: "placeholder.suzerain-io.github.io",
|
||||
Versions: []metav1.GroupVersionForDiscovery{
|
||||
{
|
||||
GroupVersion: "placeholder.suzerain-io.github.io/v1alpha1",
|
||||
Version: "v1alpha1",
|
||||
},
|
||||
},
|
||||
PreferredVersion: metav1.GroupVersionForDiscovery{
|
||||
GroupVersion: "placeholder.suzerain-io.github.io/v1alpha1",
|
||||
Version: "v1alpha1",
|
||||
},
|
||||
}
|
||||
require.Equal(t, expectedGroup, actualGroup)
|
||||
|
||||
actualPlaceHolderResources := findResources("placeholder.suzerain-io.github.io/v1alpha1", resources)
|
||||
require.NotNil(t, actualPlaceHolderResources)
|
||||
actualCrdsPlaceHolderResources := findResources("crds.placeholder.suzerain-io.github.io/v1alpha1", resources)
|
||||
require.NotNil(t, actualPlaceHolderResources)
|
||||
|
||||
expectedLoginRequestAPIResource := metav1.APIResource{
|
||||
Name: "loginrequests",
|
||||
Kind: "LoginRequest",
|
||||
Verbs: metav1.Verbs([]string{
|
||||
"create",
|
||||
}),
|
||||
Namespaced: false,
|
||||
|
||||
// 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
|
||||
// to update in the future if there is a compelling reason to do so.
|
||||
SingularName: "",
|
||||
}
|
||||
|
||||
expectedLDCAPIResource := metav1.APIResource{
|
||||
Name: "logindiscoveryconfigs",
|
||||
SingularName: "logindiscoveryconfig",
|
||||
Namespaced: true,
|
||||
Kind: "LoginDiscoveryConfig",
|
||||
Verbs: metav1.Verbs([]string{
|
||||
"delete", "deletecollection", "get", "list", "patch", "create", "update", "watch",
|
||||
}),
|
||||
ShortNames: []string{"ldc"},
|
||||
StorageVersionHash: "unknown: to be filled in automatically below",
|
||||
}
|
||||
|
||||
require.Len(t, actualPlaceHolderResources.APIResources, 1)
|
||||
require.Equal(t, expectedLoginRequestAPIResource, actualPlaceHolderResources.APIResources[0])
|
||||
|
||||
require.Len(t, actualCrdsPlaceHolderResources.APIResources, 1)
|
||||
actualAPIResource := actualCrdsPlaceHolderResources.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
|
||||
expectedLDCAPIResource.StorageVersionHash = actualAPIResource.StorageVersionHash
|
||||
require.Equal(t, expectedLDCAPIResource, 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 {
|
||||
if resource.GroupVersion == groupVersion {
|
||||
return resource
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -7,7 +7,6 @@ package integration
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -52,8 +51,7 @@ O2D8LtWhMbrYy755Fgq4H9s3vCgfvHY1AQ==
|
||||
)
|
||||
|
||||
func TestClient(t *testing.T) {
|
||||
tmcClusterToken := os.Getenv("PLACEHOLDER_NAME_TMC_CLUSTER_TOKEN")
|
||||
require.NotEmptyf(t, tmcClusterToken, "must specify PLACEHOLDER_NAME_TMC_CLUSTER_TOKEN env var for integration tests")
|
||||
tmcClusterToken := library.Getenv(t, "PLACEHOLDER_NAME_TMC_CLUSTER_TOKEN")
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
@@ -7,7 +7,6 @@ package integration
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -20,11 +19,8 @@ import (
|
||||
)
|
||||
|
||||
func TestGetDeployment(t *testing.T) {
|
||||
namespaceName := os.Getenv("PLACEHOLDER_NAME_NAMESPACE")
|
||||
require.NotEmptyf(t, namespaceName, "must specify PLACEHOLDER_NAME_NAMESPACE env var for integration tests")
|
||||
|
||||
deploymentName := os.Getenv("PLACEHOLDER_NAME_DEPLOYMENT")
|
||||
require.NotEmptyf(t, deploymentName, "must specify PLACEHOLDER_NAME_DEPLOYMENT env var for integration tests")
|
||||
namespaceName := library.Getenv(t, "PLACEHOLDER_NAME_NAMESPACE")
|
||||
deploymentName := library.Getenv(t, "PLACEHOLDER_NAME_DEPLOYMENT")
|
||||
|
||||
client := library.NewClientset(t)
|
||||
|
||||
|
||||
78
test/integration/logindiscoveryconfig_test.go
Normal file
78
test/integration/logindiscoveryconfig_test.go
Normal file
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
Copyright 2020 VMware, Inc.
|
||||
SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
package integration
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/client-go/rest"
|
||||
|
||||
crdsplaceholderv1alpha1 "github.com/suzerain-io/placeholder-name-api/pkg/apis/crdsplaceholder/v1alpha1"
|
||||
"github.com/suzerain-io/placeholder-name/test/library"
|
||||
)
|
||||
|
||||
func TestSuccessfulLoginDiscoveryConfig(t *testing.T) {
|
||||
namespaceName := library.Getenv(t, "PLACEHOLDER_NAME_NAMESPACE")
|
||||
|
||||
client := library.NewPlaceholderNameClientset(t)
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
|
||||
config := library.NewClientConfig(t)
|
||||
expectedLDCSpec := expectedLDCSpec(config)
|
||||
configList, err := client.
|
||||
CrdsV1alpha1().
|
||||
LoginDiscoveryConfigs(namespaceName).
|
||||
List(ctx, metav1.ListOptions{})
|
||||
require.NoError(t, err)
|
||||
require.Len(t, configList.Items, 1)
|
||||
require.Equal(t, expectedLDCSpec, &configList.Items[0].Spec)
|
||||
}
|
||||
|
||||
func TestReconcilingLoginDiscoveryConfig(t *testing.T) {
|
||||
namespaceName := library.Getenv(t, "PLACEHOLDER_NAME_NAMESPACE")
|
||||
|
||||
client := library.NewPlaceholderNameClientset(t)
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
|
||||
err := client.
|
||||
CrdsV1alpha1().
|
||||
LoginDiscoveryConfigs(namespaceName).
|
||||
Delete(ctx, "placeholder-name-config", metav1.DeleteOptions{})
|
||||
require.NoError(t, err)
|
||||
|
||||
config := library.NewClientConfig(t)
|
||||
expectedLDCSpec := expectedLDCSpec(config)
|
||||
|
||||
var actualLDC *crdsplaceholderv1alpha1.LoginDiscoveryConfig
|
||||
for i := 0; i < 10; i++ {
|
||||
actualLDC, err = client.
|
||||
CrdsV1alpha1().
|
||||
LoginDiscoveryConfigs(namespaceName).
|
||||
Get(ctx, "placeholder-name-config", metav1.GetOptions{})
|
||||
if err == nil {
|
||||
break
|
||||
}
|
||||
time.Sleep(time.Millisecond * 750)
|
||||
}
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, expectedLDCSpec, &actualLDC.Spec)
|
||||
}
|
||||
|
||||
func expectedLDCSpec(config *rest.Config) *crdsplaceholderv1alpha1.LoginDiscoveryConfigSpec {
|
||||
return &crdsplaceholderv1alpha1.LoginDiscoveryConfigSpec{
|
||||
Server: config.Host,
|
||||
CertificateAuthorityData: base64.StdEncoding.EncodeToString(config.TLSClientConfig.CAData),
|
||||
}
|
||||
}
|
||||
@@ -8,7 +8,6 @@ package integration
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -58,8 +57,7 @@ func addTestClusterRoleBinding(ctx context.Context, t *testing.T, adminClient ku
|
||||
}
|
||||
|
||||
func TestSuccessfulLoginRequest(t *testing.T) {
|
||||
tmcClusterToken := os.Getenv("PLACEHOLDER_NAME_TMC_CLUSTER_TOKEN")
|
||||
require.NotEmptyf(t, tmcClusterToken, "must specify PLACEHOLDER_NAME_TMC_CLUSTER_TOKEN env var for integration tests")
|
||||
tmcClusterToken := library.Getenv(t, "PLACEHOLDER_NAME_TMC_CLUSTER_TOKEN")
|
||||
|
||||
response, err := makeRequest(t, v1alpha1.LoginRequestSpec{
|
||||
Type: v1alpha1.TokenLoginCredentialType,
|
||||
@@ -180,78 +178,3 @@ func TestLoginRequest_ShouldFailWhenRequestDoesNotIncludeToken(t *testing.T) {
|
||||
require.Empty(t, response.Spec)
|
||||
require.Nil(t, response.Status.Credential)
|
||||
}
|
||||
|
||||
func TestGetAPIResourceList(t *testing.T) {
|
||||
client := library.NewPlaceholderNameClientset(t)
|
||||
|
||||
groups, resources, err := client.Discovery().ServerGroupsAndResources()
|
||||
require.NoError(t, err)
|
||||
|
||||
groupName := "placeholder.suzerain-io.github.io"
|
||||
actualGroup := findGroup(groupName, groups)
|
||||
require.NotNil(t, actualGroup)
|
||||
|
||||
expectedGroup := &metav1.APIGroup{
|
||||
Name: "placeholder.suzerain-io.github.io",
|
||||
Versions: []metav1.GroupVersionForDiscovery{
|
||||
{
|
||||
GroupVersion: "placeholder.suzerain-io.github.io/v1alpha1",
|
||||
Version: "v1alpha1",
|
||||
},
|
||||
},
|
||||
PreferredVersion: metav1.GroupVersionForDiscovery{
|
||||
GroupVersion: "placeholder.suzerain-io.github.io/v1alpha1",
|
||||
Version: "v1alpha1",
|
||||
},
|
||||
}
|
||||
require.Equal(t, expectedGroup, actualGroup)
|
||||
|
||||
resourceGroupVersion := "placeholder.suzerain-io.github.io/v1alpha1"
|
||||
actualResources := findResources(resourceGroupVersion, resources)
|
||||
require.NotNil(t, actualResources)
|
||||
|
||||
expectedResources := &metav1.APIResourceList{
|
||||
TypeMeta: metav1.TypeMeta{
|
||||
Kind: "APIResourceList",
|
||||
APIVersion: "v1",
|
||||
},
|
||||
GroupVersion: "placeholder.suzerain-io.github.io/v1alpha1",
|
||||
APIResources: []metav1.APIResource{
|
||||
{
|
||||
Name: "loginrequests",
|
||||
Kind: "LoginRequest",
|
||||
SingularName: "", // TODO(akeesler): what should this be?
|
||||
Verbs: metav1.Verbs([]string{
|
||||
"create",
|
||||
}),
|
||||
},
|
||||
},
|
||||
}
|
||||
require.Equal(t, expectedResources, actualResources)
|
||||
}
|
||||
|
||||
func TestGetAPIVersion(t *testing.T) {
|
||||
client := library.NewPlaceholderNameClientset(t)
|
||||
|
||||
version, err := client.Discovery().ServerVersion()
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, version) // TODO(akeesler): what can we assert here?
|
||||
}
|
||||
|
||||
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 {
|
||||
if resource.GroupVersion == groupVersion {
|
||||
return resource
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
22
test/library/env.go
Normal file
22
test/library/env.go
Normal file
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
Copyright 2020 VMware, Inc.
|
||||
SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
package library
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// Getenv gets the environment variable with key and asserts that it is not
|
||||
// empty. It returns the value of the environment variable.
|
||||
func Getenv(t *testing.T, key string) string {
|
||||
t.Helper()
|
||||
value := os.Getenv(key)
|
||||
require.NotEmptyf(t, value, "must specify %s env var for integration tests", key)
|
||||
return value
|
||||
}
|
||||
Reference in New Issue
Block a user