github.com/cloudfoundry-incubator/stembuild@v0.0.0-20211223202937-5b61d62226c6/iaas_cli/iaas_clients/guest_manager/guest_manager_test.go (about)

     1  package guest_manager_test
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"time"
     7  
     8  	"github.com/cloudfoundry-incubator/stembuild/iaas_cli/iaas_clients/guest_manager/guest_managerfakes"
     9  
    10  	"github.com/cloudfoundry-incubator/stembuild/iaas_cli/iaas_clients/guest_manager"
    11  	. "github.com/onsi/ginkgo"
    12  	. "github.com/onsi/gomega"
    13  	"github.com/vmware/govmomi/vim25/types"
    14  )
    15  
    16  var _ = Describe("GuestManager", func() {
    17  	var (
    18  		auth         types.NamePasswordAuthentication
    19  		ctx          context.Context
    20  		procManager  guest_managerfakes.FakeProcManager
    21  		fileManager  guest_managerfakes.FakeFileManager
    22  		client       guest_managerfakes.FakeDownloadClient
    23  		guestManager *guest_manager.GuestManager
    24  	)
    25  
    26  	BeforeEach(func() {
    27  		ctx = context.TODO()
    28  		auth = types.NamePasswordAuthentication{}
    29  		procManager = guest_managerfakes.FakeProcManager{}
    30  		fileManager = guest_managerfakes.FakeFileManager{}
    31  		client = guest_managerfakes.FakeDownloadClient{}
    32  		guestManager = guest_manager.NewGuestManager(auth, &procManager, &fileManager, &client)
    33  	})
    34  
    35  	Describe("StartProgramInGuest", func() {
    36  		It("runs the command on the guest", func() {
    37  			expectedPid := int64(600)
    38  			procManager.StartProgramReturns(expectedPid, nil)
    39  
    40  			pid, err := guestManager.StartProgramInGuest(ctx, "mkdir", "C:\\dummy")
    41  			Expect(err).NotTo(HaveOccurred())
    42  			Expect(pid).To(Equal(expectedPid))
    43  		})
    44  
    45  		It("returns an error if StartProgram does", func() {
    46  			procManager.StartProgramReturns(int64(0), errors.New("You aint nothin but a hound dog"))
    47  
    48  			_, err := guestManager.StartProgramInGuest(ctx, "mkdir", "C:\\dummy")
    49  			Expect(err).To(MatchError("vcenter_client - could not run process: mkdir C:\\dummy on guest os, error: You aint nothin but a hound dog"))
    50  		})
    51  	})
    52  
    53  	Describe("ExitCodeForProgramInGuest", func() {
    54  		It("obtains the exit code when the given program, being run on the guest os, exits", func() {
    55  			expectedTime := time.Now()
    56  			expectedExitCode := int32(0)
    57  			processInfo := types.GuestProcessInfo{
    58  				ExitCode: expectedExitCode,
    59  				EndTime:  &expectedTime,
    60  			}
    61  
    62  			procManager.ListProcessesReturns([]types.GuestProcessInfo{processInfo}, nil)
    63  
    64  			exitCode, err := guestManager.ExitCodeForProgramInGuest(ctx, 1000)
    65  			Expect(err).NotTo(HaveOccurred())
    66  			Expect(exitCode).To(Equal(expectedExitCode))
    67  		})
    68  
    69  		It("returns an error if ListProcesses does", func() {
    70  			procManager.ListProcessesReturns(nil, errors.New("yo"))
    71  
    72  			_, err := guestManager.ExitCodeForProgramInGuest(ctx, 1000)
    73  			Expect(err).To(MatchError("vcenter_client - could not observe program exiting: yo"))
    74  		})
    75  
    76  		It("returns an error if ListProcesses does not find pid", func() {
    77  			procManager.ListProcessesReturns([]types.GuestProcessInfo{}, nil)
    78  
    79  			_, err := guestManager.ExitCodeForProgramInGuest(ctx, 1000)
    80  			Expect(err).To(MatchError("vcenter_client - could not observe program exiting"))
    81  		})
    82  	})
    83  
    84  	Describe("DownloadFileInGuest", func() {
    85  		It("returns an error if  qInitiateFileTransferFromGuest fails", func() {
    86  			fileManager.InitiateFileTransferFromGuestReturns(nil, errors.New("couldn't initiate file transfer :("))
    87  
    88  			_, _, err := guestManager.DownloadFileInGuest(context.TODO(), "MYPATH")
    89  			Expect(err).To(HaveOccurred())
    90  			Expect(err).To(MatchError("vcenter_client - unable to download file: couldn't initiate file transfer :("))
    91  		})
    92  
    93  		It("returns an error if TransferURL fails", func() {
    94  			fileManager.InitiateFileTransferFromGuestReturns(&types.FileTransferInformation{Url: "my.dude.edu"}, nil)
    95  			fileManager.TransferURLReturns(nil, errors.New("couldn't initiate file transfer :("))
    96  
    97  			_, _, err := guestManager.DownloadFileInGuest(context.TODO(), "MYPATH")
    98  			Expect(err).To(HaveOccurred())
    99  			Expect(err).To(MatchError("vcenter_client - unable to download file: couldn't initiate file transfer :("))
   100  		})
   101  
   102  		It("returns an error if Download fails", func() {
   103  			fileManager.InitiateFileTransferFromGuestReturns(&types.FileTransferInformation{Url: "my.dude.edu"}, nil)
   104  			client.DownloadReturns(nil, 0, errors.New("couldn't initiate file transfer :("))
   105  
   106  			_, _, err := guestManager.DownloadFileInGuest(context.TODO(), "MYPATH")
   107  			Expect(err).To(HaveOccurred())
   108  			Expect(err).To(MatchError("vcenter_client - unable to download file: couldn't initiate file transfer :("))
   109  		})
   110  
   111  		It("successfully downloads file", func() {
   112  			fileManager.InitiateFileTransferFromGuestReturns(&types.FileTransferInformation{Url: "my.dude.edu"}, nil)
   113  
   114  			_, _, err := guestManager.DownloadFileInGuest(context.TODO(), "C://PATH")
   115  			Expect(err).ToNot(HaveOccurred())
   116  			Expect(fileManager.InitiateFileTransferFromGuestCallCount()).To(Equal(1))
   117  			Expect(fileManager.TransferURLCallCount()).To(Equal(1))
   118  			Expect(client.DownloadCallCount()).To(Equal(1))
   119  		})
   120  	})
   121  })