Chromium Code Reviews| Index: server/internal/logdog/config/flag_test.go |
| diff --git a/server/internal/logdog/config/flag_test.go b/server/internal/logdog/config/flag_test.go |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..af30105b7bb1339ddf5f46779a8b07fb727f9e20 |
| --- /dev/null |
| +++ b/server/internal/logdog/config/flag_test.go |
| @@ -0,0 +1,138 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package config |
| + |
| +import ( |
| + "errors" |
| + "flag" |
| + "io/ioutil" |
| + "os" |
| + "path/filepath" |
| + "testing" |
| + "time" |
| + |
| + "github.com/golang/protobuf/proto" |
| + "github.com/luci/luci-go/common/config/impl/remote" |
| + "github.com/luci/luci-go/common/proto/logdog/svcconfig" |
| + cc "github.com/luci/luci-go/server/internal/logdog/coordinatorClient" |
| + "golang.org/x/net/context" |
| + |
| + . "github.com/luci/luci-go/common/testing/assertions" |
| + . "github.com/smartystreets/goconvey/convey" |
| +) |
| + |
| +type testCoordinatorConfigGetter struct { |
| + cfg cc.ServiceConfig |
| + err error |
| +} |
| + |
| +func (cg *testCoordinatorConfigGetter) GetConfig(context.Context) (*cc.ServiceConfig, error) { |
| + if cg.err != nil { |
| + return nil, cg.err |
| + } |
| + return &cg.cfg, nil |
| +} |
| + |
| +func TestFlag(t *testing.T) { |
| + Convey(`A set of config Flags`, t, func() { |
| + c := context.Background() |
| + |
| + f := Flags{} |
| + |
| + Convey(`Can parse command-line options.`, func() { |
| + fs := flag.NewFlagSet("test", flag.PanicOnError) |
| + f.AddToFlagSet(fs) |
| + |
| + So(fs.Parse([]string{ |
| + "-config-file-path", "test.cfg", |
| + "-config-kill-interval", "3m", |
| + }), ShouldBeNil) |
| + So(f.ConfigFilePath, ShouldEqual, "test.cfg") |
| + So(f.KillCheckInterval, ShouldEqual, 3*time.Minute) |
| + }) |
| + |
| + Convey(`Using a Coordinator client stub`, func() { |
| + cg := testCoordinatorConfigGetter{} |
| + cg.cfg.ConfigService = "http://example.com" |
| + cg.cfg.ConfigSet = "my/config/set" |
| + cg.cfg.ConfigPath = "configpath.cfg" |
| + |
| + Convey(`Will load configuration from luci-config by default.`, func() { |
| + o, err := f.CoordinatorOptions(c, &cg) |
| + So(err, ShouldBeNil) |
| + So(o.Config, ShouldHaveSameTypeAs, remote.New(c, "")) |
|
martiniss
2016/01/26 02:24:18
Umm.. What kind of type are we concerned with here
dnj (Google)
2016/01/26 05:06:05
remoteImpl, I suppose. It's kind of a silly test,
|
| + }) |
| + |
| + Convey(`Will fail to create Options if no config service is specified.`, func() { |
|
martiniss
2016/01/26 02:24:18
These tests seem a bit too specific. I mean, I und
dnj (Google)
2016/01/26 05:06:05
They aren't really that important, but hurray cove
|
| + cg.cfg.ConfigService = "" |
| + |
| + _, err := f.CoordinatorOptions(c, &cg) |
| + So(err, ShouldErrLike, "coordinator does not specify a config service") |
| + }) |
| + |
| + Convey(`Will fail to create Options if no config set is specified.`, func() { |
| + cg.cfg.ConfigSet = "" |
| + |
| + _, err := f.CoordinatorOptions(c, &cg) |
| + So(err, ShouldErrLike, "coordinator does not specify a config set") |
| + }) |
| + |
| + Convey(`Will fail to create Options if no config path is specified.`, func() { |
| + cg.cfg.ConfigPath = "" |
| + |
| + _, err := f.CoordinatorOptions(c, &cg) |
| + So(err, ShouldErrLike, "coordinator does not specify a config path") |
| + }) |
| + |
| + Convey(`When loading from a testing file using a Coordinator stub.`, func() { |
| + tdir, err := ioutil.TempDir("", "configTest") |
| + So(err, ShouldBeNil) |
| + defer os.RemoveAll(tdir) |
| + |
| + f.ConfigFilePath = filepath.Join(tdir, "test_config.proto.txt") |
| + writeConfig := func(cfg *svcconfig.Config) error { |
| + fd, err := os.Create(f.ConfigFilePath) |
| + if err != nil { |
| + return err |
| + } |
| + defer fd.Close() |
| + |
| + return proto.MarshalText(fd, cfg) |
| + } |
| + |
| + cfg := &svcconfig.Config{ |
| + Transport: &svcconfig.Transport{ |
| + Type: &svcconfig.Transport_Pubsub{ |
| + Pubsub: &svcconfig.Transport_PubSub{ |
| + Project: "foo", |
| + Topic: "bar", |
| + Subscription: "baz", |
| + }, |
| + }, |
| + }, |
| + } |
| + So(writeConfig(cfg), ShouldBeNil) |
| + |
| + Convey(`Will fail to load Options if the Coordinator config could not be fetched.`, func() { |
| + cg := testCoordinatorConfigGetter{err: errors.New("test error")} |
| + _, err := f.CoordinatorOptions(c, &cg) |
| + So(err, ShouldErrLike, "test error") |
| + }) |
| + |
| + Convey(`Will load options from the configuration file.`, func() { |
| + o, err := f.CoordinatorOptions(c, &cg) |
| + So(err, ShouldBeNil) |
| + So(o, ShouldNotBeNil) |
| + |
| + Convey(`Can load configuration from the file.`, func() { |
| + m, err := NewManager(c, *o) |
| + So(err, ShouldBeNil) |
| + So(m.Config(), ShouldResembleV, cfg) |
| + }) |
| + }) |
| + }) |
| + }) |
| + }) |
| +} |