From deee1f704e6f10902f603caf292dcf7a4711992e Mon Sep 17 00:00:00 2001 From: ZHANG Dapeng Date: Fri, 12 Mar 2021 14:06:00 -0800 Subject: [PATCH] xds: fix Node.toBuilder() bug --- .../main/java/io/grpc/xds/EnvoyProtoData.java | 16 +++++++++------- .../java/io/grpc/xds/EnvoyProtoDataTest.java | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/xds/src/main/java/io/grpc/xds/EnvoyProtoData.java b/xds/src/main/java/io/grpc/xds/EnvoyProtoData.java index 633bd6af91a..b1db7f0b496 100644 --- a/xds/src/main/java/io/grpc/xds/EnvoyProtoData.java +++ b/xds/src/main/java/io/grpc/xds/EnvoyProtoData.java @@ -295,14 +295,16 @@ static Builder newBuilder() { } Builder toBuilder() { - Builder builder = new Builder().setId(id).setCluster(cluster); - if (metadata != null) { - builder.setMetadata(metadata); - } - if (locality != null) { - builder.setLocality(locality); - } + Builder builder = new Builder(); + builder.id = id; + builder.cluster = cluster; + builder.metadata = metadata; + builder.locality = locality; + builder.buildVersion = buildVersion; builder.listeningAddresses.addAll(listeningAddresses); + builder.userAgentName = userAgentName; + builder.userAgentVersion = userAgentVersion; + builder.clientFeatures.addAll(clientFeatures); return builder; } diff --git a/xds/src/test/java/io/grpc/xds/EnvoyProtoDataTest.java b/xds/src/test/java/io/grpc/xds/EnvoyProtoDataTest.java index 600075fc41c..f39a47e3fa7 100644 --- a/xds/src/test/java/io/grpc/xds/EnvoyProtoDataTest.java +++ b/xds/src/test/java/io/grpc/xds/EnvoyProtoDataTest.java @@ -626,4 +626,22 @@ public void clusterStats_convertToEnvoyProto() { .setDroppedCount(100)) .build()); } + + @Test + public void nodeToBuilderPropagatesAllAttributes() { + Node node = Node.newBuilder() + .setId("id") + .setCluster("cluster") + .setMetadata(ImmutableMap.of("key1", "value1", "key2", "value2")) + .setLocality(new Locality("region", "zone", "subzone")) + .setBuildVersion("v1") + .setUserAgentName("grpc-java") + .setUserAgentVersion("v1.0.9") + .addListeningAddresses(new Address("localhost", 8080)) + .addListeningAddresses(new Address("localhost", 8081)) + .addClientFeatures("feature1") + .addClientFeatures("feature2") + .build(); + assertThat(node.toBuilder().build()).isEqualTo(node); + } }