當前位置: 首頁>>代碼示例>>Golang>>正文


Golang testapi.GetCodecForObject函數代碼示例

本文整理匯總了Golang中k8s/io/kubernetes/pkg/api/testapi.GetCodecForObject函數的典型用法代碼示例。如果您正苦於以下問題:Golang GetCodecForObject函數的具體用法?Golang GetCodecForObject怎麽用?Golang GetCodecForObject使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了GetCodecForObject函數的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: TestDecodeNumbers

func TestDecodeNumbers(t *testing.T) {

	// Start with a valid pod
	originalJSON := []byte(`{
		"kind":"Pod",
		"apiVersion":"v1",
		"metadata":{"name":"pod","namespace":"foo"},
		"spec":{
			"containers":[{"name":"container","image":"container"}],
			"activeDeadlineSeconds":9223372036854775807
		}
	}`)

	pod := &api.Pod{}

	// Decode with structured codec
	codec, err := testapi.GetCodecForObject(pod)
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	err = codec.DecodeInto(originalJSON, pod)
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	// ensure pod is valid
	if errs := validation.ValidatePod(pod); len(errs) > 0 {
		t.Fatalf("pod should be valid: %v", errs)
	}

	// Round-trip with unstructured codec
	unstructuredObj, err := runtime.UnstructuredJSONScheme.Decode(originalJSON)
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	roundtripJSON, err := json.Marshal(unstructuredObj.(*runtime.Unstructured).Object)
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}

	// Decode with structured codec again
	pod2 := &api.Pod{}
	err = codec.DecodeInto(roundtripJSON, pod2)
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	// ensure pod is still valid
	if errs := validation.ValidatePod(pod2); len(errs) > 0 {
		t.Fatalf("pod should be valid: %v", errs)
	}
	// ensure round-trip preserved large integers
	if !reflect.DeepEqual(pod, pod2) {
		t.Fatalf("Expected\n\t%#v, got \n\t%#v", pod, pod2)
	}
}
開發者ID:erinboyd,項目名稱:origin,代碼行數:54,代碼來源:unstructured_test.go

示例2: roundTripSame

// roundTripSame verifies the same source object is tested in all API versions.
func roundTripSame(t *testing.T, item runtime.Object, except ...string) {
	set := sets.NewString(except...)
	seed := rand.Int63()
	fuzzInternalObject(t, "", item, seed)

	codec, err := testapi.GetCodecForObject(item)
	if err != nil {
		t.Errorf("unexpected error: %v", err)
	}

	version := testapi.Default.Version()
	if !set.Has(version) {
		fuzzInternalObject(t, version, item, seed)
		roundTrip(t, codec, item)
	}
}
開發者ID:resouer,項目名稱:contrib,代碼行數:17,代碼來源:serialization_test.go

示例3: fuzzInternalObject

	"k8s.io/kubernetes/pkg/api"
	"k8s.io/kubernetes/pkg/api/meta"
	"k8s.io/kubernetes/pkg/api/testapi"
	apitesting "k8s.io/kubernetes/pkg/api/testing"
	"k8s.io/kubernetes/pkg/api/unversioned"
	"k8s.io/kubernetes/pkg/api/v1"
	"k8s.io/kubernetes/pkg/runtime"
	"k8s.io/kubernetes/pkg/util/diff"
	"k8s.io/kubernetes/pkg/util/sets"
)

var fuzzIters = flag.Int("fuzz-iters", 20, "How many fuzzing iterations to do.")

var codecsToTest = []func(version unversioned.GroupVersion, item runtime.Object) (runtime.Codec, error){
	func(version unversioned.GroupVersion, item runtime.Object) (runtime.Codec, error) {
		return testapi.GetCodecForObject(item)
	},
}

