go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/deploy/service/model/cleanup_test.go (about) 1 // Copyright 2022 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 model 16 17 import ( 18 "context" 19 "testing" 20 "time" 21 22 "go.chromium.org/luci/common/clock" 23 "go.chromium.org/luci/common/clock/testclock" 24 "go.chromium.org/luci/gae/impl/memory" 25 "go.chromium.org/luci/gae/service/datastore" 26 27 . "github.com/smartystreets/goconvey/convey" 28 ) 29 30 func TestCleanupOldEntities(t *testing.T) { 31 t.Parallel() 32 33 Convey("With datastore", t, func() { 34 now := testclock.TestRecentTimeUTC.Round(time.Millisecond) 35 ctx, _ := testclock.UseTime(context.Background(), now) 36 ctx = memory.Use(ctx) 37 38 datastore.GetTestable(ctx).AutoIndex(true) 39 datastore.GetTestable(ctx).Consistent(true) 40 41 Convey("Cleanup works", func() { 42 oldActuation := &Actuation{ 43 ID: "old-actuation", 44 Created: clock.Now(ctx).Add(-retentionPeriod - time.Hour).UTC(), 45 } 46 newActuation := &Actuation{ 47 ID: "new-actuation", 48 Created: clock.Now(ctx).Add(-retentionPeriod + time.Hour).UTC(), 49 } 50 oldEntry := &AssetHistory{ 51 ID: 1, 52 Parent: datastore.NewKey(ctx, "Asset", "old-asset", 0, nil), 53 Created: clock.Now(ctx).Add(-retentionPeriod - time.Hour).UTC(), 54 } 55 newEntry := &AssetHistory{ 56 ID: 1, 57 Parent: datastore.NewKey(ctx, "Asset", "new-asset", 0, nil), 58 Created: clock.Now(ctx).Add(-retentionPeriod + time.Hour).UTC(), 59 } 60 So(datastore.Put(ctx, oldActuation, newActuation, oldEntry, newEntry), ShouldBeNil) 61 62 So(CleanupOldEntities(ctx), ShouldBeNil) 63 64 So(datastore.Get(ctx, oldActuation), ShouldEqual, datastore.ErrNoSuchEntity) 65 So(datastore.Get(ctx, newActuation), ShouldBeNil) 66 So(datastore.Get(ctx, oldEntry), ShouldEqual, datastore.ErrNoSuchEntity) 67 So(datastore.Get(ctx, newEntry), ShouldBeNil) 68 }) 69 }) 70 }