github.com/rancher/elemental/tests@v0.0.0-20240517125144-ae048c615b3f/e2e/configure_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  	"strings"
    21  	"time"
    22  
    23  	. "github.com/onsi/ginkgo/v2"
    24  	. "github.com/onsi/gomega"
    25  	"github.com/rancher-sandbox/ele-testhelpers/kubectl"
    26  	"github.com/rancher-sandbox/ele-testhelpers/tools"
    27  	"github.com/rancher/elemental/tests/e2e/helpers/elemental"
    28  	"golang.org/x/mod/semver"
    29  )
    30  
    31  var _ = Describe("E2E - Configure test", Label("configure"), func() {
    32  	It("Deploy a new cluster", func() {
    33  		// Report to Qase
    34  		testCaseID = 30
    35  
    36  		// Patterns to replace
    37  		basePatterns := []YamlPattern{
    38  			{
    39  				key:   "%CLUSTER_NAME%",
    40  				value: clusterName,
    41  			},
    42  			{
    43  				key:   "%K8S_VERSION%",
    44  				value: k8sDownstreamVersion,
    45  			},
    46  		}
    47  
    48  		By("Creating a cluster", func() {
    49  			// Create Yaml file
    50  			for _, p := range basePatterns {
    51  				err := tools.Sed(p.key, p.value, clusterYaml)
    52  				Expect(err).To(Not(HaveOccurred()))
    53  			}
    54  
    55  			// Apply to k8s
    56  			err := kubectl.Apply(clusterNS, clusterYaml)
    57  			Expect(err).To(Not(HaveOccurred()))
    58  
    59  			// Check that the cluster is correctly created
    60  			CheckCreatedCluster(clusterNS, clusterName)
    61  		})
    62  
    63  		By("Creating cluster selectors", func() {
    64  			// Set temporary file
    65  			selectorTmp, err := tools.CreateTemp("selector")
    66  			Expect(err).To(Not(HaveOccurred()))
    67  			defer os.Remove(selectorTmp)
    68  
    69  			for _, pool := range []string{"master", "worker"} {
    70  				// Patterns to replace
    71  				addPatterns := []YamlPattern{
    72  					{
    73  						key:   "%POOL_TYPE%",
    74  						value: pool,
    75  					},
    76  				}
    77  				patterns := append(basePatterns, addPatterns...)
    78  
    79  				// Save original file as it will have to be modified twice
    80  				err := tools.CopyFile(selectorYaml, selectorTmp)
    81  				Expect(err).To(Not(HaveOccurred()))
    82  
    83  				// Create Yaml file
    84  				for _, p := range patterns {
    85  					err := tools.Sed(p.key, p.value, selectorTmp)
    86  					Expect(err).To(Not(HaveOccurred()))
    87  				}
    88  
    89  				// Apply to k8s
    90  				err = kubectl.Apply(clusterNS, selectorTmp)
    91  				Expect(err).To(Not(HaveOccurred()))
    92  
    93  				// Check that the selector template is correctly created
    94  				CheckCreatedSelectorTemplate(clusterNS, "selector-"+pool+"-"+clusterName)
    95  			}
    96  		})
    97  
    98  		By("Adding MachineRegistration", func() {
    99  			// Set temporary file
   100  			registrationTmp, err := tools.CreateTemp("machineRegistration")
   101  			Expect(err).To(Not(HaveOccurred()))
   102  			defer os.Remove(registrationTmp)
   103  
   104  			for _, pool := range []string{"master", "worker"} {
   105  				// Patterns to replace
   106  				addPatterns := []YamlPattern{
   107  					{
   108  						key:   "%PASSWORD%",
   109  						value: userPassword,
   110  					},
   111  					{
   112  						key:   "%POOL_TYPE%",
   113  						value: pool,
   114  					},
   115  					{
   116  						key:   "%SNAP_TYPE%",
   117  						value: snapType,
   118  					},
   119  					{
   120  						key:   "%USER%",
   121  						value: userName,
   122  					},
   123  					{
   124  						key:   "%VM_NAME%",
   125  						value: vmNameRoot,
   126  					},
   127  				}
   128  				patterns := append(basePatterns, addPatterns...)
   129  
   130  				// Save original file as it will have to be modified twice
   131  				err := tools.CopyFile(registrationYaml, registrationTmp)
   132  				Expect(err).To(Not(HaveOccurred()))
   133  
   134  				// Create Yaml file
   135  				for _, p := range patterns {
   136  					err := tools.Sed(p.key, p.value, registrationTmp)
   137  					Expect(err).To(Not(HaveOccurred()))
   138  				}
   139  
   140  				// Stable version of Elemental Operator does not support snapshotter option
   141  				// NOTE: a bit dirty, but this is a workaround until Dev become the new Stable
   142  				operatorVersion, err := elemental.GetOperatorVersion()
   143  				if semver.Compare("v"+operatorVersion, "v1.6.0") == -1 {
   144  					GinkgoWriter.Printf("Found operator Stable version, apply workaround for pool %s.\n", pool)
   145  					err = exec.Command("sed", "-i", "/snapshotter:/,/type:/d", registrationTmp).Run()
   146  					Expect(err).To(Not(HaveOccurred()))
   147  				}
   148  
   149  				// Apply to k8s
   150  				err = kubectl.Apply(clusterNS, registrationTmp)
   151  				Expect(err).To(Not(HaveOccurred()))
   152  
   153  				// Check that the machine registration is correctly created
   154  				CheckCreatedRegistration(clusterNS, "machine-registration-"+pool+"-"+clusterName)
   155  			}
   156  		})
   157  	})
   158  
   159  	It("Configure Libvirt (if needed)", func() {
   160  		if !strings.Contains(testType, "airgap") {
   161  			// Report to Qase
   162  			testCaseID = 68
   163  
   164  			By("Starting default network", func() {
   165  				// Don't check return code, as the default network could be already removed
   166  				for _, c := range []string{"net-destroy", "net-undefine"} {
   167  					_ = exec.Command("sudo", "virsh", c, "default").Run()
   168  				}
   169  
   170  				// Wait a bit between virsh commands
   171  				time.Sleep(30 * time.Second)
   172  				err := exec.Command("sudo", "virsh", "net-create", netDefaultFileName).Run()
   173  				Expect(err).To(Not(HaveOccurred()))
   174  			})
   175  		}
   176  	})
   177  })