go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/gce/appengine/rpc/memory/config_test.go (about) 1 // Copyright 2018 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 memory 16 17 import ( 18 "context" 19 "testing" 20 21 "google.golang.org/protobuf/types/known/emptypb" 22 23 "go.chromium.org/luci/gce/api/config/v1" 24 25 . "github.com/smartystreets/goconvey/convey" 26 27 . "go.chromium.org/luci/common/testing/assertions" 28 ) 29 30 func TestConfig(t *testing.T) { 31 t.Parallel() 32 33 Convey("Delete", t, func() { 34 c := context.Background() 35 srv := &Config{} 36 37 Convey("nil", func() { 38 cfg, err := srv.Delete(c, nil) 39 So(err, ShouldBeNil) 40 So(cfg, ShouldResemble, &emptypb.Empty{}) 41 }) 42 43 Convey("empty", func() { 44 cfg, err := srv.Delete(c, &config.DeleteRequest{}) 45 So(err, ShouldBeNil) 46 So(cfg, ShouldResemble, &emptypb.Empty{}) 47 }) 48 49 Convey("ID", func() { 50 cfg, err := srv.Delete(c, &config.DeleteRequest{ 51 Id: "id", 52 }) 53 So(err, ShouldBeNil) 54 So(cfg, ShouldResemble, &emptypb.Empty{}) 55 }) 56 57 Convey("deleted", func() { 58 srv.cfg.Store("id", &config.Config{}) 59 cfg, err := srv.Delete(c, &config.DeleteRequest{ 60 Id: "id", 61 }) 62 So(err, ShouldBeNil) 63 So(cfg, ShouldResemble, &emptypb.Empty{}) 64 _, ok := srv.cfg.Load("id") 65 So(ok, ShouldBeFalse) 66 }) 67 }) 68 69 Convey("Ensure", t, func() { 70 c := context.Background() 71 srv := &Config{} 72 73 Convey("nil", func() { 74 cfg, err := srv.Ensure(c, nil) 75 So(err, ShouldBeNil) 76 So(cfg, ShouldResemble, (*config.Config)(nil)) 77 _, ok := srv.cfg.Load("") 78 So(ok, ShouldBeTrue) 79 }) 80 81 Convey("empty", func() { 82 cfg, err := srv.Ensure(c, &config.EnsureRequest{}) 83 So(err, ShouldBeNil) 84 So(cfg, ShouldResemble, (*config.Config)(nil)) 85 _, ok := srv.cfg.Load("") 86 So(ok, ShouldBeTrue) 87 }) 88 89 Convey("ID", func() { 90 cfg, err := srv.Ensure(c, &config.EnsureRequest{ 91 Id: "id", 92 }) 93 So(err, ShouldBeNil) 94 So(cfg, ShouldResemble, (*config.Config)(nil)) 95 _, ok := srv.cfg.Load("id") 96 So(ok, ShouldBeTrue) 97 }) 98 99 Convey("config", func() { 100 cfg, err := srv.Ensure(c, &config.EnsureRequest{ 101 Id: "id", 102 Config: &config.Config{ 103 Prefix: "prefix", 104 }, 105 }) 106 So(err, ShouldBeNil) 107 So(cfg, ShouldResemble, &config.Config{ 108 Prefix: "prefix", 109 }) 110 _, ok := srv.cfg.Load("id") 111 So(ok, ShouldBeTrue) 112 }) 113 }) 114 115 Convey("Get", t, func() { 116 c := context.Background() 117 srv := &Config{} 118 119 Convey("not found", func() { 120 Convey("nil", func() { 121 cfg, err := srv.Get(c, nil) 122 So(err, ShouldErrLike, "no config found") 123 So(cfg, ShouldBeNil) 124 }) 125 126 Convey("empty", func() { 127 cfg, err := srv.Get(c, &config.GetRequest{}) 128 So(err, ShouldErrLike, "no config found") 129 So(cfg, ShouldBeNil) 130 }) 131 132 Convey("ID", func() { 133 cfg, err := srv.Get(c, &config.GetRequest{ 134 Id: "id", 135 }) 136 So(err, ShouldErrLike, "no config found") 137 So(cfg, ShouldBeNil) 138 }) 139 }) 140 141 Convey("found", func() { 142 srv.cfg.Store("id", &config.Config{ 143 Prefix: "prefix", 144 }) 145 cfg, err := srv.Get(c, &config.GetRequest{ 146 Id: "id", 147 }) 148 So(err, ShouldBeNil) 149 So(cfg, ShouldResemble, &config.Config{ 150 Prefix: "prefix", 151 }) 152 }) 153 }) 154 }