github.com/IBM-Cloud/bluemix-go@v0.0.0-20240423071914-9e96525baef4/api/container/containerv2/workers_test.go (about)

     1  package containerv2
     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("Workers", func() {
    19  	var server *ghttp.Server
    20  	AfterEach(func() {
    21  		server.Close()
    22  	})
    23  
    24  	//ListByWorkerpool
    25  	Describe("List", func() {
    26  		Context("When List worker is successful", func() {
    27  			BeforeEach(func() {
    28  				server = ghttp.NewServer()
    29  				server.AppendHandlers(
    30  					ghttp.CombineHandlers(
    31  						ghttp.VerifyRequest(http.MethodGet, "/v2/vpc/getWorkers"),
    32  						ghttp.RespondWith(http.StatusCreated, `[
    33  							{
    34  							  "flavor": "string",
    35  							  "health": {
    36  								"message": "string",
    37  								"state": "string"
    38  							  },
    39  							  "id": "string",
    40  							  "kubeVersion": {
    41  								"actual": "string",
    42  								"desired": "string",
    43  								"eos": "string",
    44  								"masterEOS": "string",
    45  								"target": "string"
    46  							  },
    47  							  "lifecycle": {
    48  								"actualState": "string",
    49  								"desiredState": "string",
    50  								"message": "string",
    51  								"messageDate": "string",
    52  								"messageDetails": "string",
    53  								"messageDetailsDate": "string",
    54  								"pendingOperation": "string",
    55  								"reasonForDelete": "string"
    56  							  },
    57  							  "location": "string",
    58  							  "networkInterfaces": [
    59  								{
    60  								  "cidr": "string",
    61  								  "ipAddress": "string",
    62  								  "primary": true,
    63  								  "subnetID": "string"
    64  								}
    65  							  ],
    66  							  "poolID": "string",
    67  							  "poolName": "string"
    68  							}
    69  						  ]`),
    70  					),
    71  				)
    72  			})
    73  
    74  			It("should list workers in a cluster", func() {
    75  				target := ClusterTargetHeader{}
    76  
    77  				_, err := newWorker(server.URL()).ListByWorkerPool("aaa", "bbb", true, target)
    78  				Expect(err).NotTo(HaveOccurred())
    79  			})
    80  		})
    81  		Context("When list worker is unsuccessful", func() {
    82  			BeforeEach(func() {
    83  				server = ghttp.NewServer()
    84  				server.SetAllowUnhandledRequests(true)
    85  				server.AppendHandlers(
    86  					ghttp.CombineHandlers(
    87  						ghttp.VerifyRequest(http.MethodGet, "/v2/vpc/getWorkers"),
    88  						ghttp.RespondWith(http.StatusInternalServerError, `Failed to list worker`),
    89  					),
    90  				)
    91  			})
    92  
    93  			It("should return error during get worker", func() {
    94  				target := ClusterTargetHeader{}
    95  				_, err := newWorker(server.URL()).ListByWorkerPool("aaa", "bbb", true, target)
    96  				Expect(err).To(HaveOccurred())
    97  			})
    98  		})
    99  	})
   100  
   101  	//Get
   102  	Describe("Get", func() {
   103  		Context("When Get worker is successful", func() {
   104  			BeforeEach(func() {
   105  				server = ghttp.NewServer()
   106  				server.AppendHandlers(
   107  					ghttp.CombineHandlers(
   108  						ghttp.VerifyRequest(http.MethodGet, "/v2/vpc/getWorker"),
   109  						ghttp.RespondWith(http.StatusCreated, `{
   110  							  "dedicatedHostId": "dedicatedhostid1",
   111  							  "dedicatedHostPoolId": "dedicatedhostpoolid1",
   112  							  "flavor": "string",
   113  							  "health": {
   114  								"message": "string",
   115  								"state": "string"
   116  							  },
   117  							  "id": "string",
   118  							  "kubeVersion": {
   119  								"actual": "string",
   120  								"desired": "string",
   121  								"eos": "string",
   122  								"masterEOS": "string",
   123  								"target": "string"
   124  							  },
   125  							  "lifecycle": {
   126  								"actualState": "string",
   127  								"desiredState": "string",
   128  								"message": "string",
   129  								"messageDate": "string",
   130  								"messageDetails": "string",
   131  								"messageDetailsDate": "string",
   132  								"pendingOperation": "string",
   133  								"reasonForDelete": "string"
   134  							  },
   135  							  "location": "string",
   136  							  "networkInterfaces": [
   137  								{
   138  								  "cidr": "string",
   139  								  "ipAddress": "string",
   140  								  "primary": true,
   141  								  "subnetID": "string"
   142  								}
   143  							  ],
   144  							  "poolID": "string",
   145  							  "poolName": "string"
   146  							}`),
   147  					),
   148  				)
   149  			})
   150  
   151  			It("should get workers in a cluster", func() {
   152  				target := ClusterTargetHeader{}
   153  
   154  				w, err := newWorker(server.URL()).Get("test", "kube-bmrtar0d0st4h9b09vm0-myclustervp-default-0000013", target)
   155  				Expect(err).NotTo(HaveOccurred())
   156  				Expect(w.HostID).To(BeEquivalentTo("dedicatedhostid1"))
   157  				Expect(w.HostPoolID).To(BeEquivalentTo("dedicatedhostpoolid1"))
   158  			})
   159  		})
   160  		Context("When get worker is unsuccessful", func() {
   161  			BeforeEach(func() {
   162  				server = ghttp.NewServer()
   163  				server.SetAllowUnhandledRequests(true)
   164  				server.AppendHandlers(
   165  					ghttp.CombineHandlers(
   166  						ghttp.VerifyRequest(http.MethodGet, "/v2/vpc/getWorker"),
   167  						ghttp.RespondWith(http.StatusInternalServerError, `Failed to get worker`),
   168  					),
   169  				)
   170  			})
   171  
   172  			It("should return error during get worker", func() {
   173  				target := ClusterTargetHeader{}
   174  				_, err := newWorker(server.URL()).Get("test", "kube-bmrtar0d0st4h9b09vm0-myclustervp-default-0000013", target)
   175  				Expect(err).To(HaveOccurred())
   176  			})
   177  		})
   178  	})
   179  
   180  })
   181  
   182  func newWorker(url string) Workers {
   183  
   184  	sess, err := session.New()
   185  	if err != nil {
   186  		log.Fatal(err)
   187  	}
   188  	conf := sess.Config.Copy()
   189  	conf.HTTPClient = bluemixHttp.NewHTTPClient(conf)
   190  	conf.Endpoint = &url
   191  
   192  	client := client.Client{
   193  		Config:      conf,
   194  		ServiceName: bluemix.VpcContainerService,
   195  	}
   196  	return newWorkerAPI(&client)
   197  }