Skip to content

Commit

Permalink
expanded ParseFile function to include URL's and added a test for it.
Browse files Browse the repository at this point in the history
  • Loading branch information
willfairwinds committed Jul 25, 2019
1 parent 962494e commit 7b8b5fe
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 8 deletions.
29 changes: 21 additions & 8 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"fmt"
"io"
"io/ioutil"
"net/http"
"strings"

packr "github.com/gobuffalo/packr/v2"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -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)
}
Expand Down
36 changes: 36 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
package config

import (
"context"
"io"
"log"
"net/http"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit 7b8b5fe

Please sign in to comment.