github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/storage/cloud/nodelocal_storage_test.go (about)

     1  // Copyright 2019 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package cloud
    12  
    13  import (
    14  	"context"
    15  	"fmt"
    16  	"testing"
    17  
    18  	"github.com/cockroachdb/cockroach/pkg/base"
    19  	"github.com/cockroachdb/cockroach/pkg/blobs"
    20  	"github.com/cockroachdb/cockroach/pkg/testutils"
    21  	"github.com/cockroachdb/cockroach/pkg/util/leaktest"
    22  )
    23  
    24  func TestPutLocal(t *testing.T) {
    25  	defer leaktest.AfterTest(t)()
    26  
    27  	p, cleanupFn := testutils.TempDir(t)
    28  	defer cleanupFn()
    29  
    30  	testSettings.ExternalIODir = p
    31  	dest := MakeLocalStorageURI(p)
    32  
    33  	testExportStore(t, dest, false)
    34  	testListFiles(t, "nodelocal://0/listing-test/basepath")
    35  }
    36  
    37  func TestLocalIOLimits(t *testing.T) {
    38  	defer leaktest.AfterTest(t)()
    39  
    40  	ctx := context.Background()
    41  	const allowed = "/allowed"
    42  	testSettings.ExternalIODir = allowed
    43  
    44  	clientFactory := blobs.TestBlobServiceClient(testSettings.ExternalIODir)
    45  
    46  	baseDir, err := ExternalStorageFromURI(
    47  		ctx, "nodelocal://0/", base.ExternalIODirConfig{}, testSettings, clientFactory)
    48  	if err != nil {
    49  		t.Fatal(err)
    50  	}
    51  
    52  	for dest, expected := range map[string]string{allowed: "", "/../../blah": "not allowed"} {
    53  		u := fmt.Sprintf("nodelocal://0%s", dest)
    54  		e, err := ExternalStorageFromURI(
    55  			ctx, u, base.ExternalIODirConfig{}, testSettings, clientFactory)
    56  		if err != nil {
    57  			t.Fatal(err)
    58  		}
    59  
    60  		if _, err = e.ListFiles(ctx, ""); !testutils.IsError(err, expected) {
    61  			t.Fatal(err)
    62  		}
    63  		if _, err = baseDir.ListFiles(ctx, dest); !testutils.IsError(err, expected) {
    64  			t.Fatal(err)
    65  		}
    66  	}
    67  
    68  	for host, expectErr := range map[string]bool{"": false, "1": false, "0": false, "blah": true} {
    69  		u := fmt.Sprintf("nodelocal://0%s/path/to/file", host)
    70  
    71  		var expected string
    72  		if expectErr {
    73  			expected = "host component of nodelocal URI must be a node ID"
    74  		}
    75  		if _, err := ExternalStorageConfFromURI(u); !testutils.IsError(err, expected) {
    76  			t.Fatalf("%q: expected error %q, got %v", u, expected, err)
    77  		}
    78  	}
    79  }