From afcdaa84b8c9ddfeae60404364704c1d6d3b2181 Mon Sep 17 00:00:00 2001 From: Renan DelValle Date: Fri, 28 Dec 2018 11:46:11 -0800 Subject: [PATCH] Initial support for generating Thermos data objects. --- thermos.go | 49 ++++++++++++++++++++++++++++++++++++ thermos_test.go | 66 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 thermos.go create mode 100644 thermos_test.go diff --git a/thermos.go b/thermos.go new file mode 100644 index 0000000..c5ce098 --- /dev/null +++ b/thermos.go @@ -0,0 +1,49 @@ +/** + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package realis + + +type ThermosExecutor struct { + Task ThermosTask `json:"task""` +} + +type ThermosTask struct { + Processes []ThermosProcess `json:"processes"` + Constraints []ThermosConstraint `json:"constraints"` + Resources ThermosResources `json:"resources"` +} + +type ThermosConstraint struct { + Order []string `json:"order"` +} + +type ThermosResources struct { + CPU float64 `json:"cpu"` + Disk int `json:"disk"` + RAM int `json:"ram"` + GPU int `json:"gpu"` +} + +type ThermosProcess struct { + Daemon bool `json:"daemon"` + Name string `json:"name"` + Ephemeral bool `json:"ephemeral"` + MaxFailures int `json:"max_failures"` + MinDuration int `json:"min_duration"` + Cmdline string `json:"cmdline"` + Final bool `json:"final` +} + + diff --git a/thermos_test.go b/thermos_test.go new file mode 100644 index 0000000..a3baa3b --- /dev/null +++ b/thermos_test.go @@ -0,0 +1,66 @@ +package realis_test + +import ( + "encoding/json" + "testing" + + "github.com/paypal/gorealis/v2" + "github.com/stretchr/testify/assert" +) + +func TestThermosTask(t *testing.T) { + + // Test that we can successfully deserialize a minimum subset of an Aurora generated thermos payload + thermosJSON := []byte( + `{ + "task": { + "processes": [ + { + "daemon": false, + "name": "hello", + "ephemeral": false, + "max_failures": 1, + "min_duration": 5, + "cmdline": "\n while true; do\n echo hello world from gorealis\n sleep 10\n done\n ", + "final": false + } + ], + "resources": { + "gpu": 0, + "disk": 134217728, + "ram": 134217728, + "cpu": 1.1 + }, + "constraints": [ + { + "order": [ + "hello" + ] + } + ] + } +}`) + thermos := realis.ThermosExecutor{} + + err := json.Unmarshal(thermosJSON, &thermos) + + assert.NoError(t, err) + + thermosExpected := realis.ThermosExecutor{ + Task: realis.ThermosTask{ + Processes: []realis.ThermosProcess{ + { + Daemon: false, + Name: "hello", + Ephemeral: false, + MaxFailures: 1, + MinDuration: 5, + Cmdline: "\n while true; do\n echo hello world from gorealis\n sleep 10\n done\n ", + Final: false, + }, + }, + Constraints: []realis.ThermosConstraint{{Order: []string{"hello"}}}, + Resources: realis.ThermosResources{CPU: 1.1, Disk: 134217728, RAM: 134217728, GPU: 0}}} + + assert.ObjectsAreEqual(thermosExpected, thermos) +}