github.com/pf-qiu/concourse/v6@v6.7.3-0.20201207032516-1f455d73275f/fly/commands/internal/templatehelpers/yaml_template_test.go (about)

     1  package templatehelpers_test
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path/filepath"
     7  
     8  	"github.com/pf-qiu/concourse/v6/fly/commands/internal/flaghelpers"
     9  	"github.com/pf-qiu/concourse/v6/fly/commands/internal/templatehelpers"
    10  	"github.com/pf-qiu/concourse/v6/vars"
    11  
    12  	"github.com/pf-qiu/concourse/v6/atc"
    13  
    14  	. "github.com/onsi/ginkgo"
    15  	. "github.com/onsi/gomega"
    16  )
    17  
    18  var _ = Describe("YAML Template With Params", func() {
    19  
    20  	Describe("resolve", func() {
    21  		var tmpdir string
    22  
    23  		BeforeEach(func() {
    24  			var err error
    25  
    26  			tmpdir, err = ioutil.TempDir("", "yaml-template-test")
    27  			Expect(err).NotTo(HaveOccurred())
    28  
    29  			err = ioutil.WriteFile(
    30  				filepath.Join(tmpdir, "sample.yml"),
    31  				[]byte(`section:
    32  - param1: ((param1))
    33    param2: ((param2))
    34    param3:
    35      nested: ((param3))
    36  `),
    37  				0644,
    38  			)
    39  			Expect(err).NotTo(HaveOccurred())
    40  		})
    41  
    42  		AfterEach(func() {
    43  			os.RemoveAll(tmpdir)
    44  		})
    45  
    46  		It("resolves all variables successfully", func() {
    47  			variables := []flaghelpers.VariablePairFlag{
    48  				{Ref: vars.Reference{Path: "param1"}, Value: "value1"},
    49  				{Ref: vars.Reference{Path: "param2"}, Value: "value2"},
    50  				{Ref: vars.Reference{Path: "param3"}, Value: "value3"},
    51  			}
    52  			sampleYaml := templatehelpers.NewYamlTemplateWithParams(atc.PathFlag(filepath.Join(tmpdir, "sample.yml")), nil, variables, nil, nil)
    53  			result, err := sampleYaml.Evaluate(false, false)
    54  			Expect(err).NotTo(HaveOccurred())
    55  			Expect(string(result)).To(Equal(`section:
    56  - param1: value1
    57    param2: value2
    58    param3:
    59      nested: value3
    60  `))
    61  		})
    62  
    63  		It("leave param uninterpolated if it's not provided", func() {
    64  			variables := []flaghelpers.VariablePairFlag{
    65  				{Ref: vars.Reference{Path: "param1"}, Value: "value1"},
    66  				{Ref: vars.Reference{Path: "param2"}, Value: "value2"},
    67  			}
    68  			sampleYaml := templatehelpers.NewYamlTemplateWithParams(atc.PathFlag(filepath.Join(tmpdir, "sample.yml")), nil, variables, nil, nil)
    69  			result, err := sampleYaml.Evaluate(false, false)
    70  			Expect(err).NotTo(HaveOccurred())
    71  			Expect(string(result)).To(Equal(`section:
    72  - param1: value1
    73    param2: value2
    74    param3:
    75      nested: ((param3))
    76  `))
    77  		})
    78  	})
    79  })