mirror of
https://github.com/ceph/ceph-csi.git
synced 2025-06-14 10:53:34 +00:00
vendor files
This commit is contained in:
70
vendor/k8s.io/utils/temp/dir.go
generated
vendored
Normal file
70
vendor/k8s.io/utils/temp/dir.go
generated
vendored
Normal file
@ -0,0 +1,70 @@
|
||||
/*
|
||||
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 temp
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// Directory is an interface to a temporary directory, in which you can
|
||||
// create new files.
|
||||
type Directory interface {
|
||||
// NewFile creates a new file in that directory. Calling NewFile
|
||||
// with the same filename twice will result in an error.
|
||||
NewFile(name string) (io.WriteCloser, error)
|
||||
// Delete removes the directory and its content.
|
||||
Delete() error
|
||||
}
|
||||
|
||||
// TempDir is wrapping an temporary directory on disk.
|
||||
type TempDir struct {
|
||||
// Name is the name (full path) of the created directory.
|
||||
Name string
|
||||
}
|
||||
|
||||
var _ Directory = &TempDir{}
|
||||
|
||||
// CreateTempDir returns a new Directory wrapping a temporary directory
|
||||
// on disk.
|
||||
func CreateTempDir(prefix string) (*TempDir, error) {
|
||||
name, err := ioutil.TempDir("", fmt.Sprintf("%s-", prefix))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &TempDir{
|
||||
Name: name,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// NewFile creates a new file in the specified directory.
|
||||
func (d *TempDir) NewFile(name string) (io.WriteCloser, error) {
|
||||
return os.OpenFile(
|
||||
filepath.Join(d.Name, name),
|
||||
os.O_WRONLY|os.O_CREATE|os.O_TRUNC|os.O_EXCL,
|
||||
0700,
|
||||
)
|
||||
}
|
||||
|
||||
// Delete the underlying directory, and all of its content.
|
||||
func (d *TempDir) Delete() error {
|
||||
return os.RemoveAll(d.Name)
|
||||
}
|
90
vendor/k8s.io/utils/temp/dir_test.go
generated
vendored
Normal file
90
vendor/k8s.io/utils/temp/dir_test.go
generated
vendored
Normal file
@ -0,0 +1,90 @@
|
||||
/*
|
||||
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 temp
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestTempDir(t *testing.T) {
|
||||
dir, err := CreateTempDir("prefix")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Delete the directory no matter what.
|
||||
defer dir.Delete()
|
||||
|
||||
// Make sure we have created the dir, with the proper name
|
||||
_, err = os.Stat(dir.Name)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.HasPrefix(filepath.Base(dir.Name), "prefix") {
|
||||
t.Fatalf(`Directory doesn't start with "prefix": %q`,
|
||||
dir.Name)
|
||||
}
|
||||
|
||||
// Verify that the directory is empty
|
||||
entries, err := ioutil.ReadDir(dir.Name)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(entries) != 0 {
|
||||
t.Fatalf("Directory should be empty, has %d elements",
|
||||
len(entries))
|
||||
}
|
||||
|
||||
// Create a couple of files
|
||||
_, err = dir.NewFile("ONE")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = dir.NewFile("TWO")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// We can't create the same file twice
|
||||
_, err = dir.NewFile("TWO")
|
||||
if err == nil {
|
||||
t.Fatal("NewFile should fail to create the same file twice")
|
||||
}
|
||||
|
||||
// We have created only two files
|
||||
entries, err = ioutil.ReadDir(dir.Name)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(entries) != 2 {
|
||||
t.Fatalf("ReadDir should have two elements, has %d elements",
|
||||
len(entries))
|
||||
}
|
||||
|
||||
// Verify that deletion works
|
||||
err = dir.Delete()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = os.Stat(dir.Name)
|
||||
if err == nil {
|
||||
t.Fatal("Directory should be gone, still present.")
|
||||
}
|
||||
}
|
19
vendor/k8s.io/utils/temp/doc.go
generated
vendored
Normal file
19
vendor/k8s.io/utils/temp/doc.go
generated
vendored
Normal file
@ -0,0 +1,19 @@
|
||||
/*
|
||||
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 temp provides an interface to handle temporary files and
|
||||
// directories.
|
||||
package temp // import "k8s.io/utils/temp"
|
66
vendor/k8s.io/utils/temp/temptest/dir.go
generated
vendored
Normal file
66
vendor/k8s.io/utils/temp/temptest/dir.go
generated
vendored
Normal file
@ -0,0 +1,66 @@
|
||||
/*
|
||||
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 temptest
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"k8s.io/utils/temp"
|
||||
)
|
||||
|
||||
// FakeDir implements a Directory that is not backed on the
|
||||
// filesystem. This is useful for testing since the created "files" are
|
||||
// simple bytes.Buffer that can be inspected.
|
||||
type FakeDir struct {
|
||||
Files map[string]*FakeFile
|
||||
Deleted bool
|
||||
}
|
||||
|
||||
var _ temp.Directory = &FakeDir{}
|
||||
|
||||
// NewFile returns a new FakeFile if the filename doesn't exist already.
|
||||
// This function will fail if the directory has already been deleted.
|
||||
func (d *FakeDir) NewFile(name string) (io.WriteCloser, error) {
|
||||
if d.Deleted {
|
||||
return nil, errors.New("can't create file in deleted FakeDir")
|
||||
}
|
||||
if d.Files == nil {
|
||||
d.Files = map[string]*FakeFile{}
|
||||
}
|
||||
f := d.Files[name]
|
||||
if f != nil {
|
||||
return nil, fmt.Errorf(
|
||||
"FakeDir already has file named %q",
|
||||
name,
|
||||
)
|
||||
}
|
||||
f = &FakeFile{}
|
||||
d.Files[name] = f
|
||||
return f, nil
|
||||
}
|
||||
|
||||
// Delete doesn't remove anything, but records that the directory has
|
||||
// been deleted.
|
||||
func (d *FakeDir) Delete() error {
|
||||
if d.Deleted {
|
||||
return errors.New("failed to re-delete FakeDir")
|
||||
}
|
||||
d.Deleted = true
|
||||
return nil
|
||||
}
|
67
vendor/k8s.io/utils/temp/temptest/dir_test.go
generated
vendored
Normal file
67
vendor/k8s.io/utils/temp/temptest/dir_test.go
generated
vendored
Normal file
@ -0,0 +1,67 @@
|
||||
/*
|
||||
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 temptest
|
||||
|
||||
import (
|
||||
"io"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestFakeDir(t *testing.T) {
|
||||
d := &FakeDir{}
|
||||
|
||||
f, err := d.NewFile("ONE")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
n, err := io.WriteString(f, "Bonjour!")
|
||||
if n != 8 || err != nil {
|
||||
t.Fatalf(
|
||||
`WriteString(f, "Bonjour!") = (%v, %v), expected (%v, %v)`,
|
||||
n, err,
|
||||
0, nil,
|
||||
)
|
||||
}
|
||||
if got := d.Files["ONE"].Buffer.String(); got != "Bonjour!" {
|
||||
t.Fatalf(`file content is %q, expected "Bonjour!"`, got)
|
||||
}
|
||||
|
||||
f, err = d.NewFile("ONE")
|
||||
if err == nil {
|
||||
t.Fatal("Same file could be created twice.")
|
||||
}
|
||||
|
||||
err = d.Delete()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = d.Delete()
|
||||
if err == nil {
|
||||
t.Fatal("FakeDir could be deleted twice.")
|
||||
}
|
||||
|
||||
f, err = d.NewFile("TWO")
|
||||
if err == nil {
|
||||
t.Fatal("NewFile could be created in deleted dir")
|
||||
}
|
||||
|
||||
if !d.Deleted {
|
||||
t.Fatal("FakeDir should be deleted.")
|
||||
}
|
||||
}
|
19
vendor/k8s.io/utils/temp/temptest/doc.go
generated
vendored
Normal file
19
vendor/k8s.io/utils/temp/temptest/doc.go
generated
vendored
Normal file
@ -0,0 +1,19 @@
|
||||
/*
|
||||
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 temptest provides utilities for testing temp
|
||||
// files/directories testing.
|
||||
package temptest
|
57
vendor/k8s.io/utils/temp/temptest/example_test.go
generated
vendored
Normal file
57
vendor/k8s.io/utils/temp/temptest/example_test.go
generated
vendored
Normal file
@ -0,0 +1,57 @@
|
||||
/*
|
||||
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 temptest
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"k8s.io/utils/temp"
|
||||
)
|
||||
|
||||
func TestedCode(dir temp.Directory) error {
|
||||
f, err := dir.NewFile("filename")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = io.WriteString(f, "Bonjour!")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return dir.Delete()
|
||||
}
|
||||
|
||||
func Example() {
|
||||
dir := FakeDir{}
|
||||
|
||||
err := TestedCode(&dir)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
if dir.Deleted == false {
|
||||
panic(errors.New("Directory should have been deleted."))
|
||||
}
|
||||
|
||||
if dir.Files["filename"] == nil {
|
||||
panic(errors.New(`"filename" should have been created.`))
|
||||
}
|
||||
|
||||
fmt.Println(dir.Files["filename"].Buffer.String())
|
||||
// Output: Bonjour!
|
||||
}
|
52
vendor/k8s.io/utils/temp/temptest/file.go
generated
vendored
Normal file
52
vendor/k8s.io/utils/temp/temptest/file.go
generated
vendored
Normal file
@ -0,0 +1,52 @@
|
||||
/*
|
||||
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 temptest
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"io"
|
||||
)
|
||||
|
||||
// FakeFile is an implementation of a WriteCloser, that records what has
|
||||
// been written in the file (in a bytes.Buffer) and if the file has been
|
||||
// closed.
|
||||
type FakeFile struct {
|
||||
Buffer bytes.Buffer
|
||||
Closed bool
|
||||
}
|
||||
|
||||
var _ io.WriteCloser = &FakeFile{}
|
||||
|
||||
// Write appends the contents of p to the Buffer. If the file has
|
||||
// already been closed, an error is returned.
|
||||
func (f *FakeFile) Write(p []byte) (n int, err error) {
|
||||
if f.Closed {
|
||||
return 0, errors.New("can't write to closed FakeFile")
|
||||
}
|
||||
return f.Buffer.Write(p)
|
||||
}
|
||||
|
||||
// Close records that the file has been closed. If the file has already
|
||||
// been closed, an error is returned.
|
||||
func (f *FakeFile) Close() error {
|
||||
if f.Closed {
|
||||
return errors.New("FakeFile was closed multiple times")
|
||||
}
|
||||
f.Closed = true
|
||||
return nil
|
||||
}
|
56
vendor/k8s.io/utils/temp/temptest/file_test.go
generated
vendored
Normal file
56
vendor/k8s.io/utils/temp/temptest/file_test.go
generated
vendored
Normal file
@ -0,0 +1,56 @@
|
||||
/*
|
||||
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 temptest
|
||||
|
||||
import (
|
||||
"io"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestFakeFile(t *testing.T) {
|
||||
f := &FakeFile{}
|
||||
|
||||
n, err := io.WriteString(f, "Bonjour!")
|
||||
if n != 8 || err != nil {
|
||||
t.Fatalf(
|
||||
`WriteString(f, "Bonjour!") = (%v, %v), expected (%v, %v)`,
|
||||
n, err,
|
||||
8, nil,
|
||||
)
|
||||
}
|
||||
|
||||
err = f.Close()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// File can't be closed twice.
|
||||
err = f.Close()
|
||||
if err == nil {
|
||||
t.Fatal("FakeFile could be closed twice")
|
||||
}
|
||||
|
||||
// File is not writable after close.
|
||||
n, err = io.WriteString(f, "Bonjour!")
|
||||
if n != 0 || err == nil {
|
||||
t.Fatalf(
|
||||
`WriteString(f, "Bonjour!") = (%v, %v), expected (%v, %v)`,
|
||||
n, err,
|
||||
0, "non-nil",
|
||||
)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user