github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/cbr/v3/policies/testing/fixtures.go (about) 1 package testing 2 3 import ( 4 "fmt" 5 "net/http" 6 "testing" 7 8 "github.com/huaweicloud/golangsdk/openstack/cbr/v3/policies" 9 th "github.com/huaweicloud/golangsdk/testhelper" 10 "github.com/huaweicloud/golangsdk/testhelper/client" 11 ) 12 13 const ( 14 expectedRequest = ` 15 { 16 "policy" : { 17 "name" : "policy001", 18 "operation_definition" : { 19 "retention_duration_days" : 1, 20 "timezone" : "UTC+08:00" 21 }, 22 "operation_type" : "backup", 23 "trigger" : { 24 "properties" : { 25 "pattern" : [ 26 "FREQ=WEEKLY;BYDAY=MO,TU,WE,TH,FR,SA,SU;BYHOUR=14;BYMINUTE=00" 27 ] 28 } 29 } 30 } 31 }` 32 expectedCreateResponse = ` 33 { 34 "policy" : { 35 "name" : "policy001", 36 "enabled" : true, 37 "trigger" : { 38 "properties" : { 39 "pattern" : [ "FREQ=WEEKLY;BYDAY=MO,TU,WE,TH,FR,SA,SU;BYHOUR=14;BYMINUTE=00" ], 40 "start_time" : "2019-05-08 06:57:05" 41 }, 42 "type" : "time", 43 "id" : "d67269a6-5369-42d7-8150-5254bd446328", 44 "name" : "default" 45 }, 46 "operation_definition" : { 47 "retention_duration_days" : 1, 48 "year_backups" : 0, 49 "day_backups" : 0, 50 "month_backups" : 0, 51 "week_backups" : 0, 52 "timezone" : "UTC+08:00" 53 }, 54 "operation_type" : "backup", 55 "id" : "cbb3ce6f-3332-4e7c-b98e-77290d8471ff" 56 } 57 }` 58 expectedUpdateResponse = ` 59 { 60 "policy" : { 61 "name" : "policy001", 62 "enabled" : true, 63 "trigger" : { 64 "properties" : { 65 "pattern" : [ "FREQ=WEEKLY;BYDAY=MO,TU,WE,TH,FR,SA,SU;BYHOUR=14;BYMINUTE=00" ], 66 "start_time" : "2019-05-08 06:57:05" 67 }, 68 "type" : "time", 69 "id" : "d67269a6-5369-42d7-8150-5254bd446328", 70 "name" : "default" 71 }, 72 "operation_definition" : { 73 "max_backups" : 0, 74 "year_backups" : 0, 75 "day_backups" : 0, 76 "month_backups" : 0, 77 "week_backups" : 0, 78 "timezone" : "UTC+08:00", 79 "retention_duration_days": 1 80 }, 81 "operation_type" : "backup", 82 "id" : "cbb3ce6f-3332-4e7c-b98e-77290d8471ff" 83 } 84 }` 85 expectedGetResponse = ` 86 { 87 "policy" : { 88 "name" : "policy001", 89 "associated_vaults" : [ ], 90 "enabled" : true, 91 "trigger" : { 92 "properties" : { 93 "pattern" : [ "FREQ=WEEKLY;BYDAY=MO,TU,WE,TH,FR,SA,SU;BYHOUR=14;BYMINUTE=00" ], 94 "start_time" : "2019-05-08 06:57:05" 95 }, 96 "type" : "time", 97 "id" : "d67269a6-5369-42d7-8150-5254bd446328", 98 "name" : "default" 99 }, 100 "operation_definition" : { 101 "retention_duration_days" : 1, 102 "year_backups" : 0, 103 "day_backups" : 0, 104 "month_backups" : 0, 105 "week_backups" : 0, 106 "timezone" : "UTC+08:00" 107 }, 108 "operation_type" : "backup", 109 "id" : "cbb3ce6f-3332-4e7c-b98e-77290d8471ff" 110 } 111 }` 112 113 expectedListResponse = ` 114 { 115 "policies" : [ { 116 "name" : "policy001", 117 "associated_vaults" : [ ], 118 "enabled" : true, 119 "trigger" : { 120 "properties" : { 121 "pattern" : [ "FREQ=WEEKLY;BYDAY=MO,TU,WE,TH,FR,SA,SU;BYHOUR=14;BYMINUTE=00" ], 122 "start_time" : "2019-05-08 06:57:05" 123 }, 124 "type" : "time", 125 "id" : "d67269a6-5369-42d7-8150-5254bd446328", 126 "name" : "default" 127 }, 128 "operation_definition" : { 129 "retention_duration_days" : 1, 130 "year_backups" : 0, 131 "day_backups" : 0, 132 "month_backups" : 0, 133 "week_backups" : 0, 134 "timezone" : "UTC+08:00" 135 }, 136 "operation_type" : "backup", 137 "id" : "cbb3ce6f-3332-4e7c-b98e-77290d8471ff" 138 } ], 139 "count" : 10 140 }` 141 ) 142 143 var ( 144 createOpts = &policies.CreateOpts{ 145 Name: "policy001", 146 OperationDefinition: &policies.PolicyODCreate{ 147 DailyBackups: 0, 148 WeekBackups: 0, 149 YearBackups: 0, 150 MonthBackups: 0, 151 MaxBackups: 0, 152 RetentionDurationDays: 1, 153 Timezone: "UTC+08:00", 154 }, 155 OperationType: "backup", 156 Trigger: &policies.Trigger{ 157 Properties: policies.TriggerProperties{ 158 Pattern: []string{ 159 "FREQ=WEEKLY;BYDAY=MO,TU,WE,TH,FR,SA,SU;BYHOUR=14;BYMINUTE=00", 160 }, 161 }, 162 }, 163 } 164 165 expectedCreateResponseData = &policies.Policy{ 166 ID: "cbb3ce6f-3332-4e7c-b98e-77290d8471ff", 167 Name: "policy001", 168 Enabled: true, 169 OperationDefinition: &policies.PolicyODCreate{ 170 DailyBackups: 0, 171 WeekBackups: 0, 172 YearBackups: 0, 173 MonthBackups: 0, 174 MaxBackups: 0, 175 RetentionDurationDays: 1, 176 Timezone: "UTC+08:00", 177 }, 178 OperationType: "backup", 179 Trigger: &policies.PolicyTriggerResp{ 180 TriggerID: "d67269a6-5369-42d7-8150-5254bd446328", 181 Name: "default", 182 Type: "time", 183 Properties: policies.PolicyTriggerPropertiesResp{ 184 Pattern: []string{"FREQ=WEEKLY;BYDAY=MO,TU,WE,TH,FR,SA,SU;BYHOUR=14;BYMINUTE=00"}, 185 StartTime: "2019-05-08 06:57:05", 186 }, 187 }, 188 } 189 updateEnabled = true 190 updateOpts = policies.UpdateOpts{ 191 Enabled: &updateEnabled, 192 Name: "policy001", 193 OperationDefinition: &policies.PolicyODCreate{ 194 DailyBackups: 0, 195 WeekBackups: 0, 196 YearBackups: 0, 197 MonthBackups: 0, 198 MaxBackups: 1, 199 RetentionDurationDays: 1, 200 Timezone: "UTC+08:00", 201 }, 202 Trigger: &policies.Trigger{ 203 Properties: policies.TriggerProperties{ 204 Pattern: []string{ 205 "FREQ=WEEKLY;BYDAY=MO,TU,WE,TH,FR,SA,SU;BYHOUR=14;BYMINUTE=00", 206 }, 207 }, 208 }, 209 } 210 211 expectedGetResponseData = &policies.Policy{ 212 ID: "cbb3ce6f-3332-4e7c-b98e-77290d8471ff", 213 Name: "policy001", 214 AssociatedVaults: []policies.PolicyAssociateVault{}, 215 Enabled: true, 216 OperationDefinition: &policies.PolicyODCreate{ 217 DailyBackups: 0, 218 WeekBackups: 0, 219 YearBackups: 0, 220 MonthBackups: 0, 221 MaxBackups: 0, 222 RetentionDurationDays: 1, 223 Timezone: "UTC+08:00", 224 }, 225 OperationType: "backup", 226 Trigger: &policies.PolicyTriggerResp{ 227 TriggerID: "d67269a6-5369-42d7-8150-5254bd446328", 228 Name: "default", 229 Type: "time", 230 Properties: policies.PolicyTriggerPropertiesResp{ 231 Pattern: []string{"FREQ=WEEKLY;BYDAY=MO,TU,WE,TH,FR,SA,SU;BYHOUR=14;BYMINUTE=00"}, 232 StartTime: "2019-05-08 06:57:05", 233 }, 234 }, 235 } 236 237 expectedListResponseData = []policies.Policy{ 238 { 239 ID: "cbb3ce6f-3332-4e7c-b98e-77290d8471ff", 240 Name: "policy001", 241 AssociatedVaults: []policies.PolicyAssociateVault{}, 242 Enabled: true, 243 OperationDefinition: &policies.PolicyODCreate{ 244 DailyBackups: 0, 245 WeekBackups: 0, 246 YearBackups: 0, 247 MonthBackups: 0, 248 MaxBackups: 0, 249 RetentionDurationDays: 1, 250 Timezone: "UTC+08:00", 251 }, 252 OperationType: "backup", 253 Trigger: &policies.PolicyTriggerResp{ 254 TriggerID: "d67269a6-5369-42d7-8150-5254bd446328", 255 Name: "default", 256 Type: "time", 257 Properties: policies.PolicyTriggerPropertiesResp{ 258 Pattern: []string{"FREQ=WEEKLY;BYDAY=MO,TU,WE,TH,FR,SA,SU;BYHOUR=14;BYMINUTE=00"}, 259 StartTime: "2019-05-08 06:57:05", 260 }, 261 }, 262 }, 263 } 264 ) 265 266 func handlePolicyCreation(t *testing.T) { 267 th.Mux.HandleFunc("/policies", func(w http.ResponseWriter, r *http.Request) { 268 th.TestMethod(t, r, "POST") 269 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 270 w.Header().Add("Content-Type", "application/json") 271 w.WriteHeader(http.StatusOK) 272 _, _ = fmt.Fprint(w, expectedCreateResponse) 273 }) 274 } 275 276 func handlePolicyDeletion(t *testing.T) { 277 th.Mux.HandleFunc("/policies/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) { 278 th.TestMethod(t, r, "DELETE") 279 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 280 w.Header().Add("Content-Type", "application/json") 281 w.WriteHeader(http.StatusNoContent) 282 }) 283 } 284 285 func handlePolicyUpdate(t *testing.T) { 286 th.Mux.HandleFunc("/policies/cbb3ce6f-3332-4e7c-b98e-77290d8471ff", func(w http.ResponseWriter, r *http.Request) { 287 th.TestMethod(t, r, "PUT") 288 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 289 w.Header().Add("Content-Type", "application/json") 290 w.WriteHeader(http.StatusOK) 291 _, _ = fmt.Fprint(w, expectedUpdateResponse) 292 }) 293 } 294 295 func handlePolicyGet(t *testing.T) { 296 th.Mux.HandleFunc("/policies/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) { 297 th.TestMethod(t, r, "GET") 298 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 299 w.Header().Add("Content-Type", "application/json") 300 w.WriteHeader(http.StatusOK) 301 _, _ = fmt.Fprint(w, expectedGetResponse) 302 }) 303 } 304 305 func handlePolicyList(t *testing.T) { 306 th.Mux.HandleFunc("/policies", func(w http.ResponseWriter, r *http.Request) { 307 th.TestMethod(t, r, "GET") 308 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 309 w.Header().Add("Content-Type", "application/json") 310 w.WriteHeader(http.StatusOK) 311 _, _ = fmt.Fprint(w, expectedListResponse) 312 }) 313 }