Skip to content

Commit

Permalink
Update tests and refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
TisVictress committed Apr 17, 2024
1 parent a365932 commit 313d6cd
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 52 deletions.
17 changes: 6 additions & 11 deletions postal/internal/dependency_mirror.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,14 @@ func (d DependencyMirrorResolver) FindDependencyMirror(uri, platformDir string)

func (d DependencyMirrorResolver) findMirrorFromEnv(uri string) (string, error) {
const DefaultMirror = "BP_DEPENDENCY_MIRROR"
const DefaultMirrorPrefix = "BP_DEPENDENCY_MIRROR_"
const NonDefaultMirrorPrefix = "BP_DEPENDENCY_MIRROR_"
mirrors := make(map[string]string)
environmentVariables := os.Environ()
for _, ev := range environmentVariables {
idx := strings.Index(ev, "=")
if idx < 0 {
continue
}
pair := strings.SplitN(ev, "=", 2)
key := pair[0]
value := pair[1]

key := ev[:idx]
value := ev[idx+1:]
if !strings.Contains(key, DefaultMirror) {
continue
}
Expand All @@ -81,10 +78,8 @@ func (d DependencyMirrorResolver) findMirrorFromEnv(uri string) (string, error)
}

// convert key
hostnameIdx := len(DefaultMirrorPrefix)
hostname := key[hostnameIdx:]
hostname = strings.ReplaceAll(hostname, "_", ".")
hostname = strings.ReplaceAll(hostname, "__", "-")
hostname := strings.SplitN(key, NonDefaultMirrorPrefix, 2)[1]
hostname = strings.ReplaceAll(strings.ReplaceAll(hostname, "__", "-"), "_", ".")
hostname = strings.ToLower(hostname)
mirrors[hostname] = value

Expand Down
112 changes: 92 additions & 20 deletions postal/internal/dependency_mirror_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ func testDependencyMirror(t *testing.T, context spec.G, it spec.S) {

bindingResolver = &fakes.BindingResolver{}
resolver = internal.NewDependencyMirrorResolver(bindingResolver)
Expect(err).NotTo(HaveOccurred())

bindingResolver.ResolveCall.Returns.BindingSlice = []servicebindings.Binding{
{
Expand Down Expand Up @@ -91,21 +90,49 @@ func testDependencyMirror(t *testing.T, context spec.G, it spec.S) {
Expect(boundDependency).To(Equal("https://mirror.example.org/public-github"))
})
})

context("given a specific hostname binding and no default mirror binding", func() {
it.Before(func() {
Expect(os.Remove(filepath.Join(tmpDir, "default")))
Expect(os.WriteFile(filepath.Join(tmpDir, "github.com"), []byte("https://mirror.example.org/public-github"), os.ModePerm))
Expect(os.WriteFile(filepath.Join(tmpDir, "nodejs.org"), []byte("https://mirror.example.org/node-dist"), os.ModePerm))

bindingResolver.ResolveCall.Returns.BindingSlice[0].Entries = map[string]*servicebindings.Entry{
"github.com": servicebindings.NewEntry(filepath.Join(tmpDir, "github.com")),
"nodejs.org": servicebindings.NewEntry(filepath.Join(tmpDir, "nodejs.org")),
}
})

it("return empty string for non specific hostnames", func() {
boundDependency, err := resolver.FindDependencyMirror("some-uri", "some-platform-dir")
Expect(err).ToNot(HaveOccurred())
Expect(bindingResolver.ResolveCall.Receives.Typ).To(Equal("dependency-mirror"))
Expect(bindingResolver.ResolveCall.Receives.Provider).To(BeEmpty())
Expect(bindingResolver.ResolveCall.Receives.PlatformDir).To(Equal("some-platform-dir"))
Expect(boundDependency).To(Equal(""))
})

it("finds the mirror matching the specific hostname in the given uri", func() {
boundDependency, err := resolver.FindDependencyMirror("some-nodejs.org-uri", "some-platform-dir")
Expect(err).ToNot(HaveOccurred())
Expect(bindingResolver.ResolveCall.Receives.Typ).To(Equal("dependency-mirror"))
Expect(bindingResolver.ResolveCall.Receives.Provider).To(BeEmpty())
Expect(bindingResolver.ResolveCall.Receives.PlatformDir).To(Equal("some-platform-dir"))
Expect(boundDependency).To(Equal("https://mirror.example.org/node-dist"))
})
})
})

context("via environment variables", func() {
it.Before(func() {
err = os.Setenv("BP_DEPENDENCY_MIRROR", "https://mirror.example.org/{originalHost}")
Expect(err).NotTo(HaveOccurred())
Expect(os.Setenv("BP_DEPENDENCY_MIRROR", "https://mirror.example.org/{originalHost}"))

bindingResolver = &fakes.BindingResolver{}
resolver = internal.NewDependencyMirrorResolver(bindingResolver)
Expect(err).NotTo(HaveOccurred())
})

it.After(func() {
err = os.Unsetenv("BP_DEPENDENCY_MIRROR")
Expect(err).NotTo(HaveOccurred())
Expect(os.Unsetenv("BP_DEPENDENCY_MIRROR"))
})

context("given the default mirror environment variable is set", func() {
Expand All @@ -118,17 +145,13 @@ func testDependencyMirror(t *testing.T, context spec.G, it spec.S) {

context("given environment variables for a default mirror and specific hostname mirrors", func() {
it.Before(func() {
err = os.Setenv("BP_DEPENDENCY_MIRROR_GITHUB_COM", "https://mirror.example.org/public-github")
Expect(err).NotTo(HaveOccurred())
err = os.Setenv("BP_DEPENDENCY_MIRROR_NODEJS_ORG", "https://mirror.example.org/node-dist")
Expect(err).NotTo(HaveOccurred())
Expect(os.Setenv("BP_DEPENDENCY_MIRROR_GITHUB_COM", "https://mirror.example.org/public-github"))
Expect(os.Setenv("BP_DEPENDENCY_MIRROR_TESTING_123__ABC", "https://mirror.example.org/testing"))
})

it.After(func() {
err = os.Unsetenv("BP_DEPENDENCY_MIRROR_GITHUB_COM")
Expect(err).NotTo(HaveOccurred())
err = os.Unsetenv("BP_DEPENDENCY_MIRROR_NODEJS_ORG")
Expect(err).NotTo(HaveOccurred())
Expect(os.Unsetenv("BP_DEPENDENCY_MIRROR_GITHUB_COM"))
Expect(os.Unsetenv("BP_DEPENDENCY_MIRROR_TESTING_123__ABC"))
})

it("finds the default mirror when given uri does not match a specific hostname", func() {
Expand All @@ -142,6 +165,59 @@ func testDependencyMirror(t *testing.T, context spec.G, it spec.S) {
Expect(err).ToNot(HaveOccurred())
Expect(boundDependency).To(Equal("https://mirror.example.org/public-github"))
})

it("properly decodes the hostname", func() {
boundDependency, err := resolver.FindDependencyMirror("testing.123-abc-uri", "some-platform-dir")
Expect(err).ToNot(HaveOccurred())
Expect(boundDependency).To(Equal("https://mirror.example.org/testing"))
})
})

context("given environment variables for a specific hostname and none for a default mirror", func() {
it.Before(func() {
Expect(os.Unsetenv("BP_DEPENDENCY_MIRROR"))
Expect(os.Setenv("BP_DEPENDENCY_MIRROR_GITHUB_COM", "https://mirror.example.org/public-github"))
})

it.After(func() {
Expect(os.Unsetenv("BP_DEPENDENCY_MIRROR_GITHUB_COM"))
})

it("return empty string for non specific hostnames", func() {
boundDependency, err := resolver.FindDependencyMirror("some-uri", "some-platform-dir")
Expect(err).ToNot(HaveOccurred())
Expect(boundDependency).To(Equal(""))
})

it("finds the mirror matching the specific hostname in the given uri", func() {
boundDependency, err := resolver.FindDependencyMirror("some-github.com-uri", "some-platform-dir")
Expect(err).ToNot(HaveOccurred())
Expect(boundDependency).To(Equal("https://mirror.example.org/public-github"))
})
})
})

context("when mirror is provided by both bindings and environment variables", func() {
it.Before(func() {
tmpDir, err = os.MkdirTemp("", "dependency-mirror")
Expect(err).NotTo(HaveOccurred())
Expect(os.WriteFile(filepath.Join(tmpDir, "default"), []byte("https://mirror.example.org/{originalHost}"), os.ModePerm))
Expect(os.WriteFile(filepath.Join(tmpDir, "type"), []byte("dependency-mirror"), os.ModePerm))

Expect(os.Setenv("BP_DEPENDENCY_MIRROR", "https://mirror.other-example.org/{originalHost}"))
})

it.After(func() {
Expect(os.RemoveAll(tmpDir)).To(Succeed())
Expect(os.Unsetenv("BP_DEPENDENCY_MIRROR"))
})

it("defaults to environment variable and ignores binding", func() {
boundDependency, err := resolver.FindDependencyMirror("some-uri", "some-platform-dir")
Expect(err).ToNot(HaveOccurred())
Expect(boundDependency).NotTo(Equal("https://mirror.example.org/some-uri"))
Expect(boundDependency).To(Equal("https://mirror.other-example.org/some-uri"))

})
})

Expand All @@ -156,7 +232,6 @@ func testDependencyMirror(t *testing.T, context spec.G, it spec.S) {

bindingResolver = &fakes.BindingResolver{}
resolver = internal.NewDependencyMirrorResolver(bindingResolver)
Expect(err).NotTo(HaveOccurred())
})

it.After(func() {
Expand Down Expand Up @@ -190,17 +265,14 @@ func testDependencyMirror(t *testing.T, context spec.G, it spec.S) {

context("when mirror contains invalid scheme", func() {
it.Before(func() {
err = os.Setenv("BP_DEPENDENCY_MIRROR", "http://mirror.example.org/{originalHost}")
Expect(err).NotTo(HaveOccurred())
Expect(os.Setenv("BP_DEPENDENCY_MIRROR", "http://mirror.example.org/{originalHost}"))

bindingResolver = &fakes.BindingResolver{}
resolver = internal.NewDependencyMirrorResolver(bindingResolver)
Expect(err).NotTo(HaveOccurred())
})

it.After(func() {
err = os.Unsetenv("BP_DEPENDENCY_MIRROR")
Expect(err).NotTo(HaveOccurred())
Expect(os.Unsetenv("BP_DEPENDENCY_MIRROR"))
})

it("returns an error", func() {
Expand Down
20 changes: 11 additions & 9 deletions postal/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,11 @@ func stringSliceElementCount(slice []string, str string) int {
// location of the CNBPath is given so that dependencies that may be included
// in a buildpack when packaged for offline consumption can be retrieved. If
// there is a dependency mapping for the specified dependency, Deliver will use
// the given dependency mapping URI to fetch the dependency. The dependency is
// validated against the checksum value provided on the Dependency and will
// error if there are inconsistencies in the fetched result.
// the given dependency mapping URI to fetch the dependency. If there is a
// dependency mirror for the specified dependency, Deliver will use the mirror
// URI to fetch the dependency. The dependency is validated against the checksum
// value provided on the Dependency and will error if there are inconsistencies
// in the fetched result.
func (s Service) Deliver(dependency Dependency, cnbPath, layerPath, platformPath string) error {
dependencyChecksum := dependency.Checksum
if dependency.SHA256 != "" {
Expand All @@ -264,15 +266,15 @@ func (s Service) Deliver(dependency Dependency, cnbPath, layerPath, platformPath
return fmt.Errorf("failure checking for dependency mappings: %s", err)
}

if dependencyMirrorURI != "" && dependencyMappingURI != "" {
return fmt.Errorf("dependency mappings should not exist when using a dependency mirror")
}
//if dependencyMirrorURI != "" && dependencyMappingURI != "" {
// // There is no logger for postal?
// // warning: Both dependency mirror and bindings are present.
// // Please remove dependency map bindings if you wish to use the mirror.
//}

if dependencyMappingURI != "" {
dependency.URI = dependencyMappingURI
}

if dependencyMirrorURI != "" {
} else if dependencyMirrorURI != "" {
dependency.URI = dependencyMirrorURI
}

Expand Down
24 changes: 12 additions & 12 deletions postal/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1051,18 +1051,18 @@ version = "1.2.3"
})
})

context("when both dependency mapping and dependency mirror are activated", func() {
it.Before(func() {
mappingResolver.FindDependencyMappingCall.Returns.String = "dependency-mapping-entry.tgz"
mirrorResolver.FindDependencyMirrorCall.Returns.String = "dependency-mirror-url"
})

it("deliver fails with appropriate error message", func() {
err := deliver()

Expect(err).To(MatchError(ContainSubstring("dependency mappings should not exist when using a dependency mirror")))
})
})
//context("when both dependency mapping and dependency mirror are activated", func() {
// it.Before(func() {
// mappingResolver.FindDependencyMappingCall.Returns.String = "dependency-mapping-entry.tgz"
// mirrorResolver.FindDependencyMirrorCall.Returns.String = "dependency-mirror-url"
// })
//
// it("deliver fails with appropriate error message", func() { //should show a warning instead
// err := deliver()
//
// Expect(err).To(MatchError(ContainSubstring("dependency mappings should not exist when using a dependency mirror")))
// })
//})
})
})

Expand Down

0 comments on commit 313d6cd

Please sign in to comment.