• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2022 The gRPC 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 io.grpc.gcp.observability.interceptors;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 import static org.mockito.Mockito.mock;
21 import static org.mockito.Mockito.when;
22 
23 import com.google.common.collect.ImmutableList;
24 import io.grpc.gcp.observability.ObservabilityConfig;
25 import io.grpc.gcp.observability.ObservabilityConfig.LogFilter;
26 import io.grpc.gcp.observability.interceptors.ConfigFilterHelper.FilterParams;
27 import java.util.Collections;
28 import java.util.List;
29 import org.junit.Before;
30 import org.junit.Test;
31 
32 public class ConfigFilterHelperTest {
33   private static final ImmutableList<LogFilter> configLogFilters =
34       ImmutableList.of(
35           new LogFilter(Collections.emptySet(), Collections.singleton("service1/Method2"), false,
36               1024, 1024, false),
37           new LogFilter(
38               Collections.singleton("service2"), Collections.singleton("service4/method2"), false,
39               2048, 1024, false),
40           new LogFilter(
41               Collections.singleton("service2"), Collections.singleton("service4/method3"), false,
42               2048, 1024, true),
43           new LogFilter(
44               Collections.emptySet(), Collections.emptySet(), true,
45               128, 128, false));
46 
47   private ObservabilityConfig mockConfig;
48   private ConfigFilterHelper configFilterHelper;
49 
50   @Before
setup()51   public void setup() {
52     mockConfig = mock(ObservabilityConfig.class);
53     configFilterHelper = ConfigFilterHelper.getInstance(mockConfig);
54   }
55 
56   @Test
enableCloudLogging_withoutLogFilters()57   public void enableCloudLogging_withoutLogFilters() {
58     when(mockConfig.isEnableCloudLogging()).thenReturn(true);
59     assertThat(mockConfig.getClientLogFilters()).isEmpty();
60     assertThat(mockConfig.getServerLogFilters()).isEmpty();
61   }
62 
63   @Test
checkMethodLog_withoutLogFilters()64   public void checkMethodLog_withoutLogFilters() {
65     when(mockConfig.isEnableCloudLogging()).thenReturn(true);
66     assertThat(mockConfig.getClientLogFilters()).isEmpty();
67     assertThat(mockConfig.getServerLogFilters()).isEmpty();
68 
69     FilterParams expectedParams =
70         FilterParams.create(false, 0, 0);
71     FilterParams clientResultParams
72         = configFilterHelper.logRpcMethod("service3/Method3", true);
73     assertThat(clientResultParams).isEqualTo(expectedParams);
74     FilterParams serverResultParams
75         = configFilterHelper.logRpcMethod("service3/Method3", false);
76     assertThat(serverResultParams).isEqualTo(expectedParams);
77   }
78 
79   @Test
checkMethodAlwaysLogged()80   public void checkMethodAlwaysLogged() {
81     List<LogFilter> sampleLogFilters =
82         ImmutableList.of(
83             new LogFilter(
84                 Collections.emptySet(), Collections.emptySet(), true,
85                 4096, 4096, false));
86     when(mockConfig.getClientLogFilters()).thenReturn(sampleLogFilters);
87     when(mockConfig.getServerLogFilters()).thenReturn(sampleLogFilters);
88 
89     FilterParams expectedParams =
90         FilterParams.create(true, 4096, 4096);
91     FilterParams clientResultParams
92         = configFilterHelper.logRpcMethod("service1/Method6", true);
93     assertThat(clientResultParams).isEqualTo(expectedParams);
94     FilterParams serverResultParams
95         = configFilterHelper.logRpcMethod("service1/Method6", false);
96     assertThat(serverResultParams).isEqualTo(expectedParams);
97   }
98 
99   @Test
checkMethodNotToBeLogged()100   public void checkMethodNotToBeLogged() {
101     List<LogFilter> sampleLogFilters =
102         ImmutableList.of(
103             new LogFilter(Collections.emptySet(), Collections.singleton("service2/*"), false,
104                 1024, 1024, true),
105             new LogFilter(
106                 Collections.singleton("service2/Method1"), Collections.emptySet(), false,
107                 2048, 1024, false));
108     when(mockConfig.getClientLogFilters()).thenReturn(sampleLogFilters);
109     when(mockConfig.getServerLogFilters()).thenReturn(sampleLogFilters);
110 
111     FilterParams expectedParams =
112         FilterParams.create(false, 0, 0);
113     FilterParams clientResultParams1
114         = configFilterHelper.logRpcMethod("service3/Method3", true);
115     assertThat(clientResultParams1).isEqualTo(expectedParams);
116 
117     FilterParams clientResultParams2
118         = configFilterHelper.logRpcMethod("service2/Method1", true);
119     assertThat(clientResultParams2).isEqualTo(expectedParams);
120 
121     FilterParams serverResultParams
122         = configFilterHelper.logRpcMethod("service2/Method1", false);
123     assertThat(serverResultParams).isEqualTo(expectedParams);
124   }
125 
126   @Test
checkMethodToBeLoggedConditional()127   public void checkMethodToBeLoggedConditional() {
128     when(mockConfig.getClientLogFilters()).thenReturn(configLogFilters);
129     when(mockConfig.getServerLogFilters()).thenReturn(configLogFilters);
130 
131     FilterParams expectedParams =
132         FilterParams.create(true, 1024, 1024);
133     FilterParams resultParams
134         = configFilterHelper.logRpcMethod("service1/Method2", true);
135     assertThat(resultParams).isEqualTo(expectedParams);
136 
137     FilterParams expectedParamsWildCard =
138         FilterParams.create(true, 2048, 1024);
139     FilterParams resultParamsWildCard
140         = configFilterHelper.logRpcMethod("service2/Method1", true);
141     assertThat(resultParamsWildCard).isEqualTo(expectedParamsWildCard);
142 
143     FilterParams excludeParams =
144         FilterParams.create(false, 0, 0);
145     FilterParams serverResultParams
146         = configFilterHelper.logRpcMethod("service4/method3", false);
147     assertThat(serverResultParams).isEqualTo(excludeParams);
148   }
149 }
150