k8s.io/registry.k8s.io@v0.3.1/pkg/net/cloudcidrs/internal/ranges2go/parse_gcp_test.go (about)

     1  /*
     2  Copyright 2023 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 TestGCPParseIPRangesJSON(t *testing.T) {
    25  	// parse a snapshot of a valid subsest of data
    26  	const testData = `{
    27    "syncToken": "1675807451971",
    28    "creationTime": "2023-02-07T14:04:11.9716",
    29    "prefixes": [{
    30      "ipv4Prefix": "34.80.0.0/15",
    31      "service": "Google Cloud",
    32      "scope": "asia-east1"
    33    }, {
    34      "ipv6Prefix": "2600:1900:4180::/44",
    35      "service": "Google Cloud",
    36      "scope": "us-west4"
    37    }]
    38  }
    39  `
    40  	expectedParsed := &GCPCloudJSON{
    41  		Prefixes: []GCPPrefix{
    42  			{
    43  				IPv4Prefix: "34.80.0.0/15",
    44  				Scope:      "asia-east1",
    45  			},
    46  			{
    47  				IPv6Prefix: "2600:1900:4180::/44",
    48  				Scope:      "us-west4",
    49  			},
    50  		},
    51  	}
    52  	parsed, err := parseGCPCloudJSON([]byte(testData))
    53  	if err != nil {
    54  		t.Fatalf("unexpected error parsing testdata: %v", err)
    55  	}
    56  	if !reflect.DeepEqual(expectedParsed, parsed) {
    57  		t.Error("parsed did not match expected:")
    58  		t.Errorf("%#v", expectedParsed)
    59  		t.Error("parsed: ")
    60  		t.Errorf("%#v", parsed)
    61  		t.Fail()
    62  	}
    63  
    64  	// parse some bogus data
    65  	_, err = parseGCPCloudJSON([]byte(`{"prefixes": false}`))
    66  	if err == nil {
    67  		t.Fatal("expected error parsing garbage data but got none")
    68  	}
    69  }
    70  
    71  func TestGCPRegionsToPrefixesFromData(t *testing.T) {
    72  	t.Run("bad IPv4 prefixes", func(t *testing.T) {
    73  		t.Parallel()
    74  		badV4Prefixes := &GCPCloudJSON{
    75  			Prefixes: []GCPPrefix{
    76  				{
    77  					IPv4Prefix: "asdf;asdf,",
    78  					Scope:      "us-east-1",
    79  				},
    80  			},
    81  		}
    82  		_, err := gcpRegionsToPrefixesFromData(badV4Prefixes)
    83  		if err == nil {
    84  			t.Fatal("expected error parsing bogus prefix but got none")
    85  		}
    86  	})
    87  	t.Run("bad IPv6 prefixes", func(t *testing.T) {
    88  		t.Parallel()
    89  		badV6Prefixes := &GCPCloudJSON{
    90  			Prefixes: []GCPPrefix{
    91  				{
    92  					IPv4Prefix: "127.0.0.1/32",
    93  					Scope:      "us-east-1",
    94  				},
    95  				{
    96  					IPv6Prefix: "asdfasdf----....",
    97  					Scope:      "us-east-1",
    98  				},
    99  			},
   100  		}
   101  		_, err := gcpRegionsToPrefixesFromData(badV6Prefixes)
   102  		if err == nil {
   103  			t.Fatal("expected error parsing bogus prefix but got none")
   104  		}
   105  	})
   106  	t.Run("bad no prefixes", func(t *testing.T) {
   107  		t.Parallel()
   108  		badNoPrefixes := &GCPCloudJSON{
   109  			Prefixes: []GCPPrefix{
   110  				{
   111  					Scope: "us-east-1",
   112  				},
   113  			},
   114  		}
   115  		_, err := gcpRegionsToPrefixesFromData(badNoPrefixes)
   116  		if err == nil {
   117  			t.Fatal("expected error parsing bogus prefix but got none")
   118  		}
   119  	})
   120  }
   121  
   122  func TestParseGCP(t *testing.T) {
   123  	t.Run("unparsable data", func(t *testing.T) {
   124  		t.Parallel()
   125  		badJSON := `{"prefixes":false}`
   126  		_, err := parseGCP(badJSON)
   127  		if err == nil {
   128  			t.Fatal("expected error parsing bogus raw JSON but got none")
   129  		}
   130  	})
   131  }