center_download_test.go 3.0 KB
Newer Older
1 2 3 4 5 6 7
package cmd

import (
	"bytes"
	"github.com/golang/mock/gomock"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
LinuxSuRen's avatar
LinuxSuRen 已提交
8 9 10
	"io/ioutil"
	"net/http"
	"os"
11 12 13 14 15 16 17 18 19

	"github.com/jenkins-zh/jenkins-cli/mock/mhttp"
)

var _ = Describe("center download command", func() {
	var (
		ctrl           *gomock.Controller
		roundTripper   *mhttp.MockRoundTripper
		targetFilePath string
20
		tempFile       *os.File
LinuxSuRen's avatar
LinuxSuRen 已提交
21

22
		ltsResponseBody    string
LinuxSuRen's avatar
LinuxSuRen 已提交
23 24 25
		weeklyResponseBody string

		err error
26 27 28 29 30
	)

	BeforeEach(func() {
		ctrl = gomock.NewController(GinkgoT())
		roundTripper = mhttp.NewMockRoundTripper(ctrl)
LinuxSuRen's avatar
LinuxSuRen 已提交
31
		centerDownloadOption.RoundTripper = roundTripper
32 33
		tempFile, err = ioutil.TempFile(".", "jenkins.war")
		Expect(err).NotTo(HaveOccurred())
LinuxSuRen's avatar
LinuxSuRen 已提交
34

35
		targetFilePath = tempFile.Name()
36 37 38
		rootOptions.Jenkins = ""
		rootOptions.ConfigFile = "test.yaml"

LinuxSuRen's avatar
LinuxSuRen 已提交
39 40
		ltsResponseBody = "lts"
		weeklyResponseBody = "weekly"
41 42 43 44
	})

	AfterEach(func() {
		rootCmd.SetArgs([]string{})
LinuxSuRen's avatar
LinuxSuRen 已提交
45
		err = os.Remove(targetFilePath)
46 47 48 49
		ctrl.Finish()
	})

	Context("basic cases", func() {
50 51 52 53 54 55 56
		BeforeEach(func() {
			data, err := generateSampleConfig()
			Expect(err).To(BeNil())
			err = ioutil.WriteFile(rootOptions.ConfigFile, data, 0664)
			Expect(err).To(BeNil())
		})

LinuxSuRen's avatar
LinuxSuRen 已提交
57 58 59
		It("should not error", func() {
			Expect(err).NotTo(HaveOccurred())
		})
60

LinuxSuRen's avatar
LinuxSuRen 已提交
61 62 63 64 65 66 67 68 69 70
		It("download the lts Jenkins", func() {
			request, _ := http.NewRequest("GET", "http://mirrors.jenkins.io/war-stable/latest/jenkins.war", nil)
			response := &http.Response{
				StatusCode: 200,
				Request:    request,
				Body:       ioutil.NopCloser(bytes.NewBufferString(ltsResponseBody)),
			}
			roundTripper.EXPECT().
				RoundTrip(request).Return(response, nil)

71
			rootCmd.SetArgs([]string{"center", "download", "--progress=false", "--output", targetFilePath})
LinuxSuRen's avatar
LinuxSuRen 已提交
72 73 74 75 76 77 78 79 80
			_, err := rootCmd.ExecuteC()
			Expect(err).To(BeNil())

			_, err = os.Stat(targetFilePath)
			Expect(err).To(BeNil())

			content, readErr := ioutil.ReadFile(targetFilePath)
			Expect(readErr).To(BeNil())
			Expect(string(content)).To(Equal(ltsResponseBody))
81
		})
LinuxSuRen's avatar
LinuxSuRen 已提交
82 83

		It("download the weekly Jenkins", func() {
84 85 86 87
			request, _ := http.NewRequest("GET", "http://mirrors.jenkins.io/war/latest/jenkins.war", nil)
			response := &http.Response{
				StatusCode: 200,
				Request:    request,
LinuxSuRen's avatar
LinuxSuRen 已提交
88
				Body:       ioutil.NopCloser(bytes.NewBufferString(weeklyResponseBody)),
89 90 91 92
			}
			roundTripper.EXPECT().
				RoundTrip(request).Return(response, nil)

93
			rootCmd.SetArgs([]string{"center", "download", "--lts=false", "--progress=false", "--output", targetFilePath})
94 95 96 97 98 99 100 101
			_, err := rootCmd.ExecuteC()
			Expect(err).To(BeNil())

			_, err = os.Stat(targetFilePath)
			Expect(err).To(BeNil())

			content, readErr := ioutil.ReadFile(targetFilePath)
			Expect(readErr).To(BeNil())
LinuxSuRen's avatar
LinuxSuRen 已提交
102
			Expect(string(content)).To(Equal(weeklyResponseBody))
103 104 105 106 107 108 109 110
		})

		It("no mirror found", func() {
			buf := new(bytes.Buffer)
			rootCmd.SetOut(buf)

			rootCmd.SetArgs([]string{"center", "download", "--progress=false", "--mirror", "fake"})
			_, err := rootCmd.ExecuteC()
LinuxSuRen's avatar
LinuxSuRen 已提交
111 112
			Expect(err).To(HaveOccurred())
			Expect(buf.String()).To(ContainSubstring("cannot found Jenkins mirror by: fake"))
113
		})
114 115
	})
})