github.com/rancher/elemental/tests@v0.0.0-20240517125144-ae048c615b3f/e2e/app_test.go (about)

     1  /*
     2  Copyright © 2024 SUSE LLC
     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      http://www.apache.org/licenses/LICENSE-2.0
     8  Unless required by applicable law or agreed to in writing, software
     9  distributed under the License is distributed on an "AS IS" BASIS,
    10  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  See the License for the specific language governing permissions and
    12  limitations under the License.
    13  */
    14  
    15  package e2e_test
    16  
    17  import (
    18  	"fmt"
    19  	"os"
    20  	"os/exec"
    21  	"strings"
    22  	"time"
    23  
    24  	. "github.com/onsi/ginkgo/v2"
    25  	. "github.com/onsi/gomega"
    26  	"github.com/rancher-sandbox/ele-testhelpers/kubectl"
    27  	"github.com/rancher-sandbox/ele-testhelpers/rancher"
    28  	"github.com/rancher-sandbox/ele-testhelpers/tools"
    29  )
    30  
    31  var _ = Describe("E2E - Install a simple application", Label("install-app"), func() {
    32  	It("Install HelloWorld application", func() {
    33  		// Report to Qase
    34  		testCaseID = 31
    35  
    36  		kubeConfig, err := rancher.SetClientKubeConfig(clusterNS, clusterName)
    37  		defer os.Remove(kubeConfig)
    38  		Expect(err).To(Not(HaveOccurred()))
    39  
    40  		By("Installing application", func() {
    41  			err := kubectl.Apply("default", appYaml)
    42  			Expect(err).To(Not(HaveOccurred()))
    43  		})
    44  	})
    45  })
    46  
    47  var _ = Describe("E2E - Checking a simple application", Label("check-app"), func() {
    48  	It("Check HelloWorld application", func() {
    49  		// Report to Qase
    50  		testCaseID = 63
    51  
    52  		appName := "hello-world"
    53  
    54  		// File where to host client cluster kubeconfig
    55  		kubeConfig, err := rancher.SetClientKubeConfig(clusterNS, clusterName)
    56  		defer os.Remove(kubeConfig)
    57  		Expect(err).To(Not(HaveOccurred()))
    58  
    59  		By("Scaling the deployment to the number of nodes", func() {
    60  			var nodeList string
    61  			Eventually(func() string {
    62  				nodeList, _ = kubectl.RunWithoutErr("get", "nodes", "-o", "jsonpath={.items[*].metadata.name}")
    63  				return nodeList
    64  			}, tools.SetTimeout(2*time.Minute), 30*time.Second).Should(Not(BeEmpty()))
    65  
    66  			nodeNumber := len(strings.Fields(nodeList))
    67  			Expect(nodeNumber).To(Not(BeNil()))
    68  
    69  			out, err := kubectl.RunWithoutErr("scale", "--replicas="+fmt.Sprint(nodeNumber), "deployment/"+appName)
    70  			Expect(err).To(Not(HaveOccurred()), out)
    71  			Expect(out).To(ContainSubstring("deployment.apps/" + appName + " scaled"))
    72  		})
    73  
    74  		By("Waiting for deployment to be rollout", func() {
    75  			// Wait for application to be started
    76  			// NOTE: 1st or 2nd rollout command can sporadically fail, so better to use Eventually here
    77  			Eventually(func() string {
    78  				status, _ := kubectl.RunWithoutErr("rollout", "status", "deployment/"+appName)
    79  				return status
    80  			}, tools.SetTimeout(2*time.Minute), 30*time.Second).Should(ContainSubstring("successfully rolled out"))
    81  		})
    82  
    83  		By("Checking application", func() {
    84  			cmd := []string{
    85  				"get", "svc",
    86  				appName + "-loadbalancer",
    87  				"-o", "jsonpath={.status.loadBalancer.ingress[*].ip}",
    88  			}
    89  
    90  			// Wait until at least an IP address is returned
    91  			Eventually(func() bool {
    92  				ip, _ := kubectl.RunWithoutErr(cmd...)
    93  				return tools.IsIPv4(strings.Fields(ip)[0])
    94  			}, tools.SetTimeout(2*time.Minute), 5*time.Second).Should(BeTrue())
    95  
    96  			// Get load balancer IPs
    97  			appIPs, err := kubectl.RunWithoutErr(cmd...)
    98  			Expect(err).To(Not(HaveOccurred()))
    99  			Expect(appIPs).To(Not(BeEmpty()))
   100  
   101  			// Loop on each IP to check the application
   102  			for _, ip := range strings.Fields(appIPs) {
   103  				if tools.IsIPv4(ip) {
   104  					GinkgoWriter.Printf("Checking node with IP %s...\n", ip)
   105  
   106  					// Retry if needed, could take some times if a pod is restarted for example
   107  					var htmlPage []byte
   108  					Eventually(func() error {
   109  						htmlPage, err = exec.Command("curl", "http://"+ip+":8080").CombinedOutput()
   110  						return err
   111  					}, tools.SetTimeout(2*time.Minute), 5*time.Second).Should(Not(HaveOccurred()))
   112  
   113  					// Check HTML page content
   114  					Expect(string(htmlPage)).To(And(
   115  						ContainSubstring("Hello world!"),
   116  						ContainSubstring("My hostname is hello-world-"),
   117  						ContainSubstring(ip+":8080"),
   118  					), string(htmlPage))
   119  				}
   120  			}
   121  		})
   122  	})
   123  })