github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/csbs/v1/backup/testing/requests_test.go (about)

     1  package testing
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"testing"
     7  
     8  	"time"
     9  
    10  	"github.com/huaweicloud/golangsdk"
    11  	"github.com/huaweicloud/golangsdk/openstack/csbs/v1/backup"
    12  	th "github.com/huaweicloud/golangsdk/testhelper"
    13  	fake "github.com/huaweicloud/golangsdk/testhelper/client"
    14  )
    15  
    16  func TestGet(t *testing.T) {
    17  	th.SetupHTTP()
    18  	defer th.TeardownHTTP()
    19  
    20  	th.Mux.HandleFunc(backupEndpoint+"/"+checkpoint_item_id, func(w http.ResponseWriter, r *http.Request) {
    21  		th.TestMethod(t, r, "GET")
    22  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
    23  
    24  		w.Header().Add("Content-Type", "application/json")
    25  		w.WriteHeader(http.StatusOK)
    26  
    27  		fmt.Fprintf(w, getResponse)
    28  	})
    29  
    30  	s, err := backup.Get(fake.ServiceClient(), checkpoint_item_id).ExtractBackup()
    31  	th.AssertNoErr(t, err)
    32  	th.AssertEquals(t, "7b99acfd-18c3-4f26-9d39-b4ebd2ea3e12", s.Id)
    33  	th.AssertEquals(t, "backup-c2c", s.Name)
    34  	th.AssertEquals(t, "available", s.Status)
    35  	th.AssertEquals(t, "2eefe592-8424-4778-8d0d-962c8a5dd6a4", s.CheckpointId)
    36  	th.AssertEquals(t, "backup des", s.Description)
    37  	th.AssertEquals(t, "OS::Nova::Server", s.ResourceType)
    38  }
    39  
    40  func TestCreate(t *testing.T) {
    41  	th.SetupHTTP()
    42  	defer th.TeardownHTTP()
    43  
    44  	th.SetupHTTP()
    45  	defer th.TeardownHTTP()
    46  	th.Mux.HandleFunc("/providers/fc4d5750-22e7-4798-8a46-f48f62c4c1da/resources/f8ddc472-cf00-4384-851e-5f2a68c33762/action",
    47  		func(w http.ResponseWriter, r *http.Request) {
    48  			th.TestMethod(t, r, "POST")
    49  			th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
    50  			th.TestHeader(t, r, "Content-Type", "application/json")
    51  			th.TestHeader(t, r, "Accept", "application/json")
    52  			th.TestJSONRequest(t, r, createRequest)
    53  			w.Header().Add("Content-Type", "application/json")
    54  			w.WriteHeader(http.StatusOK)
    55  			fmt.Fprintf(w, createResponse)
    56  		})
    57  
    58  	options := &backup.CreateOpts{
    59  		BackupName:  "c2c-backup",
    60  		Description: "mybackup"}
    61  	n, err := backup.Create(fake.ServiceClient(), "f8ddc472-cf00-4384-851e-5f2a68c33762", options).Extract()
    62  
    63  	th.AssertNoErr(t, err)
    64  	th.AssertEquals(t, n.Id, "92dba83d-cc6f-4883-a20d-de6934510b7e")
    65  	th.AssertEquals(t, n.Status, "protecting")
    66  	th.AssertEquals(t, n.ProtectionPlan.Id, "fake_b94f8b46-b0a1-485a-ad5b-9f8876b85495")
    67  	th.AssertEquals(t, n.ProtectionPlan.Name, "server protect plan for f8ddc472-cf00-4384-851e-5f2a68c33762")
    68  }
    69  
    70  func TestQueryResourceCapability(t *testing.T) {
    71  	th.SetupHTTP()
    72  	defer th.TeardownHTTP()
    73  
    74  	th.SetupHTTP()
    75  	defer th.TeardownHTTP()
    76  	th.Mux.HandleFunc("/providers/fc4d5750-22e7-4798-8a46-f48f62c4c1da/resources/action",
    77  		func(w http.ResponseWriter, r *http.Request) {
    78  			th.TestMethod(t, r, "POST")
    79  			th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
    80  			th.TestHeader(t, r, "Content-Type", "application/json")
    81  			th.TestHeader(t, r, "Accept", "application/json")
    82  			th.TestJSONRequest(t, r, queryRequest)
    83  			w.Header().Add("Content-Type", "application/json")
    84  			w.WriteHeader(http.StatusOK)
    85  			fmt.Fprintf(w, queryResponse)
    86  		})
    87  
    88  	options := &backup.ResourceBackupCapOpts{CheckProtectable: []backup.ResourceCapQueryParams{
    89  		{ResourceId: "069e678a-f1d1-4a38-880b-459bde82fcc6",
    90  			ResourceType: "OS::Nova::Server"}}}
    91  	n, err := backup.QueryResourceBackupCapability(fake.ServiceClient(), options).ExtractQueryResponse()
    92  
    93  	th.AssertNoErr(t, err)
    94  	th.AssertEquals(t, n[0].ResourceType, "OS::Nova::Server")
    95  	th.AssertEquals(t, n[0].ResourceId, "069e678a-f1d1-4a38-880b-459bde82fcc6")
    96  	th.AssertEquals(t, n[0].Result, true)
    97  }
    98  
    99  func TestDelete(t *testing.T) {
   100  	th.SetupHTTP()
   101  	defer th.TeardownHTTP()
   102  
   103  	th.Mux.HandleFunc("/providers/fc4d5750-22e7-4798-8a46-f48f62c4c1da/checkpoints/fc4d5750-22e7-4798-8a46-f48f62c4c1da",
   104  		func(w http.ResponseWriter, r *http.Request) {
   105  			th.TestMethod(t, r, "DELETE")
   106  			th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   107  			w.WriteHeader(http.StatusOK)
   108  		})
   109  
   110  	result := backup.Delete(fake.ServiceClient(), "fc4d5750-22e7-4798-8a46-f48f62c4c1da")
   111  	th.AssertNoErr(t, result.Err)
   112  }
   113  
   114  func TestList(t *testing.T) {
   115  
   116  	th.SetupHTTP()
   117  	defer th.TeardownHTTP()
   118  
   119  	th.Mux.HandleFunc("/checkpoint_items", func(w http.ResponseWriter, r *http.Request) {
   120  		th.TestMethod(t, r, "GET")
   121  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   122  		w.Header().Add("Content-Type", "application/json")
   123  		w.WriteHeader(http.StatusOK)
   124  
   125  		fmt.Fprintf(w, listResponse)
   126  	})
   127  
   128  	actual, err := backup.List(fake.ServiceClient(), backup.ListOpts{})
   129  	if err != nil {
   130  		t.Errorf("Failed to extract backups: %v", err)
   131  	}
   132  
   133  	var FinishedAt, _ = time.Parse(golangsdk.RFC3339MilliNoZ, "2018-08-14T08:31:08.720800")
   134  	expected := []backup.Backup{
   135  		{
   136  			Status: "available",
   137  			VMMetadata: backup.VMMetadata{
   138  				Eip:              "80.158.17.102",
   139  				CloudServiceType: "QEMU",
   140  				Ram:              8192,
   141  				Vcpus:            4,
   142  				RegionName:       "",
   143  				PrivateIp:        "192.168.0.209",
   144  				Disk:             0,
   145  				ImageType:        "gold",
   146  			},
   147  			Name:         "backup-c2c",
   148  			ResourceId:   "f8ddc472-cf00-4384-851e-5f2a68c33762",
   149  			CheckpointId: "2eefe592-8424-4778-8d0d-962c8a5dd6a4",
   150  			ExtendInfo: backup.ExtendInfo{
   151  				AutoTrigger:          false,
   152  				SpaceSavingRatio:     2,
   153  				ResourceName:         "ecs-ggao",
   154  				FailReason:           "",
   155  				ResourceAz:           "eu-de-02",
   156  				ImageType:            "backup",
   157  				FinishedAt:           FinishedAt,
   158  				AverageSpeed:         19,
   159  				CopyStatus:           "na",
   160  				Incremental:          false,
   161  				TaskId:               "1afcab08-9f97-11e8-9526-286ed488ca8c",
   162  				HypervisorType:       "QEMU",
   163  				SupportedRestoreMode: "backup",
   164  				Progress:             100,
   165  				Supportlld:           true,
   166  				FailOp:               "",
   167  				ResourceType:         "OS::Nova::Server",
   168  				Size:                 146184,
   169  			},
   170  			Id:           "7b99acfd-18c3-4f26-9d39-b4ebd2ea3e12",
   171  			ResourceType: "OS::Nova::Server",
   172  			Description:  "backup des",
   173  		},
   174  	}
   175  
   176  	th.AssertDeepEquals(t, expected, actual)
   177  }