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

     1  // Copyright 2016 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  	"context"
    19  	"testing"
    20  
    21  	. "github.com/smartystreets/goconvey/convey"
    22  )
    23  
    24  func TestCreateTargetFromHostname(t *testing.T) {
    25  	t.Parallel()
    26  
    27  	Convey("A target created", t, func() {
    28  		fl := NewFlags()
    29  		fl.SysInfo = &SysInfo{Hostname: "test-c4", Region: "test-region"}
    30  
    31  		Convey("for a device with autogenerated hostname should have autogen: hostname prefix", func() {
    32  			fl.TargetType = DeviceType
    33  			fl.AutoGenHostname = true
    34  			fl.SetDefaultsFromHostname()
    35  			target, err := NewFromFlags(&fl)
    36  			So(err, ShouldBeNil)
    37  			So(target, ShouldHaveSameTypeAs, (*NetworkDevice)(nil))
    38  			So(target.(*NetworkDevice).Hostname, ShouldEqual, "autogen:test-c4")
    39  			So(target.(*NetworkDevice).Hostgroup, ShouldEqual, "4")
    40  		})
    41  		Convey("for a device with a static hostname should not have a prefix", func() {
    42  			fl.TargetType = DeviceType
    43  			fl.SetDefaultsFromHostname()
    44  			target, err := NewFromFlags(&fl)
    45  			So(err, ShouldBeNil)
    46  			So(target, ShouldHaveSameTypeAs, (*NetworkDevice)(nil))
    47  			So(target.(*NetworkDevice).Hostname, ShouldEqual, "test-c4")
    48  			So(target.(*NetworkDevice).Hostgroup, ShouldEqual, "4")
    49  		})
    50  		Convey("for a task with autogenerated hostname should have autogen: hostname prefix", func() {
    51  			fl.TargetType = TaskType
    52  			fl.TaskServiceName = "test-service"
    53  			fl.TaskJobName = "test-job"
    54  			fl.AutoGenHostname = true
    55  			fl.SetDefaultsFromHostname()
    56  			target, err := NewFromFlags(&fl)
    57  			So(err, ShouldBeNil)
    58  			So(target, ShouldHaveSameTypeAs, (*Task)(nil))
    59  			So(target.(*Task).HostName, ShouldEqual, "autogen:test-c4")
    60  		})
    61  		Convey("for a task with a static hostname should not have a prefix", func() {
    62  			fl.TargetType = TaskType
    63  			fl.TaskServiceName = "test-service"
    64  			fl.TaskJobName = "test-job"
    65  			fl.SetDefaultsFromHostname()
    66  			target, err := NewFromFlags(&fl)
    67  			So(err, ShouldBeNil)
    68  			So(target, ShouldHaveSameTypeAs, (*Task)(nil))
    69  			So(target.(*Task).HostName, ShouldEqual, "test-c4")
    70  		})
    71  	})
    72  }
    73  
    74  func TestTargetContext(t *testing.T) {
    75  	t.Parallel()
    76  	ctx := context.Background()
    77  	deviceTarget := NetworkDevice{
    78  		Metro:     "test-metro",
    79  		Role:      "test-role",
    80  		Hostname:  "test-hostname",
    81  		Hostgroup: "test-hostgroup",
    82  	}
    83  	taskTarget := Task{
    84  		ServiceName: "test-service",
    85  		JobName:     "test-job",
    86  		DataCenter:  "test-datacenter",
    87  		HostName:    "test-hostname",
    88  		TaskNum:     0,
    89  	}
    90  
    91  	Convey("Without a target context", t, func() {
    92  		Convey("Get returns nil", func() {
    93  			So(Get(ctx, taskTarget.Type()), ShouldBeNil)
    94  		})
    95  	})
    96  	Convey("With a single target context", t, func() {
    97  		tctx := Set(ctx, &taskTarget)
    98  		Convey("Get returns the target if the type matches", func() {
    99  			So(Get(tctx, taskTarget.Type()), ShouldEqual, &taskTarget)
   100  		})
   101  		Convey("Get returns nil if the type doesn't match", func() {
   102  			So(Get(tctx, deviceTarget.Type()), ShouldBeNil)
   103  		})
   104  
   105  		Convey("Get returns the same target object for the same Type", func() {
   106  			clone := (&taskTarget).Clone().(*Task)
   107  			clone.TaskNum += 1
   108  
   109  			// Get should return the same target object, whichever target object
   110  			// the type was extracted from, as long as they all are the same type
   111  			// of target instances.
   112  			So(Get(tctx, taskTarget.Type()), ShouldEqual, &taskTarget)
   113  			So(Get(tctx, clone.Type()), ShouldEqual, &taskTarget)
   114  			So(Get(tctx, (*Task)(nil).Type()), ShouldEqual, &taskTarget)
   115  		})
   116  	})
   117  
   118  	Convey("With stacked target contexts", t, func() {
   119  		tctx := Set(ctx, &taskTarget)
   120  		tdctx := Set(tctx, &deviceTarget)
   121  
   122  		Convey("Child doesn't override the target for different types", func() {
   123  			So(Get(tdctx, taskTarget.Type()), ShouldEqual, &taskTarget)
   124  			So(Get(tdctx, deviceTarget.Type()), ShouldEqual, &deviceTarget)
   125  		})
   126  
   127  		Convey("Child overrides the target for the same type", func() {
   128  			clone := (&taskTarget).Clone().(*Task)
   129  			clone.TaskNum += 1
   130  
   131  			uptctx := Set(tdctx, clone)
   132  			// The target object for DeviceType should remain the same.
   133  			So(Get(uptctx, deviceTarget.Type()), ShouldEqual, &deviceTarget)
   134  			// but the object for TaskType should have been updated.
   135  			So(Get(uptctx, taskTarget.Type()), ShouldEqual, clone)
   136  			So(Get(uptctx, taskTarget.Type()), ShouldNotEqual, &taskTarget)
   137  
   138  			// the parent context should remain the same
   139  			So(Get(tdctx, taskTarget.Type()), ShouldEqual, &taskTarget)
   140  			So(Get(tdctx, deviceTarget.Type()), ShouldEqual, &deviceTarget)
   141  		})
   142  	})
   143  }