mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-09 12:07:47 +00:00
Merge pull request #9829 from caesarxuchao/remove-config_test
remove pkg/config/config_test.json
This commit is contained in:
commit
fa389a68b5
@ -1,89 +0,0 @@
|
|||||||
/*
|
|
||||||
Copyright 2014 The Kubernetes Authors All rights reserved.
|
|
||||||
|
|
||||||
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 config
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/meta"
|
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
|
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
|
||||||
errs "github.com/GoogleCloudPlatform/kubernetes/pkg/util/fielderrors"
|
|
||||||
)
|
|
||||||
|
|
||||||
type RESTClientPoster interface {
|
|
||||||
Post() *client.Request
|
|
||||||
}
|
|
||||||
|
|
||||||
// ClientFunc returns the RESTClient defined for given resource
|
|
||||||
type ClientPosterFunc func(mapping *meta.RESTMapping) (RESTClientPoster, error)
|
|
||||||
|
|
||||||
// CreateObjects creates bulk of resources provided by items list. Each item must
|
|
||||||
// be valid API type. It requires ObjectTyper to parse the Version and Kind and
|
|
||||||
// RESTMapper to get the resource URI and REST client that knows how to create
|
|
||||||
// given type
|
|
||||||
func CreateObjects(typer runtime.ObjectTyper, mapper meta.RESTMapper, clientFor ClientPosterFunc, objects []runtime.Object) []error {
|
|
||||||
var allErrors []error
|
|
||||||
for i, obj := range objects {
|
|
||||||
version, kind, err := typer.ObjectVersionAndKind(obj)
|
|
||||||
if err != nil {
|
|
||||||
allErrors = append(allErrors, fmt.Errorf("Config.item[%d] kind: %v", i, err))
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
mapping, err := mapper.RESTMapping(kind, version)
|
|
||||||
if err != nil {
|
|
||||||
allErrors = append(allErrors, fmt.Errorf("Config.item[%d] mapping: %v", i, err))
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
client, err := clientFor(mapping)
|
|
||||||
if err != nil {
|
|
||||||
allErrors = append(allErrors, fmt.Errorf("Config.item[%d] client: %v", i, err))
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := CreateObject(client, mapping, obj); err != nil {
|
|
||||||
allErrors = append(allErrors, fmt.Errorf("Config.item[%d]: %v", i, err))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return allErrors
|
|
||||||
}
|
|
||||||
|
|
||||||
// CreateObject creates the obj using the provided clients and the resource URI
|
|
||||||
// mapping. It reports ValidationError when the object is missing the Metadata
|
|
||||||
// or the Name and it will report any error occured during create REST call
|
|
||||||
func CreateObject(client RESTClientPoster, mapping *meta.RESTMapping, obj runtime.Object) *errs.ValidationError {
|
|
||||||
name, err := mapping.MetadataAccessor.Name(obj)
|
|
||||||
if err != nil || name == "" {
|
|
||||||
return errs.NewFieldRequired("name")
|
|
||||||
}
|
|
||||||
|
|
||||||
namespace, err := mapping.Namespace(obj)
|
|
||||||
if err != nil {
|
|
||||||
return errs.NewFieldRequired("namespace")
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: This should be using RESTHelper
|
|
||||||
err = client.Post().Resource(mapping.Resource).Namespace(namespace).Body(obj).Do().Error()
|
|
||||||
if err != nil {
|
|
||||||
return errs.NewFieldInvalid(name, obj, err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
@ -1,159 +0,0 @@
|
|||||||
/*
|
|
||||||
Copyright 2014 The Kubernetes Authors All rights reserved.
|
|
||||||
|
|
||||||
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 config
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"net/http"
|
|
||||||
"net/http/httptest"
|
|
||||||
"net/url"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest"
|
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/meta"
|
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/testapi"
|
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
|
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
|
||||||
)
|
|
||||||
|
|
||||||
func getTyperAndMapper() (runtime.ObjectTyper, meta.RESTMapper) {
|
|
||||||
return api.Scheme, latest.RESTMapper
|
|
||||||
}
|
|
||||||
|
|
||||||
func getFakeClient(t *testing.T, validURLs []string) (ClientPosterFunc, *httptest.Server) {
|
|
||||||
handlerFunc := func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
for _, u := range validURLs {
|
|
||||||
if u == r.RequestURI {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
t.Errorf("Unexpected HTTP request: %s, expected %v", r.RequestURI, validURLs)
|
|
||||||
}
|
|
||||||
server := httptest.NewServer(http.HandlerFunc(handlerFunc))
|
|
||||||
return func(mapping *meta.RESTMapping) (RESTClientPoster, error) {
|
|
||||||
fakeCodec := testapi.Codec()
|
|
||||||
fakeUri, _ := url.Parse(server.URL + "/api/" + testapi.Version())
|
|
||||||
return client.NewRESTClient(fakeUri, testapi.Version(), fakeCodec, 5, 10), nil
|
|
||||||
}, server
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestCreateObjects(t *testing.T) {
|
|
||||||
items := []runtime.Object{}
|
|
||||||
|
|
||||||
items = append(items, &api.Pod{
|
|
||||||
ObjectMeta: api.ObjectMeta{Name: "test-pod", Namespace: "default"},
|
|
||||||
})
|
|
||||||
|
|
||||||
items = append(items, &api.Service{
|
|
||||||
ObjectMeta: api.ObjectMeta{Name: "test-service", Namespace: "default"},
|
|
||||||
})
|
|
||||||
|
|
||||||
typer, mapper := getTyperAndMapper()
|
|
||||||
client, s := getFakeClient(t, []string{
|
|
||||||
testapi.ResourcePath("pods", api.NamespaceDefault, ""),
|
|
||||||
testapi.ResourcePath("services", api.NamespaceDefault, ""),
|
|
||||||
})
|
|
||||||
|
|
||||||
errs := CreateObjects(typer, mapper, client, items)
|
|
||||||
s.Close()
|
|
||||||
if len(errs) != 0 {
|
|
||||||
t.Errorf("Unexpected errors during config.Create(): %v", errs)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestCreateNoNameItem(t *testing.T) {
|
|
||||||
items := []runtime.Object{}
|
|
||||||
|
|
||||||
items = append(items, &api.Service{
|
|
||||||
TypeMeta: api.TypeMeta{APIVersion: testapi.Version(), Kind: "Service"},
|
|
||||||
})
|
|
||||||
|
|
||||||
typer, mapper := getTyperAndMapper()
|
|
||||||
client, s := getFakeClient(t, []string{
|
|
||||||
testapi.ResourcePath("services", api.NamespaceDefault, ""),
|
|
||||||
})
|
|
||||||
|
|
||||||
errs := CreateObjects(typer, mapper, client, items)
|
|
||||||
s.Close()
|
|
||||||
|
|
||||||
if len(errs) == 0 {
|
|
||||||
t.Errorf("Expected required value error for missing name")
|
|
||||||
}
|
|
||||||
|
|
||||||
errStr := errs[0].Error()
|
|
||||||
if !strings.Contains(errStr, "Config.item[0]: name") {
|
|
||||||
t.Errorf("Expected 'Config.item[0]: name' in error string, got '%s'", errStr)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type InvalidItem struct{}
|
|
||||||
|
|
||||||
func (*InvalidItem) IsAnAPIObject() {}
|
|
||||||
|
|
||||||
func TestCreateInvalidItem(t *testing.T) {
|
|
||||||
items := []runtime.Object{
|
|
||||||
&InvalidItem{},
|
|
||||||
}
|
|
||||||
|
|
||||||
typer, mapper := getTyperAndMapper()
|
|
||||||
client, s := getFakeClient(t, []string{})
|
|
||||||
|
|
||||||
errs := CreateObjects(typer, mapper, client, items)
|
|
||||||
s.Close()
|
|
||||||
|
|
||||||
if len(errs) == 0 {
|
|
||||||
t.Errorf("Expected invalid value error for kind")
|
|
||||||
}
|
|
||||||
|
|
||||||
errStr := errs[0].Error()
|
|
||||||
if !strings.Contains(errStr, "Config.item[0] kind") {
|
|
||||||
t.Errorf("Expected 'Config.item[0] kind' in error string, got '%s'", errStr)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestCreateNoClientItems(t *testing.T) {
|
|
||||||
items := []runtime.Object{}
|
|
||||||
|
|
||||||
items = append(items, &api.Pod{
|
|
||||||
TypeMeta: api.TypeMeta{APIVersion: testapi.Version(), Kind: "Pod"},
|
|
||||||
ObjectMeta: api.ObjectMeta{Name: "test-pod"},
|
|
||||||
})
|
|
||||||
|
|
||||||
typer, mapper := getTyperAndMapper()
|
|
||||||
_, s := getFakeClient(t, []string{
|
|
||||||
testapi.ResourcePath("pods", api.NamespaceDefault, ""),
|
|
||||||
testapi.ResourcePath("services", api.NamespaceDefault, ""),
|
|
||||||
})
|
|
||||||
|
|
||||||
noClientFunc := func(mapping *meta.RESTMapping) (RESTClientPoster, error) {
|
|
||||||
return nil, fmt.Errorf("no client")
|
|
||||||
}
|
|
||||||
|
|
||||||
errs := CreateObjects(typer, mapper, noClientFunc, items)
|
|
||||||
s.Close()
|
|
||||||
|
|
||||||
if len(errs) == 0 {
|
|
||||||
t.Errorf("Expected invalid value error for client")
|
|
||||||
}
|
|
||||||
|
|
||||||
errStr := errs[0].Error()
|
|
||||||
if !strings.Contains(errStr, "Config.item[0] client") {
|
|
||||||
t.Errorf("Expected 'Config.item[0] client' in error string, got '%s'", errStr)
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,128 +0,0 @@
|
|||||||
[
|
|
||||||
{
|
|
||||||
"id": "frontend",
|
|
||||||
"name": "frontend",
|
|
||||||
"kind": "Service",
|
|
||||||
"apiVersion": "v1beta2",
|
|
||||||
"port": 5432,
|
|
||||||
"selector": {
|
|
||||||
"name": "frontend"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"id": "redismaster",
|
|
||||||
"name": "redismaster",
|
|
||||||
"kind": "Service",
|
|
||||||
"apiVersion": "v1beta1",
|
|
||||||
"port": 10000,
|
|
||||||
"selector": {
|
|
||||||
"name": "redis-master"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"id": "redisslave",
|
|
||||||
"name": "redisslave",
|
|
||||||
"kind": "Service",
|
|
||||||
"apiVersion": "v1beta1",
|
|
||||||
"port": 10001,
|
|
||||||
"labels": {
|
|
||||||
"name": "redisslave"
|
|
||||||
},
|
|
||||||
"selector": {
|
|
||||||
"name": "redisslave"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"id": "redis-master-2",
|
|
||||||
"name": "redis-master-2",
|
|
||||||
"kind": "Pod",
|
|
||||||
"apiVersion": "v1beta1",
|
|
||||||
"desiredState": {
|
|
||||||
"manifest": {
|
|
||||||
"version": "v1beta1",
|
|
||||||
"containers": [{
|
|
||||||
"name": "master",
|
|
||||||
"image": "redis",
|
|
||||||
"env": [
|
|
||||||
{
|
|
||||||
"name": "REDIS_PASSWORD",
|
|
||||||
"value": "secret"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"ports": [{
|
|
||||||
"containerPort": 6379
|
|
||||||
}]
|
|
||||||
}]
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"labels": {
|
|
||||||
"name": "redis-master"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"id": "frontend-controller",
|
|
||||||
"name": "frontend-controller",
|
|
||||||
"kind": "ReplicationController",
|
|
||||||
"apiVersion": "v1beta1",
|
|
||||||
"desiredState": {
|
|
||||||
"replicas": 3,
|
|
||||||
"replicaSelector": {"name": "frontend"},
|
|
||||||
"podTemplate": {
|
|
||||||
"desiredState": {
|
|
||||||
"manifest": {
|
|
||||||
"version": "v1beta1",
|
|
||||||
"containers": [{
|
|
||||||
"name": "php-redis",
|
|
||||||
"image": "brendanburns/php-redis",
|
|
||||||
"env": [
|
|
||||||
{
|
|
||||||
"name": "ADMIN_USERNAME",
|
|
||||||
"value": "admin"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"name": "ADMIN_PASSWORD",
|
|
||||||
"value": "secret"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"name": "REDIS_PASSWORD",
|
|
||||||
"value": "secret"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"ports": [{"containerPort": 80}]
|
|
||||||
}]
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"labels": {"name": "frontend"}
|
|
||||||
}},
|
|
||||||
"labels": {"name": "frontend"}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"id": "redis-slave-controller",
|
|
||||||
"name": "redis-slave-controller",
|
|
||||||
"kind": "ReplicationController",
|
|
||||||
"apiVersion": "v1beta1",
|
|
||||||
"desiredState": {
|
|
||||||
"replicas": 2,
|
|
||||||
"replicaSelector": {"name": "redisslave"},
|
|
||||||
"podTemplate": {
|
|
||||||
"desiredState": {
|
|
||||||
"manifest": {
|
|
||||||
"version": "v1beta1",
|
|
||||||
"containers": [{
|
|
||||||
"name": "slave",
|
|
||||||
"image": "brendanburns/redis-slave",
|
|
||||||
"env": [
|
|
||||||
{
|
|
||||||
"name": "REDIS_PASSWORD",
|
|
||||||
"value": "secret"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"ports": [{"containerPort": 6379}]
|
|
||||||
}]
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"labels": {"name": "redisslave"}
|
|
||||||
}},
|
|
||||||
"labels": {"name": "redisslave"}
|
|
||||||
}
|
|
||||||
]
|
|
Loading…
Reference in New Issue
Block a user