|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +#include "iceberg/catalog/rest/rest_catalog.h" |
| 21 | + |
| 22 | +#include <memory> |
| 23 | +#include <string> |
| 24 | +#include <unordered_map> |
| 25 | +#include <unordered_set> |
| 26 | + |
| 27 | +#include <gmock/gmock.h> |
| 28 | +#include <gtest/gtest.h> |
| 29 | + |
| 30 | +#include "iceberg/catalog/rest/catalog_properties.h" |
| 31 | +#include "iceberg/catalog/rest/endpoint.h" |
| 32 | +#include "iceberg/catalog/rest/error_handlers.h" |
| 33 | +#include "iceberg/catalog/rest/http_client.h" |
| 34 | +#include "iceberg/catalog/rest/resource_paths.h" |
| 35 | +#include "iceberg/catalog/rest/rest_table.h" |
| 36 | +#include "iceberg/file_io.h" |
| 37 | +#include "iceberg/table_identifier.h" |
| 38 | +#include "iceberg/test/matchers.h" |
| 39 | + |
| 40 | +namespace iceberg::rest { |
| 41 | + |
| 42 | +using ::testing::_; |
| 43 | +using ::testing::Return; |
| 44 | + |
| 45 | +// -------------------------------------------------------------------------- |
| 46 | +// Mock HTTP client (same pattern as rest_table_scan_test.cc) |
| 47 | +// -------------------------------------------------------------------------- |
| 48 | +class MockHttpClient : public HttpClient { |
| 49 | + public: |
| 50 | + MockHttpClient() : HttpClient({}) {} |
| 51 | + |
| 52 | + MOCK_METHOD(Result<HttpResponse>, Get, |
| 53 | + (const std::string& path, |
| 54 | + (const std::unordered_map<std::string, std::string>&)params, |
| 55 | + (const std::unordered_map<std::string, std::string>&)headers, |
| 56 | + const ErrorHandler& error_handler, auth::AuthSession& session), |
| 57 | + (override)); |
| 58 | + |
| 59 | + MOCK_METHOD(Result<HttpResponse>, Post, |
| 60 | + (const std::string& path, const std::string& body, |
| 61 | + (const std::unordered_map<std::string, std::string>&)headers, |
| 62 | + const ErrorHandler& error_handler, auth::AuthSession& session), |
| 63 | + (override)); |
| 64 | + |
| 65 | + MOCK_METHOD(Result<HttpResponse>, Delete, |
| 66 | + (const std::string& path, |
| 67 | + (const std::unordered_map<std::string, std::string>&)params, |
| 68 | + (const std::unordered_map<std::string, std::string>&)headers, |
| 69 | + const ErrorHandler& error_handler, auth::AuthSession& session), |
| 70 | + (override)); |
| 71 | +}; |
| 72 | + |
| 73 | +// -------------------------------------------------------------------------- |
| 74 | +// Minimal FileIO stub |
| 75 | +// -------------------------------------------------------------------------- |
| 76 | +class NoOpFileIO : public FileIO { |
| 77 | + public: |
| 78 | + Result<std::string> ReadFile(const std::string&, std::optional<size_t>) override { |
| 79 | + return IOError("NoOpFileIO"); |
| 80 | + } |
| 81 | + Status WriteFile(const std::string&, std::string_view) override { return {}; } |
| 82 | + Status DeleteFile(const std::string&) override { return {}; } |
| 83 | +}; |
| 84 | + |
| 85 | +// -------------------------------------------------------------------------- |
| 86 | +// Helper JSON bodies for LoadTable GET responses |
| 87 | +// -------------------------------------------------------------------------- |
| 88 | + |
| 89 | +// Minimal table metadata blob (no snapshots, no partitions). |
| 90 | +constexpr std::string_view kBaseMetadataJson = |
| 91 | + R"("metadata-location":"s3://bucket/metadata/v1.json","metadata":{"format-version":2,"table-uuid":"test-uuid","location":"s3://bucket/test","last-sequence-number":0,"last-updated-ms":0,"last-column-id":1,"schemas":[{"type":"struct","schema-id":1,"fields":[{"id":1,"name":"id","type":"int","required":true}]}],"current-schema-id":1,"partition-specs":[{"spec-id":0,"fields":[]}],"default-spec-id":0,"last-partition-id":0,"sort-orders":[{"order-id":0,"fields":[]}],"default-sort-order-id":0,"properties":{}})"; |
| 92 | + |
| 93 | +// LoadTable response where server config requests server-side scan planning. |
| 94 | +const std::string kLoadTableServerScanResponse = |
| 95 | + std::string("{\"config\":{\"scan-planning-mode\":\"server\"},") + |
| 96 | + std::string(kBaseMetadataJson) + "}"; |
| 97 | + |
| 98 | +// LoadTable response with no scan-planning-mode set (defaults to client). |
| 99 | +const std::string kLoadTableDefaultResponse = |
| 100 | + std::string("{") + std::string(kBaseMetadataJson) + "}"; |
| 101 | + |
| 102 | +// -------------------------------------------------------------------------- |
| 103 | +// Test fixture: builds a RestCatalog via MakeForTesting so no HTTP calls |
| 104 | +// are made during catalog construction. |
| 105 | +// -------------------------------------------------------------------------- |
| 106 | +class RestCatalogLoadTableTest : public ::testing::Test { |
| 107 | + protected: |
| 108 | + void SetUp() override { |
| 109 | + mock_client_ = std::make_shared<MockHttpClient>(); |
| 110 | + file_io_ = std::make_shared<NoOpFileIO>(); |
| 111 | + |
| 112 | + ICEBERG_UNWRAP_OR_FAIL(paths_, |
| 113 | + ResourcePaths::Make("http://test-server", /*prefix=*/"", |
| 114 | + /*namespace_separator=*/"%1F")); |
| 115 | + |
| 116 | + identifier_ = TableIdentifier{.ns = Namespace{{"default"}}, .name = "my_table"}; |
| 117 | + |
| 118 | + all_plan_endpoints_ = {Endpoint::LoadTable(), Endpoint::PlanTableScan(), |
| 119 | + Endpoint::FetchPlanningResult(), Endpoint::CancelPlanning(), |
| 120 | + Endpoint::FetchScanTasks()}; |
| 121 | + |
| 122 | + no_plan_endpoint_set_ = {Endpoint::LoadTable()}; |
| 123 | + } |
| 124 | + |
| 125 | + // Builds a RestCatalog with the given supported endpoints and an optional |
| 126 | + // client-side scan-planning-mode setting. |
| 127 | + Result<std::shared_ptr<RestCatalog>> MakeCatalog( |
| 128 | + const std::unordered_set<Endpoint>& endpoints, |
| 129 | + const std::string& client_scan_mode = "") { |
| 130 | + std::unordered_map<std::string, std::string> props{ |
| 131 | + {"uri", "http://test-server"}, |
| 132 | + }; |
| 133 | + if (!client_scan_mode.empty()) { |
| 134 | + props["scan-planning-mode"] = client_scan_mode; |
| 135 | + } |
| 136 | + auto config = RestCatalogProperties::FromMap(props); |
| 137 | + return RestCatalog::MakeForTesting(std::move(config), file_io_, mock_client_, paths_, |
| 138 | + endpoints); |
| 139 | + } |
| 140 | + |
| 141 | + std::shared_ptr<MockHttpClient> mock_client_; |
| 142 | + std::shared_ptr<FileIO> file_io_; |
| 143 | + std::shared_ptr<ResourcePaths> paths_; |
| 144 | + TableIdentifier identifier_; |
| 145 | + std::unordered_set<Endpoint> all_plan_endpoints_; |
| 146 | + std::unordered_set<Endpoint> no_plan_endpoint_set_; |
| 147 | +}; |
| 148 | + |
| 149 | +// -------------------------------------------------------------------------- |
| 150 | +// Table config "scan-planning-mode":"server" → LoadTable returns a RestTable. |
| 151 | +// -------------------------------------------------------------------------- |
| 152 | +TEST_F(RestCatalogLoadTableTest, TableConfigServerScanReturnsRestTable) { |
| 153 | + EXPECT_CALL(*mock_client_, Get(_, _, _, _, _)) |
| 154 | + .WillOnce( |
| 155 | + Return(HttpResponse::MakeForTesting(200, kLoadTableServerScanResponse))); |
| 156 | + |
| 157 | + ICEBERG_UNWRAP_OR_FAIL(auto catalog, MakeCatalog(all_plan_endpoints_)); |
| 158 | + ICEBERG_UNWRAP_OR_FAIL(auto as_catalog, catalog->AsCatalog()); |
| 159 | + ICEBERG_UNWRAP_OR_FAIL(auto table, as_catalog->LoadTable(identifier_)); |
| 160 | + |
| 161 | + EXPECT_NE(dynamic_cast<RestTable*>(table.get()), nullptr) |
| 162 | + << "Expected RestTable when server config requests server-side scan planning"; |
| 163 | +} |
| 164 | + |
| 165 | +// -------------------------------------------------------------------------- |
| 166 | +// Client config "scan-planning-mode":"server", table config absent → |
| 167 | +// LoadTable returns a RestTable. |
| 168 | +// -------------------------------------------------------------------------- |
| 169 | +TEST_F(RestCatalogLoadTableTest, ClientConfigServerScanReturnsRestTable) { |
| 170 | + EXPECT_CALL(*mock_client_, Get(_, _, _, _, _)) |
| 171 | + .WillOnce(Return(HttpResponse::MakeForTesting(200, kLoadTableDefaultResponse))); |
| 172 | + |
| 173 | + ICEBERG_UNWRAP_OR_FAIL(auto catalog, |
| 174 | + MakeCatalog(all_plan_endpoints_, /*client_scan_mode=*/"server")); |
| 175 | + ICEBERG_UNWRAP_OR_FAIL(auto as_catalog, catalog->AsCatalog()); |
| 176 | + ICEBERG_UNWRAP_OR_FAIL(auto table, as_catalog->LoadTable(identifier_)); |
| 177 | + |
| 178 | + EXPECT_NE(dynamic_cast<RestTable*>(table.get()), nullptr) |
| 179 | + << "Expected RestTable when client config requests server-side scan planning"; |
| 180 | +} |
| 181 | + |
| 182 | +// -------------------------------------------------------------------------- |
| 183 | +// "scan-planning-mode":"server" but PlanTableScan endpoint missing → |
| 184 | +// LoadTable returns NotSupported. |
| 185 | +// -------------------------------------------------------------------------- |
| 186 | +TEST_F(RestCatalogLoadTableTest, ServerScanWithMissingEndpointReturnsNotSupported) { |
| 187 | + EXPECT_CALL(*mock_client_, Get(_, _, _, _, _)) |
| 188 | + .WillOnce( |
| 189 | + Return(HttpResponse::MakeForTesting(200, kLoadTableServerScanResponse))); |
| 190 | + |
| 191 | + ICEBERG_UNWRAP_OR_FAIL(auto catalog, MakeCatalog(no_plan_endpoint_set_)); |
| 192 | + ICEBERG_UNWRAP_OR_FAIL(auto as_catalog, catalog->AsCatalog()); |
| 193 | + auto result = as_catalog->LoadTable(identifier_); |
| 194 | + |
| 195 | + EXPECT_THAT(result, IsError(ErrorKind::kNotSupported)); |
| 196 | +} |
| 197 | + |
| 198 | +// -------------------------------------------------------------------------- |
| 199 | +// No scan-planning-mode set anywhere → LoadTable returns a plain Table |
| 200 | +// (not RestTable). |
| 201 | +// -------------------------------------------------------------------------- |
| 202 | +TEST_F(RestCatalogLoadTableTest, DefaultScanModeReturnsPlainTable) { |
| 203 | + EXPECT_CALL(*mock_client_, Get(_, _, _, _, _)) |
| 204 | + .WillOnce(Return(HttpResponse::MakeForTesting(200, kLoadTableDefaultResponse))); |
| 205 | + |
| 206 | + ICEBERG_UNWRAP_OR_FAIL(auto catalog, MakeCatalog(all_plan_endpoints_)); |
| 207 | + ICEBERG_UNWRAP_OR_FAIL(auto as_catalog, catalog->AsCatalog()); |
| 208 | + ICEBERG_UNWRAP_OR_FAIL(auto table, as_catalog->LoadTable(identifier_)); |
| 209 | + |
| 210 | + EXPECT_EQ(dynamic_cast<RestTable*>(table.get()), nullptr) |
| 211 | + << "Expected plain Table (not RestTable) when no scan-planning-mode is configured"; |
| 212 | +} |
| 213 | + |
| 214 | +} // namespace iceberg::rest |
0 commit comments