Skip to content
Permalink
Browse files
Add test_package for the framework, http_admin and log_admin.
  • Loading branch information
PengZheng committed Mar 16, 2022
1 parent 3361fe3 commit 66724b94902b5cc6131ab5c50793d993ab854438
Showing 6 changed files with 367 additions and 0 deletions.
@@ -0,0 +1,67 @@
# 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.

cmake_minimum_required (VERSION 3.14)
project(test_package)
include(${CMAKE_BINARY_DIR}/conan_paths.cmake)

set(CMAKE_CXX_STANDARD 17)

find_package(Celix REQUIRED)

add_celix_bundle(hello VERSION 1.0.0 SOURCES hello_bundle.c)
# log_helper is always available
target_link_libraries(hello PRIVATE Celix::log_helper)
celix_get_bundle_file(hello HELLO_TEST_BUNDLE)
add_executable(framework test_framework.c)
target_link_libraries(framework Celix::framework)
add_celix_bundle_dependencies(framework hello)
target_compile_definitions(framework PRIVATE HELLO_TEST_BUNDLE_LOCATION="${HELLO_TEST_BUNDLE}")

option(TEST_HTTP_ADMIN "Test http_admin" ON)
if (TEST_HTTP_ADMIN)
# make sure http_admin_api is accessible
add_celix_bundle(http_admin_sut
SOURCES
test_http_admin_activator.c
VERSION 1.0.0
)
target_link_libraries(http_admin_sut PRIVATE Celix::http_admin_api)
# make sure http_admin bundle is accessible
add_celix_container(use_http_admin COPY
BUNDLES
Celix::http_admin
http_admin_sut
)
endif ()

option(TEST_LOG_SERVICE "Test log_admin" ON)
if (TEST_LOG_SERVICE)
add_celix_bundle(my_log_writer
SOURCES
my_log_writer_activator.c
VERSION 1.0.0
)
# make sure log_service_api is accessible
target_link_libraries(my_log_writer PRIVATE Celix::log_helper Celix::log_service_api)
add_celix_container(use_log_writer COPY)
# make sure log_admin is accessible
celix_container_bundles(use_log_writer LEVEL 0 Celix::log_admin)
# make sure my_log_writer is available to hello
celix_container_bundles(use_log_writer LEVEL 1 my_log_writer)
celix_container_bundles(use_log_writer LEVEL 2 hello)
endif ()
@@ -0,0 +1,43 @@
# 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.

from conans import CMake, ConanFile, tools
import os


class TestPackageConan(ConanFile):
settings = "os", "arch", "compiler", "build_type"
generators = "cmake_paths"

def requirements(self):
if not self.requires:
self.requires("celix/2.2.3@zhengpeng/testing")

def build(self):
cmake = CMake(self)
cmake.configure()
cmake.definitions["TEST_HTTP_ADMIN"] = self.options["celix"].build_http_admin
cmake.definitions["TEST_LOG_SERVICE"] = self.options["celix"].build_log_service
cmake.build()

