github.com/IBM-Cloud/bluemix-go@v0.0.0-20241117121028-a3be206688b3/api/container/containerv2/workers_test.go (about)

     1  package containerv2
     2  
     3  import (
     4  	"log"
     5  	"net/http"
     6  
     7  	"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  	. "github.com/onsi/ginkgo"
    12  	. "github.com/onsi/gomega"
    13  	"github.com/onsi/gomega/ghttp"
    14  )
    15  
    16  var _ = Describe("Workers", func() {
    17  	var server *ghttp.Server
    18  	AfterEach(func() {
    19  		server.Close()
    20  	})
    21  
    22  	//ListByWorkerpool
    23  	Describe("List", func() {
    24  		Context("When List worker is successful", func() {
    25  			BeforeEach(func() {
    26  				server = ghttp.NewServer()
    27  				server.AppendHandlers(
    28  					ghttp.CombineHandlers(
    29  						ghttp.VerifyRequest(http.MethodGet, "/v2/vpc/getWorkers"),
    30  						ghttp.RespondWith(http.StatusCreated, `[
    31  							{
    32  							  "flavor": "string",
    33  							  "health": {
    34  								"message": "string",
    35  								"state": "string"
    36  							  },
    37  							  "id": "string",
    38  							  "kubeVersion": {
    39  								"actual": "string",
    40  								"desired": "string",
    41  								"eos": "string",
    42  								"masterEOS": "string",
    43  								"target": "string"
    44  							  },
    45  							  "lifecycle": {
    46  								"actualState": "string",
    47  								"desiredState": "string",
    48  								"message": "string",
    49  								"messageDate": "string",
    50  								"messageDetails": "string",
    51  								"messageDetailsDate": "string",
    52  								"pendingOperation": "string",
    53  								"reasonForDelete": "string"
    54  							  },
    55  							  "location": "string",
    56  							  "networkInterfaces": [
    57  								{
    58  								  "cidr": "string",
    59  								  "ipAddress": "string",
    60  								  "primary": true,
    61  								  "subnetID": "string"
    62  								}
    63  							  ],
    64  							  "poolID": "string",
    65  							  "poolName": "string"
    66  							}
    67  						  ]`),
    68  					),
    69  				)
    70  			})
    71  
    72  			It("should list workers in a cluster", func() {
    73  				target := ClusterTargetHeader{}
    74  
    75  				_, err := newWorker(server.URL()).ListByWorkerPool("aaa", "bbb", true, target)
    76  				Expect(err).NotTo(HaveOccurred())
    77  			})
    78  		})
    79  		Context("When list worker is unsuccessful", func() {
    80  			BeforeEach(func() {
    81  				server = ghttp.NewServer()
    82  				server.SetAllowUnhandledRequests(true)
    83  				server.AppendHandlers(
    84  					ghttp.CombineHandlers(
    85  						ghttp.VerifyRequest(http.MethodGet, "/v2/vpc/getWorkers"),
    86  						ghttp.RespondWith(http.StatusInternalServerError, `Failed to list worker`),
    87  					),
    88  				)
    89  			})
    90  
    91  			It("should return error during get worker", func() {
    92  				target := ClusterTargetHeader{}
    93  				_, err := newWorker(server.URL()).ListByWorkerPool("aaa", "bbb", true, target)
    94  				Expect(err).To(HaveOccurred())
    95  			})
    96  		})
    97  	})
    98  
    99  	// ListAllWorkers
   100  	Describe("ListAllWorkers", func() {
   101  		Context("When ListAllWorkers is successful", func() {
   102  			BeforeEach(func() {
   103  				server = ghttp.NewServer()
   104  				server.AppendHandlers(
   105  					ghttp.CombineHandlers(
   106  						// https://containers.cloud.ibm.com/global/swagger-global-api/#/v2/getWorkers
   107  						ghttp.VerifyRequest(http.MethodGet, "/v2/getWorkers"),
   108  						ghttp.RespondWith(http.StatusOK, `[
   109    {
   110      "dedicatedHostId": "string",
   111      "dedicatedHostPoolId": "string",
   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        "actualOperatingSystem": "string",
   127        "actualState": "string",
   128        "desiredOperatingSystem": "string",
   129        "desiredState": "string",
   130        "message": "string",
   131        "messageDate": "string",
   132        "messageDetails": "string",
   133        "messageDetailsDate": "string",
   134        "pendingOperation": "string",
   135        "reasonForDelete": "string"
   136      },
   137      "location": "string",
   138      "networkInformation": {
   139        "privateIP": "string",
   140        "privateVLAN": "string",
   141        "publicIP": "string",
   142        "publicVLAN": "string"
   143      },
   144      "poolID": "string",
   145      "poolName": "string"
   146    }
   147  ]`),
   148  					),
   149  				)
   150  			})
   151  
   152  			It("should list workers in a cluster", func() {
   153  				target := ClusterTargetHeader{}
   154  
   155  				_, err := newWorker(server.URL()).ListAllWorkers("aaa", false, target)
   156  				Expect(err).NotTo(HaveOccurred())
   157  			})
   158  		})
   159  		Context("When ListAllWorkers is unsuccessful", func() {
   160  			BeforeEach(func() {
   161  				server = ghttp.NewServer()
   162  				server.SetAllowUnhandledRequests(true)
   163  				server.AppendHandlers(
   164  					ghttp.CombineHandlers(
   165  						ghttp.VerifyRequest(http.MethodGet, "/v2/getWorkers"),
   166  						ghttp.RespondWith(http.StatusInternalServerError, `Failed to list worker`),
   167  					),
   168  				)
   169  			})
   170  
   171  			It("should return error during get worker", func() {
   172  				target := ClusterTargetHeader{}
   173  				_, err := newWorker(server.URL()).ListAllWorkers("aaa", false, target)
   174  				Expect(err).To(HaveOccurred())
   175  			})
   176  		})
   177  	})
   178  
   179  	//Get
   180  	Describe("Get", func() {
   181  		Context("When Get worker is successful", func() {
   182  			BeforeEach(func() {
   183  				server = ghttp.NewServer()
   184  				server.AppendHandlers(
   185  					ghttp.CombineHandlers(
   186  						ghttp.VerifyRequest(http.MethodGet, "/v2/vpc/getWorker"),
   187  						ghttp.RespondWith(http.StatusCreated, `{
   188  							  "dedicatedHostId": "dedicatedhostid1",
   189  							  "dedicatedHostPoolId": "dedicatedhostpoolid1",
   190  							  "flavor": "string",
   191  							  "health": {
   192  								"message": "string",
   193  								"state": "string"
   194  							  },
   195  							  "id": "string",
   196  							  "kubeVersion": {
   197  								"actual": "string",
   198  								"desired": "string",
   199  								"eos": "string",
   200  								"masterEOS": "string",
   201  								"target": "string"
   202  							  },
   203  							  "lifecycle": {
   204  								"actualState": "string",
   205  								"desiredState": "string",
   206  								"message": "string",
   207  								"messageDate": "string",
   208  								"messageDetails": "string",
   209  								"messageDetailsDate": "string",
   210  								"pendingOperation": "string",
   211  								"reasonForDelete": "string"
   212  							  },
   213  							  "location": "string",
   214  							  "networkInterfaces": [
   215  								{
   216  								  "cidr": "string",
   217  								  "ipAddress": "string",
   218  								  "primary": true,
   219  								  "subnetID": "string"
   220  								}
   221  							  ],
   222  							  "poolID": "string",
   223  							  "poolName": "string"
   224  							}`),
   225  					),
   226  				)
   227  			})
   228  
   229  			It("should get workers in a cluster", func() {
   230  				target := ClusterTargetHeader{}
   231  
   232  				w, err := newWorker(server.URL()).Get("test", "kube-bmrtar0d0st4h9b09vm0-myclustervp-default-0000013", target)
   233  				Expect(err).NotTo(HaveOccurred())
   234  				Expect(w.HostID).To(BeEquivalentTo("dedicatedhostid1"))
   235  				Expect(w.HostPoolID).To(BeEquivalentTo("dedicatedhostpoolid1"))
   236  			})
   237  		})
   238  		Context("When get worker is unsuccessful", func() {
   239  			BeforeEach(func() {
   240  				server = ghttp.NewServer()
   241  				server.SetAllowUnhandledRequests(true)
   242  				server.AppendHandlers(
   243  					ghttp.CombineHandlers(
   244  						ghttp.VerifyRequest(http.MethodGet, "/v2/vpc/getWorker"),
   245  						ghttp.RespondWith(http.StatusInternalServerError, `Failed to get worker`),
   246  					),
   247  				)
   248  			})
   249  
   250  			It("should return error during get worker", func() {
   251  				target := ClusterTargetHeader{}
   252  				_, err := newWorker(server.URL()).Get("test", "kube-bmrtar0d0st4h9b09vm0-myclustervp-default-0000013", target)
   253  				Expect(err).To(HaveOccurred())
   254  			})
   255  		})
   256  	})
   257  
   258  })
   259  
   260  func newWorker(url string) Workers {
   261  
   262  	sess, err := session.New()
   263  	if err != nil {
   264  		log.Fatal(err)
   265  	}
   266  	conf := sess.Config.Copy()
   267  	conf.HTTPClient = bluemixHttp.NewHTTPClient(conf)
   268  	conf.Endpoint = &url
   269  
   270  	client := client.Client{
   271  		Config:      conf,
   272  		ServiceName: bluemix.VpcContainerService,
   273  	}
   274  	return newWorkerAPI(&client)
   275  }