github.com/hanks177/podman/v4@v4.1.3-0.20220613032544-16d90015bc83/pkg/machine/e2e/machine_test.go (about)

     1  package e2e
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"io/ioutil"
     7  	url2 "net/url"
     8  	"os"
     9  	"path"
    10  	"path/filepath"
    11  	"strings"
    12  	"testing"
    13  	"time"
    14  
    15  	"github.com/hanks177/podman/v4/pkg/machine"
    16  	. "github.com/onsi/ginkgo"
    17  	. "github.com/onsi/gomega"
    18  )
    19  
    20  func TestMain(m *testing.M) {
    21  	os.Exit(m.Run())
    22  }
    23  
    24  const (
    25  	defaultStream string = "podman-testing"
    26  )
    27  
    28  var (
    29  	tmpDir         = "/var/tmp"
    30  	fqImageName    string
    31  	suiteImageName string
    32  )
    33  
    34  func init() {
    35  	if value, ok := os.LookupEnv("TMPDIR"); ok {
    36  		tmpDir = value
    37  	}
    38  }
    39  
    40  // TestLibpod ginkgo master function
    41  func TestMachine(t *testing.T) {
    42  	RegisterFailHandler(Fail)
    43  	RunSpecs(t, "Podman Machine tests")
    44  }
    45  
    46  var _ = BeforeSuite(func() {
    47  	fcd, err := machine.GetFCOSDownload(defaultStream)
    48  	if err != nil {
    49  		Fail("unable to get virtual machine image")
    50  	}
    51  	suiteImageName = strings.TrimSuffix(path.Base(fcd.Location), ".xz")
    52  	fqImageName = filepath.Join(tmpDir, suiteImageName)
    53  	if _, err := os.Stat(fqImageName); err != nil {
    54  		if os.IsNotExist(err) {
    55  			getMe, err := url2.Parse(fcd.Location)
    56  			if err != nil {
    57  				Fail(fmt.Sprintf("unable to create url for download: %q", err))
    58  			}
    59  			now := time.Now()
    60  			if err := machine.DownloadVMImage(getMe, fqImageName+".xz"); err != nil {
    61  				Fail(fmt.Sprintf("unable to download machine image: %q", err))
    62  			}
    63  			fmt.Println("Download took: ", time.Since(now).String())
    64  			if err := machine.Decompress(fqImageName+".xz", fqImageName); err != nil {
    65  				Fail(fmt.Sprintf("unable to decompress image file: %q", err))
    66  			}
    67  		} else {
    68  			Fail(fmt.Sprintf("unable to check for cache image: %q", err))
    69  		}
    70  	}
    71  })
    72  
    73  var _ = SynchronizedAfterSuite(func() {},
    74  	func() {
    75  		fmt.Println("After")
    76  	})
    77  
    78  func setup() (string, *machineTestBuilder) {
    79  	// Set TMPDIR if this needs a new directory
    80  	homeDir, err := ioutil.TempDir("", "podman_test")
    81  	if err != nil {
    82  		Fail(fmt.Sprintf("failed to create home directory: %q", err))
    83  	}
    84  	if err := os.MkdirAll(filepath.Join(homeDir, ".ssh"), 0700); err != nil {
    85  		Fail(fmt.Sprintf("failed to create ssh dir: %q", err))
    86  	}
    87  	sshConfig, err := os.Create(filepath.Join(homeDir, ".ssh", "config"))
    88  	if err != nil {
    89  		Fail(fmt.Sprintf("failed to create ssh config: %q", err))
    90  	}
    91  	if _, err := sshConfig.WriteString("IdentitiesOnly=yes"); err != nil {
    92  		Fail(fmt.Sprintf("failed to write ssh config: %q", err))
    93  	}
    94  	if err := sshConfig.Close(); err != nil {
    95  		Fail(fmt.Sprintf("unable to close ssh config file descriptor: %q", err))
    96  	}
    97  	if err := os.Setenv("HOME", homeDir); err != nil {
    98  		Fail("failed to set home dir")
    99  	}
   100  	if err := os.Unsetenv("SSH_AUTH_SOCK"); err != nil {
   101  		Fail("unable to unset SSH_AUTH_SOCK")
   102  	}
   103  	mb, err := newMB()
   104  	if err != nil {
   105  		Fail(fmt.Sprintf("failed to create machine test: %q", err))
   106  	}
   107  	f, err := os.Open(fqImageName)
   108  	if err != nil {
   109  		Fail(fmt.Sprintf("failed to open file %s: %q", fqImageName, err))
   110  	}
   111  	mb.imagePath = filepath.Join(homeDir, suiteImageName)
   112  	n, err := os.Create(mb.imagePath)
   113  	if err != nil {
   114  		Fail(fmt.Sprintf("failed to create file %s: %q", mb.imagePath, err))
   115  	}
   116  	if _, err := io.Copy(n, f); err != nil {
   117  		Fail(fmt.Sprintf("failed to copy %ss to %s: %q", fqImageName, mb.imagePath, err))
   118  	}
   119  	return homeDir, mb
   120  }
   121  
   122  func teardown(origHomeDir string, testDir string, mb *machineTestBuilder) {
   123  	s := new(stopMachine)
   124  	for _, name := range mb.names {
   125  		if _, err := mb.setName(name).setCmd(s).run(); err != nil {
   126  			fmt.Printf("error occurred rm'ing machine: %q\n", err)
   127  		}
   128  	}
   129  	if err := os.RemoveAll(testDir); err != nil {
   130  		Fail(fmt.Sprintf("failed to remove test dir: %q", err))
   131  	}
   132  	// this needs to be last in teardown
   133  	if err := os.Setenv("HOME", origHomeDir); err != nil {
   134  		Fail("failed to set home dir")
   135  	}
   136  }