gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/test/runtimes/proctor/lib/lib_test.go (about)

     1  // Copyright 2019 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package lib
    16  
    17  import (
    18  	"io/ioutil"
    19  	"os"
    20  	"path/filepath"
    21  	"regexp"
    22  	"slices"
    23  	"strings"
    24  	"testing"
    25  
    26  	"gvisor.dev/gvisor/pkg/test/testutil"
    27  )
    28  
    29  func touch(t *testing.T, name string) {
    30  	t.Helper()
    31  	f, err := os.Create(name)
    32  	if err != nil {
    33  		t.Fatalf("error creating file %q: %v", name, err)
    34  	}
    35  	if err := f.Close(); err != nil {
    36  		t.Fatalf("error closing file %q: %v", name, err)
    37  	}
    38  }
    39  
    40  func TestSearchEmptyDir(t *testing.T) {
    41  	td, err := ioutil.TempDir(testutil.TmpDir(), "searchtest")
    42  	if err != nil {
    43  		t.Fatalf("error creating searchtest: %v", err)
    44  	}
    45  	defer os.RemoveAll(td)
    46  
    47  	var want []string
    48  
    49  	testFilter := regexp.MustCompile(`^test-[^-].+\.tc$`)
    50  	got, err := Search(td, testFilter)
    51  	if err != nil {
    52  		t.Errorf("search error: %v", err)
    53  	}
    54  
    55  	if !slices.Equal(got, want) {
    56  		t.Errorf("Found %#v; want %#v", got, want)
    57  	}
    58  }
    59  
    60  func TestSearch(t *testing.T) {
    61  	td, err := ioutil.TempDir(testutil.TmpDir(), "searchtest")
    62  	if err != nil {
    63  		t.Fatalf("error creating searchtest: %v", err)
    64  	}
    65  	defer os.RemoveAll(td)
    66  
    67  	// Creating various files similar to the test filter regex.
    68  	files := []string{
    69  		"emp/",
    70  		"tee/",
    71  		"test-foo.tc",
    72  		"test-foo.tc",
    73  		"test-bar.tc",
    74  		"test-sam.tc",
    75  		"Test-que.tc",
    76  		"test-brett",
    77  		"test--abc.tc",
    78  		"test---xyz.tc",
    79  		"test-bool.TC",
    80  		"--test-gvs.tc",
    81  		" test-pew.tc",
    82  		"dir/test_baz.tc",
    83  		"dir/testsnap.tc",
    84  		"dir/test-luk.tc",
    85  		"dir/nest/test-ok.tc",
    86  		"dir/dip/diz/goog/test-pack.tc",
    87  		"dir/dip/diz/wobble/thud/test-cas.e",
    88  		"dir/dip/diz/wobble/thud/test-cas.tc",
    89  	}
    90  	want := []string{
    91  		"dir/dip/diz/goog/test-pack.tc",
    92  		"dir/dip/diz/wobble/thud/test-cas.tc",
    93  		"dir/nest/test-ok.tc",
    94  		"dir/test-luk.tc",
    95  		"test-bar.tc",
    96  		"test-foo.tc",
    97  		"test-sam.tc",
    98  	}
    99  
   100  	for _, item := range files {
   101  		if strings.HasSuffix(item, "/") {
   102  			// This item is a directory, create it.
   103  			if err := os.MkdirAll(filepath.Join(td, item), 0755); err != nil {
   104  				t.Fatalf("error making directory: %v", err)
   105  			}
   106  		} else {
   107  			// This item is a file, create the directory and touch file.
   108  			// Create directory in which file should be created
   109  			fullDirPath := filepath.Join(td, filepath.Dir(item))
   110  			if err := os.MkdirAll(fullDirPath, 0755); err != nil {
   111  				t.Fatalf("error making directory: %v", err)
   112  			}
   113  			// Create file with full path to file.
   114  			touch(t, filepath.Join(td, item))
   115  		}
   116  	}
   117  
   118  	testFilter := regexp.MustCompile(`^test-[^-].+\.tc$`)
   119  	got, err := Search(td, testFilter)
   120  	if err != nil {
   121  		t.Errorf("search error: %v", err)
   122  	}
   123  
   124  	if !slices.Equal(got, want) {
   125  		t.Errorf("Found %#v; want %#v", got, want)
   126  	}
   127  }