github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/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/opentelekomcloud/gophertelekomcloud/openstack/cce/v3/clusters"
     9  	fake "github.com/opentelekomcloud/gophertelekomcloud/openstack/cce/v3/common"
    10  	th "github.com/opentelekomcloud/gophertelekomcloud/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.Fprint(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.Fprint(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.Fprint(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.Fprint(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.Fprint(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 TestCreateV3TurboCluster(t *testing.T) {
   169  	th.SetupHTTP()
   170  	defer th.TeardownHTTP()
   171  
   172  	th.Mux.HandleFunc("/api/v3/projects/c59fd21fd2a94963b822d8985b884673/clusters", func(w http.ResponseWriter, r *http.Request) {
   173  		th.TestMethod(t, r, "POST")
   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  
   178  		th.TestJSONRequest(t, r, `
   179  {
   180      "kind": "Cluster",
   181      "apiversion": "v3",
   182      "metadata": {
   183          "name": "test-turbo-cluster"
   184             },
   185      "spec": {
   186  		"category": "Turbo",
   187          "type": "VirtualMachine",
   188          "flavor": "cce.s2.small",
   189          "version": "v1.19.10-r0",
   190           "hostNetwork": {
   191              "vpc": "3305eb40-2707-4940-921c-9f335f84a2ca",
   192              "subnet": "00e41db7-e56b-4946-bf91-27bb9effd664"
   193          },
   194          "containerNetwork": {
   195              "mode": "eni"
   196          },
   197  		"eniNetwork": {
   198  			"eniSubnetId": "417dcc1f-95d7-43e7-8533-ab078d266303",
   199  			"eniSubnetCIDR": "192.168.0.0/24"
   200  		},
   201          "authentication": {
   202              "mode": "rbac",
   203  			"authenticatingProxy": {}
   204          }
   205      }
   206  
   207  }
   208  `)
   209  
   210  		w.Header().Set("Content-Type", "application/json")
   211  		w.WriteHeader(http.StatusCreated)
   212  		_, _ = fmt.Fprint(w, Output)
   213  	})
   214  	options := clusters.CreateOpts{Kind: "Cluster",
   215  		ApiVersion: "v3",
   216  		Metadata:   clusters.CreateMetaData{Name: "test-turbo-cluster"},
   217  		Spec: clusters.Spec{Type: "VirtualMachine",
   218  			Category: "Turbo",
   219  			Flavor:   "cce.s2.small",
   220  			Version:  "v1.19.10-r0",
   221  			HostNetwork: clusters.HostNetworkSpec{
   222  				VpcId:    "3305eb40-2707-4940-921c-9f335f84a2ca",
   223  				SubnetId: "00e41db7-e56b-4946-bf91-27bb9effd664"},
   224  			ContainerNetwork: clusters.ContainerNetworkSpec{Mode: "eni"},
   225  			EniNetwork: &clusters.EniNetworkSpec{
   226  				SubnetId: "417dcc1f-95d7-43e7-8533-ab078d266303",
   227  				Cidr:     "192.168.0.0/24",
   228  			},
   229  			Authentication: clusters.AuthenticationSpec{
   230  				Mode:                "rbac",
   231  				AuthenticatingProxy: make(map[string]string)},
   232  		},
   233  	}
   234  	actual, err := clusters.Create(fake.ServiceClient(), options).Extract()
   235  	th.AssertNoErr(t, err)
   236  	expected := Expected
   237  	th.AssertDeepEquals(t, expected, actual)
   238  
   239  }
   240  
   241  func TestUpdateV3Cluster(t *testing.T) {
   242  	th.SetupHTTP()
   243  	defer th.TeardownHTTP()
   244  
   245  	th.Mux.HandleFunc("/api/v3/projects/c59fd21fd2a94963b822d8985b884673/clusters/daa97872-59d7-11e8-a787-0255ac101f54", func(w http.ResponseWriter, r *http.Request) {
   246  		th.TestMethod(t, r, "PUT")
   247  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   248  		th.TestHeader(t, r, "Content-Type", "application/json")
   249  		th.TestHeader(t, r, "Accept", "application/json")
   250  		th.TestJSONRequest(t, r, `
   251  {
   252      "spec": {
   253          "description": "new description"
   254      }
   255  }
   256  			`)
   257  
   258  		w.Header().Add("Content-Type", "application/json")
   259  		w.WriteHeader(http.StatusOK)
   260  
   261  		_, _ = fmt.Fprint(w, Output)
   262  	})
   263  	options := clusters.UpdateOpts{Spec: clusters.UpdateSpec{Description: "new description"}}
   264  	actual, err := clusters.Update(fake.ServiceClient(), "daa97872-59d7-11e8-a787-0255ac101f54", options).Extract()
   265  	th.AssertNoErr(t, err)
   266  	expected := Expected
   267  	th.AssertDeepEquals(t, expected, actual)
   268  }
   269  
   270  func TestDeleteV3Cluster(t *testing.T) {
   271  	th.SetupHTTP()
   272  	defer th.TeardownHTTP()
   273  
   274  	th.Mux.HandleFunc("/api/v3/projects/c59fd21fd2a94963b822d8985b884673/clusters/daa97872-59d7-11e8-a787-0255ac101f54", func(w http.ResponseWriter, r *http.Request) {
   275  		th.TestMethod(t, r, "DELETE")
   276  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   277  		w.WriteHeader(http.StatusOK)
   278  	})
   279  
   280  	err := clusters.Delete(fake.ServiceClient(), "daa97872-59d7-11e8-a787-0255ac101f54").ExtractErr()
   281  	th.AssertNoErr(t, err)
   282  
   283  }
   284  
   285  func TestDeleteV3TurboCluster(t *testing.T) {
   286  	th.SetupHTTP()
   287  	defer th.TeardownHTTP()
   288  
   289  	th.Mux.HandleFunc("/api/v3/projects/c59fd21fd2a94963b822d8985b884673/clusters/daa97872-59d7-11e8-a787-0255ac101f54", func(w http.ResponseWriter, r *http.Request) {
   290  		th.TestMethod(t, r, "DELETE")
   291  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   292  		w.WriteHeader(http.StatusOK)
   293  	})
   294  
   295  	err := clusters.Delete(fake.ServiceClient(), "daa97872-59d7-11e8-a787-0255ac101f54").ExtractErr()
   296  	th.AssertNoErr(t, err)
   297  
   298  }