From 9a6051c089e86f46b55fd3e50b6ece38318c6754 Mon Sep 17 00:00:00 2001 From: Renan DelValle Date: Sat, 1 Oct 2016 01:54:11 -0400 Subject: [PATCH] Fixed clusters.json loader tester. Initial testing for CreateJob, KillJob, ScheduleCronJob, StartCronJob, DeschedulerCronJob --- clusters_test.go | 12 +-- examples/thermos_cron_payload.json | 62 +++++++++++++ realis_test.go | 138 +++++++++++++++++++++++++++++ 3 files changed, 206 insertions(+), 6 deletions(-) create mode 100644 examples/thermos_cron_payload.json create mode 100644 realis_test.go diff --git a/clusters_test.go b/clusters_test.go index ab79120..2e7118b 100644 --- a/clusters_test.go +++ b/clusters_test.go @@ -27,10 +27,10 @@ func TestLoadClusters(t *testing.T) { fmt.Print(err) } - assert.Equal(t, clusters[0].Name, "devcluster") - assert.Equal(t, clusters[0].ZK, "192.168.33.7") - assert.Equal(t, clusters[0].SchedZKPath, "/aurora/scheduler") - assert.Equal(t, clusters[0].AuthMechanism, "UNAUTHENTICATED") - assert.Equal(t, clusters[0].AgentRunDir, "latest") - assert.Equal(t, clusters[0].AgentRoot, "/var/lib/mesos") + assert.Equal(t, clusters["devcluster"].Name, "devcluster") + assert.Equal(t, clusters["devcluster"].ZK, "192.168.33.7") + assert.Equal(t, clusters["devcluster"].SchedZKPath, "/aurora/scheduler") + assert.Equal(t, clusters["devcluster"].AuthMechanism, "UNAUTHENTICATED") + assert.Equal(t, clusters["devcluster"].AgentRunDir, "latest") + assert.Equal(t, clusters["devcluster"].AgentRoot, "/var/lib/mesos") } diff --git a/examples/thermos_cron_payload.json b/examples/thermos_cron_payload.json new file mode 100644 index 0000000..fe7f8fa --- /dev/null +++ b/examples/thermos_cron_payload.json @@ -0,0 +1,62 @@ +{ + "environment": "prod", + "health_check_config": { + "initial_interval_secs": 15.0, + "health_checker": { + "http": { + "expected_response_code": 0, + "endpoint": "/health", + "expected_response": "ok" + } + }, + "interval_secs": 10.0, + "timeout_secs": 1.0, + "max_consecutive_failures": 0 + }, + "name": "hello_world_from_gorealis", + "service": false, + "max_task_failures": 1, + "cron_collision_policy": "KILL_EXISTING", + "enable_hooks": false, + "cluster": "devcluster", + "task": { + "processes": [ + { + "daemon": false, + "name": "hello", + "ephemeral": false, + "max_failures": 1, + "min_duration": 5, + "cmdline": "echo hello world from gorealis; sleep 10", + "final": false + } + ], + "name": "hello", + "finalization_wait": 30, + "max_failures": 1, + "max_concurrency": 0, + "resources": { + "gpu": 0, + "disk": 134217728, + "ram": 134217728, + "cpu": 1.0 + }, + "constraints": [ + { + "order": [ + "hello" + ] + } + ] + }, + "production": false, + "role": "vagrant", + "lifecycle": { + "http": { + "graceful_shutdown_endpoint": "/quitquitquit", + "port": "health", + "shutdown_endpoint": "/abortabortabort" + } + }, + "priority": 0 +} \ No newline at end of file diff --git a/realis_test.go b/realis_test.go new file mode 100644 index 0000000..cfd286c --- /dev/null +++ b/realis_test.go @@ -0,0 +1,138 @@ +/** + * 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 + +import ( + "testing" + "github.com/stretchr/testify/assert" + "fmt" + "os" + "io/ioutil" + "github.com/rdelval/gorealis/gen-go/apache/aurora" +) + +var r Realis +var thermosPayload []byte + +func TestMain(m *testing.M) { + // New configuration to connect to Vagrant image + config, err := NewDefaultConfig("http://192.168.33.7:8081") + if err != nil { + fmt.Println("Please run vagrant box before running test suite") + os.Exit(1) + } + + thermosPayload, err = ioutil.ReadFile("examples/thermos_payload.json") + if err != nil { + fmt.Println("Error reading thermos payload file: ", err) + os.Exit(1) + } + + // Configured for vagrant + AddBasicAuth(&config, "aurora", "secret") + r = NewClient(config) + + os.Exit(m.Run()) +} + +func TestRealisClient_CreateJob_Thermos(t *testing.T) { + + job := NewJob(). + Environment("prod"). + Role("vagrant"). + Name("create_job_test"). + ExecutorName(aurora.AURORA_EXECUTOR_NAME). + ExecutorData(string(thermosPayload)). + CPU(1). + RAM(64). + Disk(100). + IsService(true). + InstanceCount(1). + AddPorts(1) + + resp, err := r.CreateJob(job) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + + assert.Equal(t, aurora.ResponseCode_OK, resp.ResponseCode) + + // Tasks must exist for it to be killed + t.Run("TestRealisClient_KillJob_Thermos", func(t *testing.T){ + resp, err := r.KillJob(job.JobKey()) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + + assert.Equal(t, aurora.ResponseCode_OK, resp.ResponseCode) + }) +} + +func TestRealisClient_ScheduleCronJob_Thermos(t *testing.T) { + + thermosCronPayload, err := ioutil.ReadFile("examples/thermos_cron_payload.json") + if err != nil { + fmt.Println("Error reading thermos payload file: ", err) + os.Exit(1) + } + + job := NewJob(). + Environment("prod"). + Role("vagrant"). + Name("cronsched_job_test"). + ExecutorName(aurora.AURORA_EXECUTOR_NAME). + ExecutorData(string(thermosCronPayload)). + CPU(1). + RAM(64). + Disk(100). + IsService(true). + InstanceCount(1). + AddPorts(1). + CronSchedule("* * * * *"). + IsService(false) + + resp, err := r.ScheduleCronJob(job) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + + assert.Equal(t, aurora.ResponseCode_OK, resp.ResponseCode) + + + // Tasks must exist for it to be killed + t.Run("TestRealisClient_StartCronJob_Thermos", func(t *testing.T){ + resp, err := r.StartCronJob(job.JobKey()) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + + assert.Equal(t, aurora.ResponseCode_OK, resp.ResponseCode) + }) + + // Tasks must exist for it to be killed + t.Run("TestRealisClient_DeschedulerCronJob_Thermos", func(t *testing.T){ + resp, err := r.DescheduleCronJob(job.JobKey()) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + + assert.Equal(t, aurora.ResponseCode_OK, resp.ResponseCode) + }) +} \ No newline at end of file