configs: check that config doesn't contain extra fields

Signed-off-by: Andrey Vagin <avagin@openvz.org>
This commit is contained in:
Andrey Vagin 2015-03-04 12:13:45 +03:00
parent f78bf211f0
commit a72f710d89
7 changed files with 30 additions and 7 deletions

View File

@ -2,6 +2,7 @@ package configs
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"testing"
@ -43,6 +44,34 @@ func loadConfig(name string) (*Config, error) {
return nil, err
}
// Check that a config doesn't contain extra fields
var configMap, abstractMap map[string]interface{}
if _, err := f.Seek(0, 0); err != nil {
return nil, err
}
if err := json.NewDecoder(f).Decode(&abstractMap); err != nil {
return nil, err
}
configData, err := json.Marshal(&container)
if err != nil {
return nil, err
}
if err := json.Unmarshal(configData, &configMap); err != nil {
return nil, err
}
for k := range configMap {
delete(abstractMap, k)
}
if len(abstractMap) != 0 {
return nil, fmt.Errorf("unknown fields: %s", abstractMap)
}
return container, nil
}

View File

@ -104,7 +104,6 @@
],
"mount_label": "",
"hostname": "nsinit",
"console": "",
"namespaces": [
{
"type": "NEWNS",

View File

@ -104,7 +104,6 @@
],
"mount_label": "",
"hostname": "koye",
"console": "",
"namespaces": [
{
"type": "NEWNS",

View File

@ -104,7 +104,6 @@
],
"mount_label": "",
"hostname": "nsinit",
"console": "",
"namespaces": [
{
"type": "NEWNS",

View File

@ -104,7 +104,6 @@
],
"mount_label": "",
"hostname": "nsinit",
"console": "",
"namespaces": [
{
"type": "NEWNS",

View File

@ -104,7 +104,6 @@
],
"mount_label": "system_u:system_r:svirt_lxc_net_t:s0:c164,c475",
"hostname": "nsinit",
"console": "",
"namespaces": [
{
"type": "NEWNS",

View File

@ -104,7 +104,6 @@
],
"mount_label": "",
"hostname": "nsinit",
"console": "",
"namespaces": [
{
"type": "NEWNS",