github.com/manicqin/nomad@v0.9.5/nomad/structs/services_test.go (about) 1 package structs 2 3 import ( 4 "testing" 5 "time" 6 7 "github.com/hashicorp/nomad/helper" 8 "github.com/stretchr/testify/require" 9 ) 10 11 func TestConsulConnect_Validate(t *testing.T) { 12 t.Parallel() 13 14 c := &ConsulConnect{} 15 16 // An empty Connect stanza is invalid 17 require.Error(t, c.Validate()) 18 19 // Native=true is valid 20 c.Native = true 21 require.NoError(t, c.Validate()) 22 23 // Native=true + Sidecar!=nil is invalid 24 c.SidecarService = &ConsulSidecarService{} 25 require.Error(t, c.Validate()) 26 27 // Native=false + Sidecar!=nil is valid 28 c.Native = false 29 require.NoError(t, c.Validate()) 30 } 31 32 func TestConsulConnect_CopyEquals(t *testing.T) { 33 t.Parallel() 34 35 c := &ConsulConnect{ 36 SidecarService: &ConsulSidecarService{ 37 Tags: []string{"tag1", "tag2"}, 38 Port: "9001", 39 Proxy: &ConsulProxy{ 40 LocalServiceAddress: "127.0.0.1", 41 LocalServicePort: 8080, 42 Upstreams: []ConsulUpstream{ 43 { 44 DestinationName: "up1", 45 LocalBindPort: 9002, 46 }, 47 { 48 DestinationName: "up2", 49 LocalBindPort: 9003, 50 }, 51 }, 52 Config: map[string]interface{}{ 53 "foo": 1, 54 }, 55 }, 56 }, 57 } 58 59 require.NoError(t, c.Validate()) 60 61 // Copies should be equivalent 62 o := c.Copy() 63 require.True(t, c.Equals(o)) 64 65 o.SidecarService.Proxy.Upstreams = nil 66 require.False(t, c.Equals(o)) 67 } 68 69 func TestSidecarTask_MergeIntoTask(t *testing.T) { 70 71 task := MockJob().TaskGroups[0].Tasks[0] 72 sTask := &SidecarTask{ 73 Name: "sidecar", 74 Driver: "sidecar", 75 User: "test", 76 Config: map[string]interface{}{ 77 "foo": "bar", 78 }, 79 Resources: &Resources{ 80 CPU: 10000, 81 MemoryMB: 10000, 82 }, 83 Env: map[string]string{ 84 "sidecar": "proxy", 85 }, 86 Meta: map[string]string{ 87 "abc": "123", 88 }, 89 KillTimeout: helper.TimeToPtr(15 * time.Second), 90 LogConfig: &LogConfig{ 91 MaxFiles: 3, 92 }, 93 ShutdownDelay: helper.TimeToPtr(5 * time.Second), 94 KillSignal: "SIGABRT", 95 } 96 97 expected := task.Copy() 98 expected.Name = "sidecar" 99 expected.Driver = "sidecar" 100 expected.User = "test" 101 expected.Config = map[string]interface{}{ 102 "foo": "bar", 103 } 104 expected.Resources.CPU = 10000 105 expected.Resources.MemoryMB = 10000 106 expected.Env["sidecar"] = "proxy" 107 expected.Meta["abc"] = "123" 108 expected.KillTimeout = 15 * time.Second 109 expected.LogConfig.MaxFiles = 3 110 expected.ShutdownDelay = 5 * time.Second 111 expected.KillSignal = "SIGABRT" 112 113 sTask.MergeIntoTask(task) 114 require.Exactly(t, expected, task) 115 116 // Check that changing just driver config doesn't replace map 117 sTask.Config["abc"] = 123 118 expected.Config["abc"] = 123 119 120 sTask.MergeIntoTask(task) 121 require.Exactly(t, expected, task) 122 123 }