Files
kubernetes-entrypoint/dependencies/job/job_test.go
Sergiy Markin 4ae2c3a101 Fixed lint and unit tests
This PS makes sure we have linter and unit tests
processed. The code has been reformatted to adhere
to Go's code formatting conventions.

Change-Id: I31f15d6d6c4b9bda7e3837941b6c9c3c3735aea7
2024-03-26 19:41:48 +00:00

83 lines
2.2 KiB
Go

package job
import (
"context"
"fmt"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"opendev.org/airship/kubernetes-entrypoint/entrypoint"
"opendev.org/airship/kubernetes-entrypoint/mocks"
)
const (
testJobName = "TEST_JOB_NAME"
testJobNamespace = "TEST_JOB_NAMESPACE"
)
var testLabels = map[string]string{
"k1": "v1",
}
var testEntrypoint entrypoint.EntrypointInterface
var _ = Describe("Job", func() {
BeforeEach(func() {
testEntrypoint = mocks.NewEntrypoint()
})
It("constructor correctly assigns fields", func() {
nameJob := NewJob(testJobName, testJobNamespace, nil)
Expect(nameJob.name).To(Equal(testJobName))
Expect(nameJob.namespace).To(Equal(testJobNamespace))
labelsJob := NewJob("", testJobNamespace, testLabels)
Expect(labelsJob.labels).To(Equal(testLabels))
})
It("constructor returns nil when both name and labels specified", func() {
job := NewJob(testJobName, testJobNamespace, testLabels)
Expect(job).To(BeNil())
})
It("checks resolution of a succeeding job by name", func() {
job := NewJob(mocks.SucceedingJobName, mocks.SucceedingJobName, nil)
isResolved, err := job.IsResolved(context.TODO(), testEntrypoint)
Expect(isResolved).To(Equal(true))
Expect(err).NotTo(HaveOccurred())
})
It("checks resolution failure of a failing job by name", func() {
job := NewJob(mocks.FailingJobName, mocks.FailingJobName, nil)
isResolved, err := job.IsResolved(context.TODO(), testEntrypoint)
Expect(isResolved).To(Equal(false))
Expect(err.Error()).To(Equal(fmt.Sprintf(FailingStatusFormat, job)))
})
It("checks resolution of a succeeding job by labels", func() {
job := NewJob("", mocks.SucceedingJobName, map[string]string{"name": mocks.SucceedingJobLabel})
isResolved, err := job.IsResolved(context.TODO(), testEntrypoint)
Expect(isResolved).To(Equal(true))
Expect(err).NotTo(HaveOccurred())
})
It("checks resolution failure of a failing job by labels", func() {
job := NewJob("", mocks.FailingJobName, map[string]string{"name": mocks.FailingJobLabel})
isResolved, err := job.IsResolved(context.TODO(), testEntrypoint)
Expect(isResolved).To(Equal(false))
Expect(err.Error()).To(Equal(fmt.Sprintf(FailingStatusFormat, job)))
})
})