Merge pull request #99829 from palnabarun/migrate-to-go-embed

Replace go-bindata with //go:embed
This commit is contained in:
Kubernetes Prow Robot
2021-06-30 10:37:03 -07:00
committed by GitHub
71 changed files with 289 additions and 28442 deletions

View File

@@ -1,5 +1,5 @@
/*
Copyright 2015 The Kubernetes Authors.
Copyright 2021 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.
@@ -14,6 +14,20 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
package generated
package testdata
// No code is needed here. This is a stub for compilation purposes.
import (
"embed"
"k8s.io/kubernetes/test/e2e/framework/testfiles"
)
//go:embed conformance.yaml ineligible_endpoints.yaml
var conformanceTestdataFS embed.FS
func GetConformanceTestdataFS() testfiles.EmbeddedFileSource {
return testfiles.EmbeddedFileSource{
EmbeddedFS: conformanceTestdataFS,
Root: "test/conformance/testdata",
}
}

View File

@@ -33,10 +33,12 @@ import (
// "github.com/onsi/ginkgo"
"k8s.io/component-base/version"
conformancetestdata "k8s.io/kubernetes/test/conformance/testdata"
"k8s.io/kubernetes/test/e2e/framework"
"k8s.io/kubernetes/test/e2e/framework/config"
"k8s.io/kubernetes/test/e2e/framework/testfiles"
"k8s.io/kubernetes/test/e2e/generated"
e2etestingmanifests "k8s.io/kubernetes/test/e2e/testing-manifests"
testfixtures "k8s.io/kubernetes/test/fixtures"
"k8s.io/kubernetes/test/utils/image"
// test sources
@@ -85,11 +87,11 @@ func TestMain(m *testing.M) {
os.Exit(0)
}
// Enable bindata file lookup as fallback.
testfiles.AddFileSource(testfiles.BindataFileSource{
Asset: generated.Asset,
AssetNames: generated.AssetNames,
})
// Enable embedded FS file lookup as fallback
testfiles.AddFileSource(e2etestingmanifests.GetE2ETestingManifestsFS())
testfiles.AddFileSource(testfixtures.GetTestFixturesFS())
testfiles.AddFileSource(conformancetestdata.GetConformanceTestdataFS())
if framework.TestContext.ListConformanceTests {
var tests []struct {
Testname string `yaml:"testname"`

View File

@@ -0,0 +1 @@
Hello World

View File

@@ -25,13 +25,14 @@ limitations under the License.
package testfiles
import (
"embed"
"errors"
"fmt"
"io/fs"
"io/ioutil"
"os"
"path"
"path/filepath"
"sort"
"strings"
)
@@ -147,32 +148,47 @@ func (r RootFileSource) DescribeFiles() string {
return description
}
// BindataFileSource handles files stored in a package generated with bindata.
type BindataFileSource struct {
Asset func(string) ([]byte, error)
AssetNames func() []string
// EmbeddedFileSource handles files stored in a package generated with bindata.
type EmbeddedFileSource struct {
EmbeddedFS embed.FS
Root string
fileList []string
}
// ReadTestFile looks for an asset with the given path.
func (b BindataFileSource) ReadTestFile(filePath string) ([]byte, error) {
fileBytes, err := b.Asset(filePath)
// ReadTestFile looks for an embedded file with the given path.
func (e EmbeddedFileSource) ReadTestFile(filepath string) ([]byte, error) {
relativePath := strings.TrimPrefix(filepath, fmt.Sprintf("%s/", e.Root))
b, err := e.EmbeddedFS.ReadFile(relativePath)
if err != nil {
// It would be nice to have a better way to detect
// "not found" errors :-/
if strings.HasSuffix(err.Error(), "not found") {
if errors.Is(err, fs.ErrNotExist) {
return nil, nil
}
return nil, err
}
return fileBytes, nil
return b, nil
}
// DescribeFiles explains about gobindata and then lists all available files.
func (b BindataFileSource) DescribeFiles() string {
// DescribeFiles explains that it is looking inside an embedded filesystem
func (e EmbeddedFileSource) DescribeFiles() string {
var lines []string
lines = append(lines, "The following files are built into the test executable via gobindata. For questions on maintaining gobindata, contact the sig-testing group.")
assets := b.AssetNames()
sort.Strings(assets)
lines = append(lines, assets...)
description := strings.Join(lines, "\n ")
return description
lines = append(lines, "The following files are embedded into the test executable:")
if len(e.fileList) == 0 {
e.populateFileList()
}
lines = append(lines, e.fileList...)
return strings.Join(lines, "\n\t")
}
func (e *EmbeddedFileSource) populateFileList() {
fs.WalkDir(e.EmbeddedFS, ".", func(path string, d fs.DirEntry, err error) error {
if !d.IsDir() {
e.fileList = append(e.fileList, filepath.Join(e.Root, path))
}
return nil
})
}

View File

@@ -0,0 +1,63 @@
/*
Copyright 2021 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 testfiles
import (
"embed"
"testing"
"github.com/stretchr/testify/assert"
)
var (
fooContents = `Hello World
`
fooPath = "testdata/a/foo.txt"
notExistsPath = "testdata/b"
expectedDescription = `The following files are embedded into the test executable:
testdata/a/foo.txt`
)
//go:embed testdata/a
var testFS embed.FS
func getTestEmbeddedSource() *EmbeddedFileSource {
return &EmbeddedFileSource{
EmbeddedFS: testFS,
}
}
func TestEmbeddedFileSource(t *testing.T) {
s := getTestEmbeddedSource()
// read a file which exists and compare the contents
b, err := s.ReadTestFile(fooPath)
assert.NoError(t, err)
assert.Equal(t, fooContents, string(b))
// read a non-existent file and ensure that the returned value is empty and error is nil
// Note: this is done so that the next file source can be tried by the caller
b, err = s.ReadTestFile(notExistsPath)
assert.NoError(t, err)
assert.Empty(t, b)
// describing the test filesystem should list down all files
assert.Equal(t, expectedDescription, s.DescribeFiles())
}

View File

@@ -1,34 +0,0 @@
/*
Copyright 2016 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 generated
import "k8s.io/klog/v2"
/*
ReadOrDie reads a file from gobindata.
Relies heavily on the successful generation of build artifacts as per the go:generate directives above.
*/
func ReadOrDie(filePath string) []byte {
fileBytes, err := Asset(filePath)
if err != nil {
gobindataMsg := "An error occurred, possibly gobindata doesn't know about the file you're opening. For questions on maintaining gobindata, contact the sig-testing group."
klog.Infof("Available gobindata files: %v ", AssetNames())
klog.Fatalf("Failed opening %v , with error %v. %v.", filePath, err, gobindataMsg)
}
return fileBytes
}

