utils_test.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. Copyright 2015 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 bandwidth
  14. import (
  15. "reflect"
  16. "testing"
  17. "k8s.io/kubernetes/pkg/api"
  18. "k8s.io/kubernetes/pkg/api/resource"
  19. )
  20. func TestExtractPodBandwidthResources(t *testing.T) {
  21. four, _ := resource.ParseQuantity("4M")
  22. ten, _ := resource.ParseQuantity("10M")
  23. twenty, _ := resource.ParseQuantity("20M")
  24. testPod := func(ingress, egress string) *api.Pod {
  25. pod := &api.Pod{ObjectMeta: api.ObjectMeta{Annotations: map[string]string{}}}
  26. if len(ingress) != 0 {
  27. pod.Annotations["kubernetes.io/ingress-bandwidth"] = ingress
  28. }
  29. if len(egress) != 0 {
  30. pod.Annotations["kubernetes.io/egress-bandwidth"] = egress
  31. }
  32. return pod
  33. }
  34. tests := []struct {
  35. pod *api.Pod
  36. expectedIngress *resource.Quantity
  37. expectedEgress *resource.Quantity
  38. expectError bool
  39. }{
  40. {
  41. pod: &api.Pod{},
  42. },
  43. {
  44. pod: testPod("10M", ""),
  45. expectedIngress: &ten,
  46. },
  47. {
  48. pod: testPod("", "10M"),
  49. expectedEgress: &ten,
  50. },
  51. {
  52. pod: testPod("4M", "20M"),
  53. expectedIngress: &four,
  54. expectedEgress: &twenty,
  55. },
  56. {
  57. pod: testPod("foo", ""),
  58. expectError: true,
  59. },
  60. }
  61. for _, test := range tests {
  62. ingress, egress, err := ExtractPodBandwidthResources(test.pod.Annotations)
  63. if test.expectError {
  64. if err == nil {
  65. t.Errorf("unexpected non-error")
  66. }
  67. continue
  68. }
  69. if err != nil {
  70. t.Errorf("unexpected error: %v", err)
  71. continue
  72. }
  73. if !reflect.DeepEqual(ingress, test.expectedIngress) {
  74. t.Errorf("expected: %v, saw: %v", ingress, test.expectedIngress)
  75. }
  76. if !reflect.DeepEqual(egress, test.expectedEgress) {
  77. t.Errorf("expected: %v, saw: %v", egress, test.expectedEgress)
  78. }
  79. }
  80. }