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

     1  package containerv1
     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("Kms", func() {
    19  	var server *ghttp.Server
    20  	AfterEach(func() {
    21  		server.Close()
    22  	})
    23  
    24  	//Enable
    25  	Describe("Enable", func() {
    26  		Context("When enabling kms is successful", func() {
    27  			BeforeEach(func() {
    28  				server = ghttp.NewServer()
    29  				server.AppendHandlers(
    30  					ghttp.CombineHandlers(
    31  						ghttp.VerifyRequest(http.MethodPost, "/v2/enableKMS"),
    32  						ghttp.VerifyJSON(`{"cluster":"bs65tjud0j4njc8pu30g","instance_id":"12043812-757f-4e1e-8436-6af3245e6a69","crk_id":"0792853c-b9f9-4b35-9d9e-ffceab51d3c1","private_endpoint":false}`),
    33  						ghttp.RespondWith(http.StatusCreated, `{}`),
    34  					),
    35  				)
    36  			})
    37  
    38  			It("should enable Kms in a cluster", func() {
    39  				target := ClusterHeader{}
    40  				params := KmsEnableReq{
    41  					Cluster: "bs65tjud0j4njc8pu30g", Kms: "12043812-757f-4e1e-8436-6af3245e6a69", Crk: "0792853c-b9f9-4b35-9d9e-ffceab51d3c1", PrivateEndpoint: false,
    42  				}
    43  				err := newKms(server.URL()).EnableKms(params, target)
    44  				Expect(err).NotTo(HaveOccurred())
    45  			})
    46  		})
    47  		Context("When enabling kms with account ID is successful", func() {
    48  			BeforeEach(func() {
    49  				server = ghttp.NewServer()
    50  				server.AppendHandlers(
    51  					ghttp.CombineHandlers(
    52  						ghttp.VerifyRequest(http.MethodPost, "/v2/enableKMS"),
    53  						ghttp.VerifyJSON(`{"cluster":"bs65tjud0j4njc8pu30g","instance_id":"12043812-757f-4e1e-8436-6af3245e6a69","crk_id":"0792853c-b9f9-4b35-9d9e-ffceab51d3c1","private_endpoint":false,"account_id":"accountID1"}`),
    54  						ghttp.RespondWith(http.StatusCreated, `{}`),
    55  					),
    56  				)
    57  			})
    58  
    59  			It("should enable Kms in a cluster", func() {
    60  				target := ClusterHeader{}
    61  				params := KmsEnableReq{
    62  					Cluster:         "bs65tjud0j4njc8pu30g",
    63  					Kms:             "12043812-757f-4e1e-8436-6af3245e6a69",
    64  					Crk:             "0792853c-b9f9-4b35-9d9e-ffceab51d3c1",
    65  					PrivateEndpoint: false,
    66  					AccountID:       "accountID1",
    67  				}
    68  				err := newKms(server.URL()).EnableKms(params, target)
    69  				Expect(err).NotTo(HaveOccurred())
    70  			})
    71  		})
    72  		Context("When enabling is unsuccessful", func() {
    73  			BeforeEach(func() {
    74  				server = ghttp.NewServer()
    75  				server.SetAllowUnhandledRequests(true)
    76  				server.AppendHandlers(
    77  					ghttp.CombineHandlers(
    78  						ghttp.VerifyRequest(http.MethodPost, "/v2/enableKMS"),
    79  						ghttp.VerifyJSON(`{"cluster":"bs65tjud0j4njc8pu30g","instance_id":"12043812-757f-4e1e-8436-6af3245e6a69","crk_id":"0792853c-b9f9-4b35-9d9e-ffceab51d3c1","private_endpoint":false}`),
    80  						ghttp.RespondWith(http.StatusInternalServerError, `Failed to enable kms`),
    81  					),
    82  				)
    83  			})
    84  
    85  			It("should return error during enabling kms", func() {
    86  				params := KmsEnableReq{
    87  					Cluster: "bs65tjud0j4njc8pu30g", Kms: "12043812-757f-4e1e-8436-6af3245e6a69", Crk: "0792853c-b9f9-4b35-9d9e-ffceab51d3c1", PrivateEndpoint: false,
    88  				}
    89  				target := ClusterHeader{}
    90  				err := newKms(server.URL()).EnableKms(params, target)
    91  				Expect(err).To(HaveOccurred())
    92  			})
    93  		})
    94  	})
    95  })
    96  
    97  func newKms(url string) Kms {
    98  
    99  	sess, err := session.New()
   100  	if err != nil {
   101  		log.Fatal(err)
   102  	}
   103  	conf := sess.Config.Copy()
   104  	conf.HTTPClient = bluemixHttp.NewHTTPClient(conf)
   105  	conf.Endpoint = &url
   106  
   107  	client := client.Client{
   108  		Config:      conf,
   109  		ServiceName: bluemix.VpcContainerService,
   110  	}
   111  	return newKmsAPI(&client)
   112  }