identity_mappers_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. Copyright 2016 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 petset
  14. import (
  15. "fmt"
  16. "reflect"
  17. "strings"
  18. "testing"
  19. "k8s.io/kubernetes/pkg/api"
  20. api_pod "k8s.io/kubernetes/pkg/api/pod"
  21. )
  22. func TestPetIDName(t *testing.T) {
  23. replicas := 3
  24. ps := newPetSet(replicas)
  25. for i := 0; i < replicas; i++ {
  26. petName := fmt.Sprintf("%v-%d", ps.Name, i)
  27. pcb, err := newPCB(fmt.Sprintf("%d", i), ps)
  28. if err != nil {
  29. t.Fatalf("Failed to generate pet %v", err)
  30. }
  31. pod := pcb.pod
  32. if pod.Name != petName || pod.Namespace != ps.Namespace {
  33. t.Errorf("Wrong name identity, expected %v", pcb.pod.Name)
  34. }
  35. }
  36. }
  37. func TestPetIDDNS(t *testing.T) {
  38. replicas := 3
  39. ps := newPetSet(replicas)
  40. for i := 0; i < replicas; i++ {
  41. petName := fmt.Sprintf("%v-%d", ps.Name, i)
  42. petSubdomain := ps.Spec.ServiceName
  43. pcb, err := newPCB(fmt.Sprintf("%d", i), ps)
  44. pod := pcb.pod
  45. if err != nil {
  46. t.Fatalf("Failed to generate pet %v", err)
  47. }
  48. if hostname, ok := pod.Annotations[api_pod.PodHostnameAnnotation]; !ok || hostname != petName {
  49. t.Errorf("Wrong hostname: %v", hostname)
  50. }
  51. // TODO: Check this against the governing service.
  52. if subdomain, ok := pod.Annotations[api_pod.PodSubdomainAnnotation]; !ok || subdomain != petSubdomain {
  53. t.Errorf("Wrong subdomain: %v", subdomain)
  54. }
  55. }
  56. }
  57. func TestPetIDVolume(t *testing.T) {
  58. replicas := 3
  59. ps := newPetSet(replicas)
  60. for i := 0; i < replicas; i++ {
  61. pcb, err := newPCB(fmt.Sprintf("%d", i), ps)
  62. if err != nil {
  63. t.Fatalf("Failed to generate pet %v", err)
  64. }
  65. pod := pcb.pod
  66. petName := fmt.Sprintf("%v-%d", ps.Name, i)
  67. claimName := fmt.Sprintf("datadir-%v", petName)
  68. for _, v := range pod.Spec.Volumes {
  69. switch v.Name {
  70. case "datadir":
  71. c := v.VolumeSource.PersistentVolumeClaim
  72. if c == nil || c.ClaimName != claimName {
  73. t.Fatalf("Unexpected claim %v", c)
  74. }
  75. if err := checkPodForMount(pod, "datadir"); err != nil {
  76. t.Errorf("Expected pod mount: %v", err)
  77. }
  78. case "home":
  79. h := v.VolumeSource.HostPath
  80. if h == nil || h.Path != "/tmp/home" {
  81. t.Errorf("Unexpected modification to hostpath, expected /tmp/home got %+v", h)
  82. }
  83. default:
  84. t.Errorf("Unexpected volume %v", v.Name)
  85. }
  86. }
  87. }
  88. // TODO: Check volume mounts.
  89. }
  90. func TestPetIDVolumeClaims(t *testing.T) {
  91. replicas := 3
  92. ps := newPetSet(replicas)
  93. for i := 0; i < replicas; i++ {
  94. pcb, err := newPCB(fmt.Sprintf("%v", i), ps)
  95. if err != nil {
  96. t.Fatalf("Failed to generate pet %v", err)
  97. }
  98. pvcs := pcb.pvcs
  99. petName := fmt.Sprintf("%v-%d", ps.Name, i)
  100. claimName := fmt.Sprintf("datadir-%v", petName)
  101. if len(pvcs) != 1 || pvcs[0].Name != claimName {
  102. t.Errorf("Wrong pvc expected %v got %v", claimName, pvcs[0].Name)
  103. }
  104. }
  105. }
  106. func TestPetIDCrossAssignment(t *testing.T) {
  107. replicas := 3
  108. ps := newPetSet(replicas)
  109. nameMapper := &NameIdentityMapper{ps}
  110. volumeMapper := &VolumeIdentityMapper{ps}
  111. networkMapper := &NetworkIdentityMapper{ps}
  112. // Check that the name is consistent across identity.
  113. for i := 0; i < replicas; i++ {
  114. pet, _ := newPCB(fmt.Sprintf("%v", i), ps)
  115. p := pet.pod
  116. name := strings.Split(nameMapper.Identity(p), "/")[1]
  117. network := networkMapper.Identity(p)
  118. volume := volumeMapper.Identity(p)
  119. petVolume := strings.Split(volume, ":")[1]
  120. if petVolume != fmt.Sprintf("datadir-%v", name) {
  121. t.Errorf("Unexpected pet volume name %v, expected %v", petVolume, name)
  122. }
  123. if network != fmt.Sprintf("%v.%v.%v", name, ps.Spec.ServiceName, ps.Namespace) {
  124. t.Errorf("Unexpected pet network ID %v, expected %v", network, name)
  125. }
  126. t.Logf("[%v] volume: %+v, network: %+v, name: %+v", i, volume, network, name)
  127. }
  128. }
  129. func TestPetIDReset(t *testing.T) {
  130. replicas := 2
  131. ps := newPetSet(replicas)
  132. firstPCB, err := newPCB("1", ps)
  133. secondPCB, err := newPCB("2", ps)
  134. if identityHash(ps, firstPCB.pod) == identityHash(ps, secondPCB.pod) {
  135. t.Fatalf("Failed to generate uniquey identities:\n%+v\n%+v", firstPCB.pod.Spec, secondPCB.pod.Spec)
  136. }
  137. userAdded := api.Volume{
  138. Name: "test",
  139. VolumeSource: api.VolumeSource{
  140. EmptyDir: &api.EmptyDirVolumeSource{Medium: api.StorageMediumMemory},
  141. },
  142. }
  143. firstPCB.pod.Spec.Volumes = append(firstPCB.pod.Spec.Volumes, userAdded)
  144. pod, needsUpdate, err := copyPetID(firstPCB, secondPCB)
  145. if err != nil {
  146. t.Errorf("%v", err)
  147. }
  148. if !needsUpdate {
  149. t.Errorf("expected update since identity of %v was reset", secondPCB.pod.Name)
  150. }
  151. if identityHash(ps, &pod) != identityHash(ps, secondPCB.pod) {
  152. t.Errorf("Failed to copy identity for pod %v -> %v", firstPCB.pod.Name, secondPCB.pod.Name)
  153. }
  154. foundVol := false
  155. for _, v := range pod.Spec.Volumes {
  156. if reflect.DeepEqual(v, userAdded) {
  157. foundVol = true
  158. break
  159. }
  160. }
  161. if !foundVol {
  162. t.Errorf("User added volume was corrupted by reset action.")
  163. }
  164. }