reason_cache_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 kubelet
  14. import (
  15. "testing"
  16. kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
  17. "k8s.io/kubernetes/pkg/types"
  18. )
  19. func TestReasonCache(t *testing.T) {
  20. // Create test sync result
  21. syncResult := kubecontainer.PodSyncResult{}
  22. results := []*kubecontainer.SyncResult{
  23. // reason cache should be set for SyncResult with StartContainer action and error
  24. kubecontainer.NewSyncResult(kubecontainer.StartContainer, "container_1"),
  25. // reason cache should not be set for SyncResult with StartContainer action but without error
  26. kubecontainer.NewSyncResult(kubecontainer.StartContainer, "container_2"),
  27. // reason cache should not be set for SyncResult with other actions
  28. kubecontainer.NewSyncResult(kubecontainer.KillContainer, "container_3"),
  29. }
  30. results[0].Fail(kubecontainer.ErrRunContainer, "message_1")
  31. results[2].Fail(kubecontainer.ErrKillContainer, "message_3")
  32. syncResult.AddSyncResult(results...)
  33. uid := types.UID("pod_1")
  34. reasonCache := NewReasonCache()
  35. reasonCache.Update(uid, syncResult)
  36. assertReasonInfo(t, reasonCache, uid, results[0], true)
  37. assertReasonInfo(t, reasonCache, uid, results[1], false)
  38. assertReasonInfo(t, reasonCache, uid, results[2], false)
  39. reasonCache.Remove(uid, results[0].Target.(string))
  40. assertReasonInfo(t, reasonCache, uid, results[0], false)
  41. }
  42. func assertReasonInfo(t *testing.T, cache *ReasonCache, uid types.UID, result *kubecontainer.SyncResult, found bool) {
  43. name := result.Target.(string)
  44. actualReason, actualMessage, ok := cache.Get(uid, name)
  45. if ok && !found {
  46. t.Fatalf("unexpected cache hit: %v, %q", actualReason, actualMessage)
  47. }
  48. if !ok && found {
  49. t.Fatalf("corresponding reason info not found")
  50. }
  51. if !found {
  52. return
  53. }
  54. reason := result.Error
  55. message := result.Message
  56. if actualReason != reason || actualMessage != message {
  57. t.Errorf("expected %v %q, got %v %q", reason, message, actualReason, actualMessage)
  58. }
  59. }