state_compatibility_test.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. Copyright 2018 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 state
  14. import (
  15. "os"
  16. "path"
  17. "testing"
  18. "k8s.io/kubernetes/pkg/kubelet/checkpointmanager"
  19. "k8s.io/kubernetes/pkg/kubelet/cm/cpuset"
  20. )
  21. const compatibilityTestingCheckpoint = "cpumanager_state_compatibility_test"
  22. var state = &stateMemory{
  23. assignments: ContainerCPUAssignments{
  24. "container1": cpuset.NewCPUSet(4, 5, 6),
  25. "container2": cpuset.NewCPUSet(1, 2, 3),
  26. },
  27. defaultCPUSet: cpuset.NewCPUSet(1, 2, 3),
  28. }
  29. func TestFileToCheckpointCompatibility(t *testing.T) {
  30. statePath := path.Join(testingDir, compatibilityTestingCheckpoint)
  31. // ensure there is no previous state saved at testing path
  32. os.Remove(statePath)
  33. // ensure testing state is removed after testing
  34. defer os.Remove(statePath)
  35. fileState := NewFileState(statePath, "none")
  36. fileState.SetDefaultCPUSet(state.defaultCPUSet)
  37. fileState.SetCPUAssignments(state.assignments)
  38. restoredState, err := NewCheckpointState(testingDir, compatibilityTestingCheckpoint, "none")
  39. if err != nil {
  40. t.Fatalf("could not restore file state: %v", err)
  41. }
  42. AssertStateEqual(t, restoredState, state)
  43. }
  44. func TestCheckpointToFileCompatibility(t *testing.T) {
  45. cpm, err := checkpointmanager.NewCheckpointManager(testingDir)
  46. if err != nil {
  47. t.Fatalf("could not create testing checkpoint manager: %v", err)
  48. }
  49. // ensure there is no previous checkpoint
  50. cpm.RemoveCheckpoint(compatibilityTestingCheckpoint)
  51. // ensure testing checkpoint is removed after testing
  52. defer cpm.RemoveCheckpoint(compatibilityTestingCheckpoint)
  53. checkpointState, err := NewCheckpointState(testingDir, compatibilityTestingCheckpoint, "none")
  54. checkpointState.SetDefaultCPUSet(state.defaultCPUSet)
  55. checkpointState.SetCPUAssignments(state.assignments)
  56. restoredState := NewFileState(path.Join(testingDir, compatibilityTestingCheckpoint), "none")
  57. AssertStateEqual(t, restoredState, state)
  58. }