github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/dns/v2/recordsets/testing/requests_test.go (about) 1 package testing 2 3 import ( 4 "encoding/json" 5 "testing" 6 7 "github.com/huaweicloud/golangsdk/openstack/dns/v2/recordsets" 8 "github.com/huaweicloud/golangsdk/pagination" 9 th "github.com/huaweicloud/golangsdk/testhelper" 10 "github.com/huaweicloud/golangsdk/testhelper/client" 11 ) 12 13 func TestListByZone(t *testing.T) { 14 th.SetupHTTP() 15 defer th.TeardownHTTP() 16 HandleListByZoneSuccessfully(t) 17 18 count := 0 19 err := recordsets.ListByZone(client.ServiceClient(), "2150b1bf-dee2-4221-9d85-11f7886fb15f", nil).EachPage(func(page pagination.Page) (bool, error) { 20 count++ 21 actual, err := recordsets.ExtractRecordSets(page) 22 th.AssertNoErr(t, err) 23 th.CheckDeepEquals(t, ExpectedRecordSetSlice, actual) 24 25 return true, nil 26 }) 27 th.AssertNoErr(t, err) 28 th.CheckEquals(t, 1, count) 29 } 30 31 func TestListByZoneLimited(t *testing.T) { 32 th.SetupHTTP() 33 defer th.TeardownHTTP() 34 HandleListByZoneSuccessfully(t) 35 36 count := 0 37 listOpts := recordsets.ListOpts{ 38 Limit: 1, 39 Marker: "f7b10e9b-0cae-4a91-b162-562bc6096648", 40 } 41 err := recordsets.ListByZone(client.ServiceClient(), "2150b1bf-dee2-4221-9d85-11f7886fb15f", listOpts).EachPage(func(page pagination.Page) (bool, error) { 42 count++ 43 actual, err := recordsets.ExtractRecordSets(page) 44 th.AssertNoErr(t, err) 45 th.CheckDeepEquals(t, ExpectedRecordSetSliceLimited, actual) 46 47 return true, nil 48 }) 49 th.AssertNoErr(t, err) 50 th.CheckEquals(t, 1, count) 51 } 52 53 func TestListByZoneAllPages(t *testing.T) { 54 th.SetupHTTP() 55 defer th.TeardownHTTP() 56 HandleListByZoneSuccessfully(t) 57 58 allPages, err := recordsets.ListByZone(client.ServiceClient(), "2150b1bf-dee2-4221-9d85-11f7886fb15f", nil).AllPages() 59 th.AssertNoErr(t, err) 60 allRecordSets, err := recordsets.ExtractRecordSets(allPages) 61 th.AssertNoErr(t, err) 62 th.CheckEquals(t, 2, len(allRecordSets)) 63 } 64 65 func TestGet(t *testing.T) { 66 th.SetupHTTP() 67 defer th.TeardownHTTP() 68 HandleGetSuccessfully(t) 69 70 actual, err := recordsets.Get(client.ServiceClient(), "2150b1bf-dee2-4221-9d85-11f7886fb15f", "f7b10e9b-0cae-4a91-b162-562bc6096648").Extract() 71 th.AssertNoErr(t, err) 72 th.CheckDeepEquals(t, &FirstRecordSet, actual) 73 } 74 75 func TestNextPageURL(t *testing.T) { 76 var page recordsets.RecordSetPage 77 var body map[string]interface{} 78 err := json.Unmarshal([]byte(NextPageRequest), &body) 79 if err != nil { 80 t.Fatalf("Error unmarshaling data into page body: %v", err) 81 } 82 page.Body = body 83 expected := "http://127.0.0.1:9001/v2/zones/2150b1bf-dee2-4221-9d85-11f7886fb15f/recordsets?limit=1&marker=f7b10e9b-0cae-4a91-b162-562bc6096648" 84 actual, err := page.NextPageURL() 85 th.AssertNoErr(t, err) 86 th.CheckEquals(t, expected, actual) 87 } 88 89 func TestCreate(t *testing.T) { 90 th.SetupHTTP() 91 defer th.TeardownHTTP() 92 HandleCreateSuccessfully(t) 93 94 createOpts := recordsets.CreateOpts{ 95 Name: "example.org.", 96 Type: "A", 97 TTL: 3600, 98 Description: "This is an example record set.", 99 Records: []string{"10.1.0.2"}, 100 } 101 102 actual, err := recordsets.Create(client.ServiceClient(), "2150b1bf-dee2-4221-9d85-11f7886fb15f", createOpts).Extract() 103 th.AssertNoErr(t, err) 104 th.CheckDeepEquals(t, &CreatedRecordSet, actual) 105 } 106 107 func TestUpdate(t *testing.T) { 108 th.SetupHTTP() 109 defer th.TeardownHTTP() 110 HandleUpdateSuccessfully(t) 111 112 updateOpts := recordsets.UpdateOpts{ 113 TTL: 0, 114 Description: "Updated description", 115 Records: []string{"10.1.0.2", "10.1.0.3"}, 116 } 117 118 UpdatedRecordSet := CreatedRecordSet 119 UpdatedRecordSet.Status = "PENDING" 120 UpdatedRecordSet.Action = "UPDATE" 121 UpdatedRecordSet.Description = "Updated description" 122 UpdatedRecordSet.Records = []string{"10.1.0.2", "10.1.0.3"} 123 UpdatedRecordSet.Version = 2 124 125 actual, err := recordsets.Update(client.ServiceClient(), UpdatedRecordSet.ZoneID, UpdatedRecordSet.ID, updateOpts).Extract() 126 th.AssertNoErr(t, err) 127 th.CheckDeepEquals(t, &UpdatedRecordSet, actual) 128 } 129 130 func TestDelete(t *testing.T) { 131 th.SetupHTTP() 132 defer th.TeardownHTTP() 133 HandleDeleteSuccessfully(t) 134 135 DeletedRecordSet := CreatedRecordSet 136 DeletedRecordSet.Status = "PENDING" 137 DeletedRecordSet.Action = "UPDATE" 138 DeletedRecordSet.Description = "Updated description" 139 DeletedRecordSet.Records = []string{"10.1.0.2", "10.1.0.3"} 140 DeletedRecordSet.Version = 2 141 142 err := recordsets.Delete(client.ServiceClient(), DeletedRecordSet.ZoneID, DeletedRecordSet.ID).ExtractErr() 143 th.AssertNoErr(t, err) 144 //th.CheckDeepEquals(t, &DeletedZone, actual) 145 }