func fuzzInternalObject(t *testing.T, forVersion unversioned.GroupVersion, item runtime.Object, seed int64) runtime.Object {
	apitesting.FuzzerFor(t, forVersion, rand.NewSource(seed)).Fuzz(item)

	j, err := meta.TypeAccessor(item)
	if err != nil {
		t.Fatalf("Unexpected error %v for %#v", err, item)
	}
	j.SetKind("")
	j.SetAPIVersion("")

	return item
}
開發者ID:xiaohui,項目名稱:kubernetes,代碼行數:31,代碼來源:serialization_test.go

示例4: fuzzInternalObject

	"k8s.io/apimachinery/pkg/watch"
	"k8s.io/kubernetes/pkg/api"
	"k8s.io/kubernetes/pkg/api/testapi"
	apitesting "k8s.io/kubernetes/pkg/api/testing"
	"k8s.io/kubernetes/pkg/api/v1"
	"k8s.io/kubernetes/pkg/apis/extensions"
	"k8s.io/kubernetes/pkg/apis/extensions/v1beta1"
)

var fuzzIters = flag.Int("fuzz-iters", 20, "How many fuzzing iterations to do.")

// codecsToTest is a list of functions that yield the codecs to use to test a
// particular runtime object.
var codecsToTest = []func(version schema.GroupVersion, item runtime.Object) (runtime.Codec, bool, error){
	func(version schema.GroupVersion, item runtime.Object) (runtime.Codec, bool, error) {
		c, err := testapi.GetCodecForObject(item)
		return c, true, err
	},
}

// fuzzInternalObject fuzzes an arbitrary runtime object using the appropriate
// fuzzer registered with the apitesting package.
func fuzzInternalObject(t *testing.T, forVersion schema.GroupVersion, item runtime.Object, seed int64) runtime.Object {
	apitesting.FuzzerFor(t, forVersion, rand.NewSource(seed)).Fuzz(item)

	j, err := meta.TypeAccessor(item)
	if err != nil {
		t.Fatalf("Unexpected error %v for %#v", err, item)
	}
	j.SetKind("")
	j.SetAPIVersion("")
開發者ID:kubernetes,項目名稱:kubernetes,代碼行數:31,代碼來源:serialization_test.go

示例5: TestDecodeNumbers

func TestDecodeNumbers(t *testing.T) {

	// Start with a valid pod
	originalJSON := []byte(`{
		"kind":"Pod",
		"apiVersion":"v1",
		"metadata":{"name":"pod","namespace":"foo"},
		"spec":{
			"containers":[{"name":"container","image":"container"}],
			"activeDeadlineSeconds":1000030003
		}
	}`)

	pod := &api.Pod{}

	// Decode with structured codec
	codec, err := testapi.GetCodecForObject(pod)
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	err = runtime.DecodeInto(codec, originalJSON, pod)
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	// ensure pod is valid
	if errs := validation.ValidatePod(pod); len(errs) > 0 {
		t.Fatalf("pod should be valid: %v", errs)
	}

	// Round-trip with unstructured codec
	unstructuredObj, err := runtime.Decode(runtime.UnstructuredJSONScheme, originalJSON)
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	roundtripJSON, err := runtime.Encode(runtime.UnstructuredJSONScheme, unstructuredObj)
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}

	// Make sure we serialize back out in int form
	if !strings.Contains(string(roundtripJSON), `"activeDeadlineSeconds":1000030003`) {
		t.Errorf("Expected %s, got %s", `"activeDeadlineSeconds":1000030003`, string(roundtripJSON))
	}

	// Decode with structured codec again
	obj2, err := runtime.Decode(codec, roundtripJSON)
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	// ensure pod is still valid
	pod2, ok := obj2.(*api.Pod)
	if !ok {
		t.Fatalf("expected an *api.Pod, got %#v", obj2)
	}
	if errs := validation.ValidatePod(pod2); len(errs) > 0 {
		t.Fatalf("pod should be valid: %v", errs)
	}
	// ensure round-trip preserved large integers
	if !reflect.DeepEqual(pod, pod2) {
		t.Fatalf("Expected\n\t%#v, got \n\t%#v", pod, pod2)
	}
}
開發者ID:ncdc,項目名稱:kubernetes,代碼行數:62,代碼來源:unstructured_test.go

