@@ -1,9 +1,11 @@
1package options
1package options
5 "io/ioutil"
6 "io/ioutil"
8 . "github.com/onsi/ginkgo"
10 . "github.com/onsi/ginkgo"
9 . "github.com/onsi/ginkgo/extensions/table"
11 . "github.com/onsi/ginkgo/extensions/table"
@@ -295,10 +297,199 @@ var _ = Describe("Load", func() {
295 expectedOutput: NewOptions(),
297 expectedOutput: NewOptions(),
297 Entry("with an empty LegacyOptions struct, should return default values", &testOptionsTableInput{
299 Entry("with an empty LegacyOptions struct, should return default values", &testOptionsTableInput{
298− flagSet: NewFlagSet,
300+ flagSet: NewLegacyFlagSet,
299 input: &LegacyOptions{},
301 input: &LegacyOptions{},
300 expectedOutput: NewLegacyOptions(),
302 expectedOutput: NewLegacyOptions(),
308+var _ = Describe("LoadYAML", func() {
309+ Context("with a testOptions structure", func() {
310+ type TestOptionSubStruct struct {
311+ StringSliceOption []string `yaml:"stringSliceOption,omitempty"`
314+ type TestOptions struct {
315+ StringOption string `yaml:"stringOption,omitempty"`
316+ Sub TestOptionSubStruct `yaml:"sub,omitempty"`
318+ // Check that embedded fields can be unmarshalled
319+ TestOptionSubStruct `yaml:",inline,squash"`
322+ var testOptionsConfigBytesFull = []byte(`
334+ type loadYAMLTableInput struct {
338+ expectedOutput interface{}
341+ DescribeTable("LoadYAML",
342+ func(in loadYAMLTableInput) {
343+ var configFileName string
345+ if in.configFile != nil {
346+ By("Creating a config file")
347+ configFile, err := ioutil.TempFile("", "oauth2-proxy-test-config-file")
348+ Expect(err).ToNot(HaveOccurred())
349+ defer configFile.Close()
351+ _, err = configFile.Write(in.configFile)
352+ Expect(err).ToNot(HaveOccurred())
353+ defer os.Remove(configFile.Name())
355+ configFileName = configFile.Name()
358+ var input interface{}
359+ if in.input != nil {
362+ input = &TestOptions{}
364+ err := LoadYAML(configFileName, input)
365+ if in.expectedErr != nil {
366+ Expect(err).To(MatchError(in.expectedErr.Error()))
368+ Expect(err).ToNot(HaveOccurred())
370+ Expect(input).To(Equal(in.expectedOutput))
372+ Entry("with a valid input", loadYAMLTableInput{
373+ configFile: testOptionsConfigBytesFull,
374+ input: &TestOptions{},
375+ expectedOutput: &TestOptions{
376+ StringOption: "foo",
377+ Sub: TestOptionSubStruct{
378+ StringSliceOption: []string{"d", "e"},
380+ TestOptionSubStruct: TestOptionSubStruct{
381+ StringSliceOption: []string{"a", "b", "c"},
385+ Entry("with no config file", loadYAMLTableInput{
387+ input: &TestOptions{},
388+ expectedOutput: &TestOptions{},
389+ expectedErr: errors.New("no configuration file provided"),
391+ Entry("with invalid YAML", loadYAMLTableInput{
392+ configFile: []byte("\tfoo: bar"),
393+ input: &TestOptions{},
394+ expectedOutput: &TestOptions{},
395+ expectedErr: errors.New("error unmarshalling config: error converting YAML to JSON: yaml: found character that cannot start any token"),
397+ Entry("with extra fields in the YAML", loadYAMLTableInput{
398+ configFile: append(testOptionsConfigBytesFull, []byte("foo: bar\n")...),
399+ input: &TestOptions{},
400+ expectedOutput: &TestOptions{
401+ StringOption: "foo",
402+ Sub: TestOptionSubStruct{
403+ StringSliceOption: []string{"d", "e"},
405+ TestOptionSubStruct: TestOptionSubStruct{
406+ StringSliceOption: []string{"a", "b", "c"},
409+ expectedErr: errors.New("error unmarshalling config: error unmarshaling JSON: while decoding JSON: json: unknown field \"foo\""),
411+ Entry("with an incorrect type for a string field", loadYAMLTableInput{
412+ configFile: []byte(`stringOption: ["a", "b"]`),
413+ input: &TestOptions{},
414+ expectedOutput: &TestOptions{},
415+ expectedErr: errors.New("error unmarshalling config: error unmarshaling JSON: while decoding JSON: json: cannot unmarshal array into Go struct field TestOptions.StringOption of type string"),
417+ Entry("with an incorrect type for an array field", loadYAMLTableInput{
418+ configFile: []byte(`stringSliceOption: "a"`),
419+ input: &TestOptions{},
420+ expectedOutput: &TestOptions{},
421+ expectedErr: errors.New("error unmarshalling config: error unmarshaling JSON: while decoding JSON: json: cannot unmarshal string into Go struct field TestOptions.StringSliceOption of type []string"),
426+ It("should load a full example AlphaOptions", func() {
432+ flushInterval: 500ms
433+injectRequestHeaders:
434+- name: X-Forwarded-User
437+injectResponseHeaders:
443+ By("Creating a config file")
444+ configFile, err := ioutil.TempFile("", "oauth2-proxy-test-alpha-config-file")
445+ Expect(err).ToNot(HaveOccurred())
446+ defer configFile.Close()
448+ _, err = configFile.Write(config)
449+ Expect(err).ToNot(HaveOccurred())
450+ defer os.Remove(configFile.Name())
452+ configFileName := configFile.Name()
454+ By("Loading the example config")
455+ into := &AlphaOptions{}
456+ Expect(LoadYAML(configFileName, into)).To(Succeed())
458+ flushInterval := Duration(500 * time.Millisecond)
460+ Expect(into).To(Equal(&AlphaOptions{
461+ Upstreams: []Upstream{
465+ URI: "http://httpbin",
466+ FlushInterval: &flushInterval,
469+ InjectRequestHeaders: []Header{
471+ Name: "X-Forwarded-User",
472+ Values: []HeaderValue{
474+ ClaimSource: &ClaimSource{
481+ InjectResponseHeaders: []Header{
484+ Values: []HeaderValue{
486+ SecretSource: &SecretSource{
487+ Value: []byte("secret"),