| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package config |
| 6 |
| 7 import ( |
| 8 "errors" |
| 9 "flag" |
| 10 "io/ioutil" |
| 11 "os" |
| 12 "path/filepath" |
| 13 "testing" |
| 14 "time" |
| 15 |
| 16 "github.com/golang/protobuf/proto" |
| 17 "github.com/luci/luci-go/common/config/impl/remote" |
| 18 "github.com/luci/luci-go/common/proto/logdog/svcconfig" |
| 19 cc "github.com/luci/luci-go/server/internal/logdog/coordinatorClient" |
| 20 "golang.org/x/net/context" |
| 21 |
| 22 . "github.com/luci/luci-go/common/testing/assertions" |
| 23 . "github.com/smartystreets/goconvey/convey" |
| 24 ) |
| 25 |
| 26 type testCoordinatorConfigGetter struct { |
| 27 cfg cc.ServiceConfig |
| 28 err error |
| 29 } |
| 30 |
| 31 func (cg *testCoordinatorConfigGetter) GetConfig(context.Context) (*cc.ServiceCo
nfig, error) { |
| 32 if cg.err != nil { |
| 33 return nil, cg.err |
| 34 } |
| 35 return &cg.cfg, nil |
| 36 } |
| 37 |
| 38 func TestFlag(t *testing.T) { |
| 39 Convey(`A set of config Flags`, t, func() { |
| 40 c := context.Background() |
| 41 |
| 42 f := Flags{} |
| 43 |
| 44 Convey(`Can parse command-line options.`, func() { |
| 45 fs := flag.NewFlagSet("test", flag.PanicOnError) |
| 46 f.AddToFlagSet(fs) |
| 47 |
| 48 So(fs.Parse([]string{ |
| 49 "-config-file-path", "test.cfg", |
| 50 "-config-kill-interval", "3m", |
| 51 }), ShouldBeNil) |
| 52 So(f.ConfigFilePath, ShouldEqual, "test.cfg") |
| 53 So(f.KillCheckInterval, ShouldEqual, 3*time.Minute) |
| 54 }) |
| 55 |
| 56 Convey(`Using a Coordinator client stub`, func() { |
| 57 cg := testCoordinatorConfigGetter{} |
| 58 cg.cfg.ConfigService = "http://example.com" |
| 59 cg.cfg.ConfigSet = "my/config/set" |
| 60 cg.cfg.ConfigPath = "configpath.cfg" |
| 61 |
| 62 Convey(`Will load configuration from luci-config by defa
ult.`, func() { |
| 63 o, err := f.CoordinatorOptions(c, &cg) |
| 64 So(err, ShouldBeNil) |
| 65 So(o.Config, ShouldHaveSameTypeAs, remote.New(c,
"")) |
| 66 }) |
| 67 |
| 68 Convey(`Will fail to create Options if no config service
is specified.`, func() { |
| 69 cg.cfg.ConfigService = "" |
| 70 |
| 71 _, err := f.CoordinatorOptions(c, &cg) |
| 72 So(err, ShouldErrLike, "coordinator does not spe
cify a config service") |
| 73 }) |
| 74 |
| 75 Convey(`Will fail to create Options if no config set is
specified.`, func() { |
| 76 cg.cfg.ConfigSet = "" |
| 77 |
| 78 _, err := f.CoordinatorOptions(c, &cg) |
| 79 So(err, ShouldErrLike, "coordinator does not spe
cify a config set") |
| 80 }) |
| 81 |
| 82 Convey(`Will fail to create Options if no config path is
specified.`, func() { |
| 83 cg.cfg.ConfigPath = "" |
| 84 |
| 85 _, err := f.CoordinatorOptions(c, &cg) |
| 86 So(err, ShouldErrLike, "coordinator does not spe
cify a config path") |
| 87 }) |
| 88 |
| 89 Convey(`When loading from a testing file using a Coordin
ator stub.`, func() { |
| 90 tdir, err := ioutil.TempDir("", "configTest") |
| 91 So(err, ShouldBeNil) |
| 92 defer os.RemoveAll(tdir) |
| 93 |
| 94 f.ConfigFilePath = filepath.Join(tdir, "test_con
fig.proto.txt") |
| 95 writeConfig := func(cfg *svcconfig.Config) error
{ |
| 96 fd, err := os.Create(f.ConfigFilePath) |
| 97 if err != nil { |
| 98 return err |
| 99 } |
| 100 defer fd.Close() |
| 101 |
| 102 return proto.MarshalText(fd, cfg) |
| 103 } |
| 104 |
| 105 cfg := &svcconfig.Config{ |
| 106 Transport: &svcconfig.Transport{ |
| 107 Type: &svcconfig.Transport_Pubsu
b{ |
| 108 Pubsub: &svcconfig.Trans
port_PubSub{ |
| 109 Project: "f
oo", |
| 110 Topic: "b
ar", |
| 111 Subscription: "b
az", |
| 112 }, |
| 113 }, |
| 114 }, |
| 115 } |
| 116 So(writeConfig(cfg), ShouldBeNil) |
| 117 |
| 118 Convey(`Will fail to load Options if the Coordin
ator config could not be fetched.`, func() { |
| 119 cg := testCoordinatorConfigGetter{err: e
rrors.New("test error")} |
| 120 _, err := f.CoordinatorOptions(c, &cg) |
| 121 So(err, ShouldErrLike, "test error") |
| 122 }) |
| 123 |
| 124 Convey(`Will load options from the configuration
file.`, func() { |
| 125 o, err := f.CoordinatorOptions(c, &cg) |
| 126 So(err, ShouldBeNil) |
| 127 So(o, ShouldNotBeNil) |
| 128 |
| 129 Convey(`Can load configuration from the
file.`, func() { |
| 130 m, err := NewManager(c, *o) |
| 131 So(err, ShouldBeNil) |
| 132 So(m.Config(), ShouldResembleV,
cfg) |
| 133 }) |
| 134 }) |
| 135 }) |
| 136 }) |
| 137 }) |
| 138 } |
| OLD | NEW |