github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/sfs/v2/shares/testing/request_test.go (about)

     1  package testing
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/huaweicloud/golangsdk/openstack/sfs/v2/shares"
    10  	th "github.com/huaweicloud/golangsdk/testhelper"
    11  	"github.com/huaweicloud/golangsdk/testhelper/client"
    12  	fake "github.com/huaweicloud/golangsdk/testhelper/client"
    13  )
    14  
    15  func TestCreateShare(t *testing.T) {
    16  	th.SetupHTTP()
    17  	defer th.TeardownHTTP()
    18  
    19  	MockCreateResponse(t)
    20  
    21  	options := &shares.CreateOpts{Size: 1, Name: "my_test_share", ShareProto: "NFS"}
    22  	n, err := shares.Create(client.ServiceClient(), options).Extract()
    23  
    24  	th.AssertNoErr(t, err)
    25  	th.AssertEquals(t, n.Name, "my_test_share")
    26  	th.AssertEquals(t, n.Size, 1)
    27  	th.AssertEquals(t, n.ShareProto, "NFS")
    28  }
    29  
    30  func TestDeleteShare(t *testing.T) {
    31  	th.SetupHTTP()
    32  	defer th.TeardownHTTP()
    33  
    34  	MockDeleteResponse(t)
    35  
    36  	result := shares.Delete(client.ServiceClient(), shareID)
    37  	th.AssertNoErr(t, result.Err)
    38  }
    39  
    40  func TestUpdate(t *testing.T) {
    41  	th.SetupHTTP()
    42  	defer th.TeardownHTTP()
    43  
    44  	MockUpdateResponse(t)
    45  
    46  	options := &shares.UpdateOpts{DisplayName: "my_test_share_sfs", DisplayDescription: "test"}
    47  	n, err := shares.Update(client.ServiceClient(), shareID, options).Extract()
    48  
    49  	th.AssertNoErr(t, err)
    50  	th.AssertEquals(t, n.Name, "my_test_share_sfs")
    51  	th.AssertEquals(t, n.Description, "test")
    52  }
    53  
    54  func TestGetShare(t *testing.T) {
    55  	th.SetupHTTP()
    56  	defer th.TeardownHTTP()
    57  
    58  	MockGetResponse(t)
    59  
    60  	s, err := shares.Get(client.ServiceClient(), shareID).Extract()
    61  	th.AssertNoErr(t, err)
    62  	th.AssertDeepEquals(t, s, &shares.Share{
    63  		AvailabilityZone: "nova",
    64  		ShareNetworkID:   "713df749-aac0-4a54-af52-10f6c991e80c",
    65  		SnapshotID:       "",
    66  		ID:               shareID,
    67  		Size:             1,
    68  		ShareType:        "25747776-08e5-494f-ab40-a64b9d20d8f7",
    69  		ProjectID:        "16e1ab15c35a457e9c2b2aa189f544e1",
    70  		Metadata: map[string]string{
    71  			"project": "my_app",
    72  			"aim":     "doc",
    73  		},
    74  		Status:      "available",
    75  		Description: "My custom share London",
    76  		Host:        "manila2@generic1#GENERIC1",
    77  		Name:        "my_test_share",
    78  		CreatedAt:   time.Date(2015, time.September, 18, 10, 25, 24, 0, time.UTC),
    79  		ShareProto:  "NFS",
    80  		VolumeType:  "default",
    81  		IsPublic:    true,
    82  		Links: []map[string]string{
    83  			{
    84  				"href": "http://172.18.198.54:8786/v2/16e1ab15c35a457e9c2b2aa189f544e1/shares/011d21e2-fbc3-4e4a-9993-9ea223f73264",
    85  				"rel":  "self",
    86  			},
    87  			{
    88  				"href": "http://172.18.198.54:8786/16e1ab15c35a457e9c2b2aa189f544e1/shares/011d21e2-fbc3-4e4a-9993-9ea223f73264",
    89  				"rel":  "bookmark",
    90  			},
    91  		},
    92  	})
    93  }
    94  
    95  func TestListAccessRights(t *testing.T) {
    96  	th.SetupHTTP()
    97  	defer th.TeardownHTTP()
    98  
    99  	th.Mux.HandleFunc(shareEndpoint+"/"+shareID+"/action", func(w http.ResponseWriter, r *http.Request) {
   100  		th.TestMethod(t, r, "POST")
   101  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   102  		th.TestHeader(t, r, "Content-Type", "application/json")
   103  		th.TestHeader(t, r, "Accept", "application/json")
   104  		th.TestJSONRequest(t, r, listAccessRightsRequest)
   105  		w.Header().Add("Content-Type", "application/json")
   106  		w.WriteHeader(http.StatusOK)
   107  		fmt.Fprintf(w, listAccessRightsResponse)
   108  	})
   109  
   110  	c := client.ServiceClient()
   111  	// Client c must have Microversion set; minimum supported microversion for Grant Access is 2.7
   112  	c.Microversion = "2.7"
   113  
   114  	s, err := shares.ListAccessRights(c, shareID).ExtractAccessRights()
   115  
   116  	th.AssertNoErr(t, err)
   117  	th.AssertDeepEquals(t, s, []shares.AccessRight{
   118  		{
   119  			AccessType:  "cert",
   120  			AccessTo:    "5232f396-d6cc-4a81-8de3-afd7a7ecdfd8",
   121  			AccessLevel: "rw",
   122  			State:       "active",
   123  			ID:          "5158f095-4c43-49c0-b5a7-c458e85ed8c8",
   124  		},
   125  	})
   126  }
   127  
   128  func TestGrantAcessRight(t *testing.T) {
   129  	th.SetupHTTP()
   130  	defer th.TeardownHTTP()
   131  	th.Mux.HandleFunc(shareEndpoint+"/"+shareID+"/action", func(w http.ResponseWriter, r *http.Request) {
   132  		th.TestMethod(t, r, "POST")
   133  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   134  		th.TestHeader(t, r, "Content-Type", "application/json")
   135  		th.TestHeader(t, r, "Accept", "application/json")
   136  		th.TestJSONRequest(t, r, grantAccessRequest)
   137  		w.Header().Add("Content-Type", "application/json")
   138  		w.WriteHeader(http.StatusOK)
   139  		fmt.Fprintf(w, grantAccessResponse)
   140  	})
   141  
   142  	c := client.ServiceClient()
   143  	// Client c must have Microversion set; minimum supported microversion for Grant Access is 2.7
   144  	c.Microversion = "2.7"
   145  
   146  	grantaccOpts := shares.GrantAccessOpts{AccessTo: "5232f396-d6cc-4a81-8de3-afd7a7ecdfd8", AccessType: "cert", AccessLevel: "rw"}
   147  	s, err := shares.GrantAccess(c, shareID, grantaccOpts).ExtractAccess()
   148  
   149  	th.AssertNoErr(t, err)
   150  	th.AssertDeepEquals(t, s, &shares.AccessRight{
   151  		AccessType:  "cert",
   152  		AccessTo:    "5232f396-d6cc-4a81-8de3-afd7a7ecdfd8",
   153  		AccessLevel: "rw",
   154  		State:       "new",
   155  		ID:          "fc32500f-fa78-4f06-8caf-06ad7fb9726c",
   156  	})
   157  }
   158  
   159  func TestDeleteAcess(t *testing.T) {
   160  	th.SetupHTTP()
   161  	defer th.TeardownHTTP()
   162  	th.Mux.HandleFunc(shareEndpoint+"/"+shareID+"/action", func(w http.ResponseWriter, r *http.Request) {
   163  		th.TestMethod(t, r, "POST")
   164  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   165  		th.TestHeader(t, r, "Content-Type", "application/json")
   166  		th.TestHeader(t, r, "Accept", "application/json")
   167  		th.TestJSONRequest(t, r, deleteAccessRequest)
   168  		w.Header().Add("Content-Type", "application/json")
   169  		w.WriteHeader(http.StatusAccepted)
   170  
   171  	})
   172  	c := client.ServiceClient()
   173  	c.Microversion = "2.7"
   174  
   175  	res := shares.DeleteAccessOpts{AccessID: "ea07152b-d08b-4f6b-8785-ce64dce52679"}
   176  	s := shares.DeleteAccess(c, shareID, res)
   177  
   178  	th.AssertNoErr(t, s.Err)
   179  }
   180  
   181  func TestGetExportLocationsSuccess(t *testing.T) {
   182  	th.SetupHTTP()
   183  	defer th.TeardownHTTP()
   184  
   185  	th.Mux.HandleFunc(shareEndpoint+"/"+shareID+"/export_locations", func(w http.ResponseWriter, r *http.Request) {
   186  		th.TestMethod(t, r, "GET")
   187  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   188  		w.WriteHeader(http.StatusOK)
   189  		fmt.Fprintf(w, getExportLocationsResponse)
   190  	})
   191  
   192  	c := client.ServiceClient()
   193  	s, err := shares.GetExportLocations(c, shareID).ExtractExportLocations()
   194  
   195  	th.AssertNoErr(t, err)
   196  	th.AssertDeepEquals(t, s, []shares.ExportLocation{
   197  		{
   198  			Path: "sfs-nas1.eu-de.otc.t-systems.com:/share-d41ee18b",
   199  			ID:   "fab962ba-4b9a-475e-a380-8e856ed3f92d",
   200  		},
   201  	})
   202  }
   203  
   204  func TestListShare(t *testing.T) {
   205  
   206  	th.SetupHTTP()
   207  	defer th.TeardownHTTP()
   208  
   209  	th.Mux.HandleFunc(shareEndpoint+"/detail", func(w http.ResponseWriter, r *http.Request) {
   210  		th.TestMethod(t, r, "GET")
   211  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   212  		w.Header().Add("Content-Type", "application/json")
   213  		w.WriteHeader(http.StatusOK)
   214  
   215  		fmt.Fprintf(w, listResponse)
   216  	})
   217  
   218  	//count := 0
   219  
   220  	actual, err := shares.List(fake.ServiceClient(), shares.ListOpts{})
   221  	if err != nil {
   222  		t.Errorf("Failed to extract clusters: %v", err)
   223  	}
   224  
   225  	expected := []shares.Share{
   226  		{
   227  			Status:           "available",
   228  			AvailabilityZone: "nova",
   229  			ShareNetworkID:   "713df749-aac0-4a54-af52-10f6c991e80c",
   230  			Name:             "my_test_share",
   231  			ID:               "011d21e2-fbc3-4e4a-9993-9ea223f73264",
   232  			Size:             1,
   233  			ShareType:        "25747776-08e5-494f-ab40-a64b9d20d8f7",
   234  			ProjectID:        "16e1ab15c35a457e9c2b2aa189f544e1",
   235  			Description:      "My custom share London",
   236  			Host:             "manila2@generic1#GENERIC1",
   237  			IsPublic:         true,
   238  			ShareProto:       "NFS",
   239  			VolumeType:       "default",
   240  			CreatedAt:        time.Date(2015, time.September, 18, 10, 25, 24, 0, time.UTC),
   241  			Links: []map[string]string{
   242  				{
   243  					"href": "http://172.18.198.54:8786/v2/16e1ab15c35a457e9c2b2aa189f544e1/shares/011d21e2-fbc3-4e4a-9993-9ea223f73264",
   244  					"rel":  "self",
   245  				},
   246  				{
   247  					"href": "http://172.18.198.54:8786/16e1ab15c35a457e9c2b2aa189f544e1/shares/011d21e2-fbc3-4e4a-9993-9ea223f73264",
   248  					"rel":  "bookmark",
   249  				},
   250  			},
   251  		},
   252  	}
   253  
   254  	th.AssertDeepEquals(t, expected, actual)
   255  }
   256  
   257  func TestExpand(t *testing.T) {
   258  	th.SetupHTTP()
   259  	defer th.TeardownHTTP()
   260  
   261  	th.Mux.HandleFunc(shareEndpoint+"/"+shareID+"/"+"action", func(w http.ResponseWriter, r *http.Request) {
   262  		th.TestMethod(t, r, "POST")
   263  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   264  		th.TestHeader(t, r, "Accept", "application/json")
   265  		w.Header().Set("Content-Type", "application/json")
   266  		w.WriteHeader(http.StatusAccepted)
   267  	})
   268  	options := shares.ExpandOpts{OSExtend: shares.OSExtendOpts{NewSize: 3}}
   269  	resp := shares.Expand(fake.ServiceClient(), shareID, options)
   270  	th.AssertNoErr(t, resp.Err)
   271  
   272  }
   273  
   274  func TestShrink(t *testing.T) {
   275  	th.SetupHTTP()
   276  	defer th.TeardownHTTP()
   277  
   278  	th.Mux.HandleFunc(shareEndpoint+"/"+shareID+"/"+"action", func(w http.ResponseWriter, r *http.Request) {
   279  		th.TestMethod(t, r, "POST")
   280  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   281  		th.TestHeader(t, r, "Accept", "application/json")
   282  		w.Header().Set("Content-Type", "application/json")
   283  		w.WriteHeader(http.StatusAccepted)
   284  	})
   285  	options := shares.ShrinkOpts{OSShrink: shares.OSShrinkOpts{NewSize: 2}}
   286  	resp := shares.Shrink(fake.ServiceClient(), shareID, options)
   287  	th.AssertNoErr(t, resp.Err)
   288  
   289  }