kube_command_test.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright 2019 Yunion
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package command
  15. import (
  16. "reflect"
  17. "strings"
  18. "testing"
  19. o "yunion.io/x/onecloud/pkg/webconsole/options"
  20. )
  21. func init() {
  22. o.Options.KubectlPath = "/usr/bin/kubectl"
  23. }
  24. func TestKubectlExec_Command(t *testing.T) {
  25. type fields struct {
  26. Kubectl *Kubectl
  27. }
  28. type args struct {
  29. cmd string
  30. args []string
  31. }
  32. tests := []struct {
  33. name string
  34. fields fields
  35. args args
  36. want string
  37. }{
  38. {
  39. name: "bash command",
  40. fields: fields{
  41. Kubectl: NewKubectlCommand(nil, "/tmp/kubeconfig", "system"),
  42. },
  43. args: args{
  44. cmd: "bash",
  45. args: []string{"-il"},
  46. },
  47. want: "/usr/bin/kubectl --namespace system exec -i -t Pod1 -c Container1 -- bash -il",
  48. },
  49. }
  50. for _, tt := range tests {
  51. t.Run(tt.name, func(t *testing.T) {
  52. c := tt.fields.Kubectl.Exec().
  53. Stdin().TTY().
  54. Pod("Pod1").
  55. Container("Container1")
  56. cmd := c.Command(tt.args.cmd, tt.args.args...)
  57. name := cmd.name
  58. args := cmd.args
  59. got := []string{name}
  60. got = append(got, args...)
  61. if !reflect.DeepEqual(strings.Join(got, " "), tt.want) {
  62. t.Errorf("KubectlExec.Command() = %v, want %v", got, tt.want)
  63. }
  64. })
  65. }
  66. }