mirror of
https://github.com/ceph/ceph-csi.git
synced 2025-06-14 18:53:35 +00:00
vendor files
This commit is contained in:
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