job_artifact_test.go 2.8 KB
Newer Older
1 2 3
package cmd

import (
4 5
	"bytes"
	"fmt"
6 7 8 9 10
	"io/ioutil"
	"os"

	"github.com/golang/mock/gomock"
	"github.com/jenkins-zh/jenkins-cli/client"
11
	"github.com/jenkins-zh/jenkins-cli/mock/mhttp"
12 13
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
14 15 16 17 18 19
)

var _ = Describe("job artifact command", func() {
	var (
		ctrl         *gomock.Controller
		roundTripper *mhttp.MockRoundTripper
20 21
		buildID      int
		jobName      string
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
	)

	BeforeEach(func() {
		ctrl = gomock.NewController(GinkgoT())
		roundTripper = mhttp.NewMockRoundTripper(ctrl)
		rootCmd.SetArgs([]string{})
		rootOptions.Jenkins = ""
		rootOptions.ConfigFile = "test.yaml"
		buildID = 1
		jobName = "fakeJob"

		jobArtifactOption.RoundTripper = roundTripper
	})

	AfterEach(func() {
		rootCmd.SetArgs([]string{})
		os.Remove(rootOptions.ConfigFile)
		rootOptions.ConfigFile = ""
		ctrl.Finish()
	})

	Context("basic cases", func() {
		It("lack of arguments", func() {
			buf := new(bytes.Buffer)
			rootCmd.SetOutput(buf)

			rootCmd.SetArgs([]string{"job", "artifact"})
			_, err := rootCmd.ExecuteC()
50 51
			Expect(err).To(HaveOccurred())
			Expect(err.Error()).To(ContainSubstring("requires at least 1 arg(s), only received 0"))
52 53 54
		})

		It("should success", func() {
LinuxSuRen's avatar
LinuxSuRen 已提交
55
			data, err := GenerateSampleConfig()
56 57 58 59 60 61 62 63 64 65 66 67 68
			Expect(err).To(BeNil())
			err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664)
			Expect(err).To(BeNil())

			client.PrepareGetArtifacts(roundTripper, "http://localhost:8080/jenkins", "admin", "111e3a2f0231198855dceaff96f20540a9", jobName, buildID)

			buf := new(bytes.Buffer)
			rootCmd.SetOutput(buf)

			rootCmd.SetArgs([]string{"job", "artifact", jobName, fmt.Sprintf("%d", buildID)})
			_, err = rootCmd.ExecuteC()
			Expect(err).To(BeNil())

69 70
			Expect(buf.String()).To(Equal(`Name  Path  Size
a.log a.log 0
71 72 73 74
`))
		})

		It("should success, zero build id", func() {
LinuxSuRen's avatar
LinuxSuRen 已提交
75
			data, err := GenerateSampleConfig()
76 77 78 79 80 81 82 83 84 85 86 87 88 89
			Expect(err).To(BeNil())
			err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664)
			Expect(err).To(BeNil())
			buildID = 0

			client.PrepareGetArtifacts(roundTripper, "http://localhost:8080/jenkins", "admin", "111e3a2f0231198855dceaff96f20540a9", jobName, buildID)

			buf := new(bytes.Buffer)
			rootCmd.SetOutput(buf)

			rootCmd.SetArgs([]string{"job", "artifact", jobName, fmt.Sprintf("%d", buildID)})
			_, err = rootCmd.ExecuteC()
			Expect(err).To(BeNil())

90 91
			Expect(buf.String()).To(Equal(`Name  Path  Size
a.log a.log 0
92 93 94 95
`))
		})

		It("should success, invalid build id", func() {
LinuxSuRen's avatar
LinuxSuRen 已提交
96
			data, err := GenerateSampleConfig()
97 98 99 100 101 102 103 104 105
			Expect(err).To(BeNil())
			err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664)
			Expect(err).To(BeNil())

			buf := new(bytes.Buffer)
			rootCmd.SetOutput(buf)

			rootCmd.SetArgs([]string{"job", "artifact", jobName, "invalid"})
			_, err = rootCmd.ExecuteC()
106 107
			Expect(err).To(HaveOccurred())
			Expect(err.Error()).To(ContainSubstring("invalid syntax"))
108 109 110
		})
	})
})