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