go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/common/tsmon/config_test.go (about) 1 // Copyright 2015 The LUCI Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package tsmon 16 17 import ( 18 "io/ioutil" 19 "os" 20 "testing" 21 22 . "github.com/smartystreets/goconvey/convey" 23 ) 24 25 func TestLoadConfig(t *testing.T) { 26 Convey("No file", t, func() { 27 c, err := loadConfig("") 28 So(c.Credentials, ShouldEqual, "") 29 So(c.Endpoint, ShouldEqual, "") 30 So(err, ShouldBeNil) 31 }) 32 33 Convey("Missing file", t, func() { 34 c, err := loadConfig("/does/not/exist") 35 So(c.Credentials, ShouldEqual, "") 36 So(c.Endpoint, ShouldEqual, "") 37 So(err, ShouldBeNil) 38 }) 39 40 Convey("Empty file", t, func() { 41 tf, err := ioutil.TempFile("", "config_test") 42 if err != nil { 43 t.Fail() 44 } 45 defer tf.Close() 46 defer os.Remove(tf.Name()) 47 48 c, err := loadConfig(tf.Name()) 49 So(c.Endpoint, ShouldEqual, "") 50 So(c.Credentials, ShouldEqual, "") 51 So(c.AutoGenHostname, ShouldEqual, false) 52 So(c.Hostname, ShouldEqual, "") 53 So(c.Region, ShouldEqual, "") 54 So(err, ShouldNotBeNil) 55 }) 56 57 Convey("Full file", t, func() { 58 tf, err := ioutil.TempFile("", "config_test") 59 if err != nil { 60 t.Fail() 61 } 62 defer tf.Close() 63 defer os.Remove(tf.Name()) 64 65 tf.WriteString(` 66 {"endpoint": "foo", 67 "credentials": "bar", 68 "autogen_hostname": true, 69 "hostname": "test_host", 70 "region": "test_region" 71 }`) 72 tf.Sync() 73 74 c, err := loadConfig(tf.Name()) 75 So(c.Endpoint, ShouldEqual, "foo") 76 So(c.Credentials, ShouldEqual, "bar") 77 So(c.AutoGenHostname, ShouldEqual, true) 78 So(c.Hostname, ShouldEqual, "test_host") 79 So(c.Region, ShouldEqual, "test_region") 80 So(err, ShouldBeNil) 81 }) 82 }