示例6: TestExampleObjectSchemas


//.........這裏部分代碼省略.........
			"admin-pod":      &api.Pod{},
			"admin-service":  &api.Service{},
			"driver-service": &api.Service{},
			"rc":             &api.ReplicationController{},
		},
		"../docs/user-guide/secrets": {
			"secret-pod":     &api.Pod{},
			"secret":         &api.Secret{},
			"secret-env-pod": &api.Pod{},
		},
		"../examples/spark": {
			"spark-master-controller": &api.ReplicationController{},
			"spark-master-service":    &api.Service{},
			"spark-webui":             &api.Service{},
			"spark-worker-controller": &api.ReplicationController{},
			"zeppelin-controller":     &api.ReplicationController{},
			"zeppelin-service":        &api.Service{},
		},
		"../examples/spark/spark-gluster": {
			"spark-master-service":    &api.Service{},
			"spark-master-controller": &api.ReplicationController{},
			"spark-worker-controller": &api.ReplicationController{},
			"glusterfs-endpoints":     &api.Endpoints{},
		},
		"../examples/storm": {
			"storm-nimbus-service":    &api.Service{},
			"storm-nimbus":            &api.Pod{},
			"storm-worker-controller": &api.ReplicationController{},
			"zookeeper-service":       &api.Service{},
			"zookeeper":               &api.Pod{},
		},
		"../examples/cephfs/": {
			"cephfs":             &api.Pod{},
			"cephfs-with-secret": &api.Pod{},
		},
		"../examples/fibre_channel": {
			"fc": &api.Pod{},
		},
		"../examples/extensions": {
			"deployment": &extensions.Deployment{},
		},
		"../examples/javaweb-tomcat-sidecar": {
			"javaweb":   &api.Pod{},
			"javaweb-2": &api.Pod{},
		},
		"../examples/job/work-queue-1": {
			"job": &extensions.Job{},
		},
		"../examples/job/work-queue-2": {
			"redis-pod":     &api.Pod{},
			"redis-service": &api.Service{},
			"job":           &extensions.Job{},
		},
	}

	capabilities.SetForTests(capabilities.Capabilities{
		AllowPrivileged: true,
	})

	for path, expected := range cases {
		tested := 0
		err := walkJSONFiles(path, func(name, path string, data []byte) {
			expectedType, found := expected[name]
			if !found {
				t.Errorf("%s: %s does not have a test case defined", path, name)
				return
			}
			tested++
			if expectedType == nil {
				t.Logf("skipping : %s/%s\n", path, name)
				return
			}
			if strings.Contains(name, "scheduler-policy-config") {
				if err := runtime.DecodeInto(schedulerapilatest.Codec, data, expectedType); err != nil {
					t.Errorf("%s did not decode correctly: %v\n%s", path, err, string(data))
					return
				}
				//TODO: Add validate method for &schedulerapi.Policy
			} else {
				codec, err := testapi.GetCodecForObject(expectedType)
				if err != nil {
					t.Errorf("Could not get codec for %s: %s", expectedType, err)
				}
				if err := runtime.DecodeInto(codec, data, expectedType); err != nil {
					t.Errorf("%s did not decode correctly: %v\n%s", path, err, string(data))
					return
				}
				if errors := validateObject(expectedType); len(errors) > 0 {
					t.Errorf("%s did not validate correctly: %v", path, errors)
				}
			}
		})
		if err != nil {
			t.Errorf("Expected no error, Got %v", err)
		}
		if tested != len(expected) {
			t.Errorf("Directory %v: Expected %d examples, Got %d", path, len(expected), tested)
		}
	}
}
開發者ID:koori02,項目名稱:kubernetes,代碼行數:101,代碼來源:examples_test.go

示例7: TestExampleObjectSchemas


