runhcs_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // +build runhcs_test
  2. package runhcs
  3. import (
  4. "bytes"
  5. "context"
  6. "io"
  7. "os"
  8. "path/filepath"
  9. "sync"
  10. "testing"
  11. runc "github.com/containerd/go-runc"
  12. )
  13. func TestRunhcs_E2E(t *testing.T) {
  14. rhcs := Runhcs{
  15. Debug: true,
  16. }
  17. // TODO: JTERRY75 use this from assets dynamically
  18. //dir, err := ioutil.TempDir("", "runhcs-bundle")
  19. //if err != nil {
  20. // t.Fatalf("failed to create tempdir with error: %v", err)
  21. //}
  22. //defer os.Remove(dir)
  23. br := os.Getenv("RUNHCS_TEST_BUNDLE_ROOT")
  24. if br == "" {
  25. t.Fatal("You must set %RUNHCS_TEST_BUNDLE_ROOT% to the folder containing the test bundles")
  26. return
  27. }
  28. // TODO: JTERRY75 create this spec dynamically once we can do the layer
  29. // extraction in some way so we dont need hard coded bundle/config.json's
  30. dir := filepath.Join(br, "runhcs-tee-test")
  31. ctx := context.TODO()
  32. id := "runhcs-e2e-id"
  33. pio, err := runc.NewPipeIO()
  34. if err != nil {
  35. t.Fatalf("failed to create new pipe io with error: %v", err)
  36. }
  37. defer pio.Close()
  38. // Write our expected output
  39. expected := "Hello go-runhcs-container!"
  40. inbuff := bytes.NewBufferString(expected)
  41. outbuff := &bytes.Buffer{}
  42. errbuff := &bytes.Buffer{}
  43. wg := sync.WaitGroup{}
  44. wg.Add(2)
  45. go func() {
  46. _, err := io.Copy(pio.Stdin(), inbuff)
  47. if err != nil {
  48. t.Errorf("failed to copy string to stdin pipe.")
  49. }
  50. pio.Stdin().Close()
  51. }()
  52. go func() {
  53. _, err := io.Copy(outbuff, pio.Stdout())
  54. if err != nil {
  55. t.Errorf("failed to copy string from stdout pipe")
  56. }
  57. wg.Done()
  58. }()
  59. go func() {
  60. _, err := io.Copy(errbuff, pio.Stderr())
  61. if err != nil {
  62. t.Errorf("failed to copy string from stderr pipe")
  63. }
  64. wg.Done()
  65. }()
  66. copts := &CreateOpts{
  67. IO: pio,
  68. PidFile: filepath.Join(dir, "pid-file.txt"),
  69. ShimLog: filepath.Join(dir, "shim-log.txt"),
  70. VMLog: filepath.Join(dir, "vm-log.txt"),
  71. }
  72. if err := rhcs.Create(ctx, id, dir, copts); err != nil {
  73. t.Fatalf("failed to create container with error: %v", err)
  74. }
  75. defer func() {
  76. if err := rhcs.Delete(ctx, id, &DeleteOpts{Force: true}); err != nil {
  77. t.Fatalf("failed to delete container with error: %v", err)
  78. }
  79. }()
  80. if err := rhcs.Start(ctx, id); err != nil {
  81. t.Fatalf("failed to start container with error: %v", err)
  82. }
  83. wg.Wait()
  84. outstring := outbuff.String()
  85. if outstring != expected {
  86. t.Fatalf("stdout string '%s' != expected '%s'", outstring, expected)
  87. }
  88. errstring := errbuff.String()
  89. if errstring != expected {
  90. t.Fatalf("stderr string '%s' != expected '%s'", errstring, expected)
  91. }
  92. }