Skip to content

Commit

Permalink
Merge pull request #5778 from mohamedawnallah/unitTestAggregatedAPISe…
Browse files Browse the repository at this point in the history
…rver

pkg: unit test aggregatedAPIServer
  • Loading branch information
karmada-bot authored Nov 4, 2024
2 parents e90ff63 + ccccace commit 8c7607e
Show file tree
Hide file tree
Showing 2 changed files with 177 additions and 2 deletions.
13 changes: 11 additions & 2 deletions pkg/aggregatedapiserver/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ limitations under the License.
package aggregatedapiserver

import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/version"
"k8s.io/apiserver/pkg/registry/generic"
"k8s.io/apiserver/pkg/registry/rest"
genericapiserver "k8s.io/apiserver/pkg/server"
listcorev1 "k8s.io/client-go/listers/core/v1"
Expand Down Expand Up @@ -70,6 +72,13 @@ func (cfg *Config) Complete() CompletedConfig {
return CompletedConfig{&c}
}

var newClusterStorageBuilder = func(scheme *runtime.Scheme, restConfig *restclient.Config, secretLister listcorev1.SecretLister, optsGetter generic.RESTOptionsGetter) (*clusterstorage.ClusterStorage, error) {
return clusterstorage.NewStorage(scheme, restConfig, secretLister, optsGetter)
}
var apiGroupInstaller = func(server *APIServer, apiGroupInfo *genericapiserver.APIGroupInfo) error {
return server.GenericAPIServer.InstallAPIGroup(apiGroupInfo)
}

