Skip to content
This repository has been archived by the owner on Nov 8, 2022. It is now read-only.

Commit

Permalink
Move makeSchedule tests from core/task_medium_test.go to core/schedul…
Browse files Browse the repository at this point in the history
…e_small_test.go
  • Loading branch information
geauxvirtual committed Aug 23, 2016
1 parent 8741dc2 commit f131ba3
Show file tree
Hide file tree
Showing 2 changed files with 168 additions and 135 deletions.
168 changes: 168 additions & 0 deletions core/schedule_small_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
// +build small

/*
http://www.apache.org/licenses/LICENSE-2.0.txt
Copyright 2016 Intel Corporation
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 core

import (
"fmt"
"testing"
"time"

. "github.com/smartystreets/goconvey/convey"
)

const (
DUMMY_TYPE = "dummy"
)

func TestMakeSchedule(t *testing.T) {

Convey("Bad schedule type", t, func() {
sched1 := &Schedule{Type: DUMMY_TYPE}
rsched, err := makeSchedule(*sched1)
So(rsched, ShouldBeNil)
So(err, ShouldNotBeNil)
So(err.Error(), ShouldEqual, fmt.Sprintf("unknown schedule type %s", DUMMY_TYPE))
})

Convey("Simple schedule with bad duration", t, func() {
sched1 := &Schedule{Type: "simple", Interval: "dummy"}
rsched, err := makeSchedule(*sched1)
So(rsched, ShouldBeNil)
So(err, ShouldNotBeNil)
So(err.Error(), ShouldStartWith, "time: invalid duration ")
})

Convey("Simple schedule with invalid duration", t, func() {
sched1 := &Schedule{Type: "simple", Interval: "-1s"}
rsched, err := makeSchedule(*sched1)
So(rsched, ShouldBeNil)
So(err, ShouldNotBeNil)
So(err.Error(), ShouldEqual, "Interval must be greater than 0")
})

Convey("Simple schedule with proper duration", t, func() {
sched1 := &Schedule{Type: "simple", Interval: "1s"}
rsched, err := makeSchedule(*sched1)
So(err, ShouldBeNil)
So(rsched, ShouldNotBeNil)
So(rsched.GetState(), ShouldEqual, 0)
})

Convey("Windowed schedule with bad duration", t, func() {
sched1 := &Schedule{Type: "windowed", Interval: "dummy"}
rsched, err := makeSchedule(*sched1)
So(rsched, ShouldBeNil)
So(err, ShouldNotBeNil)
So(err.Error(), ShouldStartWith, "time: invalid duration ")
})

Convey("Windowed schedule with invalid duration", t, func() {
sched1 := &Schedule{Type: "windowed", Interval: "-1s"}
rsched, err := makeSchedule(*sched1)
So(rsched, ShouldBeNil)
So(err, ShouldNotBeNil)
So(err.Error(), ShouldEqual, "Interval must be greater than 0")
})

Convey("Windowed schedule with stop in the past", t, func() {
now := time.Now()
startSecs := now.Unix()
stopSecs := startSecs - 3600
sched1 := &Schedule{Type: "windowed", Interval: "1s",
StartTimestamp: &startSecs, StopTimestamp: &stopSecs}
rsched, err := makeSchedule(*sched1)
So(rsched, ShouldBeNil)
So(err, ShouldNotBeNil)
So(err.Error(), ShouldEqual, "Stop time is in the past")
})

Convey("Windowed schedule with stop before start", t, func() {
now := time.Now()
startSecs := now.Unix()
stopSecs := startSecs + 600
startSecs = stopSecs + 600
sched1 := &Schedule{Type: "windowed", Interval: "1s",
StartTimestamp: &startSecs, StopTimestamp: &stopSecs}
rsched, err := makeSchedule(*sched1)
So(rsched, ShouldBeNil)
So(err, ShouldNotBeNil)
So(err.Error(), ShouldEqual, "Stop time cannot occur before start time")
})

Convey("Windowed schedule with stop before start", t, func() {
now := time.Now()
startSecs := now.Unix()
stopSecs := startSecs + 600
sched1 := &Schedule{Type: "windowed", Interval: "1s",
StartTimestamp: &startSecs, StopTimestamp: &stopSecs}
rsched, err := makeSchedule(*sched1)
So(err, ShouldBeNil)
So(rsched, ShouldNotBeNil)
So(rsched.GetState(), ShouldEqual, 0)
})

Convey("Cron schedule with bad duration", t, func() {
sched1 := &Schedule{Type: "cron", Interval: ""}
rsched, err := makeSchedule(*sched1)
So(rsched, ShouldBeNil)
So(err, ShouldNotBeNil)
So(err.Error(), ShouldEqual, "missing cron entry")
})

Convey("Cron schedule with invalid duration", t, func() {
sched1 := &Schedule{Type: "windowed", Interval: "-1s"}
rsched, err := makeSchedule(*sched1)
So(rsched, ShouldBeNil)
So(err, ShouldNotBeNil)
So(err.Error(), ShouldEqual, "Interval must be greater than 0")
})

Convey("Cron schedule with too few fields entry", t, func() {
sched1 := &Schedule{Type: "cron", Interval: "1 2 3"}
rsched, err := makeSchedule(*sched1)
So(rsched, ShouldBeNil)
So(err, ShouldNotBeNil)
So(err.Error(), ShouldStartWith, "Expected 5 or 6 fields, found ")
})

Convey("Cron schedule with 5 fields entry", t, func() {
sched1 := &Schedule{Type: "cron", Interval: "1 2 3 4 5"}
rsched, err := makeSchedule(*sched1)
So(err, ShouldBeNil)
So(rsched, ShouldNotBeNil)
})

Convey("Cron schedule with 6 fields entry", t, func() {
sched1 := &Schedule{Type: "cron", Interval: "1 2 3 4 5 6"}
rsched, err := makeSchedule(*sched1)
So(err, ShouldBeNil)
So(rsched, ShouldNotBeNil)
})

Convey("Cron schedule with too many fields entry", t, func() {
sched1 := &Schedule{Type: "cron", Interval: "1 2 3 4 5 6 7 8"}
rsched, err := makeSchedule(*sched1)
So(rsched, ShouldBeNil)
So(err, ShouldNotBeNil)
So(err.Error(), ShouldStartWith, "Expected 5 or 6 fields, found ")
})
}
135 changes: 0 additions & 135 deletions core/task_medium_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"fmt"
"os"
"testing"
"time"

"github.com/intelsdi-x/snap/core/serror"
"github.com/intelsdi-x/snap/pkg/schedule"
Expand Down Expand Up @@ -146,140 +145,6 @@ func TestCreateTaskRequest(t *testing.T) {
})
}

func TestMakeSchedule(t *testing.T) {

Convey("Bad schedule type", t, func() {
sched1 := &Schedule{Type: DUMMY_TYPE}
rsched, err := makeSchedule(*sched1)
So(rsched, ShouldBeNil)
So(err, ShouldNotBeNil)
So(err.Error(), ShouldEqual, fmt.Sprintf("unknown schedule type %s", DUMMY_TYPE))
})

Convey("Simple schedule with bad duration", t, func() {
sched1 := &Schedule{Type: "simple", Interval: "dummy"}
rsched, err := makeSchedule(*sched1)
So(rsched, ShouldBeNil)
So(err, ShouldNotBeNil)
So(err.Error(), ShouldStartWith, "time: invalid duration ")
})

Convey("Simple schedule with invalid duration", t, func() {
sched1 := &Schedule{Type: "simple", Interval: "-1s"}
rsched, err := makeSchedule(*sched1)
So(rsched, ShouldBeNil)
So(err, ShouldNotBeNil)
So(err.Error(), ShouldEqual, "Interval must be greater than 0")
})

Convey("Simple schedule with proper duration", t, func() {
sched1 := &Schedule{Type: "simple", Interval: "1s"}
rsched, err := makeSchedule(*sched1)
So(err, ShouldBeNil)
So(rsched, ShouldNotBeNil)
So(rsched.GetState(), ShouldEqual, 0)
})

Convey("Windowed schedule with bad duration", t, func() {
sched1 := &Schedule{Type: "windowed", Interval: "dummy"}
rsched, err := makeSchedule(*sched1)
So(rsched, ShouldBeNil)
So(err, ShouldNotBeNil)
So(err.Error(), ShouldStartWith, "time: invalid duration ")
})

Convey("Windowed schedule with invalid duration", t, func() {
sched1 := &Schedule{Type: "windowed", Interval: "-1s"}
rsched, err := makeSchedule(*sched1)
So(rsched, ShouldBeNil)
So(err, ShouldNotBeNil)
So(err.Error(), ShouldEqual, "Interval must be greater than 0")
})

Convey("Windowed schedule with stop in the past", t, func() {
now := time.Now()
startSecs := now.Unix()
stopSecs := startSecs - 3600
sched1 := &Schedule{Type: "windowed", Interval: "1s",
StartTimestamp: &startSecs, StopTimestamp: &stopSecs}
rsched, err := makeSchedule(*sched1)
So(rsched, ShouldBeNil)
So(err, ShouldNotBeNil)
So(err.Error(), ShouldEqual, "Stop time is in the past")
})

Convey("Windowed schedule with stop before start", t, func() {
now := time.Now()
startSecs := now.Unix()
stopSecs := startSecs + 600
startSecs = stopSecs + 600
sched1 := &Schedule{Type: "windowed", Interval: "1s",
StartTimestamp: &startSecs, StopTimestamp: &stopSecs}
rsched, err := makeSchedule(*sched1)
So(rsched, ShouldBeNil)
So(err, ShouldNotBeNil)
So(err.Error(), ShouldEqual, "Stop time cannot occur before start time")
})

Convey("Windowed schedule with stop before start", t, func() {
now := time.Now()
startSecs := now.Unix()
stopSecs := startSecs + 600
sched1 := &Schedule{Type: "windowed", Interval: "1s",
StartTimestamp: &startSecs, StopTimestamp: &stopSecs}
rsched, err := makeSchedule(*sched1)
So(err, ShouldBeNil)
So(rsched, ShouldNotBeNil)
So(rsched.GetState(), ShouldEqual, 0)
})

Convey("Cron schedule with bad duration", t, func() {
sched1 := &Schedule{Type: "cron", Interval: ""}
rsched, err := makeSchedule(*sched1)
So(rsched, ShouldBeNil)
So(err, ShouldNotBeNil)
So(err.Error(), ShouldEqual, "missing cron entry")
})

Convey("Cron schedule with invalid duration", t, func() {
sched1 := &Schedule{Type: "windowed", Interval: "-1s"}
rsched, err := makeSchedule(*sched1)
So(rsched, ShouldBeNil)
So(err, ShouldNotBeNil)
So(err.Error(), ShouldEqual, "Interval must be greater than 0")
})

Convey("Cron schedule with too few fields entry", t, func() {
sched1 := &Schedule{Type: "cron", Interval: "1 2 3"}
rsched, err := makeSchedule(*sched1)
So(rsched, ShouldBeNil)
So(err, ShouldNotBeNil)
So(err.Error(), ShouldStartWith, "Expected 5 or 6 fields, found ")
})

Convey("Cron schedule with 5 fields entry", t, func() {
sched1 := &Schedule{Type: "cron", Interval: "1 2 3 4 5"}
rsched, err := makeSchedule(*sched1)
So(err, ShouldBeNil)
So(rsched, ShouldNotBeNil)
})

Convey("Cron schedule with 6 fields entry", t, func() {
sched1 := &Schedule{Type: "cron", Interval: "1 2 3 4 5 6"}
rsched, err := makeSchedule(*sched1)
So(err, ShouldBeNil)
So(rsched, ShouldNotBeNil)
})

Convey("Cron schedule with too many fields entry", t, func() {
sched1 := &Schedule{Type: "cron", Interval: "1 2 3 4 5 6 7 8"}
rsched, err := makeSchedule(*sched1)
So(rsched, ShouldBeNil)
So(err, ShouldNotBeNil)
So(err.Error(), ShouldStartWith, "Expected 5 or 6 fields, found ")
})
}

func TestCreateTaskFromContent(t *testing.T) {

Convey("Non existing file", t, func() {
Expand Down

0 comments on commit f131ba3

Please sign in to comment.