github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/cts/v1/tracker/testing/requests_test.go (about)

     1  package testing
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"testing"
     7  
     8  	"github.com/huaweicloud/golangsdk/openstack/cts/v1/tracker"
     9  	th "github.com/huaweicloud/golangsdk/testhelper"
    10  	fake "github.com/huaweicloud/golangsdk/testhelper/client"
    11  )
    12  
    13  func TestCreate(t *testing.T) {
    14  	th.SetupHTTP()
    15  	defer th.TeardownHTTP()
    16  
    17  	th.SetupHTTP()
    18  	defer th.TeardownHTTP()
    19  	th.Mux.HandleFunc("/tracker",
    20  		func(w http.ResponseWriter, r *http.Request) {
    21  			th.TestMethod(t, r, "POST")
    22  			th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
    23  			th.TestHeader(t, r, "Content-Type", "application/json")
    24  			th.TestHeader(t, r, "Accept", "application/json")
    25  			th.TestJSONRequest(t, r, createRequest)
    26  			w.Header().Add("Content-Type", "application/json")
    27  			w.WriteHeader(http.StatusCreated)
    28  			fmt.Fprintf(w, createResponse)
    29  		})
    30  
    31  	options := &tracker.CreateOptsWithSMN{
    32  		BucketName:     "obs-e51d",
    33  		FilePrefixName: "yO8Q",
    34  		SimpleMessageNotification: tracker.SimpleMessageNotification{
    35  			IsSupportSMN:          true,
    36  			TopicID:               "urn:smn:eu-de:626ce20e52a346c090b09cffc3e038e5:c2c-topic",
    37  			IsSendAllKeyOperation: false,
    38  			Operations:            []string{"login"},
    39  			NeedNotifyUserList:    []string{"user1", "user2"},
    40  		}}
    41  	n, err := tracker.Create(fake.ServiceClient(), options).Extract()
    42  
    43  	th.AssertNoErr(t, err)
    44  	th.AssertEquals(t, n.BucketName, "obs-e51d")
    45  	th.AssertEquals(t, n.Status, "enabled")
    46  	th.AssertEquals(t, n.TrackerName, "system")
    47  	th.AssertEquals(t, n.SimpleMessageNotification.TopicID, "urn:smn:eu-de:626ce20e52a346c090b09cffc3e038e5:c2c-topic")
    48  }
    49  
    50  func TestUpdate(t *testing.T) {
    51  	th.SetupHTTP()
    52  	defer th.TeardownHTTP()
    53  
    54  	th.Mux.HandleFunc("/tracker"+"/"+"system", func(w http.ResponseWriter, r *http.Request) {
    55  		th.TestMethod(t, r, "PUT")
    56  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
    57  		th.TestHeader(t, r, "Content-Type", "application/json")
    58  		th.TestHeader(t, r, "Accept", "application/json")
    59  		th.TestJSONRequest(t, r, updateRequest)
    60  		w.Header().Add("Content-Type", "application/json")
    61  		w.WriteHeader(http.StatusOK)
    62  		fmt.Fprintf(w, updateResponse)
    63  	})
    64  
    65  	options := &tracker.UpdateOptsWithSMN{
    66  		BucketName:     "cirros-img",
    67  		FilePrefixName: "yO8Q",
    68  		Status:         "disabled",
    69  		SimpleMessageNotification: tracker.SimpleMessageNotification{
    70  			IsSupportSMN:          false,
    71  			TopicID:               "urn:smn:eu-de:626ce20e52a346c090b09cffc3e038e5:c2c-topic",
    72  			IsSendAllKeyOperation: false,
    73  			Operations:            []string{"delete", "create", "login"},
    74  			NeedNotifyUserList:    []string{"user1", "user2"},
    75  		},
    76  	}
    77  	n, err := tracker.Update(fake.ServiceClient(), options).Extract()
    78  
    79  	th.AssertNoErr(t, err)
    80  	th.AssertEquals(t, n.BucketName, "cirros-img")
    81  	th.AssertEquals(t, n.FilePrefixName, "yO8Q")
    82  	th.AssertEquals(t, n.SimpleMessageNotification.TopicID, "urn:smn:eu-de:626ce20e52a346c090b09cffc3e038e5:c2c-topic")
    83  	th.AssertEquals(t, n.Status, "disabled")
    84  	th.AssertEquals(t, n.TrackerName, "system")
    85  }
    86  
    87  func TestDelete(t *testing.T) {
    88  	th.SetupHTTP()
    89  	defer th.TeardownHTTP()
    90  
    91  	th.Mux.HandleFunc("/tracker",
    92  		func(w http.ResponseWriter, r *http.Request) {
    93  			th.TestMethod(t, r, "DELETE")
    94  			th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
    95  			w.WriteHeader(http.StatusNoContent)
    96  		})
    97  
    98  	result := tracker.Delete(fake.ServiceClient())
    99  	th.AssertNoErr(t, result.Err)
   100  }
   101  
   102  func TestList(t *testing.T) {
   103  
   104  	th.SetupHTTP()
   105  	defer th.TeardownHTTP()
   106  
   107  	th.Mux.HandleFunc("/tracker", func(w http.ResponseWriter, r *http.Request) {
   108  		th.TestMethod(t, r, "GET")
   109  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   110  		w.Header().Add("Content-Type", "application/json")
   111  		w.WriteHeader(http.StatusOK)
   112  
   113  		fmt.Fprintf(w, getResponse)
   114  	})
   115  
   116  	actual, err := tracker.List(fake.ServiceClient(), tracker.ListOpts{})
   117  	if err != nil {
   118  		t.Errorf("Failed to extract cts tracker: %v", err)
   119  	}
   120  
   121  	expected := []tracker.Tracker{
   122  		{
   123  			Status:         "enabled",
   124  			BucketName:     "tf-test-bucket",
   125  			FilePrefixName: "yO8Q",
   126  			TrackerName:    "system",
   127  			SimpleMessageNotification: tracker.SimpleMessageNotification{
   128  				IsSupportSMN:          true,
   129  				TopicID:               "urn:smn:eu-de:626ce20e52a346c090b09cffc3e038e5:tf-test-topic",
   130  				IsSendAllKeyOperation: false,
   131  				Operations:            []string{"login"},
   132  				NeedNotifyUserList:    []string{"user1"},
   133  			},
   134  		},
   135  	}
   136  
   137  	th.AssertDeepEquals(t, expected, actual)
   138  }