Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add UT for util/pkg/vfs/memfs.go #8730

Merged
merged 1 commit into from
Mar 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions util/pkg/vfs/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ go_test(
name = "go_default_test",
srcs = [
"fs_test.go",
"memfs_test.go",
"s3context_test.go",
"s3fs_test.go",
],
Expand Down
165 changes: 165 additions & 0 deletions util/pkg/vfs/memfs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
/*
Copyright 2020 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package vfs

import (
"bytes"
"os"
"reflect"
"sort"
"testing"
)

func TestMemFsCreateFile(t *testing.T) {
tests := []struct {
path string
data []byte
}{
{
path: "/root/subdir/test1.data",
data: []byte("test data\nline 1\r\nline 2"),
},
}
for _, test := range tests {
memfspath := NewMemFSPath(NewMemFSContext(), test.path)
// Create file
err := memfspath.CreateFile(bytes.NewReader(test.data), nil)
if err != nil {
t.Errorf("Failed writing path %s, error: %v", test.path, err)
continue
}

// Create file again should result in error
err = memfspath.CreateFile(bytes.NewReader([]byte("data")), nil)
if err != os.ErrExist {
t.Errorf("Expected to get os.ErrExist, got: %v", err)
}

// Check file content
data, err := memfspath.ReadFile()
if err != nil {
t.Errorf("Failed reading path %s, error: %v", test.path, err)
continue
}
if !bytes.Equal(data, test.data) {
t.Errorf("Expected path content %v, got %v", test.data, data)
}
}
}

func TestMemFsReadDir(t *testing.T) {
tests := []struct {
path string
subpaths []string
expected []string
}{
{
path: "/root/",
subpaths: []string{
"subdir/",
"subdir2/",
"subdir2/test.data",
},
expected: []string{
"/root/subdir/",
"/root/subdir2/",
},
},
}
for _, test := range tests {
context := NewMemFSContext()
memfspath := NewMemFSPath(context, test.path)

// Create sub-paths
for _, subpath := range test.subpaths {
memfspath.Join(subpath)
}

// Read dir
paths, err := memfspath.ReadDir()
if err != nil {
t.Errorf("Failed reading dir %s, error: %v", test.path, err)
continue
}

// There is no consistent alphabetical order in the result, so we sort it
sort.Slice(paths, func(i, j int) bool {
return paths[i].Path() < paths[j].Path()
})
// Expected sub-paths
count := len(test.expected)
expected := make([]Path, count)
for i := 0; i < count; i++ {
expected[i] = NewMemFSPath(context, test.expected[i])
}
if !reflect.DeepEqual(paths, expected) {
t.Errorf("Expected sub-paths %v, got %v", expected, paths)
}
}
}

func TestMemFsReadTree(t *testing.T) {
tests := []struct {
path string
subpaths []string
expected []string
}{
{
path: "/root/dir/",
subpaths: []string{
"subdir/",
"subdir/test1.data",
"subdir2/",
"subdir2/test2.data",
},
expected: []string{
"/root/dir/subdir/test1.data",
"/root/dir/subdir2/test2.data",
},
},
}
for _, test := range tests {
context := NewMemFSContext()
memfspath := NewMemFSPath(context, test.path)

// Create sub-paths
for _, subpath := range test.subpaths {
memfspath.Join(subpath)
}

// Read dir tree
paths, err := memfspath.ReadTree()
if err != nil {
t.Errorf("Failed reading dir tree %s, error: %v", test.path, err)
continue
}

// There is no consistent alphabetical order in the result, so we sort it
sort.Slice(paths, func(i, j int) bool {
return paths[i].Path() < paths[j].Path()
})
// Expected sub-paths
count := len(test.expected)
expected := make([]Path, count)
for i := 0; i < count; i++ {
expected[i] = NewMemFSPath(context, test.expected[i])
}
if !reflect.DeepEqual(paths, expected) {
t.Errorf("Expected tree paths %v, got %v", expected, paths)
}
}
}