xref: /aosp_15_r20/external/grpc-grpc-java/xds/src/test/java/io/grpc/xds/EnvoyProtoDataTest.java (revision e07d83d3ffcef9ecfc9f7f475418ec639ff0e5fe)
1 /*
2  * Copyright 2019 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.xds;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import com.google.common.collect.ImmutableMap;
22 import com.google.protobuf.Struct;
23 import com.google.protobuf.Value;
24 import io.grpc.xds.EnvoyProtoData.Address;
25 import io.grpc.xds.EnvoyProtoData.Node;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.junit.runners.JUnit4;
29 
30 /**
31  * Unit tests for {@link EnvoyProtoData}.
32  */
33 @RunWith(JUnit4.class)
34 public class EnvoyProtoDataTest {
35 
36   @SuppressWarnings("deprecation")
37   @Test
convertNode()38   public void convertNode() {
39     Node node = Node.newBuilder()
40         .setId("node-id")
41         .setCluster("cluster")
42         .setMetadata(
43             ImmutableMap.of(
44                 "TRAFFICDIRECTOR_INTERCEPTION_PORT",
45                 "ENVOY_PORT",
46                 "TRAFFICDIRECTOR_NETWORK_NAME",
47                 "VPC_NETWORK_NAME"))
48         .setLocality(Locality.create("region", "zone", "subzone"))
49         .addListeningAddresses(new Address("www.foo.com", 8080))
50         .addListeningAddresses(new Address("www.bar.com", 8088))
51         .setBuildVersion("v1")
52         .setUserAgentName("agent")
53         .setUserAgentVersion("1.1")
54         .addClientFeatures("feature-1")
55         .addClientFeatures("feature-2")
56         .build();
57     io.envoyproxy.envoy.config.core.v3.Node nodeProto =
58         io.envoyproxy.envoy.config.core.v3.Node.newBuilder()
59             .setId("node-id")
60             .setCluster("cluster")
61             .setMetadata(Struct.newBuilder()
62                 .putFields("TRAFFICDIRECTOR_INTERCEPTION_PORT",
63                     Value.newBuilder().setStringValue("ENVOY_PORT").build())
64                 .putFields("TRAFFICDIRECTOR_NETWORK_NAME",
65                     Value.newBuilder().setStringValue("VPC_NETWORK_NAME").build()))
66             .setLocality(
67                 io.envoyproxy.envoy.config.core.v3.Locality.newBuilder()
68                     .setRegion("region")
69                     .setZone("zone")
70                     .setSubZone("subzone"))
71             .addListeningAddresses(
72                 io.envoyproxy.envoy.config.core.v3.Address.newBuilder()
73                     .setSocketAddress(
74                         io.envoyproxy.envoy.config.core.v3.SocketAddress.newBuilder()
75                             .setAddress("www.foo.com")
76                             .setPortValue(8080)))
77             .addListeningAddresses(
78                 io.envoyproxy.envoy.config.core.v3.Address.newBuilder()
79                     .setSocketAddress(
80                         io.envoyproxy.envoy.config.core.v3.SocketAddress.newBuilder()
81                             .setAddress("www.bar.com")
82                             .setPortValue(8088)))
83             .setUserAgentName("agent")
84             .setUserAgentVersion("1.1")
85             .addClientFeatures("feature-1")
86             .addClientFeatures("feature-2")
87             .build();
88     assertThat(node.toEnvoyProtoNode()).isEqualTo(nodeProto);
89   }
90 
91   @Test
nodeToBuilderPropagatesAllAttributes()92   public void nodeToBuilderPropagatesAllAttributes() {
93     Node node = Node.newBuilder()
94         .setId("id")
95         .setCluster("cluster")
96         .setMetadata(ImmutableMap.of("key1", "value1", "key2", "value2"))
97         .setLocality(Locality.create("region", "zone", "subzone"))
98         .setBuildVersion("v1")
99         .setUserAgentName("grpc-java")
100         .setUserAgentVersion("v1.0.9")
101         .addListeningAddresses(new Address("localhost", 8080))
102         .addListeningAddresses(new Address("localhost", 8081))
103         .addClientFeatures("feature1")
104         .addClientFeatures("feature2")
105         .build();
106     assertThat(node.toBuilder().build()).isEqualTo(node);
107   }
108 }
109