mirror of
https://github.com/k3s-io/kubernetes.git
synced 2026-01-05 07:27:21 +00:00
refactor testapi and test scripts to prepare for multiple API groups.
This commit is contained in:
@@ -68,23 +68,34 @@ type testClient struct {
|
||||
QueryValidator map[string]func(string, string) bool
|
||||
}
|
||||
|
||||
func (c *testClient) Setup() *testClient {
|
||||
func (c *testClient) Setup(t *testing.T) *testClient {
|
||||
c.handler = &util.FakeHandler{
|
||||
StatusCode: c.Response.StatusCode,
|
||||
}
|
||||
if responseBody := body(c.Response.Body, c.Response.RawBody); responseBody != nil {
|
||||
if responseBody := body(t, c.Response.Body, c.Response.RawBody); responseBody != nil {
|
||||
c.handler.ResponseBody = *responseBody
|
||||
}
|
||||
c.server = httptest.NewServer(c.handler)
|
||||
if c.Client == nil {
|
||||
version := c.Version
|
||||
if len(version) == 0 {
|
||||
version = testapi.Version()
|
||||
version = testapi.Default.Version()
|
||||
}
|
||||
c.Client = NewOrDie(&Config{
|
||||
Host: c.server.URL,
|
||||
Version: version,
|
||||
})
|
||||
|
||||
// TODO: caesarxuchao: hacky way to specify version of Experimental client.
|
||||
// We will fix this by supporting multiple group versions in Config
|
||||
version = c.Version
|
||||
if len(version) == 0 {
|
||||
version = testapi.Experimental.Version()
|
||||
}
|
||||
c.ExperimentalClient = NewExperimentalOrDie(&Config{
|
||||
Host: c.server.URL,
|
||||
Version: version,
|
||||
})
|
||||
}
|
||||
c.QueryValidator = map[string]func(string, string) bool{}
|
||||
return c
|
||||
@@ -124,7 +135,7 @@ func (c *testClient) ValidateCommon(t *testing.T, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
requestBody := body(c.Request.Body, c.Request.RawBody)
|
||||
requestBody := body(t, c.Request.Body, c.Request.RawBody)
|
||||
actualQuery := c.handler.RequestReceived.URL.Query()
|
||||
t.Logf("got query: %v", actualQuery)
|
||||
t.Logf("path: %v", c.Request.Path)
|
||||
@@ -136,9 +147,9 @@ func (c *testClient) ValidateCommon(t *testing.T, err error) {
|
||||
validator, ok := c.QueryValidator[key]
|
||||
if !ok {
|
||||
switch key {
|
||||
case api.LabelSelectorQueryParam(testapi.Version()):
|
||||
case api.LabelSelectorQueryParam(testapi.Default.Version()):
|
||||
validator = validateLabels
|
||||
case api.FieldSelectorQueryParam(testapi.Version()):
|
||||
case api.FieldSelectorQueryParam(testapi.Default.Version()):
|
||||
validator = validateFields
|
||||
default:
|
||||
validator = func(a, b string) bool { return a == b }
|
||||
@@ -200,9 +211,30 @@ func validateFields(a, b string) bool {
|
||||
return sA.String() == sB.String()
|
||||
}
|
||||
|
||||
func body(obj runtime.Object, raw *string) *string {
|
||||
func body(t *testing.T, obj runtime.Object, raw *string) *string {
|
||||
if obj != nil {
|
||||
bs, _ := testapi.Codec().Encode(obj)
|
||||
_, kind, err := api.Scheme.ObjectVersionAndKind(obj)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected encoding error: %v", err)
|
||||
}
|
||||
// TODO: caesarxuchao: we should detect which group an object belongs to
|
||||
// by using the version returned by Schem.ObjectVersionAndKind() once we
|
||||
// split the schemes for internal objects.
|
||||
// TODO: caesarxuchao: we should add a map from kind to group in Scheme.
|
||||
var bs []byte
|
||||
if api.Scheme.Recognizes(testapi.Default.GroupAndVersion(), kind) {
|
||||
bs, err = testapi.Default.Codec().Encode(obj)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected encoding error: %v", err)
|
||||
}
|
||||
} else if api.Scheme.Recognizes(testapi.Experimental.GroupAndVersion(), kind) {
|
||||
bs, err = testapi.Experimental.Codec().Encode(obj)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected encoding error: %v", err)
|
||||
}
|
||||
} else {
|
||||
t.Errorf("unexpected kind: %v", kind)
|
||||
}
|
||||
body := string(bs)
|
||||
return &body
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user