Skip to content

Commit

Permalink
Close #513
Browse files Browse the repository at this point in the history
* replace deprecated calls to os.ReadFile/WriteFile
* fix "handles empty names gracefully" test: if a property did not make it into dataflow-configuration-metadata.properties then it's not considered, even if it was found in spring-configuration-metadata.json
* add another test: similarly, if you have the properties alpha and beta in spring-configuration-metadata.json, we'll only consider alpha if dataflow-configuration-metadata.properties only has alpha
  • Loading branch information
anthonydahanne committed Oct 23, 2024
1 parent eb32c22 commit d52c2e3
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 78 deletions.
5 changes: 2 additions & 3 deletions boot/configuration_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"archive/zip"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -135,10 +134,10 @@ func DataFlowConfigurationExists(path string) (bool, error) {

func NewDataFlowConfigurationMetadata(path string, metadata ConfigurationMetadata) (ConfigurationMetadata, error) {
file := filepath.Join(path, "META-INF", "dataflow-configuration-metadata.properties")
b, err := ioutil.ReadFile(file)
b, err := os.ReadFile(file)
if os.IsNotExist(err) {
file := filepath.Join(path, "META-INF", "dataflow-configuration-metadata-whitelist.properties")
b, err = ioutil.ReadFile(file)
b, err = os.ReadFile(file)
if os.IsNotExist(err) {
return ConfigurationMetadata{}, nil
} else if err != nil {
Expand Down
139 changes: 64 additions & 75 deletions boot/configuration_metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package boot_test

import (
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand All @@ -38,7 +37,7 @@ func testConfigurationMetadata(t *testing.T, context spec.G, it spec.S) {
it.Before(func() {
var err error

path, err = ioutil.TempDir("", "configuration-metadata")
path, err = os.MkdirTemp("", "configuration-metadata")
Expect(err).NotTo(HaveOccurred())
})

Expand All @@ -47,47 +46,42 @@ func testConfigurationMetadata(t *testing.T, context spec.G, it spec.S) {
})

context("from path", func() {
// ... (existing test cases)

it("returns dataflow decoded contents with names", func() {
Expect(os.MkdirAll(filepath.Join(path, "META-INF"), 0755)).To(Succeed())
Expect(ioutil.WriteFile(filepath.Join(path, "META-INF", "spring-configuration-metadata.json"),
[]byte(`{ "properties": [ { "name": "alpha", "sourceType": "alpha" } ] }`), 0644)).To(Succeed())
Expect(ioutil.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata.properties"),
[]byte("configuration-properties.names=alpha"), 0644)).To(Succeed())
it("returns empty if file does not exist", func() {
Expect(boot.NewConfigurationMetadataFromPath(path)).To(BeZero())
})

cm, err := boot.NewConfigurationMetadataFromPath(path)
Expect(err).NotTo(HaveOccurred())
it("returns decoded contents", func() {
Expect(os.MkdirAll(filepath.Join(path, "META-INF"), 0755)).To(Succeed())
Expect(os.WriteFile(filepath.Join(path, "META-INF", "spring-configuration-metadata.json"),
[]byte(`{ "groups": [ { "name": "alpha" } ] }`), 0644)).To(Succeed())

Expect(boot.NewDataFlowConfigurationMetadata(path, cm)).To(Equal(boot.ConfigurationMetadata{
Properties: []boot.Property{{Name: "alpha", SourceType: "alpha"}},
Expect(boot.NewConfigurationMetadataFromPath(path)).To(Equal(boot.ConfigurationMetadata{
Groups: []boot.Group{{Name: "alpha"}},
}))
})

it("returns combined dataflow decoded contents with classes and names", func() {
it("returns dataflow decoded contents", func() {
Expect(os.MkdirAll(filepath.Join(path, "META-INF"), 0755)).To(Succeed())
Expect(ioutil.WriteFile(filepath.Join(path, "META-INF", "spring-configuration-metadata.json"),
[]byte(`{ "groups": [ { "name": "alpha", "sourceType": "alpha" }, { "name": "beta", "sourceType": "beta" } ] }`), 0644)).To(Succeed())
Expect(ioutil.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata.properties"),
[]byte("configuration-properties.classes=alpha\nconfiguration-properties.names=beta"), 0644)).To(Succeed())
Expect(os.WriteFile(filepath.Join(path, "META-INF", "spring-configuration-metadata.json"),
[]byte(`{ "groups": [ { "name": "alpha", "sourceType": "alpha" } ] }`), 0644))
Expect(os.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata.properties"),
[]byte("configuration-properties.classes=alpha"), 0644))

cm, err := boot.NewConfigurationMetadataFromPath(path)
Expect(err).NotTo(HaveOccurred())

Expect(boot.NewDataFlowConfigurationMetadata(path, cm)).To(Equal(boot.ConfigurationMetadata{
Groups: []boot.Group{
{Name: "alpha", SourceType: "alpha"},
{Name: "beta", SourceType: "beta"},
},
Groups: []boot.Group{{Name: "alpha", SourceType: "alpha"}},
}))
})

it("handles empty names gracefully", func() {
it("returns dataflow decoded contents handling trailing comma correctly", func() {
Expect(os.MkdirAll(filepath.Join(path, "META-INF"), 0755)).To(Succeed())
Expect(ioutil.WriteFile(filepath.Join(path, "META-INF", "spring-configuration-metadata.json"),
[]byte(`{ "properties": [ { "name": "alpha", "sourceType": "alpha" } ] }`), 0644)).To(Succeed())
Expect(ioutil.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata.properties"),
[]byte("configuration-properties.names="), 0644)).To(Succeed())
Expect(os.WriteFile(filepath.Join(path, "META-INF", "spring-configuration-metadata.json"),
[]byte(`{ "properties": [ { "name": "alpha", "sourceType": "alpha" }, { "name": "beta" } ] }`), 0644))
Expect(os.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata.properties"),
[]byte("configuration-properties.classes=alpha,"), 0644))

cm, err := boot.NewConfigurationMetadataFromPath(path)
Expect(err).NotTo(HaveOccurred())
Expand All @@ -96,64 +90,61 @@ func testConfigurationMetadata(t *testing.T, context spec.G, it spec.S) {
Properties: []boot.Property{{Name: "alpha", SourceType: "alpha"}},
}))
})
})


func testConfigurationMetadata(t *testing.T, context spec.G, it spec.S) {
var (
Expect = NewWithT(t).Expect

path string
)

it.Before(func() {
var err error

path, err = ioutil.TempDir("", "configuration-metadata")
Expect(err).NotTo(HaveOccurred())
})

it.After(func() {
Expect(os.RemoveAll(path)).To(Succeed())
})
it("returns dataflow decoded contents", func() {
Expect(os.MkdirAll(filepath.Join(path, "META-INF"), 0755)).To(Succeed())
Expect(os.WriteFile(filepath.Join(path, "META-INF", "spring-configuration-metadata.json"),
[]byte(`{ "groups": [ { "name": "alpha", "sourceType": "alpha" } ] }`), 0644)).To(Succeed())
Expect(os.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata-whitelist.properties"),
[]byte("configuration-properties.classes=alpha"), 0644)).To(Succeed())

context("from path", func() {
cm, err := boot.NewConfigurationMetadataFromPath(path)
Expect(err).NotTo(HaveOccurred())

it("returns empty if file does not exist", func() {
Expect(boot.NewConfigurationMetadataFromPath(path)).To(BeZero())
Expect(boot.NewDataFlowConfigurationMetadata(path, cm)).To(Equal(boot.ConfigurationMetadata{
Groups: []boot.Group{{Name: "alpha", SourceType: "alpha"}},
}))
})

it("returns decoded contents", func() {
it("returns dataflow decoded contents with names", func() {
Expect(os.MkdirAll(filepath.Join(path, "META-INF"), 0755)).To(Succeed())
Expect(ioutil.WriteFile(filepath.Join(path, "META-INF", "spring-configuration-metadata.json"),
[]byte(`{ "groups": [ { "name": "alpha" } ] }`), 0644)).To(Succeed())
Expect(os.WriteFile(filepath.Join(path, "META-INF", "spring-configuration-metadata.json"),
[]byte(`{ "properties": [ { "name": "alpha", "sourceType": "alpha" } ] }`), 0644)).To(Succeed())
Expect(os.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata.properties"),
[]byte("configuration-properties.names=alpha"), 0644)).To(Succeed())

Expect(boot.NewConfigurationMetadataFromPath(path)).To(Equal(boot.ConfigurationMetadata{
Groups: []boot.Group{{Name: "alpha"}},
cm, err := boot.NewConfigurationMetadataFromPath(path)
Expect(err).NotTo(HaveOccurred())

Expect(boot.NewDataFlowConfigurationMetadata(path, cm)).To(Equal(boot.ConfigurationMetadata{
Properties: []boot.Property{{Name: "alpha", SourceType: "alpha"}},
}))
})

it("returns dataflow decoded contents", func() {
it("returns combined dataflow decoded contents with classes and names", func() {
Expect(os.MkdirAll(filepath.Join(path, "META-INF"), 0755)).To(Succeed())
Expect(ioutil.WriteFile(filepath.Join(path, "META-INF", "spring-configuration-metadata.json"),
[]byte(`{ "groups": [ { "name": "alpha", "sourceType": "alpha" } ] }`), 0644))
Expect(ioutil.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata.properties"),
[]byte("configuration-properties.classes=alpha"), 0644))
Expect(os.WriteFile(filepath.Join(path, "META-INF", "spring-configuration-metadata.json"),
[]byte(`{ "groups": [ { "name": "alpha", "sourceType": "alpha" }, { "name": "beta", "sourceType": "beta" } ] }`), 0644)).To(Succeed())
Expect(os.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata.properties"),
[]byte("configuration-properties.classes=alpha\nconfiguration-properties.names=beta"), 0644)).To(Succeed())

cm, err := boot.NewConfigurationMetadataFromPath(path)
Expect(err).NotTo(HaveOccurred())

Expect(boot.NewDataFlowConfigurationMetadata(path, cm)).To(Equal(boot.ConfigurationMetadata{
Groups: []boot.Group{{Name: "alpha", SourceType: "alpha"}},
Groups: []boot.Group{
{Name: "alpha", SourceType: "alpha"},
{Name: "beta", SourceType: "beta"},
},
}))
})

it("returns dataflow decoded contents handling trailing comma correctly", func() {
it("handles missing names properly", func() {
Expect(os.MkdirAll(filepath.Join(path, "META-INF"), 0755)).To(Succeed())
Expect(ioutil.WriteFile(filepath.Join(path, "META-INF", "spring-configuration-metadata.json"),
[]byte(`{ "properties": [ { "name": "alpha", "sourceType": "alpha" }, { "name": "beta" } ] }`), 0644))
Expect(ioutil.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata.properties"),
[]byte("configuration-properties.classes=alpha,"), 0644))
Expect(os.WriteFile(filepath.Join(path, "META-INF", "spring-configuration-metadata.json"),
[]byte(`{ "properties": [ { "name": "alpha", "sourceType": "alpha" }, { "name": "beta", "sourceType": "beta" } ] }`), 0644)).To(Succeed())
Expect(os.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata.properties"),
[]byte("configuration-properties.names=alpha"), 0644)).To(Succeed())

cm, err := boot.NewConfigurationMetadataFromPath(path)
Expect(err).NotTo(HaveOccurred())
Expand All @@ -163,19 +154,17 @@ func testConfigurationMetadata(t *testing.T, context spec.G, it spec.S) {
}))
})

it("returns dataflow decoded contents", func() {
it("handles empty names gracefully", func() {
Expect(os.MkdirAll(filepath.Join(path, "META-INF"), 0755)).To(Succeed())
Expect(ioutil.WriteFile(filepath.Join(path, "META-INF", "spring-configuration-metadata.json"),
[]byte(`{ "groups": [ { "name": "alpha", "sourceType": "alpha" } ] }`), 0644)).To(Succeed())
Expect(ioutil.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata-whitelist.properties"),
[]byte("configuration-properties.classes=alpha"), 0644)).To(Succeed())
Expect(os.WriteFile(filepath.Join(path, "META-INF", "spring-configuration-metadata.json"),
[]byte(`{ "properties": [ { "name": "alpha", "sourceType": "alpha" } ] }`), 0644)).To(Succeed())
Expect(os.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata.properties"),
[]byte("configuration-properties.names="), 0644)).To(Succeed())

cm, err := boot.NewConfigurationMetadataFromPath(path)
Expect(err).NotTo(HaveOccurred())

Expect(boot.NewDataFlowConfigurationMetadata(path, cm)).To(Equal(boot.ConfigurationMetadata{
Groups: []boot.Group{{Name: "alpha", SourceType: "alpha"}},
}))
Expect(boot.NewDataFlowConfigurationMetadata(path, cm)).To(Equal(boot.ConfigurationMetadata{}))
})
})

Expand Down Expand Up @@ -203,14 +192,14 @@ func testConfigurationMetadata(t *testing.T, context spec.G, it spec.S) {

it("returns true if the file does exist", func() {
Expect(os.MkdirAll(filepath.Join(path, "META-INF"), 0755)).To(Succeed())
Expect(ioutil.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata-whitelist.properties"),
Expect(os.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata-whitelist.properties"),
[]byte("configuration-properties.classes=alpha"), 0644)).To(Succeed())
Expect(boot.DataFlowConfigurationExists(path)).To(BeFalse())
})

it("return false and the error if the file cannot be read", func() {
Expect(os.MkdirAll(filepath.Join(path, "META-INF"), 0755)).To(Succeed())
Expect(ioutil.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata-whitelist.properties"),
Expect(os.WriteFile(filepath.Join(path, "META-INF", "dataflow-configuration-metadata-whitelist.properties"),
[]byte("configuration-properties.classes=alpha"), 0644)).To(Succeed())

Expect(os.Chmod(filepath.Join(path, "META-INF"), 0000)).To(Succeed())
Expand Down

0 comments on commit d52c2e3

Please sign in to comment.