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