//.........這裏部分代碼省略.........
		},
		"../examples/storage/redis": {
			"redis-controller":          &api.ReplicationController{},
			"redis-master":              &api.Pod{},
			"redis-proxy":               &api.Pod{},
			"redis-sentinel-controller": &api.ReplicationController{},
			"redis-sentinel-service":    &api.Service{},
		},
		"../examples/storage/rethinkdb": {
			"admin-pod":      &api.Pod{},
			"admin-service":  &api.Service{},
			"driver-service": &api.Service{},
			"rc":             &api.ReplicationController{},
		},
		"../examples/spark": {
			"namespace-spark-cluster": &api.Namespace{},
			"spark-master-controller": &api.ReplicationController{},
			"spark-master-service":    &api.Service{},
			"spark-webui":             &api.Service{},
			"spark-worker-controller": &api.ReplicationController{},
			"zeppelin-controller":     &api.ReplicationController{},
			"zeppelin-service":        &api.Service{},
		},
		"../examples/spark/spark-gluster": {
			"spark-master-service":    &api.Service{},
			"spark-master-controller": &api.ReplicationController{},
			"spark-worker-controller": &api.ReplicationController{},
			"glusterfs-endpoints":     &api.Endpoints{},
		},
		"../examples/storm": {
			"storm-nimbus-service":    &api.Service{},
			"storm-nimbus":            &api.Pod{},
			"storm-worker-controller": &api.ReplicationController{},
			"zookeeper-service":       &api.Service{},
			"zookeeper":               &api.Pod{},
		},
		"../examples/volumes/cephfs/": {
			"cephfs":             &api.Pod{},
			"cephfs-with-secret": &api.Pod{},
		},
		"../examples/volumes/fibre_channel": {
			"fc": &api.Pod{},
		},
		"../examples/javaweb-tomcat-sidecar": {
			"javaweb":   &api.Pod{},
			"javaweb-2": &api.Pod{},
		},
		"../examples/volumes/azure_file": {
			"azure": &api.Pod{},
		},
		"../examples/volumes/azure_disk": {
			"azure": &api.Pod{},
		},
	}

	capabilities.SetForTests(capabilities.Capabilities{
		AllowPrivileged: true,
	})

	for path, expected := range cases {
		tested := 0
		err := walkJSONFiles(path, func(name, path string, data []byte) {
			expectedType, found := expected[name]
			if !found {
				t.Errorf("%s: %s does not have a test case defined", path, name)
				return
			}
			tested++
			if expectedType == nil {
				t.Logf("skipping : %s/%s\n", path, name)
				return
			}
			if strings.Contains(name, "scheduler-policy-config") {
				if err := runtime.DecodeInto(schedulerapilatest.Codec, data, expectedType); err != nil {
					t.Errorf("%s did not decode correctly: %v\n%s", path, err, string(data))
					return
				}
				//TODO: Add validate method for &schedulerapi.Policy
			} else {
				codec, err := testapi.GetCodecForObject(expectedType)
				if err != nil {
					t.Errorf("Could not get codec for %s: %s", expectedType, err)
				}
				if err := runtime.DecodeInto(codec, data, expectedType); err != nil {
					t.Errorf("%s did not decode correctly: %v\n%s", path, err, string(data))
					return
				}
				if errors := validateObject(expectedType); len(errors) > 0 {
					t.Errorf("%s did not validate correctly: %v", path, errors)
				}
			}
		})
		if err != nil {
			t.Errorf("Expected no error, Got %v", err)
		}
		if tested != len(expected) {
			t.Errorf("Directory %v: Expected %d examples, Got %d", path, len(expected), tested)
		}
	}
}
開發者ID:ncdc,項目名稱:kubernetes,代碼行數:101,代碼來源:examples_test.go

示例8: TestExampleObjectSchemas


