add: tests

This commit is contained in:
Tobias Reisinger 2020-05-10 22:37:55 +02:00
parent dd98af4edc
commit be045f59d4
11 changed files with 257 additions and 30 deletions

3
.gitignore vendored
View file

@ -1,4 +1,7 @@
build/ build/
docs/ docs/
tests/testing_tmp/
tests/testing_bak/
include/migrations/*.sql.h include/migrations/*.sql.h

View file

@ -23,11 +23,11 @@ aux_source_directory(. SRC_DIR)
aux_source_directory(models MODELS_SRC) aux_source_directory(models MODELS_SRC)
aux_source_directory(helpers HELPERS_SRC) aux_source_directory(helpers HELPERS_SRC)
aux_source_directory(handlers HANDLERS_SRC) aux_source_directory(handlers HANDLERS_SRC)
aux_source_directory(endpoints ENDPOINTSS_SRC) aux_source_directory(endpoints ENDPOINTS_SRC)
configure_file("core.ini" "core.ini" COPYONLY) configure_file("core.ini" "core.ini" COPYONLY)
target_sources(core PRIVATE ${VENDOR_SRC} ${SRC_DIR} ${HANDLERS_SRC} ${HELPERS_SRC} ${MODELS_SRC} ${ENDPOINTSS_SRC}) target_sources(core PRIVATE ${VENDOR_SRC} ${SRC_DIR} ${HANDLERS_SRC} ${HELPERS_SRC} ${MODELS_SRC} ${ENDPOINTS_SRC})
target_include_directories(core PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include) target_include_directories(core PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include)
target_include_directories(core PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/vendor) target_include_directories(core PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/vendor)
@ -57,7 +57,7 @@ add_custom_target(docs
) )
add_custom_target(test add_custom_target(test
COMMAND ./run_tests.sh ${CMAKE_BINARY_DIR}/core ${CMAKE_SOURCE_DIR}/core.ini COMMAND ./run_tests.sh ${CMAKE_BINARY_DIR}/core "dev"
DEPENDS core DEPENDS core
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/tests WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/tests
) )

View file

@ -243,6 +243,11 @@ api_v1_controllers_discover_POST(struct mg_connection *c, endpoint_args_t *args,
discovered_controller->port = discovered_command_port; discovered_controller->port = discovered_command_port;
discovered_controller->active = 1; discovered_controller->active = 1;
// TODO get relays during discovery
relay_t **discovered_relays = malloc(sizeof(relay_t*));
discovered_relays[0] = NULL;
discovered_controller->relays = discovered_relays;
controller_save(discovered_controller); controller_save(discovered_controller);
controller_free(discovered_controller); controller_free(discovered_controller);
} }

View file

@ -0,0 +1,56 @@
[controller]
name = new emgauwa device
discovery-port = 4422
relay-count = 10
database = controller_db.lmdb
log-level = debug
[relay-0]
driver = piface
pin = 0
inverted = 0
[relay-1]
driver = piface
pin = 1
inverted = 0
[relay-2]
driver = gpio
pin = 5
inverted = 1
[relay-3]
driver = gpio
pin = 4
inverted = 1
[relay-4]
driver = gpio
pin = 3
inverted = 1
[relay-5]
driver = gpio
pin = 2
inverted = 1
[relay-6]
driver = gpio
pin = 1
inverted = 1
[relay-7]
driver = gpio
pin = 0
inverted = 1
[relay-8]
driver = gpio
pin = 16
inverted = 1
[relay-9]
driver = gpio
pin = 15
inverted = 1

5
tests/core.testing.ini Normal file
View file

@ -0,0 +1,5 @@
[core]
server-port = 5000
discovery-port = 4422
database = core.sqlite
log-level = debug

View file

@ -1,17 +1,46 @@
#!/usr/bin/env sh #!/usr/bin/env sh
mkdir ./testing_tmp source_dir=$PWD
cd ./testing_tmp working_dir=$PWD/testing_tmp
working_bak=$PWD/testing_bak
cp $1 ./core rm -rf $working_dir
cp $2 ./core.ini mkdir -p $working_dir
cd $working_dir
./core start >/dev/null 2>&1 & git clone --quiet ssh://git@git.serguzim.me:3022/emgauwa/controller.git controller || exit
pushd ./controller > /dev/null
git checkout $2
mkdir build
cd build
cmake -DWIRING_PI_DEBUG=on .. >/dev/null
cp $source_dir/controller.testing.ini ./controller.ini
make >/dev/null
./controller start >$working_dir/controller.log 2>&1 &
controller_id=$!
popd > /dev/null
cp $1 $working_dir/core
cp $source_dir/core.testing.ini $working_dir/core.ini
$working_dir/core start >$working_dir/core.log 2>&1 &
core_id=$! core_id=$!
sleep 2; sleep 2;
tavern-ci --tavern-beta-new-traceback .. tavern-ci --disable-warnings $source_dir
test_result=$?
kill $core_id kill $core_id
cd .. kill $controller_id
rm -r ./testing_tmp
if [ $test_result -gt 0 ]
then
rm -rf $working_bak
mv $working_dir $working_bak
fi
rm -rf $working_dir
exit $test_result

View file

@ -0,0 +1,105 @@
test_name: Test basic controller functions
stages:
- name: "[test_controllers_basic] discover controllers"
request:
method: POST
url: "http://localhost:5000/api/v1/controllers/discover/"
response:
status_code: 200
json:
- id: !anystr
name: !anystr
relay_count: !anyint
relays: !anystr
active: !anybool
port: !anyint
ip: !anystr
relays: !anylist
save:
json:
returned_name: "[0].name"
returned_id: "[0].id"
- name: "[test_controllers_basic] get controller, check name"
request:
method: GET
url: "http://localhost:5000/api/v1/controllers/{returned_id}"
response:
status_code: 200
json:
name: "{returned_name}"
id: "{returned_id}"
relay_count: !anyint
relays: !anystr
active: !anybool
port: !anyint
ip: !anystr
relays: !anylist
- name: "[test_controllers_basic] get controller, check name"
request:
method: PUT
url: "http://localhost:5000/api/v1/controllers/{returned_id}"
json:
name: "renamed_controller"
response:
status_code: 200
json:
name: "{tavern.request_vars.json.name}"
id: "{returned_id}"
relay_count: !anyint
relays: !anystr
active: true
port: !anyint
ip: !anystr
relays: !anylist
save:
json:
changed_name: "name"
- name: "[test_controllers_basic] delete controller"
request:
method: DELETE
url: "http://localhost:5000/api/v1/controllers/{returned_id}"
response:
status_code: 200
- name: "[test_controllers_basic] get controller, expect 404"
request:
method: GET
url: "http://localhost:5000/api/v1/controllers/{returned_id}"
response:
status_code: 404
- name: "[test_controllers_basic] discover controllers again"
request:
method: POST
url: "http://localhost:5000/api/v1/controllers/discover/"
response:
status_code: 200
json:
- id: "{returned_id}"
name: "{changed_name}"
relay_count: !anyint
relays: !anystr
active: true
port: !anyint
ip: !anystr
relays: !anylist
- name: "[test_controllers_basic] get controller again, check name"
request:
method: GET
url: "http://localhost:5000/api/v1/controllers/{returned_id}"
response:
status_code: 200
json:
id: "{returned_id}"
name: "{changed_name}"
relay_count: !anyint
relays: !anystr
active: true
port: !anyint
ip: !anystr
relays: !anylist

View file

@ -1,21 +1,21 @@
test_name: Test basic get all requests test_name: "[get_all] Test basic get all requests"
stages: stages:
- name: "[test_get_all] get all schedules" - name: "[get_all] get all schedules"
request: request:
url: "http://localhost:5000/api/v1/schedules/" url: "http://localhost:5000/api/v1/schedules/"
method: GET method: GET
response: response:
status_code: 200 status_code: 200
- name: "[test_get_all] get all relays" - name: "[get_all] get all relays"
request: request:
url: "http://localhost:5000/api/v1/relays/" url: "http://localhost:5000/api/v1/relays/"
method: GET method: GET
response: response:
status_code: 200 status_code: 200
- name: "[test_get_all] get all controllers" - name: "[get_all] get all controllers"
request: request:
url: "http://localhost:5000/api/v1/controllers/" url: "http://localhost:5000/api/v1/controllers/"
method: GET method: GET

View file

@ -22,26 +22,34 @@ stages:
end: "01:00" end: "01:00"
response: response:
status_code: 201 status_code: 201
body: json:
name: "{tavern.request_vars.json.name}" name: "{tavern.request_vars.json.name}"
id: !anystr
periods: !anylist
tags: !anylist
save: save:
body: json:
returned_name: name returned_name: "name"
returned_id: id returned_id: "id"
- name: "[test_schedules_basic] get schedule, check name and some periods" - name: "[test_schedules_basic] get schedule, check name and some periods"
request: request:
method: GET method: GET
url: "http://localhost:5000/api/v1/schedules/{returned_id}" url: "http://localhost:5000/api/v1/schedules/{returned_id}"
response: response:
status_code: 200 status_code: 200
body: json:
name: "{returned_name}" name: "{returned_name}"
id: !anystr
periods: !anylist
tags: !anylist
- name: "[test_schedules_basic] delete schedule" - name: "[test_schedules_basic] delete schedule"
request: request:
method: DELETE method: DELETE
url: "http://localhost:5000/api/v1/schedules/{returned_id}" url: "http://localhost:5000/api/v1/schedules/{returned_id}"
response: response:
status_code: 200 status_code: 200
- name: "[test_schedules_basic] get deleted schedule, expect 404" - name: "[test_schedules_basic] get deleted schedule, expect 404"
request: request:
method: GET method: GET

View file

@ -7,15 +7,19 @@ stages:
url: "http://localhost:5000/api/v1/schedules/off" url: "http://localhost:5000/api/v1/schedules/off"
response: response:
status_code: 403 status_code: 403
- name: "[test_schedules_protected] get protected off schedule" - name: "[test_schedules_protected] get protected off schedule"
request: request:
method: GET method: GET
url: "http://localhost:5000/api/v1/schedules/off" url: "http://localhost:5000/api/v1/schedules/off"
response: response:
status_code: 200 status_code: 200
body: json:
id: "off"
name: "off" name: "off"
periods: [] periods: []
tags: !anylist
- name: "[test_schedules_protected] overwrite protected off schedule" - name: "[test_schedules_protected] overwrite protected off schedule"
request: request:
method: PUT method: PUT
@ -27,26 +31,33 @@ stages:
end: "00:20" end: "00:20"
response: response:
status_code: 200 status_code: 200
body: json:
id: "off"
name: "{tavern.request_vars.json.name}" name: "{tavern.request_vars.json.name}"
periods: [] periods: []
tags: !anylist
- name: "[test_schedules_protected] delete protected on schedule; expect forbidden/fail" - name: "[test_schedules_protected] delete protected on schedule; expect forbidden/fail"
request: request:
method: DELETE method: DELETE
url: "http://localhost:5000/api/v1/schedules/on" url: "http://localhost:5000/api/v1/schedules/on"
response: response:
status_code: 403 status_code: 403
- name: get protected on schedule - name: get protected on schedule
request: request:
method: GET method: GET
url: "http://localhost:5000/api/v1/schedules/on" url: "http://localhost:5000/api/v1/schedules/on"
response: response:
status_code: 200 status_code: 200
body: json:
id: "on"
name: "on" name: "on"
periods: periods:
- start: "00:00" - start: "00:00"
end: "23:59" end: "23:59"
tags: !anylist
- name: "[test_schedules_protected] overwrite protected on schedule" - name: "[test_schedules_protected] overwrite protected on schedule"
request: request:
method: PUT method: PUT
@ -58,8 +69,10 @@ stages:
end: "17:20" end: "17:20"
response: response:
status_code: 200 status_code: 200
body: json:
id: "on"
name: "{tavern.request_vars.json.name}" name: "{tavern.request_vars.json.name}"
periods: periods:
- start: "00:00" - start: "00:00"
end: "23:59" end: "23:59"
tags: !anylist

View file

@ -1,7 +1,7 @@
test_name: Test tagging of schedules and relays test_name: "[tags] Test tagging of schedules and relays"
stages: stages:
- name: "[test_tags] post schedule, expect it to be echoed back by tag" - name: "[tags] post schedule, expect it to be echoed back by tag"
request: request:
method: POST method: POST
url: "http://localhost:5000/api/v1/schedules/" url: "http://localhost:5000/api/v1/schedules/"
@ -14,7 +14,7 @@ stages:
- "test_tag_1" - "test_tag_1"
response: response:
status_code: 201 status_code: 201
body: json:
id: !anystr id: !anystr
name: "{tavern.request_vars.json.name}" name: "{tavern.request_vars.json.name}"
periods: periods:
@ -23,18 +23,21 @@ stages:
tags: tags:
- "{tavern.request_vars.json.tags[0]}" - "{tavern.request_vars.json.tags[0]}"
save: save:
body: json:
returned_name: name returned_name: name
returned_id: id returned_id: id
- name: "[test_tags] get schedule, check name and some periods"
- name: "[tags] get schedule, check name and some periods"
request: request:
method: GET method: GET
url: "http://localhost:5000/api/v1/schedules/tag/test_tag_1" url: "http://localhost:5000/api/v1/schedules/tag/test_tag_1"
response: response:
status_code: 200 status_code: 200
body: json:
- name: "{returned_name}" - name: "{returned_name}"
id: "{returned_id}" id: "{returned_id}"
periods: periods:
- start: "00:50" - start: "00:50"
end: "01:00" end: "01:00"
tags:
- "test_tag_1"