...

Source file src/k8s.io/kubernetes/pkg/kubelet/container/container_hash_test.go

Documentation: k8s.io/kubernetes/pkg/kubelet/container

     1  /*
     2  Copyright 2019 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 container
    18  
    19  import (
    20  	"encoding/json"
    21  	"testing"
    22  
    23  	"k8s.io/api/core/v1"
    24  )
    25  
    26  var (
    27  	sampleContainer = `
    28  {
    29    "name": "test_container",
    30    "image": "foo/image:v1",
    31    "command": [
    32      "/bin/testcmd"
    33    ],
    34    "args": [
    35      "/bin/sh",
    36      "-c",
    37      "echo abc"
    38    ],
    39    "ports": [
    40      {
    41        "containerPort": 8001
    42      }
    43    ],
    44    "env": [
    45      {
    46        "name": "ENV_FOO",
    47        "value": "bar"
    48      },
    49      {
    50        "name": "ENV_BAR",
    51        "valueFrom": {
    52          "secretKeyRef": {
    53            "name": "foo",
    54            "key": "bar",
    55            "optional": true
    56          }
    57        }
    58      }
    59    ],
    60    "resources": {
    61      "limits": {
    62        "foo": "1G"
    63      },
    64      "requests": {
    65        "foo": "500M"
    66      }
    67    }
    68  }
    69  `
    70  
    71  	sampleV115HashValue = uint64(0x311670a)
    72  	sampleV116HashValue = sampleV115HashValue
    73  )
    74  
    75  func TestConsistentHashContainer(t *testing.T) {
    76  	container := &v1.Container{}
    77  	if err := json.Unmarshal([]byte(sampleContainer), container); err != nil {
    78  		t.Error(err)
    79  	}
    80  
    81  	currentHash := HashContainer(container)
    82  	if currentHash != sampleV116HashValue {
    83  		t.Errorf("mismatched hash value with v1.16")
    84  	}
    85  
    86  	if currentHash != sampleV115HashValue {
    87  		t.Errorf("mismatched hash value with v1.15")
    88  	}
    89  }
    90  

View as plain text