utils_test.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. Copyright 2017 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package strategy_test
  14. import (
  15. . "github.com/onsi/gomega"
  16. "fmt"
  17. "path/filepath"
  18. "strings"
  19. "sigs.k8s.io/yaml"
  20. "k8s.io/apimachinery/pkg/util/diff"
  21. "k8s.io/kubernetes/pkg/kubectl/apply"
  22. "k8s.io/kubernetes/pkg/kubectl/apply/parse"
  23. "k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi"
  24. tst "k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi/testing"
  25. )
  26. const (
  27. hasConflict = true
  28. noConflict = false
  29. )
  30. var fakeResources = tst.NewFakeResources(filepath.Join("..", "..", "..", "..", "api", "openapi-spec", "swagger.json"))
  31. // run parses the openapi and runs the tests
  32. func run(instance apply.Strategy, recorded, local, remote, expected map[string]interface{}) {
  33. runWith(instance, recorded, local, remote, expected, fakeResources)
  34. }
  35. func runWith(instance apply.Strategy, recorded, local, remote, expected map[string]interface{}, resources openapi.Resources) {
  36. parseFactory := parse.Factory{Resources: resources}
  37. parsed, err := parseFactory.CreateElement(recorded, local, remote)
  38. Expect(err).Should(Not(HaveOccurred()))
  39. merged, err := parsed.Merge(instance)
  40. Expect(err).ShouldNot(HaveOccurred())
  41. Expect(merged.Operation).Should(Equal(apply.SET))
  42. Expect(merged.MergedResult).Should(Equal(expected), diff.ObjectDiff(merged.MergedResult, expected))
  43. }
  44. // create parses the yaml string into a map[string]interface{}. Verifies that the string does not have
  45. // any tab characters.
  46. func create(config string) map[string]interface{} {
  47. result := map[string]interface{}{}
  48. // The yaml parser will throw an obscure error if there are tabs in the yaml. Check for this
  49. Expect(strings.Contains(config, "\t")).To(
  50. BeFalse(), fmt.Sprintf("Yaml %s cannot contain tabs", config))
  51. Expect(yaml.Unmarshal([]byte(config), &result)).Should(
  52. Not(HaveOccurred()), fmt.Sprintf("Could not parse config:\n\n%s\n", config))
  53. return result
  54. }
  55. func runConflictTest(instance apply.Strategy, recorded, local, remote map[string]interface{}, isConflict bool) {
  56. parseFactory := parse.Factory{Resources: fakeResources}
  57. parsed, err := parseFactory.CreateElement(recorded, local, remote)
  58. Expect(err).Should(Not(HaveOccurred()))
  59. merged, err := parsed.Merge(instance)
  60. if isConflict {
  61. Expect(err).Should(HaveOccurred())
  62. } else {
  63. Expect(err).ShouldNot(HaveOccurred())
  64. Expect(merged.Operation).Should(Equal(apply.SET))
  65. }
  66. }