| 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/api/logdog_coordinator/services/v1" |
| 18 "github.com/luci/luci-go/common/config/impl/remote" |
| 19 "github.com/luci/luci-go/common/proto/logdog/svcconfig" |
| 20 "golang.org/x/net/context" |
| 21 "google.golang.org/grpc" |
| 22 |
| 23 . "github.com/luci/luci-go/common/testing/assertions" |
| 24 . "github.com/smartystreets/goconvey/convey" |
| 25 ) |
| 26 |
| 27 type testServicesClient struct { |
| 28 services.ServicesClient |
| 29 cfg services.GetConfigResponse |
| 30 err error |
| 31 } |
| 32 |
| 33 func (tsc *testServicesClient) GetConfig(context.Context, *services.Void, ...grp
c.CallOption) ( |
| 34 *services.GetConfigResponse, error) { |
| 35 if tsc.err != nil { |
| 36 return nil, tsc.err |
| 37 } |
| 38 return &tsc.cfg, nil |
| 39 } |
| 40 |
| 41 func TestFlag(t *testing.T) { |
| 42 Convey(`A set of config Flags`, t, func() { |
| 43 c := context.Background() |
| 44 |
| 45 f := Flags{} |
| 46 |
| 47 Convey(`Can parse command-line options.`, func() { |
| 48 fs := flag.NewFlagSet("test", flag.PanicOnError) |
| 49 f.AddToFlagSet(fs) |
| 50 |
| 51 So(fs.Parse([]string{ |
| 52 "-config-file-path", "test.cfg", |
| 53 "-config-kill-interval", "3m", |
| 54 }), ShouldBeNil) |
| 55 So(f.ConfigFilePath, ShouldEqual, "test.cfg") |
| 56 So(f.KillCheckInterval, ShouldEqual, 3*time.Minute) |
| 57 }) |
| 58 |
| 59 Convey(`Using a Coordinator client stub`, func() { |
| 60 tsc := testServicesClient{} |
| 61 tsc.cfg.ConfigServiceUrl = "http://example.com" |
| 62 tsc.cfg.ConfigSet = "my/config/set" |
| 63 tsc.cfg.ConfigPath = "configpath.cfg" |
| 64 |
| 65 Convey(`Will load configuration from luci-config by defa
ult.`, func() { |
| 66 o, err := f.CoordinatorOptions(c, &tsc) |
| 67 So(err, ShouldBeNil) |
| 68 So(o.Config, ShouldHaveSameTypeAs, remote.New(c,
"")) |
| 69 }) |
| 70 |
| 71 Convey(`Will fail to create Options if no config service
is specified.`, func() { |
| 72 tsc.cfg.ConfigServiceUrl = "" |
| 73 |
| 74 _, err := f.CoordinatorOptions(c, &tsc) |
| 75 So(err, ShouldErrLike, "coordinator does not spe
cify a config service") |
| 76 }) |
| 77 |
| 78 Convey(`Will fail to create Options if no config set is
specified.`, func() { |
| 79 tsc.cfg.ConfigSet = "" |
| 80 |
| 81 _, err := f.CoordinatorOptions(c, &tsc) |
| 82 So(err, ShouldErrLike, "coordinator does not spe
cify a config set") |
| 83 }) |
| 84 |
| 85 Convey(`Will fail to create Options if no config path is
specified.`, func() { |
| 86 tsc.cfg.ConfigPath = "" |
| 87 |
| 88 _, err := f.CoordinatorOptions(c, &tsc) |
| 89 So(err, ShouldErrLike, "coordinator does not spe
cify a config path") |
| 90 }) |
| 91 |
| 92 Convey(`When loading from a testing file using a Coordin
ator stub.`, func() { |
| 93 tdir, err := ioutil.TempDir("", "configTest") |
| 94 So(err, ShouldBeNil) |
| 95 defer os.RemoveAll(tdir) |
| 96 |
| 97 f.ConfigFilePath = filepath.Join(tdir, "test_con
fig.proto.txt") |
| 98 writeConfig := func(cfg *svcconfig.Config) error
{ |
| 99 fd, err := os.Create(f.ConfigFilePath) |
| 100 if err != nil { |
| 101 return err |
| 102 } |
| 103 defer fd.Close() |
| 104 |
| 105 return proto.MarshalText(fd, cfg) |
| 106 } |
| 107 |
| 108 cfg := &svcconfig.Config{ |
| 109 Transport: &svcconfig.Transport{ |
| 110 Type: &svcconfig.Transport_Pubsu
b{ |
| 111 Pubsub: &svcconfig.Trans
port_PubSub{ |
| 112 Project: "f
oo", |
| 113 Topic: "b
ar", |
| 114 Subscription: "b
az", |
| 115 }, |
| 116 }, |
| 117 }, |
| 118 } |
| 119 So(writeConfig(cfg), ShouldBeNil) |
| 120 |
| 121 Convey(`Will fail to load Options if the Coordin
ator config could not be fetched.`, func() { |
| 122 tsc := testServicesClient{err: errors.Ne
w("test error")} |
| 123 _, err := f.CoordinatorOptions(c, &tsc) |
| 124 So(err, ShouldErrLike, "test error") |
| 125 }) |
| 126 |
| 127 Convey(`Will load options from the configuration
file.`, func() { |
| 128 o, err := f.CoordinatorOptions(c, &tsc) |
| 129 So(err, ShouldBeNil) |
| 130 So(o, ShouldNotBeNil) |
| 131 |
| 132 Convey(`Can load configuration from the
file.`, func() { |
| 133 m, err := NewManager(c, *o) |
| 134 So(err, ShouldBeNil) |
| 135 So(m.Config(), ShouldResembleV,
cfg) |
| 136 }) |
| 137 }) |
| 138 }) |
| 139 }) |
| 140 }) |
| 141 } |
| OLD | NEW |