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

     1  package mock
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	"github.com/hashicorp/nomad/helper/uuid"
     8  	"github.com/hashicorp/nomad/nomad/structs"
     9  )
    10  
    11  func HCL() string {
    12  	return `job "my-job" {
    13  	datacenters = ["dc1"]
    14  	type = "service"
    15  	constraint {
    16  		attribute = "${attr.kernel.name}"
    17  		value = "linux"
    18  	}
    19  
    20  	group "web" {
    21  		count = 10
    22  		restart {
    23  			attempts = 3
    24  			interval = "10m"
    25  			delay = "1m"
    26  			mode = "delay"
    27  		}
    28  		task "web" {
    29  			driver = "exec"
    30  			config {
    31  				command = "/bin/date"
    32  			}
    33  			resources {
    34  				cpu = 500
    35  				memory = 256
    36  			}
    37  		}
    38  	}
    39  }
    40  `
    41  }
    42  
    43  func Eval() *structs.Evaluation {
    44  	now := time.Now().UTC().UnixNano()
    45  	eval := &structs.Evaluation{
    46  		ID:         uuid.Generate(),
    47  		Namespace:  structs.DefaultNamespace,
    48  		Priority:   50,
    49  		Type:       structs.JobTypeService,
    50  		JobID:      uuid.Generate(),
    51  		Status:     structs.EvalStatusPending,
    52  		CreateTime: now,
    53  		ModifyTime: now,
    54  	}
    55  	return eval
    56  }
    57  
    58  func BlockedEval() *structs.Evaluation {
    59  	e := Eval()
    60  	e.Status = structs.EvalStatusBlocked
    61  	e.FailedTGAllocs = map[string]*structs.AllocMetric{
    62  		"cache": {
    63  			DimensionExhausted: map[string]int{
    64  				"memory": 1,
    65  			},
    66  			ResourcesExhausted: map[string]*structs.Resources{
    67  				"redis": {
    68  					CPU:      100,
    69  					MemoryMB: 1024,
    70  				},
    71  			},
    72  		},
    73  	}
    74  
    75  	return e
    76  }
    77  
    78  func JobSummary(jobID string) *structs.JobSummary {
    79  	return &structs.JobSummary{
    80  		JobID:     jobID,
    81  		Namespace: structs.DefaultNamespace,
    82  		Summary: map[string]structs.TaskGroupSummary{
    83  			"web": {
    84  				Queued:   0,
    85  				Starting: 0,
    86  			},
    87  		},
    88  	}
    89  }
    90  
    91  func JobSysBatchSummary(jobID string) *structs.JobSummary {
    92  	return &structs.JobSummary{
    93  		JobID:     jobID,
    94  		Namespace: structs.DefaultNamespace,
    95  		Summary: map[string]structs.TaskGroupSummary{
    96  			"pinger": {
    97  				Queued:   0,
    98  				Starting: 0,
    99  			},
   100  		},
   101  	}
   102  }
   103  
   104  func VaultAccessor() *structs.VaultAccessor {
   105  	return &structs.VaultAccessor{
   106  		Accessor:    uuid.Generate(),
   107  		NodeID:      uuid.Generate(),
   108  		AllocID:     uuid.Generate(),
   109  		CreationTTL: 86400,
   110  		Task:        "foo",
   111  	}
   112  }
   113  
   114  func SITokenAccessor() *structs.SITokenAccessor {
   115  	return &structs.SITokenAccessor{
   116  		NodeID:     uuid.Generate(),
   117  		AllocID:    uuid.Generate(),
   118  		AccessorID: uuid.Generate(),
   119  		TaskName:   "foo",
   120  	}
   121  }
   122  
   123  func Deployment() *structs.Deployment {
   124  	return &structs.Deployment{
   125  		ID:             uuid.Generate(),
   126  		JobID:          uuid.Generate(),
   127  		Namespace:      structs.DefaultNamespace,
   128  		JobVersion:     2,
   129  		JobModifyIndex: 20,
   130  		JobCreateIndex: 18,
   131  		TaskGroups: map[string]*structs.DeploymentState{
   132  			"web": {
   133  				DesiredTotal: 10,
   134  			},
   135  		},
   136  		Status:            structs.DeploymentStatusRunning,
   137  		StatusDescription: structs.DeploymentStatusDescriptionRunning,
   138  		ModifyIndex:       23,
   139  		CreateIndex:       21,
   140  	}
   141  }
   142  
   143  func Plan() *structs.Plan {
   144  	return &structs.Plan{
   145  		Priority: 50,
   146  	}
   147  }
   148  
   149  func PlanResult() *structs.PlanResult {
   150  	return &structs.PlanResult{}
   151  }
   152  
   153  func ScalingPolicy() *structs.ScalingPolicy {
   154  	return &structs.ScalingPolicy{
   155  		ID:   uuid.Generate(),
   156  		Min:  1,
   157  		Max:  100,
   158  		Type: structs.ScalingPolicyTypeHorizontal,
   159  		Target: map[string]string{
   160  			structs.ScalingTargetNamespace: structs.DefaultNamespace,
   161  			structs.ScalingTargetJob:       uuid.Generate(),
   162  			structs.ScalingTargetGroup:     uuid.Generate(),
   163  			structs.ScalingTargetTask:      uuid.Generate(),
   164  		},
   165  		Policy: map[string]interface{}{
   166  			"a": "b",
   167  		},
   168  		Enabled: true,
   169  	}
   170  }
   171  
   172  func Events(index uint64) *structs.Events {
   173  	return &structs.Events{
   174  		Index: index,
   175  		Events: []structs.Event{
   176  			{
   177  				Index:   index,
   178  				Topic:   "Node",
   179  				Type:    "update",
   180  				Key:     uuid.Generate(),
   181  				Payload: Node(),
   182  			},
   183  			{
   184  				Index:   index,
   185  				Topic:   "Eval",
   186  				Type:    "update",
   187  				Key:     uuid.Generate(),
   188  				Payload: Eval(),
   189  			},
   190  		},
   191  	}
   192  }
   193  
   194  func Namespace() *structs.Namespace {
   195  	id := uuid.Generate()
   196  	ns := &structs.Namespace{
   197  		Name:        fmt.Sprintf("team-%s", id),
   198  		Meta:        map[string]string{"team": id},
   199  		Description: "test namespace",
   200  		CreateIndex: 100,
   201  		ModifyIndex: 200,
   202  	}
   203  	ns.SetHash()
   204  	return ns
   205  }
   206  
   207  // ServiceRegistrations generates an array containing two unique service
   208  // registrations.
   209  func ServiceRegistrations() []*structs.ServiceRegistration {
   210  	return []*structs.ServiceRegistration{
   211  		{
   212  			ID:          "_nomad-task-2873cf75-42e5-7c45-ca1c-415f3e18be3d-group-cache-example-cache-db",
   213  			ServiceName: "example-cache",
   214  			Namespace:   "default",
   215  			NodeID:      "17a6d1c0-811e-2ca9-ded0-3d5d6a54904c",
   216  			Datacenter:  "dc1",
   217  			JobID:       "example",
   218  			AllocID:     "2873cf75-42e5-7c45-ca1c-415f3e18be3d",
   219  			Tags:        []string{"foo"},
   220  			Address:     "192.168.10.1",
   221  			Port:        23000,
   222  		},
   223  		{
   224  			ID:          "_nomad-task-ca60e901-675a-0ab2-2e57-2f3b05fdc540-group-api-countdash-api-http",
   225  			ServiceName: "countdash-api",
   226  			Namespace:   "platform",
   227  			NodeID:      "ba991c17-7ce5-9c20-78b7-311e63578583",
   228  			Datacenter:  "dc2",
   229  			JobID:       "countdash-api",
   230  			AllocID:     "ca60e901-675a-0ab2-2e57-2f3b05fdc540",
   231  			Tags:        []string{"bar"},
   232  			Address:     "192.168.200.200",
   233  			Port:        29000,
   234  		},
   235  	}
   236  }