state_compatibility_test.go 2.6 KB

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