Created
February 16, 2014 23:30
-
-
Save mipearson/9042189 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"io/ioutil" | |
"os" | |
"path" | |
"testing" | |
"reflect" | |
"time" | |
) | |
func TestJSONLoading(t *testing.T) { | |
var f FileConfig | |
err := json.Unmarshal([]byte("{ \"paths\": [ \"/var/log/fail2ban.log\" ], \"fields\": { \"type\": \"fail2ban\" } }"), &f) | |
if err != nil { | |
t.Fatalf("json.Unmarshal failed") | |
} | |
if len(f.Paths) != 1 { | |
t.FailNow() | |
} | |
if f.Paths[0] != "/var/log/fail2ban.log" { | |
t.FailNow() | |
} | |
if f.Fields["type"] != "fail2ban" { | |
t.FailNow() | |
} | |
} | |
func chkerr(t *testing.T, err error) { | |
if err != nil { | |
t.Errorf("Error encountered: %s", err) | |
} | |
} | |
func makeTempDir(t *testing.T) (string) { | |
tmpdir, err := ioutil.TempDir("", "logstash-config-test") | |
chkerr(t, err) | |
return tmpdir | |
} | |
func rmTempDir(tmpdir string) { | |
_ = os.RemoveAll(tmpdir) | |
} | |
func TestDiscoverConfigs(t *testing.T) { | |
tmpdir := makeTempDir(t) | |
defer rmTempDir(tmpdir) | |
tmpfile1 := path.Join(tmpdir, "myfile1") | |
tmpfile2 := path.Join(tmpdir, "myfile2") | |
err := ioutil.WriteFile(tmpfile1, make([]byte, 0), 0644) | |
chkerr(t, err) | |
err = ioutil.WriteFile(tmpfile2, make([]byte, 0), 0644) | |
configs, err := DiscoverConfigs(tmpdir) | |
chkerr(t, err) | |
expected := []string{tmpfile1, tmpfile2} | |
if !reflect.DeepEqual(configs, expected) { | |
t.Fatalf("Expected to find %v, got %v instead", configs, expected) | |
} | |
configs, err = DiscoverConfigs(tmpfile1) | |
expected = []string{tmpfile1} | |
if !reflect.DeepEqual(configs, expected) { | |
t.Fatalf("Expected to find %v, got %v instead", configs, expected) | |
} | |
} | |
func TestLoadConfigAndStripComments(t *testing.T) { | |
configJson := ` | |
# A comment at the beginning of the line | |
{ | |
# A comment after some spaces | |
"network": { | |
"servers": [ "localhost:5043" ], | |
"ssl certificate": "./logstash-forwarder.crt", | |
"ssl key": "./logstash-forwarder.key", | |
"ssl ca": "./logstash-forwarder.crt", | |
"timeout": 20 | |
}, | |
# A comment in the middle of the JSON | |
"files": [ | |
{ | |
"paths": [ | |
"/var/log/messages" | |
], | |
"fields": { "type": "syslog" } | |
} | |
] | |
}` | |
tmpdir := makeTempDir(t) | |
defer rmTempDir(tmpdir) | |
configFile := path.Join(tmpdir, "myconfig") | |
err := ioutil.WriteFile(configFile, []byte(configJson), 0644) | |
chkerr(t, err) | |
config, err := LoadConfig(configFile) | |
if err != nil { | |
t.Fatalf("Error loading config file: %s", err) | |
} | |
expected := Config{ | |
Network: NetworkConfig{ | |
Servers: []string{"localhost:5043"}, | |
SSLCertificate: "./logstash-forwarder.crt", | |
SSLKey: "./logstash-forwarder.key", | |
SSLCA: "./logstash-forwarder.crt", | |
Timeout: 20, | |
}, | |
Files: []FileConfig{{ | |
Paths: []string{"/var/log/messages"}, | |
Fields: map[string]string{ "type": "syslog" }, | |
}}, | |
} | |
if !reflect.DeepEqual(config, expected) { | |
t.Fatalf("Expected %v, got %v from LoadConfig", expected, config) | |
} | |
} | |
func TestFinalizeConfig(t *testing.T) { | |
config := Config{} | |
FinalizeConfig(&config) | |
if config.Network.Timeout != DefaultTimeout { | |
t.Fatalf("Expected FinalizeConfig to default timeout to %d, got %d instead", DefaultTimeout, config.Network.Timeout) | |
} | |
config.Network.Timeout = 40 | |
expected := time.Duration(40) * time.Second | |
FinalizeConfig(&config) | |
if config.Network.timeout != expected { | |
t.Fatalf("Expected FinalizeConfig to set the timeout duration to %v, got %v instead", config.Network.timeout, expected) | |
} | |
} | |
func TestMergeConfig(t *testing.T) { | |
configA := Config{ | |
Network: NetworkConfig{ | |
Servers: []string{"localhost:5043"}, | |
SSLCertificate: "./logstash-forwarder.crt", | |
SSLKey: "./logstash-forwarder.key", | |
}, | |
Files: []FileConfig{{ | |
Paths: []string{"/var/log/messagesA"}, | |
}}, | |
} | |
configB := Config{ | |
Network: NetworkConfig{ | |
Servers: []string{"otherhost:5043"}, | |
SSLCA: "./logstash-forwarder.crt", | |
Timeout: 20, | |
}, | |
Files: []FileConfig{{ | |
Paths: []string{"/var/log/messagesB"}, | |
}}, | |
} | |
expected := Config{ | |
Network: NetworkConfig{ | |
Servers: []string{"localhost:5043", "otherhost:5043"}, | |
SSLCertificate: "./logstash-forwarder.crt", | |
SSLKey: "./logstash-forwarder.key", | |
SSLCA: "./logstash-forwarder.crt", | |
Timeout: 20, | |
}, | |
Files: []FileConfig{{ | |
Paths: []string{"/var/log/messagesA"}, | |
}, { | |
Paths: []string("/var/log/messagesB"), | |
}}, | |
} | |
err := MergeConfig(&configA, configB) | |
chkerr(t, err) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment