Skip to content

Commit 147c4b8

Browse files
committedJan 30, 2020
feat(appr): exit build early if no manifests were downloaded
1 parent e517d18 commit 147c4b8

File tree

2 files changed

+93
-49
lines changed

2 files changed

+93
-49
lines changed
 

‎pkg/appregistry/builder.go

+14
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"compress/gzip"
77
"context"
88
"database/sql"
9+
"fmt"
910
"io"
1011
"io/ioutil"
1112
"os"
@@ -90,6 +91,11 @@ func (b *AppregistryImageBuilder) Build() error {
9091
if err := downloader.DownloadManifests(b.ManifestDir, b.AppRegistryOrg); err != nil {
9192
return err
9293
}
94+
95+
if !hasManifests(b.ManifestDir) {
96+
return fmt.Errorf("no manifests downloaded from appregistry %s/%s", b.AppRegistryEndpoint, b.AppRegistryOrg)
97+
}
98+
9399
klog.V(4).Infof("downloaded manifests to %s\n", b.ManifestDir)
94100

95101
if err := BuildDatabase(b.ManifestDir, b.DatabasePath); err != nil {
@@ -212,3 +218,11 @@ func BuildLayer(directory string) (string, error) {
212218

213219
return archive.Name(), nil
214220
}
221+
222+
func hasManifests(path string) bool {
223+
files, err := ioutil.ReadDir(path)
224+
if err != nil {
225+
return false
226+
}
227+
return len(files) > 0
228+
}

‎pkg/appregistry/builder_test.go

+79-49
Original file line numberDiff line numberDiff line change
@@ -20,63 +20,93 @@ var _ = Describe("Image building", func() {
2020
operator *apprclient.OperatorMetadata
2121
)
2222

23-
JustBeforeEach(func() {
24-
var err error
25-
builder, err = NewAppregistryImageBuilder(options...)
26-
Expect(err).ToNot(HaveOccurred())
27-
Expect(builder).ToNot(BeNil())
28-
Expect(builder.Build()).To(Succeed())
29-
})
23+
Context("with no manifests returned", func() {
24+
BeforeEach(func() {
25+
var noopAppender ImageAppendFunc = func(from, to, layer string) error {
26+
return nil
27+
}
3028

31-
BeforeEach(func() {
32-
var noopAppender ImageAppendFunc = func(from, to, layer string) error {
33-
return nil
34-
}
35-
36-
client := &apprclientfakes.FakeClient{}
37-
pkg = &apprclient.RegistryMetadata{
38-
Namespace: "marsupials",
39-
Name: "koala",
40-
}
41-
client.ListPackagesReturns([]*apprclient.RegistryMetadata{pkg}, nil)
42-
43-
pkgBlob, err := ioutil.ReadFile("testdata/golden/marsupials_pkg.tar")
44-
Expect(err).ToNot(HaveOccurred())
45-
46-
operator = &apprclient.OperatorMetadata{
47-
RegistryMetadata: *pkg,
48-
Blob: pkgBlob,
49-
}
50-
client.RetrieveOneReturns(operator, nil)
51-
52-
options = append(options,
53-
WithAppender(noopAppender),
54-
WithAppRegistryOrg("metatheria"),
55-
WithClient(client),
56-
)
57-
})
29+
client := &apprclientfakes.FakeClient{}
30+
client.RetrieveOneReturns(nil, nil)
5831

59-
Context("with a custom cache dir", func() {
60-
var (
61-
cacheDir string
62-
manifestsGlob string
63-
)
32+
options = append(options,
33+
WithAppender(noopAppender),
34+
WithAppRegistryOrg("metatheria"),
35+
WithClient(client),
36+
)
37+
})
6438

65-
BeforeEach(func() {
66-
cacheDir = filepath.Join("testdata", "custom-cache")
67-
options = append(options, WithCacheDir(cacheDir))
68-
manifestsGlob = filepath.Join(cacheDir, "manifests-*/koala/marsupials")
39+
It("should show an error", func() {
40+
var err error
41+
builder, err = NewAppregistryImageBuilder(options...)
42+
Expect(err).ToNot(HaveOccurred())
43+
Expect(builder).ToNot(BeNil())
44+
err = builder.Build()
45+
Expect(err).To(HaveOccurred())
46+
Expect(err.Error()).To(Equal("no manifests downloaded from appregistry https://quay.io/cnr/metatheria"))
6947
})
48+
})
49+
50+
Context("with good manifests returned", func() {
51+
BeforeEach(func() {
52+
var noopAppender ImageAppendFunc = func(from, to, layer string) error {
53+
return nil
54+
}
55+
56+
client := &apprclientfakes.FakeClient{}
57+
pkg = &apprclient.RegistryMetadata{
58+
Namespace: "marsupials",
59+
Name: "koala",
60+
}
61+
client.ListPackagesReturns([]*apprclient.RegistryMetadata{pkg}, nil)
7062

71-
AfterEach(func() {
72-
Expect(os.RemoveAll(cacheDir)).To(Succeed())
63+
pkgBlob, err := ioutil.ReadFile("testdata/golden/marsupials_pkg.tar")
64+
Expect(err).ToNot(HaveOccurred())
65+
66+
operator = &apprclient.OperatorMetadata{
67+
RegistryMetadata: *pkg,
68+
Blob: pkgBlob,
69+
}
70+
client.RetrieveOneReturns(operator, nil)
71+
72+
options = append(options,
73+
WithAppender(noopAppender),
74+
WithAppRegistryOrg("metatheria"),
75+
WithClient(client),
76+
)
7377
})
7478

75-
It("should retain unpacked operator manifests", func() {
76-
Expect(cacheDir).To(BeADirectory())
77-
Expect(filepath.Glob(filepath.Join(manifestsGlob, "package.yaml"))).To(HaveLen(1))
78-
Expect(filepath.Glob(filepath.Join(manifestsGlob, "v1.0.0", "koala.v1.0.0.clusterserviceversion.yaml"))).To(HaveLen(1))
79+
JustBeforeEach(func() {
80+
var err error
81+
builder, err = NewAppregistryImageBuilder(options...)
82+
Expect(err).ToNot(HaveOccurred())
83+
Expect(builder).ToNot(BeNil())
84+
Expect(builder.Build()).To(Succeed())
7985
})
8086

87+
Context("with a custom cache dir", func() {
88+
var (
89+
cacheDir string
90+
manifestsGlob string
91+
)
92+
93+
BeforeEach(func() {
94+
cacheDir = filepath.Join("testdata", "custom-cache")
95+
options = append(options, WithCacheDir(cacheDir))
96+
manifestsGlob = filepath.Join(cacheDir, "manifests-*/koala/marsupials")
97+
})
98+
99+
AfterEach(func() {
100+
Expect(os.RemoveAll(cacheDir)).To(Succeed())
101+
})
102+
103+
It("should retain unpacked operator manifests", func() {
104+
Expect(cacheDir).To(BeADirectory())
105+
Expect(filepath.Glob(filepath.Join(manifestsGlob, "package.yaml"))).To(HaveLen(1))
106+
Expect(filepath.Glob(filepath.Join(manifestsGlob, "v1.0.0", "koala.v1.0.0.clusterserviceversion.yaml"))).To(HaveLen(1))
107+
})
108+
109+
})
81110
})
111+
82112
})

0 commit comments

Comments
 (0)
Please sign in to comment.