go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/gce/appengine/backend/internal/metrics/instances_test.go (about) 1 // Copyright 2019 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 metrics 16 17 import ( 18 "context" 19 "testing" 20 "time" 21 22 "go.chromium.org/luci/common/tsmon" 23 "go.chromium.org/luci/gae/impl/memory" 24 "go.chromium.org/luci/gae/service/datastore" 25 26 . "github.com/smartystreets/goconvey/convey" 27 ) 28 29 func TestInstances(t *testing.T) { 30 t.Parallel() 31 32 Convey("Instances", t, func() { 33 c, _ := tsmon.WithDummyInMemory(memory.Use(context.Background())) 34 datastore.GetTestable(c).AutoIndex(true) 35 datastore.GetTestable(c).Consistent(true) 36 s := tsmon.Store(c) 37 38 Convey("InstanceCount", func() { 39 ic := &InstanceCount{} 40 So(ic.Configured, ShouldBeEmpty) 41 So(ic.Created, ShouldBeEmpty) 42 So(ic.Connected, ShouldBeEmpty) 43 44 Convey("Configured", func() { 45 ic.AddConfigured(1, "project-1") 46 ic.AddConfigured(1, "project-2") 47 ic.AddConfigured(1, "project-1") 48 So(ic.Configured, ShouldHaveLength, 2) 49 So(ic.Configured, ShouldContain, configuredCount{ 50 Count: 2, 51 Project: "project-1", 52 }) 53 So(ic.Configured, ShouldContain, configuredCount{ 54 Count: 1, 55 Project: "project-2", 56 }) 57 }) 58 59 Convey("Created", func() { 60 ic.AddCreated(1, "project", "zone-1") 61 ic.AddCreated(1, "project", "zone-2") 62 ic.AddCreated(1, "project", "zone-1") 63 So(ic.Created, ShouldHaveLength, 2) 64 So(ic.Created, ShouldContain, createdCount{ 65 Count: 2, 66 Project: "project", 67 Zone: "zone-1", 68 }) 69 So(ic.Created, ShouldContain, createdCount{ 70 Count: 1, 71 Project: "project", 72 Zone: "zone-2", 73 }) 74 }) 75 76 Convey("Connected", func() { 77 ic.AddConnected(1, "project", "server-1", "zone") 78 ic.AddConnected(1, "project", "server-2", "zone") 79 ic.AddConnected(1, "project", "server-1", "zone") 80 So(ic.Connected, ShouldHaveLength, 2) 81 So(ic.Connected, ShouldContain, connectedCount{ 82 Count: 2, 83 Project: "project", 84 Server: "server-1", 85 Zone: "zone", 86 }) 87 So(ic.Connected, ShouldContain, connectedCount{ 88 Count: 1, 89 Project: "project", 90 Server: "server-2", 91 Zone: "zone", 92 }) 93 }) 94 95 Convey("Update", func() { 96 ic.AddConfigured(1, "project") 97 ic.AddCreated(1, "project", "zone") 98 ic.AddConnected(1, "project", "server", "zone") 99 So(ic.Update(c, "prefix"), ShouldBeNil) 100 ic = &InstanceCount{ 101 ID: "prefix", 102 } 103 So(datastore.Get(c, ic), ShouldBeNil) 104 So(ic.Configured, ShouldHaveLength, 1) 105 So(ic.Created, ShouldHaveLength, 1) 106 So(ic.Connected, ShouldHaveLength, 1) 107 So(ic.Prefix, ShouldEqual, ic.ID) 108 }) 109 }) 110 111 Convey("updateInstances", func() { 112 confFields := []any{"prefix", "project"} 113 creaFields1 := []any{"prefix", "project", "zone-1"} 114 creaFields2 := []any{"prefix", "project", "zone-2"} 115 connFields := []any{"prefix", "project", "server", "zone"} 116 117 ic := &InstanceCount{ 118 ID: "prefix", 119 Configured: []configuredCount{ 120 { 121 Count: 3, 122 Project: "project", 123 }, 124 }, 125 Created: []createdCount{ 126 { 127 Count: 2, 128 Project: "project", 129 Zone: "zone-1", 130 }, 131 { 132 Count: 1, 133 Project: "project", 134 Zone: "zone-2", 135 }, 136 }, 137 Connected: []connectedCount{ 138 { 139 Count: 1, 140 Project: "project", 141 Server: "server", 142 Zone: "zone", 143 }, 144 }, 145 Prefix: "prefix", 146 } 147 148 ic.Computed = time.Time{} 149 So(datastore.Put(c, ic), ShouldBeNil) 150 updateInstances(c) 151 So(s.Get(c, configuredInstances, time.Time{}, confFields), ShouldBeNil) 152 So(s.Get(c, createdInstances, time.Time{}, creaFields1), ShouldBeNil) 153 So(s.Get(c, createdInstances, time.Time{}, creaFields2), ShouldBeNil) 154 So(s.Get(c, connectedInstances, time.Time{}, connFields), ShouldBeNil) 155 So(datastore.Get(c, &InstanceCount{ 156 ID: ic.ID, 157 }), ShouldEqual, datastore.ErrNoSuchEntity) 158 159 ic.Computed = time.Now().UTC() 160 So(datastore.Put(c, ic), ShouldBeNil) 161 updateInstances(c) 162 So(s.Get(c, configuredInstances, time.Time{}, confFields).(int64), ShouldEqual, 3) 163 So(s.Get(c, createdInstances, time.Time{}, creaFields1).(int64), ShouldEqual, 2) 164 So(s.Get(c, createdInstances, time.Time{}, creaFields2).(int64), ShouldEqual, 1) 165 So(s.Get(c, connectedInstances, time.Time{}, connFields).(int64), ShouldEqual, 1) 166 So(datastore.Get(c, &InstanceCount{ 167 ID: ic.ID, 168 }), ShouldBeNil) 169 }) 170 }) 171 }