Commit 83ff8f2d authored by Phillip Wittrock's avatar Phillip Wittrock

Print a newline after ginkgo tests so the test infra doesn't think that they fail

Fixes #45279
parent f84b61b4
......@@ -8,24 +8,6 @@ load(
"go_test",
)
go_test(
name = "go_default_test",
srcs = [
"openapi_cache_test.go",
"openapi_getter_test.go",
"openapi_test.go",
],
library = ":go_default_library",
tags = ["automanaged"],
deps = [
"//vendor/github.com/go-openapi/loads:go_default_library",
"//vendor/github.com/go-openapi/spec:go_default_library",
"//vendor/github.com/onsi/ginkgo:go_default_library",
"//vendor/github.com/onsi/gomega:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
......@@ -47,11 +29,24 @@ go_library(
go_test(
name = "go_default_xtest",
srcs = ["openapi_suite_test.go"],
size = "small",
srcs = [
"openapi_cache_test.go",
"openapi_getter_test.go",
"openapi_suite_test.go",
"openapi_test.go",
],
data = ["//api/openapi-spec:swagger-spec"],
tags = ["automanaged"],
deps = [
"//pkg/kubectl/cmd/util/openapi:go_default_library",
"//vendor/github.com/go-openapi/loads:go_default_library",
"//vendor/github.com/go-openapi/spec:go_default_library",
"//vendor/github.com/onsi/ginkgo:go_default_library",
"//vendor/github.com/onsi/ginkgo/config:go_default_library",
"//vendor/github.com/onsi/ginkgo/types:go_default_library",
"//vendor/github.com/onsi/gomega:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
],
)
......@@ -67,3 +62,8 @@ filegroup(
srcs = [":package-srcs"],
tags = ["automanaged"],
)
filegroup(
name = "testdata",
srcs = glob(["testdata/*"]),
)
......@@ -133,8 +133,8 @@ type Type struct {
Extensions spec.Extensions
}
// newOpenAPIData parses the resource definitions in openapi data by groupversionkind and name
func newOpenAPIData(s *spec.Swagger) (*Resources, error) {
// NewOpenAPIData parses the resource definitions in openapi data by groupversionkind and name
func NewOpenAPIData(s *spec.Swagger) (*Resources, error) {
o := &Resources{
GroupVersionKindToName: map[schema.GroupVersionKind]string{},
NameToDefinition: map[string]Kind{},
......@@ -338,7 +338,7 @@ func (o *Resources) nameForDefinitionField(s spec.Schema) string {
return strings.Replace(p, "/definitions/", "", -1)
}
// getGroupVersionKind implements openAPIData
// getGroupVersionKind implements OpenAPIData
// getGVK parses the gropuversionkind for a resource definition from the x-kubernetes
// extensions
// Expected format for s.Extensions: map[string][]map[string]string
......
......@@ -37,31 +37,31 @@ func init() {
const openapiFileName = "openapi_cache"
type cachingOpenAPIClient struct {
type CachingOpenAPIClient struct {
version string
client discovery.OpenAPISchemaInterface
cacheDirName string
}
// newCachingOpenAPIClient returns a new discovery.OpenAPISchemaInterface
// NewCachingOpenAPIClient returns a new discovery.OpenAPISchemaInterface
// that will read the openapi spec from a local cache if it exists, and
// if not will then fetch an openapi spec using a client.
// client: used to fetch a new openapi spec if a local cache is not found
// version: the server version and used as part of the cache file location
// cacheDir: the directory under which the cache file will be written
func newCachingOpenAPIClient(client discovery.OpenAPISchemaInterface, version, cacheDir string) *cachingOpenAPIClient {
return &cachingOpenAPIClient{
func NewCachingOpenAPIClient(client discovery.OpenAPISchemaInterface, version, cacheDir string) *CachingOpenAPIClient {
return &CachingOpenAPIClient{
client: client,
version: version,
cacheDirName: cacheDir,
}
}
// openAPIData returns an openapi spec.
// OpenAPIData returns an openapi spec.
// It will first attempt to read the spec from a local cache
// If it cannot read a local cache, it will read the file
// using the client and then write the cache.
func (c *cachingOpenAPIClient) openAPIData() (*Resources, error) {
func (c *CachingOpenAPIClient) OpenAPIData() (*Resources, error) {
// Try to use the cached version
if c.useCache() {
doc, err := c.readOpenAPICache()
......@@ -77,7 +77,7 @@ func (c *cachingOpenAPIClient) openAPIData() (*Resources, error) {
return nil, err
}
oa, err := newOpenAPIData(s)
oa, err := NewOpenAPIData(s)
if err != nil {
glog.V(2).Infof("Failed to parse openapi data %v", err)
return nil, err
......@@ -97,12 +97,12 @@ func (c *cachingOpenAPIClient) openAPIData() (*Resources, error) {
}
// useCache returns true if the client should try to use the cache file
func (c *cachingOpenAPIClient) useCache() bool {
func (c *CachingOpenAPIClient) useCache() bool {
return len(c.version) > 0 && len(c.cacheDirName) > 0
}
// readOpenAPICache tries to read the openapi spec from the local file cache
func (c *cachingOpenAPIClient) readOpenAPICache() (*Resources, error) {
func (c *CachingOpenAPIClient) readOpenAPICache() (*Resources, error) {
// Get the filename to read
filename := c.openAPICacheFilename()
......@@ -119,7 +119,7 @@ func (c *cachingOpenAPIClient) readOpenAPICache() (*Resources, error) {
}
// decodeSpec binary decodes the openapi spec
func (c *cachingOpenAPIClient) decodeSpec(data []byte) (*Resources, error) {
func (c *CachingOpenAPIClient) decodeSpec(data []byte) (*Resources, error) {
b := bytes.NewBuffer(data)
d := gob.NewDecoder(b)
parsed := &Resources{}
......@@ -128,7 +128,7 @@ func (c *cachingOpenAPIClient) decodeSpec(data []byte) (*Resources, error) {
}
// encodeSpec binary encodes the openapi spec
func (c *cachingOpenAPIClient) encodeSpec(parsed *Resources) ([]byte, error) {
func (c *CachingOpenAPIClient) encodeSpec(parsed *Resources) ([]byte, error) {
b := &bytes.Buffer{}
e := gob.NewEncoder(b)
err := e.Encode(parsed)
......@@ -138,7 +138,7 @@ func (c *cachingOpenAPIClient) encodeSpec(parsed *Resources) ([]byte, error) {
// writeToCache tries to write the openapi spec to the local file cache.
// writes the data to a new tempfile, and then links the cache file and the tempfile
func (c *cachingOpenAPIClient) writeToCache(parsed *Resources) error {
func (c *CachingOpenAPIClient) writeToCache(parsed *Resources) error {
// Get the constant filename used to read the cache.
cacheFile := c.openAPICacheFilename()
......@@ -168,7 +168,7 @@ func (c *cachingOpenAPIClient) writeToCache(parsed *Resources) error {
}
// openAPICacheFilename returns the filename to read the cache from
func (c *cachingOpenAPIClient) openAPICacheFilename() string {
func (c *CachingOpenAPIClient) openAPICacheFilename() string {
// Cache using the client and server versions
return filepath.Join(c.cacheDirName, c.version, version.Get().GitVersion, openapiFileName)
}
......
......@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
package openapi
package openapi_test
import (
"fmt"
......@@ -27,25 +27,27 @@ import (
"github.com/go-openapi/spec"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi"
)
var _ = Describe("When reading openAPIData", func() {
var tmpDir string
var err error
var client *fakeOpenAPIClient
var instance *cachingOpenAPIClient
var expectedData *Resources
var instance *openapi.CachingOpenAPIClient
var expectedData *openapi.Resources
BeforeEach(func() {
tmpDir, err = ioutil.TempDir("", "openapi_cache_test")
Expect(err).To(BeNil())
client = &fakeOpenAPIClient{}
instance = newCachingOpenAPIClient(client, "v1.6", tmpDir)
instance = openapi.NewCachingOpenAPIClient(client, "v1.6", tmpDir)
d, err := data.OpenAPISchema()
Expect(err).To(BeNil())
expectedData, err = newOpenAPIData(d)
expectedData, err = openapi.NewOpenAPIData(d)
Expect(err).To(BeNil())
})
......@@ -55,7 +57,7 @@ var _ = Describe("When reading openAPIData", func() {
It("should write to the cache", func() {
By("getting the live openapi spec from the server")
result, err := instance.openAPIData()
result, err := instance.OpenAPIData()
Expect(err).To(BeNil())
expectEqual(result, expectedData)
Expect(client.calls).To(Equal(1))
......@@ -77,13 +79,13 @@ var _ = Describe("When reading openAPIData", func() {
It("should read from the cache", func() {
// First call should use the client
result, err := instance.openAPIData()
result, err := instance.OpenAPIData()
Expect(err).To(BeNil())
expectEqual(result, expectedData)
Expect(client.calls).To(Equal(1))
// Second call shouldn't use the client
result, err = instance.openAPIData()
result, err = instance.OpenAPIData()
Expect(err).To(BeNil())
expectEqual(result, expectedData)
Expect(client.calls).To(Equal(1))
......@@ -96,7 +98,7 @@ var _ = Describe("When reading openAPIData", func() {
It("propagate errors that are encountered", func() {
// Expect an error
client.err = fmt.Errorf("expected error")
result, err := instance.openAPIData()
result, err := instance.OpenAPIData()
Expect(err.Error()).To(Equal(client.err.Error()))
Expect(result).To(BeNil())
Expect(client.calls).To(Equal(1))
......@@ -107,7 +109,7 @@ var _ = Describe("When reading openAPIData", func() {
Expect(files).To(HaveLen(0))
// Client error is not cached
result, err = instance.openAPIData()
result, err = instance.OpenAPIData()
Expect(err.Error()).To(Equal(client.err.Error()))
Expect(result).To(BeNil())
Expect(client.calls).To(Equal(2))
......@@ -138,16 +140,16 @@ var _ = Describe("Reading openAPIData", func() {
It("should not cache the result", func() {
client := &fakeOpenAPIClient{}
instance := newCachingOpenAPIClient(client, serverVersion, cacheDir)
instance := openapi.NewCachingOpenAPIClient(client, serverVersion, cacheDir)
d, err := data.OpenAPISchema()
Expect(err).To(BeNil())
expectedData, err := newOpenAPIData(d)
expectedData, err := openapi.NewOpenAPIData(d)
Expect(err).To(BeNil())
By("getting the live openapi schema")
result, err := instance.openAPIData()
result, err := instance.OpenAPIData()
Expect(err).To(BeNil())
expectEqual(result, expectedData)
Expect(client.calls).To(Equal(1))
......@@ -166,16 +168,16 @@ var _ = Describe("Reading openAPIData", func() {
It("should not cache the result", func() {
client := &fakeOpenAPIClient{}
instance := newCachingOpenAPIClient(client, serverVersion, cacheDir)
instance := openapi.NewCachingOpenAPIClient(client, serverVersion, cacheDir)
d, err := data.OpenAPISchema()
Expect(err).To(BeNil())
expectedData, err := newOpenAPIData(d)
expectedData, err := openapi.NewOpenAPIData(d)
Expect(err).To(BeNil())
By("getting the live openapi schema")
result, err := instance.openAPIData()
result, err := instance.OpenAPIData()
Expect(err).To(BeNil())
expectEqual(result, expectedData)
Expect(client.calls).To(Equal(1))
......@@ -200,7 +202,7 @@ func getFilenames(path string) ([]string, error) {
return result, nil
}
func expectEqual(a *Resources, b *Resources) {
func expectEqual(a *openapi.Resources, b *openapi.Resources) {
Expect(a.NameToDefinition).To(HaveLen(len(b.NameToDefinition)))
for k, v := range a.NameToDefinition {
Expect(v).To(Equal(b.NameToDefinition[k]),
......
......@@ -38,7 +38,7 @@ var _ Getter = &synchronizedOpenAPIGetter{}
// Getter is an interface for fetching openapi specs and parsing them into an Resources struct
type Getter interface {
// openAPIData returns the parsed openAPIData
// OpenAPIData returns the parsed OpenAPIData
Get() (*Resources, error)
}
......@@ -55,8 +55,8 @@ func NewOpenAPIGetter(cacheDir, serverVersion string, openAPIClient discovery.Op
// Resources implements Getter
func (g *synchronizedOpenAPIGetter) Get() (*Resources, error) {
g.Do(func() {
client := newCachingOpenAPIClient(g.openAPIClient, g.serverVersion, g.cacheDir)
result, err := client.openAPIData()
client := NewCachingOpenAPIClient(g.openAPIClient, g.serverVersion, g.cacheDir)
result, err := client.OpenAPIData()
if err != nil {
g.err = err
return
......
......@@ -14,29 +14,31 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
package openapi
package openapi_test
import (
"fmt"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi"
)
var _ = Describe("Getting the Resources", func() {
var client *fakeOpenAPIClient
var expectedData *Resources
var instance Getter
var expectedData *openapi.Resources
var instance openapi.Getter
BeforeEach(func() {
client = &fakeOpenAPIClient{}
d, err := data.OpenAPISchema()
Expect(err).To(BeNil())
expectedData, err = newOpenAPIData(d)
expectedData, err = openapi.NewOpenAPIData(d)
Expect(err).To(BeNil())
instance = NewOpenAPIGetter("", "", client)
instance = openapi.NewOpenAPIGetter("", "", client)
})
Context("when the server returns a successful result", func() {
......
......@@ -18,12 +18,32 @@ package openapi_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/ginkgo/config"
. "github.com/onsi/ginkgo/types"
. "github.com/onsi/gomega"
"fmt"
"testing"
)
func TestOpenapi(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Openapi Suite")
RunSpecsWithDefaultAndCustomReporters(t, "Openapi Suite", []Reporter{newlineReporter{}})
}
// Print a newline after the default newlineReporter due to issue
// https://github.com/jstemmer/go-junit-report/issues/31
type newlineReporter struct{}
func (newlineReporter) SpecSuiteWillBegin(config GinkgoConfigType, summary *SuiteSummary) {}
func (newlineReporter) BeforeSuiteDidRun(setupSummary *SetupSummary) {}
func (newlineReporter) AfterSuiteDidRun(setupSummary *SetupSummary) {}
func (newlineReporter) SpecWillRun(specSummary *SpecSummary) {}
func (newlineReporter) SpecDidComplete(specSummary *SpecSummary) {}
// SpecSuiteDidEnd Prints a newline between "35 Passed | 0 Failed | 0 Pending | 0 Skipped" and "--- PASS:"
func (newlineReporter) SpecSuiteDidEnd(summary *SuiteSummary) { fmt.Printf("\n") }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment