Skip to content

Commit 0d4370e

Browse files
committed
fix unit tests
1 parent cfa135e commit 0d4370e

5 files changed

Lines changed: 60 additions & 19 deletions

File tree

google/cloud/storage/bucket_ip_filter_test.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
// limitations under the License.
1414

1515
#include "google/cloud/storage/bucket_ip_filter.h"
16-
#include "google/cloud/testing_util/is_proto_equal.h"
1716
#include <gmock/gmock.h>
1817

1918
namespace google {

google/cloud/storage/internal/bucket_requests.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,16 @@ void DiffIamConfiguration(BucketMetadataPatchBuilder& builder,
8989
}
9090
}
9191

92+
void DiffIpFilter(BucketMetadataPatchBuilder& builder, BucketMetadata const& o,
93+
BucketMetadata const& u) {
94+
if (o.ip_filter_as_optional() == u.ip_filter_as_optional()) return;
95+
if (u.has_ip_filter()) {
96+
builder.SetIpFilter(u.ip_filter());
97+
} else {
98+
builder.ResetIpFilter();
99+
}
100+
}
101+
92102
void DiffLabels(BucketMetadataPatchBuilder& builder, BucketMetadata const& o,
93103
BucketMetadata const& u) {
94104
if (o.labels() == u.labels()) return;
@@ -199,6 +209,7 @@ BucketMetadataPatchBuilder DiffBucketMetadata(BucketMetadata const& original,
199209
DiffDefaultObjectAcl(builder, original, updated);
200210
DiffEncryption(builder, original, updated);
201211
DiffIamConfiguration(builder, original, updated);
212+
DiffIpFilter(builder, original, updated);
202213
DiffLabels(builder, original, updated);
203214
DiffLifecycle(builder, original, updated);
204215
DiffLogging(builder, original, updated);

google/cloud/storage/internal/bucket_requests_test.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -464,11 +464,11 @@ TEST(PatchBucketRequestTest, DiffSetIpFilter) {
464464
BucketIpFilterPublicNetworkSource{{"1.2.3.4/32"}};
465465
ip_filter.vpc_network_sources =
466466
absl::make_optional<std::vector<BucketIpFilterVpcNetworkSource>>(
467-
{BucketIpFilterVpcNetworkSource{"projects/p/global/networks/n",
468-
{"5.6.7.8/32"}},
469-
BucketIpFilterVpcNetworkSource{"projects/p/global/networks/m",
470-
{"9.0.1.2/32"}}});
471-
updated.set_ip_filter(ip_filter);
467+
{BucketIpFilterVpcNetworkSource{
468+
"projects/p/global/networks/n", {"5.6.7.8/32"}},
469+
BucketIpFilterVpcNetworkSource{
470+
"projects/p/global/networks/m", {"9.0.1.2/32"}}});
471+
updated.set_ip_filter(std::move(ip_filter));
472472
PatchBucketRequest request("test-bucket", original, updated);
473473

474474
auto patch = nlohmann::json::parse(request.payload());

google/cloud/storage/internal/grpc/bucket_request_parser.cc

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,46 @@ Status PatchIamConfig(Bucket& b, nlohmann::json const& i) {
209209
return Status{};
210210
}
211211

212+
Status PatchIpFilter(Bucket& b, nlohmann::json const& p) {
213+
if (p.is_null()) {
214+
b.clear_ip_filter();
215+
return Status{};
216+
}
217+
auto& ip_filter = *b.mutable_ip_filter();
218+
if (p.contains("mode")) {
219+
ip_filter.set_mode(p.value("mode", ""));
220+
}
221+
if (p.contains("allowAllServiceAgentAccess")) {
222+
ip_filter.set_allow_all_service_agent_access(
223+
p.value("allowAllServiceAgentAccess", false));
224+
}
225+
if (p.contains("allowCrossOrgVpcs")) {
226+
ip_filter.set_allow_cross_org_vpcs(p.value("allowCrossOrgVpcs", false));
227+
}
228+
if (p.contains("publicNetworkSource")) {
229+
auto& pns = *ip_filter.mutable_public_network_source();
230+
auto const& public_network_source = p["publicNetworkSource"];
231+
if (public_network_source.contains("allowedIpCidrRanges")) {
232+
for (auto const& v :
233+
public_network_source["allowedIpCidrRanges"]) {
234+
pns.add_allowed_ip_cidr_ranges(v.get<std::string>());
235+
}
236+
}
237+
}
238+
if (p.contains("vpcNetworkSources")) {
239+
for (auto const& v : p["vpcNetworkSources"]) {
240+
auto& entry = *ip_filter.add_vpc_network_sources();
241+
entry.set_network(v.value("network", ""));
242+
if (v.contains("allowedIpCidrRanges")) {
243+
for (auto const& ip : v["allowedIpCidrRanges"]) {
244+
entry.add_allowed_ip_cidr_ranges(ip.get<std::string>());
245+
}
246+
}
247+
}
248+
}
249+
return Status{};
250+
}
251+
212252
Status PatchAutoclass(Bucket& bucket, nlohmann::json const& p) {
213253
if (p.is_null()) {
214254
bucket.clear_autoclass();
@@ -558,6 +598,7 @@ StatusOr<google::storage::v2::UpdateBucketRequest> ToProto(
558598
{"billing", "", PatchBilling},
559599
{"retentionPolicy", "retention_policy", PatchRetentionPolicy},
560600
{"iamConfiguration", "iam_config", PatchIamConfig},
601+
{"ipFilter", "ip_filter", PatchIpFilter},
561602
{"autoclass", "", PatchAutoclass},
562603
};
563604

google/cloud/storage/internal/grpc/bucket_request_parser_test.cc

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,7 @@ TEST(GrpcBucketRequestParser, PatchBucketRequestResetLabels) {
643643
TEST(GrpcBucketRequestParser, PatchBucketRequestResetIpFilter) {
644644
auto constexpr kTextProto = R"pb(
645645
bucket { name: "projects/_/buckets/bucket-name" }
646-
update_mask {}
646+
update_mask { paths: "ip_filter" }
647647
)pb";
648648
google::storage::v2::UpdateBucketRequest expected;
649649
ASSERT_TRUE(TextFormat::ParseFromString(kTextProto, &expected));
@@ -653,11 +653,6 @@ TEST(GrpcBucketRequestParser, PatchBucketRequestResetIpFilter) {
653653

654654
auto actual = ToProto(req);
655655
ASSERT_STATUS_OK(actual);
656-
// First check the paths. We do not care about their order, so checking them
657-
// with IsProtoEqual does not work.
658-
EXPECT_THAT(actual->update_mask().paths(), UnorderedElementsAre("ip_filter"));
659-
// Clear the paths, which we already compared, and compare the proto.
660-
actual->mutable_update_mask()->clear_paths();
661656
EXPECT_THAT(*actual, IsProtoEqual(expected));
662657
}
663658

@@ -676,7 +671,7 @@ TEST(GrpcBucketRequestParser, PatchBucketRequestIpFilter) {
676671
}
677672
}
678673
}
679-
update_mask {}
674+
update_mask { paths: "ip_filter" }
680675
)pb";
681676
google::storage::v2::UpdateBucketRequest expected;
682677
ASSERT_TRUE(TextFormat::ParseFromString(kTextProto, &expected));
@@ -693,15 +688,10 @@ TEST(GrpcBucketRequestParser, PatchBucketRequestIpFilter) {
693688
"projects/p/global/networks/n", {"5.6.7.8/32"}}});
694689
storage::internal::PatchBucketRequest req(
695690
"bucket-name",
696-
storage::BucketMetadataPatchBuilder().SetIpFilter(ip_filter));
691+
storage::BucketMetadataPatchBuilder().SetIpFilter(std::move(ip_filter)));
697692

698693
auto actual = ToProto(req);
699694
ASSERT_STATUS_OK(actual);
700-
// First check the paths. We do not care about their order, so checking them
701-
// with IsProtoEqual does not work.
702-
EXPECT_THAT(actual->update_mask().paths(), UnorderedElementsAre("ip_filter"));
703-
// Clear the paths, which we already compared, and compare the proto.
704-
actual->mutable_update_mask()->clear_paths();
705695
EXPECT_THAT(*actual, IsProtoEqual(expected));
706696
}
707697

0 commit comments

Comments
 (0)