k8s.io/registry.k8s.io@v0.3.1/pkg/net/cloudcidrs/ipmapper_test.go (about)

     1  /*
     2  Copyright 2022 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 cloudcidrs
    18  
    19  import (
    20  	"net/netip"
    21  	"testing"
    22  
    23  	"k8s.io/registry.k8s.io/pkg/net/cidrs"
    24  )
    25  
    26  type testCase struct {
    27  	Addr           netip.Addr
    28  	ExpectedRegion string
    29  }
    30  
    31  // some known IPs and their regions
    32  var testCasesIPv4 = []testCase{
    33  	{Addr: netip.MustParseAddr("35.180.1.1"), ExpectedRegion: "eu-west-3"},
    34  	{Addr: netip.MustParseAddr("35.250.1.1"), ExpectedRegion: ""},
    35  	{Addr: netip.MustParseAddr("35.0.1.1"), ExpectedRegion: ""},
    36  	{Addr: netip.MustParseAddr("52.94.76.1"), ExpectedRegion: "us-west-2"},
    37  	{Addr: netip.MustParseAddr("52.94.77.1"), ExpectedRegion: "us-west-2"},
    38  	{Addr: netip.MustParseAddr("52.93.127.172"), ExpectedRegion: "us-east-1"},
    39  }
    40  var testCasesIPv6 = []testCase{
    41  	// ipv6
    42  	{Addr: netip.MustParseAddr("2400:6500:0:9::2"), ExpectedRegion: "ap-southeast-3"},
    43  	{Addr: netip.MustParseAddr("2400:6500:0:9::1"), ExpectedRegion: "ap-southeast-3"},
    44  	{Addr: netip.MustParseAddr("2400:6500:0:9::3"), ExpectedRegion: "ap-southeast-3"},
    45  	{Addr: netip.MustParseAddr("2600:1f01:4874::47"), ExpectedRegion: "us-west-2"},
    46  	{Addr: netip.MustParseAddr("2400:6500:0:9::100"), ExpectedRegion: ""},
    47  }
    48  
    49  // NOTE: we need to append to an empty slice so we do not modify the existing slices
    50  // append may re-use the first existing slice ...
    51  var allTestCases = append(append([]testCase{}, testCasesIPv4...), testCasesIPv6...)
    52  
    53  func TestNewIPMapper(t *testing.T) {
    54  	mapper := NewIPMapper()
    55  	for i := range allTestCases {
    56  		tc := allTestCases[i]
    57  		t.Run(tc.Addr.String(), func(t *testing.T) {
    58  			r, matched := mapper.GetIP(tc.Addr)
    59  			expectMatched := tc.ExpectedRegion != ""
    60  			if matched != expectMatched || r.Region != tc.ExpectedRegion {
    61  				t.Fatalf(
    62  					"result does not match for %v, got: (%q, %t) expected: (%q, %t)",
    63  					tc.Addr, r.Region, matched, tc.ExpectedRegion, expectMatched,
    64  				)
    65  			}
    66  		})
    67  	}
    68  }
    69  
    70  /*  for benchmarking memory / init time */
    71  
    72  func BenchmarkNewIPMapper(b *testing.B) {
    73  	for n := 0; n < b.N; n++ {
    74  		mapper := NewIPMapper()
    75  		// get any address just to prevent mapper being optimized out
    76  		mapper.GetIP(allTestCases[0].Addr)
    77  	}
    78  }
    79  
    80  func BenchmarkNewegionBruteForce(b *testing.B) {
    81  	for n := 0; n < b.N; n++ {
    82  		mapper := cidrs.NewBruteForceMapper(regionToRanges)
    83  		// get any address just to prevent mapper being optimized out
    84  		mapper.GetIP(allTestCases[0].Addr)
    85  	}
    86  }
    87  
    88  /* for benchmarking matching time */
    89  
    90  func BenchmarkRegionTrieMapIPv4(b *testing.B) {
    91  	mapper := NewIPMapper()
    92  	for n := 0; n < b.N; n++ {
    93  		tc := testCasesIPv4[n%len(testCasesIPv4)]
    94  		r, matched := mapper.GetIP(tc.Addr)
    95  		expectMatched := tc.ExpectedRegion != ""
    96  		if matched != expectMatched || r.Region != tc.ExpectedRegion {
    97  			b.Fatalf(
    98  				"result does not match for %v, got: (%q, %t) expected: (%q, %t)",
    99  				tc.Addr, r.Region, matched, tc.ExpectedRegion, expectMatched,
   100  			)
   101  		}
   102  	}
   103  }
   104  
   105  func BenchmarkRegionTrieMapIPv6(b *testing.B) {
   106  	mapper := NewIPMapper()
   107  	for n := 0; n < b.N; n++ {
   108  		tc := testCasesIPv6[n%len(testCasesIPv6)]
   109  		r, matched := mapper.GetIP(tc.Addr)
   110  		expectMatched := tc.ExpectedRegion != ""
   111  		if matched != expectMatched || r.Region != tc.ExpectedRegion {
   112  			b.Fatalf(
   113  				"result does not match for %v, got: (%q, %t) expected: (%q, %t)",
   114  				tc.Addr, r.Region, matched, tc.ExpectedRegion, expectMatched,
   115  			)
   116  		}
   117  	}
   118  }
   119  
   120  func BenchmarkRegionBruteForceIPv4(b *testing.B) {
   121  	mapper := cidrs.NewBruteForceMapper(regionToRanges)
   122  	for n := 0; n < b.N; n++ {
   123  		tc := testCasesIPv4[n%len(testCasesIPv4)]
   124  		r, matched := mapper.GetIP(tc.Addr)
   125  		expectMatched := tc.ExpectedRegion != ""
   126  		if matched != expectMatched || r.Region != tc.ExpectedRegion {
   127  			b.Fatalf(
   128  				"result does not match for %v, got: (%q, %t) expected: (%q, %t)",
   129  				tc.Addr, r.Region, matched, tc.ExpectedRegion, expectMatched,
   130  			)
   131  		}
   132  	}
   133  }
   134  
   135  func BenchmarkRegionBruteForceIPv6(b *testing.B) {
   136  	mapper := cidrs.NewBruteForceMapper(regionToRanges)
   137  	for n := 0; n < b.N; n++ {
   138  		tc := testCasesIPv6[n%len(testCasesIPv6)]
   139  		r, matched := mapper.GetIP(tc.Addr)
   140  		expectMatched := tc.ExpectedRegion != ""
   141  		if matched != expectMatched || r.Region != tc.ExpectedRegion {
   142  			b.Fatalf(
   143  				"result does not match for %v, got: (%q, %t) expected: (%q, %t)",
   144  				tc.Addr, r.Region, matched, tc.ExpectedRegion, expectMatched,
   145  			)
   146  		}
   147  	}
   148  }