Tests for config files.

pull/22/head
weetmuts 2019-02-23 21:21:17 +01:00
rodzic ca79c0063a
commit ff06425bf9
14 zmienionych plików z 144 dodań i 50 usunięć

Wyświetl plik

@ -46,7 +46,9 @@ endif
$(shell mkdir -p $(BUILD))
CXXFLAGS := $(DEBUG_FLAGS) -fPIC -fmessage-length=0 -std=c++11 -Wall -Wno-maybe-uninitialized -Wno-unused-function "-DWMBUSMETERS_VERSION=\"$(VERSION)\""
#EXTRAS=-Wno-maybe-uninitialized
CXXFLAGS := $(DEBUG_FLAGS) -fPIC -fmessage-length=0 -std=c++11 -Wall -Wno-unused-function "-DWMBUSMETERS_VERSION=\"$(VERSION)\""
$(BUILD)/%.o: src/%.cc $(wildcard src/%.h)
$(CXX) $(CXXFLAGS) $< -c -o $@

Wyświetl plik

@ -69,58 +69,101 @@ void parseMeterConfig(CommandLine *c, vector<char> &buf, string file)
}
unique_ptr<CommandLine> loadConfiguration()
void handleLoglevel(CommandLine *c, string loglevel)
{
if (loglevel == "verbose") { c->verbose = true; }
else if (loglevel == "debug") { c->debug = true; }
else if (loglevel == "silent") { c->silence = true; }
else if (loglevel == "normal") { }
else {
warning("No such log level: \"%s\"\n", loglevel.c_str());
}
}
void handleDevice(CommandLine *c, string device)
{
c->usb_device = device;
}
void handleLogtelegrams(CommandLine *c, string logtelegrams)
{
if (logtelegrams == "true") { c->logtelegrams = true; }
else if (logtelegrams == "false") { c->logtelegrams = false;}
else {
warning("No such logtelegrams setting: \"%s\"\n", logtelegrams.c_str());
}
}
void handleMeterfilesdir(CommandLine *c, string meterfilesdir)
{
if (meterfilesdir.length() > 0)
{
c->meterfiles_dir = meterfilesdir;
c->meterfiles = true;
if (!checkIfDirExists(c->meterfiles_dir.c_str())) {
warning("Cannot write meter files into dir \"%s\"\n", c->meterfiles_dir.c_str());
}
}
}
void handleRobot(CommandLine *c, string robot)
{
if (robot == "json")
{
c->json = true;
c->fields = false;
}
else if (robot == "fields")
{
c->json = false;
c->fields = true;
c->separator = ';';
} else {
warning("Unknown output format: \"%s\"\n", robot.c_str());
}
}
void handleSeparator(CommandLine *c, string s)
{
if (s.length() == 1) {
c->separator = s[0];
} else {
warning("Separator must be a single character.\n");
}
}
unique_ptr<CommandLine> loadConfiguration(string root)
{
CommandLine *c = new CommandLine;
vector<char> global_conf;
loadFile("/etc/wmbusmeters.conf", &global_conf);
loadFile(root+"/etc/wmbusmeters.conf", &global_conf);
auto i = global_conf.begin();
string loglevel;
string device;
string meterfiles_dir;
for (;;) {
auto p = getNextKeyValue(global_conf, i);
if (p.first == "") break;
if (p.first == "loglevel") loglevel = p.second;
if (p.first == "device") device = p.second;
if (p.first == "meterfilesdir") meterfiles_dir = p.second;
if (p.first == "loglevel") handleLoglevel(c, p.second);
else if (p.first == "device") handleDevice(c, p.second);
else if (p.first == "logtelegrams") handleLogtelegrams(c, p.second);
else if (p.first == "meterfilesdir") handleMeterfilesdir(c, p.second);
else if (p.first == "robot") handleRobot(c, p.second);
else if (p.first == "separator") handleSeparator(c, p.second);
else
{
warning("No such key: %s\n", p.first.c_str());
}
}
if (loglevel == "verbose") {
c->verbose = true;
} else
if (loglevel == "debug") {
c->debug = true;
} else
if (loglevel == "silent") {
c->silence = true;
} else
if (loglevel == "normal") {
} else
{
warning("No such log level: \"%s\"\n", loglevel.c_str());
}
// cmdline->logtelegrams
vector<string> meters;
listFiles(root+"/etc/wmbusmeters.d", &meters);
c->meterfiles_dir = meterfiles_dir;
if (!checkIfDirExists(c->meterfiles_dir.c_str())) {
warning("Cannot write meter files into dir \"%s\"\n", c->meterfiles_dir.c_str());
}
c->usb_device = device;
vector<string> meter_files;
listFiles("/etc/wmbusmeters.d", &meter_files);
for (auto& f : meter_files)
for (auto& f : meters)
{
vector<char> meter_conf;
string file = string("/etc/wmbusmeters.d/")+f;
string file = root+"/etc/wmbusmeters.d/"+f;
loadFile(file.c_str(), &meter_conf);
parseMeterConfig(c, meter_conf, file);
}

Wyświetl plik

@ -65,6 +65,6 @@ struct CommandLine {
~CommandLine() = default;
};
unique_ptr<CommandLine> loadConfiguration();
unique_ptr<CommandLine> loadConfiguration(string root);
#endif

Wyświetl plik

@ -39,7 +39,7 @@ using namespace std;
void oneshotCheck(CommandLine *cmdline, SerialCommunicationManager *manager, Meter *meter, vector<unique_ptr<Meter>> &meters);
void startUsingCommandline(CommandLine *cmdline);
void startUsingConfigFiles();
void startUsingConfigFiles(string root);
void startDaemon(); // Will use config files.
int main(int argc, char **argv)
@ -80,13 +80,18 @@ int main(int argc, char **argv)
}
else
if (cmdline->useconfig) {
startUsingConfigFiles();
// This is primarily used for testing.
const char *r = getenv("WMBUSMETERS_CONFIG_ROOT");
string root = "";
if (r != NULL) {
root = r;
}
startUsingConfigFiles(root);
exit(0);
}
else {
// We want the data visible in the log file asap!
setbuf(stdout, NULL);
startUsingCommandline(cmdline.get());
}
}
@ -279,12 +284,12 @@ void startDaemon()
close(STDOUT_FILENO);
close(STDERR_FILENO);
startUsingConfigFiles();
startUsingConfigFiles("");
}
void startUsingConfigFiles()
void startUsingConfigFiles(string root)
{
unique_ptr<CommandLine> cmdline = loadConfiguration();
unique_ptr<CommandLine> cmdline = loadConfiguration(root);
startUsingCommandline(cmdline.get());
}

