Add fake filesystem for test to avoid temporal files on the local filesystem
This commit is contained in:
parent
14b5259b0f
commit
18d6573981
15 changed files with 633 additions and 89 deletions
289
internal/file/bindata.go
Normal file
289
internal/file/bindata.go
Normal file
File diff suppressed because one or more lines are too long
127
internal/file/filesystem.go
Normal file
127
internal/file/filesystem.go
Normal file
|
|
@ -0,0 +1,127 @@
|
|||
package file
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/golang/glog"
|
||||
|
||||
"k8s.io/kubernetes/pkg/util/filesystem"
|
||||
)
|
||||
|
||||
// Filesystem is an interface that we can use to mock various filesystem operations
|
||||
type Filesystem interface {
|
||||
filesystem.Filesystem
|
||||
}
|
||||
|
||||
// NewLocalFS implements Filesystem using same-named functions from "os" and "io/ioutil".
|
||||
func NewLocalFS() (Filesystem, error) {
|
||||
fs := filesystem.DefaultFs{}
|
||||
|
||||
err := initialize(false, fs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return fs, nil
|
||||
}
|
||||
|
||||
// NewFakeFS creates an in-memory filesytem with all the required
|
||||
// paths used by the ingress controller.
|
||||
// This allows running test without polluting the local machine.
|
||||
func NewFakeFS() (Filesystem, error) {
|
||||
fs := filesystem.NewFakeFs()
|
||||
|
||||
err := initialize(true, fs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return fs, nil
|
||||
}
|
||||
|
||||
// initialize creates the required directory structure and when
|
||||
// runs as virtual filesystem it copies the local files to it
|
||||
func initialize(isVirtual bool, fs Filesystem) error {
|
||||
for _, directory := range directories {
|
||||
err := fs.MkdirAll(directory, 0655)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if !isVirtual {
|
||||
return nil
|
||||
}
|
||||
|
||||
for _, file := range files {
|
||||
f, err := fs.Create(file)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = f.Write([]byte(""))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = f.Close()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
err := fs.MkdirAll("/proc", 0655)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
glog.Info("Restoring generated (go-bindata) assets in virtual filesystem...")
|
||||
for _, assetName := range AssetNames() {
|
||||
err := restoreAsset("/", assetName, fs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// restoreAsset restores an asset under the given directory
|
||||
func restoreAsset(dir, name string, fs Filesystem) error {
|
||||
data, err := Asset(name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
info, err := AssetInfo(name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = fs.MkdirAll(_filePath(dir, filepath.Dir(name)), os.FileMode(0755))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
f, err := fs.Create(_filePath(dir, name))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = f.Write(data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = f.Close()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
//Missing info.Mode()
|
||||
|
||||
err = fs.Chtimes(_filePath(dir, name), info.ModTime(), info.ModTime())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
37
internal/file/filesystem_test.go
Normal file
37
internal/file/filesystem_test.go
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
/*
|
||||
Copyright 2017 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 file
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNewFakeFS(t *testing.T) {
|
||||
fs, err := NewFakeFS()
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error creating filesystem abstraction: %v", err)
|
||||
}
|
||||
|
||||
if fs == nil {
|
||||
t.Fatal("expected a filesystem but none returned")
|
||||
}
|
||||
|
||||
_, err = fs.Stat("/etc/nginx/nginx.conf")
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error reading default nginx.conf file: %v", err)
|
||||
}
|
||||
}
|
||||
26
internal/file/structure.go
Normal file
26
internal/file/structure.go
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
package file
|
||||
|
||||
const (
|
||||
// AuthDirectory default directory used to store files
|
||||
// to authenticate request
|
||||
AuthDirectory = "/etc/ingress-controller/auth"
|
||||
|
||||
// DefaultSSLDirectory defines the location where the SSL certificates will be generated
|
||||
// This directory contains all the SSL certificates that are specified in Ingress rules.
|
||||
// The name of each file is <namespace>-<secret name>.pem. The content is the concatenated
|
||||
// certificate and key.
|
||||
DefaultSSLDirectory = "/ingress-controller/ssl"
|
||||
)
|
||||
|
||||
var (
|
||||
directories = []string{
|
||||
"/etc/nginx/template",
|
||||
"/run",
|
||||
DefaultSSLDirectory,
|
||||
AuthDirectory,
|
||||
}
|
||||
|
||||
files = []string{
|
||||
"/run/nginx.pid",
|
||||
}
|
||||
)
|
||||
Loading…
Add table
Add a link
Reference in a new issue