2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2015-08-11 21:27:14 +00:00
|
|
|
package mock
|
|
|
|
|
2015-10-31 01:34:23 +00:00
|
|
|
import (
|
2017-08-07 21:21:00 +00:00
|
|
|
"fmt"
|
2015-10-31 01:34:23 +00:00
|
|
|
"time"
|
2015-11-13 01:47:51 +00:00
|
|
|
|
2017-09-29 16:58:48 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/uuid"
|
2015-11-13 01:47:51 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2015-10-31 01:34:23 +00:00
|
|
|
)
|
2015-08-11 21:27:14 +00:00
|
|
|
|
2018-04-12 18:45:38 +00:00
|
|
|
func HCL() string {
|
|
|
|
return `job "my-job" {
|
|
|
|
datacenters = ["dc1"]
|
|
|
|
type = "service"
|
|
|
|
constraint {
|
|
|
|
attribute = "${attr.kernel.name}"
|
|
|
|
value = "linux"
|
|
|
|
}
|
|
|
|
|
|
|
|
group "web" {
|
|
|
|
count = 10
|
|
|
|
restart {
|
|
|
|
attempts = 3
|
|
|
|
interval = "10m"
|
|
|
|
delay = "1m"
|
|
|
|
mode = "delay"
|
|
|
|
}
|
|
|
|
task "web" {
|
|
|
|
driver = "exec"
|
|
|
|
config {
|
|
|
|
command = "/bin/date"
|
|
|
|
}
|
|
|
|
resources {
|
|
|
|
cpu = 500
|
|
|
|
memory = 256
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
|
2023-04-11 13:45:08 +00:00
|
|
|
// HCLVar returns a the HCL of job that requires a HCL variables S, N,
|
|
|
|
// and B to be set. Also returns the content of a vars-file to satisfy
|
|
|
|
// those variables.
|
|
|
|
func HCLVar() (string, string) {
|
|
|
|
return `
|
|
|
|
variable "S" {
|
|
|
|
type = string
|
|
|
|
}
|
|
|
|
|
|
|
|
variable "N" {
|
|
|
|
type = number
|
|
|
|
}
|
|
|
|
|
|
|
|
variable "B" {
|
|
|
|
type = bool
|
|
|
|
}
|
|
|
|
|
|
|
|
job "var-job" {
|
|
|
|
type = "batch"
|
|
|
|
constraint {
|
|
|
|
attribute = "${attr.kernel.name}"
|
|
|
|
value = "linux"
|
|
|
|
}
|
|
|
|
group "group" {
|
|
|
|
task "task" {
|
|
|
|
driver = "raw_exec"
|
|
|
|
config {
|
|
|
|
command = "echo"
|
|
|
|
args = ["S is ${var.S}, N is ${var.N}, B is ${var.B}"]
|
|
|
|
}
|
|
|
|
resources {
|
|
|
|
cpu = 10
|
|
|
|
memory = 32
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`, `
|
|
|
|
S = "stringy"
|
|
|
|
N = 42
|
|
|
|
B = true
|
|
|
|
`
|
|
|
|
}
|
|
|
|
|
2015-08-11 21:27:14 +00:00
|
|
|
func Eval() *structs.Evaluation {
|
2019-08-07 16:50:35 +00:00
|
|
|
now := time.Now().UTC().UnixNano()
|
2015-08-11 21:27:14 +00:00
|
|
|
eval := &structs.Evaluation{
|
2019-08-07 16:50:35 +00:00
|
|
|
ID: uuid.Generate(),
|
|
|
|
Namespace: structs.DefaultNamespace,
|
|
|
|
Priority: 50,
|
|
|
|
Type: structs.JobTypeService,
|
|
|
|
JobID: uuid.Generate(),
|
|
|
|
Status: structs.EvalStatusPending,
|
|
|
|
CreateTime: now,
|
|
|
|
ModifyTime: now,
|
2015-08-11 21:27:14 +00:00
|
|
|
}
|
|
|
|
return eval
|
|
|
|
}
|
|
|
|
|
2021-04-29 19:03:45 +00:00
|
|
|
func BlockedEval() *structs.Evaluation {
|
|
|
|
e := Eval()
|
|
|
|
e.Status = structs.EvalStatusBlocked
|
|
|
|
e.FailedTGAllocs = map[string]*structs.AllocMetric{
|
|
|
|
"cache": {
|
|
|
|
DimensionExhausted: map[string]int{
|
|
|
|
"memory": 1,
|
|
|
|
},
|
|
|
|
ResourcesExhausted: map[string]*structs.Resources{
|
|
|
|
"redis": {
|
|
|
|
CPU: 100,
|
|
|
|
MemoryMB: 1024,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2016-07-20 21:09:03 +00:00
|
|
|
func JobSummary(jobID string) *structs.JobSummary {
|
2020-10-09 21:31:38 +00:00
|
|
|
return &structs.JobSummary{
|
2017-09-07 23:56:15 +00:00
|
|
|
JobID: jobID,
|
|
|
|
Namespace: structs.DefaultNamespace,
|
2016-07-20 21:09:03 +00:00
|
|
|
Summary: map[string]structs.TaskGroupSummary{
|
2016-07-21 21:43:21 +00:00
|
|
|
"web": {
|
|
|
|
Queued: 0,
|
|
|
|
Starting: 0,
|
2016-07-20 21:09:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2020-10-09 21:31:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func JobSysBatchSummary(jobID string) *structs.JobSummary {
|
|
|
|
return &structs.JobSummary{
|
|
|
|
JobID: jobID,
|
|
|
|
Namespace: structs.DefaultNamespace,
|
|
|
|
Summary: map[string]structs.TaskGroupSummary{
|
|
|
|
"pinger": {
|
|
|
|
Queued: 0,
|
|
|
|
Starting: 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2016-07-20 21:09:03 +00:00
|
|
|
}
|
|
|
|
|
2016-08-19 01:14:58 +00:00
|
|
|
func VaultAccessor() *structs.VaultAccessor {
|
|
|
|
return &structs.VaultAccessor{
|
2017-09-29 16:58:48 +00:00
|
|
|
Accessor: uuid.Generate(),
|
|
|
|
NodeID: uuid.Generate(),
|
|
|
|
AllocID: uuid.Generate(),
|
2016-08-19 01:14:58 +00:00
|
|
|
CreationTTL: 86400,
|
|
|
|
Task: "foo",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-06 20:46:46 +00:00
|
|
|
func SITokenAccessor() *structs.SITokenAccessor {
|
|
|
|
return &structs.SITokenAccessor{
|
|
|
|
NodeID: uuid.Generate(),
|
|
|
|
AllocID: uuid.Generate(),
|
|
|
|
AccessorID: uuid.Generate(),
|
|
|
|
TaskName: "foo",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-24 21:49:23 +00:00
|
|
|
func Deployment() *structs.Deployment {
|
|
|
|
return &structs.Deployment{
|
2017-09-29 16:58:48 +00:00
|
|
|
ID: uuid.Generate(),
|
|
|
|
JobID: uuid.Generate(),
|
2017-09-07 23:56:15 +00:00
|
|
|
Namespace: structs.DefaultNamespace,
|
2017-05-05 20:52:01 +00:00
|
|
|
JobVersion: 2,
|
|
|
|
JobModifyIndex: 20,
|
|
|
|
JobCreateIndex: 18,
|
|
|
|
TaskGroups: map[string]*structs.DeploymentState{
|
2017-09-26 22:26:33 +00:00
|
|
|
"web": {
|
2017-05-05 20:52:01 +00:00
|
|
|
DesiredTotal: 10,
|
|
|
|
},
|
|
|
|
},
|
2017-04-24 21:49:23 +00:00
|
|
|
Status: structs.DeploymentStatusRunning,
|
2017-06-29 05:00:18 +00:00
|
|
|
StatusDescription: structs.DeploymentStatusDescriptionRunning,
|
2017-04-24 21:49:23 +00:00
|
|
|
ModifyIndex: 23,
|
|
|
|
CreateIndex: 21,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-11 21:27:14 +00:00
|
|
|
func Plan() *structs.Plan {
|
|
|
|
return &structs.Plan{
|
|
|
|
Priority: 50,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func PlanResult() *structs.PlanResult {
|
|
|
|
return &structs.PlanResult{}
|
|
|
|
}
|
2017-08-07 21:21:00 +00:00
|
|
|
|
2020-01-15 20:51:57 +00:00
|
|
|
func ScalingPolicy() *structs.ScalingPolicy {
|
|
|
|
return &structs.ScalingPolicy{
|
2020-09-29 21:57:46 +00:00
|
|
|
ID: uuid.Generate(),
|
|
|
|
Min: 1,
|
|
|
|
Max: 100,
|
|
|
|
Type: structs.ScalingPolicyTypeHorizontal,
|
2020-02-21 21:23:30 +00:00
|
|
|
Target: map[string]string{
|
|
|
|
structs.ScalingTargetNamespace: structs.DefaultNamespace,
|
|
|
|
structs.ScalingTargetJob: uuid.Generate(),
|
|
|
|
structs.ScalingTargetGroup: uuid.Generate(),
|
2020-09-29 21:57:46 +00:00
|
|
|
structs.ScalingTargetTask: uuid.Generate(),
|
2020-02-21 21:23:30 +00:00
|
|
|
},
|
2020-01-15 20:51:57 +00:00
|
|
|
Policy: map[string]interface{}{
|
|
|
|
"a": "b",
|
|
|
|
},
|
2020-09-09 22:30:40 +00:00
|
|
|
Enabled: true,
|
2020-01-15 20:51:57 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-16 20:12:52 +00:00
|
|
|
|
2020-10-04 19:12:35 +00:00
|
|
|
func Events(index uint64) *structs.Events {
|
|
|
|
return &structs.Events{
|
|
|
|
Index: index,
|
|
|
|
Events: []structs.Event{
|
|
|
|
{
|
|
|
|
Index: index,
|
|
|
|
Topic: "Node",
|
|
|
|
Type: "update",
|
|
|
|
Key: uuid.Generate(),
|
|
|
|
Payload: Node(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Index: index,
|
|
|
|
Topic: "Eval",
|
|
|
|
Type: "update",
|
|
|
|
Key: uuid.Generate(),
|
|
|
|
Payload: Eval(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2020-10-20 20:15:37 +00:00
|
|
|
|
2020-10-21 04:16:25 +00:00
|
|
|
func Namespace() *structs.Namespace {
|
2022-09-22 18:49:58 +00:00
|
|
|
id := uuid.Generate()
|
2020-10-21 04:16:25 +00:00
|
|
|
ns := &structs.Namespace{
|
2022-09-22 18:49:58 +00:00
|
|
|
Name: fmt.Sprintf("team-%s", id),
|
|
|
|
Meta: map[string]string{"team": id},
|
2020-10-21 04:16:25 +00:00
|
|
|
Description: "test namespace",
|
|
|
|
CreateIndex: 100,
|
|
|
|
ModifyIndex: 200,
|
|
|
|
}
|
2023-06-16 20:30:22 +00:00
|
|
|
ns.Canonicalize()
|
2020-10-21 04:16:25 +00:00
|
|
|
ns.SetHash()
|
|
|
|
return ns
|
|
|
|
}
|
2022-02-28 09:14:25 +00:00
|
|
|
|
2023-05-15 14:49:08 +00:00
|
|
|
func NodePool() *structs.NodePool {
|
2023-06-12 17:24:24 +00:00
|
|
|
pool := &structs.NodePool{
|
2023-05-15 14:49:08 +00:00
|
|
|
Name: fmt.Sprintf("pool-%s", uuid.Short()),
|
|
|
|
Description: "test node pool",
|
|
|
|
Meta: map[string]string{"team": "test"},
|
|
|
|
}
|
2023-06-12 17:24:24 +00:00
|
|
|
pool.SetHash()
|
|
|
|
return pool
|
2023-05-15 14:49:08 +00:00
|
|
|
}
|
|
|
|
|
2022-02-28 09:14:25 +00:00
|
|
|
// ServiceRegistrations generates an array containing two unique service
|
|
|
|
// registrations.
|
|
|
|
func ServiceRegistrations() []*structs.ServiceRegistration {
|
|
|
|
return []*structs.ServiceRegistration{
|
|
|
|
{
|
|
|
|
ID: "_nomad-task-2873cf75-42e5-7c45-ca1c-415f3e18be3d-group-cache-example-cache-db",
|
|
|
|
ServiceName: "example-cache",
|
|
|
|
Namespace: "default",
|
|
|
|
NodeID: "17a6d1c0-811e-2ca9-ded0-3d5d6a54904c",
|
|
|
|
Datacenter: "dc1",
|
|
|
|
JobID: "example",
|
|
|
|
AllocID: "2873cf75-42e5-7c45-ca1c-415f3e18be3d",
|
|
|
|
Tags: []string{"foo"},
|
|
|
|
Address: "192.168.10.1",
|
|
|
|
Port: 23000,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: "_nomad-task-ca60e901-675a-0ab2-2e57-2f3b05fdc540-group-api-countdash-api-http",
|
|
|
|
ServiceName: "countdash-api",
|
|
|
|
Namespace: "platform",
|
|
|
|
NodeID: "ba991c17-7ce5-9c20-78b7-311e63578583",
|
|
|
|
Datacenter: "dc2",
|
|
|
|
JobID: "countdash-api",
|
|
|
|
AllocID: "ca60e901-675a-0ab2-2e57-2f3b05fdc540",
|
|
|
|
Tags: []string{"bar"},
|
|
|
|
Address: "192.168.200.200",
|
|
|
|
Port: 29000,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|