1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
12 "git.arvados.org/arvados.git/lib/controller/api"
13 "git.arvados.org/arvados.git/lib/ctrlctx"
14 "git.arvados.org/arvados.git/sdk/go/arvados"
15 "git.arvados.org/arvados.git/sdk/go/arvadostest"
16 "git.arvados.org/arvados.git/sdk/go/auth"
17 "github.com/jmoiron/sqlx"
18 check "gopkg.in/check.v1"
21 var _ = check.Suite(&activityPeriodSuite{})
23 type activityPeriodSuite struct{}
25 // The important thing is that, even when daylight savings time is
26 // making things difficult, the current period ends in the future.
27 func (*activityPeriodSuite) TestPeriod(c *check.C) {
28 toronto, err := time.LoadLocation("America/Toronto")
29 c.Assert(err, check.IsNil)
31 format := "2006-01-02 15:04:05 MST"
32 dststartday, err := time.ParseInLocation(format, "2022-03-13 00:00:00 EST", toronto)
33 c.Assert(err, check.IsNil)
34 dstendday, err := time.ParseInLocation(format, "2022-11-06 00:00:00 EDT", toronto)
35 c.Assert(err, check.IsNil)
37 for _, period := range []time.Duration{
44 for offset := time.Duration(0); offset < 48*time.Hour; offset += 3 * time.Minute {
45 t := dststartday.Add(offset)
46 end := alignedPeriod(t, period)
47 c.Check(end.After(t), check.Equals, true, check.Commentf("period %v offset %v", period, offset))
49 t = dstendday.Add(offset)
50 end = alignedPeriod(t, period)
51 c.Check(end.After(t), check.Equals, true, check.Commentf("period %v offset %v", period, offset))
56 func (s *CollectionSuite) TestLogActivity(c *check.C) {
57 starttime := time.Now()
58 s.localdb.activeUsersLock.Lock()
59 s.localdb.activeUsersReset = starttime
60 s.localdb.activeUsersLock.Unlock()
61 db := arvadostest.DB(c, s.cluster)
62 wrap := api.ComposeWrappers(
63 ctrlctx.WrapCallsInTransactions(func(ctx context.Context) (*sqlx.DB, error) { return db, nil }),
64 ctrlctx.WrapCallsWithAuth(s.cluster))
65 collectionCreate := wrap(func(ctx context.Context, opts interface{}) (interface{}, error) {
66 return s.localdb.CollectionCreate(ctx, opts.(arvados.CreateOptions))
68 ctx := auth.NewContext(context.Background(), &auth.Credentials{Tokens: []string{arvadostest.ActiveTokenV2}})
69 for i := 0; i < 2; i++ {
70 logthreshold := time.Now()
71 _, err := collectionCreate(ctx, arvados.CreateOptions{
72 Attrs: map[string]interface{}{
73 "name": "test collection",
75 EnsureUniqueName: true,
77 c.Assert(err, check.IsNil)
79 err = db.QueryRowContext(ctx, `select uuid from logs where object_uuid = $1 and event_at > $2`, arvadostest.ActiveUserUUID, logthreshold.UTC()).Scan(&uuid)
81 c.Check(err, check.IsNil)
82 c.Check(uuid, check.HasLen, 27)
84 c.Check(err, check.Equals, sql.ErrNoRows)