mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-28 05:57:25 +00:00
Adding a test for runtime-config to federation-apiserver
This commit is contained in:
parent
e8fb6d1c47
commit
c778f07e85
@ -35,18 +35,29 @@ import (
|
|||||||
"k8s.io/kubernetes/test/integration/federation/framework"
|
"k8s.io/kubernetes/test/integration/federation/framework"
|
||||||
)
|
)
|
||||||
|
|
||||||
var groupVersions = []schema.GroupVersion{
|
// List of group versions that are enabled by default.
|
||||||
|
var enabledGroupVersions = []schema.GroupVersion{
|
||||||
fed_v1b1.SchemeGroupVersion,
|
fed_v1b1.SchemeGroupVersion,
|
||||||
ext_v1b1.SchemeGroupVersion,
|
ext_v1b1.SchemeGroupVersion,
|
||||||
// batch_v1.SchemeGroupVersion,
|
|
||||||
// autoscaling_v1.SchemeGroupVersion,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type apiTestFunc func(t *testing.T, host string)
|
// List of group versions that are disabled by default.
|
||||||
|
var disabledGroupVersions = []schema.GroupVersion{
|
||||||
|
batch_v1.SchemeGroupVersion,
|
||||||
|
autoscaling_v1.SchemeGroupVersion,
|
||||||
|
}
|
||||||
|
|
||||||
func TestFederationAPI(t *testing.T) {
|
type apiTestFunc func(t *testing.T, host string, expectedGroupVersions []schema.GroupVersion)
|
||||||
|
|
||||||
|
func testFederationAPI(t *testing.T, runtimeConfig string, expectedGroupVersions []schema.GroupVersion) {
|
||||||
f := &framework.FederationAPIFixture{}
|
f := &framework.FederationAPIFixture{}
|
||||||
|
if runtimeConfig == "" {
|
||||||
f.SetUp(t)
|
f.SetUp(t)
|
||||||
|
} else {
|
||||||
|
runOptions := framework.GetRunOptions()
|
||||||
|
runOptions.APIEnablement.RuntimeConfig.Set(runtimeConfig)
|
||||||
|
f.SetUpWithRunOptions(t, runOptions)
|
||||||
|
}
|
||||||
defer f.TearDown(t)
|
defer f.TearDown(t)
|
||||||
|
|
||||||
testCases := map[string]apiTestFunc{
|
testCases := map[string]apiTestFunc{
|
||||||
@ -58,11 +69,23 @@ func TestFederationAPI(t *testing.T) {
|
|||||||
}
|
}
|
||||||
for testName, testFunc := range testCases {
|
for testName, testFunc := range testCases {
|
||||||
t.Run(testName, func(t *testing.T) {
|
t.Run(testName, func(t *testing.T) {
|
||||||
testFunc(t, f.Host)
|
testFunc(t, f.Host, expectedGroupVersions)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Verifies that only default APIs are enabled when no runtime config is set.
|
||||||
|
func TestDefaultRun(t *testing.T) {
|
||||||
|
testFederationAPI(t, "", enabledGroupVersions)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verifies that all APIs are enabled when runtime config is set to all.
|
||||||
|
func TestRunWithRuntimeConfigAll(t *testing.T) {
|
||||||
|
expectedGroupVersions := enabledGroupVersions
|
||||||
|
expectedGroupVersions = append(enabledGroupVersions, disabledGroupVersions...)
|
||||||
|
testFederationAPI(t, "api/all=true", expectedGroupVersions)
|
||||||
|
}
|
||||||
|
|
||||||
func readResponse(serverURL string) ([]byte, error) {
|
func readResponse(serverURL string) ([]byte, error) {
|
||||||
response, err := http.Get(serverURL)
|
response, err := http.Get(serverURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -79,7 +102,7 @@ func readResponse(serverURL string) ([]byte, error) {
|
|||||||
return contents, nil
|
return contents, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func testSwaggerSpec(t *testing.T, host string) {
|
func testSwaggerSpec(t *testing.T, host string, expectedGroupVersions []schema.GroupVersion) {
|
||||||
serverURL := host + "/swaggerapi"
|
serverURL := host + "/swaggerapi"
|
||||||
_, err := readResponse(serverURL)
|
_, err := readResponse(serverURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -87,7 +110,7 @@ func testSwaggerSpec(t *testing.T, host string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func testSupport(t *testing.T, host string) {
|
func testSupport(t *testing.T, host string, expectedGroupVersions []schema.GroupVersion) {
|
||||||
serverURL := host + "/version"
|
serverURL := host + "/version"
|
||||||
_, err := readResponse(serverURL)
|
_, err := readResponse(serverURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -104,9 +127,9 @@ func findGroup(groups []metav1.APIGroup, groupName string) *metav1.APIGroup {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func testAPIGroupList(t *testing.T, host string) {
|
func testAPIGroupList(t *testing.T, host string, expectedGroupVersions []schema.GroupVersion) {
|
||||||
groupVersionForDiscoveryMap := make(map[string]metav1.GroupVersionForDiscovery)
|
groupVersionForDiscoveryMap := make(map[string]metav1.GroupVersionForDiscovery)
|
||||||
for _, groupVersion := range groupVersions {
|
for _, groupVersion := range expectedGroupVersions {
|
||||||
groupVersionForDiscoveryMap[groupVersion.Group] = metav1.GroupVersionForDiscovery{
|
groupVersionForDiscoveryMap[groupVersion.Group] = metav1.GroupVersionForDiscovery{
|
||||||
GroupVersion: groupVersion.String(),
|
GroupVersion: groupVersion.String(),
|
||||||
Version: groupVersion.Version,
|
Version: groupVersion.Version,
|
||||||
@ -124,7 +147,8 @@ func testAPIGroupList(t *testing.T, host string) {
|
|||||||
t.Fatalf("Error in unmarshalling response from server %s: %v", serverURL, err)
|
t.Fatalf("Error in unmarshalling response from server %s: %v", serverURL, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, groupVersion := range groupVersions {
|
assert.Equal(t, len(apiGroupList.Groups), len(expectedGroupVersions), "expected: %v, actual: %v", expectedGroupVersions, apiGroupList.Groups)
|
||||||
|
for _, groupVersion := range expectedGroupVersions {
|
||||||
found := findGroup(apiGroupList.Groups, groupVersion.Group)
|
found := findGroup(apiGroupList.Groups, groupVersion.Group)
|
||||||
assert.NotNil(t, found)
|
assert.NotNil(t, found)
|
||||||
assert.Equal(t, groupVersion.Group, found.Name)
|
assert.Equal(t, groupVersion.Group, found.Name)
|
||||||
@ -135,8 +159,8 @@ func testAPIGroupList(t *testing.T, host string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func testAPIGroup(t *testing.T, host string) {
|
func testAPIGroup(t *testing.T, host string, expectedGroupVersions []schema.GroupVersion) {
|
||||||
for _, groupVersion := range groupVersions {
|
for _, groupVersion := range expectedGroupVersions {
|
||||||
serverURL := host + "/apis/" + groupVersion.Group
|
serverURL := host + "/apis/" + groupVersion.Group
|
||||||
contents, err := readResponse(serverURL)
|
contents, err := readResponse(serverURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -188,12 +212,25 @@ func findResource(resources []metav1.APIResource, resourceName string) *metav1.A
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func testAPIResourceList(t *testing.T, host string) {
|
func testAPIResourceList(t *testing.T, host string, expectedGroupVersions []schema.GroupVersion) {
|
||||||
testFederationResourceList(t, host)
|
testFederationResourceList(t, host)
|
||||||
testCoreResourceList(t, host)
|
testCoreResourceList(t, host)
|
||||||
testExtensionsResourceList(t, host)
|
testExtensionsResourceList(t, host)
|
||||||
// testBatchResourceList(t, host)
|
if contains(expectedGroupVersions, batch_v1.SchemeGroupVersion) {
|
||||||
// testAutoscalingResourceList(t, host)
|
testBatchResourceList(t, host)
|
||||||
|
}
|
||||||
|
if contains(expectedGroupVersions, autoscaling_v1.SchemeGroupVersion) {
|
||||||
|
testAutoscalingResourceList(t, host)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func contains(gvs []schema.GroupVersion, requiredGV schema.GroupVersion) bool {
|
||||||
|
for _, gv := range gvs {
|
||||||
|
if gv.String() == requiredGV.String() {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func testFederationResourceList(t *testing.T, host string) {
|
func testFederationResourceList(t *testing.T, host string) {
|
||||||
@ -233,8 +270,7 @@ func testCoreResourceList(t *testing.T, host string) {
|
|||||||
}
|
}
|
||||||
assert.Equal(t, "", apiResourceList.APIVersion)
|
assert.Equal(t, "", apiResourceList.APIVersion)
|
||||||
assert.Equal(t, v1.SchemeGroupVersion.String(), apiResourceList.GroupVersion)
|
assert.Equal(t, v1.SchemeGroupVersion.String(), apiResourceList.GroupVersion)
|
||||||
// Assert that there are exactly 7 resources.
|
assert.Equal(t, 8, len(apiResourceList.APIResources), "ResourceList: %v", apiResourceList.APIResources)
|
||||||
assert.Equal(t, 8, len(apiResourceList.APIResources))
|
|
||||||
|
|
||||||
// Verify services.
|
// Verify services.
|
||||||
found := findResource(apiResourceList.APIResources, "services")
|
found := findResource(apiResourceList.APIResources, "services")
|
||||||
|
@ -33,7 +33,8 @@ import (
|
|||||||
|
|
||||||
const apiNoun = "federation apiserver"
|
const apiNoun = "federation apiserver"
|
||||||
|
|
||||||
func getRunOptions() *options.ServerRunOptions {
|
// GetRunOptions returns the default run options that can be used to run a test federation apiserver.
|
||||||
|
func GetRunOptions() *options.ServerRunOptions {
|
||||||
r := options.NewServerRunOptions()
|
r := options.NewServerRunOptions()
|
||||||
r.Etcd.StorageConfig.ServerList = []string{framework.GetEtcdURLFromEnv()}
|
r.Etcd.StorageConfig.ServerList = []string{framework.GetEtcdURLFromEnv()}
|
||||||
// Use a unique prefix to ensure isolation from other tests using the same etcd instance
|
// Use a unique prefix to ensure isolation from other tests using the same etcd instance
|
||||||
@ -49,7 +50,14 @@ type FederationAPIFixture struct {
|
|||||||
stopChan chan struct{}
|
stopChan chan struct{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetUp runs federation apiserver with default run options.
|
||||||
func (f *FederationAPIFixture) SetUp(t *testing.T) {
|
func (f *FederationAPIFixture) SetUp(t *testing.T) {
|
||||||
|
f.SetUpWithRunOptions(t, GetRunOptions())
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetUpWithRunOptions runs federation apiserver with the given run options.
|
||||||
|
// Uses default run options if runOptions is nil.
|
||||||
|
func (f *FederationAPIFixture) SetUpWithRunOptions(t *testing.T, runOptions *options.ServerRunOptions) {
|
||||||
if f.stopChan != nil {
|
if f.stopChan != nil {
|
||||||
t.Fatal("SetUp() already called")
|
t.Fatal("SetUp() already called")
|
||||||
}
|
}
|
||||||
@ -57,8 +65,6 @@ func (f *FederationAPIFixture) SetUp(t *testing.T) {
|
|||||||
|
|
||||||
f.stopChan = make(chan struct{})
|
f.stopChan = make(chan struct{})
|
||||||
|
|
||||||
runOptions := getRunOptions()
|
|
||||||
|
|
||||||
err := startServer(t, runOptions, f.stopChan)
|
err := startServer(t, runOptions, f.stopChan)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
|
Loading…
Reference in New Issue
Block a user