k8s.io/registry.k8s.io@v0.3.1/pkg/net/cloudcidrs/internal/ranges2go/parse_aws_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  	"reflect"
    21  	"testing"
    22  )
    23  
    24  func TestAWSParseIPRangesJSON(t *testing.T) {
    25  	// parse a snapshot of a valid subsest of data
    26  	const testData = `{
    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    "ipv6_prefixes": [
    38      {
    39        "ipv6_prefix": "2a05:d07a:a000::/40",
    40        "region": "eu-south-1",
    41        "service": "AMAZON",
    42        "network_border_group": "eu-south-1"
    43      }
    44    ]
    45  }`
    46  	expectedParsed := &AWSIPRangesJSON{
    47  		Prefixes: []AWSPrefix{
    48  			{
    49  				IPPrefix: "3.5.140.0/22",
    50  				Region:   "ap-northeast-2",
    51  				Service:  "AMAZON",
    52  			},
    53  		},
    54  		IPv6Prefixes: []AWSIPv6Prefix{
    55  			{
    56  				IPv6Prefix: "2a05:d07a:a000::/40",
    57  				Region:     "eu-south-1",
    58  				Service:    "AMAZON",
    59  			},
    60  		},
    61  	}
    62  	parsed, err := parseAWSIPRangesJSON([]byte(testData))
    63  	if err != nil {
    64  		t.Fatalf("unexpected error parsing testdata: %v", err)
    65  	}
    66  	if !reflect.DeepEqual(expectedParsed, parsed) {
    67  		t.Error("parsed did not match expected:")
    68  		t.Errorf("%#v", expectedParsed)
    69  		t.Error("parsed: ")
    70  		t.Errorf("%#v", parsed)
    71  		t.Fail()
    72  	}
    73  
    74  	// parse some bogus data
    75  	_, err = parseAWSIPRangesJSON([]byte(`{"prefixes": false}`))
    76  	if err == nil {
    77  		t.Fatal("expected error parsing garbage data but got none")
    78  	}
    79  }
    80  
    81  func TestAWSRegionsToPrefixesFromData(t *testing.T) {
    82  	t.Run("bad IPv4 prefixes", func(t *testing.T) {
    83  		t.Parallel()
    84  		badV4Prefixes := &AWSIPRangesJSON{
    85  			Prefixes: []AWSPrefix{
    86  				{
    87  					IPPrefix: "asdf;asdf,",
    88  					Service:  "AMAZON",
    89  					Region:   "us-east-1",
    90  				},
    91  			},
    92  		}
    93  		_, err := awsRegionsToPrefixesFromData(badV4Prefixes)
    94  		if err == nil {
    95  			t.Fatal("expected error parsing bogus prefix but got none")
    96  		}
    97  	})
    98  	t.Run("bad IPv6 prefixes", func(t *testing.T) {
    99  		t.Parallel()
   100  		badV6Prefixes := &AWSIPRangesJSON{
   101  			Prefixes: []AWSPrefix{
   102  				{
   103  					IPPrefix: "127.0.0.1/32",
   104  					Service:  "AMAZON",
   105  					Region:   "us-east-1",
   106  				},
   107  			},
   108  			IPv6Prefixes: []AWSIPv6Prefix{
   109  				{
   110  					IPv6Prefix: "asdfasdf----....",
   111  					Service:    "AMAZON",
   112  					Region:     "us-east-1",
   113  				},
   114  			},
   115  		}
   116  		_, err := awsRegionsToPrefixesFromData(badV6Prefixes)
   117  		if err == nil {
   118  			t.Fatal("expected error parsing bogus prefix but got none")
   119  		}
   120  	})
   121  }
   122  
   123  func TestParseAWS(t *testing.T) {
   124  	t.Run("unparsable data", func(t *testing.T) {
   125  		t.Parallel()
   126  		badJSON := `{"prefixes":false}`
   127  		_, err := parseAWS(badJSON)
   128  		if err == nil {
   129  			t.Fatal("expected error parsing bogus raw JSON but got none")
   130  		}
   131  	})
   132  }