github.com/pf-qiu/concourse/v6@v6.7.3-0.20201207032516-1f455d73275f/fly/integration/get_pipeline_test.go (about)

     1  package integration_test
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"net/http"
     7  	"os/exec"
     8  
     9  	. "github.com/onsi/ginkgo"
    10  	. "github.com/onsi/gomega"
    11  
    12  	"github.com/onsi/gomega/gbytes"
    13  	"github.com/onsi/gomega/gexec"
    14  	"github.com/onsi/gomega/ghttp"
    15  	"github.com/tedsuo/rata"
    16  	"sigs.k8s.io/yaml"
    17  
    18  	"github.com/pf-qiu/concourse/v6/atc"
    19  )
    20  
    21  var _ = Describe("Fly CLI", func() {
    22  	Describe("get-pipeline", func() {
    23  		var (
    24  			config atc.Config
    25  		)
    26  
    27  		BeforeEach(func() {
    28  			config = atc.Config{
    29  				Groups: atc.GroupConfigs{
    30  					{
    31  						Name:      "some-group",
    32  						Jobs:      []string{"job-1", "job-2"},
    33  						Resources: []string{"resource-1", "resource-2"},
    34  					},
    35  					{
    36  						Name:      "some-other-group",
    37  						Jobs:      []string{"job-3", "job-4"},
    38  						Resources: []string{"resource-6", "resource-4"},
    39  					},
    40  				},
    41  
    42  				Resources: atc.ResourceConfigs{
    43  					{
    44  						Name: "some-resource",
    45  						Type: "some-type",
    46  						Source: atc.Source{
    47  							"source-config": "some-value",
    48  						},
    49  					},
    50  					{
    51  						Name: "some-other-resource",
    52  						Type: "some-other-type",
    53  						Source: atc.Source{
    54  							"source-config": "some-value",
    55  						},
    56  					},
    57  				},
    58  
    59  				ResourceTypes: atc.ResourceTypes{
    60  					{
    61  						Name: "some-resource-type",
    62  						Type: "some-type",
    63  						Source: atc.Source{
    64  							"source-config": "some-value",
    65  						},
    66  					},
    67  					{
    68  						Name: "some-other-resource-type",
    69  						Type: "some-other-type",
    70  						Source: atc.Source{
    71  							"source-config": "some-value",
    72  						},
    73  					},
    74  				},
    75  
    76  				Jobs: atc.JobConfigs{
    77  					{
    78  						Name:   "some-job",
    79  						Public: true,
    80  						Serial: true,
    81  					},
    82  					{
    83  						Name: "some-other-job",
    84  					},
    85  				},
    86  			}
    87  		})
    88  
    89  		Describe("getting", func() {
    90  			Context("when not specifying a pipeline name", func() {
    91  				It("fails and says you should give a pipeline name", func() {
    92  					flyCmd := exec.Command(flyPath, "-t", targetName, "get-pipeline")
    93  
    94  					sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
    95  					Expect(err).NotTo(HaveOccurred())
    96  
    97  					<-sess.Exited
    98  					Expect(sess.ExitCode()).To(Equal(1))
    99  
   100  					Expect(sess.Err).To(gbytes.Say("error: the required flag `" + osFlag("p", "pipeline") + "' was not specified"))
   101  				})
   102  			})
   103  
   104  			Context("when the pipeline flag is invalid", func() {
   105  				It("fails and print invalid flag error", func() {
   106  					flyCmd := exec.Command(flyPath, "-t", targetName, "get-pipeline", "-p", "forbidden/pipelinename")
   107  
   108  					sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
   109  					Expect(err).NotTo(HaveOccurred())
   110  
   111  					<-sess.Exited
   112  					Expect(sess.ExitCode()).To(Equal(1))
   113  
   114  					Expect(sess.Err).To(gbytes.Say("error: invalid argument for flag `" + osFlag("p", "pipeline")))
   115  				})
   116  			})
   117  
   118  			Context("when specifying a pipeline name", func() {
   119  				var (
   120  					path        string
   121  					queryParams string
   122  				)
   123  
   124  				BeforeEach(func() {
   125  					var err error
   126  					path, err = atc.Routes.CreatePathForRoute(atc.GetConfig, rata.Params{"pipeline_name": "some-pipeline", "team_name": "main"})
   127  					Expect(err).NotTo(HaveOccurred())
   128  
   129  					queryParams = "instance_vars=%7B%22branch%22%3A%22master%22%7D"
   130  				})
   131  
   132  				Context("when specifying pipeline vars", func() {
   133  
   134  					Context("and pipeline exists", func() {
   135  						BeforeEach(func() {
   136  							atcServer.AppendHandlers(
   137  								ghttp.CombineHandlers(
   138  									ghttp.VerifyRequest("GET", path, queryParams),
   139  									ghttp.RespondWithJSONEncoded(200, atc.ConfigResponse{Config: config}, http.Header{atc.ConfigVersionHeader: {"42"}}),
   140  								),
   141  							)
   142  						})
   143  
   144  						It("prints the config as yaml to stdout", func() {
   145  							flyCmd := exec.Command(flyPath, "-t", targetName, "get-pipeline", "--pipeline", "some-pipeline/branch:master")
   146  
   147  							sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
   148  							Expect(err).NotTo(HaveOccurred())
   149  
   150  							<-sess.Exited
   151  							Expect(sess.ExitCode()).To(Equal(0))
   152  
   153  							var printedConfig atc.Config
   154  							err = yaml.Unmarshal(sess.Out.Contents(), &printedConfig)
   155  							Expect(err).NotTo(HaveOccurred())
   156  
   157  							Expect(printedConfig).To(Equal(config))
   158  						})
   159  					})
   160  				})
   161  
   162  				Context("and pipeline is not found", func() {
   163  					JustBeforeEach(func() {
   164  						atcServer.AppendHandlers(
   165  							ghttp.CombineHandlers(
   166  								ghttp.VerifyRequest("GET", path),
   167  								ghttp.RespondWithJSONEncoded(http.StatusNotFound, ""),
   168  							),
   169  						)
   170  					})
   171  
   172  					It("should print pipeline not found error", func() {
   173  						flyCmd := exec.Command(flyPath, "-t", targetName, "get-pipeline", "--pipeline", "some-pipeline", "-j")
   174  
   175  						sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
   176  						Expect(err).NotTo(HaveOccurred())
   177  
   178  						<-sess.Exited
   179  						Expect(sess.ExitCode()).To(Equal(1))
   180  
   181  						Expect(sess.Err).To(gbytes.Say("error: pipeline not found"))
   182  					})
   183  				})
   184  
   185  				Context("when atc returns valid config", func() {
   186  					BeforeEach(func() {
   187  						atcServer.AppendHandlers(
   188  							ghttp.CombineHandlers(
   189  								ghttp.VerifyRequest("GET", path),
   190  								ghttp.RespondWithJSONEncoded(200, atc.ConfigResponse{Config: config}, http.Header{atc.ConfigVersionHeader: {"42"}}),
   191  							),
   192  						)
   193  					})
   194  
   195  					It("prints the config as yaml to stdout", func() {
   196  						flyCmd := exec.Command(flyPath, "-t", targetName, "get-pipeline", "--pipeline", "some-pipeline")
   197  
   198  						sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
   199  						Expect(err).NotTo(HaveOccurred())
   200  
   201  						<-sess.Exited
   202  						Expect(sess.ExitCode()).To(Equal(0))
   203  
   204  						var printedConfig atc.Config
   205  						err = yaml.Unmarshal(sess.Out.Contents(), &printedConfig)
   206  						Expect(err).NotTo(HaveOccurred())
   207  
   208  						Expect(printedConfig).To(Equal(config))
   209  					})
   210  
   211  					Context("when -j is given", func() {
   212  						It("prints the config as json to stdout", func() {
   213  							flyCmd := exec.Command(flyPath, "-t", targetName, "get-pipeline", "--pipeline", "some-pipeline", "-j")
   214  
   215  							sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
   216  							Expect(err).NotTo(HaveOccurred())
   217  
   218  							<-sess.Exited
   219  							Expect(sess.ExitCode()).To(Equal(0))
   220  
   221  							var printedConfig atc.Config
   222  							err = json.Unmarshal(sess.Out.Contents(), &printedConfig)
   223  							Expect(err).NotTo(HaveOccurred())
   224  
   225  							Expect(printedConfig).To(Equal(config))
   226  						})
   227  					})
   228  				})
   229  			})
   230  
   231  			Context("with a custom team", func() {
   232  				team := "diff-team"
   233  				BeforeEach(func() {
   234  					atcServer.AppendHandlers(
   235  						ghttp.CombineHandlers(
   236  							ghttp.VerifyRequest("GET", fmt.Sprintf("/api/v1/teams/%s", team)),
   237  							ghttp.RespondWithJSONEncoded(http.StatusOK, atc.Team{
   238  								Name: team,
   239  							}),
   240  						),
   241  					)
   242  				})
   243  				Context("when specifying a pipeline name", func() {
   244  					var (
   245  						path        string
   246  						queryParams string
   247  					)
   248  
   249  					BeforeEach(func() {
   250  						var err error
   251  						path, err = atc.Routes.CreatePathForRoute(atc.GetConfig, rata.Params{"pipeline_name": "some-pipeline", "team_name": team})
   252  						Expect(err).NotTo(HaveOccurred())
   253  
   254  						queryParams = "instance_vars=%7B%22branch%22%3A%22master%22%7D"
   255  					})
   256  
   257  					Context("when specifying pipeline vars", func() {
   258  
   259  						Context("and pipeline exists", func() {
   260  							BeforeEach(func() {
   261  								atcServer.AppendHandlers(
   262  									ghttp.CombineHandlers(
   263  										ghttp.VerifyRequest("GET", path, queryParams),
   264  										ghttp.RespondWithJSONEncoded(200, atc.ConfigResponse{Config: config}, http.Header{atc.ConfigVersionHeader: {"42"}}),
   265  									),
   266  								)
   267  							})
   268  
   269  							It("prints the config as yaml to stdout", func() {
   270  								flyCmd := exec.Command(flyPath, "-t", targetName, "get-pipeline", "--pipeline", "some-pipeline/branch:master", "--team", team)
   271  
   272  								sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
   273  								Expect(err).NotTo(HaveOccurred())
   274  
   275  								<-sess.Exited
   276  								Expect(sess.ExitCode()).To(Equal(0))
   277  
   278  								var printedConfig atc.Config
   279  								err = yaml.Unmarshal(sess.Out.Contents(), &printedConfig)
   280  								Expect(err).NotTo(HaveOccurred())
   281  
   282  								Expect(printedConfig).To(Equal(config))
   283  							})
   284  						})
   285  					})
   286  
   287  					Context("and pipeline is not found", func() {
   288  						JustBeforeEach(func() {
   289  							atcServer.AppendHandlers(
   290  								ghttp.CombineHandlers(
   291  									ghttp.VerifyRequest("GET", path),
   292  									ghttp.RespondWithJSONEncoded(http.StatusNotFound, ""),
   293  								),
   294  							)
   295  						})
   296  
   297  						It("should print pipeline not found error", func() {
   298  							flyCmd := exec.Command(flyPath, "-t", targetName, "get-pipeline", "--pipeline", "some-pipeline", "-j", "--team", team)
   299  
   300  							sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
   301  							Expect(err).NotTo(HaveOccurred())
   302  
   303  							<-sess.Exited
   304  							Expect(sess.ExitCode()).To(Equal(1))
   305  
   306  							Expect(sess.Err).To(gbytes.Say("error: pipeline not found"))
   307  						})
   308  					})
   309  
   310  					Context("when atc returns valid config", func() {
   311  						BeforeEach(func() {
   312  							atcServer.AppendHandlers(
   313  								ghttp.CombineHandlers(
   314  									ghttp.VerifyRequest("GET", path),
   315  									ghttp.RespondWithJSONEncoded(200, atc.ConfigResponse{Config: config}, http.Header{atc.ConfigVersionHeader: {"42"}}),
   316  								),
   317  							)
   318  						})
   319  
   320  						It("prints the config as yaml to stdout", func() {
   321  							flyCmd := exec.Command(flyPath, "-t", targetName, "get-pipeline", "--pipeline", "some-pipeline", "--team", team)
   322  
   323  							sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
   324  							Expect(err).NotTo(HaveOccurred())
   325  
   326  							<-sess.Exited
   327  							Expect(sess.ExitCode()).To(Equal(0))
   328  
   329  							var printedConfig atc.Config
   330  							err = yaml.Unmarshal(sess.Out.Contents(), &printedConfig)
   331  							Expect(err).NotTo(HaveOccurred())
   332  
   333  							Expect(printedConfig).To(Equal(config))
   334  						})
   335  
   336  						Context("when -j is given", func() {
   337  							It("prints the config as json to stdout", func() {
   338  								flyCmd := exec.Command(flyPath, "-t", targetName, "get-pipeline", "--pipeline", "some-pipeline", "-j", "--team", team)
   339  
   340  								sess, err := gexec.Start(flyCmd, GinkgoWriter, GinkgoWriter)
   341  								Expect(err).NotTo(HaveOccurred())
   342  
   343  								<-sess.Exited
   344  								Expect(sess.ExitCode()).To(Equal(0))
   345  
   346  								var printedConfig atc.Config
   347  								err = json.Unmarshal(sess.Out.Contents(), &printedConfig)
   348  								Expect(err).NotTo(HaveOccurred())
   349  
   350  								Expect(printedConfig).To(Equal(config))
   351  							})
   352  						})
   353  					})
   354  				})
   355  			})
   356  		})
   357  	})
   358  })