def test(self):
if not tools.cross_building(self, skip_x64_x86=True):
self.run("./framework", run_environment=True)
if self.options["celix"].build_http_admin:
self.run("./use_http_admin", cwd=os.path.join("deploy", "use_http_admin"), run_environment=True)
if self.options["celix"].build_log_service:
self.run("./use_log_writer", cwd=os.path.join("deploy", "use_log_writer"), run_environment=True)
@@ -0,0 +1,52 @@
/*
* 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 <celix_api.h>
#include <celix_log_helper.h>
#include <stdio.h>

typedef struct activator_data {
celix_log_helper_t *logger;
} activator_data_t;


static celix_status_t activator_start(activator_data_t *data, celix_bundle_context_t *ctx) {
const char *key = NULL;
data->logger = celix_logHelper_create(ctx, "test");
printf("Hello world from C bundle with id %li\n", celix_bundle_getId(celix_bundleContext_getBundle(ctx)));
celix_properties_t *prop = celix_properties_create();
celix_properties_set(prop, "name", "FOO");
celix_properties_setLong(prop, "age", 39);
celix_properties_setBool(prop, "married", true);
celix_properties_setDouble(prop, "height", 1.75);
CELIX_PROPERTIES_FOR_EACH(prop, key) {
celix_logHelper_info(data->logger,"[Hello Bundle] |- %s=%s\n", key, celix_properties_get(prop, key, NULL));
}
celix_properties_store(prop, "prop.txt", NULL);
celix_properties_destroy(prop);
celix_framework_stopBundleAsync(celix_bundleContext_getFramework(ctx), CELIX_FRAMEWORK_BUNDLE_ID); // make to container quit immediately
return CELIX_SUCCESS;
}

static celix_status_t activator_stop(activator_data_t *data, celix_bundle_context_t *ctx) {
celix_logHelper_info(data->logger, "Goodbye world from C bundle with id %li\n", celix_bundle_getId(celix_bundleContext_getBundle(ctx)));
celix_logHelper_destroy(data->logger);
return CELIX_SUCCESS;
}

CELIX_GEN_BUNDLE_ACTIVATOR(activator_data_t, activator_start, activator_stop)
@@ -0,0 +1,71 @@
/*
* 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 "celix_api.h"
#include "celix_log_sink.h"
#define _GNU_SOURCE
#include <stdio.h>

typedef struct my_log_writer_activator {
celix_log_sink_t logSinkSvc;
long logSinkSvcId;
} my_log_writer_activator_t;

static void myLogWriter_sinkLog(void *handle __attribute__((unused)), celix_log_level_e level, long logServiceId __attribute__((unused)), const char* logServiceName, const char* file, const char* function, int line, const char *format, va_list formatArgs) {

//note details are note used
(void)file;
(void)function;
(void)line;
(void)level;

char buffer[1024];
size_t needed = vsnprintf(buffer, 1024, format, formatArgs);
if (needed > 1024) {
char *allocatedBuffer = NULL;
vasprintf(&allocatedBuffer, format, formatArgs);
printf("my [%s]: %s", logServiceName, allocatedBuffer);
free(allocatedBuffer);
} else {
printf("my [%s]: %s", logServiceName, buffer);
}
}

static celix_status_t myLogWriterActivator_start(my_log_writer_activator_t* act, celix_bundle_context_t* ctx) {
act->logSinkSvc.handle = NULL;
act->logSinkSvc.sinkLog = myLogWriter_sinkLog;

celix_service_registration_options_t opts = CELIX_EMPTY_SERVICE_REGISTRATION_OPTIONS;
celix_properties_t* props = celix_properties_create();
celix_properties_set(props, CELIX_LOG_SINK_PROPERTY_NAME, "my_log_writer");
opts.serviceName = CELIX_LOG_SINK_NAME;
opts.serviceVersion = CELIX_LOG_SINK_VERSION;
opts.properties = props;
opts.svc = &act->logSinkSvc;
act->logSinkSvcId = celix_bundleContext_registerServiceWithOptions(ctx, &opts);

return CELIX_SUCCESS;
}

static celix_status_t myLogWriterActivator_stop(my_log_writer_activator_t* act, celix_bundle_context_t* ctx) {
celix_bundleContext_unregisterService(ctx, act->logSinkSvcId);
return CELIX_SUCCESS;
}

CELIX_GEN_BUNDLE_ACTIVATOR(my_log_writer_activator_t, myLogWriterActivator_start, myLogWriterActivator_stop);
@@ -0,0 +1,39 @@
/*
* 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 <celix_api.h>
#include <assert.h>
#include <stddef.h>

int main() {
celix_framework_t* fw = NULL;
celix_bundle_context_t *ctx = NULL;
celix_properties_t *properties = NULL;

properties = properties_create();
properties_set(properties, "LOGHELPER_ENABLE_STDOUT_FALLBACK", "true");
properties_set(properties, "org.osgi.framework.storage.clean", "onFirstInit");
properties_set(properties, "org.osgi.framework.storage", ".cacheBundleContextTestFramework");

fw = celix_frameworkFactory_createFramework(properties);
ctx = framework_getContext(fw);
long bndId = celix_bundleContext_installBundle(ctx, HELLO_TEST_BUNDLE_LOCATION, true);
assert(bndId >= 0);
celix_frameworkFactory_destroyFramework(fw);
return 0;
}
@@ -0,0 +1,95 @@
/*
* 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 <stdlib.h>

#include "celix_api.h"
#include "http_admin/api.h"

#include "civetweb.h"

struct activator {
celix_http_service_t httpSvc;
celix_http_service_t httpSvc2;
celix_http_service_t httpSvc3;
long httpSvcId;
long httpSvcId2;
long httpSvcId3;

celix_websocket_service_t sockSvc;
long sockSvcId;
};

//Local function prototypes
int alias_test_put(void *handle, struct mg_connection *connection, const char *path, const char *data, size_t length);
int websocket_data_echo(struct mg_connection *connection, int op_code, char *data, size_t length, void *handle);

celix_status_t bnd_start(struct activator *act, celix_bundle_context_t *ctx) {
celix_properties_t *props = celix_properties_create();
celix_properties_set(props, HTTP_ADMIN_URI, "/alias");
act->httpSvc.handle = act;
act->httpSvc.doPut = alias_test_put;
act->httpSvcId = celix_bundleContext_registerService(ctx, &act->httpSvc, HTTP_ADMIN_SERVICE_NAME, props);

celix_properties_t *props2 = celix_properties_create();
celix_properties_set(props2, HTTP_ADMIN_URI, "/foo/bar");
act->httpSvc2.handle = act;
act->httpSvcId2 = celix_bundleContext_registerService(ctx, &act->httpSvc2, HTTP_ADMIN_SERVICE_NAME, props2);

celix_properties_t *props3 = celix_properties_create();
celix_properties_set(props3, HTTP_ADMIN_URI, "/");
act->httpSvc3.handle = act;
act->httpSvcId3 = celix_bundleContext_registerService(ctx, &act->httpSvc3, HTTP_ADMIN_SERVICE_NAME, props3);

celix_properties_t *props4 = celix_properties_create();
celix_properties_set(props4, WEBSOCKET_ADMIN_URI, "/");
act->sockSvc.handle = act;
act->sockSvc.data = websocket_data_echo;
act->sockSvcId = celix_bundleContext_registerService(ctx, &act->sockSvc, WEBSOCKET_ADMIN_SERVICE_NAME, props4);
celix_framework_stopBundleAsync(celix_bundleContext_getFramework(ctx), CELIX_FRAMEWORK_BUNDLE_ID); // make to container quit immediately
return CELIX_SUCCESS;
}

celix_status_t bnd_stop(struct activator *act, celix_bundle_context_t *ctx) {
celix_bundleContext_unregisterService(ctx, act->httpSvcId);
celix_bundleContext_unregisterService(ctx, act->httpSvcId2);
celix_bundleContext_unregisterService(ctx, act->httpSvcId3);
celix_bundleContext_unregisterService(ctx, act->sockSvcId);

return CELIX_SUCCESS;
}

CELIX_GEN_BUNDLE_ACTIVATOR(struct activator, bnd_start, bnd_stop);

int alias_test_put(void *handle __attribute__((unused)), struct mg_connection *connection, const char *path __attribute__((unused)), const char *data, size_t length) {
//If data received, echo the data for the test case
if(length > 0 && data != NULL) {
const char *mime_type = mg_get_header(connection, "Content-Type");
mg_printf(connection,
"HTTP/1.1 200 OK\r\nContent-Type: %s\r\nConnection: close\r\n\r\n%s", mime_type, data);

}

return 200;
}

int websocket_data_echo(struct mg_connection *connection, int op_code __attribute__((unused)), char *data, size_t length, void *handle __attribute__((unused))) {
mg_websocket_write(connection, MG_WEBSOCKET_OPCODE_PONG, data, length);

return 0; //Close socket after echoing.
}

0 comments on commit 66724b9

Please sign in to comment.