github.com/polarismesh/polaris@v1.17.8/cache/service/router_rule_query_test.go (about)

     1  /**
     2   * Tencent is pleased to support the open source community by making Polaris available.
     3   *
     4   * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
     5   *
     6   * Licensed under the BSD 3-Clause License (the "License");
     7   * you may not use this file except in compliance with the License.
     8   * You may obtain a copy of the License at
     9   *
    10   * https://opensource.org/licenses/BSD-3-Clause
    11   *
    12   * Unless required by applicable law or agreed to in writing, software distributed
    13   * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
    14   * CONDITIONS OF ANY KIND, either express or implied. See the License for the
    15   * specific language governing permissions and limitations under the License.
    16   */
    17  
    18  package service
    19  
    20  import (
    21  	"testing"
    22  
    23  	apitraffic "github.com/polarismesh/specification/source/go/api/v1/traffic_manage"
    24  
    25  	"github.com/polarismesh/polaris/common/model"
    26  )
    27  
    28  func Test_queryRoutingRuleV2ByService(t *testing.T) {
    29  	type args struct {
    30  		rule            *model.ExtendRouterConfig
    31  		sourceNamespace string
    32  		sourceService   string
    33  		destNamespace   string
    34  		destService     string
    35  		both            bool
    36  	}
    37  	tests := []struct {
    38  		name string
    39  		args args
    40  		want bool
    41  	}{
    42  		{
    43  			name: "命名空间-或-精确查询",
    44  			args: args{
    45  				rule: &model.ExtendRouterConfig{
    46  					RuleRouting: &apitraffic.RuleRoutingConfig{
    47  						Rules: []*apitraffic.SubRuleRouting{
    48  							{
    49  								Sources: []*apitraffic.SourceService{
    50  									{
    51  										Service:   "test-1",
    52  										Namespace: "test-1",
    53  									},
    54  								},
    55  								Destinations: []*apitraffic.DestinationGroup{
    56  									{
    57  										Service:   "test-1",
    58  										Namespace: "test-1",
    59  									},
    60  								},
    61  							},
    62  						},
    63  					},
    64  				},
    65  				sourceNamespace: "test-1",
    66  				sourceService:   "",
    67  				destNamespace:   "test-2",
    68  				destService:     "",
    69  				both:            false,
    70  			},
    71  			want: true,
    72  		},
    73  		{
    74  			name: "命名空间-与-精确查询",
    75  			args: args{
    76  				rule: &model.ExtendRouterConfig{
    77  					RuleRouting: &apitraffic.RuleRoutingConfig{
    78  						Rules: []*apitraffic.SubRuleRouting{
    79  							{
    80  								Sources: []*apitraffic.SourceService{
    81  									{
    82  										Service:   "test-1",
    83  										Namespace: "test-1",
    84  									},
    85  								},
    86  								Destinations: []*apitraffic.DestinationGroup{
    87  									{
    88  										Service:   "test-1",
    89  										Namespace: "test-1",
    90  									},
    91  								},
    92  							},
    93  						},
    94  					},
    95  				},
    96  				sourceNamespace: "test-1",
    97  				sourceService:   "",
    98  				destNamespace:   "test-2",
    99  				destService:     "",
   100  				both:            true,
   101  			},
   102  			want: false,
   103  		},
   104  		{
   105  			name: "命名空间-或-模糊查询",
   106  			args: args{
   107  				rule: &model.ExtendRouterConfig{
   108  					RuleRouting: &apitraffic.RuleRoutingConfig{
   109  						Rules: []*apitraffic.SubRuleRouting{
   110  							{
   111  								Sources: []*apitraffic.SourceService{
   112  									{
   113  										Service:   "test-1",
   114  										Namespace: "test-1",
   115  									},
   116  								},
   117  								Destinations: []*apitraffic.DestinationGroup{
   118  									{
   119  										Service:   "test-1",
   120  										Namespace: "test-1",
   121  									},
   122  								},
   123  							},
   124  						},
   125  					},
   126  				},
   127  				sourceNamespace: "test*",
   128  				sourceService:   "",
   129  				destNamespace:   "test-1",
   130  				destService:     "",
   131  				both:            false,
   132  			},
   133  			want: true,
   134  		},
   135  		{
   136  			name: "命名空间-与-模糊查询",
   137  			args: args{
   138  				rule: &model.ExtendRouterConfig{
   139  					RuleRouting: &apitraffic.RuleRoutingConfig{
   140  						Rules: []*apitraffic.SubRuleRouting{
   141  							{
   142  								Sources: []*apitraffic.SourceService{
   143  									{
   144  										Service:   "test-1",
   145  										Namespace: "test-1",
   146  									},
   147  								},
   148  								Destinations: []*apitraffic.DestinationGroup{
   149  									{
   150  										Service:   "test-1",
   151  										Namespace: "test-1",
   152  									},
   153  								},
   154  							},
   155  						},
   156  					},
   157  				},
   158  				sourceNamespace: "test*",
   159  				sourceService:   "",
   160  				destNamespace:   "tesa*",
   161  				destService:     "",
   162  				both:            true,
   163  			},
   164  			want: false,
   165  		},
   166  		{
   167  			name: "(命名空间精确查询+服务名精确查询)-或",
   168  			args: args{
   169  				rule: &model.ExtendRouterConfig{
   170  					RuleRouting: &apitraffic.RuleRoutingConfig{
   171  						Rules: []*apitraffic.SubRuleRouting{
   172  							{
   173  								Sources: []*apitraffic.SourceService{
   174  									{
   175  										Service:   "test-1",
   176  										Namespace: "test-1",
   177  									},
   178  								},
   179  								Destinations: []*apitraffic.DestinationGroup{
   180  									{
   181  										Service:   "test-1",
   182  										Namespace: "test-1",
   183  									},
   184  								},
   185  							},
   186  						},
   187  					},
   188  				},
   189  				sourceNamespace: "test-1",
   190  				sourceService:   "test-1",
   191  				destNamespace:   "test-1",
   192  				destService:     "test-1",
   193  				both:            false,
   194  			},
   195  			want: true,
   196  		},
   197  		{
   198  			name: "(命名空间精确查询+服务名精确查询)-与",
   199  			args: args{
   200  				rule: &model.ExtendRouterConfig{
   201  					RuleRouting: &apitraffic.RuleRoutingConfig{
   202  						Rules: []*apitraffic.SubRuleRouting{
   203  							{
   204  								Sources: []*apitraffic.SourceService{
   205  									{
   206  										Service:   "test-1",
   207  										Namespace: "test-1",
   208  									},
   209  								},
   210  								Destinations: []*apitraffic.DestinationGroup{
   211  									{
   212  										Service:   "test-1",
   213  										Namespace: "test-1",
   214  									},
   215  								},
   216  							},
   217  						},
   218  					},
   219  				},
   220  				sourceNamespace: "test-1",
   221  				sourceService:   "test-1",
   222  				destNamespace:   "test-1",
   223  				destService:     "test-1",
   224  				both:            true,
   225  			},
   226  			want: true,
   227  		},
   228  		{
   229  			name: "(命名空间精确查询+服务名精确查询)-与",
   230  			args: args{
   231  				rule: &model.ExtendRouterConfig{
   232  					RuleRouting: &apitraffic.RuleRoutingConfig{
   233  						Rules: []*apitraffic.SubRuleRouting{
   234  							{
   235  								Sources: []*apitraffic.SourceService{
   236  									{
   237  										Service:   "test-1",
   238  										Namespace: "test-1",
   239  									},
   240  								},
   241  								Destinations: []*apitraffic.DestinationGroup{
   242  									{
   243  										Service:   "test-1",
   244  										Namespace: "test-1",
   245  									},
   246  								},
   247  							},
   248  						},
   249  					},
   250  				},
   251  				sourceNamespace: "test-1",
   252  				sourceService:   "test-1",
   253  				destNamespace:   "test-1",
   254  				destService:     "test-2",
   255  				both:            true,
   256  			},
   257  			want: false,
   258  		},
   259  		{
   260  			name: "(命名空间模糊+服务名精确查询)-或",
   261  			args: args{
   262  				rule: &model.ExtendRouterConfig{
   263  					RuleRouting: &apitraffic.RuleRoutingConfig{
   264  						Rules: []*apitraffic.SubRuleRouting{
   265  							{
   266  								Sources: []*apitraffic.SourceService{
   267  									{
   268  										Service:   "test-1",
   269  										Namespace: "test-1",
   270  									},
   271  								},
   272  								Destinations: []*apitraffic.DestinationGroup{
   273  									{
   274  										Service:   "test-1",
   275  										Namespace: "test-1",
   276  									},
   277  								},
   278  							},
   279  						},
   280  					},
   281  				},
   282  				sourceNamespace: "te*",
   283  				sourceService:   "test-1",
   284  				destNamespace:   "tes*",
   285  				destService:     "test-1",
   286  				both:            false,
   287  			},
   288  			want: true,
   289  		},
   290  		{
   291  			name: "(命名空间模糊+服务名精确查询)-或",
   292  			args: args{
   293  				rule: &model.ExtendRouterConfig{
   294  					RuleRouting: &apitraffic.RuleRoutingConfig{
   295  						Rules: []*apitraffic.SubRuleRouting{
   296  							{
   297  								Sources: []*apitraffic.SourceService{
   298  									{
   299  										Service:   "test-1",
   300  										Namespace: "test-1",
   301  									},
   302  								},
   303  								Destinations: []*apitraffic.DestinationGroup{
   304  									{
   305  										Service:   "test-1",
   306  										Namespace: "test-1",
   307  									},
   308  								},
   309  							},
   310  						},
   311  					},
   312  				},
   313  				sourceNamespace: "te*",
   314  				sourceService:   "test-1",
   315  				destNamespace:   "",
   316  				destService:     "",
   317  				both:            false,
   318  			},
   319  			want: true,
   320  		},
   321  		{
   322  			name: "(命名空间模糊+服务名精确查询)-或",
   323  			args: args{
   324  				rule: &model.ExtendRouterConfig{
   325  					RuleRouting: &apitraffic.RuleRoutingConfig{
   326  						Rules: []*apitraffic.SubRuleRouting{
   327  							{
   328  								Sources: []*apitraffic.SourceService{
   329  									{
   330  										Service:   "test-1",
   331  										Namespace: "test-1",
   332  									},
   333  								},
   334  								Destinations: []*apitraffic.DestinationGroup{
   335  									{
   336  										Service:   "test-1",
   337  										Namespace: "test-1",
   338  									},
   339  								},
   340  							},
   341  						},
   342  					},
   343  				},
   344  				sourceNamespace: "",
   345  				sourceService:   "",
   346  				destNamespace:   "tese*",
   347  				destService:     "test-1",
   348  				both:            false,
   349  			},
   350  			want: false,
   351  		},
   352  	}
   353  	for _, tt := range tests {
   354  		t.Run(tt.name, func(t *testing.T) {
   355  			if got := queryRoutingRuleV2ByService(tt.args.rule, tt.args.sourceNamespace, tt.args.sourceService,
   356  				tt.args.destNamespace, tt.args.destService, tt.args.both); got != tt.want {
   357  
   358  				t.Errorf("queryRoutingRuleV2ByService() = %v, want %v", got, tt.want)
   359  			}
   360  		})
   361  	}
   362  }