github.com/sealerio/sealer@v0.11.1-0.20240507115618-f4f89c5853ae/test/sealer_apply_test.go (about)

     1  // Copyright © 2021 Alibaba Group Holding Ltd.
     2  
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package test
    16  
    17  import (
    18  	"strings"
    19  	"time"
    20  
    21  	"github.com/sealerio/sealer/test/suites/apply"
    22  	"github.com/sealerio/sealer/test/testhelper"
    23  	"github.com/sealerio/sealer/test/testhelper/client/k8s"
    24  	"github.com/sealerio/sealer/test/testhelper/settings"
    25  	utilsnet "github.com/sealerio/sealer/utils/net"
    26  
    27  	. "github.com/onsi/ginkgo"
    28  )
    29  
    30  var _ = Describe("sealer apply", func() {
    31  	Context("start apply", func() {
    32  		rawClusterFilePath := apply.GetRawClusterFilePath()
    33  		rawCluster := apply.LoadClusterFileFromDisk(rawClusterFilePath)
    34  		rawCluster.Spec.Image = settings.TestImageName
    35  		apply.CheckDockerAndSwapOff()
    36  		BeforeEach(func() {
    37  			if rawCluster.Spec.Image != settings.TestImageName {
    38  				//rawClusterImageName updated to customImageName
    39  				rawCluster.Spec.Image = settings.TestImageName
    40  				apply.MarshalClusterToFile(rawClusterFilePath, rawCluster)
    41  			}
    42  		})
    43  
    44  		Context("check regular scenario that provider is container, executes machine is master0", func() {
    45  			var tempFile string
    46  			BeforeEach(func() {
    47  				tempFile = testhelper.CreateTempFile()
    48  			})
    49  
    50  			AfterEach(func() {
    51  				testhelper.RemoveTempFile(tempFile)
    52  			})
    53  			It("init, scale up, scale down, clean up", func() {
    54  				By("start to prepare infra")
    55  				cluster := rawCluster.DeepCopy()
    56  				cluster.Spec.Provider = settings.CONTAINER
    57  				cluster.Spec.Image = settings.TestImageName
    58  				cluster = apply.CreateContainerInfraAndSave(cluster, tempFile)
    59  				defer apply.CleanUpContainerInfra(cluster)
    60  				sshClient := testhelper.NewSSHClientByCluster(cluster)
    61  				testhelper.CheckFuncBeTrue(func() bool {
    62  					err := sshClient.SSH.Copy(sshClient.RemoteHostIP, settings.DefaultSealerBin, settings.DefaultSealerBin)
    63  					return err == nil
    64  				}, settings.MaxWaiteTime)
    65  
    66  				By("start to init cluster")
    67  				apply.GenerateClusterfile(tempFile)
    68  				apply.SendAndApplyCluster(sshClient, tempFile)
    69  				client, err := k8s.NewK8sClient(sshClient)
    70  				testhelper.CheckErr(err)
    71  				apply.CheckNodeNumWithSSH(client, 2)
    72  
    73  				By("Wait for the cluster to be ready", func() {
    74  					apply.WaitAllNodeRunningBySSH(client)
    75  				})
    76  
    77  				By("Use join command to add 1master and 2nodes for scale up cluster in container mode", func() {
    78  					cluster.Spec.Nodes.Count = "2"
    79  					cluster.Spec.Masters.Count = "1"
    80  					cluster = apply.CreateContainerInfraAndSave(cluster, tempFile)
    81  					//waiting for service to start
    82  					time.Sleep(10 * time.Second)
    83  					joinMastersIPStrs := utilsnet.IPsToIPStrs(cluster.Spec.Masters.IPList[1:])
    84  					joinMasters := strings.Join(joinMastersIPStrs, ",")
    85  					joinNodesIPStrs := utilsnet.IPsToIPStrs(cluster.Spec.Nodes.IPList[1:])
    86  					joinNodes := strings.Join(joinNodesIPStrs, ",")
    87  					//sealer join master and node
    88  					apply.SendAndJoinCluster(sshClient, tempFile, joinMasters, joinNodes)
    89  					//add 3 masters and 3 nodes
    90  					apply.CheckNodeNumWithSSH(client, 3)
    91  				})
    92  
    93  				By("start to scale down cluster")
    94  				deleteNode := cluster.Spec.Nodes.IPList[1].String()
    95  				err = sshClient.SSH.CmdAsync(sshClient.RemoteHostIP, nil, apply.SealerDeleteNodeCmd(deleteNode))
    96  				testhelper.CheckErr(err)
    97  				apply.CheckNodeNumWithSSH(client, 2)
    98  				cluster.Spec.Nodes.Count = "1"
    99  				cluster.Spec.Nodes.IPList = cluster.Spec.Nodes.IPList[:1]
   100  				cluster.Spec.Masters.Count = "1"
   101  				cluster.Spec.Provider = settings.CONTAINER
   102  				apply.WriteClusterFileToDisk(cluster, tempFile)
   103  				// sealer apply scale down is not supported, if supported, we can use this
   104  				// apply.SendAndApplyCluster(sshClient, tempFile)
   105  
   106  				By("start to delete cluster")
   107  				err = sshClient.SSH.CmdAsync(sshClient.RemoteHostIP, nil, apply.SealerDeleteAll())
   108  				testhelper.CheckErr(err)
   109  			})
   110  
   111  		})
   112  
   113  		// Context("check regular scenario that provider is bare metal, executes machine is not master0", func() {
   114  		// 	var tempFile string
   115  		// 	BeforeEach(func() {
   116  		// 		tempFile = testhelper.CreateTempFile()
   117  		// 	})
   118  
   119  		// 	AfterEach(func() {
   120  		// 		testhelper.RemoveTempFile(tempFile)
   121  		// 		testhelper.DeleteFileLocally(settings.GetClusterWorkClusterfile(rawCluster.Name))
   122  		// 	})
   123  		// 	It("init, scale up, scale down, clean up", func() {
   124  		// 		By("start to prepare infra")
   125  		// 		cluster := apply.LoadClusterFileFromDisk(rawClusterFilePath)
   126  		// 		cluster.Spec.Provider = settings.CONTAINER
   127  		// 		usedCluster := apply.ChangeMasterOrderAndSave(cluster, tempFile)
   128  		// 		defer apply.CleanUpContainerInfra(usedCluster)
   129  		// 		sshClient := testhelper.NewSSHClientByCluster(usedCluster)
   130  		// 		testhelper.CheckFuncBeTrue(func() bool {
   131  		// 			err := sshClient.SSH.Copy(sshClient.RemoteHostIP, settings.DefaultSealerBin, settings.DefaultSealerBin)
   132  		// 			return err == nil
   133  		// 		}, settings.MaxWaiteTime)
   134  
   135  		// 		By("start to init cluster")
   136  		// 		apply.SendAndApplyCluster(sshClient, tempFile)
   137  		// 		apply.CheckNodeNumWithSSH(sshClient, 4)
   138  
   139  		// 		By("Wait for the cluster to be ready", func() {
   140  		// 			apply.WaitAllNodeRunningBySSH(sshClient.SSH, sshClient.RemoteHostIP)
   141  		// 		})
   142  
   143  		// 		By("Use join command to add 3master and 3node for scale up cluster in baremetal mode", func() {
   144  		// 			usedCluster.Spec.Nodes.Count = "3"
   145  		// 			usedCluster.Spec.Masters.Count = "3"
   146  		// 			usedCluster = apply.CreateContainerInfraAndSave(usedCluster, tempFile)
   147  		// 			//waiting for service to start
   148  		// 			time.Sleep(10 * time.Second)
   149  		// 			joinNodesIPStrs := utilsnet.IPsToIPStrs(usedCluster.Spec.Nodes.IPList[1:])
   150  		// 			joinNodes := strings.Join(joinNodesIPStrs, ",")
   151  		// 			//sealer join master and node
   152  		// 			apply.SendAndJoinCluster(sshClient, tempFile, "", joinNodes)
   153  		// 			//add 3 masters and 3 nodes
   154  		// 			apply.CheckNodeNumWithSSH(sshClient, 6)
   155  		// 		})
   156  
   157  		// 		By("start to scale down cluster")
   158  		// 		usedCluster.Spec.Nodes.Count = "1"
   159  		// 		usedCluster.Spec.Nodes.IPList = usedCluster.Spec.Nodes.IPList[:1]
   160  		// 		usedCluster.Spec.Masters.Count = "3"
   161  		// 		usedCluster.Spec.Provider = settings.BAREMETAL
   162  		// 		apply.WriteClusterFileToDisk(usedCluster, tempFile)
   163  		// 		apply.SendAndApplyCluster(sshClient, tempFile)
   164  		// 		apply.CheckNodeNumWithSSH(sshClient, 4)
   165  
   166  		// 		By("start to delete cluster")
   167  		// 		err := sshClient.SSH.CmdAsync(sshClient.RemoteHostIP, apply.SealerDeleteCmd(tempFile))
   168  		// 		testhelper.CheckErr(err)
   169  		// 	})
   170  
   171  		// })
   172  	})
   173  
   174  	// Context("start nydus image apply", func() {
   175  	// 	rawCluster := apply.LoadClusterFileFromDisk(apply.GetRawClusterFilePath())
   176  	// 	rawCluster.Spec.Image = settings.TestNydusImageName
   177  
   178  	// 	Context("check regular scenario that provider is bare metal, executes machine is master0", func() {
   179  	// 		var tempFile string
   180  	// 		BeforeEach(func() {
   181  	// 			tempFile = testhelper.CreateTempFile()
   182  	// 		})
   183  
   184  	// 		AfterEach(func() {
   185  	// 			testhelper.RemoveTempFile(tempFile)
   186  	// 		})
   187  	// 		It("init, scale up, scale down, clean up", func() {
   188  	// 			By("start to prepare infra")
   189  	// 			cluster := rawCluster.DeepCopy()
   190  	// 			cluster.Spec.Provider = settings.CONTAINER
   191  	// 			cluster.Spec.Image = settings.TestNydusImageName
   192  	// 			cluster = apply.CreateContainerInfraAndSave(cluster, tempFile)
   193  	// 			defer apply.CleanUpContainerInfra(cluster)
   194  	// 			sshClient := testhelper.NewSSHClientByCluster(cluster)
   195  	// 			testhelper.CheckFuncBeTrue(func() bool {
   196  	// 				err := sshClient.SSH.Copy(sshClient.RemoteHostIP, settings.DefaultSealerBin, settings.DefaultSealerBin)
   197  	// 				return err == nil
   198  	// 			}, settings.MaxWaiteTime)
   199  
   200  	// 			By("start to init cluster")
   201  	// 			apply.GenerateClusterfile(tempFile)
   202  	// 			apply.SendAndApplyCluster(sshClient, tempFile)
   203  	// 			apply.CheckNodeNumWithSSH(sshClient, 2)
   204  
   205  	// 			By("Wait for the cluster to be ready", func() {
   206  	// 				apply.WaitAllNodeRunningBySSH(sshClient.SSH, sshClient.RemoteHostIP)
   207  	// 			})
   208  
   209  	// 			By("Use join command to add 3master and 3node for scale up cluster in baremetal mode", func() {
   210  	// 				cluster.Spec.Nodes.Count = "3"
   211  	// 				cluster.Spec.Masters.Count = "3"
   212  	// 				cluster = apply.CreateContainerInfraAndSave(cluster, tempFile)
   213  	// 				//waiting for service to start
   214  	// 				time.Sleep(10 * time.Second)
   215  	// 				joinMastersIPStrs := utilsnet.IPsToIPStrs(cluster.Spec.Masters.IPList[1:])
   216  	// 				joinMasters := strings.Join(joinMastersIPStrs, ",")
   217  	// 				joinNodesIPStrs := utilsnet.IPsToIPStrs(cluster.Spec.Nodes.IPList[1:])
   218  	// 				joinNodes := strings.Join(joinNodesIPStrs, ",")
   219  	// 				//sealer join master and node
   220  	// 				apply.SendAndJoinCluster(sshClient, tempFile, joinMasters, joinNodes)
   221  	// 				//add 3 masters and 3 nodes
   222  	// 				apply.CheckNodeNumWithSSH(sshClient, 6)
   223  	// 			})
   224  
   225  	// 			By("start to scale down cluster")
   226  	// 			cluster.Spec.Nodes.Count = "1"
   227  	// 			cluster.Spec.Nodes.IPList = cluster.Spec.Nodes.IPList[:1]
   228  	// 			cluster.Spec.Masters.Count = "3"
   229  	// 			apply.WriteClusterFileToDisk(cluster, tempFile)
   230  	// 			apply.SendAndApplyCluster(sshClient, tempFile)
   231  	// 			apply.CheckNodeNumWithSSH(sshClient, 4)
   232  	// 			By("start to delete cluster")
   233  	// 			err := sshClient.SSH.CmdAsync(sshClient.RemoteHostIP, apply.SealerDeleteCmd(tempFile))
   234  	// 			testhelper.CheckErr(err)
   235  	// 		})
   236  
   237  	// 	})
   238  	// })
   239  })