func (c completedConfig) New(restConfig *restclient.Config, secretLister listcorev1.SecretLister) (*APIServer, error) {
genericServer, err := c.GenericConfig.New("aggregated-apiserver", genericapiserver.NewEmptyDelegate())
if err != nil {
Expand All @@ -82,7 +91,7 @@ func (c completedConfig) New(restConfig *restclient.Config, secretLister listcor

apiGroupInfo := genericapiserver.NewDefaultAPIGroupInfo(clusterapis.GroupName, clusterscheme.Scheme, clusterscheme.ParameterCodec, clusterscheme.Codecs)

clusterStorage, err := clusterstorage.NewStorage(clusterscheme.Scheme, restConfig, secretLister, c.GenericConfig.RESTOptionsGetter)
clusterStorage, err := newClusterStorageBuilder(clusterscheme.Scheme, restConfig, secretLister, c.GenericConfig.RESTOptionsGetter)
if err != nil {
klog.Errorf("Unable to create REST storage for a resource due to %v, will die", err)
return nil, err
Expand All @@ -93,7 +102,7 @@ func (c completedConfig) New(restConfig *restclient.Config, secretLister listcor
v1alpha1cluster["clusters/proxy"] = clusterStorage.Proxy
apiGroupInfo.VersionedResourcesStorageMap["v1alpha1"] = v1alpha1cluster

if err = server.GenericAPIServer.InstallAPIGroup(&apiGroupInfo); err != nil {
if err = apiGroupInstaller(server, &apiGroupInfo); err != nil {
return nil, err
}

Expand Down
166 changes: 166 additions & 0 deletions pkg/aggregatedapiserver/apiserver_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/*
Copyright 2024 The Karmada Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package aggregatedapiserver

import (
"errors"
"net"
"net/http"
"strings"
"testing"

"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apiserver/pkg/endpoints/openapi"
"k8s.io/apiserver/pkg/registry/generic"
genericapiserver "k8s.io/apiserver/pkg/server"
"k8s.io/client-go/informers"
clientset "k8s.io/client-go/kubernetes"
fakeclientset "k8s.io/client-go/kubernetes/fake"
listcorev1 "k8s.io/client-go/listers/core/v1"
restclient "k8s.io/client-go/rest"

clusterscheme "github.com/karmada-io/karmada/pkg/apis/cluster/scheme"
generatedopenapi "github.com/karmada-io/karmada/pkg/generated/openapi"
clusterstorage "github.com/karmada-io/karmada/pkg/registry/cluster/storage"
)

func TestNewAggregatedAPIServer(t *testing.T) {
tests := []struct {
name string
cfg *completedConfig
genericAPIServerConfig *genericapiserver.Config
restConfig *restclient.Config
secretLister listcorev1.SecretLister
client clientset.Interface
prep func(*completedConfig, *genericapiserver.Config, clientset.Interface) error
wantErr bool
errMsg string
}{
{
name: "NewAggregatedAPIServer_NetworkIssue_FailedToCreateRESTStorage",
cfg: &completedConfig{
ExtraConfig: &ExtraConfig{},
},
genericAPIServerConfig: &genericapiserver.Config{
RESTOptionsGetter: generic.RESTOptions{},
Serializer: runtime.NewSimpleNegotiatedSerializer(runtime.SerializerInfo{
MediaType: runtime.ContentTypeJSON,
}),
LoopbackClientConfig: &restclient.Config{},
EquivalentResourceRegistry: runtime.NewEquivalentResourceRegistry(),
BuildHandlerChainFunc: func(http.Handler, *genericapiserver.Config) (secure http.Handler) {
return nil
},
ExternalAddress: "10.0.0.0:10000",
},
client: fakeclientset.NewSimpleClientset(),
prep: func(cfg *completedConfig, genericAPIServerCfg *genericapiserver.Config, client clientset.Interface) error {
sharedInformer := informers.NewSharedInformerFactory(client, 0)
cfg.GenericConfig = genericAPIServerCfg.Complete(sharedInformer)
newClusterStorageBuilder = func(*runtime.Scheme, *restclient.Config, listcorev1.SecretLister, generic.RESTOptionsGetter) (*clusterstorage.ClusterStorage, error) {
return nil, errors.New("unexpected network issue while creating the cluster storage")
}
return nil
},
wantErr: true,
errMsg: "unexpected network issue while creating the cluster storage",
},
{
name: "NewAggregatedAPIServer_InstalledAPIGroup_FailedToInstallAPIGroup",
cfg: &completedConfig{
ExtraConfig: &ExtraConfig{},
},
genericAPIServerConfig: &genericapiserver.Config{
RESTOptionsGetter: generic.RESTOptions{},
Serializer: runtime.NewSimpleNegotiatedSerializer(runtime.SerializerInfo{
MediaType: runtime.ContentTypeJSON,
}),
LoopbackClientConfig: &restclient.Config{},
EquivalentResourceRegistry: runtime.NewEquivalentResourceRegistry(),
BuildHandlerChainFunc: func(http.Handler, *genericapiserver.Config) (secure http.Handler) {
return nil
},
OpenAPIV3Config: genericapiserver.DefaultOpenAPIV3Config(generatedopenapi.GetOpenAPIDefinitions, openapi.NewDefinitionNamer(clusterscheme.Scheme)),
ExternalAddress: "10.0.0.0:10000",
},
prep: func(cfg *completedConfig, genericAPIServerCfg *genericapiserver.Config, client clientset.Interface) error {
sharedInformer := informers.NewSharedInformerFactory(client, 0)
cfg.GenericConfig = genericAPIServerCfg.Complete(sharedInformer)
newClusterStorageBuilder = func(*runtime.Scheme, *restclient.Config, listcorev1.SecretLister, generic.RESTOptionsGetter) (*clusterstorage.ClusterStorage, error) {
return &clusterstorage.ClusterStorage{}, nil
}
apiGroupInstaller = func(*APIServer, *genericapiserver.APIGroupInfo) error {
return errors.New("failed to install api group")
}
return nil
},
wantErr: true,
errMsg: "failed to install api group",
},
{
name: "NewAggregatedAPIServer_InstalledAPIGroup_APIGroupInstalled",
cfg: &completedConfig{
ExtraConfig: &ExtraConfig{},
},
genericAPIServerConfig: &genericapiserver.Config{
RESTOptionsGetter: generic.RESTOptions{},
SecureServing: &genericapiserver.SecureServingInfo{
Listener: &net.TCPListener{},
},
Serializer: runtime.NewSimpleNegotiatedSerializer(runtime.SerializerInfo{
MediaType: runtime.ContentTypeJSON,
}),
LoopbackClientConfig: &restclient.Config{},
EquivalentResourceRegistry: runtime.NewEquivalentResourceRegistry(),
BuildHandlerChainFunc: func(http.Handler, *genericapiserver.Config) (secure http.Handler) {
return nil
},
OpenAPIV3Config: genericapiserver.DefaultOpenAPIV3Config(generatedopenapi.GetOpenAPIDefinitions, openapi.NewDefinitionNamer(clusterscheme.Scheme)),
ExternalAddress: "10.0.0.0:10000",
},
prep: func(cfg *completedConfig, genericAPIServerCfg *genericapiserver.Config, client clientset.Interface) error {
sharedInformer := informers.NewSharedInformerFactory(client, 0)
cfg.GenericConfig = genericAPIServerCfg.Complete(sharedInformer)
newClusterStorageBuilder = func(*runtime.Scheme, *restclient.Config, listcorev1.SecretLister, generic.RESTOptionsGetter) (*clusterstorage.ClusterStorage, error) {
return &clusterstorage.ClusterStorage{}, nil
}
apiGroupInstaller = func(*APIServer, *genericapiserver.APIGroupInfo) error {
return nil
}
return nil
},
wantErr: false,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if err := test.prep(test.cfg, test.genericAPIServerConfig, test.client); err != nil {
t.Fatalf("failed to prep test environment before creating new aggregated apiserver, got: %v", err)
}
_, err := test.cfg.New(test.restConfig, test.secretLister)
if err == nil && test.wantErr {
t.Fatal("expected an error, but got none")
}
if err != nil && !test.wantErr {
t.Errorf("unexpected error, got: %v", err)
}
if err != nil && test.wantErr && !strings.Contains(err.Error(), test.errMsg) {
t.Errorf("expected error message %s to be in %s", test.errMsg, err.Error())
}
})
}
}

0 comments on commit 8c7607e

Please sign in to comment.