github.com/wanddynosios/cli@v7.1.0+incompatible/integration/v6/push/interpolate_manifest_test.go (about)

     1  package push
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  
     9  	"code.cloudfoundry.org/cli/integration/helpers"
    10  	. "github.com/onsi/ginkgo"
    11  	. "github.com/onsi/gomega"
    12  	. "github.com/onsi/gomega/gbytes"
    13  	. "github.com/onsi/gomega/gexec"
    14  )
    15  
    16  var _ = Describe("Push with manifest variable interpolation", func() {
    17  	var (
    18  		appName   string
    19  		instances int
    20  
    21  		manifestPath string
    22  	)
    23  
    24  	BeforeEach(func() {
    25  		appName = helpers.NewAppName()
    26  		instances = 4
    27  
    28  		tmp, err := ioutil.TempFile("", "manifest-interpolation")
    29  		Expect(err).ToNot(HaveOccurred())
    30  		Expect(tmp.Close()).ToNot(HaveOccurred())
    31  		manifestPath = tmp.Name()
    32  
    33  		helpers.WriteManifest(manifestPath, map[string]interface{}{
    34  			"applications": []map[string]interface{}{
    35  				{
    36  					"name":      "((vars1))",
    37  					"instances": "((vars2))",
    38  				},
    39  			},
    40  		})
    41  	})
    42  
    43  	AfterEach(func() {
    44  		Expect(os.RemoveAll(manifestPath)).ToNot(HaveOccurred())
    45  	})
    46  
    47  	When("only `--vars-file` flags are provided", func() {
    48  		var (
    49  			tmpDir string
    50  
    51  			firstVarsFilePath  string
    52  			secondVarsFilePath string
    53  		)
    54  
    55  		BeforeEach(func() {
    56  			var err error
    57  			tmpDir, err = ioutil.TempDir("", "vars-files")
    58  			Expect(err).ToNot(HaveOccurred())
    59  		})
    60  
    61  		AfterEach(func() {
    62  			Expect(os.RemoveAll(tmpDir)).ToNot(HaveOccurred())
    63  		})
    64  
    65  		When("there are no duplicate variables", func() {
    66  			BeforeEach(func() {
    67  				firstVarsFilePath = filepath.Join(tmpDir, "vars1")
    68  				vars1 := fmt.Sprintf("vars1: %s", appName)
    69  				err := ioutil.WriteFile(firstVarsFilePath, []byte(vars1), 0666)
    70  				Expect(err).ToNot(HaveOccurred())
    71  
    72  				secondVarsFilePath = filepath.Join(tmpDir, "vars2")
    73  				vars2 := fmt.Sprintf("vars2: %d", instances)
    74  				err = ioutil.WriteFile(secondVarsFilePath, []byte(vars2), 0666)
    75  				Expect(err).ToNot(HaveOccurred())
    76  			})
    77  
    78  			It("pushes the app with the interpolated values in the manifest", func() {
    79  				helpers.WithHelloWorldApp(func(dir string) {
    80  
    81  					session := helpers.CustomCF(helpers.CFEnv{WorkingDirectory: dir}, PushCommandName, "-f", manifestPath, "--vars-file", firstVarsFilePath, "--vars-file", secondVarsFilePath)
    82  					Eventually(session).Should(Say(`Getting app info\.\.\.`))
    83  					Eventually(session).Should(Say(`Creating app with these attributes\.\.\.`))
    84  					Eventually(session).Should(Say(`\+\s+name:\s+%s`, appName))
    85  					Eventually(session).Should(Say(`\+\s+instances:\s+%d`, instances))
    86  					Eventually(session).Should(Say(`Mapping routes\.\.\.`))
    87  					Eventually(session).Should(Say(`Waiting for app to start\.\.\.`))
    88  					Eventually(session).Should(Say(`requested state:\s+started`))
    89  					Eventually(session).Should(Exit(0))
    90  				})
    91  				session := helpers.CF("app", appName)
    92  				Eventually(session).Should(Exit(0))
    93  			})
    94  		})
    95  
    96  		When("a variable in manifest is not found in var_file", func() {
    97  			BeforeEach(func() {
    98  				firstVarsFilePath = filepath.Join(tmpDir, "vars1")
    99  				vars1 := fmt.Sprintf("vars1: %s", appName)
   100  				err := ioutil.WriteFile(firstVarsFilePath, []byte(vars1), 0666)
   101  				Expect(err).ToNot(HaveOccurred())
   102  
   103  				helpers.WriteManifest(manifestPath, map[string]interface{}{
   104  					"applications": []map[string]interface{}{
   105  						{
   106  							"name": "((not_vars))",
   107  						},
   108  					},
   109  				})
   110  			})
   111  
   112  			It("fails with error saying that variable is missing", func() {
   113  				helpers.WithHelloWorldApp(func(dir string) {
   114  					session := helpers.CustomCF(helpers.CFEnv{WorkingDirectory: dir}, PushCommandName, "-f", manifestPath, "--vars-file", firstVarsFilePath)
   115  					Eventually(session.Err).Should(Say("Expected to find variables: not_vars"))
   116  					Eventually(session).Should(Exit(1))
   117  				})
   118  			})
   119  		})
   120  
   121  		When("there are duplicate variables", func() {
   122  			BeforeEach(func() {
   123  				firstVarsFilePath = filepath.Join(tmpDir, "vars1")
   124  				vars1 := fmt.Sprintf("vars1: %s\nvars2: %d", "some-garbage-appname", instances)
   125  				err := ioutil.WriteFile(firstVarsFilePath, []byte(vars1), 0666)
   126  				Expect(err).ToNot(HaveOccurred())
   127  
   128  				secondVarsFilePath = filepath.Join(tmpDir, "vars2")
   129  				vars2 := fmt.Sprintf("vars1: %s", appName)
   130  				err = ioutil.WriteFile(secondVarsFilePath, []byte(vars2), 0666)
   131  				Expect(err).ToNot(HaveOccurred())
   132  			})
   133  
   134  			It("pushes the app using the values from the latter vars-file interpolated in the manifest", func() {
   135  				helpers.WithHelloWorldApp(func(dir string) {
   136  					session := helpers.CustomCF(helpers.CFEnv{WorkingDirectory: dir}, PushCommandName, "-f", manifestPath, "--vars-file", firstVarsFilePath, "--vars-file", secondVarsFilePath)
   137  					Eventually(session).Should(Say(`Getting app info\.\.\.`))
   138  					Eventually(session).Should(Say(`Creating app with these attributes\.\.\.`))
   139  					Eventually(session).Should(Say(`\+\s+name:\s+%s`, appName))
   140  					Eventually(session).Should(Say(`\+\s+instances:\s+%d`, instances))
   141  					Eventually(session).Should(Say(`Mapping routes\.\.\.`))
   142  					Eventually(session).Should(Say(`Waiting for app to start\.\.\.`))
   143  					Eventually(session).Should(Say(`requested state:\s+started`))
   144  					Eventually(session).Should(Exit(0))
   145  				})
   146  				session := helpers.CF("app", appName)
   147  				Eventually(session).Should(Exit(0))
   148  			})
   149  		})
   150  	})
   151  
   152  	When("only `--var` flag vars are provided", func() {
   153  		It("replaces the variables with the provided values", func() {
   154  			helpers.WithHelloWorldApp(func(dir string) {
   155  				session := helpers.CustomCF(helpers.CFEnv{WorkingDirectory: dir}, PushCommandName, "-f", manifestPath, "--var", fmt.Sprintf("vars1=%s", appName), "--var", fmt.Sprintf("vars2=%d", instances))
   156  				Eventually(session).Should(Say(`Getting app info\.\.\.`))
   157  				Eventually(session).Should(Say(`Creating app with these attributes\.\.\.`))
   158  				Eventually(session).Should(Say(`\+\s+name:\s+%s`, appName))
   159  				Eventually(session).Should(Say(`\+\s+instances:\s+%d`, instances))
   160  				Eventually(session).Should(Say(`Mapping routes\.\.\.`))
   161  				Eventually(session).Should(Say(`Waiting for app to start\.\.\.`))
   162  				Eventually(session).Should(Say(`requested state:\s+started`))
   163  				Eventually(session).Should(Exit(0))
   164  			})
   165  			session := helpers.CF("app", appName)
   166  			Eventually(session).Should(Exit(0))
   167  		})
   168  	})
   169  
   170  	When("`--vars-file` and `--var` flag vars are provided", func() {
   171  		var varsFilePath string
   172  		BeforeEach(func() {
   173  			tmp, err := ioutil.TempFile("", "varsfile-interpolation")
   174  			Expect(err).ToNot(HaveOccurred())
   175  			Expect(tmp.Close()).ToNot(HaveOccurred())
   176  
   177  			varsFilePath = tmp.Name()
   178  			vars1 := fmt.Sprintf("vars1: %s\nvars2: %d", "some-garbage-appname", instances)
   179  			Expect(ioutil.WriteFile(varsFilePath, []byte(vars1), 0666)).ToNot(HaveOccurred())
   180  		})
   181  
   182  		AfterEach(func() {
   183  			Expect(os.RemoveAll(varsFilePath)).ToNot(HaveOccurred())
   184  		})
   185  
   186  		It("overwrites the vars-file with the provided var key value pair", func() {
   187  			helpers.WithHelloWorldApp(func(dir string) {
   188  				session := helpers.CustomCF(helpers.CFEnv{WorkingDirectory: dir}, PushCommandName, "-f", manifestPath, "--var", fmt.Sprintf("vars1=%s", appName), "--vars-file", varsFilePath)
   189  				Eventually(session).Should(Say(`Getting app info\.\.\.`))
   190  				Eventually(session).Should(Say(`Creating app with these attributes\.\.\.`))
   191  				Eventually(session).Should(Say(`\+\s+name:\s+%s`, appName))
   192  				Eventually(session).Should(Say(`\+\s+instances:\s+%d`, instances))
   193  				Eventually(session).Should(Say(`Mapping routes\.\.\.`))
   194  				Eventually(session).Should(Say(`Waiting for app to start\.\.\.`))
   195  				Eventually(session).Should(Say(`requested state:\s+started`))
   196  				Eventually(session).Should(Exit(0))
   197  			})
   198  			session := helpers.CF("app", appName)
   199  			Eventually(session).Should(Exit(0))
   200  		})
   201  	})
   202  })