From 7b8b5fee4d27b6b3cfd0e9e8f739a45432e6a6ba Mon Sep 17 00:00:00 2001 From: Will Ledingham Date: Thu, 25 Jul 2019 16:31:17 -0400 Subject: [PATCH 01/10] expanded ParseFile function to include URL's and added a test for it. --- pkg/config/config.go | 29 +++++++++++++++++++++-------- pkg/config/config_test.go | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 8 deletions(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index 18f268e4..cd2c97da 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -19,6 +19,8 @@ import ( "fmt" "io" "io/ioutil" + "net/http" + "strings" packr "github.com/gobuffalo/packr/v2" corev1 "k8s.io/api/core/v1" @@ -112,16 +114,27 @@ type SecurityCapabilityLists struct { // ParseFile parses config from a file. func ParseFile(path string) (Configuration, error) { - configBox := packr.New("Config", "../../examples") - var rawBytes []byte var err error - if path == "" { - rawBytes, err = configBox.Find("config.yaml") + var rawBytes []byte + + if strings.HasPrefix(path, "https://") || strings.HasPrefix(path, "http://") { + //path is a url + response, err := http.Get(path) + if err != nil { + return Configuration{}, err + } + rawBytes, err = ioutil.ReadAll(response.Body) } else { - rawBytes, err = ioutil.ReadFile(path) - } - if err != nil { - return Configuration{}, err + //path is local + configBox := packr.New("Config", "../../examples") + if path == "" { + rawBytes, err = configBox.Find("config.yaml") + } else { + rawBytes, err = ioutil.ReadFile(path) + } + if err != nil { + return Configuration{}, err + } } return Parse(rawBytes) } diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index 23720459..3dfa81ef 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -15,6 +15,10 @@ package config import ( + "context" + "io" + "log" + "net/http" "testing" "github.com/stretchr/testify/assert" @@ -120,6 +124,38 @@ func TestParseJson(t *testing.T) { testParsedConfig(t, &parsedConf) } +func TestConfigFromURL(t *testing.T) { + var err error + var parsedConf Configuration + srv := &http.Server{Addr: ":8081"} + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + io.WriteString(w, resourceConfYAML1) + }) + + go func() { + // returns ErrServerClosed on graceful close + if err := srv.ListenAndServe(); err != http.ErrServerClosed { + // NOTE: there is a chance that next line won't have time to run, + // as main() doesn't wait for this goroutine to stop. don't use + // code with race conditions like these for production. see post + // comments below on more discussion on how to handle this. + log.Fatalf("ListenAndServe(): %s", err) + } + }() + + parsedConf, err = ParseFile("http://localhost:8081/exampleURL") + assert.NoError(t, err, "Expected no error when parsing YAML from URL") + if err := srv.Shutdown(context.TODO()); err != nil { + panic(err) // failure/timeout shutting down the server gracefully + } + testParsedConfig(t, &parsedConf) + + //http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + // fmt.Fprintf(w, resourceConfYAML1, html.EscapeString(r.URL.Path)) + //}) + //log.Fatal(http.ListenAndServe(":8081", nil)) +} + func testParsedConfig(t *testing.T, config *Configuration) { cpuRequests := config.Resources.CPURequestRanges assert.Equal(t, int64(100), cpuRequests.Error.Below.ScaledValue(resource.Milli)) From e01c39230a740fe58d5369e5336aace6ab8a60d1 Mon Sep 17 00:00:00 2001 From: Will Ledingham Date: Thu, 25 Jul 2019 16:34:59 -0400 Subject: [PATCH 02/10] cleaned up unnecessary comments --- pkg/config/config_test.go | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index 3dfa81ef..7c139ea8 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -133,12 +133,7 @@ func TestConfigFromURL(t *testing.T) { }) go func() { - // returns ErrServerClosed on graceful close if err := srv.ListenAndServe(); err != http.ErrServerClosed { - // NOTE: there is a chance that next line won't have time to run, - // as main() doesn't wait for this goroutine to stop. don't use - // code with race conditions like these for production. see post - // comments below on more discussion on how to handle this. log.Fatalf("ListenAndServe(): %s", err) } }() @@ -146,14 +141,10 @@ func TestConfigFromURL(t *testing.T) { parsedConf, err = ParseFile("http://localhost:8081/exampleURL") assert.NoError(t, err, "Expected no error when parsing YAML from URL") if err := srv.Shutdown(context.TODO()); err != nil { - panic(err) // failure/timeout shutting down the server gracefully + panic(err) } testParsedConfig(t, &parsedConf) - //http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - // fmt.Fprintf(w, resourceConfYAML1, html.EscapeString(r.URL.Path)) - //}) - //log.Fatal(http.ListenAndServe(":8081", nil)) } func testParsedConfig(t *testing.T, config *Configuration) { From baa652197a6283fe9216589e6b8cf907209acc3a Mon Sep 17 00:00:00 2001 From: Will Ledingham Date: Mon, 29 Jul 2019 10:33:27 -0400 Subject: [PATCH 03/10] Fixed error handling in ParseFile, added a test for parseFile where no Http server is configured --- pkg/config/config.go | 15 ++++++++------- pkg/config/config_test.go | 8 +++++++- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index cd2c97da..5465bfb2 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -116,7 +116,13 @@ type SecurityCapabilityLists struct { func ParseFile(path string) (Configuration, error) { var err error var rawBytes []byte - + configBox := packr.New("Config", "../../examples") + if path == "" { + rawBytes, err = configBox.Find("config.yaml") + if err != nil { + return Configuration{}, err + } + } if strings.HasPrefix(path, "https://") || strings.HasPrefix(path, "http://") { //path is a url response, err := http.Get(path) @@ -126,12 +132,7 @@ func ParseFile(path string) (Configuration, error) { rawBytes, err = ioutil.ReadAll(response.Body) } else { //path is local - configBox := packr.New("Config", "../../examples") - if path == "" { - rawBytes, err = configBox.Find("config.yaml") - } else { - rawBytes, err = ioutil.ReadFile(path) - } + rawBytes, err = ioutil.ReadFile(path) if err != nil { return Configuration{}, err } diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index 7c139ea8..6781ee1d 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -139,7 +139,7 @@ func TestConfigFromURL(t *testing.T) { }() parsedConf, err = ParseFile("http://localhost:8081/exampleURL") - assert.NoError(t, err, "Expected no error when parsing YAML from URL") + assert.NoError(t, err, "Expected no error when parsing JSON config") if err := srv.Shutdown(context.TODO()); err != nil { panic(err) } @@ -147,6 +147,12 @@ func TestConfigFromURL(t *testing.T) { } +func TestConfigNoServerError(t *testing.T) { + var err error + _, err = ParseFile("http://localhost:8081/exampleURL") + assert.EqualError(t, err, "Get http://localhost:8081/exampleURL: dial tcp [::1]:8081: connect: connection refused") +} + func testParsedConfig(t *testing.T, config *Configuration) { cpuRequests := config.Resources.CPURequestRanges assert.Equal(t, int64(100), cpuRequests.Error.Below.ScaledValue(resource.Milli)) From b55f4717b10178877554b2cedca5a664e2ef0509 Mon Sep 17 00:00:00 2001 From: Will Ledingham Date: Mon, 29 Jul 2019 10:44:01 -0400 Subject: [PATCH 04/10] typo --- pkg/config/config_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index 6781ee1d..18af22c1 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -139,7 +139,7 @@ func TestConfigFromURL(t *testing.T) { }() parsedConf, err = ParseFile("http://localhost:8081/exampleURL") - assert.NoError(t, err, "Expected no error when parsing JSON config") + assert.NoError(t, err, "Expected no error when parsing YAML from URL") if err := srv.Shutdown(context.TODO()); err != nil { panic(err) } From c8c0e07a223c167a2c30f682e9297b4b7d6fd54b Mon Sep 17 00:00:00 2001 From: Will Ledingham Date: Mon, 29 Jul 2019 11:09:33 -0400 Subject: [PATCH 05/10] altered to fix tests on CircleCl. --- pkg/config/config_test.go | 4 +++- pkg/validator/resources_test.go | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) create mode 100644 pkg/validator/resources_test.go diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index 18af22c1..4ac4c7ae 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -19,6 +19,7 @@ import ( "io" "log" "net/http" + "regexp" "testing" "github.com/stretchr/testify/assert" @@ -150,7 +151,8 @@ func TestConfigFromURL(t *testing.T) { func TestConfigNoServerError(t *testing.T) { var err error _, err = ParseFile("http://localhost:8081/exampleURL") - assert.EqualError(t, err, "Get http://localhost:8081/exampleURL: dial tcp [::1]:8081: connect: connection refused") + assert.Error(t, err) + assert.Regexp(t, regexp.MustCompile("connection refused"), err.Error()) } func testParsedConfig(t *testing.T, config *Configuration) { diff --git a/pkg/validator/resources_test.go b/pkg/validator/resources_test.go new file mode 100644 index 00000000..782ca330 --- /dev/null +++ b/pkg/validator/resources_test.go @@ -0,0 +1 @@ +adfsafdasfd \ No newline at end of file From 10025764cdbc0b8092a9b7aca96d75e09aaf8215 Mon Sep 17 00:00:00 2001 From: Will Ledingham Date: Mon, 29 Jul 2019 11:10:59 -0400 Subject: [PATCH 06/10] altered to fix tests on CircleCl. --- pkg/validator/resources_test.go | 1 - 1 file changed, 1 deletion(-) delete mode 100644 pkg/validator/resources_test.go diff --git a/pkg/validator/resources_test.go b/pkg/validator/resources_test.go deleted file mode 100644 index 782ca330..00000000 --- a/pkg/validator/resources_test.go +++ /dev/null @@ -1 +0,0 @@ -adfsafdasfd \ No newline at end of file From c132f7ee5147ebc2439f659934240922a894e766 Mon Sep 17 00:00:00 2001 From: Will Ledingham Date: Mon, 29 Jul 2019 13:29:53 -0400 Subject: [PATCH 07/10] more edits --- pkg/config/config.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index 5465bfb2..d24672dd 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -122,8 +122,7 @@ func ParseFile(path string) (Configuration, error) { if err != nil { return Configuration{}, err } - } - if strings.HasPrefix(path, "https://") || strings.HasPrefix(path, "http://") { + } else if strings.HasPrefix(path, "https://") || strings.HasPrefix(path, "http://") { //path is a url response, err := http.Get(path) if err != nil { From 2049698c8e30de398f13fa20d0b156ee6700d245 Mon Sep 17 00:00:00 2001 From: Will Ledingham Date: Tue, 30 Jul 2019 16:37:24 -0400 Subject: [PATCH 08/10] small edit --- pkg/config/config.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index d24672dd..6d79bcad 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -119,16 +119,13 @@ func ParseFile(path string) (Configuration, error) { configBox := packr.New("Config", "../../examples") if path == "" { rawBytes, err = configBox.Find("config.yaml") - if err != nil { - return Configuration{}, err - } } else if strings.HasPrefix(path, "https://") || strings.HasPrefix(path, "http://") { //path is a url response, err := http.Get(path) + rawBytes, err = ioutil.ReadAll(response.Body) if err != nil { return Configuration{}, err } - rawBytes, err = ioutil.ReadAll(response.Body) } else { //path is local rawBytes, err = ioutil.ReadFile(path) From e4fe5ab085dd66e8bc23064a4735816a073833f9 Mon Sep 17 00:00:00 2001 From: Will Ledingham Date: Tue, 30 Jul 2019 16:45:49 -0400 Subject: [PATCH 09/10] more edits --- pkg/config/config.go | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index 6d79bcad..22ea2d41 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -123,15 +123,12 @@ func ParseFile(path string) (Configuration, error) { //path is a url response, err := http.Get(path) rawBytes, err = ioutil.ReadAll(response.Body) - if err != nil { - return Configuration{}, err - } } else { //path is local rawBytes, err = ioutil.ReadFile(path) - if err != nil { - return Configuration{}, err - } + } + if err != nil { + return Configuration{}, err } return Parse(rawBytes) } From 79b0a00d5cf6a22114c73a7e7ed23be6e50c9709 Mon Sep 17 00:00:00 2001 From: Will Ledingham Date: Tue, 30 Jul 2019 16:59:11 -0400 Subject: [PATCH 10/10] more edits --- pkg/config/config.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index 22ea2d41..346cfd7b 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -114,14 +114,17 @@ type SecurityCapabilityLists struct { // ParseFile parses config from a file. func ParseFile(path string) (Configuration, error) { - var err error var rawBytes []byte + var err error configBox := packr.New("Config", "../../examples") if path == "" { rawBytes, err = configBox.Find("config.yaml") } else if strings.HasPrefix(path, "https://") || strings.HasPrefix(path, "http://") { //path is a url - response, err := http.Get(path) + response, err2 := http.Get(path) + if err2 != nil { + return Configuration{}, err2 + } rawBytes, err = ioutil.ReadAll(response.Body) } else { //path is local