Merge pull request #54444 from apelisse/long-strategic-test

Automatic merge from submit-queue. If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>.

apply/strategy: Improve test performance

**What this PR does / why we need it**:

Test running time goes from 150 seconds to 7 seconds with that one
simple trick. Do not re-parse openapi schema for every test, but keep
one version for the whole suite (specifically since it's read-only anyway).

**Which issue this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close that issue when PR gets merged)*: fixes #

**Special notes for your reviewer**:

**Release note**:
```release-note
NONE
```
pull/6/head
Kubernetes Submit Queue 2017-10-23 22:07:58 -07:00 committed by GitHub
commit 06a93e5f63
4 changed files with 22 additions and 13 deletions

View File

@ -20,6 +20,8 @@ import (
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
"k8s.io/kubernetes/pkg/kubectl/apply/strategy" "k8s.io/kubernetes/pkg/kubectl/apply/strategy"
"k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi"
tst "k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi/testing"
) )
var _ = Describe("Merging fields of type list-of-map with openapi", func() { var _ = Describe("Merging fields of type list-of-map with openapi", func() {
@ -431,6 +433,11 @@ spec:
}) })
var _ = Describe("Merging fields of type list-of-map with openapi containing a multi-field mergekey", func() { var _ = Describe("Merging fields of type list-of-map with openapi containing a multi-field mergekey", func() {
var resources openapi.Resources
BeforeEach(func() {
resources = tst.NewFakeResources("test_swagger.json")
})
Context("where one of the items has been deleted", func() { Context("where one of the items has been deleted", func() {
It("should delete the item", func() { It("should delete the item", func() {
recorded := create(` recorded := create(`
@ -492,7 +499,7 @@ spec:
protocol: TCP protocol: TCP
hostPort: 2020 hostPort: 2020
`) `)
runWith(strategy.Create(strategy.Options{}), recorded, local, remote, expected, "test_swagger.json") runWith(strategy.Create(strategy.Options{}), recorded, local, remote, expected, resources)
}) })
}) })
@ -564,7 +571,7 @@ spec:
hostPort: 2022 hostPort: 2022
hostIP: "127.0.0.1" hostIP: "127.0.0.1"
`) `)
runWith(strategy.Create(strategy.Options{}), recorded, local, remote, expected, "test_swagger.json") runWith(strategy.Create(strategy.Options{}), recorded, local, remote, expected, resources)
}) })
}) })
@ -630,7 +637,7 @@ spec:
protocol: UDP protocol: UDP
hostPort: 2022 hostPort: 2022
`) `)
runWith(strategy.Create(strategy.Options{}), recorded, local, remote, expected, "test_swagger.json") runWith(strategy.Create(strategy.Options{}), recorded, local, remote, expected, resources)
}) })
}) })
}) })

View File

@ -51,7 +51,6 @@ func (v mergeStrategy) MergeList(e apply.ListElement) (apply.Result, error) {
return apply.Result{}, err return apply.Result{}, err
} }
fmt.Printf("\nResult %+v\n%+v\n", m.MergedResult, value)
switch m.Operation { switch m.Operation {
case apply.SET: case apply.SET:
// Keep the list item value // Keep the list item value

View File

@ -20,9 +20,16 @@ import (
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
"k8s.io/kubernetes/pkg/kubectl/apply/strategy" "k8s.io/kubernetes/pkg/kubectl/apply/strategy"
"k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi"
tst "k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi/testing"
) )
var _ = Describe("Replacing fields of type map with openapi for some fields", func() { var _ = Describe("Replacing fields of type map with openapi for some fields", func() {
var resources openapi.Resources
BeforeEach(func() {
resources = tst.NewFakeResources("test_swagger.json")
})
Context("where a field is has been updated", func() { Context("where a field is has been updated", func() {
It("should update the field", func() { It("should update the field", func() {
recorded := create(` recorded := create(`
@ -67,7 +74,7 @@ spec:
`) `)
// Use modified swagger for ReplicaSet spec // Use modified swagger for ReplicaSet spec
runWith(strategy.Create(strategy.Options{}), recorded, local, remote, expected, "test_swagger.json") runWith(strategy.Create(strategy.Options{}), recorded, local, remote, expected, resources)
}) })
}) })
}) })

View File

@ -32,18 +32,14 @@ import (
tst "k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi/testing" tst "k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi/testing"
) )
var fakeResources = tst.NewFakeResources(filepath.Join("..", "..", "..", "..", "api", "openapi-spec", "swagger.json"))
// run parses the openapi and runs the tests // run parses the openapi and runs the tests
func run(instance apply.Strategy, recorded, local, remote, expected map[string]interface{}) { func run(instance apply.Strategy, recorded, local, remote, expected map[string]interface{}) {
runWith(instance, recorded, local, remote, expected, runWith(instance, recorded, local, remote, expected, fakeResources)
filepath.Join("..", "..", "..", "..", "api", "openapi-spec", "swagger.json"))
} }
func runWith(instance apply.Strategy, recorded, local, remote, expected map[string]interface{}, swaggerPath string) { func runWith(instance apply.Strategy, recorded, local, remote, expected map[string]interface{}, resources openapi.Resources) {
fakeSchema := tst.Fake{Path: swaggerPath}
s, err := fakeSchema.OpenAPISchema()
Expect(err).To(BeNil())
resources, err := openapi.NewOpenAPIData(s)
Expect(err).To(BeNil())
parseFactory := parse.Factory{Resources: resources} parseFactory := parse.Factory{Resources: resources}
parsed, err := parseFactory.CreateElement(recorded, local, remote) parsed, err := parseFactory.CreateElement(recorded, local, remote)