github.phpd.cn/cilium/cilium@v1.6.12/pkg/k8s/utils/utils_test.go (about)

     1  // Copyright 2018 Authors of Cilium
     2  //
     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  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // +build !privileged_tests
    16  
    17  package utils
    18  
    19  import (
    20  	"testing"
    21  
    22  	. "gopkg.in/check.v1"
    23  )
    24  
    25  // Hook up gocheck into the "go test" runner.
    26  func Test(t *testing.T) {
    27  	TestingT(t)
    28  }
    29  
    30  type FactorySuite struct{}
    31  
    32  var _ = Suite(&FactorySuite{})
    33  
    34  func (s *FactorySuite) TestIsInfraContainer(c *C) {
    35  	type args struct {
    36  		labels map[string]string
    37  	}
    38  	type want struct {
    39  		IsInfraContainer bool
    40  	}
    41  	tests := []struct {
    42  		name      string
    43  		setupArgs func() args
    44  		setupWant func() want
    45  	}{
    46  		{
    47  			name: "non infra container",
    48  			setupArgs: func() args {
    49  				return args{
    50  					labels: map[string]string{
    51  						"annotation.io.kubernetes.container.terminationMessagePath":   "/dev/termination-log",
    52  						"io.kubernetes.docker.type":                                   "container",
    53  						"io.kubernetes.pod.name":                                      "guestbook-xt4gk",
    54  						"io.kubernetes.pod.uid":                                       "e01003fb-e379-11e8-b8e7-0800271bbcb9",
    55  						"io.kubernetes.sandbox.id":                                    "03aa8c8d48e423fffcdd7c3e9a3d319197c064520598142c17638508dd4c83df",
    56  						"annotation.io.kubernetes.container.hash":                     "84a5e346",
    57  						"annotation.io.kubernetes.container.restartCount":             "69",
    58  						"annotation.io.kubernetes.container.terminationMessagePolicy": "File",
    59  						"annotation.io.kubernetes.pod.terminationGracePeriod":         "30",
    60  						"io.kubernetes.container.logpath":                             "/var/log/pods/e01003fb-e379-11e8-b8e7-0800271bbcb9/guestbook/69.log",
    61  						"io.kubernetes.container.name":                                "guestbook",
    62  						"io.kubernetes.pod.namespace":                                 "kube-system",
    63  					},
    64  				}
    65  			},
    66  			setupWant: func() want {
    67  				return want{
    68  					false,
    69  				}
    70  			},
    71  		},
    72  		{
    73  			name: "infra container",
    74  			setupArgs: func() args {
    75  				return args{
    76  					labels: map[string]string{
    77  						"annotation.kubernetes.io/config.seen":   "2018-11-08T17:15:22.477743596Z",
    78  						"io.kubernetes.docker.type":              "podsandbox",
    79  						"pod-template-generation":                "1",
    80  						"annotation.kubernetes.io/config.source": "api",
    81  						"app":                                    "guestbook",
    82  						"controller-revision-hash":               "677b87bff7",
    83  						"io.kubernetes.container.name":           "POD",
    84  						"io.kubernetes.pod.name":                 "guestbook-xt4gk",
    85  						"io.kubernetes.pod.namespace":            "kube-system",
    86  						"io.kubernetes.pod.uid":                  "e01003fb-e379-11e8-b8e7-0800271bbcb9",
    87  					},
    88  				}
    89  			},
    90  			setupWant: func() want {
    91  				return want{
    92  					true,
    93  				}
    94  			},
    95  		},
    96  	}
    97  	for _, tt := range tests {
    98  		args := tt.setupArgs()
    99  		want := tt.setupWant()
   100  		got := IsInfraContainer(args.labels)
   101  		c.Assert(got, Equals, want.IsInfraContainer, Commentf("Test Name: %s", tt.name))
   102  	}
   103  }