コード例 #1
0
func TestDynamicClient(t *testing.T) {
	_, s := framework.RunAMaster(t)
	defer s.Close()

	framework.DeleteAllEtcdKeys()
	gv := testapi.Default.GroupVersion()
	config := &restclient.Config{
		Host:          s.URL,
		ContentConfig: restclient.ContentConfig{GroupVersion: gv},
	}

	client := uclient.NewOrDie(config)
	dynamicClient, err := dynamic.NewClient(config)
	_ = dynamicClient
	if err != nil {
		t.Fatalf("unexpected error creating dynamic client: %v", err)
	}

	// Find the Pod resource
	resources, err := client.Discovery().ServerResourcesForGroupVersion(gv.String())
	if err != nil {
		t.Fatalf("unexpected error listing resources: %v", err)
	}

	var resource unversioned.APIResource
	for _, r := range resources.APIResources {
		if r.Kind == "Pod" {
			resource = r
			break
		}
	}

	if len(resource.Name) == 0 {
		t.Fatalf("could not find the pod resource in group/version %q", gv.String())
	}

	// Create a Pod with the normal client
	pod := &api.Pod{
		ObjectMeta: api.ObjectMeta{
			GenerateName: "test",
		},
		Spec: api.PodSpec{
			Containers: []api.Container{
				{
					Name:  "test",
					Image: "test-image",
				},
			},
		},
	}

	actual, err := client.Pods(framework.TestNS).Create(pod)
	if err != nil {
		t.Fatalf("unexpected error when creating pod: %v", err)
	}

	// check dynamic list
	unstructuredList, err := dynamicClient.Resource(&resource, framework.TestNS).List(v1.ListOptions{})
	if err != nil {
		t.Fatalf("unexpected error when listing pods: %v", err)
	}

	if len(unstructuredList.Items) != 1 {
		t.Fatalf("expected one pod, got %d", len(unstructuredList.Items))
	}

	got, err := unstructuredToPod(unstructuredList.Items[0])
	if err != nil {
		t.Fatalf("unexpected error converting Unstructured to api.Pod: %v", err)
	}

	if !reflect.DeepEqual(actual, got) {
		t.Fatalf("unexpected pod in list. wanted %#v, got %#v", actual, got)
	}

	// check dynamic get
	unstruct, err := dynamicClient.Resource(&resource, framework.TestNS).Get(actual.Name)
	if err != nil {
		t.Fatalf("unexpected error when getting pod %q: %v", actual.Name, err)
	}

	got, err = unstructuredToPod(unstruct)
	if err != nil {
		t.Fatalf("unexpected error converting Unstructured to api.Pod: %v", err)
	}

	if !reflect.DeepEqual(actual, got) {
		t.Fatalf("unexpected pod in list. wanted %#v, got %#v", actual, got)
	}

	// delete the pod dynamically
	err = dynamicClient.Resource(&resource, framework.TestNS).Delete(actual.Name, nil)
	if err != nil {
		t.Fatalf("unexpected error when deleting pod: %v", err)
	}

	list, err := client.Pods(framework.TestNS).List(api.ListOptions{})
	if err != nil {
		t.Fatalf("unexpected error when listing pods: %v", err)
	}

	if len(list.Items) != 0 {
		t.Fatalf("expected zero pods, got %d", len(list.Items))
	}
}
コード例 #2
0
func TestDynamicClient(t *testing.T) {
	_, s := framework.RunAMaster(nil)
	defer s.Close()

	ns := framework.CreateTestingNamespace("dynamic-client", s, t)
	defer framework.DeleteTestingNamespace(ns, s, t)

	gv := &registered.GroupOrDie(v1.GroupName).GroupVersion
	config := &restclient.Config{
		Host:          s.URL,
		ContentConfig: restclient.ContentConfig{GroupVersion: gv},
	}

	client := clientset.NewForConfigOrDie(config)
	dynamicClient, err := dynamic.NewClient(config)
	_ = dynamicClient
	if err != nil {
		t.Fatalf("unexpected error creating dynamic client: %v", err)
	}

	// Find the Pod resource
	resources, err := client.Discovery().ServerResourcesForGroupVersion(gv.String())
	if err != nil {
		t.Fatalf("unexpected error listing resources: %v", err)
	}

	var resource metav1.APIResource
	for _, r := range resources.APIResources {
		if r.Kind == "Pod" {
			resource = r
			break
		}
	}

	if len(resource.Name) == 0 {
		t.Fatalf("could not find the pod resource in group/version %q", gv.String())
	}

	// Create a Pod with the normal client
	pod := &v1.Pod{
		ObjectMeta: v1.ObjectMeta{
			GenerateName: "test",
		},
		Spec: v1.PodSpec{
			Containers: []v1.Container{
				{
					Name:  "test",
					Image: "test-image",
				},
			},
		},
	}

	actual, err := client.Core().Pods(ns.Name).Create(pod)
	if err != nil {
		t.Fatalf("unexpected error when creating pod: %v", err)
	}

	// check dynamic list
	obj, err := dynamicClient.Resource(&resource, ns.Name).List(&v1.ListOptions{})
	unstructuredList, ok := obj.(*runtime.UnstructuredList)
	if !ok {
		t.Fatalf("expected *runtime.UnstructuredList, got %#v", obj)
	}
	if err != nil {
		t.Fatalf("unexpected error when listing pods: %v", err)
	}

	if len(unstructuredList.Items) != 1 {
		t.Fatalf("expected one pod, got %d", len(unstructuredList.Items))
	}

	got, err := unstructuredToPod(unstructuredList.Items[0])
	if err != nil {
		t.Fatalf("unexpected error converting Unstructured to v1.Pod: %v", err)
	}

	if !reflect.DeepEqual(actual, got) {
		t.Fatalf("unexpected pod in list. wanted %#v, got %#v", actual, got)
	}

	// check dynamic get
	unstruct, err := dynamicClient.Resource(&resource, ns.Name).Get(actual.Name)
	if err != nil {
		t.Fatalf("unexpected error when getting pod %q: %v", actual.Name, err)
	}

	got, err = unstructuredToPod(unstruct)
	if err != nil {
		t.Fatalf("unexpected error converting Unstructured to v1.Pod: %v", err)
	}

	if !reflect.DeepEqual(actual, got) {
		t.Fatalf("unexpected pod in list. wanted %#v, got %#v", actual, got)
	}

	// delete the pod dynamically
	err = dynamicClient.Resource(&resource, ns.Name).Delete(actual.Name, nil)
	if err != nil {
		t.Fatalf("unexpected error when deleting pod: %v", err)
	}

	list, err := client.Core().Pods(ns.Name).List(v1.ListOptions{})
	if err != nil {
		t.Fatalf("unexpected error when listing pods: %v", err)
	}

	if len(list.Items) != 0 {
		t.Fatalf("expected zero pods, got %d", len(list.Items))
	}
}