//.........這裏部分代碼省略.........
		},
		"../docs/admin/namespaces": {
			"namespace-dev":  {&api.Namespace{}},
			"namespace-prod": {&api.Namespace{}},
		},
		"../docs/admin/limitrange": {
			"invalid-pod": {&api.Pod{}},
			"limits":      {&api.LimitRange{}},
			"namespace":   {&api.Namespace{}},
			"valid-pod":   {&api.Pod{}},
		},
		"../docs/user-guide/logging-demo": {
			"synthetic_0_25lps": {&api.Pod{}},
			"synthetic_10lps":   {&api.Pod{}},
		},
		"../docs/user-guide/node-selection": {
			"pod": {&api.Pod{}},
			"pod-with-node-affinity": {&api.Pod{}},
			"pod-with-pod-affinity":  {&api.Pod{}},
		},
		"../docs/admin/resourcequota": {
			"best-effort":       {&api.ResourceQuota{}},
			"compute-resources": {&api.ResourceQuota{}},
			"limits":            {&api.LimitRange{}},
			"namespace":         {&api.Namespace{}},
			"not-best-effort":   {&api.ResourceQuota{}},
			"object-counts":     {&api.ResourceQuota{}},
		},
		"../docs/user-guide/secrets": {
			"secret-pod":     {&api.Pod{}},
			"secret":         {&api.Secret{}},
			"secret-env-pod": {&api.Pod{}},
		},
		"../docs/tutorials/stateful-application": {
			"gce-volume":        {&api.PersistentVolume{}},
			"mysql-deployment":  {&api.Service{}, &api.PersistentVolumeClaim{}, &extensions.Deployment{}},
			"mysql-services":    {&api.Service{}, &api.Service{}},
			"mysql-configmap":   {&api.ConfigMap{}},
			"mysql-statefulset": {&apps.StatefulSet{}},
			"web":               {&api.Service{}, &apps.StatefulSet{}},
			"zookeeper":         {&api.Service{}, &api.ConfigMap{}, &policy.PodDisruptionBudget{}, &apps.StatefulSet{}},
		},
	}

	capabilities.SetForTests(capabilities.Capabilities{
		AllowPrivileged: true,
	})

	for path, expected := range cases {
		tested := 0
		numExpected := 0
		err := walkConfigFiles(path, func(name, path string, docs [][]byte) {
			expectedTypes, found := expected[name]
			if !found {
				t.Errorf("%s: %s does not have a test case defined", path, name)
				return
			}
			numExpected += len(expectedTypes)
			if len(expectedTypes) != len(docs) {
				t.Errorf("%s: number of expected types (%v) doesn't match number of docs in YAML (%v)", path, len(expectedTypes), len(docs))
				return
			}
			for i, data := range docs {
				expectedType := expectedTypes[i]
				tested++
				if expectedType == nil {
					t.Logf("skipping : %s/%s\n", path, name)
					return
				}
				if strings.Contains(name, "scheduler-policy-config") {
					if err := runtime.DecodeInto(schedulerapilatest.Codec, data, expectedType); err != nil {
						t.Errorf("%s did not decode correctly: %v\n%s", path, err, string(data))
						return
					}
					// TODO: Add validate method for
					// &schedulerapi.Policy, and remove this
					// special case
				} else {
					codec, err := testapi.GetCodecForObject(expectedType)
					if err != nil {
						t.Errorf("Could not get codec for %s: %s", expectedType, err)
					}
					if err := runtime.DecodeInto(codec, data, expectedType); err != nil {
						t.Errorf("%s did not decode correctly: %v\n%s", path, err, string(data))
						return
					}
					if errors := validateObject(expectedType); len(errors) > 0 {
						t.Errorf("%s did not validate correctly: %v", path, errors)
					}
				}
			}
		})
		if err != nil {
			t.Errorf("Expected no error, Got %v on Path %v", err, path)
		}
		if tested != numExpected {
			t.Errorf("Directory %v: Expected %d examples, Got %d", path, len(expected), tested)
		}
	}
}
開發者ID:kubernetes,項目名稱:kubernetes.github.io,代碼行數:101,代碼來源:examples_test.go


注:本文中的k8s/io/kubernetes/pkg/api/testapi.GetCodecForObject函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。