go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/analysis/internal/metrics/cron_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 metrics
    16  
    17  import (
    18  	"testing"
    19  	"time"
    20  
    21  	"go.chromium.org/luci/gae/impl/memory"
    22  
    23  	"go.chromium.org/luci/analysis/internal/clustering/rules"
    24  	"go.chromium.org/luci/analysis/internal/config"
    25  	"go.chromium.org/luci/analysis/internal/ingestion/control"
    26  	"go.chromium.org/luci/analysis/internal/testutil"
    27  	configpb "go.chromium.org/luci/analysis/proto/config"
    28  
    29  	. "github.com/smartystreets/goconvey/convey"
    30  )
    31  
    32  func TestGlobalMetrics(t *testing.T) {
    33  	Convey(`With Spanner Test Database`, t, func() {
    34  		ctx := testutil.IntegrationTestContext(t)
    35  
    36  		ctx = memory.Use(ctx) // For project config in datastore.
    37  
    38  		// Setup project configuration.
    39  		projectCfgs := map[string]*configpb.ProjectConfig{
    40  			"project-a": {},
    41  			"project-b": {},
    42  		}
    43  		So(config.SetTestProjectConfig(ctx, projectCfgs), ShouldBeNil)
    44  
    45  		// Create some active rules.
    46  		rulesToCreate := []*rules.Entry{
    47  			rules.NewRule(0).WithProject("project-a").WithActive(true).Build(),
    48  			rules.NewRule(1).WithProject("project-a").WithActive(true).Build(),
    49  		}
    50  		err := rules.SetForTesting(ctx, rulesToCreate)
    51  		So(err, ShouldBeNil)
    52  
    53  		// Create some ingestion control records.
    54  		reference := time.Now().Add(-1 * time.Minute)
    55  		entriesToCreate := []*control.Entry{
    56  			control.NewEntry(0).
    57  				WithBuildProject("project-a").
    58  				WithPresubmitProject("project-b").
    59  				WithInvocationProject("project-c").
    60  				WithBuildJoinedTime(reference).
    61  				WithPresubmitJoinedTime(reference).
    62  				WithInvocationJoinedTime(reference).
    63  				Build(),
    64  			control.NewEntry(1).
    65  				WithBuildProject("project-d").
    66  				WithBuildJoinedTime(reference).
    67  				WithInvocationResult(nil).
    68  				WithPresubmitResult(nil).Build(),
    69  			control.NewEntry(2).
    70  				WithPresubmitProject("project-e").
    71  				WithPresubmitJoinedTime(reference).
    72  				WithBuildResult(nil).
    73  				WithInvocationResult(nil).Build(),
    74  			control.NewEntry(3).
    75  				WithInvocationProject("project-f").
    76  				WithInvocationJoinedTime(reference).
    77  				WithBuildResult(nil).
    78  				WithPresubmitResult(nil).Build(),
    79  		}
    80  		_, err = control.SetEntriesForTesting(ctx, entriesToCreate...)
    81  		So(err, ShouldBeNil)
    82  
    83  		err = GlobalMetrics(ctx)
    84  		So(err, ShouldBeNil)
    85  	})
    86  }