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

     1  /*
     2  Copyright © 2022 - 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  	"os"
    19  	"os/exec"
    20  	"time"
    21  
    22  	. "github.com/onsi/ginkgo/v2"
    23  	. "github.com/onsi/gomega"
    24  	"github.com/rancher-sandbox/ele-testhelpers/kubectl"
    25  	"github.com/rancher-sandbox/ele-testhelpers/tools"
    26  )
    27  
    28  func checkRC(err error) {
    29  	if err != nil {
    30  		GinkgoWriter.Printf("%s\n", err)
    31  	}
    32  }
    33  
    34  var _ = Describe("E2E - Getting logs node", Label("logs"), func() {
    35  	type binary struct {
    36  		Url  string
    37  		Name string
    38  	}
    39  
    40  	type getResourceLog struct {
    41  		Name string
    42  		Verb []string
    43  	}
    44  
    45  	It("Get the upstream cluster logs", func() {
    46  		// Report to Qase
    47  		testCaseID = 69
    48  
    49  		By("Downloading and executing tools to generate logs", func() {
    50  			elemental := binary{
    51  				elementalSupport,
    52  				"elemental-support",
    53  			}
    54  
    55  			logCollector := binary{
    56  				rancherLogCollector,
    57  				"rancher2_logs_collector.sh",
    58  			}
    59  
    60  			_ = os.Mkdir("logs", 0755)
    61  			_ = os.Chdir("logs")
    62  			myDir, _ := os.Getwd()
    63  
    64  			for _, b := range []binary{elemental, logCollector} {
    65  				Eventually(func() error {
    66  					return exec.Command("curl", "-L", b.Url, "-o", b.Name).Run()
    67  				}, tools.SetTimeout(1*time.Minute), 5*time.Second).Should(BeNil())
    68  
    69  				err := exec.Command("chmod", "+x", b.Name).Run()
    70  				checkRC(err)
    71  				if b.Name == "elemental-support" {
    72  					err := exec.Command(myDir + "/" + b.Name).Run()
    73  					checkRC(err)
    74  				} else {
    75  					err := exec.Command("sudo", myDir+"/"+b.Name, "-d", "../logs").Run()
    76  					checkRC(err)
    77  				}
    78  			}
    79  		})
    80  
    81  		By("Collecting additionals logs with kubectl commands", func() {
    82  			Bundles := getResourceLog{
    83  				"bundles",
    84  				[]string{"get", "describe"},
    85  			}
    86  
    87  			var getResources []getResourceLog = []getResourceLog{Bundles}
    88  			for _, r := range getResources {
    89  				for _, v := range r.Verb {
    90  					outcmd, err := kubectl.RunWithoutErr(v, r.Name, "--all-namespaces")
    91  					checkRC(err)
    92  					err = os.WriteFile(r.Name+"-"+v+".log", []byte(outcmd), os.ModePerm)
    93  					checkRC(err)
    94  				}
    95  			}
    96  		})
    97  
    98  		if proxy == "elemental" || proxy == "rancher" {
    99  			By("Collecting proxy log and make sure traffic went through it", func() {
   100  				out, err := exec.Command("docker", "exec", "squid_proxy", "cat", "/var/log/squid/access.log").CombinedOutput()
   101  				checkRC(err)
   102  				err = os.WriteFile("squid.log", []byte(out), os.ModePerm)
   103  				checkRC(err)
   104  				Expect(out).Should(MatchRegexp("TCP_TUNNEL/200.*CONNECT.*rancher.io"))
   105  			})
   106  		}
   107  	})
   108  })