github.com/cloudfoundry-incubator/stembuild@v0.0.0-20211223202937-5b61d62226c6/integration/package/package_test.go (about)

     1  package package_test
     2  
     3  import (
     4  	"archive/tar"
     5  	"compress/gzip"
     6  	"crypto/sha1"
     7  	"fmt"
     8  	"io"
     9  	"io/ioutil"
    10  	"math/rand"
    11  	"os"
    12  	"path/filepath"
    13  	"strings"
    14  	"time"
    15  
    16  	"github.com/onsi/gomega/gexec"
    17  
    18  	"github.com/cloudfoundry-incubator/stembuild/test/helpers"
    19  
    20  	"github.com/vmware/govmomi/govc/cli"
    21  
    22  	. "github.com/onsi/ginkgo"
    23  	. "github.com/onsi/gomega"
    24  	_ "github.com/vmware/govmomi/govc/vm"
    25  )
    26  
    27  var _ = Describe("Package", func() {
    28  	var (
    29  		workingDir                string
    30  		vmPath                    string
    31  		vcenterURL                string
    32  		vcenterAdminCredentialUrl string
    33  		vcenterStembuildUsername  string
    34  		vcenterStembuildPassword  string
    35  		err                       error
    36  	)
    37  
    38  	BeforeEach(func() {
    39  		vcenterFolder := helpers.EnvMustExist(vcenterFolderVariable)
    40  
    41  		rand.Seed(time.Now().UnixNano())
    42  		packageTestVMName := fmt.Sprintf("stembuild-package-test-%d", rand.Int())
    43  
    44  		baseVMWithPath := fmt.Sprintf(vcenterFolder + "/" + baseVMName)
    45  		vmPath = strings.Join([]string{vcenterFolder, packageTestVMName}, "/")
    46  
    47  		vcenterAdminCredentialUrl = helpers.EnvMustExist(vcenterAdminCredentialUrlVariable)
    48  
    49  		cli.Run([]string{
    50  			"vm.clone",
    51  			"-vm", baseVMWithPath,
    52  			"-folder", vcenterFolder,
    53  			"-on=false",
    54  			"-u", vcenterAdminCredentialUrl,
    55  			"-tls-ca-certs", pathToCACert,
    56  			packageTestVMName,
    57  		})
    58  
    59  		vcenterURL = helpers.EnvMustExist(vcenterURLVariable)
    60  		vcenterStembuildUsername = helpers.EnvMustExist(vcenterStembuildUsernameVariable)
    61  		vcenterStembuildPassword = helpers.EnvMustExist(vcenterStembuildPasswordVariable)
    62  
    63  		workingDir, err = ioutil.TempDir(os.TempDir(), "stembuild-package-test")
    64  		Expect(err).NotTo(HaveOccurred())
    65  	})
    66  
    67  	AfterEach(func() {
    68  		os.RemoveAll(workingDir)
    69  		if vmPath != "" {
    70  			cli.Run([]string{
    71  				"vm.destroy",
    72  				"-vm.ipath", vmPath,
    73  				"-u", vcenterAdminCredentialUrl,
    74  				"-tls-ca-certs", pathToCACert,
    75  			})
    76  		}
    77  	})
    78  
    79  	It("generates a stemcell with the correct shasum", func() {
    80  		session := helpers.RunCommandInDir(
    81  			workingDir,
    82  			executable, "package",
    83  			"-vcenter-url", vcenterURL,
    84  			"-vcenter-username", vcenterStembuildUsername,
    85  			"-vcenter-password", vcenterStembuildPassword,
    86  			"-vm-inventory-path", vmPath,
    87  			"-vcenter-ca-certs", pathToCACert,
    88  		)
    89  
    90  		Eventually(session, 60*time.Minute, 5*time.Second).Should(gexec.Exit(0))
    91  		var out []byte
    92  		session.Out.Write(out)
    93  		fmt.Print(string(out))
    94  
    95  		expectedOSVersion := strings.Split(stembuildVersion, ".")[0]
    96  		expectedStemcellVersion := strings.Split(stembuildVersion, ".")[:2]
    97  
    98  		expectedFilename := fmt.Sprintf(
    99  			"bosh-stemcell-%s-vsphere-esxi-windows%s-go_agent.tgz", strings.Join(expectedStemcellVersion, "."), expectedOSVersion)
   100  
   101  		stemcellPath := filepath.Join(workingDir, expectedFilename)
   102  
   103  		image, err := os.Create(filepath.Join(workingDir, "image"))
   104  		Expect(err).NotTo(HaveOccurred())
   105  		copyFileFromTar(stemcellPath, "image", image)
   106  
   107  		vmdkSha := sha1.New()
   108  		ovfSha := sha1.New()
   109  
   110  		imageMF, err := os.Create(filepath.Join(workingDir, "image.mf"))
   111  		Expect(err).NotTo(HaveOccurred())
   112  
   113  		copyFileFromTar(filepath.Join(workingDir, "image"), ".mf", imageMF)
   114  		copyFileFromTar(filepath.Join(workingDir, "image"), ".vmdk", vmdkSha)
   115  		copyFileFromTar(filepath.Join(workingDir, "image"), ".ovf", ovfSha)
   116  
   117  		imageMFFile, err := helpers.ReadFile(filepath.Join(workingDir, "image.mf"))
   118  		Expect(err).NotTo(HaveOccurred())
   119  		Expect(imageMFFile).To(ContainSubstring(fmt.Sprintf("%x", vmdkSha.Sum(nil))))
   120  		Expect(imageMFFile).To(ContainSubstring(fmt.Sprintf("%x", ovfSha.Sum(nil))))
   121  
   122  		By("and the stemcell manifest specifies agent api_version 3", func() {
   123  			stemcellManifestPath, err := os.Create(filepath.Join(workingDir, "stemcell.MF"))
   124  			Expect(err).NotTo(HaveOccurred())
   125  			copyFileFromTar(stemcellPath, "stemcell.MF", stemcellManifestPath)
   126  
   127  			stemcellManifest, err := helpers.ReadFile(stemcellManifestPath.Name())
   128  			Expect(err).NotTo(HaveOccurred())
   129  			Expect(stemcellManifest).To(ContainSubstring("api_version: 3"))
   130  		})
   131  	})
   132  
   133  	It("generates a stemcell with a patch number when specified", func() {
   134  		session := helpers.RunCommandInDir(
   135  			workingDir,
   136  			executable, "package",
   137  			"-vcenter-url", vcenterURL,
   138  			"-vcenter-username", vcenterStembuildUsername,
   139  			"-vcenter-password", vcenterStembuildPassword,
   140  			"-vm-inventory-path", vmPath,
   141  			"-patch-version", "5",
   142  			"-vcenter-ca-certs", pathToCACert,
   143  		)
   144  
   145  		Eventually(session, 60*time.Minute, 5*time.Second).Should(gexec.Exit(0))
   146  		var out []byte
   147  		session.Out.Write(out)
   148  		fmt.Print(string(out))
   149  
   150  		expectedOSVersion := strings.Split(stembuildVersion, ".")[0]
   151  		expectedStemcellVersion := strings.Split(stembuildVersion, ".")[:2]
   152  		expectedStemcellVersion = append(expectedStemcellVersion, "5")
   153  
   154  		expectedFilename := fmt.Sprintf(
   155  			"bosh-stemcell-%s-vsphere-esxi-windows%s-go_agent.tgz", strings.Join(expectedStemcellVersion, "."), expectedOSVersion)
   156  
   157  		stemcellPath := filepath.Join(workingDir, expectedFilename)
   158  		Expect(stemcellPath).To(BeAnExistingFile())
   159  
   160  		stemcellManifestPath, err := os.Create(filepath.Join(workingDir, "stemcell.MF"))
   161  		Expect(err).NotTo(HaveOccurred())
   162  
   163  		copyFileFromTar(stemcellPath, "stemcell.MF", stemcellManifestPath)
   164  
   165  		stemcellManifest, err := helpers.ReadFile(stemcellManifestPath.Name())
   166  		Expect(err).NotTo(HaveOccurred())
   167  		Expect(stemcellManifest).To(ContainSubstring(strings.Join(expectedStemcellVersion, ".")))
   168  	})
   169  })
   170  
   171  func copyFileFromTar(t string, f string, w io.Writer) {
   172  	z, err := os.OpenFile(t, os.O_RDONLY, 0777)
   173  	Expect(err).NotTo(HaveOccurred())
   174  	gzr, err := gzip.NewReader(z)
   175  	Expect(err).NotTo(HaveOccurred())
   176  	defer gzr.Close()
   177  
   178  	r := tar.NewReader(gzr)
   179  	for {
   180  		header, err := r.Next()
   181  		if err == io.EOF {
   182  			break
   183  		}
   184  
   185  		if strings.Contains(header.Name, f) {
   186  			_, err = io.Copy(w, r)
   187  			Expect(err).NotTo(HaveOccurred())
   188  		}
   189  	}
   190  }