Unit test unknown value in config

This commit is contained in:
Nick Sardo 2017-07-26 12:22:37 -07:00
parent cde038b9be
commit 3f01685943
2 changed files with 29 additions and 3 deletions

View File

@ -198,11 +198,11 @@ func newGCECloud(config io.Reader) (*GCECloud, error) {
var nodeTags []string
var nodeInstancePrefix string
if config != nil {
var cfg Config
if err := gcfg.FatalOnly(gcfg.ReadInto(&cfg, config)); err != nil {
glog.Errorf("Couldn't read config: %v", err)
cfg, err := readConfig(config)
if err != nil {
return nil, err
}
glog.Infof("Using GCE provider config %+v", cfg)
if cfg.Global.ApiEndpoint != "" {
apiEndpoint = cfg.Global.ApiEndpoint
@ -241,6 +241,15 @@ func newGCECloud(config io.Reader) (*GCECloud, error) {
nodeTags, nodeInstancePrefix, tokenSource, true /* useMetadataServer */)
}
func readConfig(reader io.Reader) (*Config, error) {
cfg := &Config{}
if err := gcfg.FatalOnly(gcfg.ReadInto(cfg, reader)); err != nil {
glog.Errorf("Couldn't read config: %v", err)
return nil, err
}
return cfg, nil
}
// Creates a GCECloud object using the specified parameters.
// If no networkUrl is specified, loads networkName via rest call.
// If no tokenSource is specified, uses oauth2.DefaultTokenSource.

View File

@ -18,9 +18,26 @@ package gce
import (
"reflect"
"strings"
"testing"
)
func TestExtraKeyInConfig(t *testing.T) {
const s = `[Global]
project-id = my-project
unknown-key = abc
network-name = my-network
`
reader := strings.NewReader(s)
config, err := readConfig(reader)
if err != nil {
t.Fatalf("Unexpected config parsing error %v", err)
}
if config.Global.ProjectID != "my-project" || config.Global.NetworkName != "my-network" {
t.Fatalf("Expected config values to continue to be read despite extra key-value pair.")
}
}
func TestGetRegion(t *testing.T) {
zoneName := "us-central1-b"
regionName, err := GetGCERegion(zoneName)