Add basic unittests for the config related functionality

This commit is contained in:
Arturo Filastò
2018-06-22 11:01:15 +02:00
parent 2653a3f67f
commit 1bba0c7899
5 changed files with 102 additions and 25 deletions
+3 -19
View File
@@ -3,7 +3,6 @@ package config
import (
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
"sync"
@@ -14,21 +13,6 @@ import (
// ReadConfig reads the configuration from the path
func ReadConfig(path string) (*Config, error) {
b, err := ioutil.ReadFile(path)
if os.IsNotExist(err) {
c := &Config{}
if err = c.Default(); err != nil {
return nil, errors.Wrap(err, "defaulting")
}
if err = c.Validate(); err != nil {
return nil, errors.Wrap(err, "validating")
}
return c, nil
}
if err != nil {
return nil, errors.Wrap(err, "reading file")
}
@@ -43,9 +27,9 @@ func ReadConfig(path string) (*Config, error) {
// ParseConfig returns config from JSON bytes.
func ParseConfig(b []byte) (*Config, error) {
c := &Config{}
var c Config
if err := json.Unmarshal(b, c); err != nil {
if err := json.Unmarshal(b, &c); err != nil {
return nil, errors.Wrap(err, "parsing json")
}
@@ -57,7 +41,7 @@ func ParseConfig(b []byte) (*Config, error) {
return nil, errors.Wrap(err, "validating")
}
return c, nil
return &c, nil
}
// Config for the OONI Probe installation
+19
View File
@@ -0,0 +1,19 @@
package config
import (
"testing"
)
func TestParseConfig(t *testing.T) {
config, err := ReadConfig("testdata/valid-config.json")
if err != nil {
t.Error(err)
}
if len(config.NettestGroups.Middlebox.EnabledTests) < 0 {
t.Error("at least one middlebox test should be enabled")
}
if config.Advanced.IncludeCountry == false {
t.Error("country should be included")
}
}
+63
View File
@@ -0,0 +1,63 @@
{
"_": "This is your OONI Probe config file. See https://ooni.io/help/probe-cli for help",
"_version": 0,
"_informed_consent": false,
"auto_update": true,
"sharing": {
"include_ip": false,
"include_asn": true,
"include_gps": true,
"upload_results": true,
"send_crash_reports": true
},
"notifications": {
"enabled": true,
"notify_on_test_completion": true,
"notify_on_news": false
},
"automated_testing": {
"enabled": false,
"enabled_tests": [
"web-connectivity",
"facebook-messenger",
"whatsapp",
"telegram",
"dash",
"ndt",
"http-invalid-request-line",
"http-header-field-manipulation"
],
"monthly_allowance": "300MB"
},
"test_settings": {
"websites": {
"enabled_categories": []
},
"instant_messaging": {
"enabled_tests": [
"facebook-messenger",
"whatsapp",
"telegram"
]
},
"performance": {
"enabled_tests": [
"ndt"
],
"ndt_server": "auto",
"ndt_server_port": "auto",
"dash_server": "auto",
"dash_server_port": "auto"
},
"middlebox": {
"enabled_tests": [
"http-invalid-request-line",
"http-header-field-manipulation"
]
}
},
"advanced": {
"include_country": true,
"use_domain_fronting": false
}
}