Wyświetl plik

@ -26,6 +26,8 @@
#include <sys/file.h>
#include <sys/ioctl.h>
#include <sys/select.h>
#include <sys/errno.h>
#include <sys/types.h>
#include <stdio.h>
#include <termios.h>
#include <unistd.h>

Wyświetl plik

@ -460,12 +460,12 @@ bool crc16_CCITT_check(uchar *data, uint16_t length)
return crc == CRC16_GOOD_VALUE;
}
bool listFiles(const char *dir, vector<string> *files)
bool listFiles(string dir, vector<string> *files)
{
DIR *dp = NULL;
struct dirent *dptr = NULL;
if (NULL == (dp = opendir(dir)))
if (NULL == (dp = opendir(dir.c_str())))
{
return false;
}
@ -485,12 +485,12 @@ bool listFiles(const char *dir, vector<string> *files)
return true;
}
bool loadFile(const char *file, vector<char> *buf)
bool loadFile(string file, vector<char> *buf)
{
int blocksize = 1024;
char block[blocksize];
int fd = open(file, O_RDONLY);
int fd = open(file.c_str(), O_RDONLY);
if (fd == -1) {
return false;
}

Wyświetl plik

@ -65,8 +65,8 @@ void incrementIV(uchar *iv, size_t len);
bool checkCharacterDeviceExists(const char *tty, bool fail_if_not);
bool checkIfSimulationFile(const char *file);
bool checkIfDirExists(const char *dir);
bool listFiles(const char *dir, std::vector<std::string> *files);
bool loadFile(const char *file, std::vector<char> *buf);
bool listFiles(std::string dir, std::vector<std::string> *files);
bool loadFile(std::string file, std::vector<char> *buf);
std::string eatTo(std::vector<uchar> &v, std::vector<uchar>::iterator &i, int c, size_t max, bool *eof, bool *err);

Wyświetl plik

@ -12,3 +12,4 @@ tests/test_c1_meters.sh $PROG
tests/test_t1_meters.sh $PROG
tests/test_shell.sh $PROG
tests/test_meterfiles.sh $PROG
tests/test_config1.sh $PROG

Wyświetl plik

@ -0,0 +1,4 @@
loglevel=normal
device=simulations/simulation_c1.txt
logtelegrams=false
robot=json

Wyświetl plik

@ -0,0 +1,4 @@
name=MyElectricity
type=omnipower
id=15947107
key=

Wyświetl plik

@ -0,0 +1,4 @@
name=MyHeater
type=multical302
id=12345678
key=

Wyświetl plik

@ -0,0 +1,4 @@
name=MyTapWater
type=multical21
id=76348799
key=

Wyświetl plik

@ -0,0 +1,4 @@
name=Vadden
type=multical21
id=44556677
key=

Wyświetl plik

@ -0,0 +1,21 @@
#!/bin/bash
PROG="$1"
TEST=testoutput
mkdir -p $TEST
cat simulations/simulation_c1.txt | grep '^{' > $TEST/test_expected.txt
WMBUSMETERS_CONFIG_ROOT=tests/config1 $PROG --useconfig > $TEST/test_output.txt
if [ "$?" == "0" ]
then
cat $TEST/test_output.txt | sed 's/"timestamp":"....-..-..T..:..:..Z"/"timestamp":"1111-11-11T11:11:11Z"/' > $TEST/test_responses.txt
diff $TEST/test_expected.txt $TEST/test_responses.txt
if [ "$?" == "0" ]
then
echo Config files 1 OK
fi
else
Failure.
fi