github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/geminidb/v3/instances/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/geminidb/v3/instances"
     9  	"github.com/huaweicloud/golangsdk/pagination"
    10  	th "github.com/huaweicloud/golangsdk/testhelper"
    11  	"github.com/huaweicloud/golangsdk/testhelper/client"
    12  )
    13  
    14  func TestCreate(t *testing.T) {
    15  	th.SetupHTTP()
    16  	defer th.TeardownHTTP()
    17  	th.Mux.HandleFunc("/instances", func(w http.ResponseWriter, r *http.Request) {
    18  		th.TestMethod(t, r, "POST")
    19  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
    20  		th.TestJSONRequest(t, r, CreateRequest)
    21  		w.Header().Add("Content-Type", "application/json")
    22  		w.WriteHeader(http.StatusAccepted)
    23  		fmt.Fprintf(w, CreateResponse)
    24  	})
    25  
    26  	options := instances.CreateGeminiDBOpts{
    27  		Name: "test-cassandra-01",
    28  		DataStore: instances.DataStore{
    29  			Type:          "GeminiDB-Cassandra",
    30  			Version:       "3.11",
    31  			StorageEngine: "rocksDB",
    32  		},
    33  		Region:           "aaa",
    34  		AvailabilityZone: "bbb",
    35  		VpcId:            "674e9b42-cd8d-4d25-a2e6-5abcc565b961",
    36  		SubnetId:         "f1df08c5-71d1-406a-aff0-de435a51007b",
    37  		SecurityGroupId:  "7aa51dbf-5b63-40db-9724-dad3c4828b58",
    38  		Password:         "Test@123",
    39  		Mode:             "Cluster",
    40  		Flavor: []instances.FlavorOpt{
    41  			{
    42  				Num:      "3",
    43  				Size:     500,
    44  				Storage:  "ULTRAHIGH",
    45  				SpecCode: "nosql.cassandra.4xlarge.4",
    46  			},
    47  		},
    48  		BackupStrategy: &instances.BackupStrategyOpt{
    49  			StartTime: "08:15-09:15",
    50  			KeepDays:  "8",
    51  		},
    52  		EnterpriseProjectId: "0",
    53  	}
    54  
    55  	actual, err := instances.Create(client.ServiceClient(), options).Extract()
    56  	th.AssertNoErr(t, err)
    57  	expected := instances.CreateResponse{
    58  		AvailabilityZone: "bbb",
    59  		Flavor: []instances.Flavor{
    60  			{
    61  				Num:      "3",
    62  				Size:     "500",
    63  				Storage:  "ULTRAHIGH",
    64  				SpecCode: "nosql.cassandra.4xlarge.4",
    65  			},
    66  		},
    67  		JobId: "c010abd0-48cf-4fa8-8cbc-090f093eaa2f",
    68  		BackupStrategy: instances.BackupStrategyOpt{
    69  			StartTime: "08:15-09:15",
    70  			KeepDays:  "8",
    71  		},
    72  	}
    73  
    74  	expected.GeminiDBBase = instances.GeminiDBBase{
    75  		Id:   "39b6a1a278844ac48119d86512e0000bin06",
    76  		Name: "test-cassandra-01",
    77  		DataStore: instances.DataStore{
    78  			Type:          "GeminiDB-Cassandra",
    79  			Version:       "3.11",
    80  			StorageEngine: "rocksDB",
    81  		},
    82  		Status:              "creating",
    83  		Region:              "aaa",
    84  		VpcId:               "674e9b42-cd8d-4d25-a2e6-5abcc565b961",
    85  		SubnetId:            "f1df08c5-71d1-406a-aff0-de435a51007b",
    86  		SecurityGroupId:     "7aa51dbf-5b63-40db-9724-dad3c4828b58",
    87  		Mode:                "Cluster",
    88  		EnterpriseProjectId: "0",
    89  	}
    90  	th.AssertDeepEquals(t, expected, *actual)
    91  }
    92  
    93  func TestDelete(t *testing.T) {
    94  	th.SetupHTTP()
    95  	defer th.TeardownHTTP()
    96  
    97  	th.Mux.HandleFunc("/instances/4e8e5957", func(w http.ResponseWriter, r *http.Request) {
    98  		th.TestMethod(t, r, "DELETE")
    99  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   100  		w.WriteHeader(http.StatusAccepted)
   101  	})
   102  	res := instances.Delete(client.ServiceClient(), "4e8e5957")
   103  	th.AssertNoErr(t, res.Err)
   104  }
   105  
   106  func TestGetAllInstances(t *testing.T) {
   107  	th.SetupHTTP()
   108  	defer th.TeardownHTTP()
   109  	th.Mux.HandleFunc("/instances", func(w http.ResponseWriter, r *http.Request) {
   110  		th.TestMethod(t, r, "GET")
   111  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   112  
   113  		w.Header().Add("Content-Type", "application/json")
   114  		w.WriteHeader(http.StatusOK)
   115  
   116  		fmt.Fprintf(w, AllInstancesResponse)
   117  	})
   118  
   119  	ExpectedAllInstancesResponse := instances.ListGeminiDBResponse{
   120  		TotalCount: 2,
   121  		Instances: []instances.GeminiDBInstance{
   122  			{
   123  				Port:              "8635",
   124  				Engine:            " rocksDB ",
   125  				DbUserName:        "rwuser",
   126  				PayMode:           "0",
   127  				TimeZone:          "",
   128  				MaintenanceWindow: "02:00-06:00",
   129  				Actions:           []string{"CREATE"},
   130  				Groups: []instances.Groups{
   131  					{
   132  						Id:     "0b0ff12541794e1084f6827e424be2d6gr06",
   133  						Status: "creating",
   134  						Volume: instances.Volume{
   135  							Size: "100",
   136  							Used: "0.003",
   137  						},
   138  						Nodes: []instances.Nodes{
   139  							{
   140  								Id:               "233eaac9c6f245c0bb9c2d21eea12d1bno06",
   141  								Name:             "GeminiDB-efa6_priam_node_2",
   142  								Status:           "normal",
   143  								PrivateIp:        "192.168.0.174",
   144  								SpecCode:         "nosql.cassandra.xlarge.4",
   145  								AvailabilityZone: "bbb",
   146  							},
   147  							{
   148  								Id:               "d57d76d6320a4a7b86db82c317550c4ano02",
   149  								Name:             "GeminiDB-efa6_priam_node_1",
   150  								Status:           "normal",
   151  								PrivateIp:        "192.168.0.39",
   152  								SpecCode:         "nosql.cassandra.xlarge.4",
   153  								AvailabilityZone: "bbb",
   154  							},
   155  							{
   156  								Id:               "f46b0a1cf4d9400e9fd7af17f8742d37no02",
   157  								Name:             "GeminiDB-efa6_prima_node_3",
   158  								Status:           "normal",
   159  								PrivateIp:        "192.168.0.176",
   160  								SpecCode:         "nosql.cassandra.xlarge.4",
   161  								AvailabilityZone: "bbb",
   162  							},
   163  						},
   164  					},
   165  				},
   166  				BackupStrategy: instances.BackupStrategy{
   167  					StartTime: "16:00-17:00",
   168  					KeepDays:  7,
   169  				},
   170  			},
   171  
   172  			{
   173  				Port:              "8635",
   174  				Engine:            " rocksDB ",
   175  				DbUserName:        "rwuser",
   176  				PayMode:           "0",
   177  				TimeZone:          "",
   178  				MaintenanceWindow: "02:00-06:00",
   179  				Actions:           []string{"CREATE"},
   180  				Groups: []instances.Groups{
   181  					{
   182  						Id:     "0b0ff12541794e1084f6827e424be2d1gr07",
   183  						Status: "creating",
   184  						Volume: instances.Volume{
   185  							Size: "100",
   186  							Used: "0.003",
   187  						},
   188  						Nodes: []instances.Nodes{
   189  							{
   190  								Id:               "233eaac9c6f245c0bb9c2d21eea12d1bno06",
   191  								Name:             "GeminiDB-efa7_priam_node_2",
   192  								Status:           "normal",
   193  								PrivateIp:        "192.168.0.174",
   194  								SpecCode:         "nosql.cassandra.xlarge.4",
   195  								AvailabilityZone: "bbb",
   196  							},
   197  							{
   198  								Id:               "d57d76d6320a4a7b86db82c317550c4ano02",
   199  								Name:             "GeminiDB-efa7_priam_node_1",
   200  								Status:           "normal",
   201  								PrivateIp:        "192.168.0.39",
   202  								SpecCode:         "nosql.cassandra.xlarge.4",
   203  								AvailabilityZone: "bbb",
   204  							},
   205  							{
   206  								Id:               "f46b0a1cf4d9400e9fd7af17f8742d37no02",
   207  								Name:             "GeminiDB-efa7_prima_node_3",
   208  								Status:           "normal",
   209  								PrivateIp:        "192.168.0.176",
   210  								SpecCode:         "nosql.cassandra.xlarge.4",
   211  								AvailabilityZone: "bbb",
   212  							},
   213  						},
   214  					},
   215  				},
   216  				BackupStrategy: instances.BackupStrategy{
   217  					StartTime: "16:00-17:00",
   218  					KeepDays:  7,
   219  				},
   220  			},
   221  		},
   222  	}
   223  
   224  	ExpectedAllInstancesResponse.Instances[0].GeminiDBBase = instances.GeminiDBBase{
   225  		Id:              "8436a91546294036b75931e879882200in06",
   226  		Name:            "GeminiDB-efa6",
   227  		Status:          "normal",
   228  		Region:          "aaa",
   229  		Mode:            " Cluster",
   230  		VpcId:           "674e9b42-cd8d-4d25-a2e6-5abcc565b961",
   231  		SubnetId:        "f1df08c5-71d1-406a-aff0-de435a51007b",
   232  		SecurityGroupId: "7aa51dbf-5b63-40db-9724-dad3c4828b58",
   233  		DataStore: instances.DataStore{
   234  			Type:    "GeminiDB-Cassandra",
   235  			Version: "3.11",
   236  		},
   237  		EnterpriseProjectId: "0",
   238  	}
   239  
   240  	ExpectedAllInstancesResponse.Instances[1].GeminiDBBase = instances.GeminiDBBase{
   241  		Id:              "1236a91546294036b75931e879882200in06",
   242  		Name:            "GeminiDB-efa7",
   243  		Status:          "normal",
   244  		Region:          "aaa",
   245  		Mode:            " Cluster ",
   246  		VpcId:           "674e9b42-cd8d-4d25-a2e6-5abcc565b961",
   247  		SubnetId:        "f1df08c5-71d1-406a-aff0-de435a51007b",
   248  		SecurityGroupId: "7aa51dbf-5b63-40db-9724-dad3c4828b58",
   249  		DataStore: instances.DataStore{
   250  			Type:    "GeminiDB-Cassandra",
   251  			Version: "3.11",
   252  		},
   253  		EnterpriseProjectId: "0",
   254  	}
   255  
   256  	options := instances.ListGeminiDBInstanceOpts{
   257  		Id: "ed7cc6166ec24360a5ed5c5c9c2ed726in06",
   258  	}
   259  
   260  	count := 0
   261  	err := instances.List(client.ServiceClient(), options).EachPage(func(page pagination.Page) (bool, error) {
   262  		count++
   263  		actual, err := instances.ExtractGeminiDBInstances(page)
   264  		if err != nil {
   265  			t.Errorf("Failed to extract instances: %v", err)
   266  			return false, err
   267  		}
   268  		th.CheckDeepEquals(t, ExpectedAllInstancesResponse, actual)
   269  		return true, nil
   270  	})
   271  
   272  	th.AssertNoErr(t, err)
   273  	th.AssertEquals(t, 1, count)
   274  }