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