github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/nomad/structs/check_test.go (about)

     1  package structs
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/hashicorp/nomad/ci"
     8  	"github.com/shoenig/test/must"
     9  )
    10  
    11  func TestChecks_NomadCheckID(t *testing.T) {
    12  	ci.Parallel(t)
    13  
    14  	orig := ServiceCheck{
    15  		Name:        "c1",
    16  		Type:        "http",
    17  		Path:        "/health",
    18  		Protocol:    "https",
    19  		PortLabel:   "web",
    20  		AddressMode: "host",
    21  		Interval:    1 * time.Minute,
    22  		Timeout:     10 * time.Second,
    23  		Method:      "GET",
    24  		TaskName:    "t1",
    25  		OnUpdate:    OnUpdateIgnore,
    26  	}
    27  
    28  	different := func(a, b ServiceCheck) bool {
    29  		idA := NomadCheckID("id", "group", &a)
    30  		idB := NomadCheckID("id", "group", &b)
    31  		return idA != idB
    32  	}
    33  
    34  	t.Run("same", func(t *testing.T) {
    35  		c := orig
    36  		must.False(t, different(orig, c))
    37  	})
    38  
    39  	t.Run("different name", func(t *testing.T) {
    40  		c := orig
    41  		c.Name = "c2"
    42  		must.True(t, different(orig, c))
    43  	})
    44  
    45  	t.Run("different type", func(t *testing.T) {
    46  		c := orig
    47  		c.Type = "tcp"
    48  		must.True(t, different(orig, c))
    49  	})
    50  
    51  	t.Run("different path", func(t *testing.T) {
    52  		c := orig
    53  		c.Path = "/metrics"
    54  		must.True(t, different(orig, c))
    55  	})
    56  
    57  	t.Run("different protocol", func(t *testing.T) {
    58  		c := orig
    59  		c.Protocol = "http"
    60  		must.True(t, different(orig, c))
    61  	})
    62  
    63  	t.Run("different port label", func(t *testing.T) {
    64  		c := orig
    65  		c.PortLabel = "ingress"
    66  		must.True(t, different(orig, c))
    67  	})
    68  
    69  	t.Run("different address mode", func(t *testing.T) {
    70  		c := orig
    71  		c.AddressMode = "bridge"
    72  		must.True(t, different(orig, c))
    73  	})
    74  
    75  	t.Run("different interval", func(t *testing.T) {
    76  		c := orig
    77  		c.Interval = 1 * time.Second
    78  		must.True(t, different(orig, c))
    79  	})
    80  
    81  	t.Run("different timeout", func(t *testing.T) {
    82  		c := orig
    83  		c.Timeout = 5 * time.Second
    84  		must.True(t, different(orig, c))
    85  	})
    86  
    87  	t.Run("different method", func(t *testing.T) {
    88  		c := orig
    89  		c.Method = "POST"
    90  		must.True(t, different(orig, c))
    91  	})
    92  
    93  	t.Run("different task", func(t *testing.T) {
    94  		c := orig
    95  		c.TaskName = "task2"
    96  		must.True(t, different(orig, c))
    97  	})
    98  
    99  	t.Run("different on update", func(t *testing.T) {
   100  		c := orig
   101  		c.OnUpdate = "checks"
   102  		must.True(t, different(orig, c))
   103  	})
   104  }