summaryrefslogtreecommitdiff
path: root/manager/dryad_job_manager_test.go
blob: 795c99c6ef112af8ca780238b85c86ab4639ff07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
 *  Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
 *
 *  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 manager

import (
	"sync"

	. "git.tizen.org/tools/weles"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/ginkgo/extensions/table"
	. "github.com/onsi/gomega"
)

var _ = Describe("DryadJobManager", func() {
	var djm DryadJobManager
	jobID := JobID(666)

	BeforeEach(func() {
		djm = NewDryadJobManager()
	})

	create := func() {
		err := djm.Create(jobID, Dryad{}, Config{}, nil)
		Expect(err).ToNot(HaveOccurred())
	}

	It("should work for a single job", func() {
		By("create")
		create()

		By("cancel")
		err := djm.Cancel(jobID)
		Expect(err).ToNot(HaveOccurred())
	})

	It("should fail to duplicate jobs", func() {
		create()

		err := djm.Create(jobID, Dryad{}, Config{}, nil)
		Expect(err).To(Equal(ErrDuplicated))
	})

	It("should fail to cancel non-existing job", func() {
		err := djm.Cancel(jobID)
		Expect(err).To(Equal(ErrNotExist))
	})

	Describe("list", func() {
		var list []DryadJobInfo

		createN := func(start, end int, status DryadJobStatus) {
			dj := djm.(*DryadJobs)
			for i := start; i <= end; i++ {
				id := JobID(i)
				info := DryadJobInfo{
					Job:    id,
					Status: status,
				}
				dj.jobs[id] = &dryadJob{
					mutex: new(sync.Mutex),
					info:  info,
				}
				list = append(list, info)
			}
		}

		BeforeEach(func() {
			list = make([]DryadJobInfo, 0, 11)
			createN(0, 2, DJ_NEW)
			createN(3, 5, DJ_DEPLOY)
			createN(6, 8, DJ_BOOT)
			createN(9, 11, DJ_TEST)
		})

		It("should list created jobs", func() {
			l, err := djm.List(nil)
			Expect(err).ToNot(HaveOccurred())
			Expect(l).To(HaveLen(len(list)))
		})

		DescribeTable("list of jobs with status",
			func(start, end int, s []DryadJobStatus) {
				l, err := djm.List(&DryadJobFilter{
					Statuses: s,
				})
				Expect(err).ToNot(HaveOccurred())
				Expect(l).To(HaveLen(end - start + 1))
				for _, j := range l {
					Expect(s).To(ContainElement(j.Status))
					Expect(j.Job).To(BeNumerically(">=", start))
					Expect(j.Job).To(BeNumerically("<=", end))
				}
			},
			Entry("NEW", 0, 2, []DryadJobStatus{DJ_NEW}),
			Entry("DEPLOY", 3, 5, []DryadJobStatus{DJ_DEPLOY}),
			Entry("BOOT", 6, 8, []DryadJobStatus{DJ_BOOT}),
			Entry("TEST", 9, 11, []DryadJobStatus{DJ_TEST}),
			Entry("NEW and DEPLOY", 0, 5, []DryadJobStatus{DJ_NEW, DJ_DEPLOY}),
		)

		DescribeTable("list of jobs with id",
			func(ids []JobID, exp []int) {
				l, err := djm.List(&DryadJobFilter{
					References: ids,
				})
				Expect(err).ToNot(HaveOccurred())
				Expect(l).To(HaveLen(len(exp)))
				expected := make([]DryadJobInfo, 0)
				for _, i := range exp {
					expected = append(expected, list[i])
				}
				for _, j := range l {
					Expect(expected).To(ContainElement(Equal(j)))
				}
			},
			Entry("any - 0", []JobID{0}, []int{0}),
			Entry("any - 10", []JobID{10}, []int{10}),
			Entry("out of bounds - 128", []JobID{128}, []int{}),
			Entry("many - 1 and 8", []JobID{1, 8}, []int{1, 8}),
		)

		DescribeTable("list of jobs with status and id",
			func(filter DryadJobFilter, exp []int) {
				l, err := djm.List(&filter)
				Expect(err).ToNot(HaveOccurred())
				Expect(l).To(HaveLen(len(exp)))
				expected := make([]DryadJobInfo, 0)
				for _, i := range exp {
					expected = append(expected, list[i])
				}
				for _, j := range l {
					Expect(expected).To(ContainElement(Equal(j)))
				}
			},
			Entry("NEW - 2, 3", DryadJobFilter{
				References: []JobID{2, 3},
				Statuses:   []DryadJobStatus{DJ_NEW},
			}, []int{2}),
			Entry("NEW, TEST - 0, 6, 8, 10, 11", DryadJobFilter{
				References: []JobID{0, 6, 8, 10, 11},
				Statuses:   []DryadJobStatus{DJ_NEW, DJ_TEST},
			}, []int{0, 10, 11}),
		)
	})
})