...

Source file src/k8s.io/kubernetes/pkg/kubelet/reason_cache_test.go

Documentation: k8s.io/kubernetes/pkg/kubelet

     1  /*
     2  Copyright 2016 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package kubelet
    18  
    19  import (
    20  	"testing"
    21  
    22  	"k8s.io/apimachinery/pkg/types"
    23  	kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
    24  )
    25  
    26  func TestReasonCache(t *testing.T) {
    27  	// Create test sync result
    28  	syncResult := kubecontainer.PodSyncResult{}
    29  	results := []*kubecontainer.SyncResult{
    30  		// reason cache should be set for SyncResult with StartContainer action and error
    31  		kubecontainer.NewSyncResult(kubecontainer.StartContainer, "container_1"),
    32  		// reason cache should not be set for SyncResult with StartContainer action but without error
    33  		kubecontainer.NewSyncResult(kubecontainer.StartContainer, "container_2"),
    34  		// reason cache should not be set for SyncResult with other actions
    35  		kubecontainer.NewSyncResult(kubecontainer.KillContainer, "container_3"),
    36  	}
    37  	results[0].Fail(kubecontainer.ErrRunContainer, "message_1")
    38  	results[2].Fail(kubecontainer.ErrKillContainer, "message_3")
    39  	syncResult.AddSyncResult(results...)
    40  	uid := types.UID("pod_1")
    41  
    42  	reasonCache := NewReasonCache()
    43  	reasonCache.Update(uid, syncResult)
    44  	assertReasonInfo(t, reasonCache, uid, results[0], true)
    45  	assertReasonInfo(t, reasonCache, uid, results[1], false)
    46  	assertReasonInfo(t, reasonCache, uid, results[2], false)
    47  
    48  	reasonCache.Remove(uid, results[0].Target.(string))
    49  	assertReasonInfo(t, reasonCache, uid, results[0], false)
    50  }
    51  
    52  func assertReasonInfo(t *testing.T, cache *ReasonCache, uid types.UID, result *kubecontainer.SyncResult, found bool) {
    53  	name := result.Target.(string)
    54  	actualReason, ok := cache.Get(uid, name)
    55  	if ok && !found {
    56  		t.Fatalf("unexpected cache hit: %v, %q", actualReason.Err, actualReason.Message)
    57  	}
    58  	if !ok && found {
    59  		t.Fatalf("corresponding reason info not found")
    60  	}
    61  	if !found {
    62  		return
    63  	}
    64  	reason := result.Error
    65  	message := result.Message
    66  	if actualReason.Err != reason || actualReason.Message != message {
    67  		t.Errorf("expected %v %q, got %v %q", reason, message, actualReason.Err, actualReason.Message)
    68  	}
    69  }
    70  

View as plain text