github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/dns/v2/zones/testing/requests_test.go (about)

     1  package testing
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"io"
     7  	"net/http"
     8  	"testing"
     9  
    10  	"github.com/vnpaycloud-console/gophercloud/v2/openstack/dns/v2/zones"
    11  	"github.com/vnpaycloud-console/gophercloud/v2/pagination"
    12  	th "github.com/vnpaycloud-console/gophercloud/v2/testhelper"
    13  	"github.com/vnpaycloud-console/gophercloud/v2/testhelper/client"
    14  )
    15  
    16  func TestList(t *testing.T) {
    17  	th.SetupHTTP()
    18  	defer th.TeardownHTTP()
    19  	HandleListSuccessfully(t)
    20  
    21  	count := 0
    22  	err := zones.List(client.ServiceClient(), nil).EachPage(context.TODO(), func(_ context.Context, page pagination.Page) (bool, error) {
    23  		count++
    24  		actual, err := zones.ExtractZones(page)
    25  		th.AssertNoErr(t, err)
    26  		th.CheckDeepEquals(t, ExpectedZonesSlice, actual)
    27  
    28  		return true, nil
    29  	})
    30  	th.AssertNoErr(t, err)
    31  	th.CheckEquals(t, 1, count)
    32  }
    33  
    34  func TestListAllPages(t *testing.T) {
    35  	th.SetupHTTP()
    36  	defer th.TeardownHTTP()
    37  	HandleListSuccessfully(t)
    38  
    39  	allPages, err := zones.List(client.ServiceClient(), nil).AllPages(context.TODO())
    40  	th.AssertNoErr(t, err)
    41  	allZones, err := zones.ExtractZones(allPages)
    42  	th.AssertNoErr(t, err)
    43  	th.CheckEquals(t, 2, len(allZones))
    44  }
    45  
    46  func TestGet(t *testing.T) {
    47  	th.SetupHTTP()
    48  	defer th.TeardownHTTP()
    49  	HandleGetSuccessfully(t)
    50  
    51  	actual, err := zones.Get(context.TODO(), client.ServiceClient(), "a86dba58-0043-4cc6-a1bb-69d5e86f3ca3").Extract()
    52  	th.AssertNoErr(t, err)
    53  	th.CheckDeepEquals(t, &FirstZone, actual)
    54  }
    55  
    56  func TestCreate(t *testing.T) {
    57  	th.SetupHTTP()
    58  	defer th.TeardownHTTP()
    59  	HandleCreateSuccessfully(t)
    60  
    61  	createOpts := zones.CreateOpts{
    62  		Name:        "example.org.",
    63  		Email:       "joe@example.org",
    64  		Type:        "PRIMARY",
    65  		TTL:         7200,
    66  		Description: "This is an example zone.",
    67  	}
    68  
    69  	actual, err := zones.Create(context.TODO(), client.ServiceClient(), createOpts).Extract()
    70  	th.AssertNoErr(t, err)
    71  	th.CheckDeepEquals(t, &CreatedZone, actual)
    72  }
    73  
    74  func TestUpdate(t *testing.T) {
    75  	th.SetupHTTP()
    76  	defer th.TeardownHTTP()
    77  	HandleUpdateSuccessfully(t)
    78  
    79  	var description = "Updated Description"
    80  	updateOpts := zones.UpdateOpts{
    81  		TTL:         600,
    82  		Description: &description,
    83  	}
    84  
    85  	UpdatedZone := CreatedZone
    86  	UpdatedZone.Status = "PENDING"
    87  	UpdatedZone.Action = "UPDATE"
    88  	UpdatedZone.TTL = 600
    89  	UpdatedZone.Description = "Updated Description"
    90  
    91  	actual, err := zones.Update(context.TODO(), client.ServiceClient(), UpdatedZone.ID, updateOpts).Extract()
    92  	th.AssertNoErr(t, err)
    93  	th.CheckDeepEquals(t, &UpdatedZone, actual)
    94  }
    95  
    96  func TestDelete(t *testing.T) {
    97  	th.SetupHTTP()
    98  	defer th.TeardownHTTP()
    99  	HandleDeleteSuccessfully(t)
   100  
   101  	DeletedZone := CreatedZone
   102  	DeletedZone.Status = "PENDING"
   103  	DeletedZone.Action = "DELETE"
   104  	DeletedZone.TTL = 600
   105  	DeletedZone.Description = "Updated Description"
   106  
   107  	actual, err := zones.Delete(context.TODO(), client.ServiceClient(), DeletedZone.ID).Extract()
   108  	th.AssertNoErr(t, err)
   109  	th.CheckDeepEquals(t, &DeletedZone, actual)
   110  }
   111  
   112  func TestShare(t *testing.T) {
   113  	th.SetupHTTP()
   114  	defer th.TeardownHTTP()
   115  
   116  	th.Mux.HandleFunc("/zones/zone-id/shares", func(w http.ResponseWriter, r *http.Request) {
   117  		th.AssertEquals(t, r.Method, "POST")
   118  
   119  		body, err := io.ReadAll(r.Body)
   120  		defer r.Body.Close()
   121  		th.AssertNoErr(t, err)
   122  
   123  		var reqBody map[string]string
   124  		err = json.Unmarshal(body, &reqBody)
   125  		th.AssertNoErr(t, err)
   126  		expectedBody := map[string]string{"target_project_id": "project-id"}
   127  		th.CheckDeepEquals(t, expectedBody, reqBody)
   128  
   129  		w.WriteHeader(http.StatusCreated)
   130  	})
   131  
   132  	opts := zones.ShareZoneOpts{TargetProjectID: "project-id"}
   133  	err := zones.Share(context.TODO(), client.ServiceClient(), "zone-id", opts).ExtractErr()
   134  	th.AssertNoErr(t, err)
   135  }
   136  
   137  func TestUnshare(t *testing.T) {
   138  	th.SetupHTTP()
   139  	defer th.TeardownHTTP()
   140  
   141  	th.Mux.HandleFunc("/zones/zone-id/shares/share-id", func(w http.ResponseWriter, r *http.Request) {
   142  		th.AssertEquals(t, r.Method, "DELETE")
   143  		w.WriteHeader(http.StatusNoContent)
   144  	})
   145  
   146  	err := zones.Unshare(context.TODO(), client.ServiceClient(), "zone-id", "share-id").ExtractErr()
   147  	th.AssertNoErr(t, err)
   148  }