storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/cmd/erasure-common_test.go (about)

     1  /*
     2   * MinIO Cloud Storage, (C) 2017 MinIO, Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package cmd
    18  
    19  import (
    20  	"bytes"
    21  	"context"
    22  	"os"
    23  	"testing"
    24  )
    25  
    26  // Tests for if parent directory is object
    27  func TestErasureParentDirIsObject(t *testing.T) {
    28  	ctx, cancel := context.WithCancel(context.Background())
    29  	defer cancel()
    30  
    31  	obj, fsDisks, err := prepareErasureSets32(ctx)
    32  	if err != nil {
    33  		t.Fatalf("Unable to initialize 'Erasure' object layer.")
    34  	}
    35  	defer obj.Shutdown(context.Background())
    36  
    37  	// Remove all disks.
    38  	for _, disk := range fsDisks {
    39  		defer os.RemoveAll(disk)
    40  	}
    41  
    42  	bucketName := "testbucket"
    43  	objectName := "object"
    44  
    45  	if err = obj.MakeBucketWithLocation(GlobalContext, bucketName, BucketOptions{}); err != nil {
    46  		t.Fatal(err)
    47  	}
    48  
    49  	objectContent := "12345"
    50  	_, err = obj.PutObject(GlobalContext, bucketName, objectName,
    51  		mustGetPutObjReader(t, bytes.NewReader([]byte(objectContent)), int64(len(objectContent)), "", ""), ObjectOptions{})
    52  	if err != nil {
    53  		t.Fatal(err)
    54  	}
    55  
    56  	testCases := []struct {
    57  		expectedErr bool
    58  		objectName  string
    59  	}{
    60  		{
    61  			expectedErr: true,
    62  			objectName:  pathJoin(objectName, "parent-is-object"),
    63  		},
    64  		{
    65  			expectedErr: false,
    66  			objectName:  pathJoin("no-parent", "object"),
    67  		},
    68  	}
    69  
    70  	for _, testCase := range testCases {
    71  		t.Run("", func(t *testing.T) {
    72  			_, err = obj.PutObject(GlobalContext, bucketName, testCase.objectName,
    73  				mustGetPutObjReader(t, bytes.NewReader([]byte(objectContent)), int64(len(objectContent)), "", ""), ObjectOptions{})
    74  			if testCase.expectedErr && err == nil {
    75  				t.Error("Expected error but got nil")
    76  			}
    77  			if !testCase.expectedErr && err != nil {
    78  				t.Errorf("Expected nil but got %v", err)
    79  			}
    80  		})
    81  	}
    82  }