mock fs and file for testing

This commit is contained in:
ymqytw 2018-01-30 09:38:23 -08:00
parent 62ba5fb685
commit 8368d2e3ba
5 changed files with 210 additions and 0 deletions

View File

@ -0,0 +1,53 @@
/*
Copyright 2018 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 fs
import (
"os"
"k8s.io/kubectl/pkg/kinflate/util/fs"
)
var _ fs.File = &FakeFile{}
// FakeFile implements FileSystem using a fake in-memory filesystem.
type FakeFile struct {
content []byte
open bool
}
// Close closes a file.
func (f *FakeFile) Close() error {
f.open = false
return nil
}
// Read reads a file's content.
func (f *FakeFile) Read(p []byte) (n int, err error) {
return len(p), nil
}
// Write writes bytes to a file
func (f *FakeFile) Write(p []byte) (n int, err error) {
f.content = p
return len(p), nil
}
// Stat returns an interface which has all the information regarding the file.
func (f *FakeFile) Stat() (os.FileInfo, error) {
return nil, nil
}

View File

@ -0,0 +1,49 @@
/*
Copyright 2018 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 fs
import (
"os"
"k8s.io/kubectl/pkg/kinflate/util/fs"
)
var _ fs.FileSystem = &FakeFS{}
// FakeFS implements FileSystem using a fake in-memory filesystem.
type FakeFS struct{ m map[string]*FakeFile }
// Create creates a file given the filename.
func (fs *FakeFS) Create(name string) (fs.File, error) {
if fs.m == nil {
fs.m = map[string]*FakeFile{}
}
fs.m[name] = &FakeFile{}
return fs.m[name], nil
}
// Open opens a file given the filename.
func (fs *FakeFS) Open(name string) (fs.File, error) {
if fs.m == nil {
fs.m = map[string]*FakeFile{}
}
fs.m[name] = &FakeFile{open: true}
return fs.m[name], nil
}
// Stat return an interface which has all the information regarding the file.
func (fs *FakeFS) Stat(name string) (os.FileInfo, error) { return os.Stat(name) }

View File

@ -0,0 +1,38 @@
/*
Copyright 2018 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 fs
import (
"os"
)
var _ File = &OSFile{}
// OSFile implements File using the local filesystem.
type OSFile struct{ file *os.File }
// Close closes a file.
func (f *OSFile) Close() error { return f.file.Close() }
// Read reads a file's content.
func (f *OSFile) Read(p []byte) (n int, err error) { return f.file.Read(p) }
// Write writes bytes to a file
func (f *OSFile) Write(p []byte) (n int, err error) { return f.file.Write(p) }
// Stat returns an interface which has all the information regarding the file.
func (f *OSFile) Stat() (os.FileInfo, error) { return f.file.Stat() }

View File

@ -0,0 +1,35 @@
/*
Copyright 2018 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 fs
import (
"os"
)
var _ FileSystem = OSFS{}
// OSFS implements FileSystem using the local filesystem.
type OSFS struct{}
// Create creates a file given the filename.
func (OSFS) Create(name string) (File, error) { return os.Create(name) }
// Open opens a file given the filename.
func (OSFS) Open(name string) (File, error) { return os.Open(name) }
// Stat return an interface which has all the information regarding the file.
func (OSFS) Stat(name string) (os.FileInfo, error) { return os.Stat(name) }

View File

@ -0,0 +1,35 @@
/*
Copyright 2018 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 fs
import (
"io"
"os"
)
// FileSystem is the interface that groups the basic filesystem methods.
type FileSystem interface {
Create(name string) (File, error)
Open(name string) (File, error)
Stat(name string) (os.FileInfo, error)
}
// File is the interface that groups the basic file methods.
type File interface {
io.ReadWriteCloser
Stat() (os.FileInfo, error)
}