Skip to content

Commit

Permalink
Merge pull request #181 from FairwindsOps/wl/config-URL
Browse files Browse the repository at this point in the history
Wl/config url
  • Loading branch information
willfairwinds authored Jul 31, 2019
2 parents 20bd32a + fae7a9f commit dce24a0
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
12 changes: 11 additions & 1 deletion 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,12 +114,20 @@ 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
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, err2 := http.Get(path)
if err2 != nil {
return Configuration{}, err2
}
rawBytes, err = ioutil.ReadAll(response.Body)
} else {
//path is local
rawBytes, err = ioutil.ReadFile(path)
}
if err != nil {
Expand Down
35 changes: 35 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
package config

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

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -120,6 +125,36 @@ 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() {
if err := srv.ListenAndServe(); err != http.ErrServerClosed {
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)
}
testParsedConfig(t, &parsedConf)

}

func TestConfigNoServerError(t *testing.T) {
var err error
_, err = ParseFile("http://localhost:8081/exampleURL")
assert.Error(t, err)
assert.Regexp(t, regexp.MustCompile("connection refused"), err.Error())
}

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 dce24a0

Please sign in to comment.