|
| 1 | +//go:build unit || !integration |
| 2 | + |
| 3 | +package models_test |
| 4 | + |
| 5 | +import ( |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/suite" |
| 10 | + |
| 11 | + "github.com/bacalhau-project/bacalhau/pkg/models" |
| 12 | + "github.com/bacalhau-project/bacalhau/pkg/test/mock" |
| 13 | +) |
| 14 | + |
| 15 | +type JobTestSuite struct { |
| 16 | + suite.Suite |
| 17 | +} |
| 18 | + |
| 19 | +func (suite *JobTestSuite) TestJobNormalization() { |
| 20 | + testCases := []struct { |
| 21 | + jobType string |
| 22 | + expectedCount int |
| 23 | + }{ |
| 24 | + {models.JobTypeBatch, 0}, |
| 25 | + {models.JobTypeService, 0}, |
| 26 | + {models.JobTypeOps, 1}, |
| 27 | + {models.JobTypeDaemon, 1}, |
| 28 | + } |
| 29 | + |
| 30 | + for _, tc := range testCases { |
| 31 | + job := &models.Job{ |
| 32 | + ID: "test-job", |
| 33 | + Type: tc.jobType, |
| 34 | + Name: "", |
| 35 | + Namespace: "", |
| 36 | + Meta: nil, |
| 37 | + Labels: nil, |
| 38 | + Constraints: nil, |
| 39 | + Tasks: nil, |
| 40 | + } |
| 41 | + |
| 42 | + job.Normalize() |
| 43 | + |
| 44 | + suite.Equal(models.DefaultNamespace, job.Namespace) |
| 45 | + suite.Equal("test-job", job.Name) |
| 46 | + suite.Equal(tc.jobType, job.Type) |
| 47 | + suite.Equal(tc.expectedCount, job.Count) |
| 48 | + suite.NotNil(job.Meta) |
| 49 | + suite.NotNil(job.Labels) |
| 50 | + suite.NotNil(job.Constraints) |
| 51 | + suite.NotNil(job.Tasks) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +func (suite *JobTestSuite) TestJobValidation() { |
| 56 | + job := &models.Job{ |
| 57 | + ID: "invalid job id", |
| 58 | + Name: "", |
| 59 | + Namespace: "", |
| 60 | + } |
| 61 | + |
| 62 | + err := job.Validate() |
| 63 | + suite.Error(err) |
| 64 | + suite.Contains(err.Error(), "missing job name") |
| 65 | + suite.Contains(err.Error(), "job ID contains a space") |
| 66 | + suite.Contains(err.Error(), "job must be in a namespace") |
| 67 | +} |
| 68 | + |
| 69 | +func (suite *JobTestSuite) TestJobSanitization() { |
| 70 | + job := &models.Job{ |
| 71 | + ID: "test-job", |
| 72 | + Name: "test-job", |
| 73 | + Namespace: "default", |
| 74 | + State: models.State[models.JobStateType]{StateType: models.JobStateTypeRunning}, |
| 75 | + Revision: 1, |
| 76 | + Version: 1, |
| 77 | + CreateTime: time.Now().UnixNano(), |
| 78 | + ModifyTime: time.Now().UnixNano(), |
| 79 | + Tasks: []*models.Task{ |
| 80 | + { |
| 81 | + Name: "test-task", |
| 82 | + }, |
| 83 | + }, |
| 84 | + } |
| 85 | + |
| 86 | + warnings := job.SanitizeSubmission() |
| 87 | + |
| 88 | + suite.NotEmpty(warnings) |
| 89 | + suite.Equal(models.JobStateTypeUndefined, job.State.StateType) |
| 90 | + suite.Equal(uint64(0), job.Revision) |
| 91 | + suite.Equal(uint64(0), job.Version) |
| 92 | + suite.Equal(int64(0), job.CreateTime) |
| 93 | + suite.Equal(int64(0), job.ModifyTime) |
| 94 | +} |
| 95 | + |
| 96 | +func (suite *JobTestSuite) TestJobCopy() { |
| 97 | + job := mock.Job() |
| 98 | + cpy := job.Copy() |
| 99 | + |
| 100 | + suite.NotNil(cpy) |
| 101 | + suite.Equal(job, cpy, "The job and its copy should be deeply equal") |
| 102 | + suite.NotSame(job, cpy, "The job and its copy should not be the same instance") |
| 103 | + |
| 104 | + // Ensure nested objects are deeply copied |
| 105 | + for i := range job.Tasks { |
| 106 | + suite.NotSame(job.Tasks[i], cpy.Tasks[i], "The tasks in the job and its copy should not be the same instance") |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +func (suite *JobTestSuite) TestJobTask() { |
| 111 | + job := mock.Job() |
| 112 | + suite.Equal(job.Tasks[0], job.Task()) |
| 113 | +} |
| 114 | + |
| 115 | +func (suite *JobTestSuite) TestIsTerminal() { |
| 116 | + job := &models.Job{ |
| 117 | + State: models.State[models.JobStateType]{StateType: models.JobStateTypeCompleted}, |
| 118 | + } |
| 119 | + suite.True(job.IsTerminal()) |
| 120 | + |
| 121 | + job.State.StateType = models.JobStateTypeFailed |
| 122 | + suite.True(job.IsTerminal()) |
| 123 | + |
| 124 | + job.State.StateType = models.JobStateTypeStopped |
| 125 | + suite.True(job.IsTerminal()) |
| 126 | + |
| 127 | + job.State.StateType = models.JobStateTypeRunning |
| 128 | + suite.False(job.IsTerminal()) |
| 129 | +} |
| 130 | + |
| 131 | +func (suite *JobTestSuite) TestNamespacedID() { |
| 132 | + job := mock.Job() |
| 133 | + nsID := job.NamespacedID() |
| 134 | + suite.Equal(job.ID, nsID.ID) |
| 135 | + suite.Equal(job.Namespace, nsID.Namespace) |
| 136 | +} |
| 137 | + |
| 138 | +func (suite *JobTestSuite) TestAllStorageTypes() { |
| 139 | + job := mock.Job() |
| 140 | + job.Tasks = []*models.Task{ |
| 141 | + { |
| 142 | + InputSources: []*models.InputSource{ |
| 143 | + { |
| 144 | + Source: &models.SpecConfig{ |
| 145 | + Type: "s3", |
| 146 | + }, |
| 147 | + }, |
| 148 | + { |
| 149 | + Source: &models.SpecConfig{ |
| 150 | + Type: "url", |
| 151 | + }, |
| 152 | + }, |
| 153 | + }, |
| 154 | + }, |
| 155 | + } |
| 156 | + |
| 157 | + storageTypes := job.AllStorageTypes() |
| 158 | + suite.ElementsMatch([]string{"s3", "url"}, storageTypes) |
| 159 | +} |
| 160 | + |
| 161 | +// Run the test suite |
| 162 | +func TestJobTestSuite(t *testing.T) { |
| 163 | + suite.Run(t, new(JobTestSuite)) |
| 164 | +} |
0 commit comments