github.com/sleungcy-sap/cli@v7.1.0+incompatible/integration/v7/push/simple_manifest_only_test.go (about)

     1  package push
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path/filepath"
     7  
     8  	"code.cloudfoundry.org/cli/integration/helpers"
     9  	. "github.com/onsi/ginkgo"
    10  	. "github.com/onsi/ginkgo/extensions/table"
    11  	. "github.com/onsi/gomega"
    12  	. "github.com/onsi/gomega/gbytes"
    13  	. "github.com/onsi/gomega/gexec"
    14  )
    15  
    16  var _ = Describe("push with a simple manifest", func() {
    17  	var (
    18  		appName       string
    19  		secondAppName string
    20  	)
    21  
    22  	BeforeEach(func() {
    23  		appName = helpers.NewAppName()
    24  		secondAppName = helpers.NewAppName()
    25  	})
    26  
    27  	When("the manifest is in the current directory", func() {
    28  		It("uses the manifest", func() {
    29  			helpers.WithHelloWorldApp(func(dir string) {
    30  				helpers.WriteManifest(filepath.Join(dir, "manifest.yml"), map[string]interface{}{
    31  					"applications": []map[string]interface{}{
    32  						{
    33  							"name": appName,
    34  							"env": map[string]interface{}{
    35  								"key1": "val1",
    36  								"key4": false,
    37  							},
    38  						},
    39  					},
    40  				})
    41  
    42  				session := helpers.CustomCF(helpers.CFEnv{WorkingDirectory: dir}, PushCommandName, "--no-start")
    43  				Eventually(session).Should(Exit(0))
    44  			})
    45  
    46  			session := helpers.CF("env", appName)
    47  			Eventually(session).Should(Say(`key1:\s+val1`))
    48  			Eventually(session).Should(Say(`key4:\s+false`))
    49  			Eventually(session).Should(Exit(0))
    50  		})
    51  
    52  		When("the --no-manifest flag is provided", func() {
    53  			It("ignores the manifest", func() {
    54  				helpers.WithHelloWorldApp(func(dir string) {
    55  					helpers.WriteManifest(filepath.Join(dir, "manifest.yml"), map[string]interface{}{
    56  						"applications": []map[string]interface{}{
    57  							{
    58  								"name": appName,
    59  								"env": map[string]interface{}{
    60  									"key1": "val1",
    61  									"key4": false,
    62  								},
    63  							},
    64  						},
    65  					})
    66  
    67  					session := helpers.CustomCF(helpers.CFEnv{WorkingDirectory: dir}, PushCommandName, appName, "--no-start", "--no-manifest")
    68  					Eventually(session).Should(Exit(0))
    69  				})
    70  
    71  				session := helpers.CF("env", appName)
    72  				Consistently(session).ShouldNot(Say(`key1:\s+val1`))
    73  				Consistently(session).ShouldNot(Say(`key4:\s+false`))
    74  				Eventually(session).Should(Exit(0))
    75  			})
    76  			DescribeTable("incompatible flags",
    77  				func(flag string, needsPath bool) {
    78  					helpers.WithHelloWorldApp(func(dir string) {
    79  						var path string
    80  						var session *Session
    81  						if needsPath {
    82  							path = filepath.Join(dir, "file.yml")
    83  							helpers.WriteManifest(path, map[string]interface{}{
    84  								"applications": []map[string]interface{}{
    85  									{
    86  										"name": appName,
    87  									},
    88  								},
    89  							},
    90  							)
    91  							session = helpers.CustomCF(helpers.CFEnv{WorkingDirectory: dir}, PushCommandName, appName, "--no-start", "--no-manifest", flag, path)
    92  						} else {
    93  							session = helpers.CustomCF(helpers.CFEnv{WorkingDirectory: dir}, PushCommandName, appName, "--no-start", "--no-manifest", flag)
    94  						}
    95  						Eventually(session).Should(Exit(1))
    96  					})
    97  				},
    98  				Entry("--vars-file flag", "--vars-file", true),
    99  				Entry("-f flag", "-f", true),
   100  				Entry("--var flag", "--var=foo=bar", false),
   101  			)
   102  		})
   103  	})
   104  
   105  	When("the manifest is provided via '-f' flag", func() {
   106  		var (
   107  			tempDir        string
   108  			pathToManifest string
   109  		)
   110  
   111  		BeforeEach(func() {
   112  			var err error
   113  			tempDir, err = ioutil.TempDir("", "simple-manifest-test")
   114  			Expect(err).ToNot(HaveOccurred())
   115  			pathToManifest = filepath.Join(tempDir, "manifest.yml")
   116  			helpers.WriteManifest(pathToManifest, map[string]interface{}{
   117  				"applications": []map[string]interface{}{
   118  					{
   119  						"name": appName,
   120  						"env": map[string]interface{}{
   121  							"key1": "val1",
   122  							"key4": false,
   123  						},
   124  					},
   125  					{
   126  						"name": secondAppName,
   127  						"env": map[string]interface{}{
   128  							"diff-key1": "diff-val1",
   129  							"diff-key4": true,
   130  						},
   131  					},
   132  				},
   133  			})
   134  		})
   135  
   136  		AfterEach(func() {
   137  			Expect(os.RemoveAll(tempDir)).ToNot(HaveOccurred())
   138  		})
   139  
   140  		It("uses the manifest", func() {
   141  			helpers.WithHelloWorldApp(func(dir string) {
   142  				session := helpers.CustomCF(
   143  					helpers.CFEnv{WorkingDirectory: dir},
   144  					PushCommandName,
   145  					"-f", pathToManifest,
   146  					"--no-start")
   147  				Eventually(session).Should(Exit(0))
   148  			})
   149  
   150  			session := helpers.CF("env", appName)
   151  			Eventually(session).Should(Say(`key1:\s+val1`))
   152  			Eventually(session).Should(Say(`key4:\s+false`))
   153  			Eventually(session).Should(Exit(0))
   154  
   155  			session = helpers.CF("env", secondAppName)
   156  			Eventually(session).Should(Say(`diff-key1:\s+diff-val1`))
   157  			Eventually(session).Should(Say(`diff-key4:\s+true`))
   158  			Eventually(session).Should(Exit(0))
   159  		})
   160  	})
   161  
   162  	When("A single --vars-file is specified", func() {
   163  		var (
   164  			tempDir        string
   165  			pathToManifest string
   166  			pathToVarsFile string
   167  		)
   168  
   169  		BeforeEach(func() {
   170  			var err error
   171  			tempDir, err = ioutil.TempDir("", "simple-manifest-test")
   172  			Expect(err).ToNot(HaveOccurred())
   173  			pathToManifest = filepath.Join(tempDir, "manifest.yml")
   174  			helpers.WriteManifest(pathToManifest, map[string]interface{}{
   175  				"applications": []map[string]interface{}{
   176  					{
   177  						"name": appName,
   178  						"env": map[string]interface{}{
   179  							"key1": "((var1))",
   180  							"key4": false,
   181  						},
   182  					},
   183  				},
   184  			})
   185  
   186  			pathToVarsFile = filepath.Join(tempDir, "vars.yml")
   187  			helpers.WriteManifest(pathToVarsFile, map[string]interface{}{"var1": "secret-key"})
   188  		})
   189  
   190  		It("uses the manifest with substituted variables", func() {
   191  			helpers.WithHelloWorldApp(func(dir string) {
   192  				session := helpers.CustomCF(
   193  					helpers.CFEnv{WorkingDirectory: dir},
   194  					PushCommandName, appName,
   195  					"-f", pathToManifest,
   196  					"--vars-file", pathToVarsFile,
   197  					"--no-start")
   198  				Eventually(session).Should(Exit(0))
   199  			})
   200  
   201  			session := helpers.CF("env", appName)
   202  			Eventually(session).Should(Say(`key1:\s+secret-key`))
   203  			Eventually(session).Should(Say(`key4:\s+false`))
   204  			Eventually(session).Should(Exit(0))
   205  		})
   206  
   207  	})
   208  
   209  	When("A multiple --vars-file are specified", func() {
   210  		var (
   211  			tempDir         string
   212  			pathToManifest  string
   213  			pathToVarsFile1 string
   214  			pathToVarsFile2 string
   215  		)
   216  
   217  		BeforeEach(func() {
   218  			var err error
   219  			tempDir, err = ioutil.TempDir("", "simple-manifest-test")
   220  			Expect(err).ToNot(HaveOccurred())
   221  			pathToManifest = filepath.Join(tempDir, "manifest.yml")
   222  			helpers.WriteManifest(pathToManifest, map[string]interface{}{
   223  				"applications": []map[string]interface{}{
   224  					{
   225  						"name": appName,
   226  						"env": map[string]interface{}{
   227  							"key1": "((var1))",
   228  							"key4": "((var2))",
   229  						},
   230  					},
   231  				},
   232  			})
   233  
   234  			pathToVarsFile1 = filepath.Join(tempDir, "vars1.yml")
   235  			helpers.WriteManifest(pathToVarsFile1, map[string]interface{}{"var1": "not-so-secret-key", "var2": "foobar"})
   236  
   237  			pathToVarsFile2 = filepath.Join(tempDir, "vars2.yml")
   238  			helpers.WriteManifest(pathToVarsFile2, map[string]interface{}{"var1": "secret-key"})
   239  		})
   240  
   241  		It("uses the manifest with substituted variables", func() {
   242  			helpers.WithHelloWorldApp(func(dir string) {
   243  				session := helpers.CustomCF(
   244  					helpers.CFEnv{WorkingDirectory: dir},
   245  					PushCommandName, appName,
   246  					"-f", pathToManifest,
   247  					"--vars-file", pathToVarsFile1,
   248  					"--vars-file", pathToVarsFile2,
   249  					"--no-start")
   250  				Eventually(session).Should(Exit(0))
   251  			})
   252  
   253  			session := helpers.CF("env", appName)
   254  			Eventually(session).Should(Say(`key1:\s+secret-key`))
   255  			Eventually(session).Should(Say(`key4:\s+foobar`))
   256  			Eventually(session).Should(Exit(0))
   257  		})
   258  
   259  	})
   260  
   261  	When("A manifest contains a variable not in the vars-file or --var", func() {
   262  		var (
   263  			tempDir         string
   264  			pathToManifest  string
   265  			pathToVarsFile1 string
   266  		)
   267  
   268  		BeforeEach(func() {
   269  			var err error
   270  			tempDir, err = ioutil.TempDir("", "simple-manifest-test")
   271  			Expect(err).ToNot(HaveOccurred())
   272  			pathToManifest = filepath.Join(tempDir, "manifest.yml")
   273  			helpers.WriteManifest(pathToManifest, map[string]interface{}{
   274  				"applications": []map[string]interface{}{
   275  					{
   276  						"name": appName,
   277  						"env": map[string]interface{}{
   278  							"key1": "((var1))",
   279  							"key4": "((var2))",
   280  						},
   281  					},
   282  				},
   283  			})
   284  
   285  			pathToVarsFile1 = filepath.Join(tempDir, "vars1.yml")
   286  			helpers.WriteManifest(pathToVarsFile1, map[string]interface{}{"var2": "foobar"})
   287  		})
   288  
   289  		It("It fails to push because of unfound variables", func() {
   290  			helpers.WithHelloWorldApp(func(dir string) {
   291  				session := helpers.CustomCF(
   292  					helpers.CFEnv{WorkingDirectory: dir},
   293  					PushCommandName, appName,
   294  					"-f", pathToManifest,
   295  					"--vars-file", pathToVarsFile1,
   296  					"--var=not=here",
   297  					"--no-start")
   298  				Eventually(session).Should(Exit(1))
   299  				Eventually(session.Err).Should(Say(`Expected to find variables: var1`))
   300  			})
   301  		})
   302  
   303  	})
   304  
   305  	When("A single --var is provided", func() {
   306  
   307  		var (
   308  			tempDir        string
   309  			pathToManifest string
   310  		)
   311  
   312  		BeforeEach(func() {
   313  			var err error
   314  			tempDir, err = ioutil.TempDir("", "simple-manifest-test")
   315  			Expect(err).ToNot(HaveOccurred())
   316  			pathToManifest = filepath.Join(tempDir, "manifest.yml")
   317  			helpers.WriteManifest(pathToManifest, map[string]interface{}{
   318  				"applications": []map[string]interface{}{
   319  					{
   320  						"name": appName,
   321  						"env": map[string]interface{}{
   322  							"key1": "((var1))",
   323  						},
   324  					},
   325  				},
   326  			})
   327  		})
   328  
   329  		It("uses the manifest with substituted variables", func() {
   330  			helpers.WithHelloWorldApp(func(dir string) {
   331  				session := helpers.CustomCF(
   332  					helpers.CFEnv{WorkingDirectory: dir},
   333  					PushCommandName, appName,
   334  					"-f", pathToManifest,
   335  					"--var=var1=secret-key",
   336  					"--no-start")
   337  				Eventually(session).Should(Exit(0))
   338  			})
   339  
   340  			session := helpers.CF("env", appName)
   341  			Eventually(session).Should(Say(`key1:\s+secret-key`))
   342  			Eventually(session).Should(Exit(0))
   343  		})
   344  	})
   345  
   346  	When("Multiple --vars are provided", func() {
   347  
   348  		var (
   349  			tempDir        string
   350  			pathToManifest string
   351  		)
   352  
   353  		BeforeEach(func() {
   354  			var err error
   355  			tempDir, err = ioutil.TempDir("", "simple-manifest-test")
   356  			Expect(err).ToNot(HaveOccurred())
   357  			pathToManifest = filepath.Join(tempDir, "manifest.yml")
   358  			helpers.WriteManifest(pathToManifest, map[string]interface{}{
   359  				"applications": []map[string]interface{}{
   360  					{
   361  						"name": appName,
   362  						"env": map[string]interface{}{
   363  							"key1": "((var1))",
   364  							"key4": "((var2))",
   365  						},
   366  					},
   367  				},
   368  			})
   369  		})
   370  
   371  		It("uses the manifest with substituted variables", func() {
   372  			helpers.WithHelloWorldApp(func(dir string) {
   373  				session := helpers.CustomCF(
   374  					helpers.CFEnv{WorkingDirectory: dir},
   375  					PushCommandName, appName,
   376  					"-f", pathToManifest,
   377  					"--var=var1=secret-key", "--var=var2=foobar",
   378  					"--no-start")
   379  				Eventually(session).Should(Exit(0))
   380  			})
   381  
   382  			session := helpers.CF("env", appName)
   383  			Eventually(session).Should(Say(`key1:\s+secret-key`))
   384  			Eventually(session).Should(Say(`key4:\s+foobar`))
   385  			Eventually(session).Should(Exit(0))
   386  		})
   387  	})
   388  
   389  })