View File

@@ -0,0 +1,22 @@
# test/e2e/testing-manifests
## Embedded Test Data
In case one needs to use any test fixture inside your tests and those are defined inside this directory, they need to be added to the `//go:embed` directive in `embed.go`.
For example, if one wants to include this Readme as a test fixture (potential bad idea in reality!),
```
// embed.go
...
//go:embed some other files README.md
...
```
This fixture can be accessed in the e2e tests using `test/e2e/framework/testfiles.Read` like
`testfiles.Read("test/e2e/testing-manifests/README.md)`.
This is needed since [migrating to //go:embed from go-bindata][1].
[1]: https://github.com/kubernetes/kubernetes/pull/99829

View File

@@ -0,0 +1,33 @@
/*
Copyright 2021 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 testing_manifests
import (
"embed"
e2etestfiles "k8s.io/kubernetes/test/e2e/framework/testfiles"
)
//go:embed flexvolume guestbook kubectl sample-device-plugin.yaml scheduling/nvidia-driver-installer.yaml statefulset storage-csi
var e2eTestingManifestsFS embed.FS
func GetE2ETestingManifestsFS() e2etestfiles.EmbeddedFileSource {
return e2etestfiles.EmbeddedFileSource{
EmbeddedFS: e2eTestingManifestsFS,
Root: "test/e2e/testing-manifests",
}
}

View File

@@ -27,6 +27,7 @@ import (
"flag"
"fmt"
"io/ioutil"
"math/rand"
"os"
"os/exec"
@@ -44,8 +45,8 @@ import (
"k8s.io/kubernetes/test/e2e/framework"
e2econfig "k8s.io/kubernetes/test/e2e/framework/config"
e2etestfiles "k8s.io/kubernetes/test/e2e/framework/testfiles"
"k8s.io/kubernetes/test/e2e/generated"
"k8s.io/kubernetes/test/e2e_node/services"
e2enodetestingmanifests "k8s.io/kubernetes/test/e2e_node/testing-manifests"
system "k8s.io/system-validators/validators"
"github.com/onsi/ginkgo"
@@ -90,12 +91,8 @@ func registerNodeFlags(flags *flag.FlagSet) {
}
func init() {
// Enable bindata file lookup as fallback.
e2etestfiles.AddFileSource(e2etestfiles.BindataFileSource{
Asset: generated.Asset,
AssetNames: generated.AssetNames,
})
// Enable embedded FS file lookup as fallback
e2etestfiles.AddFileSource(e2enodetestingmanifests.GetE2ENodeTestingManifestsFS())
}
func TestMain(m *testing.M) {

View File

@@ -0,0 +1,33 @@
/*
Copyright 2021 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 testing_manifests
import (
"embed"
e2etestfiles "k8s.io/kubernetes/test/e2e/framework/testfiles"
)
//go:embed *.yaml
var e2eNodeTestingManifestsFS embed.FS
func GetE2ENodeTestingManifestsFS() e2etestfiles.EmbeddedFileSource {
return e2etestfiles.EmbeddedFileSource{
EmbeddedFS: e2eNodeTestingManifestsFS,
Root: "test/e2e_node/testing-manifests",
}
}

22
test/fixtures/README.md vendored Normal file
View File

@@ -0,0 +1,22 @@
# test/fixtures
## Embedded Test Data
In case one needs to use any test fixture inside your tests and those are defined inside this directory, they need to be added to the `//go:embed` directive in `embed.go`.
For example, if one wants to include this Readme as a test fixture (potential bad idea in reality!),
```
// embed.go
...
//go:embed some other files README.md
...
```
This fixture can be accessed in the e2e tests using `test/e2e/framework/testfiles.Read` like
`testfiles.Read("test/fixtures/README.md)`.
This is needed since [migrating to //go:embed from go-bindata][1].
[1]: https://github.com/kubernetes/kubernetes/pull/99829

33
test/fixtures/embed.go vendored Normal file
View File

@@ -0,0 +1,33 @@
/*
Copyright 2021 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 fixtures
import (
"embed"
"k8s.io/kubernetes/test/e2e/framework/testfiles"
)
//go:embed doc-yaml/user-guide/liveness doc-yaml/user-guide/secrets doc-yaml/user-guide/downward-api doc-yaml/user-guide/update-demo
var testFixturesFS embed.FS
func GetTestFixturesFS() testfiles.EmbeddedFileSource {
return testfiles.EmbeddedFileSource{
EmbeddedFS: testFixturesFS,
Root: "test/fixtures",
}
}