github.com/IBM-Cloud/bluemix-go@v0.0.0-20240423071914-9e96525baef4/api/icd/icdv4/task_test.go (about)

     1  package icdv4
     2  
     3  import (
     4  	"log"
     5  	"net/http"
     6  
     7  	bluemix "github.com/IBM-Cloud/bluemix-go"
     8  	"github.com/IBM-Cloud/bluemix-go/client"
     9  	bluemixHttp "github.com/IBM-Cloud/bluemix-go/http"
    10  	"github.com/IBM-Cloud/bluemix-go/session"
    11  
    12  	"github.com/onsi/gomega/ghttp"
    13  
    14  	. "github.com/onsi/ginkgo"
    15  	. "github.com/onsi/gomega"
    16  )
    17  
    18  var _ = Describe("Tasks", func() {
    19  	var server *ghttp.Server
    20  	AfterEach(func() {
    21  		server.Close()
    22  	})
    23  	Describe("Get", func() {
    24  		Context("When get is successful", func() {
    25  			BeforeEach(func() {
    26  				server = ghttp.NewServer()
    27  				server.AppendHandlers(
    28  					ghttp.CombineHandlers(
    29  						ghttp.VerifyRequest(http.MethodGet, "/v4/ibm/tasks/5abb6a7d11a1a5001479a0ac"),
    30  						ghttp.RespondWith(http.StatusOK, `
    31                             {
    32                              "task": {
    33                                "id": "5abb6a7d11a1a5001479a0ac",
    34                                "description": "Creating task for database",
    35                                "status": "running",
    36                                "deployment_id": "59b14b19874a1c0018009482",
    37                                "progress_percent": 5,
    38                                "created_at": "2018-03-28T10:21:30Z"
    39                              }
    40                            }
    41                          `),
    42  					),
    43  				)
    44  			})
    45  
    46  			It("should return task", func() {
    47  				target := "5abb6a7d11a1a5001479a0ac"
    48  				task, err := newTask(server.URL()).GetTask(target)
    49  				Expect(err).NotTo(HaveOccurred())
    50  				Expect(task).ShouldNot(BeNil())
    51  				Expect(task.Id).Should(Equal("5abb6a7d11a1a5001479a0ac"))
    52  				Expect(task.Description).Should(Equal("Creating task for database"))
    53  				Expect(task.Status).Should(Equal("running"))
    54  				Expect(task.DeploymentId).Should(Equal("59b14b19874a1c0018009482"))
    55  				Expect(task.ProgressPercent).Should(Equal(5))
    56  				Expect(task.CreatedAt).Should(Equal("2018-03-28T10:21:30Z"))
    57  			})
    58  		})
    59  		Context("When get is unsuccessful", func() {
    60  			BeforeEach(func() {
    61  				server = ghttp.NewServer()
    62  				server.SetAllowUnhandledRequests(true)
    63  				server.AppendHandlers(
    64  					ghttp.CombineHandlers(
    65  						ghttp.VerifyRequest(http.MethodGet, "/v4/ibm/tasks/5abb6a7d11a1a5001479a0ac"),
    66  						ghttp.RespondWith(http.StatusInternalServerError, `Failed to get task`),
    67  					),
    68  				)
    69  			})
    70  
    71  			It("should return error during get task", func() {
    72  				target := "5abb6a7d11a1a5001479a0ac"
    73  				task, err := newTask(server.URL()).GetTask(target)
    74  				Expect(err).To(HaveOccurred())
    75  				Expect(task.Id).Should(Equal(""))
    76  			})
    77  		})
    78  	})
    79  })
    80  
    81  func newTask(url string) Tasks {
    82  
    83  	sess, err := session.New()
    84  	if err != nil {
    85  		log.Fatal(err)
    86  	}
    87  	conf := sess.Config.Copy()
    88  	conf.HTTPClient = bluemixHttp.NewHTTPClient(conf)
    89  	conf.Endpoint = &url
    90  
    91  	client := client.Client{
    92  		Config:      conf,
    93  		ServiceName: bluemix.ICDService,
    94  	}
    95  	return newTaskAPI(&client)
    96  }