github.com/gophercloud/gophercloud@v1.11.0/openstack/objectstorage/v1/containers/testing/requests_test.go (about)

     1  package testing
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/gophercloud/gophercloud/openstack/objectstorage/v1/containers"
     8  	"github.com/gophercloud/gophercloud/pagination"
     9  	th "github.com/gophercloud/gophercloud/testhelper"
    10  	fake "github.com/gophercloud/gophercloud/testhelper/client"
    11  )
    12  
    13  var (
    14  	metadata = map[string]string{"gophercloud-test": "containers"}
    15  )
    16  
    17  func TestContainerNames(t *testing.T) {
    18  	for _, tc := range [...]struct {
    19  		name          string
    20  		containerName string
    21  	}{
    22  		{
    23  			"rejects_a_slash",
    24  			"one/two",
    25  		},
    26  		{
    27  			"rejects_an_escaped_slash",
    28  			"one%2Ftwo",
    29  		},
    30  		{
    31  			"rejects_an_escaped_slash_lowercase",
    32  			"one%2ftwo",
    33  		},
    34  	} {
    35  		t.Run(tc.name, func(t *testing.T) {
    36  			t.Run("create", func(t *testing.T) {
    37  				th.SetupHTTP()
    38  				defer th.TeardownHTTP()
    39  				HandleCreateContainerSuccessfully(t)
    40  
    41  				_, err := containers.Create(fake.ServiceClient(), tc.containerName, nil).Extract()
    42  				th.CheckErr(t, err, &containers.ErrInvalidContainerName{})
    43  			})
    44  			t.Run("delete", func(t *testing.T) {
    45  				th.SetupHTTP()
    46  				defer th.TeardownHTTP()
    47  				HandleDeleteContainerSuccessfully(t, WithPath("/"))
    48  
    49  				res := containers.Delete(fake.ServiceClient(), tc.containerName)
    50  				th.CheckErr(t, res.Err, &containers.ErrInvalidContainerName{})
    51  			})
    52  			t.Run("update", func(t *testing.T) {
    53  				th.SetupHTTP()
    54  				defer th.TeardownHTTP()
    55  				HandleUpdateContainerSuccessfully(t, WithPath("/"))
    56  
    57  				contentType := "text/plain"
    58  				options := &containers.UpdateOpts{
    59  					Metadata:         map[string]string{"foo": "bar"},
    60  					ContainerWrite:   new(string),
    61  					ContainerRead:    new(string),
    62  					ContainerSyncTo:  new(string),
    63  					ContainerSyncKey: new(string),
    64  					ContentType:      &contentType,
    65  				}
    66  				res := containers.Update(fake.ServiceClient(), tc.containerName, options)
    67  				th.CheckErr(t, res.Err, &containers.ErrInvalidContainerName{})
    68  			})
    69  			t.Run("get", func(t *testing.T) {
    70  				th.SetupHTTP()
    71  				defer th.TeardownHTTP()
    72  				HandleGetContainerSuccessfully(t, WithPath("/"))
    73  
    74  				res := containers.Get(fake.ServiceClient(), tc.containerName, nil)
    75  				_, err := res.ExtractMetadata()
    76  				th.CheckErr(t, err, &containers.ErrInvalidContainerName{})
    77  
    78  				_, err = res.Extract()
    79  				th.CheckErr(t, err, &containers.ErrInvalidContainerName{})
    80  			})
    81  		})
    82  	}
    83  }
    84  
    85  func TestListContainerInfo(t *testing.T) {
    86  	th.SetupHTTP()
    87  	defer th.TeardownHTTP()
    88  	HandleListContainerInfoSuccessfully(t)
    89  
    90  	count := 0
    91  	err := containers.List(fake.ServiceClient(), &containers.ListOpts{Full: true}).EachPage(func(page pagination.Page) (bool, error) {
    92  		count++
    93  		actual, err := containers.ExtractInfo(page)
    94  		th.AssertNoErr(t, err)
    95  
    96  		th.CheckDeepEquals(t, ExpectedListInfo, actual)
    97  
    98  		return true, nil
    99  	})
   100  	th.AssertNoErr(t, err)
   101  	th.CheckEquals(t, 1, count)
   102  }
   103  
   104  func TestListAllContainerInfo(t *testing.T) {
   105  	th.SetupHTTP()
   106  	defer th.TeardownHTTP()
   107  	HandleListContainerInfoSuccessfully(t)
   108  
   109  	allPages, err := containers.List(fake.ServiceClient(), &containers.ListOpts{Full: true}).AllPages()
   110  	th.AssertNoErr(t, err)
   111  	actual, err := containers.ExtractInfo(allPages)
   112  	th.AssertNoErr(t, err)
   113  	th.CheckDeepEquals(t, ExpectedListInfo, actual)
   114  }
   115  
   116  func TestListContainerNames(t *testing.T) {
   117  	th.SetupHTTP()
   118  	defer th.TeardownHTTP()
   119  	HandleListContainerNamesSuccessfully(t)
   120  
   121  	count := 0
   122  	err := containers.List(fake.ServiceClient(), &containers.ListOpts{Full: false}).EachPage(func(page pagination.Page) (bool, error) {
   123  		count++
   124  		actual, err := containers.ExtractNames(page)
   125  		if err != nil {
   126  			t.Errorf("Failed to extract container names: %v", err)
   127  			return false, err
   128  		}
   129  
   130  		th.CheckDeepEquals(t, ExpectedListNames, actual)
   131  
   132  		return true, nil
   133  	})
   134  	th.AssertNoErr(t, err)
   135  	th.CheckEquals(t, 1, count)
   136  }
   137  
   138  func TestListAllContainerNames(t *testing.T) {
   139  	th.SetupHTTP()
   140  	defer th.TeardownHTTP()
   141  	HandleListContainerNamesSuccessfully(t)
   142  
   143  	allPages, err := containers.List(fake.ServiceClient(), &containers.ListOpts{Full: false}).AllPages()
   144  	th.AssertNoErr(t, err)
   145  	actual, err := containers.ExtractNames(allPages)
   146  	th.AssertNoErr(t, err)
   147  	th.CheckDeepEquals(t, ExpectedListNames, actual)
   148  }
   149  
   150  func TestListZeroContainerNames(t *testing.T) {
   151  	th.SetupHTTP()
   152  	defer th.TeardownHTTP()
   153  	HandleListZeroContainerNames204(t)
   154  
   155  	allPages, err := containers.List(fake.ServiceClient(), &containers.ListOpts{Full: false}).AllPages()
   156  	th.AssertNoErr(t, err)
   157  	actual, err := containers.ExtractNames(allPages)
   158  	th.AssertNoErr(t, err)
   159  	th.CheckDeepEquals(t, []string{}, actual)
   160  }
   161  
   162  func TestCreateContainer(t *testing.T) {
   163  	th.SetupHTTP()
   164  	defer th.TeardownHTTP()
   165  	HandleCreateContainerSuccessfully(t)
   166  
   167  	options := containers.CreateOpts{ContentType: "application/json", Metadata: map[string]string{"foo": "bar"}}
   168  	res := containers.Create(fake.ServiceClient(), "testContainer", options)
   169  	th.CheckEquals(t, "bar", res.Header["X-Container-Meta-Foo"][0])
   170  
   171  	expected := &containers.CreateHeader{
   172  		ContentLength: 0,
   173  		ContentType:   "text/html; charset=UTF-8",
   174  		Date:          time.Date(2016, time.August, 17, 19, 25, 43, 0, time.UTC),
   175  		TransID:       "tx554ed59667a64c61866f1-0058b4ba37",
   176  	}
   177  	actual, err := res.Extract()
   178  	th.AssertNoErr(t, err)
   179  	th.AssertDeepEquals(t, expected, actual)
   180  }
   181  
   182  func TestDeleteContainer(t *testing.T) {
   183  	th.SetupHTTP()
   184  	defer th.TeardownHTTP()
   185  	HandleDeleteContainerSuccessfully(t)
   186  
   187  	res := containers.Delete(fake.ServiceClient(), "testContainer")
   188  	th.AssertNoErr(t, res.Err)
   189  }
   190  
   191  func TestBulkDelete(t *testing.T) {
   192  	th.SetupHTTP()
   193  	defer th.TeardownHTTP()
   194  	HandleBulkDeleteSuccessfully(t)
   195  
   196  	expected := containers.BulkDeleteResponse{
   197  		ResponseStatus: "foo",
   198  		ResponseBody:   "bar",
   199  		NumberDeleted:  2,
   200  		Errors:         [][]string{},
   201  	}
   202  
   203  	resp, err := containers.BulkDelete(fake.ServiceClient(), []string{"testContainer1", "testContainer2"}).Extract()
   204  	th.AssertNoErr(t, err)
   205  	th.AssertDeepEquals(t, expected, *resp)
   206  }
   207  
   208  func TestUpdateContainer(t *testing.T) {
   209  	th.SetupHTTP()
   210  	defer th.TeardownHTTP()
   211  	HandleUpdateContainerSuccessfully(t)
   212  
   213  	contentType := "text/plain"
   214  	options := &containers.UpdateOpts{
   215  		Metadata:         map[string]string{"foo": "bar"},
   216  		ContainerWrite:   new(string),
   217  		ContainerRead:    new(string),
   218  		ContainerSyncTo:  new(string),
   219  		ContainerSyncKey: new(string),
   220  		ContentType:      &contentType,
   221  	}
   222  	res := containers.Update(fake.ServiceClient(), "testContainer", options)
   223  	th.AssertNoErr(t, res.Err)
   224  }
   225  
   226  func TestGetContainer(t *testing.T) {
   227  	th.SetupHTTP()
   228  	defer th.TeardownHTTP()
   229  	HandleGetContainerSuccessfully(t)
   230  
   231  	getOpts := containers.GetOpts{
   232  		Newest: true,
   233  	}
   234  	res := containers.Get(fake.ServiceClient(), "testContainer", getOpts)
   235  	_, err := res.ExtractMetadata()
   236  	th.AssertNoErr(t, err)
   237  
   238  	expected := &containers.GetHeader{
   239  		AcceptRanges:    "bytes",
   240  		BytesUsed:       100,
   241  		ContentType:     "application/json; charset=utf-8",
   242  		Date:            time.Date(2016, time.August, 17, 19, 25, 43, 0, time.UTC),
   243  		ObjectCount:     4,
   244  		Read:            []string{"test"},
   245  		TransID:         "tx554ed59667a64c61866f1-0057b4ba37",
   246  		Write:           []string{"test2", "user4"},
   247  		StoragePolicy:   "test_policy",
   248  		Timestamp:       1471298837.95721,
   249  		VersionsEnabled: true,
   250  	}
   251  	actual, err := res.Extract()
   252  	th.AssertNoErr(t, err)
   253  	th.AssertDeepEquals(t, expected, actual)
   254  }
   255  
   256  func TestUpdateContainerVersioningOff(t *testing.T) {
   257  	th.SetupHTTP()
   258  	defer th.TeardownHTTP()
   259  	HandleUpdateContainerVersioningOff(t)
   260  
   261  	contentType := "text/plain"
   262  	options := &containers.UpdateOpts{
   263  		Metadata:         map[string]string{"foo": "bar"},
   264  		ContainerWrite:   new(string),
   265  		ContainerRead:    new(string),
   266  		ContainerSyncTo:  new(string),
   267  		ContainerSyncKey: new(string),
   268  		ContentType:      &contentType,
   269  		VersionsEnabled:  new(bool),
   270  	}
   271  	_, err := containers.Update(fake.ServiceClient(), "testVersioning", options).Extract()
   272  	th.AssertNoErr(t, err)
   273  }
   274  
   275  func TestUpdateContainerVersioningOn(t *testing.T) {
   276  	th.SetupHTTP()
   277  	defer th.TeardownHTTP()
   278  	HandleUpdateContainerVersioningOn(t)
   279  
   280  	iTrue := true
   281  	contentType := "text/plain"
   282  	options := &containers.UpdateOpts{
   283  		Metadata:         map[string]string{"foo": "bar"},
   284  		ContainerWrite:   new(string),
   285  		ContainerRead:    new(string),
   286  		ContainerSyncTo:  new(string),
   287  		ContainerSyncKey: new(string),
   288  		ContentType:      &contentType,
   289  		VersionsEnabled:  &iTrue,
   290  	}
   291  	_, err := containers.Update(fake.ServiceClient(), "testVersioning", options).Extract()
   292  	th.AssertNoErr(t, err)
   293  }