common_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 flexvolume
  14. import (
  15. "encoding/json"
  16. "k8s.io/api/core/v1"
  17. "k8s.io/kubernetes/pkg/volume"
  18. volumetesting "k8s.io/kubernetes/pkg/volume/testing"
  19. "k8s.io/kubernetes/test/utils/harness"
  20. "k8s.io/utils/exec"
  21. fakeexec "k8s.io/utils/exec/testing"
  22. )
  23. func testPlugin(h *harness.Harness) (*flexVolumeAttachablePlugin, string) {
  24. rootDir := h.TempDir("", "flexvolume_test")
  25. return &flexVolumeAttachablePlugin{
  26. flexVolumePlugin: &flexVolumePlugin{
  27. driverName: "test",
  28. execPath: "/plugin",
  29. host: volumetesting.NewFakeVolumeHost(rootDir, nil, nil),
  30. unsupportedCommands: []string{},
  31. },
  32. }, rootDir
  33. }
  34. func assertDriverCall(t *harness.Harness, output fakeexec.FakeCombinedOutputAction, expectedCommand string, expectedArgs ...string) fakeexec.FakeCommandAction {
  35. return func(cmd string, args ...string) exec.Cmd {
  36. if cmd != "/plugin/test" {
  37. t.Errorf("Wrong executable called: got %v, expected %v", cmd, "/plugin/test")
  38. }
  39. if args[0] != expectedCommand {
  40. t.Errorf("Wrong command called: got %v, expected %v", args[0], expectedCommand)
  41. }
  42. cmdArgs := args[1:]
  43. if !sameArgs(cmdArgs, expectedArgs) {
  44. t.Errorf("Wrong args for %s: got %v, expected %v", args[0], cmdArgs, expectedArgs)
  45. }
  46. return &fakeexec.FakeCmd{
  47. Argv: args,
  48. CombinedOutputScript: []fakeexec.FakeCombinedOutputAction{output},
  49. }
  50. }
  51. }
  52. func fakeRunner(fakeCommands ...fakeexec.FakeCommandAction) exec.Interface {
  53. return &fakeexec.FakeExec{
  54. CommandScript: fakeCommands,
  55. }
  56. }
  57. func fakeResultOutput(result interface{}) fakeexec.FakeCombinedOutputAction {
  58. return func() ([]byte, error) {
  59. bytes, err := json.Marshal(result)
  60. if err != nil {
  61. panic("Unable to marshal result: " + err.Error())
  62. }
  63. return bytes, nil
  64. }
  65. }
  66. func successOutput() fakeexec.FakeCombinedOutputAction {
  67. return fakeResultOutput(&DriverStatus{StatusSuccess, "", "", "", true, nil, 0})
  68. }
  69. func notSupportedOutput() fakeexec.FakeCombinedOutputAction {
  70. return fakeResultOutput(&DriverStatus{StatusNotSupported, "", "", "", false, nil, 0})
  71. }
  72. func sameArgs(args, expectedArgs []string) bool {
  73. if len(args) != len(expectedArgs) {
  74. return false
  75. }
  76. for i, v := range args {
  77. if v != expectedArgs[i] {
  78. return false
  79. }
  80. }
  81. return true
  82. }
  83. func fakeVolumeSpec() *volume.Spec {
  84. vol := &v1.Volume{
  85. Name: "vol1",
  86. VolumeSource: v1.VolumeSource{
  87. FlexVolume: &v1.FlexVolumeSource{
  88. Driver: "kubernetes.io/fakeAttacher",
  89. ReadOnly: false,
  90. },
  91. },
  92. }
  93. return volume.NewSpecFromVolume(vol)
  94. }
  95. func specJSON(plugin *flexVolumeAttachablePlugin, spec *volume.Spec, extraOptions map[string]string) string {
  96. o, err := NewOptionsForDriver(spec, plugin.host, extraOptions)
  97. if err != nil {
  98. panic("Failed to convert spec: " + err.Error())
  99. }
  100. bytes, err := json.Marshal(o)
  101. if err != nil {
  102. panic("Unable to marshal result: " + err.Error())
  103. }
  104. return string(bytes)
  105. }