From 8368d2e3baf17bc556dc283e517600c697054bbc Mon Sep 17 00:00:00 2001 From: ymqytw Date: Tue, 30 Jan 2018 09:38:23 -0800 Subject: [PATCH] mock fs and file for testing --- pkg/kinflate/util/fs/fake/file.go | 53 +++++++++++++++++++++++++++++++ pkg/kinflate/util/fs/fake/fs.go | 49 ++++++++++++++++++++++++++++ pkg/kinflate/util/fs/file.go | 38 ++++++++++++++++++++++ pkg/kinflate/util/fs/fs.go | 35 ++++++++++++++++++++ pkg/kinflate/util/fs/interface.go | 35 ++++++++++++++++++++ 5 files changed, 210 insertions(+) create mode 100644 pkg/kinflate/util/fs/fake/file.go create mode 100644 pkg/kinflate/util/fs/fake/fs.go create mode 100644 pkg/kinflate/util/fs/file.go create mode 100644 pkg/kinflate/util/fs/fs.go create mode 100644 pkg/kinflate/util/fs/interface.go diff --git a/pkg/kinflate/util/fs/fake/file.go b/pkg/kinflate/util/fs/fake/file.go new file mode 100644 index 000000000..53b8cf203 --- /dev/null +++ b/pkg/kinflate/util/fs/fake/file.go @@ -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 +} diff --git a/pkg/kinflate/util/fs/fake/fs.go b/pkg/kinflate/util/fs/fake/fs.go new file mode 100644 index 000000000..1500e2adc --- /dev/null +++ b/pkg/kinflate/util/fs/fake/fs.go @@ -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) } diff --git a/pkg/kinflate/util/fs/file.go b/pkg/kinflate/util/fs/file.go new file mode 100644 index 000000000..1a27787be --- /dev/null +++ b/pkg/kinflate/util/fs/file.go @@ -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() } diff --git a/pkg/kinflate/util/fs/fs.go b/pkg/kinflate/util/fs/fs.go new file mode 100644 index 000000000..6435f8759 --- /dev/null +++ b/pkg/kinflate/util/fs/fs.go @@ -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) } diff --git a/pkg/kinflate/util/fs/interface.go b/pkg/kinflate/util/fs/interface.go new file mode 100644 index 000000000..e130b014b --- /dev/null +++ b/pkg/kinflate/util/fs/interface.go @@ -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) +}