Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(bindings/C): support BDD tests using GTest #2254

Merged
merged 15 commits into from
May 11, 2023
63 changes: 63 additions & 0 deletions .github/workflows/bindings_c.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

name: Bindings C CI

on:
push:
branches:
- main
tags:
- '*'
pull_request:
branches:
- main
paths:
- "bindings/c/**"
- ".github/workflows/bindings_c.yml"
workflow_dispatch:

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}-${{ github.event_name }}
cancel-in-progress: true

permissions:
contents: read

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install gtest manually
run: |
sudo apt-get install libgtest-dev
cd /usr/src/gtest
sudo cmake CMakeLists.txt
sudo make
sudo cp lib/*.a /usr/lib
sudo ln -s /usr/lib/libgtest.a /usr/local/lib/libgtest.a
sudo ln -s /usr/lib/libgtest_main.a /usr/local/lib/libgtest_main.a
- name: Setup Rust toolchain
uses: ./.github/actions/setup
- name: Build C binding
working-directory: "bindings/c"
run: make build
- name: Build and Run BDD tests
working-directory: "bindings/c"
run: make test
xyjixyjixyji marked this conversation as resolved.
Show resolved Hide resolved

9 changes: 5 additions & 4 deletions bindings/c/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@
# under the License.

RPATH=$(PWD)/../../target/debug
CFLAGS=-I./include
CXXFLAGS=-I./include -std=c++14
LDFLAGS=-L$(RPATH) -Wl,-rpath,$(RPATH)
LIBS=-lopendal_c
LIBS=-lopendal_c -lgtest
OBJ_DIR=./build

.PHONY: all
all: build test

.PHONY: format
format:
find . -name '*.c' -exec clang-format -i --style=WebKit --verbose {} \;
find . -name '*.cpp' -exec clang-format -i --style=WebKit --verbose {} \;

.PHONY: build
build:
Expand All @@ -35,7 +35,8 @@ build:

.PHONY: test
test:
$(CC) tests/basicio.c -o $(OBJ_DIR)/basicio $(CFLAGS) $(LDFLAGS) $(LIBS)
$(CXX) tests/bdd.cpp -o $(OBJ_DIR)/bdd $(CXXFLAGS) $(LDFLAGS) $(LIBS)
$(OBJ_DIR)/bdd

.PHONY: clean
clean:
Expand Down
89 changes: 0 additions & 89 deletions bindings/c/tests/basicio.c

This file was deleted.

122 changes: 122 additions & 0 deletions bindings/c/tests/bdd.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

#include "gtest/gtest.h"

extern "C" {
#include "opendal.h"
}

class OpendalBddTest : public ::testing::Test {
protected:
// Setup code for the fixture
opendal_operator_ptr p;
std::string scheme;
std::string path;
std::string content;

// the fixture setup is an operator write, which will be
// run at the beginning of every tests
void SetUp() override
{
// construct the memory operator
this->scheme = std::string("memory");
this->path = std::string("test");
this->content = std::string("Hello, World!");

this->p = opendal_operator_new(scheme.c_str());

EXPECT_TRUE(this->p);

const opendal_bytes data = {
.data = (uint8_t*)this->content.c_str(),
.len = this->content.length(),
};

opendal_code code = opendal_operator_blocking_write(this->p, this->path.c_str(), data);

EXPECT_EQ(code, OPENDAL_OK);
}

// Teardown code for the fixture, free the operator
void TearDown() override
{
opendal_operator_free(&this->p);
}
};

// do nothing, the fixture does the Write Test
TEST_F(OpendalBddTest, Write)
{
}

// The path must exist
TEST_F(OpendalBddTest, Exist)
{
opendal_result_is_exist r = opendal_operator_is_exist(this->p, this->path.c_str());

EXPECT_EQ(r.code, OPENDAL_OK);
EXPECT_TRUE(r.is_exist);
}

// The entry mode must be file
TEST_F(OpendalBddTest, EntryMode)
{
opendal_result_stat r = opendal_operator_stat(this->p, this->path.c_str());
EXPECT_EQ(r.code, OPENDAL_OK);

opendal_metadata meta = r.meta;
EXPECT_TRUE(opendal_metadata_is_file(&meta));

opendal_metadata_free(&meta);
}

// The content length must be consistent
TEST_F(OpendalBddTest, ContentLength)
{
opendal_result_stat r = opendal_operator_stat(this->p, this->path.c_str());
EXPECT_EQ(r.code, OPENDAL_OK);

opendal_metadata meta = r.meta;
EXPECT_EQ(opendal_metadata_content_length(&meta), 13);

opendal_metadata_free(&meta);
}

// We must read the correct content
TEST_F(OpendalBddTest, Read)
{
struct opendal_result_read r = opendal_operator_blocking_read(this->p, this->path.c_str());

EXPECT_EQ(r.code, OPENDAL_OK);
EXPECT_EQ(r.data->len, this->content.length());

for (int i = 0; i < r.data->len; i++) {
EXPECT_EQ(this->content[i], (char)(r.data->data[i]));
}

// free the bytes's heap memory
opendal_bytes_free(r.data);
}

int main(int argc, char** argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}