Skip to content

Commit

Permalink
Add integration test framework (#580)
Browse files Browse the repository at this point in the history
* implement integration testing flow with quickstart
* use latest ziti-builder
* use ziti cli full path
* set ZITI_CONFIG_DIR
* check for quickstart readiness
  • Loading branch information
ekoby authored Nov 30, 2023
1 parent 318cc5c commit 99394ab
Show file tree
Hide file tree
Showing 8 changed files with 225 additions and 7 deletions.
9 changes: 6 additions & 3 deletions .github/actions/build/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ runs:
run: echo "target = ${{ inputs.target }}"
shell: bash

- uses: actions/setup-go@v4
with:
go-version: '1.20.x'

- uses: lukka/[email protected]

- uses: lukka/run-vcpkg@v10
Expand Down Expand Up @@ -55,9 +59,8 @@ runs:
TEST_ID: ${{ inputs.test_id }}
ZITI_TEST_IDENTITY: ${{ runner.workspace }}/test_id.json
ZITI_LOG: "4"
if: inputs.test == 'true' && inputs.test_id != ''
if: inputs.test == 'true'
working-directory: ./build
shell: bash
run: |
echo "${TEST_ID}" > "${ZITI_TEST_IDENTITY}"
ctest -VV --no-compress-output -R zitilib_tests
ctest -VV --no-compress-output -C ${{ inputs.config }} -R quickstart-test
6 changes: 3 additions & 3 deletions .github/workflows/cmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ jobs:
fail-fast: false
matrix:
spec:
- { name: 'Linux x86_64', runner: 'ubuntu-20.04', target: 'linux-x64', builder: 'openziti/ziti-builder:1.0.7', test: 'true' }
- { name: 'Linux ARM', runner: 'ubuntu-20.04', target: 'linux-arm', builder: 'openziti/ziti-builder:1.0.7' }
- { name: 'Linux ARM64', runner: 'ubuntu-20.04', target: 'linux-arm64', builder: 'openziti/ziti-builder:1.0.7' }
- { name: 'Linux x86_64', runner: 'ubuntu-20.04', target: 'linux-x64', builder: 'openziti/ziti-builder:1.0.11', test: 'true' }
- { name: 'Linux ARM', runner: 'ubuntu-20.04', target: 'linux-arm', builder: 'openziti/ziti-builder:1.0.11' }
- { name: 'Linux ARM64', runner: 'ubuntu-20.04', target: 'linux-arm64', builder: 'openziti/ziti-builder:1.0.11' }
- { name: 'MacOS x86_64', runner: 'macOS-11', target: 'macOS-x64', test: 'true' }
- { name: 'MacOS arm64', runner: 'macOS-11', target: 'macOS-arm64' }
- { name: 'Windows x86_64', runner: 'windows-2022', target: 'windows-x64', test: 'true' }
Expand Down
2 changes: 1 addition & 1 deletion programs/sample_enroll/sample_enroll.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ int main(int argc, char **argv) {
FILE *id_file = fopen(argv[2], "w");
if (id_file) {
fprintf(id_file, "%.*s", (int) len, cfg);
printf("ziti identity is save in %s\n", argv[1]);
printf("ziti identity is saved in %s\n", argv[2]);
}
else {
printf("err = %d(%s)\n", errno, strerror(errno));
Expand Down
2 changes: 2 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,5 @@ add_test(quick_tests all_tests -d yes "~[integ]~[metrics]")

add_test(zitilib_tests zitilib-tests -d yes)

add_subdirectory(integ)

61 changes: 61 additions & 0 deletions tests/integ/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@

find_program(EXPECTOR NAMES expect)
if (NOT EXPECTOR)
message(WARNING "expect not found: integration testing is not enabled")
return()
endif ()

execute_process(COMMAND ${EXPECTOR} -v)

find_program(GOLANG_EXE NAMES go REQUIRED)

add_executable(integ-tests
main.cpp)
target_link_libraries(integ-tests
PRIVATE ziti
PRIVATE Catch2::Catch2WithMain
)

if (WIN32)
set_property(TARGET integ-tests PROPERTY CXX_STANDARD 20)
else ()
set_property(TARGET integ-tests PROPERTY CXX_STANDARD 14)
endif ()

set(ZITI_CLI_VER "v0.31.0" CACHE STRING "ziti version for integration tests")
add_custom_target(ziti-cli ALL
COMMAND ${CMAKE_COMMAND} -E env GOBIN=${CMAKE_CURRENT_BINARY_DIR}
${GOLANG_EXE} install github.com/openziti/ziti/ziti@${ZITI_CLI_VER}
BYPRODUCTS ${CMAKE_CURRENT_BINARY_DIR}/ziti
)

set(ZITI_CLI ${CMAKE_CURRENT_BINARY_DIR}/ziti)

set(qs_home ${CMAKE_CURRENT_BINARY_DIR}/qs-home)
add_test(NAME quickstart-bootstrap
COMMAND ${EXPECTOR} ${CMAKE_CURRENT_SOURCE_DIR}/bootstrap.exp
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
COMMAND_EXPAND_LISTS
)
set_tests_properties(quickstart-bootstrap PROPERTIES
FIXTURES_SETUP QS
ENVIRONMENT "ZITI_CLI=${ZITI_CLI};ZITI_CONFIG_DIR=${qs_home};QUICKSTART_HOME=${qs_home};ENROLLER=$<TARGET_FILE:sample_enroll>"
DEPENDS "sample_enroll;ziti-cli"
)

add_test(NAME quickstart-test
COMMAND ${EXPECTOR} ${CMAKE_CURRENT_SOURCE_DIR}/integ-test.exp
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
)
set_tests_properties(quickstart-test PROPERTIES
FIXTURES_REQUIRED QS
DEPENDS "integ-tests"
ENVIRONMENT "ZITI_CLI=${ZITI_CLI};ZITI_CONFIG_DIR=${qs_home};QUICKSTART_HOME=${qs_home};TEST_EXE=$<TARGET_FILE:integ-tests>"
)

add_test(NAME quickstart-cleanup
COMMAND rm -rf ${qs_home}
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
)
set_tests_properties(quickstart-cleanup PROPERTIES FIXTURES_CLEANUP QS)

89 changes: 89 additions & 0 deletions tests/integ/bootstrap.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#!expect

set timeout 20
proc abort errs {
puts "test failed: $errs"
exit 2
}
set ziti $env(ZITI_CLI)
puts "ziti = $ziti"

set enroller $env(ENROLLER)
puts "enroller = $env(ENROLLER)"

puts "Starting Ziti Quickstart"
puts "========================"

spawn $ziti edge quickstart --home $env(QUICKSTART_HOME)
set qs_id @spawn_id
# wait until ER is connected
expect {
"finished sychronizing api sessions" { }
timeout { error "qs not started in time" }
eof { error "qs not started" }
}

puts "Configure model"
puts "==============="
log_user 1

expect_before {
timeout { abort "timeout" }
"connection refused" { abort "QS not started" }
}

spawn $ziti edge create identity test-client -a client -o test-client.jwt
expect {
"New identity test-client created" {}
"error" { abort $expect_out(buffer) }
eof { abort "unexpected" }
}

spawn $ziti edge create identity test-server -a server -o test-server.jwt
expect {
"New identity test-server created" {}
"error" { error $expect_out(buffer) }
}

spawn $ziti edge create service test-service
expect {
"New service test-service created" { }
-re "error(.*)" { error $expect_out(1,string) }
eof { error "not cool" }
}

spawn $ziti edge create service-policy servers-bind Bind --identity-roles \#server --service-roles \#all
expect {
"New service policy servers-bind created" {}
-re "error(.*)" { error $expect_out(1,string) }
eof { error "not cool" }
}

spawn $ziti edge create service-policy clients-dial Dial --identity-roles \#client --service-roles \#all
expect {
"New service policy clients-dial created" {}
-re "error(.*)" { error $expect_out(1,string) }
eof { error "not cool" }
}

spawn $enroller ./test-server.jwt ./test-server.json
expect {
"ziti identity is saved in ./test-server.json" {}
eof { error "test-server not enrolled" }
}

spawn $enroller ./test-client.jwt ./test-client.json
expect {
"ziti identity is saved in ./test-client.json" {}
eof { error "test-client not enrolled" }
}










23 changes: 23 additions & 0 deletions tests/integ/integ-test.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!expect

set ziti $env(ZITI_CLI)
puts "ziti = $ziti"

set tester $env(TEST_EXE)
puts "test driver = $tester"

puts "Starting Ziti Quickstart"
puts "========================"
spawn $ziti edge quickstart --already-initialized --home $env(QUICKSTART_HOME)
set qs_id $spawn_id

expect {
"finished sychronizing api sessions" { }
timeout { error "qs not started in time" }
eof { error "qs not started" }
}

spawn $tester
expect {
-re "test cases: (.*) failed" { error "$expect_out(1,string)" }
}
40 changes: 40 additions & 0 deletions tests/integ/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// Copyright NetFoundry Inc.
//
// Licensed 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
//
// https://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 <catch2/catch_all.hpp>
#include <ziti/zitilib.h>
#include <ziti/ziti.h>

class ZitilibManager: public Catch::EventListenerBase{

public:
using Catch::EventListenerBase::EventListenerBase;

void testRunStarting(Catch::TestRunInfo const &) override {
Ziti_lib_init();
}

void testRunEnded(const Catch::TestRunStats &testRunStats) override {
Ziti_lib_shutdown();
}
};

CATCH_REGISTER_LISTENER(ZitilibManager)

TEST_CASE("version", "[integ]") {
const ziti_version *version = ziti_get_version();
REQUIRE(version != nullptr);
}

0 comments on commit 99394ab

Please sign in to comment.