go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/common/tsmon/target/populate_test.go (about)

     1  // Copyright 2021 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 target
    16  
    17  import (
    18  	"testing"
    19  
    20  	pb "go.chromium.org/luci/common/tsmon/ts_mon_proto"
    21  
    22  	"google.golang.org/protobuf/proto"
    23  
    24  	. "github.com/smartystreets/goconvey/convey"
    25  	. "go.chromium.org/luci/common/testing/assertions"
    26  )
    27  
    28  func TestTargetPopulate(t *testing.T) {
    29  	t.Parallel()
    30  	deviceTarget := NetworkDevice{
    31  		Metro:     "test-metro",
    32  		Role:      "test-role",
    33  		Hostname:  "test-hostname",
    34  		Hostgroup: "test-hostgroup",
    35  	}
    36  	taskTarget := Task{
    37  		ServiceName: "test-service",
    38  		JobName:     "test-job",
    39  		DataCenter:  "test-datacenter",
    40  		HostName:    "test-hostname",
    41  		TaskNum:     1,
    42  	}
    43  
    44  	Convey("Populate metrics collection", t, func() {
    45  		Convey("With DeviceTarget", func() {
    46  			d := &pb.MetricsCollection{}
    47  			deviceTarget.PopulateProto(d)
    48  			So(d.RootLabels, ShouldResembleProto, []*pb.MetricsCollection_RootLabels{
    49  				{
    50  					Key:   proto.String("proxy_environment"),
    51  					Value: &pb.MetricsCollection_RootLabels_StringValue{StringValue: "pa"},
    52  				},
    53  				{
    54  					Key:   proto.String("acquisition_name"),
    55  					Value: &pb.MetricsCollection_RootLabels_StringValue{StringValue: "mon-chrome-infra"},
    56  				},
    57  				{
    58  					Key:   proto.String("proxy_zone"),
    59  					Value: &pb.MetricsCollection_RootLabels_StringValue{StringValue: "atl"},
    60  				},
    61  				{
    62  					Key:   proto.String("pop"),
    63  					Value: &pb.MetricsCollection_RootLabels_StringValue{StringValue: ""},
    64  				},
    65  				{
    66  					Key:   proto.String("alertable"),
    67  					Value: &pb.MetricsCollection_RootLabels_BoolValue{BoolValue: true},
    68  				},
    69  				{
    70  					Key:   proto.String("realm"),
    71  					Value: &pb.MetricsCollection_RootLabels_StringValue{StringValue: "ACQ_CHROME"},
    72  				},
    73  				{
    74  					Key:   proto.String("asn"),
    75  					Value: &pb.MetricsCollection_RootLabels_Int64Value{Int64Value: 0},
    76  				},
    77  				{
    78  					Key:   proto.String("metro"),
    79  					Value: &pb.MetricsCollection_RootLabels_StringValue{StringValue: "test-metro"},
    80  				},
    81  				{
    82  					Key:   proto.String("role"),
    83  					Value: &pb.MetricsCollection_RootLabels_StringValue{StringValue: "test-role"},
    84  				},
    85  				{
    86  					Key:   proto.String("hostname"),
    87  					Value: &pb.MetricsCollection_RootLabels_StringValue{StringValue: "test-hostname"},
    88  				},
    89  				{
    90  					Key:   proto.String("vendor"),
    91  					Value: &pb.MetricsCollection_RootLabels_StringValue{StringValue: ""},
    92  				},
    93  				{
    94  					Key:   proto.String("hostgroup"),
    95  					Value: &pb.MetricsCollection_RootLabels_StringValue{StringValue: "test-hostgroup"},
    96  				},
    97  			})
    98  		})
    99  
   100  		Convey("With TaskTarget", func() {
   101  			d := &pb.MetricsCollection{}
   102  			taskTarget.PopulateProto(d)
   103  			So(d.RootLabels, ShouldResembleProto, []*pb.MetricsCollection_RootLabels{
   104  				{
   105  					Key:   proto.String("proxy_environment"),
   106  					Value: &pb.MetricsCollection_RootLabels_StringValue{StringValue: "pa"},
   107  				},
   108  				{
   109  					Key:   proto.String("acquisition_name"),
   110  					Value: &pb.MetricsCollection_RootLabels_StringValue{StringValue: "mon-chrome-infra"},
   111  				},
   112  				{
   113  					Key:   proto.String("proxy_zone"),
   114  					Value: &pb.MetricsCollection_RootLabels_StringValue{StringValue: "atl"},
   115  				},
   116  				{
   117  					Key:   proto.String("service_name"),
   118  					Value: &pb.MetricsCollection_RootLabels_StringValue{StringValue: "test-service"},
   119  				},
   120  				{
   121  					Key:   proto.String("job_name"),
   122  					Value: &pb.MetricsCollection_RootLabels_StringValue{StringValue: "test-job"},
   123  				},
   124  				{
   125  					Key:   proto.String("data_center"),
   126  					Value: &pb.MetricsCollection_RootLabels_StringValue{StringValue: "test-datacenter"},
   127  				},
   128  				{
   129  					Key:   proto.String("host_name"),
   130  					Value: &pb.MetricsCollection_RootLabels_StringValue{StringValue: "test-hostname"},
   131  				},
   132  				{
   133  					Key:   proto.String("task_num"),
   134  					Value: &pb.MetricsCollection_RootLabels_Int64Value{Int64Value: 1},
   135  				},
   136  			})
   137  		})
   138  	})
   139  }