gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/go-control-plane/pkg/cache/v3/resource_test.go (about)

     1  // Copyright 2018 Envoyproxy Authors
     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  package cache_test
    16  
    17  import (
    18  	"gitee.com/ks-custle/core-gm/go-control-plane/pkg/cache/v3"
    19  	"gitee.com/ks-custle/core-gm/go-control-plane/pkg/test/resource/v3"
    20  	"reflect"
    21  	"testing"
    22  
    23  	cluster "gitee.com/ks-custle/core-gm/go-control-plane/envoy/config/cluster/v3"
    24  	route "gitee.com/ks-custle/core-gm/go-control-plane/envoy/config/route/v3"
    25  	"gitee.com/ks-custle/core-gm/go-control-plane/pkg/cache/types"
    26  )
    27  
    28  const (
    29  	clusterName         = "cluster0"
    30  	routeName           = "route0"
    31  	listenerName        = "listener0"
    32  	runtimeName         = "runtime0"
    33  	tlsName             = "secret0"
    34  	rootName            = "root0"
    35  	extensionConfigName = "extensionConfig0"
    36  )
    37  
    38  var (
    39  	testEndpoint        = resource.MakeEndpoint(clusterName, 8080)
    40  	testCluster         = resource.MakeCluster(resource.Ads, clusterName)
    41  	testRoute           = resource.MakeRoute(routeName, clusterName)
    42  	testListener        = resource.MakeHTTPListener(resource.Ads, listenerName, 80, routeName)
    43  	testRuntime         = resource.MakeRuntime(runtimeName)
    44  	testSecret          = resource.MakeSecrets(tlsName, rootName)
    45  	testExtensionConfig = resource.MakeExtensionConfig(resource.Ads, extensionConfigName, routeName)
    46  )
    47  
    48  func TestValidate(t *testing.T) {
    49  	if err := testEndpoint.Validate(); err != nil {
    50  		t.Error(err)
    51  	}
    52  	if err := testCluster.Validate(); err != nil {
    53  		t.Error(err)
    54  	}
    55  	if err := testRoute.Validate(); err != nil {
    56  		t.Error(err)
    57  	}
    58  	if err := testListener.Validate(); err != nil {
    59  		t.Error(err)
    60  	}
    61  	if err := testRuntime.Validate(); err != nil {
    62  		t.Error(err)
    63  	}
    64  
    65  	invalidRoute := &route.RouteConfiguration{
    66  		Name: "test",
    67  		VirtualHosts: []*route.VirtualHost{{
    68  			Name:    "test",
    69  			Domains: []string{},
    70  		}},
    71  	}
    72  
    73  	if err := invalidRoute.Validate(); err == nil {
    74  		t.Error("expected an error")
    75  	}
    76  	if err := invalidRoute.VirtualHosts[0].Validate(); err == nil {
    77  		t.Error("expected an error")
    78  	}
    79  }
    80  
    81  func TestGetResourceName(t *testing.T) {
    82  	if name := cache.GetResourceName(testEndpoint); name != clusterName {
    83  		t.Errorf("GetResourceName(%v) => got %q, want %q", testEndpoint, name, clusterName)
    84  	}
    85  	if name := cache.GetResourceName(testCluster); name != clusterName {
    86  		t.Errorf("GetResourceName(%v) => got %q, want %q", testCluster, name, clusterName)
    87  	}
    88  	if name := cache.GetResourceName(testRoute); name != routeName {
    89  		t.Errorf("GetResourceName(%v) => got %q, want %q", testRoute, name, routeName)
    90  	}
    91  	if name := cache.GetResourceName(testListener); name != listenerName {
    92  		t.Errorf("GetResourceName(%v) => got %q, want %q", testListener, name, listenerName)
    93  	}
    94  	if name := cache.GetResourceName(testRuntime); name != runtimeName {
    95  		t.Errorf("GetResourceName(%v) => got %q, want %q", testRuntime, name, runtimeName)
    96  	}
    97  	if name := cache.GetResourceName(nil); name != "" {
    98  		t.Errorf("GetResourceName(nil) => got %q, want none", name)
    99  	}
   100  }
   101  
   102  func TestGetResourceReferences(t *testing.T) {
   103  	cases := []struct {
   104  		in  types.Resource
   105  		out map[string]bool
   106  	}{
   107  		{
   108  			in:  nil,
   109  			out: map[string]bool{},
   110  		},
   111  		{
   112  			in:  testCluster,
   113  			out: map[string]bool{clusterName: true},
   114  		},
   115  		{
   116  			in: &cluster.Cluster{Name: clusterName, ClusterDiscoveryType: &cluster.Cluster_Type{Type: cluster.Cluster_EDS},
   117  				EdsClusterConfig: &cluster.Cluster_EdsClusterConfig{ServiceName: "test"}},
   118  			out: map[string]bool{"test": true},
   119  		},
   120  		{
   121  			in:  resource.MakeHTTPListener(resource.Ads, listenerName, 80, routeName),
   122  			out: map[string]bool{routeName: true},
   123  		},
   124  		{
   125  			in:  resource.MakeTCPListener(listenerName, 80, clusterName),
   126  			out: map[string]bool{},
   127  		},
   128  		{
   129  			in:  testRoute,
   130  			out: map[string]bool{},
   131  		},
   132  		{
   133  			in:  testEndpoint,
   134  			out: map[string]bool{},
   135  		},
   136  		{
   137  			in:  testRuntime,
   138  			out: map[string]bool{},
   139  		},
   140  	}
   141  	for _, cs := range cases {
   142  		names := cache.GetResourceReferences(cache.IndexResourcesByName([]types.ResourceWithTTL{{Resource: cs.in}}))
   143  		if !reflect.DeepEqual(names, cs.out) {
   144  			t.Errorf("GetResourceReferences(%v) => got %v, want %v", cs.in, names, cs.out)
   145  		}
   146  	}
   147  }