Merge pull request #181 from FairwindsOps/wl/config-URL

Wl/config url
This commit is contained in:
Will Ledingham
2019-07-31 10:45:21 -04:00
committed by GitHub
2 changed files with 46 additions and 1 deletions

View File

@@ -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,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 {

View File

@@ -15,6 +15,11 @@
package config
import (
"context"
"io"
"log"
"net/http"
"regexp"
"testing"
"github.com/stretchr/testify/assert"
@@ -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))