github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/vbs/v2/policies/testing/requests_test.go (about) 1 package testing 2 3 import ( 4 "fmt" 5 "net/http" 6 "testing" 7 8 fake "github.com/huaweicloud/golangsdk/openstack/vbs/v2/common" 9 "github.com/huaweicloud/golangsdk/openstack/vbs/v2/policies" 10 th "github.com/huaweicloud/golangsdk/testhelper" 11 ) 12 13 func TestCreateV2Policy(t *testing.T) { 14 th.SetupHTTP() 15 defer th.TeardownHTTP() 16 17 th.Mux.HandleFunc("/backuppolicy", func(w http.ResponseWriter, r *http.Request) { 18 th.TestMethod(t, r, "POST") 19 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 20 w.Header().Set("Content-Type", "application/json") 21 th.TestHeader(t, r, "Accept", "application/json") 22 23 th.TestJSONRequest(t, r, ` 24 { 25 "backup_policy_name": "Test_Policy", 26 "scheduled_policy" : { 27 "remain_first_backup_of_curMonth" : "Y", 28 "rentention_num" : 10, 29 "frequency" : 1, 30 "start_time" : "12:00", 31 "status" : "ON" 32 }, 33 "tags":[{ 34 "key":"key", 35 "value":"value" 36 }] 37 } 38 `) 39 w.Header().Set("Content-Type", "application/json") 40 w.WriteHeader(http.StatusOK) 41 fmt.Fprintf(w, Output) 42 }) 43 createOptions := policies.CreateOpts{Name: "Test_Policy", ScheduledPolicy: policies.ScheduledPolicy{StartTime: "12:00", Status: "ON", Frequency: 1, RententionNum: 10, RemainFirstBackup: "Y"}, Tags: []policies.Tag{{Key: "key", Value: "value"}}} 44 actual, err := policies.Create(fake.ServiceClient(), createOptions).Extract() 45 th.AssertNoErr(t, err) 46 expected := Expected 47 th.AssertDeepEquals(t, expected, actual) 48 } 49 50 func TestUpdateV2Policy(t *testing.T) { 51 th.SetupHTTP() 52 defer th.TeardownHTTP() 53 54 th.Mux.HandleFunc("/backuppolicy/af8a20b0-117d-4fc3-ae53-aa3968a4f870", func(w http.ResponseWriter, r *http.Request) { 55 th.TestMethod(t, r, "PUT") 56 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 57 th.TestHeader(t, r, "Content-Type", "application/json") 58 th.TestHeader(t, r, "Accept", "application/json") 59 60 th.TestJSONRequest(t, r, ` 61 { 62 "backup_policy_name": "Test_02", 63 "scheduled_policy" : { 64 "remain_first_backup_of_curMonth" : "Y", 65 "rentention_num" : 10, 66 "frequency" : 1, 67 "start_time" : "10:00", 68 "status" : "ON" 69 } 70 } 71 `) 72 w.Header().Set("Content-Type", "application/json") 73 w.WriteHeader(http.StatusOK) 74 fmt.Fprintf(w, UpdateOutput) 75 }) 76 updateOptions := policies.UpdateOpts{Name: "Test_02", ScheduledPolicy: policies.UpdateSchedule{StartTime: "10:00", Status: "ON", Frequency: 1, RententionNum: 10, RemainFirstBackup: "Y"}} 77 update, err := policies.Update(fake.ServiceClient(), "af8a20b0-117d-4fc3-ae53-aa3968a4f870", updateOptions).Extract() 78 th.AssertNoErr(t, err) 79 expected := Update 80 th.AssertDeepEquals(t, expected, update) 81 } 82 83 func TestDeleteV2Policy(t *testing.T) { 84 th.SetupHTTP() 85 defer th.TeardownHTTP() 86 87 th.Mux.HandleFunc("/backuppolicy/af8a20b0-117d-4fc3-ae53-aa3968a4f870", func(w http.ResponseWriter, r *http.Request) { 88 th.TestMethod(t, r, "DELETE") 89 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 90 w.WriteHeader(http.StatusNoContent) 91 }) 92 93 delete := policies.Delete(fake.ServiceClient(), "af8a20b0-117d-4fc3-ae53-aa3968a4f870") 94 th.AssertNoErr(t, delete.Err) 95 } 96 97 func TestListV2Policy(t *testing.T) { 98 99 th.SetupHTTP() 100 defer th.TeardownHTTP() 101 102 th.Mux.HandleFunc("/backuppolicy", func(w http.ResponseWriter, r *http.Request) { 103 th.TestMethod(t, r, "GET") 104 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 105 106 w.Header().Add("Content-Type", "application/json") 107 w.WriteHeader(http.StatusOK) 108 109 fmt.Fprintf(w, ListOutput) 110 }) 111 112 actual, err := policies.List(fake.ServiceClient(), policies.ListOpts{}) 113 if err != nil { 114 t.Errorf("Failed to extract clusters: %v", err) 115 } 116 117 expected := ListPolicies 118 119 th.AssertDeepEquals(t, expected, actual) 120 } 121 122 func TestAssociateV2Policy(t *testing.T) { 123 th.SetupHTTP() 124 defer th.TeardownHTTP() 125 126 th.Mux.HandleFunc("/backuppolicyresources", func(w http.ResponseWriter, r *http.Request) { 127 th.TestMethod(t, r, "POST") 128 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 129 th.TestHeader(t, r, "Content-Type", "application/json") 130 th.TestHeader(t, r, "Accept", "application/json") 131 132 th.TestJSONRequest(t, r, ` 133 { 134 "backup_policy_id":"915d1fd8-63cb-4054-a2b0-2778210e3a75", 135 "resources":[{ 136 "resource_id":"0f187b65-8d0e-4fc0-9096-3b55d330531e", 137 "resource_type":"volume" 138 }] 139 } 140 `) 141 w.Header().Set("Content-Type", "application/json") 142 w.WriteHeader(http.StatusOK) 143 fmt.Fprintf(w, AssociateOutput) 144 }) 145 AssoOpts := policies.AssociateOpts{PolicyID: "915d1fd8-63cb-4054-a2b0-2778210e3a75", Resources: []policies.AssociateResource{{ResourceID: "0f187b65-8d0e-4fc0-9096-3b55d330531e", ResourceType: "volume"}}} 146 associate, err := policies.Associate(fake.ServiceClient(), AssoOpts).ExtractResource() 147 th.AssertNoErr(t, err) 148 expected := Associate 149 th.AssertDeepEquals(t, expected, associate) 150 } 151 152 func TestDisassociateV2Policy(t *testing.T) { 153 th.SetupHTTP() 154 defer th.TeardownHTTP() 155 156 th.Mux.HandleFunc("/backuppolicyresources/915d1fd8-63cb-4054-a2b0-2778210e3a75/deleted_resources", func(w http.ResponseWriter, r *http.Request) { 157 th.TestMethod(t, r, "POST") 158 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 159 th.TestHeader(t, r, "Content-Type", "application/json") 160 th.TestHeader(t, r, "Accept", "application/json") 161 162 th.TestJSONRequest(t, r, ` 163 { 164 "resources": [ 165 { 166 "resource_id": "0f187b65-8d0e-4fc0-9096-3b55d330531e" 167 } 168 ] 169 } 170 `) 171 w.Header().Set("Content-Type", "application/json") 172 w.WriteHeader(http.StatusOK) 173 fmt.Fprintf(w, DisssociateOutput) 174 }) 175 options := policies.DisassociateOpts{Resources: []policies.DisassociateResource{{ResourceID: "0f187b65-8d0e-4fc0-9096-3b55d330531e"}}} 176 associate, err := policies.Disassociate(fake.ServiceClient(), "915d1fd8-63cb-4054-a2b0-2778210e3a75", options).ExtractResource() 177 th.AssertNoErr(t, err) 178 expected := Disassociate 179 th.AssertDeepEquals(t, expected, associate) 180 }