istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/test/framework/components/echo/match/matchers_test.go (about)

     1  // Copyright Istio 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 match_test
    16  
    17  import (
    18  	"testing"
    19  
    20  	"istio.io/api/annotation"
    21  	"istio.io/istio/pkg/test"
    22  	"istio.io/istio/pkg/test/framework/components/cluster"
    23  	"istio.io/istio/pkg/test/framework/components/echo"
    24  	"istio.io/istio/pkg/test/framework/components/echo/match"
    25  	"istio.io/istio/pkg/test/framework/components/namespace"
    26  	"istio.io/istio/pkg/test/framework/resource"
    27  )
    28  
    29  var (
    30  	// 2 clusters on 2 networks
    31  	cls1 = &cluster.FakeCluster{Topology: cluster.Topology{ClusterName: "cls1", Network: "n1", Index: 0, ClusterKind: cluster.Fake}}
    32  
    33  	// simple pod
    34  	a1 = &fakeInstance{Cluster: cls1, Namespace: namespace.Static("echo"), Service: "a"}
    35  	// simple pod with different svc
    36  	b1 = &fakeInstance{Cluster: cls1, Namespace: namespace.Static("echo"), Service: "b"}
    37  	// virtual machine
    38  	vm1 = &fakeInstance{Cluster: cls1, Namespace: namespace.Static("echo"), Service: "vm", DeployAsVM: true}
    39  	// headless
    40  	headless1 = &fakeInstance{Cluster: cls1, Namespace: namespace.Static("echo"), Service: "headless", Headless: true}
    41  	// naked pod (uninjected)
    42  	naked1 = &fakeInstance{Cluster: cls1, Namespace: namespace.Static("echo"), Service: "naked", Subsets: []echo.SubsetConfig{{
    43  		Annotations: map[string]string{annotation.SidecarInject.Name: "false"},
    44  	}}}
    45  	// external svc
    46  	external1 = &fakeInstance{
    47  		Cluster: cls1, Namespace: namespace.Static("echo"), Service: "external", DefaultHostHeader: "external.com", Subsets: []echo.SubsetConfig{{
    48  			Annotations: map[string]string{annotation.SidecarInject.Name: "false"},
    49  		}},
    50  	}
    51  )
    52  
    53  func TestRegularPod(t *testing.T) {
    54  	tests := []struct {
    55  		app    echo.Instance
    56  		expect bool
    57  	}{
    58  		{app: a1, expect: true},
    59  		{app: b1, expect: true},
    60  		{app: vm1, expect: false},
    61  		{app: naked1, expect: false},
    62  		{app: external1, expect: false},
    63  		{app: headless1, expect: false},
    64  	}
    65  	for _, tt := range tests {
    66  		t.Run(tt.app.Config().Service, func(t *testing.T) {
    67  			if got := match.RegularPod(tt.app); got != tt.expect {
    68  				t.Errorf("got %v expected %v", got, tt.expect)
    69  			}
    70  		})
    71  	}
    72  }
    73  
    74  func TestNaked(t *testing.T) {
    75  	tests := []struct {
    76  		app    echo.Instance
    77  		expect bool
    78  	}{
    79  		{app: a1, expect: false},
    80  		{app: headless1, expect: false},
    81  		{app: naked1, expect: true},
    82  		{app: external1, expect: true},
    83  	}
    84  	for _, tt := range tests {
    85  		t.Run(tt.app.Config().Service, func(t *testing.T) {
    86  			if got := tt.app.Config().IsNaked(); got != tt.expect {
    87  				t.Errorf("got %v expected %v", got, tt.expect)
    88  			}
    89  		})
    90  	}
    91  }
    92  
    93  var _ echo.Instance = fakeInstance{}
    94  
    95  // fakeInstance wraps echo.Config for test-framework internals tests where we don't actually make calls
    96  type fakeInstance echo.Config
    97  
    98  func (f fakeInstance) WithWorkloads(wl ...echo.Workload) echo.Instance {
    99  	// TODO implement me
   100  	panic("implement me")
   101  }
   102  
   103  func (f fakeInstance) Instances() echo.Instances {
   104  	return echo.Instances{f}
   105  }
   106  
   107  func (f fakeInstance) ID() resource.ID {
   108  	panic("implement me")
   109  }
   110  
   111  func (f fakeInstance) NamespacedName() echo.NamespacedName {
   112  	return f.Config().NamespacedName()
   113  }
   114  
   115  func (f fakeInstance) PortForName(name string) echo.Port {
   116  	return f.Config().Ports.MustForName(name)
   117  }
   118  
   119  func (f fakeInstance) Config() echo.Config {
   120  	cfg := echo.Config(f)
   121  	_ = cfg.FillDefaults(nil)
   122  	return cfg
   123  }
   124  
   125  func (f fakeInstance) ServiceName() string {
   126  	return f.Config().Service
   127  }
   128  
   129  func (f fakeInstance) NamespaceName() string {
   130  	return f.Config().NamespaceName()
   131  }
   132  
   133  func (f fakeInstance) ServiceAccountName() string {
   134  	return f.Config().ServiceAccountName()
   135  }
   136  
   137  func (f fakeInstance) ClusterLocalFQDN() string {
   138  	return f.Config().ClusterLocalFQDN()
   139  }
   140  
   141  func (f fakeInstance) ClusterSetLocalFQDN() string {
   142  	return f.Config().ClusterSetLocalFQDN()
   143  }
   144  
   145  func (f fakeInstance) Address() string {
   146  	panic("implement me")
   147  }
   148  
   149  func (f fakeInstance) Addresses() []string {
   150  	panic("implement me")
   151  }
   152  
   153  func (f fakeInstance) Workloads() (echo.Workloads, error) {
   154  	panic("implement me")
   155  }
   156  
   157  func (f fakeInstance) WorkloadsOrFail(test.Failer) echo.Workloads {
   158  	panic("implement me")
   159  }
   160  
   161  func (f fakeInstance) MustWorkloads() echo.Workloads {
   162  	panic("implement me")
   163  }
   164  
   165  func (f fakeInstance) Clusters() cluster.Clusters {
   166  	panic("implement me")
   167  }
   168  
   169  func (f fakeInstance) Call(echo.CallOptions) (echo.CallResult, error) {
   170  	panic("implement me")
   171  }
   172  
   173  func (f fakeInstance) CallOrFail(test.Failer, echo.CallOptions) echo.CallResult {
   174  	panic("implement me")
   175  }
   176  
   177  func (f fakeInstance) UpdateWorkloadLabel(add map[string]string, remove []string) error {
   178  	panic("implement me")
   179  }
   180  
   181  func (f fakeInstance) Restart() error {
   182  	panic("implement me")
   183  }