github.com/oam-dev/kubevela@v1.9.11/e2e/commonContext.go (about)

     1  /*
     2  Copyright 2021 The KubeVela Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package e2e
    18  
    19  import (
    20  	"fmt"
    21  	"os"
    22  
    23  	"github.com/Netflix/go-expect"
    24  	"github.com/onsi/ginkgo/v2"
    25  	"github.com/onsi/gomega"
    26  )
    27  
    28  var (
    29  
    30  	// EnvInitContext used for test Env
    31  	EnvInitContext = func(context string, envName string) bool {
    32  		return ginkgo.It(context+": should print environment initiation successful message", func() {
    33  			cli := fmt.Sprintf("vela env init %s", envName)
    34  			var answer = "default"
    35  			if envName != "env-application" {
    36  				answer = "vela-system"
    37  			}
    38  			output, err := InteractiveExec(cli, func(c *expect.Console) {
    39  				data := []struct {
    40  					q, a string
    41  				}{
    42  					{
    43  						q: "Would you like to choose an existing namespaces as your env?",
    44  						a: answer,
    45  					},
    46  				}
    47  				for _, qa := range data {
    48  					_, err := c.ExpectString(qa.q)
    49  					gomega.Expect(err).NotTo(gomega.HaveOccurred())
    50  					_, err = c.SendLine(qa.a)
    51  					gomega.Expect(err).NotTo(gomega.HaveOccurred())
    52  				}
    53  				c.ExpectEOF()
    54  			})
    55  			gomega.Expect(err).NotTo(gomega.HaveOccurred())
    56  			expectedOutput := fmt.Sprintf("environment %s with namespace %s created", envName, answer)
    57  			gomega.Expect(output).To(gomega.ContainSubstring(expectedOutput))
    58  		})
    59  	}
    60  
    61  	EnvInitWithNamespaceOptionContext = func(context string, envName string, namespace string) bool {
    62  		return ginkgo.It(context+": should print environment initiation successful message", func() {
    63  			cli := fmt.Sprintf("vela env init %s --namespace %s", envName, namespace)
    64  			output, err := Exec(cli)
    65  			gomega.Expect(err).NotTo(gomega.HaveOccurred())
    66  			expectedOutput := fmt.Sprintf("environment %s with namespace %s created", envName, namespace)
    67  			gomega.Expect(output).To(gomega.ContainSubstring(expectedOutput))
    68  		})
    69  	}
    70  
    71  	JsonAppFileContext = func(context, jsonAppFile string) bool {
    72  		return ginkgo.It(context+": Start the application through the app file in JSON format.", func() {
    73  			writeStatus := os.WriteFile("vela.json", []byte(jsonAppFile), 0644)
    74  			gomega.Expect(writeStatus).NotTo(gomega.HaveOccurred())
    75  			output, err := Exec("vela up -f vela.json")
    76  			gomega.Expect(err).NotTo(gomega.HaveOccurred())
    77  			gomega.Expect(output).NotTo(gomega.ContainSubstring("Error:"))
    78  		})
    79  	}
    80  
    81  	JsonAppFileContextWithWait = func(context, jsonAppFile string) bool {
    82  		return ginkgo.It(context+": Start the application through the app file in JSON format.", func() {
    83  			writeStatus := os.WriteFile("vela.json", []byte(jsonAppFile), 0644)
    84  			gomega.Expect(writeStatus).NotTo(gomega.HaveOccurred())
    85  			output, err := Exec("vela up -f vela.json --wait")
    86  			gomega.Expect(err).NotTo(gomega.HaveOccurred())
    87  			gomega.Expect(output).To(gomega.ContainSubstring("Application Deployed Successfully!"))
    88  		})
    89  	}
    90  
    91  	JsonAppFileContextWithTimeout = func(context, jsonAppFile, duration string) bool {
    92  		return ginkgo.It(context+": Start the application through the app file in JSON format.", func() {
    93  			writeStatus := os.WriteFile("vela.json", []byte(jsonAppFile), 0644)
    94  			gomega.Expect(writeStatus).NotTo(gomega.HaveOccurred())
    95  			output, err := Exec("vela up -f vela.json --wait --timeout=" + duration)
    96  			gomega.Expect(err).NotTo(gomega.HaveOccurred())
    97  			gomega.Expect(output).To(gomega.ContainSubstring("Timeout waiting Application to be healthy!"))
    98  		})
    99  	}
   100  
   101  	DeleteEnvFunc = func(context string, envName string) bool {
   102  		return ginkgo.It(context+": should print env does not exist message", func() {
   103  			cli := fmt.Sprintf("vela env delete %s", envName)
   104  			_, err := Exec(cli)
   105  			gomega.Expect(err).NotTo(gomega.HaveOccurred())
   106  		})
   107  	}
   108  
   109  	EnvShowContext = func(context string, envName string) bool {
   110  		return ginkgo.It(context+": should show detailed environment message", func() {
   111  			cli := fmt.Sprintf("vela env ls %s", envName)
   112  			output, err := Exec(cli)
   113  			gomega.Expect(err).NotTo(gomega.HaveOccurred())
   114  			gomega.Expect(output).To(gomega.ContainSubstring("NAME"))
   115  			gomega.Expect(output).To(gomega.ContainSubstring("NAMESPACE"))
   116  			gomega.Expect(output).To(gomega.ContainSubstring(envName))
   117  		})
   118  	}
   119  
   120  	EnvSetContext = func(context string, envName string) bool {
   121  		return ginkgo.It(context+": should show environment set message", func() {
   122  			cli := fmt.Sprintf("vela env sw %s", envName)
   123  			output, err := Exec(cli)
   124  			gomega.Expect(err).NotTo(gomega.HaveOccurred())
   125  			gomega.Expect(output).To(gomega.ContainSubstring(envName))
   126  		})
   127  	}
   128  
   129  	EnvDeleteContext = func(context string, envName string) bool {
   130  		return ginkgo.It(context+": should delete an environment", func() {
   131  			cli := fmt.Sprintf("vela env delete %s", envName)
   132  			output, err := Exec(cli)
   133  			gomega.Expect(err).NotTo(gomega.HaveOccurred())
   134  			expectedOutput := fmt.Sprintf("%s deleted", envName)
   135  			gomega.Expect(output).To(gomega.ContainSubstring(expectedOutput))
   136  		})
   137  	}
   138  
   139  	WorkloadDeleteContext = func(context string, applicationName string) bool {
   140  		return ginkgo.It(context+": should print successful deletion information", func() {
   141  			cli := fmt.Sprintf("vela delete %s -y", applicationName)
   142  			output, err := Exec(cli)
   143  			gomega.Expect(err).NotTo(gomega.HaveOccurred())
   144  			gomega.Expect(output).To(gomega.ContainSubstring("succeeded"))
   145  		})
   146  	}
   147  
   148  	WorkloadCapabilityListContext = func() bool {
   149  		return ginkgo.It("list workload capabilities: should sync capabilities from cluster before listing workload capabilities", func() {
   150  			output, err := Exec("vela components")
   151  			gomega.Expect(err).NotTo(gomega.HaveOccurred())
   152  			gomega.Expect(output).To(gomega.ContainSubstring("webservice"))
   153  		})
   154  	}
   155  
   156  	TraitCapabilityListContext = func() bool {
   157  		return ginkgo.It("list traits capabilities: should sync capabilities from cluster before listing trait capabilities", func() {
   158  			output, err := Exec("vela traits")
   159  			gomega.Expect(err).NotTo(gomega.HaveOccurred())
   160  			gomega.Expect(output).To(gomega.ContainSubstring("scaler"))
   161  		})
   162  	}
   163  
   164  	// ComponentListContext used for test vela svc ls
   165  	ComponentListContext = func(context string, applicationName string, workloadType string, traitAlias string) bool {
   166  		return ginkgo.It(context+": should list all applications", func() {
   167  			output, err := Exec("vela ls")
   168  			gomega.Expect(err).NotTo(gomega.HaveOccurred())
   169  			gomega.Expect(output).To(gomega.ContainSubstring("COMPONENT"))
   170  			gomega.Expect(output).To(gomega.ContainSubstring(applicationName))
   171  			gomega.Expect(output).To(gomega.ContainSubstring(workloadType))
   172  			if traitAlias != "" {
   173  				gomega.Expect(output).To(gomega.ContainSubstring(traitAlias))
   174  			}
   175  		})
   176  	}
   177  
   178  	ShowCapabilityReference = func(context string, capabilityName string) bool {
   179  		return ginkgo.It(context+": should show capability reference", func() {
   180  			cli := fmt.Sprintf("vela show %s", capabilityName)
   181  			_, err := Exec(cli)
   182  			gomega.Expect(err).Should(gomega.BeNil())
   183  		})
   184  	}
   185  
   186  	ShowCapabilityReferenceMarkdown = func(context string, capabilityName string) bool {
   187  		return ginkgo.It(context+": should show capability reference in markdown", func() {
   188  			cli := fmt.Sprintf("vela show %s --format=markdown", capabilityName)
   189  			_, err := Exec(cli)
   190  			gomega.Expect(err).Should(gomega.BeNil())
   191  		})
   192  	}
   193  )