mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-26 21:17:23 +00:00
Simplify genericapiserver tests
This commit is contained in:
parent
9e1960c507
commit
bda1c76f39
@ -46,25 +46,24 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// setUp is a convience function for setting up for (most) tests.
|
// setUp is a convience function for setting up for (most) tests.
|
||||||
func setUp(t *testing.T) (*GenericAPIServer, *etcdtesting.EtcdTestServer, Config, *assert.Assertions) {
|
func setUp(t *testing.T) (*etcdtesting.EtcdTestServer, Config, *assert.Assertions) {
|
||||||
etcdServer, _ := etcdtesting.NewUnsecuredEtcd3TestClientServer(t)
|
etcdServer, _ := etcdtesting.NewUnsecuredEtcd3TestClientServer(t)
|
||||||
|
|
||||||
genericapiserver := &GenericAPIServer{}
|
|
||||||
config := Config{}
|
config := Config{}
|
||||||
config.PublicAddress = net.ParseIP("192.168.10.4")
|
config.PublicAddress = net.ParseIP("192.168.10.4")
|
||||||
config.RequestContextMapper = api.NewRequestContextMapper()
|
config.RequestContextMapper = api.NewRequestContextMapper()
|
||||||
return genericapiserver, etcdServer, config, assert.New(t)
|
|
||||||
}
|
|
||||||
|
|
||||||
func newMaster(t *testing.T) (*GenericAPIServer, *etcdtesting.EtcdTestServer, Config, *assert.Assertions) {
|
|
||||||
_, etcdserver, config, assert := setUp(t)
|
|
||||||
|
|
||||||
config.ProxyDialer = func(network, addr string) (net.Conn, error) { return nil, nil }
|
config.ProxyDialer = func(network, addr string) (net.Conn, error) { return nil, nil }
|
||||||
config.ProxyTLSClientConfig = &tls.Config{}
|
config.ProxyTLSClientConfig = &tls.Config{}
|
||||||
config.Serializer = api.Codecs
|
config.Serializer = api.Codecs
|
||||||
config.APIPrefix = "/api"
|
config.APIPrefix = "/api"
|
||||||
config.APIGroupPrefix = "/apis"
|
config.APIGroupPrefix = "/apis"
|
||||||
|
|
||||||
|
return etcdServer, config, assert.New(t)
|
||||||
|
}
|
||||||
|
|
||||||
|
func newMaster(t *testing.T) (*GenericAPIServer, *etcdtesting.EtcdTestServer, Config, *assert.Assertions) {
|
||||||
|
etcdserver, config, assert := setUp(t)
|
||||||
|
|
||||||
s, err := config.New()
|
s, err := config.New()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Error in bringing up the server: %v", err)
|
t.Fatalf("Error in bringing up the server: %v", err)
|
||||||
@ -104,14 +103,11 @@ func TestNew(t *testing.T) {
|
|||||||
|
|
||||||
// Verifies that AddGroupVersions works as expected.
|
// Verifies that AddGroupVersions works as expected.
|
||||||
func TestInstallAPIGroups(t *testing.T) {
|
func TestInstallAPIGroups(t *testing.T) {
|
||||||
_, etcdserver, config, assert := setUp(t)
|
etcdserver, config, assert := setUp(t)
|
||||||
defer etcdserver.Terminate(t)
|
defer etcdserver.Terminate(t)
|
||||||
|
|
||||||
config.ProxyDialer = func(network, addr string) (net.Conn, error) { return nil, nil }
|
|
||||||
config.ProxyTLSClientConfig = &tls.Config{}
|
|
||||||
config.APIPrefix = "/apiPrefix"
|
config.APIPrefix = "/apiPrefix"
|
||||||
config.APIGroupPrefix = "/apiGroupPrefix"
|
config.APIGroupPrefix = "/apiGroupPrefix"
|
||||||
config.Serializer = api.Codecs
|
|
||||||
|
|
||||||
s, err := config.New()
|
s, err := config.New()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -174,9 +170,10 @@ func TestNewHandlerContainer(t *testing.T) {
|
|||||||
// TestHandleWithAuth verifies HandleWithAuth adds the path
|
// TestHandleWithAuth verifies HandleWithAuth adds the path
|
||||||
// to the MuxHelper.RegisteredPaths.
|
// to the MuxHelper.RegisteredPaths.
|
||||||
func TestHandleWithAuth(t *testing.T) {
|
func TestHandleWithAuth(t *testing.T) {
|
||||||
server, etcdserver, _, assert := setUp(t)
|
etcdserver, _, assert := setUp(t)
|
||||||
defer etcdserver.Terminate(t)
|
defer etcdserver.Terminate(t)
|
||||||
|
|
||||||
|
server := &GenericAPIServer{}
|
||||||
server.Mux = apiserver.NewPathRecorderMux(http.NewServeMux())
|
server.Mux = apiserver.NewPathRecorderMux(http.NewServeMux())
|
||||||
handler := func(r http.ResponseWriter, w *http.Request) { w.Write(nil) }
|
handler := func(r http.ResponseWriter, w *http.Request) { w.Write(nil) }
|
||||||
server.HandleWithAuth("/test", http.HandlerFunc(handler))
|
server.HandleWithAuth("/test", http.HandlerFunc(handler))
|
||||||
@ -187,9 +184,10 @@ func TestHandleWithAuth(t *testing.T) {
|
|||||||
// TestHandleFuncWithAuth verifies HandleFuncWithAuth adds the path
|
// TestHandleFuncWithAuth verifies HandleFuncWithAuth adds the path
|
||||||
// to the MuxHelper.RegisteredPaths.
|
// to the MuxHelper.RegisteredPaths.
|
||||||
func TestHandleFuncWithAuth(t *testing.T) {
|
func TestHandleFuncWithAuth(t *testing.T) {
|
||||||
server, etcdserver, _, assert := setUp(t)
|
etcdserver, _, assert := setUp(t)
|
||||||
defer etcdserver.Terminate(t)
|
defer etcdserver.Terminate(t)
|
||||||
|
|
||||||
|
server := &GenericAPIServer{}
|
||||||
server.Mux = apiserver.NewPathRecorderMux(http.NewServeMux())
|
server.Mux = apiserver.NewPathRecorderMux(http.NewServeMux())
|
||||||
handler := func(r http.ResponseWriter, w *http.Request) { w.Write(nil) }
|
handler := func(r http.ResponseWriter, w *http.Request) { w.Write(nil) }
|
||||||
server.HandleFuncWithAuth("/test", handler)
|
server.HandleFuncWithAuth("/test", handler)
|
||||||
@ -199,16 +197,13 @@ func TestHandleFuncWithAuth(t *testing.T) {
|
|||||||
|
|
||||||
// TestNotRestRoutesHaveAuth checks that special non-routes are behind authz/authn.
|
// TestNotRestRoutesHaveAuth checks that special non-routes are behind authz/authn.
|
||||||
func TestNotRestRoutesHaveAuth(t *testing.T) {
|
func TestNotRestRoutesHaveAuth(t *testing.T) {
|
||||||
_, etcdserver, config, _ := setUp(t)
|
etcdserver, config, _ := setUp(t)
|
||||||
defer etcdserver.Terminate(t)
|
defer etcdserver.Terminate(t)
|
||||||
|
|
||||||
authz := mockAuthorizer{}
|
authz := mockAuthorizer{}
|
||||||
|
|
||||||
config.ProxyDialer = func(network, addr string) (net.Conn, error) { return nil, nil }
|
|
||||||
config.ProxyTLSClientConfig = &tls.Config{}
|
|
||||||
config.APIPrefix = "/apiPrefix"
|
config.APIPrefix = "/apiPrefix"
|
||||||
config.APIGroupPrefix = "/apiGroupPrefix"
|
config.APIGroupPrefix = "/apiGroupPrefix"
|
||||||
config.Serializer = api.Codecs
|
|
||||||
config.Authorizer = &authz
|
config.Authorizer = &authz
|
||||||
|
|
||||||
config.EnableSwaggerUI = true
|
config.EnableSwaggerUI = true
|
||||||
@ -267,10 +262,11 @@ func (authn *mockAuthenticator) AuthenticateRequest(req *http.Request) (user.Inf
|
|||||||
// TestInstallSwaggerAPI verifies that the swagger api is added
|
// TestInstallSwaggerAPI verifies that the swagger api is added
|
||||||
// at the proper endpoint.
|
// at the proper endpoint.
|
||||||
func TestInstallSwaggerAPI(t *testing.T) {
|
func TestInstallSwaggerAPI(t *testing.T) {
|
||||||
server, etcdserver, _, assert := setUp(t)
|
etcdserver, _, assert := setUp(t)
|
||||||
defer etcdserver.Terminate(t)
|
defer etcdserver.Terminate(t)
|
||||||
|
|
||||||
mux := http.NewServeMux()
|
mux := http.NewServeMux()
|
||||||
|
server := &GenericAPIServer{}
|
||||||
server.HandlerContainer = NewHandlerContainer(mux, nil)
|
server.HandlerContainer = NewHandlerContainer(mux, nil)
|
||||||
|
|
||||||
// Ensure swagger isn't installed without the call
|
// Ensure swagger isn't installed without the call
|
||||||
|
Loading…
Reference in New Issue
Block a user