Skip to content
This repository was archived by the owner on Oct 11, 2025. It is now read-only.

Commit ee3ded5

Browse files
author
Micah Young
committed
Change StemcellStore to return local image paths when they exist
- Fix test polution, use explicit configs when needed
1 parent 6b445f5 commit ee3ded5

File tree

10 files changed

+66
-35
lines changed

10 files changed

+66
-35
lines changed

src/bosh-vmrun-cpi/action/create_stemcell.go

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,45 +26,43 @@ func NewCreateStemcellMethod(driverClient driver.Client, stemcellClient stemcell
2626
}
2727

2828
func (c CreateStemcellMethod) CreateStemcell(localImagePath string, stemcellCloudProps apiv1.StemcellCloudProps) (apiv1.StemcellCID, error) {
29+
defer c.stemcellStore.Cleanup()
30+
2931
stemcellUuid, _ := c.uuidGen.Generate()
3032
stemcellId := "cs-" + stemcellUuid
3133
stemcellCID := apiv1.NewStemcellCID(stemcellUuid)
32-
imagePath := ""
34+
storeImagePath := ""
3335

34-
c.logger.DebugWithDetails("cpi", "stemcellCloudProps:", stemcellCloudProps)
36+
c.logger.Debug("cpi", "stemcellCloudProps: %#+v", stemcellCloudProps)
3537
stemcellProps, err := stemcell.NewStemcellProps(stemcellCloudProps)
3638
if err != nil {
3739
return stemcellCID, err
3840
}
3941

40-
c.logger.DebugWithDetails("cpi", "LocalImagePath:", localImagePath)
42+
c.logger.Debug("cpi", "LocalImagePath: %s", localImagePath)
43+
44+
storeImagePath, err = c.stemcellStore.GetByImagePathMapping(localImagePath)
45+
if err != nil {
46+
c.logger.Debug("cpi", "Stemcell image found at path:", localImagePath)
47+
48+
return stemcellCID, err
49+
}
4150

42-
if c.fs.FileExists(localImagePath) {
43-
imagePath = localImagePath
44-
} else {
45-
defer c.stemcellStore.Cleanup()
46-
storeImagePath, err := c.stemcellStore.GetByImagePathMapping(localImagePath)
51+
if storeImagePath == "" {
52+
storeImagePath, err = c.stemcellStore.GetByMetadata(stemcellProps.Name, stemcellProps.Version)
4753
if err != nil {
54+
c.logger.Debug("cpi", "Stemcell image found by metadata")
4855
return stemcellCID, err
4956
}
57+
}
5058

51-
if storeImagePath == "" {
52-
storeImagePath, err = c.stemcellStore.GetByMetadata(stemcellProps.Name, stemcellProps.Version)
53-
if err != nil {
54-
return stemcellCID, err
55-
}
56-
}
57-
58-
c.logger.DebugWithDetails("cpi", "StoreImagePath:", storeImagePath)
59+
c.logger.DebugWithDetails("cpi", "StoreImagePath:", storeImagePath)
5960

60-
if c.fs.FileExists(storeImagePath) {
61-
imagePath = storeImagePath
62-
} else {
63-
return stemcellCID, errors.New("stemcell image not found locally or in image store")
64-
}
61+
if storeImagePath == "" {
62+
return stemcellCID, errors.New("stemcell image not found locally or in image store")
6563
}
6664

67-
ovfPath, err := c.stemcellClient.ExtractOvf(imagePath)
65+
ovfPath, err := c.stemcellClient.ExtractOvf(storeImagePath)
6866
if err != nil {
6967
return stemcellCID, err
7068
}

src/bosh-vmrun-cpi/action/create_stemcell_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ var _ = Describe("CreateStemcell", func() {
4242
err = fs.MkdirAll("image-path", 0777)
4343
Expect(err).ToNot(HaveOccurred())
4444

45+
localImagePath := "/path/to/image"
46+
47+
stemcellStore.GetByImagePathMappingReturns(localImagePath, nil)
4548
stemcellClient.ExtractOvfReturns("extracted-path", nil)
4649

4750
var resourceCloudProps apiv1.CloudPropsImpl
4851
json.Unmarshal([]byte(`{}`), &resourceCloudProps)
4952

50-
imageFile, _ := fs.TempFile("image")
51-
localImagePath := imageFile.Name()
52-
5353
cid, err := m.CreateStemcell(localImagePath, resourceCloudProps)
5454
Expect(err).ToNot(HaveOccurred())
5555

@@ -63,15 +63,15 @@ var _ = Describe("CreateStemcell", func() {
6363
Expect(stemcellClient.CleanupCallCount()).To(Equal(1))
6464
})
6565

66-
It("uses the stemcell store", func() {
66+
It("uses the stemcell store if supplied image path does not exist", func() {
6767
var err error
6868

6969
err = fs.MkdirAll("image-path", 0777)
7070
Expect(err).ToNot(HaveOccurred())
7171

72-
imageFile, _ := fs.TempFile("image")
73-
storeImagePath := imageFile.Name()
72+
storeImagePath := "/path/to/store/image"
7473

74+
stemcellStore.GetByImagePathMappingReturns("", nil)
7575
stemcellStore.GetByMetadataReturns(storeImagePath, nil)
7676
stemcellClient.ExtractOvfReturns("extracted-path", nil)
7777

src/bosh-vmrun-cpi/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ require (
2121
golang.org/x/crypto v0.0.0-20190513172903-22d7a77e9e5f
2222
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4 // indirect
2323
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
24-
gopkg.in/yaml.v2 v2.2.2 // indirect
24+
gopkg.in/yaml.v2 v2.2.2
2525
)
2626

2727
go 1.13

src/bosh-vmrun-cpi/go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ github.com/charlievieth/fs v0.0.0-20170613215519-7dc373669fa1/go.mod h1:sAoA1zHC
77
github.com/cloudfoundry/bosh-utils v0.0.0-20180226215852-d73926502f7c h1:CMe6bHlZDS8iHIZSgHNiHj1yGXtuOpBH1SM6Tky2ODo=
88
github.com/cloudfoundry/bosh-utils v0.0.0-20180226215852-d73926502f7c/go.mod h1:JCrKwetZGjxbfq1U139TZuXDBfdGLtjOEAfxMWKV/QM=
99
github.com/cloudfoundry/bosh-utils v0.0.0-20191218225925-e190b792a340 h1:yjPS9iDPtWum9yHpm+4fSybSIq7zqriVcs7a87pn+Uw=
10+
github.com/cloudfoundry/bosh-utils v0.0.0-20191228100201-5f094101dfe7 h1:b1YddTIFo/z16pBowCiujI8aK2BLen8r3Pn6lYXtp9k=
1011
github.com/cppforlife/bosh-cpi-go v0.0.0-20171102051050-24a661ddbeef h1:8qA38RJGsce6BJYlolLPYCiS6ZN3ma18g8a1Ad4WrYc=
1112
github.com/cppforlife/bosh-cpi-go v0.0.0-20171102051050-24a661ddbeef/go.mod h1:WkeM0jfnGyGPPqo7JoBL8pwutlzeKgGElbkIV9ieSlU=
1213
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=

src/bosh-vmrun-cpi/integration/cpi_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ var _ = Describe("cpi integration", func() {
2020
cpiBin, err := gexec.Build("bosh-vmrun-cpi/cmd/cpi")
2121
Expect(err).ToNot(HaveOccurred())
2222

23+
generateCPIConfig(CpiConfigPath, DirectCPIConfig)
24+
2325
request := fmt.Sprintf(`{ "method": "info", "arguments": [] }`)
2426

2527
session, stdin := GexecCommandWithStdin(cpiBin, "-configPath", CpiConfigPath)

src/bosh-vmrun-cpi/integration/driver_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ var _ = Describe("driver integration", func() {
3838
retryFileLock := driver.NewRetryFileLock(logger)
3939
vmxBuilder = vmx.NewVmxBuilder(logger)
4040

41+
generateCPIConfig(CpiConfigPath, DirectCPIConfig)
4142
cpiConfigJson, err := fs.ReadFileString(CpiConfigPath)
4243
Expect(err).ToNot(HaveOccurred())
4344
cpiConfig, err := cpiconfig.NewConfigFromJson(cpiConfigJson)

src/bosh-vmrun-cpi/integration/installer_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,6 @@ var _ = Describe("installer integration", func() {
143143
nonExistantDirsSSHConfig := SSHCPIConfig
144144
nonExistantDirsSSHConfig.VmStorePath = template.JSEscapeString(notPrexistingVmStoreDir)
145145
nonExistantDirsSSHConfig.StemcellStorePath = template.JSEscapeString(notPrexistingStemcellStoreDir)
146-
VmStoreDir = notPrexistingVmStoreDir
147-
StemcellStoreDir = notPrexistingStemcellStoreDir
148146
generateCPIConfig(CpiConfigPath, nonExistantDirsSSHConfig)
149147
})
150148

@@ -205,9 +203,9 @@ version: '97.16'
205203

206204
Eventually(session).Should(gexec.Exit(0))
207205

208-
expectedStemcellPath := filepath.Join(StemcellStoreDir, "bosh-stemcell-97.16-vsphere-esxi-ubuntu-xenial-go_agent.tgz")
206+
expectedStemcellPath := filepath.Join(notPrexistingStemcellStoreDir, "bosh-stemcell-97.16-vsphere-esxi-ubuntu-xenial-go_agent.tgz")
209207
Expect(expectedStemcellPath).To(BeAnExistingFile())
210-
expectedMappingPath := filepath.Join(StemcellStoreDir, "mappings", fmt.Sprintf("%x.mapping", sha1.Sum([]byte(stemcellImagePath))))
208+
expectedMappingPath := filepath.Join(notPrexistingStemcellStoreDir, "mappings", fmt.Sprintf("%x.mapping", sha1.Sum([]byte(stemcellImagePath))))
211209
Expect(expectedMappingPath).To(BeAnExistingFile())
212210

213211
// running again will reuse stemcell but regenerate mapping

src/bosh-vmrun-cpi/integration/integration_suite_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,6 @@ var _ = BeforeEach(func() {
245245
configFile.Close()
246246

247247
CpiConfigPath, err = filepath.Abs(configFile.Name())
248-
generateCPIConfig(CpiConfigPath, DirectCPIConfig)
249248
})
250249

251250
var _ = AfterEach(func() {

src/bosh-vmrun-cpi/integration/stemcell_store_test.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package integration_test
22

33
import (
4+
"io/ioutil"
45
"os"
56

67
. "github.com/onsi/ginkgo"
@@ -17,6 +18,8 @@ import (
1718
var _ = Describe("StemcellStore integration", func() {
1819
var stemcellStore stemcell.StemcellStore
1920
BeforeEach(func() {
21+
generateCPIConfig(CpiConfigPath, DirectCPIConfig)
22+
2023
logger := boshlog.NewWriterLogger(boshlog.LevelWarn, os.Stderr)
2124
fs := boshsys.NewOsFileSystem(logger)
2225

@@ -44,14 +47,39 @@ var _ = Describe("StemcellStore integration", func() {
4447

4548
Expect(stemcellImagePath).To(BeAnExistingFile())
4649
})
50+
51+
It("returns empty when metadata doesnt match", func() {
52+
stemcellImagePath, err := stemcellStore.GetByMetadata("non-existant-stemcell", "1234.5")
53+
Expect(err).ToNot(HaveOccurred())
54+
55+
Expect(stemcellImagePath).To(Equal(""))
56+
})
4757
})
4858

4959
Context("GetByImagePathMapping", func() {
50-
It("finds stemcells by image path mapping", func() {
60+
It("finds stemcells when path exists", func() {
61+
tmpImageFile, err := ioutil.TempFile("", "image")
62+
Expect(err).ToNot(HaveOccurred())
63+
defer os.RemoveAll(tmpImageFile.Name())
64+
65+
stemcellImagePath, err := stemcellStore.GetByImagePathMapping(tmpImageFile.Name())
66+
Expect(err).ToNot(HaveOccurred())
67+
68+
Expect(stemcellImagePath).To(Equal(tmpImageFile.Name()))
69+
})
70+
71+
It("finds stemcells when path does not exist but image path mapping exists", func() {
5172
stemcellImagePath, err := stemcellStore.GetByImagePathMapping("/test-stemcell-tmp-image-path")
5273
Expect(err).ToNot(HaveOccurred())
5374

5475
Expect(stemcellImagePath).To(BeAnExistingFile())
5576
})
77+
78+
It("returns empty when path and mapping don't exist", func() {
79+
stemcellImagePath, err := stemcellStore.GetByImagePathMapping("/non-existing/image")
80+
Expect(err).ToNot(HaveOccurred())
81+
82+
Expect(stemcellImagePath).To(Equal(""))
83+
})
5684
})
5785
})

src/bosh-vmrun-cpi/stemcell/store.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ func (s stemcellStoreImpl) GetByImagePathMapping(imagePath string) (string, erro
3333
var err error
3434
var extractedImagePath string
3535

36+
if s.fs.FileExists(imagePath) {
37+
return imagePath, nil
38+
}
39+
3640
expectedMappingFileName := fmt.Sprintf("%x.mapping", sha1.Sum([]byte(imagePath)))
3741
expectedMappingPath := filepath.Join(s.storePath, "mappings", expectedMappingFileName)
3842
var stemcellTarballPathBytes []byte

0 commit comments

Comments
 (0)