2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2019-08-19 13:17:38 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2021-01-22 19:45:26 +00:00
|
|
|
"fmt"
|
2019-08-19 13:17:38 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2022-03-17 13:34:57 +00:00
|
|
|
"github.com/hashicorp/nomad/api/internal/testutil"
|
2023-01-01 18:57:26 +00:00
|
|
|
"github.com/shoenig/test/must"
|
2019-08-19 13:17:38 +00:00
|
|
|
)
|
|
|
|
|
2022-03-24 08:08:45 +00:00
|
|
|
func TestServiceRegistrations_List(t *testing.T) {
|
|
|
|
// TODO(jrasell) add tests once registration process is in place.
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestServiceRegistrations_Get(t *testing.T) {
|
|
|
|
// TODO(jrasell) add tests once registration process is in place.
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestServiceRegistrations_Delete(t *testing.T) {
|
|
|
|
// TODO(jrasell) add tests once registration process is in place.
|
|
|
|
}
|
|
|
|
|
2021-01-22 19:45:26 +00:00
|
|
|
func TestService_Canonicalize(t *testing.T) {
|
2022-03-17 13:34:57 +00:00
|
|
|
testutil.Parallel(t)
|
2021-01-22 19:45:26 +00:00
|
|
|
|
2022-08-17 16:26:34 +00:00
|
|
|
j := &Job{Name: pointerOf("job")}
|
|
|
|
tg := &TaskGroup{Name: pointerOf("group")}
|
2021-01-22 19:45:26 +00:00
|
|
|
task := &Task{Name: "task"}
|
2022-05-31 15:06:39 +00:00
|
|
|
s := &Service{
|
|
|
|
TaggedAddresses: make(map[string]string),
|
|
|
|
}
|
2021-01-22 19:45:26 +00:00
|
|
|
|
|
|
|
s.Canonicalize(task, tg, j)
|
|
|
|
|
2023-01-01 18:57:26 +00:00
|
|
|
must.Eq(t, fmt.Sprintf("%s-%s-%s", *j.Name, *tg.Name, task.Name), s.Name)
|
|
|
|
must.Eq(t, "auto", s.AddressMode)
|
|
|
|
must.Eq(t, OnUpdateRequireHealthy, s.OnUpdate)
|
|
|
|
must.Eq(t, ServiceProviderConsul, s.Provider)
|
|
|
|
must.Nil(t, s.Meta)
|
|
|
|
must.Nil(t, s.CanaryMeta)
|
|
|
|
must.Nil(t, s.TaggedAddresses)
|
2021-01-22 19:45:26 +00:00
|
|
|
}
|
|
|
|
|
2021-02-04 15:18:03 +00:00
|
|
|
func TestServiceCheck_Canonicalize(t *testing.T) {
|
2022-03-17 13:34:57 +00:00
|
|
|
testutil.Parallel(t)
|
2021-02-04 15:18:03 +00:00
|
|
|
|
2022-08-17 16:26:34 +00:00
|
|
|
j := &Job{Name: pointerOf("job")}
|
|
|
|
tg := &TaskGroup{Name: pointerOf("group")}
|
2021-02-04 15:18:03 +00:00
|
|
|
task := &Task{Name: "task"}
|
|
|
|
s := &Service{
|
|
|
|
Checks: []ServiceCheck{
|
|
|
|
{
|
|
|
|
Name: "check",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Canonicalize(task, tg, j)
|
2023-01-01 18:57:26 +00:00
|
|
|
must.Eq(t, OnUpdateRequireHealthy, s.Checks[0].OnUpdate)
|
2021-02-04 15:18:03 +00:00
|
|
|
}
|
|
|
|
|
2020-08-08 01:22:06 +00:00
|
|
|
func TestService_Check_PassFail(t *testing.T) {
|
2022-03-17 13:34:57 +00:00
|
|
|
testutil.Parallel(t)
|
2020-08-08 01:22:06 +00:00
|
|
|
|
2022-08-17 16:26:34 +00:00
|
|
|
job := &Job{Name: pointerOf("job")}
|
|
|
|
tg := &TaskGroup{Name: pointerOf("group")}
|
2020-08-08 01:22:06 +00:00
|
|
|
task := &Task{Name: "task"}
|
|
|
|
|
|
|
|
t.Run("enforce minimums", func(t *testing.T) {
|
|
|
|
s := &Service{
|
|
|
|
Checks: []ServiceCheck{{
|
|
|
|
SuccessBeforePassing: -1,
|
|
|
|
FailuresBeforeCritical: -2,
|
|
|
|
}},
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Canonicalize(task, tg, job)
|
2023-01-01 18:57:26 +00:00
|
|
|
must.Zero(t, s.Checks[0].SuccessBeforePassing)
|
|
|
|
must.Zero(t, s.Checks[0].FailuresBeforeCritical)
|
2020-08-08 01:22:06 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("normal", func(t *testing.T) {
|
|
|
|
s := &Service{
|
|
|
|
Checks: []ServiceCheck{{
|
|
|
|
SuccessBeforePassing: 3,
|
|
|
|
FailuresBeforeCritical: 4,
|
|
|
|
}},
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Canonicalize(task, tg, job)
|
2023-01-01 18:57:26 +00:00
|
|
|
must.Eq(t, 3, s.Checks[0].SuccessBeforePassing)
|
|
|
|
must.Eq(t, 4, s.Checks[0].FailuresBeforeCritical)
|
2020-08-08 01:22:06 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-08-19 13:17:38 +00:00
|
|
|
// TestService_CheckRestart asserts Service.CheckRestart settings are properly
|
|
|
|
// inherited by Checks.
|
|
|
|
func TestService_CheckRestart(t *testing.T) {
|
2022-03-17 13:34:57 +00:00
|
|
|
testutil.Parallel(t)
|
client: enable configuring enable_tag_override for services
Consul provides a feature of Service Definitions where the tags
associated with a service can be modified through the Catalog API,
overriding the value(s) configured in the agent's service configuration.
To enable this feature, the flag enable_tag_override must be configured
in the service definition.
Previously, Nomad did not allow configuring this flag, and thus the default
value of false was used. Now, it is configurable.
Because Nomad itself acts as a state machine around the the service definitions
of the tasks it manages, it's worth describing what happens when this feature
is enabled and why.
Consider the basic case where there is no Nomad, and your service is provided
to consul as a boring JSON file. The ultimate source of truth for the definition
of that service is the file, and is stored in the agent. Later, Consul performs
"anti-entropy" which synchronizes the Catalog (stored only the leaders). Then
with enable_tag_override=true, the tags field is available for "external"
modification through the Catalog API (rather than directly configuring the
service definition file, or using the Agent API). The important observation
is that if the service definition ever changes (i.e. the file is changed &
config reloaded OR the Agent API is used to modify the service), those
"external" tag values are thrown away, and the new service definition is
once again the source of truth.
In the Nomad case, Nomad itself is the source of truth over the Agent in
the same way the JSON file was the source of truth in the example above.
That means any time Nomad sets a new service definition, any externally
configured tags are going to be replaced. When does this happen? Only on
major lifecycle events, for example when a task is modified because of an
updated job spec from the 'nomad job run <existing>' command. Otherwise,
Nomad's periodic re-sync's with Consul will now no longer try to restore
the externally modified tag values (as long as enable_tag_override=true).
Fixes #2057
2020-02-07 21:22:19 +00:00
|
|
|
|
2022-08-17 16:26:34 +00:00
|
|
|
job := &Job{Name: pointerOf("job")}
|
|
|
|
tg := &TaskGroup{Name: pointerOf("group")}
|
2019-08-19 13:17:38 +00:00
|
|
|
task := &Task{Name: "task"}
|
|
|
|
service := &Service{
|
|
|
|
CheckRestart: &CheckRestart{
|
|
|
|
Limit: 11,
|
2022-08-17 16:26:34 +00:00
|
|
|
Grace: pointerOf(11 * time.Second),
|
2019-08-19 13:17:38 +00:00
|
|
|
IgnoreWarnings: true,
|
|
|
|
},
|
|
|
|
Checks: []ServiceCheck{
|
|
|
|
{
|
|
|
|
Name: "all-set",
|
|
|
|
CheckRestart: &CheckRestart{
|
|
|
|
Limit: 22,
|
2022-08-17 16:26:34 +00:00
|
|
|
Grace: pointerOf(22 * time.Second),
|
2019-08-19 13:17:38 +00:00
|
|
|
IgnoreWarnings: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "some-set",
|
|
|
|
CheckRestart: &CheckRestart{
|
|
|
|
Limit: 33,
|
2022-08-17 16:26:34 +00:00
|
|
|
Grace: pointerOf(33 * time.Second),
|
2019-08-19 13:17:38 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "unset",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
service.Canonicalize(task, tg, job)
|
2023-01-01 18:57:26 +00:00
|
|
|
must.Eq(t, 22, service.Checks[0].CheckRestart.Limit)
|
|
|
|
must.Eq(t, 22*time.Second, *service.Checks[0].CheckRestart.Grace)
|
|
|
|
must.True(t, service.Checks[0].CheckRestart.IgnoreWarnings)
|
2019-08-19 13:17:38 +00:00
|
|
|
|
2023-01-01 18:57:26 +00:00
|
|
|
must.Eq(t, 33, service.Checks[1].CheckRestart.Limit)
|
|
|
|
must.Eq(t, 33*time.Second, *service.Checks[1].CheckRestart.Grace)
|
|
|
|
must.True(t, service.Checks[1].CheckRestart.IgnoreWarnings)
|
2019-08-19 13:17:38 +00:00
|
|
|
|
2023-01-01 18:57:26 +00:00
|
|
|
must.Eq(t, 11, service.Checks[2].CheckRestart.Limit)
|
|
|
|
must.Eq(t, 11*time.Second, *service.Checks[2].CheckRestart.Grace)
|
|
|
|
must.True(t, service.Checks[2].CheckRestart.IgnoreWarnings)
|
2019-08-19 13:17:38 +00:00
|
|
|
}
|
2019-09-23 18:30:48 +00:00
|
|
|
|
2020-05-18 22:03:10 +00:00
|
|
|
func TestService_Connect_proxy_settings(t *testing.T) {
|
2022-03-17 13:34:57 +00:00
|
|
|
testutil.Parallel(t)
|
client: enable configuring enable_tag_override for services
Consul provides a feature of Service Definitions where the tags
associated with a service can be modified through the Catalog API,
overriding the value(s) configured in the agent's service configuration.
To enable this feature, the flag enable_tag_override must be configured
in the service definition.
Previously, Nomad did not allow configuring this flag, and thus the default
value of false was used. Now, it is configurable.
Because Nomad itself acts as a state machine around the the service definitions
of the tasks it manages, it's worth describing what happens when this feature
is enabled and why.
Consider the basic case where there is no Nomad, and your service is provided
to consul as a boring JSON file. The ultimate source of truth for the definition
of that service is the file, and is stored in the agent. Later, Consul performs
"anti-entropy" which synchronizes the Catalog (stored only the leaders). Then
with enable_tag_override=true, the tags field is available for "external"
modification through the Catalog API (rather than directly configuring the
service definition file, or using the Agent API). The important observation
is that if the service definition ever changes (i.e. the file is changed &
config reloaded OR the Agent API is used to modify the service), those
"external" tag values are thrown away, and the new service definition is
once again the source of truth.
In the Nomad case, Nomad itself is the source of truth over the Agent in
the same way the JSON file was the source of truth in the example above.
That means any time Nomad sets a new service definition, any externally
configured tags are going to be replaced. When does this happen? Only on
major lifecycle events, for example when a task is modified because of an
updated job spec from the 'nomad job run <existing>' command. Otherwise,
Nomad's periodic re-sync's with Consul will now no longer try to restore
the externally modified tag values (as long as enable_tag_override=true).
Fixes #2057
2020-02-07 21:22:19 +00:00
|
|
|
|
2022-08-17 16:26:34 +00:00
|
|
|
job := &Job{Name: pointerOf("job")}
|
|
|
|
tg := &TaskGroup{Name: pointerOf("group")}
|
2019-09-23 18:30:48 +00:00
|
|
|
task := &Task{Name: "task"}
|
|
|
|
service := &Service{
|
|
|
|
Connect: &ConsulConnect{
|
|
|
|
SidecarService: &ConsulSidecarService{
|
|
|
|
Proxy: &ConsulProxy{
|
|
|
|
Upstreams: []*ConsulUpstream{
|
|
|
|
{
|
2021-02-23 15:49:18 +00:00
|
|
|
DestinationName: "upstream",
|
|
|
|
LocalBindPort: 80,
|
|
|
|
Datacenter: "dc2",
|
|
|
|
LocalBindAddress: "127.0.0.2",
|
2019-09-23 18:30:48 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
LocalServicePort: 8000,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
service.Canonicalize(task, tg, job)
|
|
|
|
proxy := service.Connect.SidecarService.Proxy
|
2023-01-01 18:57:26 +00:00
|
|
|
must.Eq(t, "upstream", proxy.Upstreams[0].DestinationName)
|
|
|
|
must.Eq(t, 80, proxy.Upstreams[0].LocalBindPort)
|
|
|
|
must.Eq(t, "dc2", proxy.Upstreams[0].Datacenter)
|
|
|
|
must.Eq(t, "127.0.0.2", proxy.Upstreams[0].LocalBindAddress)
|
|
|
|
must.Eq(t, 8000, proxy.LocalServicePort)
|
2019-09-23 18:30:48 +00:00
|
|
|
}
|
client: enable configuring enable_tag_override for services
Consul provides a feature of Service Definitions where the tags
associated with a service can be modified through the Catalog API,
overriding the value(s) configured in the agent's service configuration.
To enable this feature, the flag enable_tag_override must be configured
in the service definition.
Previously, Nomad did not allow configuring this flag, and thus the default
value of false was used. Now, it is configurable.
Because Nomad itself acts as a state machine around the the service definitions
of the tasks it manages, it's worth describing what happens when this feature
is enabled and why.
Consider the basic case where there is no Nomad, and your service is provided
to consul as a boring JSON file. The ultimate source of truth for the definition
of that service is the file, and is stored in the agent. Later, Consul performs
"anti-entropy" which synchronizes the Catalog (stored only the leaders). Then
with enable_tag_override=true, the tags field is available for "external"
modification through the Catalog API (rather than directly configuring the
service definition file, or using the Agent API). The important observation
is that if the service definition ever changes (i.e. the file is changed &
config reloaded OR the Agent API is used to modify the service), those
"external" tag values are thrown away, and the new service definition is
once again the source of truth.
In the Nomad case, Nomad itself is the source of truth over the Agent in
the same way the JSON file was the source of truth in the example above.
That means any time Nomad sets a new service definition, any externally
configured tags are going to be replaced. When does this happen? Only on
major lifecycle events, for example when a task is modified because of an
updated job spec from the 'nomad job run <existing>' command. Otherwise,
Nomad's periodic re-sync's with Consul will now no longer try to restore
the externally modified tag values (as long as enable_tag_override=true).
Fixes #2057
2020-02-07 21:22:19 +00:00
|
|
|
|
|
|
|
func TestService_Tags(t *testing.T) {
|
2022-03-17 13:34:57 +00:00
|
|
|
testutil.Parallel(t)
|
client: enable configuring enable_tag_override for services
Consul provides a feature of Service Definitions where the tags
associated with a service can be modified through the Catalog API,
overriding the value(s) configured in the agent's service configuration.
To enable this feature, the flag enable_tag_override must be configured
in the service definition.
Previously, Nomad did not allow configuring this flag, and thus the default
value of false was used. Now, it is configurable.
Because Nomad itself acts as a state machine around the the service definitions
of the tasks it manages, it's worth describing what happens when this feature
is enabled and why.
Consider the basic case where there is no Nomad, and your service is provided
to consul as a boring JSON file. The ultimate source of truth for the definition
of that service is the file, and is stored in the agent. Later, Consul performs
"anti-entropy" which synchronizes the Catalog (stored only the leaders). Then
with enable_tag_override=true, the tags field is available for "external"
modification through the Catalog API (rather than directly configuring the
service definition file, or using the Agent API). The important observation
is that if the service definition ever changes (i.e. the file is changed &
config reloaded OR the Agent API is used to modify the service), those
"external" tag values are thrown away, and the new service definition is
once again the source of truth.
In the Nomad case, Nomad itself is the source of truth over the Agent in
the same way the JSON file was the source of truth in the example above.
That means any time Nomad sets a new service definition, any externally
configured tags are going to be replaced. When does this happen? Only on
major lifecycle events, for example when a task is modified because of an
updated job spec from the 'nomad job run <existing>' command. Otherwise,
Nomad's periodic re-sync's with Consul will now no longer try to restore
the externally modified tag values (as long as enable_tag_override=true).
Fixes #2057
2020-02-07 21:22:19 +00:00
|
|
|
|
|
|
|
// canonicalize does not modify eto or tags
|
2022-08-17 16:26:34 +00:00
|
|
|
job := &Job{Name: pointerOf("job")}
|
|
|
|
tg := &TaskGroup{Name: pointerOf("group")}
|
client: enable configuring enable_tag_override for services
Consul provides a feature of Service Definitions where the tags
associated with a service can be modified through the Catalog API,
overriding the value(s) configured in the agent's service configuration.
To enable this feature, the flag enable_tag_override must be configured
in the service definition.
Previously, Nomad did not allow configuring this flag, and thus the default
value of false was used. Now, it is configurable.
Because Nomad itself acts as a state machine around the the service definitions
of the tasks it manages, it's worth describing what happens when this feature
is enabled and why.
Consider the basic case where there is no Nomad, and your service is provided
to consul as a boring JSON file. The ultimate source of truth for the definition
of that service is the file, and is stored in the agent. Later, Consul performs
"anti-entropy" which synchronizes the Catalog (stored only the leaders). Then
with enable_tag_override=true, the tags field is available for "external"
modification through the Catalog API (rather than directly configuring the
service definition file, or using the Agent API). The important observation
is that if the service definition ever changes (i.e. the file is changed &
config reloaded OR the Agent API is used to modify the service), those
"external" tag values are thrown away, and the new service definition is
once again the source of truth.
In the Nomad case, Nomad itself is the source of truth over the Agent in
the same way the JSON file was the source of truth in the example above.
That means any time Nomad sets a new service definition, any externally
configured tags are going to be replaced. When does this happen? Only on
major lifecycle events, for example when a task is modified because of an
updated job spec from the 'nomad job run <existing>' command. Otherwise,
Nomad's periodic re-sync's with Consul will now no longer try to restore
the externally modified tag values (as long as enable_tag_override=true).
Fixes #2057
2020-02-07 21:22:19 +00:00
|
|
|
task := &Task{Name: "task"}
|
|
|
|
service := &Service{
|
|
|
|
Tags: []string{"a", "b"},
|
|
|
|
CanaryTags: []string{"c", "d"},
|
|
|
|
EnableTagOverride: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
service.Canonicalize(task, tg, job)
|
2023-01-01 18:57:26 +00:00
|
|
|
must.True(t, service.EnableTagOverride)
|
|
|
|
must.Eq(t, []string{"a", "b"}, service.Tags)
|
|
|
|
must.Eq(t, []string{"c", "d"}, service.CanaryTags)
|
2022-05-31 15:06:39 +00:00
|
|
|
}
|