1package vars_test
2
3import (
4	"errors"
5
6	. "github.com/onsi/ginkgo"
7	. "github.com/onsi/gomega"
8
9	. "github.com/concourse/concourse/vars"
10)
11
12var _ = Describe("MultiVariables", func() {
13	Describe("Get", func() {
14		It("return no value and not found if there are no sources", func() {
15			val, found, err := NewMultiVars(nil).Get(VariableDefinition{})
16			Expect(val).To(BeNil())
17			Expect(found).To(BeFalse())
18			Expect(err).ToNot(HaveOccurred())
19		})
20
21		It("return no value and not found if variable is not found in any of the sources", func() {
22			vars1 := StaticVariables{"key1": "val"}
23			vars2 := StaticVariables{"key2": "val"}
24			vars := NewMultiVars([]Variables{vars1, vars2})
25
26			val, found, err := vars.Get(VariableDefinition{Ref: VariableReference{Path: "key3"}})
27			Expect(val).To(BeNil())
28			Expect(found).To(BeFalse())
29			Expect(err).ToNot(HaveOccurred())
30		})
31
32		It("return error as soon as one source fails", func() {
33			vars1 := StaticVariables{"key1": "val"}
34			vars2 := &FakeVariables{GetErr: errors.New("fake-err")}
35			vars := NewMultiVars([]Variables{vars1, vars2})
36
37			val, found, err := vars.Get(VariableDefinition{Ref: VariableReference{Path: "key3"}})
38			Expect(val).To(BeNil())
39			Expect(found).To(BeFalse())
40			Expect(err).To(Equal(errors.New("fake-err")))
41		})
42
43		It("return found value as soon as one source succeeds", func() {
44			vars1 := &FakeVariables{}
45			vars2 := StaticVariables{"key2": "val"}
46			vars3 := &FakeVariables{GetErr: errors.New("fake-err")}
47			vars := NewMultiVars([]Variables{vars1, vars2, vars3})
48
49			val, found, err := vars.Get(VariableDefinition{Ref: VariableReference{Path: "key2"}})
50			Expect(val).To(Equal("val"))
51			Expect(found).To(BeTrue())
52			Expect(err).ToNot(HaveOccurred())
53
54			// Didn't get past second variables
55			Expect(vars1.GetCallCount).To(Equal(1))
56			Expect(vars3.GetCallCount).To(Equal(0))
57		})
58
59		It("sends full variable definition to each tried source", func() {
60			vars1 := &FakeVariables{}
61			vars2 := StaticVariables{"key2": "val"}
62			vars := NewMultiVars([]Variables{vars1, vars2})
63
64			val, found, err := vars.Get(VariableDefinition{Ref: VariableReference{Path: "key2"}, Type: "type", Options: "opts"})
65			Expect(val).To(Equal("val"))
66			Expect(found).To(BeTrue())
67			Expect(err).ToNot(HaveOccurred())
68
69			Expect(vars1.GetVarDef).To(Equal(VariableDefinition{Ref: VariableReference{Path: "key2"}, Type: "type", Options: "opts"}))
70		})
71	})
72
73	Describe("List", func() {
74		It("returns list of names from multiple vars with duplicates", func() {
75			defs, err := NewMultiVars(nil).List()
76			Expect(defs).To(BeEmpty())
77			Expect(err).ToNot(HaveOccurred())
78
79			vars := NewMultiVars([]Variables{StaticVariables{"a": "1", "b": "2"}, StaticVariables{"b": "3", "c": "4"}})
80
81			defs, err = vars.List()
82			Expect(defs).To(ConsistOf([]VariableDefinition{
83				{Ref: VariableReference{Path: "a"}},
84				{Ref: VariableReference{Path: "b"}},
85				{Ref: VariableReference{Path: "b"}},
86				{Ref: VariableReference{Path: "c"}},
87			}))
88			Expect(err).ToNot(HaveOccurred())
89		})
90	})
91})
92