github.com/fafucoder/cilium@v1.6.11/pkg/endpoint/id/id_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 id
    18  
    19  import (
    20  	"net"
    21  	"strings"
    22  	"testing"
    23  
    24  	. "gopkg.in/check.v1"
    25  )
    26  
    27  // Hook up gocheck into the "go test" runner.
    28  func Test(t *testing.T) { TestingT(t) }
    29  
    30  type IDSuite struct{}
    31  
    32  var _ = Suite(&IDSuite{})
    33  
    34  func (s *IDSuite) TestSplitID(c *C) {
    35  	type args struct {
    36  		id string
    37  	}
    38  	type want struct {
    39  		prefixType      PrefixType
    40  		prefixTypeCheck Checker
    41  		id              string
    42  		idCheck         Checker
    43  	}
    44  	tests := []struct {
    45  		name        string
    46  		setupArgs   func() args
    47  		setupWant   func() want
    48  		preTestRun  func()
    49  		postTestRun func()
    50  	}{
    51  		{
    52  			name: "ID without a prefix",
    53  			preTestRun: func() {
    54  			},
    55  			setupArgs: func() args {
    56  				return args{
    57  					"123456",
    58  				}
    59  			},
    60  			setupWant: func() want {
    61  				return want{
    62  					prefixType:      CiliumLocalIdPrefix,
    63  					prefixTypeCheck: Equals,
    64  					id:              "123456",
    65  					idCheck:         Equals,
    66  				}
    67  			},
    68  			postTestRun: func() {
    69  			},
    70  		},
    71  		{
    72  			name: "ID CiliumLocalIdPrefix prefix",
    73  			preTestRun: func() {
    74  			},
    75  			setupArgs: func() args {
    76  				return args{
    77  					string(CiliumLocalIdPrefix) + ":123456",
    78  				}
    79  			},
    80  			setupWant: func() want {
    81  				return want{
    82  					prefixType:      CiliumLocalIdPrefix,
    83  					prefixTypeCheck: Equals,
    84  					id:              "123456",
    85  					idCheck:         Equals,
    86  				}
    87  			},
    88  			postTestRun: func() {
    89  			},
    90  		},
    91  		{
    92  			name: "ID with PodNamePrefix prefix",
    93  			preTestRun: func() {
    94  			},
    95  			setupArgs: func() args {
    96  				return args{
    97  					string(PodNamePrefix) + ":default:foobar",
    98  				}
    99  			},
   100  			setupWant: func() want {
   101  				return want{
   102  					prefixType:      PodNamePrefix,
   103  					prefixTypeCheck: Equals,
   104  					id:              "default:foobar",
   105  					idCheck:         Equals,
   106  				}
   107  			},
   108  			postTestRun: func() {
   109  			},
   110  		},
   111  		{
   112  			name: "ID with ':'",
   113  			preTestRun: func() {
   114  			},
   115  			setupArgs: func() args {
   116  				return args{
   117  					":",
   118  				}
   119  			},
   120  			setupWant: func() want {
   121  				return want{
   122  					prefixType:      "",
   123  					prefixTypeCheck: Equals,
   124  					id:              "",
   125  					idCheck:         Equals,
   126  				}
   127  			},
   128  			postTestRun: func() {
   129  			},
   130  		},
   131  		{
   132  			name: "Empty ID",
   133  			preTestRun: func() {
   134  			},
   135  			setupArgs: func() args {
   136  				return args{
   137  					"",
   138  				}
   139  			},
   140  			setupWant: func() want {
   141  				return want{
   142  					prefixType:      CiliumLocalIdPrefix,
   143  					prefixTypeCheck: Equals,
   144  					id:              "",
   145  					idCheck:         Equals,
   146  				}
   147  			},
   148  			postTestRun: func() {
   149  			},
   150  		},
   151  	}
   152  	for _, tt := range tests {
   153  		tt.preTestRun()
   154  		args := tt.setupArgs()
   155  		want := tt.setupWant()
   156  		prefixType, id := splitID(args.id)
   157  		c.Assert(prefixType, want.prefixTypeCheck, want.prefixType, Commentf("Test Name: %s", tt.name))
   158  		c.Assert(id, want.idCheck, want.id, Commentf("Test Name: %s", tt.name))
   159  		tt.postTestRun()
   160  	}
   161  }
   162  
   163  func BenchmarkSplitID(b *testing.B) {
   164  	tests := []struct {
   165  		str        string
   166  		prefixType PrefixType
   167  		id         string
   168  	}{
   169  		{"123456", CiliumLocalIdPrefix, "123456"},
   170  		{string(CiliumLocalIdPrefix + ":123456"), CiliumLocalIdPrefix, "123456"},
   171  		{string(PodNamePrefix + ":default:foobar"), PodNamePrefix, "default:foobar"},
   172  	}
   173  	count := 0
   174  	b.ResetTimer()
   175  	for i := 0; i < b.N; i++ {
   176  		for _, test := range tests {
   177  			pt, str := splitID(test.str)
   178  			if pt == test.prefixType && str == test.id {
   179  				count++
   180  			}
   181  		}
   182  	}
   183  	b.StopTimer()
   184  	if count != len(tests)*b.N {
   185  		b.Errorf("splitID didn't produce correct results")
   186  	}
   187  	b.ReportAllocs()
   188  }
   189  
   190  func (s *IDSuite) TestParse(c *C) {
   191  	type test struct {
   192  		input      PrefixType
   193  		wantPrefix PrefixType
   194  		wantID     string
   195  		expectFail bool
   196  	}
   197  
   198  	tests := []test{
   199  		{DockerEndpointPrefix + ":foo", DockerEndpointPrefix, "foo", false},
   200  		{DockerEndpointPrefix + ":foo:foo", DockerEndpointPrefix, "foo:foo", false},
   201  		{"unknown:unknown", "", "", true},
   202  		{"unknown", CiliumLocalIdPrefix, "unknown", false},
   203  	}
   204  
   205  	for _, t := range tests {
   206  		prefix, id, err := Parse(string(t.input))
   207  		c.Assert(prefix, Equals, t.wantPrefix)
   208  		c.Assert(id, Equals, t.wantID)
   209  		if t.expectFail {
   210  			c.Assert(err, Not(IsNil))
   211  		} else {
   212  			c.Assert(err, IsNil)
   213  		}
   214  	}
   215  }
   216  
   217  func (s *IDSuite) TestNewIPPrefix(c *C) {
   218  	c.Assert(strings.HasPrefix(NewIPPrefixID(net.ParseIP("1.1.1.1")), string(IPv4Prefix)), Equals, true)
   219  	c.Assert(strings.HasPrefix(NewIPPrefixID(net.ParseIP("f00d::1")), string(IPv6Prefix)), Equals, true)
   220  }