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