k8s.io/registry.k8s.io@v0.3.1/pkg/net/cloudcidrs/internal/ranges2go/gen_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 main
    18  
    19  import (
    20  	"bytes"
    21  	"testing"
    22  )
    23  
    24  func TestGenerateRangesGo(t *testing.T) {
    25  	// raw data to generate from
    26  	const rawAWSData = `{
    27    "syncToken": "1649878400",
    28    "createDate": "2022-04-13-19-33-20",
    29    "prefixes": [
    30      {
    31        "ip_prefix": "3.5.140.0/22",
    32        "region": "ap-northeast-2",
    33        "service": "AMAZON",
    34        "network_border_group": "ap-northeast-2"
    35      },
    36      {
    37        "ip_prefix": "52.95.174.0/24",
    38        "region": "me-south-1",
    39        "service": "AMAZON",
    40        "network_border_group": "me-south-1"
    41      },
    42      {
    43        "ip_prefix": "15.185.0.0/16",
    44        "region": "me-south-1",
    45        "service": "AMAZON",
    46        "network_border_group": "me-south-1"
    47      },
    48      {
    49        "ip_prefix": "69.107.7.136/29",
    50        "region": "me-south-1",
    51        "service": "AMAZON",
    52        "network_border_group": "me-south-1"
    53      }
    54    ],
    55    "ipv6_prefixes": [
    56      {
    57        "ipv6_prefix": "2a05:d07a:a000::/40",
    58        "region": "eu-south-1",
    59        "service": "AMAZON",
    60        "network_border_group": "eu-south-1"
    61      },
    62      {
    63        "ipv6_prefix": "2a05:d03a:a000:200::/56",
    64        "region": "eu-south-1",
    65        "service": "AMAZON",
    66        "network_border_group": "eu-south-1"
    67      },
    68      {
    69        "ipv6_prefix": "2a05:d03a:a000:400::/56",
    70        "region": "eu-south-1",
    71        "service": "AMAZON",
    72        "network_border_group": "eu-south-1"
    73      },
    74      {
    75        "ipv6_prefix": "2a05:d03a:a000::/56",
    76        "region": "eu-south-1",
    77        "service": "AMAZON",
    78        "network_border_group": "eu-south-1"
    79      }
    80    ]
    81  }
    82  `
    83  	awsRTP, err := parseAWS(rawAWSData)
    84  	if err != nil {
    85  		t.Fatalf("unexpected error parsing test data: %v", err)
    86  	}
    87  	const rawGCPData = `{
    88    "syncToken": "1678334702365",
    89    "creationTime": "2023-03-08T20:05:02.365608",
    90    "prefixes": [{
    91      "ipv4Prefix": "34.80.0.0/15",
    92      "service": "Google Cloud",
    93      "scope": "asia-east1"
    94    }, {
    95      "ipv4Prefix": "34.137.0.0/16",
    96      "service": "Google Cloud",
    97      "scope": "asia-east1"
    98    }, {
    99      "ipv4Prefix": "35.185.128.0/19",
   100      "service": "Google Cloud",
   101      "scope": "asia-east1"
   102    }, {
   103      "ipv4Prefix": "130.211.240.0/20",
   104      "service": "Google Cloud",
   105      "scope": "asia-east1"
   106    }, {
   107      "ipv6Prefix": "2600:1900:4030::/44",
   108      "service": "Google Cloud",
   109      "scope": "asia-east1"
   110    }, {
   111      "ipv6Prefix": "2600:1900:4180::/44",
   112      "service": "Google Cloud",
   113      "scope": "us-west4"
   114    }]
   115  }
   116  `
   117  	gcpRTP, err := parseGCP(rawGCPData)
   118  	if err != nil {
   119  		t.Fatalf("unexpected error parsing test data: %v", err)
   120  	}
   121  
   122  	// expected generated result
   123  	const goldenText = `/*
   124  Copyright The Kubernetes Authors.
   125  
   126  Licensed under the Apache License, Version 2.0 (the "License");
   127  you may not use this file except in compliance with the License.
   128  You may obtain a copy of the License at
   129  
   130      http://www.apache.org/licenses/LICENSE-2.0
   131  
   132  Unless required by applicable law or agreed to in writing, software
   133  distributed under the License is distributed on an "AS IS" BASIS,
   134  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   135  See the License for the specific language governing permissions and
   136  limitations under the License.
   137  */
   138  
   139  // File generated by ranges2go DO NOT EDIT
   140  
   141  package cloudcidrs
   142  
   143  import (
   144  	"net/netip"
   145  )
   146  
   147  // AWS cloud
   148  const AWS = "AWS"
   149  // GCP cloud
   150  const GCP = "GCP"
   151  
   152  // regionToRanges contains a preparsed map of cloud IPInfo to netip.Prefix
   153  var regionToRanges = map[IPInfo][]netip.Prefix{
   154  	{Cloud: AWS, Region: "ap-northeast-2"}: {
   155  		netip.PrefixFrom(netip.AddrFrom4([4]byte{3, 5, 140, 0}), 22),
   156  	},
   157  	{Cloud: AWS, Region: "eu-south-1"}: {
   158  		netip.PrefixFrom(netip.AddrFrom16([16]byte{42, 5, 208, 58, 160, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0}), 56),
   159  		netip.PrefixFrom(netip.AddrFrom16([16]byte{42, 5, 208, 58, 160, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}), 56),
   160  		netip.PrefixFrom(netip.AddrFrom16([16]byte{42, 5, 208, 122, 160, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}), 40),
   161  	},
   162  	{Cloud: AWS, Region: "me-south-1"}: {
   163  		netip.PrefixFrom(netip.AddrFrom4([4]byte{52, 95, 174, 0}), 24),
   164  		netip.PrefixFrom(netip.AddrFrom4([4]byte{69, 107, 7, 136}), 29),
   165  	},
   166  	{Cloud: GCP, Region: "asia-east1"}: {
   167  		netip.PrefixFrom(netip.AddrFrom16([16]byte{38, 0, 25, 0, 64, 48, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}), 44),
   168  		netip.PrefixFrom(netip.AddrFrom4([4]byte{34, 137, 0, 0}), 16),
   169  		netip.PrefixFrom(netip.AddrFrom4([4]byte{34, 80, 0, 0}), 15),
   170  		netip.PrefixFrom(netip.AddrFrom4([4]byte{35, 185, 128, 0}), 19),
   171  	},
   172  	{Cloud: GCP, Region: "us-west4"}: {
   173  		netip.PrefixFrom(netip.AddrFrom16([16]byte{38, 0, 25, 0, 65, 128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}), 44),
   174  	},
   175  }
   176  `
   177  
   178  	cloudToRTP := map[string]regionsToPrefixes{
   179  		"AWS": awsRTP,
   180  		"GCP": gcpRTP,
   181  	}
   182  	// generate and compare
   183  	w := &bytes.Buffer{}
   184  	if err := generateRangesGo(w, cloudToRTP); err != nil {
   185  		t.Fatalf("unexpected error generating: %v", err)
   186  	}
   187  	result := w.String()
   188  	if result != goldenText {
   189  		t.Error("result does not equal expected golden text")
   190  		t.Error("golden text:")
   191  		t.Error(goldenText)
   192  		t.Error("result:")
   193  		t.Error(result)
   194  		t.Fail()
   195  	}
   196  }