github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/csbs/v1/policies/testing/requests_test.go (about) 1 package testing 2 3 import ( 4 "fmt" 5 "net/http" 6 "testing" 7 "time" 8 9 "github.com/huaweicloud/golangsdk" 10 "github.com/huaweicloud/golangsdk/openstack/csbs/v1/policies" 11 th "github.com/huaweicloud/golangsdk/testhelper" 12 fake "github.com/huaweicloud/golangsdk/testhelper/client" 13 ) 14 15 func TestGet(t *testing.T) { 16 th.SetupHTTP() 17 defer th.TeardownHTTP() 18 19 th.Mux.HandleFunc(policiesEndpoint+"/"+policies_id, func(w http.ResponseWriter, r *http.Request) { 20 th.TestMethod(t, r, "GET") 21 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 22 23 w.Header().Add("Content-Type", "application/json") 24 w.WriteHeader(http.StatusOK) 25 26 fmt.Fprintf(w, getResponse) 27 }) 28 29 s, err := policies.Get(fake.ServiceClient(), policies_id).Extract() 30 th.AssertNoErr(t, err) 31 th.AssertEquals(t, "5af626d2-19b9-4dc4-8e95-ddba008318b3", s.ID) 32 th.AssertEquals(t, "c2c-policy", s.Name) 33 th.AssertEquals(t, "OS::Nova::Server", s.Resources[0].Type) 34 th.AssertEquals(t, "resource1", s.Resources[0].Name) 35 th.AssertEquals(t, "cd5955b4-44c0-4f0a-ac57-2401b89cb347", s.Resources[0].Id) 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(policiesEndpoint, func(w http.ResponseWriter, r *http.Request) { 45 th.TestMethod(t, r, "POST") 46 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 47 th.TestHeader(t, r, "Content-Type", "application/json") 48 th.TestHeader(t, r, "Accept", "application/json") 49 th.TestJSONRequest(t, r, createRequest) 50 w.Header().Add("Content-Type", "application/json") 51 w.WriteHeader(http.StatusOK) 52 fmt.Fprintf(w, createResponse) 53 }) 54 55 options := &policies.CreateOpts{ 56 Name: "c2c-policy", 57 Description: "My plan", 58 ProviderId: "fc4d5750-22e7-4798-8a46-f48f62c4c1da", 59 Parameters: policies.PolicyParam{ 60 Common: map[string]interface{}{}, 61 }, 62 ScheduledOperations: []policies.ScheduledOperation{{ 63 Name: "my-backup-policy", 64 Description: "My backup policy", 65 Enabled: true, 66 OperationDefinition: policies.OperationDefinition{ 67 MaxBackups: 20, 68 }, 69 Trigger: policies.Trigger{ 70 Properties: policies.TriggerProperties{ 71 Pattern: "BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nRRULE:FREQ=WEEKLY;BYDAY=TH;BYHOUR=12;BYMINUTE=27\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n", 72 }, 73 }, 74 OperationType: "backup", 75 }}, 76 Resources: []policies.Resource{{ 77 Id: "cd5955b4-44c0-4f0a-ac57-2401b89cb347", 78 Type: "OS::Nova::Server", 79 Name: "resource1"}}, 80 } 81 n, err := policies.Create(fake.ServiceClient(), options).Extract() 82 83 th.AssertNoErr(t, err) 84 th.AssertEquals(t, n.ID, "5af626d2-19b9-4dc4-8e95-ddba008318b3") 85 th.AssertEquals(t, n.Status, "suspended") 86 th.AssertEquals(t, "OS::Nova::Server", n.Resources[0].Type) 87 th.AssertEquals(t, "resource1", n.Resources[0].Name) 88 th.AssertEquals(t, "cd5955b4-44c0-4f0a-ac57-2401b89cb347", n.Resources[0].Id) 89 } 90 91 func TestDelete(t *testing.T) { 92 th.SetupHTTP() 93 defer th.TeardownHTTP() 94 95 th.Mux.HandleFunc(policiesEndpoint+"/"+policies_id, func(w http.ResponseWriter, r *http.Request) { 96 th.TestMethod(t, r, "DELETE") 97 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 98 w.WriteHeader(http.StatusOK) 99 }) 100 101 result := policies.Delete(fake.ServiceClient(), policies_id) 102 103 th.AssertNoErr(t, result.Err) 104 } 105 106 func TestUpdate(t *testing.T) { 107 th.SetupHTTP() 108 defer th.TeardownHTTP() 109 110 th.Mux.HandleFunc(policiesEndpoint+"/"+policies_id, func(w http.ResponseWriter, r *http.Request) { 111 th.TestMethod(t, r, "PUT") 112 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 113 th.TestHeader(t, r, "Content-Type", "application/json") 114 th.TestHeader(t, r, "Accept", "application/json") 115 th.TestJSONRequest(t, r, updateRequest) 116 w.Header().Add("Content-Type", "application/json") 117 w.WriteHeader(http.StatusOK) 118 fmt.Fprintf(w, updateResponse) 119 }) 120 121 options := &policies.UpdateOpts{ 122 Name: "c2c-policy-update", 123 Parameters: policies.PolicyParam{ 124 Common: map[string]interface{}{}, 125 }, 126 ScheduledOperations: []policies.ScheduledOperationToUpdate{{ 127 Name: "my-backup-policy", 128 Description: "My backup policy", 129 Enabled: true, 130 Id: "b70c712d-f48b-43f7-9a0f-3bab86d59149", 131 OperationDefinition: policies.OperationDefinition{ 132 RetentionDurationDays: -1, 133 MaxBackups: 20, 134 }, 135 Trigger: policies.Trigger{ 136 Properties: policies.TriggerProperties{ 137 Pattern: "BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nRRULE:FREQ=WEEKLY;BYDAY=TH;BYHOUR=12;BYMINUTE=27\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n", 138 }, 139 }, 140 }}, 141 } 142 n, err := policies.Update(fake.ServiceClient(), policies_id, options).Extract() 143 144 th.AssertNoErr(t, err) 145 th.AssertEquals(t, n.Name, "c2c-policy-update") 146 th.AssertEquals(t, n.ID, "5af626d2-19b9-4dc4-8e95-ddba008318b3") 147 } 148 149 func TestList(t *testing.T) { 150 151 th.SetupHTTP() 152 defer th.TeardownHTTP() 153 154 th.Mux.HandleFunc(policiesEndpoint, func(w http.ResponseWriter, r *http.Request) { 155 th.TestMethod(t, r, "GET") 156 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 157 w.Header().Add("Content-Type", "application/json") 158 w.WriteHeader(http.StatusOK) 159 160 fmt.Fprintf(w, listResponse) 161 }) 162 163 actual, err := policies.List(fake.ServiceClient(), policies.ListOpts{}) 164 if err != nil { 165 t.Errorf("Failed to extract backup policies: %v", err) 166 } 167 168 var CreatedAt, _ = time.Parse(golangsdk.RFC3339MilliNoZ, "2018-08-20T10:43:56.246383") 169 expected := []policies.BackupPolicy{ 170 { 171 Status: "suspended", 172 ProviderId: "fc4d5750-22e7-4798-8a46-f48f62c4c1da", 173 Description: "My plann", 174 ScheduledOperations: []policies.ScheduledOperationResp{{ 175 176 Description: "My backup policy", 177 Enabled: true, 178 TriggerID: "831b5e69-0b75-420c-918e-9cbcb32d97f1", 179 Trigger: policies.TriggerResp{ 180 Properties: policies.TriggerPropertiesResp{ 181 Pattern: "BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nRRULE:FREQ=WEEKLY;BYDAY=TH;BYHOUR=12;BYMINUTE=27\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n", 182 }, 183 Type: "time", 184 ID: "831b5e69-0b75-420c-918e-9cbcb32d97f1", 185 Name: "default", 186 }, 187 OperationDefinition: policies.OperationDefinitionResp{ 188 MaxBackups: 5, 189 ProviderId: "fc4d5750-22e7-4798-8a46-f48f62c4c1da", 190 PlanId: "4d1ce19b-d681-4e44-a87e-c44eb9bfc4c7", 191 }, 192 OperationType: "backup", 193 ID: "e7d50d4c-2f38-40a4-9f9b-c9c355a52417", 194 Name: "my-backupp", 195 }, 196 }, 197 ID: "4d1ce19b-d681-4e44-a87e-c44eb9bfc4c7", 198 Name: "my-plan-test1", 199 Parameters: policies.PolicyParam{ 200 Common: map[string]interface{}{}, 201 }, 202 CreatedAt: CreatedAt, 203 ProjectId: "91d687759aed45d28b5f6084bc2fa8ad", 204 Resources: []policies.Resource{ 205 { 206 Type: "OS::Nova::Server", 207 Id: "9422f270-6fcf-4ba2-9319-a007f2f63a8e", 208 Name: "resource4", 209 }, 210 }, 211 }, 212 } 213 214 th.AssertDeepEquals(t, expected, actual) 215 }