diff --git a/.gitignore b/.gitignore index fbaaad5..293edcd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,7 @@ build/ docs/ +tests/testing_tmp/ +tests/testing_bak/ + include/migrations/*.sql.h diff --git a/CMakeLists.txt b/CMakeLists.txt index bac8ca6..84587e8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,11 +23,11 @@ aux_source_directory(. SRC_DIR) aux_source_directory(models MODELS_SRC) aux_source_directory(helpers HELPERS_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) -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}/vendor) @@ -57,7 +57,7 @@ add_custom_target(docs ) 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 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/tests ) diff --git a/endpoints/api_v1_controllers_discover.c b/endpoints/api_v1_controllers_discover.c index 94a954c..01a8242 100644 --- a/endpoints/api_v1_controllers_discover.c +++ b/endpoints/api_v1_controllers_discover.c @@ -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->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_free(discovered_controller); } diff --git a/tests/controller.testing.ini b/tests/controller.testing.ini new file mode 100644 index 0000000..e34fecd --- /dev/null +++ b/tests/controller.testing.ini @@ -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 diff --git a/tests/core.testing.ini b/tests/core.testing.ini new file mode 100644 index 0000000..35d603c --- /dev/null +++ b/tests/core.testing.ini @@ -0,0 +1,5 @@ +[core] +server-port = 5000 +discovery-port = 4422 +database = core.sqlite +log-level = debug diff --git a/tests/run_tests.sh b/tests/run_tests.sh index 10919ae..6ddbce8 100755 --- a/tests/run_tests.sh +++ b/tests/run_tests.sh @@ -1,17 +1,46 @@ #!/usr/bin/env sh -mkdir ./testing_tmp -cd ./testing_tmp +source_dir=$PWD +working_dir=$PWD/testing_tmp +working_bak=$PWD/testing_bak -cp $1 ./core -cp $2 ./core.ini +rm -rf $working_dir +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=$! sleep 2; -tavern-ci --tavern-beta-new-traceback .. +tavern-ci --disable-warnings $source_dir +test_result=$? + kill $core_id -cd .. -rm -r ./testing_tmp +kill $controller_id + +if [ $test_result -gt 0 ] +then + rm -rf $working_bak + mv $working_dir $working_bak +fi + +rm -rf $working_dir + +exit $test_result diff --git a/tests/test_controllers_basic.tavern.yaml b/tests/test_controllers_basic.tavern.yaml new file mode 100644 index 0000000..8458ffa --- /dev/null +++ b/tests/test_controllers_basic.tavern.yaml @@ -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 diff --git a/tests/test_get_all.tavern.yaml b/tests/test_get_all.tavern.yaml index e20e014..9d28bf0 100644 --- a/tests/test_get_all.tavern.yaml +++ b/tests/test_get_all.tavern.yaml @@ -1,21 +1,21 @@ -test_name: Test basic get all requests +test_name: "[get_all] Test basic get all requests" stages: -- name: "[test_get_all] get all schedules" +- name: "[get_all] get all schedules" request: url: "http://localhost:5000/api/v1/schedules/" method: GET response: status_code: 200 -- name: "[test_get_all] get all relays" +- name: "[get_all] get all relays" request: url: "http://localhost:5000/api/v1/relays/" method: GET response: status_code: 200 -- name: "[test_get_all] get all controllers" +- name: "[get_all] get all controllers" request: url: "http://localhost:5000/api/v1/controllers/" method: GET diff --git a/tests/test_schedules_basic.tavern.yaml b/tests/test_schedules_basic.tavern.yaml index c877cae..f1866c1 100644 --- a/tests/test_schedules_basic.tavern.yaml +++ b/tests/test_schedules_basic.tavern.yaml @@ -22,26 +22,34 @@ stages: end: "01:00" response: status_code: 201 - body: + json: name: "{tavern.request_vars.json.name}" + id: !anystr + periods: !anylist + tags: !anylist save: - body: - returned_name: name - returned_id: id + json: + returned_name: "name" + returned_id: "id" - name: "[test_schedules_basic] get schedule, check name and some periods" request: method: GET url: "http://localhost:5000/api/v1/schedules/{returned_id}" response: status_code: 200 - body: + json: name: "{returned_name}" + id: !anystr + periods: !anylist + tags: !anylist + - name: "[test_schedules_basic] delete schedule" request: method: DELETE url: "http://localhost:5000/api/v1/schedules/{returned_id}" response: status_code: 200 + - name: "[test_schedules_basic] get deleted schedule, expect 404" request: method: GET diff --git a/tests/test_schedules_protected.tavern.yaml b/tests/test_schedules_protected.tavern.yaml index f484408..ae420c3 100644 --- a/tests/test_schedules_protected.tavern.yaml +++ b/tests/test_schedules_protected.tavern.yaml @@ -7,15 +7,19 @@ stages: url: "http://localhost:5000/api/v1/schedules/off" response: status_code: 403 + - name: "[test_schedules_protected] get protected off schedule" request: method: GET url: "http://localhost:5000/api/v1/schedules/off" response: status_code: 200 - body: + json: + id: "off" name: "off" periods: [] + tags: !anylist + - name: "[test_schedules_protected] overwrite protected off schedule" request: method: PUT @@ -27,26 +31,33 @@ stages: end: "00:20" response: status_code: 200 - body: + json: + id: "off" name: "{tavern.request_vars.json.name}" periods: [] + tags: !anylist + - name: "[test_schedules_protected] delete protected on schedule; expect forbidden/fail" request: method: DELETE url: "http://localhost:5000/api/v1/schedules/on" response: status_code: 403 + - name: get protected on schedule request: method: GET url: "http://localhost:5000/api/v1/schedules/on" response: status_code: 200 - body: + json: + id: "on" name: "on" periods: - start: "00:00" end: "23:59" + tags: !anylist + - name: "[test_schedules_protected] overwrite protected on schedule" request: method: PUT @@ -58,8 +69,10 @@ stages: end: "17:20" response: status_code: 200 - body: + json: + id: "on" name: "{tavern.request_vars.json.name}" periods: - start: "00:00" end: "23:59" + tags: !anylist diff --git a/tests/test_tags.tavern.yaml b/tests/test_tags.tavern.yaml index 4090f53..ff2b1ec 100644 --- a/tests/test_tags.tavern.yaml +++ b/tests/test_tags.tavern.yaml @@ -1,7 +1,7 @@ -test_name: Test tagging of schedules and relays +test_name: "[tags] Test tagging of schedules and relays" 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: method: POST url: "http://localhost:5000/api/v1/schedules/" @@ -14,7 +14,7 @@ stages: - "test_tag_1" response: status_code: 201 - body: + json: id: !anystr name: "{tavern.request_vars.json.name}" periods: @@ -23,18 +23,21 @@ stages: tags: - "{tavern.request_vars.json.tags[0]}" save: - body: + json: returned_name: name returned_id: id -- name: "[test_tags] get schedule, check name and some periods" + +- name: "[tags] get schedule, check name and some periods" request: method: GET url: "http://localhost:5000/api/v1/schedules/tag/test_tag_1" response: status_code: 200 - body: + json: - name: "{returned_name}" id: "{returned_id}" periods: - start: "00:50" end: "01:00" + tags: + - "test_tag_1"