github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/cce/v3/clusters/testing/requests_test.go (about)

     1  package testing
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"testing"
     7  
     8  	"github.com/huaweicloud/golangsdk/openstack/cce/v3/clusters"
     9  	fake "github.com/huaweicloud/golangsdk/openstack/cce/v3/common"
    10  	th "github.com/huaweicloud/golangsdk/testhelper"
    11  )
    12  
    13  func TestGetV3Cluster(t *testing.T) {
    14  	th.SetupHTTP()
    15  	defer th.TeardownHTTP()
    16  
    17  	th.Mux.HandleFunc("/api/v3/projects/c59fd21fd2a94963b822d8985b884673/clusters/daa97872-59d7-11e8-a787-0255ac101f54", func(w http.ResponseWriter, r *http.Request) {
    18  		th.TestMethod(t, r, "GET")
    19  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
    20  		w.Header().Add("Content-Type", "application/json")
    21  		w.WriteHeader(http.StatusOK)
    22  		fmt.Fprintf(w, Output)
    23  	})
    24  
    25  	actual, err := clusters.Get(fake.ServiceClient(), "daa97872-59d7-11e8-a787-0255ac101f54").Extract()
    26  	th.AssertNoErr(t, err)
    27  	expected := Expected
    28  	th.AssertDeepEquals(t, expected, actual)
    29  
    30  }
    31  
    32  func TestGetV3ClusterOTC(t *testing.T) {
    33  	th.SetupHTTP()
    34  	defer th.TeardownHTTP()
    35  
    36  	th.Mux.HandleFunc("/api/v3/projects/c59fd21fd2a94963b822d8985b884673/clusters/daa97872-59d7-11e8-a787-0255ac101f54", func(w http.ResponseWriter, r *http.Request) {
    37  		th.TestMethod(t, r, "GET")
    38  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
    39  		w.Header().Add("Content-Type", "application/json")
    40  		w.WriteHeader(http.StatusOK)
    41  		fmt.Fprintf(w, OutputOTC)
    42  	})
    43  
    44  	actual, err := clusters.Get(fake.ServiceClient(), "daa97872-59d7-11e8-a787-0255ac101f54").Extract()
    45  	th.AssertNoErr(t, err)
    46  	expected := ExpectedOTC
    47  	th.AssertDeepEquals(t, expected, actual)
    48  
    49  }
    50  
    51  func TestListV3Cluster(t *testing.T) {
    52  
    53  	th.SetupHTTP()
    54  	defer th.TeardownHTTP()
    55  
    56  	th.Mux.HandleFunc("/api/v3/projects/c59fd21fd2a94963b822d8985b884673/clusters", func(w http.ResponseWriter, r *http.Request) {
    57  		th.TestMethod(t, r, "GET")
    58  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
    59  
    60  		w.Header().Add("Content-Type", "application/json")
    61  		w.WriteHeader(http.StatusOK)
    62  
    63  		fmt.Fprintf(w, ListOutput)
    64  	})
    65  
    66  	//count := 0
    67  
    68  	actual, err := clusters.List(fake.ServiceClient(), clusters.ListOpts{})
    69  	if err != nil {
    70  		t.Errorf("Failed to extract clusters: %v", err)
    71  	}
    72  
    73  	expected := ListExpected
    74  
    75  	th.AssertDeepEquals(t, expected, actual)
    76  }
    77  
    78  func TestListV3ClusterOTC(t *testing.T) {
    79  
    80  	th.SetupHTTP()
    81  	defer th.TeardownHTTP()
    82  
    83  	th.Mux.HandleFunc("/api/v3/projects/c59fd21fd2a94963b822d8985b884673/clusters", func(w http.ResponseWriter, r *http.Request) {
    84  		th.TestMethod(t, r, "GET")
    85  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
    86  
    87  		w.Header().Add("Content-Type", "application/json")
    88  		w.WriteHeader(http.StatusOK)
    89  
    90  		fmt.Fprintf(w, ListOutputOTC)
    91  	})
    92  
    93  	//count := 0
    94  
    95  	actual, err := clusters.List(fake.ServiceClient(), clusters.ListOpts{})
    96  	if err != nil {
    97  		t.Errorf("Failed to extract clusters: %v", err)
    98  	}
    99  
   100  	expected := ListExpectedOTC
   101  
   102  	th.AssertDeepEquals(t, expected, actual)
   103  }
   104  
   105  func TestCreateV3Cluster(t *testing.T) {
   106  	th.SetupHTTP()
   107  	defer th.TeardownHTTP()
   108  
   109  	th.Mux.HandleFunc("/api/v3/projects/c59fd21fd2a94963b822d8985b884673/clusters", func(w http.ResponseWriter, r *http.Request) {
   110  		th.TestMethod(t, r, "POST")
   111  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   112  		th.TestHeader(t, r, "Content-Type", "application/json")
   113  		th.TestHeader(t, r, "Accept", "application/json")
   114  
   115  		th.TestJSONRequest(t, r, `
   116  {
   117      "kind": "Cluster",
   118      "apiversion": "v3",
   119      "metadata": {
   120          "name": "test-cluster"
   121             },
   122      "spec": {
   123          "type": "VirtualMachine",
   124          "flavor": "cce.s1.small",
   125          "version": "v1.7.3-r10",
   126           "hostNetwork": {
   127              "vpc": "3305eb40-2707-4940-921c-9f335f84a2ca",
   128              "subnet": "00e41db7-e56b-4946-bf91-27bb9effd664"
   129          },
   130          "containerNetwork": {
   131              "mode": "overlay_l2"
   132  		},
   133          "authentication": {
   134              "mode": "rbac",
   135  			"authenticatingProxy": {}
   136          }
   137      }
   138  
   139  }
   140  `)
   141  
   142  		w.Header().Set("Content-Type", "application/json")
   143  		w.WriteHeader(http.StatusCreated)
   144  		fmt.Fprintf(w, Output)
   145  	})
   146  	options := clusters.CreateOpts{Kind: "Cluster",
   147  		ApiVersion: "v3",
   148  		Metadata:   clusters.CreateMetaData{Name: "test-cluster"},
   149  		Spec: clusters.Spec{Type: "VirtualMachine",
   150  			Flavor:  "cce.s1.small",
   151  			Version: "v1.7.3-r10",
   152  			HostNetwork: clusters.HostNetworkSpec{
   153  				VpcId:    "3305eb40-2707-4940-921c-9f335f84a2ca",
   154  				SubnetId: "00e41db7-e56b-4946-bf91-27bb9effd664"},
   155  			ContainerNetwork: clusters.ContainerNetworkSpec{Mode: "overlay_l2"},
   156  			Authentication: clusters.AuthenticationSpec{
   157  				Mode:                "rbac",
   158  				AuthenticatingProxy: make(map[string]string)},
   159  		},
   160  	}
   161  	actual, err := clusters.Create(fake.ServiceClient(), options).Extract()
   162  	th.AssertNoErr(t, err)
   163  	expected := Expected
   164  	th.AssertDeepEquals(t, expected, actual)
   165  
   166  }
   167  
   168  func TestUpdateV3Cluster(t *testing.T) {
   169  	th.SetupHTTP()
   170  	defer th.TeardownHTTP()
   171  
   172  	th.Mux.HandleFunc("/api/v3/projects/c59fd21fd2a94963b822d8985b884673/clusters/daa97872-59d7-11e8-a787-0255ac101f54", func(w http.ResponseWriter, r *http.Request) {
   173  		th.TestMethod(t, r, "PUT")
   174  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   175  		th.TestHeader(t, r, "Content-Type", "application/json")
   176  		th.TestHeader(t, r, "Accept", "application/json")
   177  		th.TestJSONRequest(t, r, `
   178  {
   179      "spec": {
   180          "description": "new description"
   181      }
   182  }
   183  			`)
   184  
   185  		w.Header().Add("Content-Type", "application/json")
   186  		w.WriteHeader(http.StatusOK)
   187  
   188  		fmt.Fprintf(w, Output)
   189  	})
   190  	options := clusters.UpdateOpts{Spec: clusters.UpdateSpec{Description: "new description"}}
   191  	actual, err := clusters.Update(fake.ServiceClient(), "daa97872-59d7-11e8-a787-0255ac101f54", options).Extract()
   192  	th.AssertNoErr(t, err)
   193  	expected := Expected
   194  	th.AssertDeepEquals(t, expected, actual)
   195  }
   196  
   197  func TestDeleteV3Cluster(t *testing.T) {
   198  	th.SetupHTTP()
   199  	defer th.TeardownHTTP()
   200  
   201  	th.Mux.HandleFunc("/api/v3/projects/c59fd21fd2a94963b822d8985b884673/clusters/daa97872-59d7-11e8-a787-0255ac101f54", func(w http.ResponseWriter, r *http.Request) {
   202  		th.TestMethod(t, r, "DELETE")
   203  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   204  		w.WriteHeader(http.StatusOK)
   205  	})
   206  
   207  	err := clusters.Delete(fake.ServiceClient(), "daa97872-59d7-11e8-a787-0255ac101f54").ExtractErr()
   208  	th.AssertNoErr(t, err)
   209  
   210  }
   211  
   212  func TestDeleteWithOptsV3Cluster(t *testing.T) {
   213  	th.SetupHTTP()
   214  	defer th.TeardownHTTP()
   215  
   216  	th.Mux.HandleFunc("/api/v3/projects/c59fd21fd2a94963b822d8985b884673/clusters/daa97872-59d7-11e8-a787-0255ac101f54", func(w http.ResponseWriter, r *http.Request) {
   217  		th.TestMethod(t, r, "DELETE")
   218  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   219  		w.WriteHeader(http.StatusOK)
   220  	})
   221  	options := clusters.DeleteOpts{
   222  		DeleteEfs: "true",
   223  		DeleteENI: "true",
   224  		DeleteEvs: "true",
   225  		DeleteNet: "true",
   226  		DeleteObs: "true",
   227  		DeleteSfs: "true",
   228  	}
   229  	err := clusters.DeleteWithOpts(fake.ServiceClient(), "daa97872-59d7-11e8-a787-0255ac101f54", options).ExtractErr()
   230  	th.AssertNoErr(t, err)
   231  }