patch_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. Copyright 2019 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 service
  14. import (
  15. "context"
  16. "reflect"
  17. "testing"
  18. v1 "k8s.io/api/core/v1"
  19. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  20. "k8s.io/client-go/kubernetes/fake"
  21. )
  22. func addAnnotations(svc *v1.Service) {
  23. svc.Annotations["foo"] = "bar"
  24. }
  25. func TestPatch(t *testing.T) {
  26. svcOrigin := &v1.Service{
  27. ObjectMeta: metav1.ObjectMeta{
  28. Name: "test-patch",
  29. Annotations: map[string]string{},
  30. },
  31. Spec: v1.ServiceSpec{
  32. ClusterIP: "10.0.0.1",
  33. },
  34. }
  35. fakeCs := fake.NewSimpleClientset(svcOrigin)
  36. // Issue a separate update and verify patch doesn't fail after this.
  37. svcToUpdate := svcOrigin.DeepCopy()
  38. addAnnotations(svcToUpdate)
  39. if _, err := fakeCs.CoreV1().Services(svcOrigin.Namespace).Update(context.TODO(), svcToUpdate, metav1.UpdateOptions{}); err != nil {
  40. t.Fatalf("Failed to update service: %v", err)
  41. }
  42. // Attempt to patch based the original service.
  43. svcToPatch := svcOrigin.DeepCopy()
  44. svcToPatch.Finalizers = []string{"foo"}
  45. svcToPatch.Spec.ClusterIP = "10.0.0.2"
  46. svcToPatch.Status = v1.ServiceStatus{
  47. LoadBalancer: v1.LoadBalancerStatus{
  48. Ingress: []v1.LoadBalancerIngress{
  49. {IP: "8.8.8.8"},
  50. },
  51. },
  52. }
  53. svcPatched, err := patch(fakeCs.CoreV1(), svcOrigin, svcToPatch)
  54. if err != nil {
  55. t.Fatalf("Failed to patch service: %v", err)
  56. }
  57. // Service returned by patch will contain latest content (e.g from
  58. // the separate update).
  59. addAnnotations(svcToPatch)
  60. if !reflect.DeepEqual(svcPatched, svcToPatch) {
  61. t.Errorf("PatchStatus() = %+v, want %+v", svcPatched, svcToPatch)
  62. }
  63. // Explicitly validate if spec is unchanged from origin.
  64. if !reflect.DeepEqual(svcPatched.Spec, svcOrigin.Spec) {
  65. t.Errorf("Got spec = %+v, want %+v", svcPatched.Spec, svcOrigin.Spec)
  66. }
  67. }
  68. func TestGetPatchBytes(t *testing.T) {
  69. origin := &v1.Service{
  70. ObjectMeta: metav1.ObjectMeta{
  71. Name: "test-patch-bytes",
  72. Finalizers: []string{"foo"},
  73. },
  74. }
  75. updated := &v1.Service{
  76. ObjectMeta: metav1.ObjectMeta{
  77. Name: "test-patch-bytes",
  78. Finalizers: []string{"foo", "bar"},
  79. },
  80. }
  81. b, err := getPatchBytes(origin, updated)
  82. if err != nil {
  83. t.Fatal(err)
  84. }
  85. expected := `{"metadata":{"$setElementOrder/finalizers":["foo","bar"],"finalizers":["bar"]}}`
  86. if string(b) != expected {
  87. t.Errorf("getPatchBytes(%+v, %+v) = %s ; want %s", origin, updated, string(b), expected)
  88. }
  89. }