Skip to content

Commit

Permalink
Add Unit Tests to server
Browse files Browse the repository at this point in the history
Coverage of ~12% since most is network communication
  • Loading branch information
mulbc committed Aug 20, 2019
1 parent 8af3235 commit 76c4a57
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 12 deletions.
24 changes: 12 additions & 12 deletions server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,33 +22,33 @@ func init() {
FullTimestamp: true,
})
rand.Seed(time.Now().UnixNano())
}

var configFileLocation string
var readyWorkers chan *net.Conn

func loadConfigFromFile() common.Testconf {
flag.StringVar(&configFileLocation, "c", "", "Config file describing test run")
flag.Parse()
if configFileLocation == "" {
// Only demand this flag if we are not running go test
if configFileLocation == "" && flag.Lookup("test.v") == nil {
log.Fatal("-c is a mandatory parameter - please specify the config file")
}
}

configFileContent, err := ioutil.ReadFile(configFileLocation)
if err != nil {
log.WithError(err).Fatalf("Error reading config file:")
}
var configFileLocation string
var readyWorkers chan *net.Conn

func loadConfigFromFile(configFileContent []byte) common.Testconf {
var config common.Testconf
err = yaml.Unmarshal(configFileContent, &config)
err := yaml.Unmarshal(configFileContent, &config)
if err != nil {
log.WithError(err).Fatalf("Error unmarshaling config file:")
}
return config
}

func main() {
config := loadConfigFromFile()
configFileContent, err := ioutil.ReadFile(configFileLocation)
if err != nil {
log.WithError(err).Fatalf("Error reading config file:")
}
config := loadConfigFromFile(configFileContent)
common.CheckConfig(config)

readyWorkers = make(chan *net.Conn)
Expand Down
46 changes: 46 additions & 0 deletions server/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package main

import (
"reflect"
"testing"

"github.com/mulbc/gosbench/common"
)

func Test_loadConfigFromFile(t *testing.T) {
type args struct {
configFileContent []byte
}
tests := []struct {
name string
args args
want common.Testconf
}{
{"empty file", args{[]byte{}}, common.Testconf{}},
// TODO discover how to handle log.Fatal with logrus here
// https://github.com/sirupsen/logrus#fatal-handlers
// {"unparsable", args{[]byte(`corrupt!`)}, common.Testconf{}},
{"S3Config", args{[]byte(`s3_config:
- access_key: secretKey
secret_key: secretSecret
endpoint: test`)}, common.Testconf{
S3Config: []*common.S3Configuration{&common.S3Configuration{
Endpoint: "test",
AccessKey: "secretKey",
SecretKey: "secretSecret",
}},
}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Log("Recovered in f", r)
}
}()
if got := loadConfigFromFile(tt.args.configFileContent); !reflect.DeepEqual(got, tt.want) {
t.Errorf("loadConfigFromFile() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 76c4a57

Please sign in to comment.