Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
eb735bfe
Commit
eb735bfe
authored
Aug 06, 2017
by
Antoine Pelisse
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
openapi: Move Fakes to testing package
parent
4e3d37c0
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
146 additions
and
15 deletions
+146
-15
BUILD
pkg/kubectl/cmd/util/openapi/BUILD
+5
-1
openapi_getter_test.go
pkg/kubectl/cmd/util/openapi/openapi_getter_test.go
+13
-12
openapi_test.go
pkg/kubectl/cmd/util/openapi/openapi_test.go
+7
-2
BUILD
pkg/kubectl/cmd/util/openapi/testing/BUILD
+32
-0
openapi.go
pkg/kubectl/cmd/util/openapi/testing/openapi.go
+89
-0
No files found.
pkg/kubectl/cmd/util/openapi/BUILD
View file @
eb735bfe
...
@@ -40,6 +40,7 @@ go_test(
...
@@ -40,6 +40,7 @@ go_test(
data = ["//api/openapi-spec:swagger-spec"],
data = ["//api/openapi-spec:swagger-spec"],
deps = [
deps = [
":go_default_library",
":go_default_library",
"//pkg/kubectl/cmd/util/openapi/testing:go_default_library",
"//vendor/github.com/googleapis/gnostic/OpenAPIv2:go_default_library",
"//vendor/github.com/googleapis/gnostic/OpenAPIv2:go_default_library",
"//vendor/github.com/googleapis/gnostic/compiler:go_default_library",
"//vendor/github.com/googleapis/gnostic/compiler:go_default_library",
"//vendor/github.com/onsi/ginkgo:go_default_library",
"//vendor/github.com/onsi/ginkgo:go_default_library",
...
@@ -60,7 +61,10 @@ filegroup(
...
@@ -60,7 +61,10 @@ filegroup(
filegroup(
filegroup(
name = "all-srcs",
name = "all-srcs",
srcs = [":package-srcs"],
srcs = [
":package-srcs",
"//pkg/kubectl/cmd/util/openapi/testing:all-srcs",
],
tags = ["automanaged"],
tags = ["automanaged"],
)
)
...
...
pkg/kubectl/cmd/util/openapi/openapi_getter_test.go
View file @
eb735bfe
...
@@ -23,16 +23,17 @@ import (
...
@@ -23,16 +23,17 @@ import (
.
"github.com/onsi/gomega"
.
"github.com/onsi/gomega"
"k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi"
"k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi"
tst
"k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi/testing"
)
)
var
_
=
Describe
(
"Getting the Resources"
,
func
()
{
var
_
=
Describe
(
"Getting the Resources"
,
func
()
{
var
client
*
fakeOpenAPI
Client
var
client
*
tst
.
Fake
Client
var
expectedData
openapi
.
Resources
var
expectedData
openapi
.
Resources
var
instance
openapi
.
Getter
var
instance
openapi
.
Getter
BeforeEach
(
func
()
{
BeforeEach
(
func
()
{
client
=
&
fakeOpenAPIClient
{}
client
=
tst
.
NewFakeClient
(
&
fakeSchema
)
d
,
err
:=
dat
a
.
OpenAPISchema
()
d
,
err
:=
fakeSchem
a
.
OpenAPISchema
()
Expect
(
err
)
.
To
(
BeNil
())
Expect
(
err
)
.
To
(
BeNil
())
expectedData
,
err
=
openapi
.
NewOpenAPIData
(
d
)
expectedData
,
err
=
openapi
.
NewOpenAPIData
(
d
)
...
@@ -43,34 +44,34 @@ var _ = Describe("Getting the Resources", func() {
...
@@ -43,34 +44,34 @@ var _ = Describe("Getting the Resources", func() {
Context
(
"when the server returns a successful result"
,
func
()
{
Context
(
"when the server returns a successful result"
,
func
()
{
It
(
"should return the same data for multiple calls"
,
func
()
{
It
(
"should return the same data for multiple calls"
,
func
()
{
Expect
(
client
.
c
alls
)
.
To
(
Equal
(
0
))
Expect
(
client
.
C
alls
)
.
To
(
Equal
(
0
))
result
,
err
:=
instance
.
Get
()
result
,
err
:=
instance
.
Get
()
Expect
(
err
)
.
To
(
BeNil
())
Expect
(
err
)
.
To
(
BeNil
())
Expect
(
result
)
.
To
(
Equal
(
expectedData
))
Expect
(
result
)
.
To
(
Equal
(
expectedData
))
Expect
(
client
.
c
alls
)
.
To
(
Equal
(
1
))
Expect
(
client
.
C
alls
)
.
To
(
Equal
(
1
))
result
,
err
=
instance
.
Get
()
result
,
err
=
instance
.
Get
()
Expect
(
err
)
.
To
(
BeNil
())
Expect
(
err
)
.
To
(
BeNil
())
Expect
(
result
)
.
To
(
Equal
(
expectedData
))
Expect
(
result
)
.
To
(
Equal
(
expectedData
))
// No additional client calls expected
// No additional client calls expected
Expect
(
client
.
c
alls
)
.
To
(
Equal
(
1
))
Expect
(
client
.
C
alls
)
.
To
(
Equal
(
1
))
})
})
})
})
Context
(
"when the server returns an unsuccessful result"
,
func
()
{
Context
(
"when the server returns an unsuccessful result"
,
func
()
{
It
(
"should return the same instance for multiple calls."
,
func
()
{
It
(
"should return the same instance for multiple calls."
,
func
()
{
Expect
(
client
.
c
alls
)
.
To
(
Equal
(
0
))
Expect
(
client
.
C
alls
)
.
To
(
Equal
(
0
))
client
.
e
rr
=
fmt
.
Errorf
(
"expected error"
)
client
.
E
rr
=
fmt
.
Errorf
(
"expected error"
)
_
,
err
:=
instance
.
Get
()
_
,
err
:=
instance
.
Get
()
Expect
(
err
)
.
To
(
Equal
(
client
.
e
rr
))
Expect
(
err
)
.
To
(
Equal
(
client
.
E
rr
))
Expect
(
client
.
c
alls
)
.
To
(
Equal
(
1
))
Expect
(
client
.
C
alls
)
.
To
(
Equal
(
1
))
_
,
err
=
instance
.
Get
()
_
,
err
=
instance
.
Get
()
Expect
(
err
)
.
To
(
Equal
(
client
.
e
rr
))
Expect
(
err
)
.
To
(
Equal
(
client
.
E
rr
))
// No additional client calls expected
// No additional client calls expected
Expect
(
client
.
c
alls
)
.
To
(
Equal
(
1
))
Expect
(
client
.
C
alls
)
.
To
(
Equal
(
1
))
})
})
})
})
})
})
pkg/kubectl/cmd/util/openapi/openapi_test.go
View file @
eb735bfe
...
@@ -17,17 +17,22 @@ limitations under the License.
...
@@ -17,17 +17,22 @@ limitations under the License.
package
openapi_test
package
openapi_test
import
(
import
(
"path/filepath"
.
"github.com/onsi/ginkgo"
.
"github.com/onsi/ginkgo"
.
"github.com/onsi/gomega"
.
"github.com/onsi/gomega"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi"
"k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi"
tst
"k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi/testing"
)
)
var
fakeSchema
=
tst
.
Fake
{
Path
:
filepath
.
Join
(
".."
,
".."
,
".."
,
".."
,
".."
,
"api"
,
"openapi-spec"
,
"swagger.json"
)}
var
_
=
Describe
(
"Reading apps/v1beta1/Deployment from openAPIData"
,
func
()
{
var
_
=
Describe
(
"Reading apps/v1beta1/Deployment from openAPIData"
,
func
()
{
var
resources
openapi
.
Resources
var
resources
openapi
.
Resources
BeforeEach
(
func
()
{
BeforeEach
(
func
()
{
s
,
err
:=
dat
a
.
OpenAPISchema
()
s
,
err
:=
fakeSchem
a
.
OpenAPISchema
()
Expect
(
err
)
.
To
(
BeNil
())
Expect
(
err
)
.
To
(
BeNil
())
resources
,
err
=
openapi
.
NewOpenAPIData
(
s
)
resources
,
err
=
openapi
.
NewOpenAPIData
(
s
)
Expect
(
err
)
.
To
(
BeNil
())
Expect
(
err
)
.
To
(
BeNil
())
...
@@ -136,7 +141,7 @@ var _ = Describe("Reading apps/v1beta1/Deployment from openAPIData", func() {
...
@@ -136,7 +141,7 @@ var _ = Describe("Reading apps/v1beta1/Deployment from openAPIData", func() {
var
_
=
Describe
(
"Reading authorization.k8s.io/v1/SubjectAccessReview from openAPIData"
,
func
()
{
var
_
=
Describe
(
"Reading authorization.k8s.io/v1/SubjectAccessReview from openAPIData"
,
func
()
{
var
resources
openapi
.
Resources
var
resources
openapi
.
Resources
BeforeEach
(
func
()
{
BeforeEach
(
func
()
{
s
,
err
:=
dat
a
.
OpenAPISchema
()
s
,
err
:=
fakeSchem
a
.
OpenAPISchema
()
Expect
(
err
)
.
To
(
BeNil
())
Expect
(
err
)
.
To
(
BeNil
())
resources
,
err
=
openapi
.
NewOpenAPIData
(
s
)
resources
,
err
=
openapi
.
NewOpenAPIData
(
s
)
Expect
(
err
)
.
To
(
BeNil
())
Expect
(
err
)
.
To
(
BeNil
())
...
...
pkg/kubectl/cmd/util/openapi/testing/BUILD
0 → 100644
View file @
eb735bfe
package(default_visibility = ["//visibility:public"])
licenses(["notice"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
)
go_library(
name = "go_default_library",
srcs = ["openapi.go"],
tags = ["automanaged"],
deps = [
"//vendor/github.com/googleapis/gnostic/OpenAPIv2:go_default_library",
"//vendor/github.com/googleapis/gnostic/compiler:go_default_library",
"//vendor/gopkg.in/yaml.v2:go_default_library",
],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
)
pkg/kubectl/cmd/util/openapi/testing/openapi.go
0 → 100644
View file @
eb735bfe
/*
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
testing
import
(
"io/ioutil"
"os"
"sync"
yaml
"gopkg.in/yaml.v2"
"github.com/googleapis/gnostic/OpenAPIv2"
"github.com/googleapis/gnostic/compiler"
)
// Fake opens and returns a openapi swagger from a file Path. It will
// parse only once and then return the same copy everytime.
type
Fake
struct
{
Path
string
once
sync
.
Once
document
*
openapi_v2
.
Document
err
error
}
// OpenAPISchema returns the openapi document and a potential error.
func
(
f
*
Fake
)
OpenAPISchema
()
(
*
openapi_v2
.
Document
,
error
)
{
f
.
once
.
Do
(
func
()
{
_
,
err
:=
os
.
Stat
(
f
.
Path
)
if
err
!=
nil
{
f
.
err
=
err
return
}
spec
,
err
:=
ioutil
.
ReadFile
(
f
.
Path
)
if
err
!=
nil
{
f
.
err
=
err
return
}
var
info
yaml
.
MapSlice
err
=
yaml
.
Unmarshal
(
spec
,
&
info
)
if
err
!=
nil
{
f
.
err
=
err
return
}
f
.
document
,
f
.
err
=
openapi_v2
.
NewDocument
(
info
,
compiler
.
NewContext
(
"$root"
,
nil
))
})
return
f
.
document
,
f
.
err
}
// FakeClient implements a dummy OpenAPISchemaInterface that uses the
// fake OpenAPI schema given as a parameter, and count the number of
// call to the function.
type
FakeClient
struct
{
Calls
int
Err
error
fake
*
Fake
}
// NewFakeClient creates a new FakeClient from the given Fake.
func
NewFakeClient
(
f
*
Fake
)
*
FakeClient
{
return
&
FakeClient
{
fake
:
f
}
}
// OpenAPISchema returns a OpenAPI Document as returned by the fake, but
// it also counts the number of calls.
func
(
f
*
FakeClient
)
OpenAPISchema
()
(
*
openapi_v2
.
Document
,
error
)
{
f
.
Calls
=
f
.
Calls
+
1
if
f
.
Err
!=
nil
{
return
nil
,
f
.
Err
}
return
f
.
fake
.
OpenAPISchema
()
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment