Skip to content

Commit fa27190

Browse files
authored
make job name optional (#4005)
1 parent 5c482d4 commit fa27190

File tree

4 files changed

+183
-2
lines changed

4 files changed

+183
-2
lines changed

pkg/models/job.go

+4
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,10 @@ func (j *Job) Normalize() {
162162
j.Namespace = DefaultNamespace
163163
}
164164

165+
if j.Name == "" {
166+
j.Name = j.ID
167+
}
168+
165169
if (j.Type == JobTypeDaemon || j.Type == JobTypeOps) && j.Count == 0 {
166170
j.Count = 1
167171
}

pkg/models/job_test.go

+164
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
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+
}

testdata/data.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ import (
66
"os"
77
"testing"
88

9-
"github.com/bacalhau-project/bacalhau/pkg/lib/marshaller"
10-
"github.com/bacalhau-project/bacalhau/pkg/models"
119
"github.com/ipld/go-ipld-prime/codec/json"
1210
"github.com/stretchr/testify/require"
1311

12+
"github.com/bacalhau-project/bacalhau/pkg/lib/marshaller"
13+
"github.com/bacalhau-project/bacalhau/pkg/models"
14+
1415
"github.com/bacalhau-project/bacalhau/pkg/model"
1516
)
1617

@@ -65,6 +66,9 @@ var dockerS3YAML []byte
6566
//go:embed jobs/empty.yaml
6667
var emptyJobYAML []byte
6768

69+
//go:embed jobs/nameless.yaml
70+
var namelessJobYAML []byte
71+
6872
//go:embed jobs/noop.yaml
6973
var noopJobYAML []byte
7074

@@ -92,6 +96,7 @@ var (
9296
DockerOutputJSON *Fixture
9397
DockerS3YAML *Fixture
9498
EmptyJobYAML *Fixture
99+
NamelessJobYAML *Fixture
95100
NoopJobYAML *Fixture
96101
WasmJobYAML *Fixture
97102
)
@@ -121,6 +126,7 @@ func init() {
121126
DockerOutputJSON = NewJobFixture("docker with output json", dockerOutputJSON, false)
122127
DockerS3YAML = NewJobFixture("docker with s3", dockerS3YAML, false)
123128
EmptyJobYAML = NewJobFixture("empty job", emptyJobYAML, true)
129+
NamelessJobYAML = NewJobFixture("nameless job", namelessJobYAML, false)
124130
NoopJobYAML = NewJobFixture("noop", noopJobYAML, false)
125131
WasmJobYAML = NewJobFixture("wasm", wasmJobYAML, false)
126132
}
@@ -139,6 +145,7 @@ func AllFixtures() []*Fixture {
139145
DockerOutputJSON,
140146
DockerS3YAML,
141147
EmptyJobYAML,
148+
NamelessJobYAML,
142149
NoopJobYAML,
143150
WasmJobYAML,
144151
}

testdata/jobs/nameless.yaml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Type: batch
2+
Count: 1
3+
Tasks:
4+
- Name: main
5+
Engine:
6+
Type: noop

0 commit comments

Comments
 (0)