checkpoint_manager_test.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. Copyright 2017 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 checkpointmanager
  14. import (
  15. "encoding/json"
  16. "sort"
  17. "testing"
  18. "github.com/stretchr/testify/assert"
  19. "k8s.io/kubernetes/pkg/kubelet/checkpointmanager/checksum"
  20. utilstore "k8s.io/kubernetes/pkg/kubelet/checkpointmanager/testing"
  21. "k8s.io/kubernetes/pkg/kubelet/checkpointmanager/testing/example_checkpoint_formats/v1"
  22. )
  23. var testStore *utilstore.MemStore
  24. type FakeCheckpoint interface {
  25. Checkpoint
  26. GetData() ([]*PortMapping, bool)
  27. }
  28. // Data contains all types of data that can be stored in the checkpoint.
  29. type Data struct {
  30. PortMappings []*PortMapping `json:"port_mappings,omitempty"`
  31. HostNetwork bool `json:"host_network,omitempty"`
  32. }
  33. type CheckpointDataV2 struct {
  34. PortMappings []*PortMapping `json:"port_mappings,omitempty"`
  35. HostNetwork bool `json:"host_network,omitempty"`
  36. V2Field string `json:"v2field"`
  37. }
  38. type protocol string
  39. // portMapping is the port mapping configurations of a sandbox.
  40. type PortMapping struct {
  41. // protocol of the port mapping.
  42. Protocol *protocol
  43. // Port number within the container.
  44. ContainerPort *int32
  45. // Port number on the host.
  46. HostPort *int32
  47. // Host ip to expose.
  48. HostIP string
  49. }
  50. // CheckpointData is a sample example structure to be used in test cases for checkpointing
  51. type CheckpointData struct {
  52. Version string
  53. Name string
  54. Data *Data
  55. Checksum checksum.Checksum
  56. }
  57. func newFakeCheckpointV1(name string, portMappings []*PortMapping, hostNetwork bool) FakeCheckpoint {
  58. return &CheckpointData{
  59. Version: "v1",
  60. Name: name,
  61. Data: &Data{
  62. PortMappings: portMappings,
  63. HostNetwork: hostNetwork,
  64. },
  65. }
  66. }
  67. func (cp *CheckpointData) MarshalCheckpoint() ([]byte, error) {
  68. cp.Checksum = checksum.New(*cp.Data)
  69. return json.Marshal(*cp)
  70. }
  71. func (cp *CheckpointData) UnmarshalCheckpoint(blob []byte) error {
  72. return json.Unmarshal(blob, cp)
  73. }
  74. func (cp *CheckpointData) VerifyChecksum() error {
  75. return cp.Checksum.Verify(*cp.Data)
  76. }
  77. func (cp *CheckpointData) GetData() ([]*PortMapping, bool) {
  78. return cp.Data.PortMappings, cp.Data.HostNetwork
  79. }
  80. type checkpointDataV2 struct {
  81. Version string
  82. Name string
  83. Data *CheckpointDataV2
  84. Checksum checksum.Checksum
  85. }
  86. func newFakeCheckpointV2(name string, portMappings []*PortMapping, hostNetwork bool) FakeCheckpoint {
  87. return &checkpointDataV2{
  88. Version: "v2",
  89. Name: name,
  90. Data: &CheckpointDataV2{
  91. PortMappings: portMappings,
  92. HostNetwork: hostNetwork,
  93. },
  94. }
  95. }
  96. func newFakeCheckpointRemoteV1(name string, portMappings []*v1.PortMapping, hostNetwork bool) Checkpoint {
  97. return &v1.CheckpointData{
  98. Version: "v1",
  99. Name: name,
  100. Data: &v1.Data{
  101. PortMappings: portMappings,
  102. HostNetwork: hostNetwork,
  103. },
  104. }
  105. }
  106. func (cp *checkpointDataV2) MarshalCheckpoint() ([]byte, error) {
  107. cp.Checksum = checksum.New(*cp.Data)
  108. return json.Marshal(*cp)
  109. }
  110. func (cp *checkpointDataV2) UnmarshalCheckpoint(blob []byte) error {
  111. return json.Unmarshal(blob, cp)
  112. }
  113. func (cp *checkpointDataV2) VerifyChecksum() error {
  114. return cp.Checksum.Verify(*cp.Data)
  115. }
  116. func (cp *checkpointDataV2) GetData() ([]*PortMapping, bool) {
  117. return cp.Data.PortMappings, cp.Data.HostNetwork
  118. }
  119. func newTestCheckpointManager() CheckpointManager {
  120. return &impl{store: testStore}
  121. }
  122. func TestCheckpointManager(t *testing.T) {
  123. var err error
  124. testStore = utilstore.NewMemStore()
  125. manager := newTestCheckpointManager()
  126. port80 := int32(80)
  127. port443 := int32(443)
  128. proto := protocol("tcp")
  129. ip1234 := "1.2.3.4"
  130. portMappings := []*PortMapping{
  131. {
  132. &proto,
  133. &port80,
  134. &port80,
  135. ip1234,
  136. },
  137. {
  138. &proto,
  139. &port443,
  140. &port443,
  141. ip1234,
  142. },
  143. }
  144. checkpoint1 := newFakeCheckpointV1("check1", portMappings, true)
  145. checkpoints := []struct {
  146. checkpointKey string
  147. checkpoint FakeCheckpoint
  148. expectHostNetwork bool
  149. }{
  150. {
  151. "key1",
  152. checkpoint1,
  153. true,
  154. },
  155. {
  156. "key2",
  157. newFakeCheckpointV1("check2", nil, false),
  158. false,
  159. },
  160. }
  161. for _, tc := range checkpoints {
  162. // Test CreateCheckpoints
  163. err = manager.CreateCheckpoint(tc.checkpointKey, tc.checkpoint)
  164. assert.NoError(t, err)
  165. // Test GetCheckpoints
  166. checkpointOut := newFakeCheckpointV1("", nil, false)
  167. err := manager.GetCheckpoint(tc.checkpointKey, checkpointOut)
  168. assert.NoError(t, err)
  169. actualPortMappings, actualHostNetwork := checkpointOut.GetData()
  170. expPortMappings, expHostNetwork := tc.checkpoint.GetData()
  171. assert.Equal(t, actualPortMappings, expPortMappings)
  172. assert.Equal(t, actualHostNetwork, expHostNetwork)
  173. }
  174. // Test it fails if tried to read V1 structure into V2, a different structure from the structure which is checkpointed
  175. checkpointV2 := newFakeCheckpointV2("", nil, false)
  176. err = manager.GetCheckpoint("key1", checkpointV2)
  177. assert.EqualError(t, err, "checkpoint is corrupted")
  178. // Test it fails if tried to read V1 structure into the same structure but defined in another package
  179. checkpointRemoteV1 := newFakeCheckpointRemoteV1("", nil, false)
  180. err = manager.GetCheckpoint("key1", checkpointRemoteV1)
  181. assert.EqualError(t, err, "checkpoint is corrupted")
  182. // Test it works if tried to read V1 structure using into a new V1 structure
  183. checkpointV1 := newFakeCheckpointV1("", nil, false)
  184. err = manager.GetCheckpoint("key1", checkpointV1)
  185. assert.NoError(t, err)
  186. // Test corrupt checksum case
  187. checkpointOut := newFakeCheckpointV1("", nil, false)
  188. blob, err := checkpointOut.MarshalCheckpoint()
  189. assert.NoError(t, err)
  190. testStore.Write("key1", blob)
  191. err = manager.GetCheckpoint("key1", checkpoint1)
  192. assert.EqualError(t, err, "checkpoint is corrupted")
  193. // Test ListCheckpoints
  194. keys, err := manager.ListCheckpoints()
  195. assert.NoError(t, err)
  196. sort.Strings(keys)
  197. assert.Equal(t, keys, []string{"key1", "key2"})
  198. // Test RemoveCheckpoints
  199. err = manager.RemoveCheckpoint("key1")
  200. assert.NoError(t, err)
  201. // Test Remove Nonexisted Checkpoints
  202. err = manager.RemoveCheckpoint("key1")
  203. assert.NoError(t, err)
  204. // Test ListCheckpoints
  205. keys, err = manager.ListCheckpoints()
  206. assert.NoError(t, err)
  207. assert.Equal(t, keys, []string{"key2"})
  208. // Test Get NonExisted Checkpoint
  209. checkpointNE := newFakeCheckpointV1("NE", nil, false)
  210. err = manager.GetCheckpoint("key1", checkpointNE)
  211. assert.Error(t, err)
  212. }