github.com/ali-iotechsys/cli@v20.10.0+incompatible/internal/test/builders/task.go (about)

     1  package builders
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/docker/docker/api/types/swarm"
     7  )
     8  
     9  var (
    10  	defaultTime = time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)
    11  )
    12  
    13  // Task creates a task with default values .
    14  // Any number of task function builder can be pass to augment it.
    15  func Task(taskBuilders ...func(*swarm.Task)) *swarm.Task {
    16  	task := &swarm.Task{
    17  		ID: "taskID",
    18  		Meta: swarm.Meta{
    19  			CreatedAt: defaultTime,
    20  		},
    21  		Annotations: swarm.Annotations{
    22  			Name: "defaultTaskName",
    23  		},
    24  		Spec:         *TaskSpec(),
    25  		ServiceID:    "rl02d5gwz6chzu7il5fhtb8be",
    26  		Slot:         1,
    27  		Status:       *TaskStatus(),
    28  		DesiredState: swarm.TaskStateReady,
    29  	}
    30  
    31  	for _, builder := range taskBuilders {
    32  		builder(task)
    33  	}
    34  
    35  	return task
    36  }
    37  
    38  // TaskID sets the task ID
    39  func TaskID(id string) func(*swarm.Task) {
    40  	return func(task *swarm.Task) {
    41  		task.ID = id
    42  	}
    43  }
    44  
    45  // TaskName sets the task name
    46  func TaskName(name string) func(*swarm.Task) {
    47  	return func(task *swarm.Task) {
    48  		task.Annotations.Name = name
    49  	}
    50  }
    51  
    52  // TaskServiceID sets the task service's ID
    53  func TaskServiceID(id string) func(*swarm.Task) {
    54  	return func(task *swarm.Task) {
    55  		task.ServiceID = id
    56  	}
    57  }
    58  
    59  // TaskNodeID sets the task's node id
    60  func TaskNodeID(id string) func(*swarm.Task) {
    61  	return func(task *swarm.Task) {
    62  		task.NodeID = id
    63  	}
    64  }
    65  
    66  // TaskDesiredState sets the task's desired state
    67  func TaskDesiredState(state swarm.TaskState) func(*swarm.Task) {
    68  	return func(task *swarm.Task) {
    69  		task.DesiredState = state
    70  	}
    71  }
    72  
    73  // TaskSlot sets the task's slot
    74  func TaskSlot(slot int) func(*swarm.Task) {
    75  	return func(task *swarm.Task) {
    76  		task.Slot = slot
    77  	}
    78  }
    79  
    80  // WithStatus sets the task status
    81  func WithStatus(statusBuilders ...func(*swarm.TaskStatus)) func(*swarm.Task) {
    82  	return func(task *swarm.Task) {
    83  		task.Status = *TaskStatus(statusBuilders...)
    84  	}
    85  }
    86  
    87  // TaskStatus creates a task status with default values .
    88  // Any number of taskStatus function builder can be pass to augment it.
    89  func TaskStatus(statusBuilders ...func(*swarm.TaskStatus)) *swarm.TaskStatus {
    90  	timestamp := defaultTime.Add(1 * time.Hour)
    91  	taskStatus := &swarm.TaskStatus{
    92  		State:     swarm.TaskStateReady,
    93  		Timestamp: timestamp,
    94  	}
    95  
    96  	for _, builder := range statusBuilders {
    97  		builder(taskStatus)
    98  	}
    99  
   100  	return taskStatus
   101  }
   102  
   103  // Timestamp sets the task status timestamp
   104  func Timestamp(t time.Time) func(*swarm.TaskStatus) {
   105  	return func(taskStatus *swarm.TaskStatus) {
   106  		taskStatus.Timestamp = t
   107  	}
   108  }
   109  
   110  // StatusErr sets the tasks status error
   111  func StatusErr(err string) func(*swarm.TaskStatus) {
   112  	return func(taskStatus *swarm.TaskStatus) {
   113  		taskStatus.Err = err
   114  	}
   115  }
   116  
   117  // TaskState sets the task's current state
   118  func TaskState(state swarm.TaskState) func(*swarm.TaskStatus) {
   119  	return func(taskStatus *swarm.TaskStatus) {
   120  		taskStatus.State = state
   121  	}
   122  }
   123  
   124  // PortStatus sets the tasks port config status
   125  // FIXME(vdemeester) should be a sub builder 👼
   126  func PortStatus(portConfigs []swarm.PortConfig) func(*swarm.TaskStatus) {
   127  	return func(taskStatus *swarm.TaskStatus) {
   128  		taskStatus.PortStatus.Ports = portConfigs
   129  	}
   130  }
   131  
   132  // WithTaskSpec sets the task spec
   133  func WithTaskSpec(specBuilders ...func(*swarm.TaskSpec)) func(*swarm.Task) {
   134  	return func(task *swarm.Task) {
   135  		task.Spec = *TaskSpec(specBuilders...)
   136  	}
   137  }
   138  
   139  // TaskSpec creates a task spec with default values .
   140  // Any number of taskSpec function builder can be pass to augment it.
   141  func TaskSpec(specBuilders ...func(*swarm.TaskSpec)) *swarm.TaskSpec {
   142  	taskSpec := &swarm.TaskSpec{
   143  		ContainerSpec: &swarm.ContainerSpec{
   144  			Image: "myimage:mytag",
   145  		},
   146  	}
   147  
   148  	for _, builder := range specBuilders {
   149  		builder(taskSpec)
   150  	}
   151  
   152  	return taskSpec
   153  }
   154  
   155  // TaskImage sets the task's image
   156  func TaskImage(image string) func(*swarm.TaskSpec) {
   157  	return func(taskSpec *swarm.TaskSpec) {
   158  		taskSpec.ContainerSpec.Image = image
   159  	}
   160  }