default_binder_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. Copyright 2020 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package defaultbinder
  14. import (
  15. "context"
  16. "errors"
  17. "testing"
  18. "github.com/google/go-cmp/cmp"
  19. v1 "k8s.io/api/core/v1"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. "k8s.io/apimachinery/pkg/runtime"
  22. "k8s.io/client-go/kubernetes/fake"
  23. clienttesting "k8s.io/client-go/testing"
  24. framework "k8s.io/kubernetes/pkg/scheduler/framework/v1alpha1"
  25. )
  26. func TestDefaultBinder(t *testing.T) {
  27. testPod := &v1.Pod{
  28. ObjectMeta: metav1.ObjectMeta{Name: "foo", Namespace: "ns"},
  29. }
  30. testNode := "foohost.kubernetes.mydomain.com"
  31. tests := []struct {
  32. name string
  33. injectErr error
  34. wantBinding *v1.Binding
  35. }{
  36. {
  37. name: "successful",
  38. wantBinding: &v1.Binding{
  39. ObjectMeta: metav1.ObjectMeta{Namespace: "ns", Name: "foo"},
  40. Target: v1.ObjectReference{Kind: "Node", Name: testNode},
  41. },
  42. }, {
  43. name: "binding error",
  44. injectErr: errors.New("binding error"),
  45. },
  46. }
  47. for _, tt := range tests {
  48. t.Run(tt.name, func(t *testing.T) {
  49. var gotBinding *v1.Binding
  50. client := fake.NewSimpleClientset(testPod)
  51. client.PrependReactor("create", "pods", func(action clienttesting.Action) (bool, runtime.Object, error) {
  52. if action.GetSubresource() != "binding" {
  53. return false, nil, nil
  54. }
  55. if tt.injectErr != nil {
  56. return true, nil, tt.injectErr
  57. }
  58. gotBinding = action.(clienttesting.CreateAction).GetObject().(*v1.Binding)
  59. return true, gotBinding, nil
  60. })
  61. fh, err := framework.NewFramework(nil, nil, nil, framework.WithClientSet(client))
  62. if err != nil {
  63. t.Fatal(err)
  64. }
  65. binder := &DefaultBinder{handle: fh}
  66. status := binder.Bind(context.Background(), nil, testPod, "foohost.kubernetes.mydomain.com")
  67. if got := status.AsError(); (tt.injectErr != nil) != (got != nil) {
  68. t.Errorf("got error %q, want %q", got, tt.injectErr)
  69. }
  70. if diff := cmp.Diff(tt.wantBinding, gotBinding); diff != "" {
  71. t.Errorf("got different binding (-want, +got): %s", diff)
  72. }
  73. })
  74. }
  75. }