@@ -30,6 +30,7 @@ import (
3030 statepb "github.com/GoogleCloudPlatform/testgrid/pb/state"
3131 teststatuspb "github.com/GoogleCloudPlatform/testgrid/pb/test_status"
3232 "github.com/GoogleCloudPlatform/testgrid/pkg/updater"
33+ durationpb "github.com/golang/protobuf/ptypes/duration"
3334 timestamppb "github.com/golang/protobuf/ptypes/timestamp"
3435 "github.com/google/go-cmp/cmp"
3536 "github.com/google/go-cmp/cmp/cmpopts"
@@ -3372,3 +3373,170 @@ func TestCompileHeaders(t *testing.T) {
33723373 })
33733374 }
33743375}
3376+
3377+ func TestCalculateMetrics (t * testing.T ) {
3378+ cases := []struct {
3379+ name string
3380+ sar * singleActionResult
3381+ want map [string ]float64
3382+ }{
3383+ {
3384+ name : "no numeric properties, no duration" ,
3385+ sar : & singleActionResult {
3386+ ActionProto : & resultstore.Action {
3387+ ActionType : & resultstore.Action_TestAction {
3388+ TestAction : & resultstore.TestAction {
3389+ TestSuite : & resultstore.TestSuite {
3390+ Properties : []* resultstore.Property {
3391+ {Key : "marco" , Value : "polo" },
3392+ },
3393+ },
3394+ },
3395+ },
3396+ },
3397+ },
3398+ want : map [string ]float64 {},
3399+ },
3400+ {
3401+ name : "no numeric properties, suite duration" ,
3402+ sar : & singleActionResult {
3403+ ActionProto : & resultstore.Action {
3404+ ActionType : & resultstore.Action_TestAction {
3405+ TestAction : & resultstore.TestAction {
3406+ TestSuite : & resultstore.TestSuite {
3407+ Properties : []* resultstore.Property {
3408+ {Key : "marco" , Value : "polo" },
3409+ },
3410+ Timing : & resultstore.Timing {
3411+ Duration : & durationpb.Duration {
3412+ Seconds : 120 ,
3413+ },
3414+ },
3415+ },
3416+ },
3417+ },
3418+ },
3419+ },
3420+ want : map [string ]float64 {
3421+ updater .TestMethodsElapsedKey : 2 ,
3422+ },
3423+ },
3424+ {
3425+ name : "no numeric properties, cases duration only" ,
3426+ sar : & singleActionResult {
3427+ ActionProto : & resultstore.Action {
3428+ ActionType : & resultstore.Action_TestAction {
3429+ TestAction : & resultstore.TestAction {
3430+ TestSuite : & resultstore.TestSuite {
3431+ Properties : []* resultstore.Property {
3432+ {Key : "marco" , Value : "polo" },
3433+ },
3434+ Tests : []* resultstore.Test {
3435+ {
3436+ TestType : & resultstore.Test_TestCase {
3437+ TestCase : & resultstore.TestCase {
3438+ Timing : & resultstore.Timing {
3439+ Duration : & durationpb.Duration {
3440+ Seconds : 60 ,
3441+ },
3442+ },
3443+ },
3444+ },
3445+ },
3446+ {
3447+ TestType : & resultstore.Test_TestCase {
3448+ TestCase : & resultstore.TestCase {
3449+ Timing : & resultstore.Timing {
3450+ Duration : & durationpb.Duration {
3451+ Seconds : 60 ,
3452+ },
3453+ },
3454+ },
3455+ },
3456+ },
3457+ },
3458+ },
3459+ },
3460+ },
3461+ },
3462+ },
3463+ want : map [string ]float64 {
3464+ updater .TestMethodsElapsedKey : 2 ,
3465+ },
3466+ },
3467+ {
3468+ name : "numeric properties and durations" ,
3469+ sar : & singleActionResult {
3470+ ActionProto : & resultstore.Action {
3471+ ActionType : & resultstore.Action_TestAction {
3472+ TestAction : & resultstore.TestAction {
3473+ TestSuite : & resultstore.TestSuite {
3474+ Properties : []* resultstore.Property {
3475+ {Key : "pizza" , Value : "12" },
3476+ },
3477+ Tests : []* resultstore.Test {
3478+ {
3479+ TestType : & resultstore.Test_TestCase {
3480+ TestCase : & resultstore.TestCase {
3481+ Timing : & resultstore.Timing {
3482+ Duration : & durationpb.Duration {
3483+ Seconds : 60 ,
3484+ },
3485+ },
3486+ Properties : []* resultstore.Property {
3487+ {Key : "pizza" , Value : "6" },
3488+ },
3489+ },
3490+ },
3491+ },
3492+ {
3493+ TestType : & resultstore.Test_TestCase {
3494+ TestCase : & resultstore.TestCase {
3495+ Timing : & resultstore.Timing {
3496+ Duration : & durationpb.Duration {
3497+ Seconds : 60 ,
3498+ },
3499+ },
3500+ Properties : []* resultstore.Property {
3501+ {Key : "pizza" , Value : "6" },
3502+ },
3503+ },
3504+ },
3505+ },
3506+ },
3507+ },
3508+ },
3509+ },
3510+ },
3511+ },
3512+ want : map [string ]float64 {
3513+ "pizza" : 8 ,
3514+ updater .TestMethodsElapsedKey : 2 ,
3515+ },
3516+ },
3517+ {
3518+ name : "numeric properties and durations" ,
3519+ sar : & singleActionResult {
3520+ TargetProto : & resultstore.Target {
3521+ Timing : & resultstore.Timing {
3522+ Duration : & durationpb.Duration {
3523+ Seconds : 600 ,
3524+ },
3525+ },
3526+ },
3527+ },
3528+ want : map [string ]float64 {
3529+ updater .ElapsedKey : 10 ,
3530+ },
3531+ },
3532+ }
3533+
3534+ for _ , tc := range cases {
3535+ t .Run (tc .name , func (t * testing.T ) {
3536+ got := calculateMetrics (tc .sar )
3537+ if diff := cmp .Diff (tc .want , got ); diff != "" {
3538+ t .Fatalf ("calculateMetrics(...) differed (-want,+got): %s" , diff )
3539+ }
3540+ })
3541+ }
3542+ }
0 commit comments