PageRenderTime 123ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/jenkins_test.go

https://gitlab.com/Kravcenko/gojenkins
Go | 178 lines | 145 code | 33 blank | 0 comment | 7 complexity | d1c468d52d9dfcb6ef959c7fc8adfd30 MD5 | raw file
  1. package gojenkins
  2. import (
  3. "fmt"
  4. "github.com/stretchr/testify/assert"
  5. "io/ioutil"
  6. "math/rand"
  7. "testing"
  8. "time"
  9. )
  10. var (
  11. jenkins *Jenkins
  12. )
  13. func TestInit(t *testing.T) {
  14. jenkins = CreateJenkins("http://localhost:8080", "admin", "admin")
  15. _, err := jenkins.Init()
  16. assert.Nil(t, err, "Jenkins Initialization should not fail")
  17. }
  18. func TestCreateJobs(t *testing.T) {
  19. job1ID := "Job1_test"
  20. job2ID := "Job2_test"
  21. job_data := getFileAsString("job.xml")
  22. job1, err := jenkins.CreateJob(job_data, job1ID)
  23. if err != nil {
  24. fmt.Print(err)
  25. }
  26. assert.Equal(t, "Some Job Description", job1.GetDescription())
  27. assert.Equal(t, job1ID, job1.GetName())
  28. job2, _ := jenkins.CreateJob(job_data, job2ID)
  29. assert.Equal(t, "Some Job Description", job2.GetDescription())
  30. assert.Equal(t, job2ID, job2.GetName())
  31. }
  32. func TestCreateNodes(t *testing.T) {
  33. id1 := "node1_test"
  34. id2 := "node2_test"
  35. id3 := "node3_test"
  36. jnlp := map[string]string{"method": "JNLPLauncher"}
  37. ssh := map[string]string{"method": "SSHLauncher"}
  38. node1, _ := jenkins.CreateNode(id1, 1, "Node 1 Description", "/var/lib/jenkins", jnlp)
  39. assert.Equal(t, id1, node1.GetName())
  40. node2, _ := jenkins.CreateNode(id2, 1, "Node 2 Description", "/var/lib/jenkins", ssh)
  41. assert.Equal(t, id2, node2.GetName())
  42. node3, _ := jenkins.CreateNode(id3, 1, "Node 3 Description", "/var/lib/jenkins")
  43. assert.Equal(t, id3, node3.GetName())
  44. }
  45. func TestCreateBuilds(t *testing.T) {
  46. jobs, _ := jenkins.GetAllJobs()
  47. for _, item := range jobs {
  48. item.InvokeSimple(map[string]string{"param1": "param1"})
  49. item.Poll()
  50. isQueued, _ := item.IsQueued()
  51. assert.Equal(t, true, isQueued)
  52. time.Sleep(10 * time.Second)
  53. builds, _ := item.GetAllBuildIds()
  54. assert.True(t, (len(builds) > 0))
  55. }
  56. }
  57. func TestCreateViews(t *testing.T) {
  58. list_view, err := jenkins.CreateView("test_list_view", LIST_VIEW)
  59. assert.Nil(t, err)
  60. assert.Equal(t, "test_list_view", list_view.GetName())
  61. assert.Equal(t, "", list_view.GetDescription())
  62. assert.Equal(t, 0, len(list_view.GetJobs()))
  63. my_view, err := jenkins.CreateView("test_my_view", MY_VIEW)
  64. assert.Nil(t, err)
  65. assert.Equal(t, "test_my_view", my_view.GetName())
  66. assert.Equal(t, "", my_view.GetDescription())
  67. assert.Equal(t, 2, len(my_view.GetJobs()))
  68. }
  69. func TestGetAllJobs(t *testing.T) {
  70. jobs, _ := jenkins.GetAllJobs()
  71. assert.Equal(t, 2, len(jobs))
  72. assert.Equal(t, jobs[0].Raw.Color, "blue")
  73. }
  74. func TestGetAllNodes(t *testing.T) {
  75. nodes, _ := jenkins.GetAllNodes()
  76. assert.Equal(t, 4, len(nodes))
  77. assert.Equal(t, nodes[0].GetName(), "master")
  78. }
  79. func TestGetAllBuilds(t *testing.T) {
  80. builds, _ := jenkins.GetAllBuildIds("Job1_test")
  81. for _, b := range builds {
  82. build, _ := jenkins.GetBuild("Job1_test", b.Number)
  83. assert.Equal(t, "SUCCESS", build.GetResult())
  84. }
  85. assert.Equal(t, 1, len(builds))
  86. }
  87. func TestBuildMethods(t *testing.T) {
  88. job, _ := jenkins.GetJob("Job1_test")
  89. build, _ := job.GetLastBuild()
  90. params := build.GetParameters()
  91. assert.Equal(t, "params1", params[0].Name)
  92. }
  93. func TestGetSingleJob(t *testing.T) {
  94. job, _ := jenkins.GetJob("Job1_test")
  95. isRunning, _ := job.IsRunning()
  96. config, _ := job.GetConfig()
  97. assert.Equal(t, false, isRunning)
  98. assert.Contains(t, config, "<project>")
  99. }
  100. func TestEnableDisableJob(t *testing.T) {
  101. job, _ := jenkins.GetJob("Job1_test")
  102. result, _ := job.Disable()
  103. assert.Equal(t, true, result)
  104. result, _ = job.Enable()
  105. assert.Equal(t, true, result)
  106. }
  107. func TestCopyDeleteJob(t *testing.T) {
  108. job, _ := jenkins.GetJob("Job1_test")
  109. jobCopy, _ := job.Copy("Job1_test_copy")
  110. assert.Equal(t, jobCopy.GetName(), "Job1_test_copy")
  111. jobDelete, _ := job.Delete()
  112. assert.Equal(t, true, jobDelete)
  113. }
  114. func TestGetPlugins(t *testing.T) {
  115. plugins, _ := jenkins.GetPlugins(3)
  116. assert.Equal(t, 19, plugins.Count())
  117. }
  118. func TestGetViews(t *testing.T) {
  119. views, _ := jenkins.GetAllViews()
  120. assert.Equal(t, len(views), 3)
  121. assert.Equal(t, len(views[0].Raw.Jobs), 2)
  122. }
  123. func TestGetSingleView(t *testing.T) {
  124. view, _ := jenkins.GetView("All")
  125. view2, _ := jenkins.GetView("test_list_view")
  126. assert.Equal(t, len(view.Raw.Jobs), 2)
  127. assert.Equal(t, len(view2.Raw.Jobs), 0)
  128. assert.Equal(t, view2.Raw.Name, "test_list_view")
  129. }
  130. func getFileAsString(path string) string {
  131. buf, err := ioutil.ReadFile("_tests/" + path)
  132. if err != nil {
  133. panic(err)
  134. }
  135. return string(buf)
  136. }
  137. func getRandomString(n int) string {
  138. rand.Seed(time.Now().UnixNano())
  139. letters := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
  140. b := make([]rune, n)
  141. for i := range b {
  142. b[i] = letters[rand.Intn(len(letters))]
  143. }
  144. return string(b)
  145. }