|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +//! Integration tests for FileIO Google Cloud Storage (GCS). |
| 19 | +
|
| 20 | +use std::collections::HashMap; |
| 21 | +use std::net::SocketAddr; |
| 22 | +use std::sync::RwLock; |
| 23 | + |
| 24 | +use bytes::Bytes; |
| 25 | +use ctor::{ctor, dtor}; |
| 26 | +use iceberg::io::{FileIO, FileIOBuilder, GCS_NO_AUTH, GCS_SERVICE_PATH}; |
| 27 | +use iceberg_test_utils::docker::DockerCompose; |
| 28 | +use iceberg_test_utils::{normalize_test_name, set_up}; |
| 29 | + |
| 30 | +static DOCKER_COMPOSE_ENV: RwLock<Option<DockerCompose>> = RwLock::new(None); |
| 31 | +static FAKE_GCS_PORT: u16 = 4443; |
| 32 | +static FAKE_GCS_BUCKET: &str = "test-bucket"; |
| 33 | + |
| 34 | +#[ctor] |
| 35 | +fn before_all() { |
| 36 | + let mut guard = DOCKER_COMPOSE_ENV.write().unwrap(); |
| 37 | + let docker_compose = DockerCompose::new( |
| 38 | + normalize_test_name(module_path!()), |
| 39 | + format!("{}/testdata/file_io_gcs", env!("CARGO_MANIFEST_DIR")), |
| 40 | + ); |
| 41 | + docker_compose.run(); |
| 42 | + guard.replace(docker_compose); |
| 43 | +} |
| 44 | + |
| 45 | +#[dtor] |
| 46 | +fn after_all() { |
| 47 | + let mut guard = DOCKER_COMPOSE_ENV.write().unwrap(); |
| 48 | + guard.take(); |
| 49 | +} |
| 50 | + |
| 51 | +async fn get_file_io_gcs() -> FileIO { |
| 52 | + set_up(); |
| 53 | + |
| 54 | + let ip = DOCKER_COMPOSE_ENV |
| 55 | + .read() |
| 56 | + .unwrap() |
| 57 | + .as_ref() |
| 58 | + .unwrap() |
| 59 | + .get_container_ip("gcs-server"); |
| 60 | + let addr = SocketAddr::new(ip, FAKE_GCS_PORT); |
| 61 | + |
| 62 | + // A bucket must exist for FileIO |
| 63 | + create_bucket(FAKE_GCS_BUCKET, addr.to_string()) |
| 64 | + .await |
| 65 | + .unwrap(); |
| 66 | + |
| 67 | + FileIOBuilder::new("gcs") |
| 68 | + .with_props(vec![ |
| 69 | + (GCS_SERVICE_PATH, format!("http://{}", addr)), |
| 70 | + (GCS_NO_AUTH, "true".to_string()), |
| 71 | + ]) |
| 72 | + .build() |
| 73 | + .unwrap() |
| 74 | +} |
| 75 | + |
| 76 | +// Create a bucket against the emulated GCS storage server. |
| 77 | +async fn create_bucket(name: &str, server_addr: String) -> anyhow::Result<()> { |
| 78 | + let mut bucket_data = HashMap::new(); |
| 79 | + bucket_data.insert("name", name); |
| 80 | + |
| 81 | + let client = reqwest::Client::new(); |
| 82 | + let endpoint = format!("http://{}/storage/v1/b", server_addr); |
| 83 | + client.post(endpoint).json(&bucket_data).send().await?; |
| 84 | + Ok(()) |
| 85 | +} |
| 86 | + |
| 87 | +fn get_gs_path() -> String { |
| 88 | + format!("gs://{}", FAKE_GCS_BUCKET) |
| 89 | +} |
| 90 | + |
| 91 | +#[tokio::test] |
| 92 | +async fn gcs_exists() { |
| 93 | + let file_io = get_file_io_gcs().await; |
| 94 | + assert!(file_io |
| 95 | + .is_exist(format!("{}/", get_gs_path())) |
| 96 | + .await |
| 97 | + .unwrap()); |
| 98 | +} |
| 99 | + |
| 100 | +#[tokio::test] |
| 101 | +async fn gcs_write() { |
| 102 | + let gs_file = format!("{}/write-file", get_gs_path()); |
| 103 | + let file_io = get_file_io_gcs().await; |
| 104 | + let output = file_io.new_output(&gs_file).unwrap(); |
| 105 | + output |
| 106 | + .write(bytes::Bytes::from_static(b"iceberg-gcs!")) |
| 107 | + .await |
| 108 | + .expect("Write to test output file"); |
| 109 | + assert!(file_io.is_exist(gs_file).await.unwrap()) |
| 110 | +} |
| 111 | + |
| 112 | +#[tokio::test] |
| 113 | +async fn gcs_read() { |
| 114 | + let gs_file = format!("{}/read-gcs", get_gs_path()); |
| 115 | + let file_io = get_file_io_gcs().await; |
| 116 | + let output = file_io.new_output(&gs_file).unwrap(); |
| 117 | + output |
| 118 | + .write(bytes::Bytes::from_static(b"iceberg!")) |
| 119 | + .await |
| 120 | + .expect("Write to test output file"); |
| 121 | + assert!(file_io.is_exist(&gs_file).await.unwrap()); |
| 122 | + |
| 123 | + let input = file_io.new_input(gs_file).unwrap(); |
| 124 | + assert_eq!(input.read().await.unwrap(), Bytes::from_static(b"iceberg!")); |
| 125 | +} |
0 commit comments