playbook_test.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 ansible
  15. import (
  16. "bytes"
  17. "context"
  18. "os/exec"
  19. "reflect"
  20. "testing"
  21. )
  22. func skipIfNoAnsible(t *testing.T) {
  23. _, err := exec.LookPath("ansible")
  24. if err != nil {
  25. t.Skipf("looking for ansible: %v", err)
  26. }
  27. }
  28. func TestPlaybook(t *testing.T) {
  29. skipIfNoAnsible(t)
  30. pb := NewPlaybook()
  31. pb.Inventory = Inventory{
  32. Hosts: []Host{
  33. {
  34. Name: "127.0.0.1",
  35. Vars: map[string]string{
  36. "ansible_connection": "local",
  37. },
  38. },
  39. },
  40. }
  41. pb.Modules = []Module{
  42. {
  43. Name: "ping",
  44. },
  45. {
  46. Name: "copy",
  47. Args: []string{
  48. "src=afile",
  49. "dest=/tmp/afile",
  50. },
  51. },
  52. {
  53. Name: "copy",
  54. Args: []string{
  55. "src=adir/afile",
  56. "dest=/tmp/adirfile",
  57. },
  58. },
  59. }
  60. pb.Files = map[string][]byte{
  61. "afile": []byte("afilecontent"),
  62. "adir/afile": []byte("afilecontent under adir"),
  63. }
  64. t.Run("copy", func(t *testing.T) {
  65. pb2 := pb.Copy()
  66. if !reflect.DeepEqual(pb2, pb) {
  67. t.Errorf("copy and the original should be equal")
  68. }
  69. })
  70. t.Run("run", func(t *testing.T) {
  71. b := &bytes.Buffer{}
  72. pb.OutputWriter(b)
  73. err := pb.Run(context.TODO())
  74. t.Logf("%s", b.String())
  75. if err != nil {
  76. t.Fatalf("not expecting err: %v", err)
  77. }
  78. })
  79. }