From 8bd26f2dedc72c0c7b831d76ac9bd4b37f24a6ab Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Tue, 10 Apr 2018 16:27:19 +0800 Subject: [PATCH] add support for environment variables in device tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Previously device tests included information such as access point SSID/password at compile time. This made it difficult to compile test binaries once and then send them to multiple test runners for execution. This change adds a command to the test library to set environment variable on the target device: “setenv key value”. C library setenv/getenv facility is used to store variables. Test runner, tests, and makefile are updated to use this functionality. --- tests/device/.gitignore | 1 + tests/device/Makefile | 29 ++-- tests/device/libraries/BSTest/runner.py | 51 +++++- tests/device/libraries/BSTest/src/BSArduino.h | 24 ++- tests/device/libraries/BSTest/src/BSArgs.h | 151 ++++++++++++++++++ .../device/libraries/BSTest/src/BSProtocol.h | 35 +++- tests/device/libraries/BSTest/src/BSStdio.h | 13 +- tests/device/libraries/BSTest/src/BSTest.h | 8 +- tests/device/libraries/test_config/.gitignore | 1 - .../libraries/test_config/library.properties | 9 -- .../test_config/test_config.h.template | 12 -- .../test_ClientContext/test_ClientContext.ino | 5 +- .../test_WiFiClient/test_WiFiClient.ino | 3 +- .../test_WiFiServer/test_WiFiServer.ino | 3 +- .../test_WiFi_events/test_WiFi_events.ino | 9 +- tests/device/test_env.cfg.template | 9 ++ .../test_http_client/test_http_client.ino | 13 +- .../test_http_server/test_http_server.ino | 3 +- tests/device/test_schedule/test_schedule.ino | 1 - tests/device/test_tests/test_tests.ino | 1 - tests/device/test_time/test_time.ino | 3 +- 21 files changed, 306 insertions(+), 78 deletions(-) create mode 100644 tests/device/libraries/BSTest/src/BSArgs.h delete mode 100644 tests/device/libraries/test_config/.gitignore delete mode 100644 tests/device/libraries/test_config/library.properties delete mode 100644 tests/device/libraries/test_config/test_config.h.template create mode 100644 tests/device/test_env.cfg.template diff --git a/tests/device/.gitignore b/tests/device/.gitignore index b2b1b8d45..102e164f2 100644 --- a/tests/device/.gitignore +++ b/tests/device/.gitignore @@ -2,3 +2,4 @@ .hardware test_report.xml test_report.html +test_env.cfg diff --git a/tests/device/Makefile b/tests/device/Makefile index ff79cbd80..c462f6a19 100644 --- a/tests/device/Makefile +++ b/tests/device/Makefile @@ -12,17 +12,10 @@ BS_DIR ?= libraries/BSTest DEBUG_LEVEL ?= DebugLevel=None____ FQBN ?= esp8266com:esp8266:generic:CpuFrequency=80,FlashFreq=40,FlashMode=dio,UploadSpeed=115200,FlashSize=4M1M,LwIPVariant=v2mss536,ResetMethod=none,Debug=Serial,$(DEBUG_LEVEL) BUILD_TOOL := $(ARDUINO_IDE_PATH)/arduino-builder -TEST_CONFIG := libraries/test_config/test_config.h +TEST_CONFIG := test_env.cfg TEST_REPORT_XML := test_report.xml TEST_REPORT_HTML := test_report.html -ifeq ("$(UPLOAD_PORT)","") -$(error "Failed to detect upload port, please export UPLOAD_PORT manually") -endif - -ifeq ("$(ARDUINO_IDE_PATH)","") -$(error "Please export ARDUINO_IDE_PATH") -endif ifneq ("$(V)","1") SILENT = @ @@ -35,16 +28,17 @@ endif all: count tests test_report -count: - @echo Running $(words $(TEST_LIST)) tests +$(TEST_LIST): | virtualenv $(TEST_CONFIG) $(BUILD_DIR) $(HARDWARE_DIR) -tests: $(BUILD_DIR) $(HARDWARE_DIR) virtualenv $(TEST_CONFIG) $(TEST_LIST) +tests: $(TEST_LIST) $(TEST_LIST): LOCAL_BUILD_DIR=$(BUILD_DIR)/$(notdir $@) $(TEST_LIST): + @echo Running $(words $(TEST_LIST)) tests $(SILENT)mkdir -p $(LOCAL_BUILD_DIR) ifneq ("$(NO_BUILD)","1") + @test -n "$(ARDUINO_IDE_PATH)" || (echo "Please export ARDUINO_IDE_PATH" && exit 1) @echo Compiling $(notdir $@) $(SILENT)$(BUILD_TOOL) -compile -logger=human \ -libraries "$(PWD)/libraries" \ @@ -59,6 +53,7 @@ ifneq ("$(NO_BUILD)","1") $@ endif ifneq ("$(NO_UPLOAD)","1") + @test -n "$(UPLOAD_PORT)" || (echo "Failed to detect upload port, please export UPLOAD_PORT manually" && exit 1) @echo Uploading binary $(SILENT)$(ESPTOOL) $(UPLOAD_VERBOSE_FLAG) \ -cp $(UPLOAD_PORT) \ @@ -67,6 +62,7 @@ ifneq ("$(NO_UPLOAD)","1") -cf $(LOCAL_BUILD_DIR)/$(notdir $@).bin endif ifneq ("$(NO_RUN)","1") + @test -n "$(UPLOAD_PORT)" || (echo "Failed to detect upload port, please export UPLOAD_PORT manually" && exit 1) @echo Running tests $(SILENT)$(ESPTOOL) $(UPLOAD_VERBOSE_FLAG) -cp $(UPLOAD_PORT) -cd $(UPLOAD_BOARD) -cr @source $(BS_DIR)/virtualenv/bin/activate && \ @@ -75,6 +71,7 @@ ifneq ("$(NO_RUN)","1") -p $(UPLOAD_PORT) \ -n $(basename $(notdir $@)) \ -o $(LOCAL_BUILD_DIR)/test_result.xml \ + --env-file $(TEST_CONFIG) \ `test -f $(addsuffix .py, $(basename $@)) && echo "-m $(addsuffix .py, $(basename $@))" || echo ""` endif @@ -95,7 +92,7 @@ $(HARDWARE_DIR): cd $(HARDWARE_DIR)/esp8266com && ln -s $(realpath $(ESP8266_CORE_PATH)) esp8266 virtualenv: - make -C $(BS_DIR) virtualenv + @make -C $(BS_DIR) virtualenv clean: rm -rf $(BUILD_DIR) @@ -104,9 +101,9 @@ clean: $(TEST_CONFIG): @echo "****** " - @echo "****** libraries/test_config/test_config.h does not exist" - @echo "****** Create one from libraries/test_config/test_config.h.template" + @echo "****** $(TEST_CONFIG) does not exist" + @echo "****** Create one from $(TEST_CONFIG).template" @echo "****** " - false + @false -.PHONY: tests all count virtualenv test_report $(BUILD_DIR) $(TEST_LIST) +.PHONY: tests all count virtualenv test_report $(TEST_LIST) diff --git a/tests/device/libraries/BSTest/runner.py b/tests/device/libraries/BSTest/runner.py index e12b42711..4f964f55b 100644 --- a/tests/device/libraries/BSTest/runner.py +++ b/tests/device/libraries/BSTest/runner.py @@ -9,6 +9,11 @@ import argparse import serial import subprocess import imp +try: + from configparser import ConfigParser +except: + from ConfigParser import ConfigParser +import itertools from urlparse import urlparse from junit_xml import TestSuite, TestCase try: @@ -34,12 +39,13 @@ class BSTestRunner(object): CRASH = 3 BEGINTIMEOUT = 4 - def __init__(self, spawn_obj, name, mocks): + def __init__(self, spawn_obj, name, mocks, env_vars): self.sp = spawn_obj self.tests = [] self.reset_timeout = 2 self.name = name self.mocks = mocks + self.env_vars = env_vars def get_test_list(self): self.sp.sendline('-1') @@ -73,6 +79,7 @@ class BSTestRunner(object): def run_tests(self): test_cases = [] + should_update_env = True for test in self.tests: desc = test['desc'] name = test['name'] @@ -84,13 +91,20 @@ class BSTestRunner(object): else: test_output = StringIO() self.sp.logfile = test_output + print('running test "{}"'.format(name)) + if should_update_env: + res = self.update_env() + if res != BSTestRunner.SUCCESS: + print('failed to set environment variables') + break; + should_update_env = False if name in self.mocks: - print('setting up mocks') + debug_print('setting up mocks') self.mocks[name]['setup']() t_start = time.time() result = self.run_test(index) if name in self.mocks: - print('tearing down mocks') + debug_print('tearing down mocks') self.mocks[name]['teardown']() t_stop = time.time() self.sp.logfile = None @@ -103,6 +117,7 @@ class BSTestRunner(object): else: print('test "{}" failed'.format(name)) test_case.add_failure_info('Test failed', output=test_output.getvalue()) + should_update_env = True test_output.close() test_cases += [test_case]; return TestSuite(self.name, test_cases) @@ -152,6 +167,22 @@ class BSTestRunner(object): if timeout <= 0: return BSTestRunner.TIMEOUT + def update_env(self): + for env_kv in self.env_vars: + self.sp.sendline('setenv "{}" "{}"'.format(env_kv[0], env_kv[1])) + timeout = 10 + while timeout > 0: + res = self.sp.expect(['>>>>>bs_test_setenv ok', EOF, TIMEOUT]) + if res == 0: + break + time.sleep(0.1) + timeout -= 0.1 + if res == 0: + continue + else: + return BSTestRunner.TIMEOUT + return BSTestRunner.SUCCESS + ser = None def spawn_port(port_name, baudrate=115200): @@ -162,8 +193,8 @@ def spawn_port(port_name, baudrate=115200): def spawn_exec(name): return pexpect.spawn(name, timeout=0) -def run_tests(spawn, name, mocks): - tw = BSTestRunner(spawn, name, mocks) +def run_tests(spawn, name, mocks, env_vars): + tw = BSTestRunner(spawn, name, mocks, env_vars) tw.get_test_list() return tw.run_tests() @@ -175,6 +206,7 @@ def parse_args(): parser.add_argument('-n', '--name', help='Test run name') parser.add_argument('-o', '--output', help='Output JUnit format test report') parser.add_argument('-m', '--mock', help='Set python script to use for mocking purposes') + parser.add_argument('--env-file', help='File containing a list of environment variables to set', type=argparse.FileType('r')) return parser.parse_args() def main(): @@ -194,12 +226,19 @@ def main(): if spawn_func is None: debug_print("Please specify port or executable", file=sys.stderr) return 1 + env_vars = [] + if args.env_file is not None: + cfg = ConfigParser() + cfg.optionxform = str + with args.env_file as fp: + cfg.readfp(fp) + env_vars = cfg.items('global') mocks = {} if args.mock is not None: mocks_mod = imp.load_source('mocks', args.mock) mocks = mock_decorators.env with spawn_func(spawn_arg) as sp: - ts = run_tests(sp, name, mocks) + ts = run_tests(sp, name, mocks, env_vars) if args.output: with open(args.output, "w") as f: TestSuite.to_file(f, [ts]) diff --git a/tests/device/libraries/BSTest/src/BSArduino.h b/tests/device/libraries/BSTest/src/BSArduino.h index ff53ca19a..2fd4dc66e 100644 --- a/tests/device/libraries/BSTest/src/BSArduino.h +++ b/tests/device/libraries/BSTest/src/BSArduino.h @@ -35,11 +35,27 @@ public: return len; } - bool read_int(int& result) + size_t read_line(char* dest, size_t dest_size) { - // TODO: fix this for 0 value - result = m_stream.parseInt(); - return result != 0; + size_t len = 0; + // Can't use Stream::readBytesUntil here because it can't tell the + // difference between timing out and receiving the terminator. + while (len < dest_size - 1) { + int c = m_stream.read(); + if (c < 0) { + delay(1); + continue; + } + if (c == '\r') { + continue; + } + if (c == '\n') { + dest[len] = 0; + break; + } + dest[len++] = c; + } + return len; } protected: diff --git a/tests/device/libraries/BSTest/src/BSArgs.h b/tests/device/libraries/BSTest/src/BSArgs.h new file mode 100644 index 000000000..e4a7008f1 --- /dev/null +++ b/tests/device/libraries/BSTest/src/BSArgs.h @@ -0,0 +1,151 @@ +/* Splitting string into tokens, taking quotes and escape sequences into account. + * From https://github.com/espressif/esp-idf/blob/master/components/console/split_argv.c + * Copyright 2016-2017 Espressif Systems (Shanghai) PTE LTD + * Licensed under the Apache License 2.0. + */ + +#ifndef BS_ARGS_H +#define BS_ARGS_H + +#include +#include +#include + +namespace bs +{ +namespace protocol +{ + +#define SS_FLAG_ESCAPE 0x8 + +typedef enum { + /* parsing the space between arguments */ + SS_SPACE = 0x0, + /* parsing an argument which isn't quoted */ + SS_ARG = 0x1, + /* parsing a quoted argument */ + SS_QUOTED_ARG = 0x2, + /* parsing an escape sequence within unquoted argument */ + SS_ARG_ESCAPED = SS_ARG | SS_FLAG_ESCAPE, + /* parsing an escape sequence within a quoted argument */ + SS_QUOTED_ARG_ESCAPED = SS_QUOTED_ARG | SS_FLAG_ESCAPE, +} split_state_t; + +/* helper macro, called when done with an argument */ +#define END_ARG() do { \ + char_out = 0; \ + argv[argc++] = next_arg_start; \ + state = SS_SPACE; \ + } while(0); + + +/** + * @brief Split command line into arguments in place + * + * - This function finds whitespace-separated arguments in the given input line. + * + * 'abc def 1 20 .3' -> [ 'abc', 'def', '1', '20', '.3' ] + * + * - Argument which include spaces may be surrounded with quotes. In this case + * spaces are preserved and quotes are stripped. + * + * 'abc "123 456" def' -> [ 'abc', '123 456', 'def' ] + * + * - Escape sequences may be used to produce backslash, double quote, and space: + * + * 'a\ b\\c\"' -> [ 'a b\c"' ] + * + * Pointers to at most argv_size - 1 arguments are returned in argv array. + * The pointer after the last one (i.e. argv[argc]) is set to NULL. + * + * @param line pointer to buffer to parse; it is modified in place + * @param argv array where the pointers to arguments are written + * @param argv_size number of elements in argv_array (max. number of arguments will be argv_size - 1) + * @return number of arguments found (argc) + */ +inline size_t split_args(char *line, char **argv, size_t argv_size) +{ + const int QUOTE = '"'; + const int ESCAPE = '\\'; + const int SPACE = ' '; + split_state_t state = SS_SPACE; + int argc = 0; + char *next_arg_start = line; + char *out_ptr = line; + for (char *in_ptr = line; argc < argv_size - 1; ++in_ptr) { + int char_in = (unsigned char) *in_ptr; + if (char_in == 0) { + break; + } + int char_out = -1; + + switch (state) { + case SS_SPACE: + if (char_in == SPACE) { + /* skip space */ + } else if (char_in == QUOTE) { + next_arg_start = out_ptr; + state = SS_QUOTED_ARG; + } else if (char_in == ESCAPE) { + next_arg_start = out_ptr; + state = SS_ARG_ESCAPED; + } else { + next_arg_start = out_ptr; + state = SS_ARG; + char_out = char_in; + } + break; + + case SS_QUOTED_ARG: + if (char_in == QUOTE) { + END_ARG(); + } else if (char_in == ESCAPE) { + state = SS_QUOTED_ARG_ESCAPED; + } else { + char_out = char_in; + } + break; + + case SS_ARG_ESCAPED: + case SS_QUOTED_ARG_ESCAPED: + if (char_in == ESCAPE || char_in == QUOTE || char_in == SPACE) { + char_out = char_in; + } else { + /* unrecognized escape character, skip */ + } + state = (split_state_t) (state & (~SS_FLAG_ESCAPE)); + break; + + case SS_ARG: + if (char_in == SPACE) { + END_ARG(); + } else if (char_in == ESCAPE) { + state = SS_ARG_ESCAPED; + } else { + char_out = char_in; + } + break; + } + /* need to output anything? */ + if (char_out >= 0) { + *out_ptr = char_out; + ++out_ptr; + } + } + /* make sure the final argument is terminated */ + *out_ptr = 0; + /* finalize the last argument */ + if (state != SS_SPACE && argc < argv_size - 1) { + argv[argc++] = next_arg_start; + } + /* add a NULL at the end of argv */ + argv[argc] = NULL; + + return argc; +} + +} // namespace bs + +} // namespace protocol + +#endif //BS_ARGS_H \ No newline at end of file diff --git a/tests/device/libraries/BSTest/src/BSProtocol.h b/tests/device/libraries/BSTest/src/BSProtocol.h index 54d7bcc0e..0c72dd60b 100644 --- a/tests/device/libraries/BSTest/src/BSProtocol.h +++ b/tests/device/libraries/BSTest/src/BSProtocol.h @@ -1,6 +1,8 @@ #ifndef BS_PROTOCOL_H #define BS_PROTOCOL_H +#include "BSArgs.h" + #define BS_LINE_PREFIX ">>>>>bs_test_" namespace bs @@ -44,9 +46,38 @@ void output_menu_end(IO& io) } template -bool input_menu_choice(IO& io, int& result) +void output_setenv_result(IO& io, const char* key, const char* value) { - return io.read_int(result); + io.printf(BS_LINE_PREFIX "setenv ok key='%s' value='%s'\n", key, value); +} + +template +bool input_handle(IO& io, char* line_buf, size_t line_buf_size, int& test_num) +{ + int cb_read = io.read_line(line_buf, line_buf_size); + if (cb_read == 0 || line_buf[0] == '\n') { + return false; + } + char* argv[4]; + size_t argc = split_args(line_buf, argv, sizeof(argv)/sizeof(argv[0])); + if (argc == 0) { + return false; + } + if (strcmp(argv[0], "setenv") == 0) { + if (argc != 3) { + return false; + } + setenv(argv[1], argv[2], 1); + output_setenv_result(io, argv[1], argv[2]); + test_num = -1; + return false; /* we didn't get the test number yet, so return false */ + } + char* endptr; + test_num = (int) strtol(argv[0], &endptr, 10); + if (endptr != argv[0] + strlen(argv[0])) { + return false; + } + return true; } } // ::protocol diff --git a/tests/device/libraries/BSTest/src/BSStdio.h b/tests/device/libraries/BSTest/src/BSStdio.h index ea22f2236..4d1bfb56f 100644 --- a/tests/device/libraries/BSTest/src/BSStdio.h +++ b/tests/device/libraries/BSTest/src/BSStdio.h @@ -22,9 +22,18 @@ public: return result; } - bool read_int(int& result) + size_t read_line(char* dest, size_t dest_size) { - return scanf("%d", &result) == 1; + char* res = fgets(dest, dest_size, stdin); + if (res == NULL) { + return 0; + } + size_t len = strlen(dest); + if (dest[len - 1] == '\n') { + dest[len - 1] = 0; + len--; + } + return len; } }; diff --git a/tests/device/libraries/BSTest/src/BSTest.h b/tests/device/libraries/BSTest/src/BSTest.h index c15f9b64d..11b4ee9f6 100644 --- a/tests/device/libraries/BSTest/src/BSTest.h +++ b/tests/device/libraries/BSTest/src/BSTest.h @@ -14,6 +14,10 @@ #include "BSStdio.h" #endif +#ifndef BS_LINE_BUF_SIZE +#define BS_LINE_BUF_SIZE 80 +#endif + namespace bs { typedef void(*test_case_func_t)(); @@ -143,7 +147,8 @@ protected: protocol::output_menu_end(m_io); while(true) { int id; - if (!protocol::input_menu_choice(m_io, id)) { + char line_buf[BS_LINE_BUF_SIZE]; + if (!protocol::input_handle(m_io, line_buf, sizeof(line_buf), id)) { continue; } if (id < 0) { @@ -214,4 +219,5 @@ inline void require(bool condition, size_t line) #define BS_ENV_DECLARE() namespace bs { Env g_env; } #define BS_RUN(...) do { bs::IOHelper helper = bs::IOHelper(__VA_ARGS__); bs::Runner runner(helper); runner.run(); } while(0); + #endif //BSTEST_H diff --git a/tests/device/libraries/test_config/.gitignore b/tests/device/libraries/test_config/.gitignore deleted file mode 100644 index 4be1df52e..000000000 --- a/tests/device/libraries/test_config/.gitignore +++ /dev/null @@ -1 +0,0 @@ -test_config.h diff --git a/tests/device/libraries/test_config/library.properties b/tests/device/libraries/test_config/library.properties deleted file mode 100644 index 5df5c966e..000000000 --- a/tests/device/libraries/test_config/library.properties +++ /dev/null @@ -1,9 +0,0 @@ -name=TestConfig -version=0.0 -author= -maintainer= -sentence= -paragraph= -category=Uncategorized -url= -architectures=esp8266 diff --git a/tests/device/libraries/test_config/test_config.h.template b/tests/device/libraries/test_config/test_config.h.template deleted file mode 100644 index f94f583e1..000000000 --- a/tests/device/libraries/test_config/test_config.h.template +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once - -// Tests which use STA mode will connect to this AP: -#define STA_SSID "test_wifi" -#define STA_PASS "test_wifi_pass" - -// Tests which use AP mode will set up a SoftAP with these parameters: -#define AP_SSID "test_wifi_ap" -#define AP_PASS "test_wifi_ap_pass" - -// IP address of the PC running the tests (needed for HTTP client and HTTP server tests) -#define SERVER_IP "192.168.10.1" diff --git a/tests/device/test_ClientContext/test_ClientContext.ino b/tests/device/test_ClientContext/test_ClientContext.ino index ea6ea015d..10cedf107 100644 --- a/tests/device/test_ClientContext/test_ClientContext.ino +++ b/tests/device/test_ClientContext/test_ClientContext.ino @@ -1,6 +1,5 @@ #include #include -#include #include extern "C" { @@ -26,7 +25,7 @@ void setup() Serial.setDebugOutput(true); WiFi.persistent(false); WiFi.mode(WIFI_STA); - WiFi.begin(STA_SSID, STA_PASS); + WiFi.begin(getenv("STA_SSID"), getenv("STA_PASS")); while (WiFi.status() != WL_CONNECTED) { delay(500); } @@ -37,7 +36,7 @@ TEST_CASE("WiFi release ClientContext", "[clientcontext]") { #define MAXLOOPS 50 #define SUCCESS_GOAL 10 - #define srv SERVER_IP + #define srv getenv("SERVER_IP") WiFiClient client; diff --git a/tests/device/test_WiFiClient/test_WiFiClient.ino b/tests/device/test_WiFiClient/test_WiFiClient.ino index a9cdd5630..4d7eb7391 100644 --- a/tests/device/test_WiFiClient/test_WiFiClient.ino +++ b/tests/device/test_WiFiClient/test_WiFiClient.ino @@ -1,6 +1,5 @@ #include #include -#include #include #include @@ -16,7 +15,7 @@ void setup() Serial.setDebugOutput(true); WiFi.persistent(false); WiFi.mode(WIFI_STA); - WiFi.begin(STA_SSID, STA_PASS); + WiFi.begin(getenv("STA_SSID"), getenv("STA_PASS")); while (WiFi.status() != WL_CONNECTED) { delay(500); } diff --git a/tests/device/test_WiFiServer/test_WiFiServer.ino b/tests/device/test_WiFiServer/test_WiFiServer.ino index 88b5ef6d6..2040df54c 100644 --- a/tests/device/test_WiFiServer/test_WiFiServer.ino +++ b/tests/device/test_WiFiServer/test_WiFiServer.ino @@ -3,7 +3,6 @@ #include #include #include -#include BS_ENV_DECLARE(); @@ -12,7 +11,7 @@ void setup() { Serial.begin(115200); WiFi.persistent(false); - WiFi.begin(STA_SSID, STA_PASS); + WiFi.begin(getenv("STA_SSID"), getenv("STA_PASS")); while (WiFi.status() != WL_CONNECTED) { delay(500); } diff --git a/tests/device/test_WiFi_events/test_WiFi_events.ino b/tests/device/test_WiFi_events/test_WiFi_events.ino index 3344f24ab..704f3c3c7 100644 --- a/tests/device/test_WiFi_events/test_WiFi_events.ino +++ b/tests/device/test_WiFi_events/test_WiFi_events.ino @@ -3,7 +3,6 @@ #include #include #include -#include #include BS_ENV_DECLARE(); @@ -36,7 +35,7 @@ TEST_CASE("WiFi.onEvent is called for specific events", "[wifi][events]") WiFi.onEvent(onWiFiEvent, WIFI_EVENT_ANY); WiFi.mode(WIFI_STA); - WiFi.begin(STA_SSID, STA_PASS); + WiFi.begin(getenv("STA_SSID"), getenv("STA_PASS")); unsigned long start = millis(); while (WiFi.status() != WL_CONNECTED) { delay(500); @@ -68,7 +67,7 @@ TEST_CASE("STA mode events are called both when using DHCP and static config", " // run the test with DHCP WiFi.mode(WIFI_STA); - WiFi.begin(STA_SSID, STA_PASS); + WiFi.begin(getenv("STA_SSID"), getenv("STA_PASS")); unsigned long start = millis(); while (WiFi.status() != WL_CONNECTED) { delay(500); @@ -88,7 +87,7 @@ TEST_CASE("STA mode events are called both when using DHCP and static config", " WiFi.mode(WIFI_STA); WiFi.config(localIP, gatewayIP, subnetMask); - WiFi.begin(STA_SSID, STA_PASS); + WiFi.begin(getenv("STA_SSID"), getenv("STA_PASS")); start = millis(); while (WiFi.status() != WL_CONNECTED) { delay(500); @@ -115,7 +114,7 @@ TEST_CASE("Events are not called if handler is deleted", "[wifi][events]") }); WiFi.mode(WIFI_STA); - WiFi.begin(STA_SSID, STA_PASS); + WiFi.begin(getenv("STA_SSID"), getenv("STA_PASS")); unsigned long start = millis(); while (WiFi.status() != WL_CONNECTED) { delay(500); diff --git a/tests/device/test_env.cfg.template b/tests/device/test_env.cfg.template new file mode 100644 index 000000000..33f0bc15e --- /dev/null +++ b/tests/device/test_env.cfg.template @@ -0,0 +1,9 @@ +[global] + +STA_SSID = my_ssid +STA_PASS = my_password + +AP_SSID = test_wifi_ap +AP_PASS = test_wifi_ap_pass + +SERVER_IP = 192.168.1.100 diff --git a/tests/device/test_http_client/test_http_client.ino b/tests/device/test_http_client/test_http_client.ino index d23c85ce3..70699c0bb 100644 --- a/tests/device/test_http_client/test_http_client.ino +++ b/tests/device/test_http_client/test_http_client.ino @@ -2,7 +2,6 @@ #include #include #include -#include #include BS_ENV_DECLARE(); @@ -12,7 +11,7 @@ void setup() Serial.begin(115200); Serial.setDebugOutput(true); WiFi.persistent(false); - WiFi.begin(STA_SSID, STA_PASS); + WiFi.begin(getenv("STA_SSID"), getenv("STA_PASS")); while (WiFi.status() != WL_CONNECTED) { delay(500); } @@ -26,7 +25,7 @@ TEST_CASE("HTTP GET & POST requests", "[HTTPClient]") { // small request HTTPClient http; - http.begin(SERVER_IP, 8088, "/"); + http.begin(getenv("SERVER_IP"), 8088, "/"); auto httpCode = http.GET(); REQUIRE(httpCode == HTTP_CODE_OK); String payload = http.getString(); @@ -35,7 +34,7 @@ TEST_CASE("HTTP GET & POST requests", "[HTTPClient]") { // request which returns 8000 bytes HTTPClient http; - http.begin(SERVER_IP, 8088, "/data?size=8000"); + http.begin(getenv("SERVER_IP"), 8088, "/data?size=8000"); auto httpCode = http.GET(); REQUIRE(httpCode == HTTP_CODE_OK); String payload = http.getString(); @@ -50,7 +49,7 @@ TEST_CASE("HTTP GET & POST requests", "[HTTPClient]") { // can do two POST requests with one HTTPClient object (#1902) HTTPClient http; - http.begin(SERVER_IP, 8088, "/"); + http.begin(getenv("SERVER_IP"), 8088, "/"); http.addHeader("Content-Type", "text/plain"); auto httpCode = http.POST("foo"); Serial.println(httpCode); @@ -69,7 +68,7 @@ TEST_CASE("HTTPS GET request", "[HTTPClient]") { // small request HTTPClient http; - http.begin(SERVER_IP, 8088, "/", fp); + http.begin(getenv("SERVER_IP"), 8088, "/", fp); auto httpCode = http.GET(); REQUIRE(httpCode == HTTP_CODE_OK); String payload = http.getString(); @@ -78,7 +77,7 @@ TEST_CASE("HTTPS GET request", "[HTTPClient]") { // request which returns 8000 bytes HTTPClient http; - http.begin(SERVER_IP, 8088, "/data?size=4000", fp); + http.begin(getenv("SERVER_IP"), 8088, "/data?size=4000", fp); auto httpCode = http.GET(); REQUIRE(httpCode == HTTP_CODE_OK); String payload = http.getString(); diff --git a/tests/device/test_http_server/test_http_server.ino b/tests/device/test_http_server/test_http_server.ino index 7ad662619..4231cd066 100644 --- a/tests/device/test_http_server/test_http_server.ino +++ b/tests/device/test_http_server/test_http_server.ino @@ -3,7 +3,6 @@ #include #include #include -#include #include BS_ENV_DECLARE(); @@ -17,7 +16,7 @@ void setup() Serial.begin(115200); Serial.setDebugOutput(true); WiFi.persistent(false); - WiFi.begin(STA_SSID, STA_PASS); + WiFi.begin(getenv("STA_SSID"), getenv("STA_PASS")); while (WiFi.status() != WL_CONNECTED) { delay(500); } diff --git a/tests/device/test_schedule/test_schedule.ino b/tests/device/test_schedule/test_schedule.ino index 812d3339b..963635a68 100644 --- a/tests/device/test_schedule/test_schedule.ino +++ b/tests/device/test_schedule/test_schedule.ino @@ -1,5 +1,4 @@ #include -#include #include BS_ENV_DECLARE(); diff --git a/tests/device/test_tests/test_tests.ino b/tests/device/test_tests/test_tests.ino index 9db064afa..2420805fc 100644 --- a/tests/device/test_tests/test_tests.ino +++ b/tests/device/test_tests/test_tests.ino @@ -1,5 +1,4 @@ #include -#include BS_ENV_DECLARE(); diff --git a/tests/device/test_time/test_time.ino b/tests/device/test_time/test_time.ino index 5568faa50..6642d3687 100644 --- a/tests/device/test_time/test_time.ino +++ b/tests/device/test_time/test_time.ino @@ -2,7 +2,6 @@ #include #include #include -#include BS_ENV_DECLARE(); @@ -10,7 +9,7 @@ void setup() { Serial.begin(115200); WiFi.persistent(false); - WiFi.begin(STA_SSID, STA_PASS); + WiFi.begin(getenv("STA_SSID"), getenv("STA_PASS")); while (WiFi.status() != WL_CONNECTED) { delay(500); }