github.com/alibaba/sealer@v0.8.6-0.20220430115802-37a2bdaa8173/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/alibaba/sealer/test/suites/build"
    22  
    23  	. "github.com/onsi/ginkgo"
    24  
    25  	"github.com/alibaba/sealer/test/suites/apply"
    26  	"github.com/alibaba/sealer/test/testhelper"
    27  	"github.com/alibaba/sealer/test/testhelper/settings"
    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  		BeforeEach(func() {
    36  			if rawCluster.Spec.Image != settings.TestImageName {
    37  				//rawCluster imageName updated to customImageName
    38  				rawCluster.Spec.Image = settings.TestImageName
    39  				apply.MarshalClusterToFile(rawClusterFilePath, rawCluster)
    40  			}
    41  		})
    42  
    43  		Context("check regular scenario that provider is bare metal, executes machine is master0", func() {
    44  			var tempFile string
    45  			BeforeEach(func() {
    46  				tempFile = testhelper.CreateTempFile()
    47  			})
    48  
    49  			AfterEach(func() {
    50  				testhelper.RemoveTempFile(tempFile)
    51  			})
    52  			It("init, scale up, scale down, clean up", func() {
    53  				By("start to prepare infra")
    54  				cluster := rawCluster.DeepCopy()
    55  				cluster.Spec.Provider = settings.AliCloud
    56  				cluster.Spec.Image = build.GetTestImageName()
    57  				cluster = apply.CreateAliCloudInfraAndSave(cluster, tempFile)
    58  				defer apply.CleanUpAliCloudInfra(cluster)
    59  				sshClient := testhelper.NewSSHClientByCluster(cluster)
    60  				testhelper.CheckFuncBeTrue(func() bool {
    61  					err := sshClient.SSH.Copy(sshClient.RemoteHostIP, settings.DefaultSealerBin, settings.DefaultSealerBin)
    62  					return err == nil
    63  				}, settings.MaxWaiteTime)
    64  
    65  				By("start to init cluster")
    66  				apply.GenerateClusterfile(tempFile)
    67  				apply.SendAndApplyCluster(sshClient, tempFile)
    68  				apply.CheckNodeNumWithSSH(sshClient, 2)
    69  
    70  				By("Wait for the cluster to be ready", func() {
    71  					apply.WaitAllNodeRunningBySSH(sshClient.SSH, sshClient.RemoteHostIP)
    72  				})
    73  
    74  				By("Use join command to add 3master and 3node for scale up cluster in baremetal mode", func() {
    75  					cluster.Spec.Nodes.Count = "3"
    76  					cluster.Spec.Masters.Count = "3"
    77  					cluster = apply.CreateAliCloudInfraAndSave(cluster, tempFile)
    78  					//waiting for service to start
    79  					time.Sleep(10 * time.Second)
    80  					joinMasters := strings.Join(cluster.Spec.Masters.IPList[1:], ",")
    81  					joinNodes := strings.Join(cluster.Spec.Nodes.IPList[1:], ",")
    82  					//sealer join master and node
    83  					apply.SendAndJoinCluster(sshClient, tempFile, joinMasters, joinNodes)
    84  					//add 3 masters and 3 nodes
    85  					apply.CheckNodeNumWithSSH(sshClient, 6)
    86  				})
    87  
    88  				By("start to scale down cluster")
    89  				cluster.Spec.Nodes.Count = "1"
    90  				cluster.Spec.Nodes.IPList = cluster.Spec.Nodes.IPList[:1]
    91  				cluster.Spec.Masters.Count = "3"
    92  				cluster.Spec.Provider = settings.BAREMETAL
    93  				apply.WriteClusterFileToDisk(cluster, tempFile)
    94  				apply.SendAndApplyCluster(sshClient, tempFile)
    95  				apply.CheckNodeNumWithSSH(sshClient, 4)
    96  				By("start to delete cluster")
    97  				err := sshClient.SSH.CmdAsync(sshClient.RemoteHostIP, apply.SealerDeleteCmd(tempFile))
    98  				testhelper.CheckErr(err)
    99  			})
   100  
   101  		})
   102  
   103  		Context("check regular scenario that provider is bare metal, executes machine is not master0", func() {
   104  			var tempFile string
   105  			BeforeEach(func() {
   106  				tempFile = testhelper.CreateTempFile()
   107  			})
   108  
   109  			AfterEach(func() {
   110  				testhelper.RemoveTempFile(tempFile)
   111  				testhelper.DeleteFileLocally(settings.GetClusterWorkClusterfile(rawCluster.Name))
   112  			})
   113  			It("init, scale up, scale down, clean up", func() {
   114  				By("start to prepare infra")
   115  				cluster := apply.LoadClusterFileFromDisk(rawClusterFilePath)
   116  				cluster.Spec.Provider = settings.AliCloud
   117  				usedCluster := apply.ChangeMasterOrderAndSave(cluster, tempFile)
   118  				defer apply.CleanUpAliCloudInfra(usedCluster)
   119  				sshClient := testhelper.NewSSHClientByCluster(usedCluster)
   120  				testhelper.CheckFuncBeTrue(func() bool {
   121  					err := sshClient.SSH.Copy(sshClient.RemoteHostIP, settings.DefaultSealerBin, settings.DefaultSealerBin)
   122  					return err == nil
   123  				}, settings.MaxWaiteTime)
   124  
   125  				By("start to init cluster")
   126  				apply.SendAndApplyCluster(sshClient, tempFile)
   127  				apply.CheckNodeNumWithSSH(sshClient, 4)
   128  
   129  				By("Wait for the cluster to be ready", func() {
   130  					apply.WaitAllNodeRunningBySSH(sshClient.SSH, sshClient.RemoteHostIP)
   131  				})
   132  
   133  				By("Use join command to add 3master and 3node for scale up cluster in baremetal mode", func() {
   134  					usedCluster.Spec.Nodes.Count = "3"
   135  					usedCluster.Spec.Masters.Count = "3"
   136  					usedCluster = apply.CreateAliCloudInfraAndSave(usedCluster, tempFile)
   137  					//waiting for service to start
   138  					time.Sleep(10 * time.Second)
   139  					joinNodes := strings.Join(usedCluster.Spec.Nodes.IPList[1:], ",")
   140  					//sealer join master and node
   141  					apply.SendAndJoinCluster(sshClient, tempFile, "", joinNodes)
   142  					//add 3 masters and 3 nodes
   143  					apply.CheckNodeNumWithSSH(sshClient, 6)
   144  				})
   145  
   146  				By("start to scale down cluster")
   147  				usedCluster.Spec.Nodes.Count = "1"
   148  				usedCluster.Spec.Nodes.IPList = usedCluster.Spec.Nodes.IPList[:1]
   149  				usedCluster.Spec.Masters.Count = "3"
   150  				usedCluster.Spec.Provider = settings.BAREMETAL
   151  				apply.WriteClusterFileToDisk(usedCluster, tempFile)
   152  				apply.SendAndApplyCluster(sshClient, tempFile)
   153  				apply.CheckNodeNumWithSSH(sshClient, 4)
   154  
   155  				By("start to delete cluster")
   156  				err := sshClient.SSH.CmdAsync(sshClient.RemoteHostIP, apply.SealerDeleteCmd(tempFile))
   157  				testhelper.CheckErr(err)
   158  			})
   159  
   160  		})
   161  	})
   162  
   163  	Context("start nydus image apply", func() {
   164  		rawCluster := apply.LoadClusterFileFromDisk(apply.GetRawClusterFilePath())
   165  		rawCluster.Spec.Image = settings.TestNydusImageName
   166  
   167  		Context("check regular scenario that provider is bare metal, executes machine is master0", func() {
   168  			var tempFile string
   169  			BeforeEach(func() {
   170  				tempFile = testhelper.CreateTempFile()
   171  			})
   172  
   173  			AfterEach(func() {
   174  				testhelper.RemoveTempFile(tempFile)
   175  			})
   176  			It("init, scale up, scale down, clean up", func() {
   177  				By("start to prepare infra")
   178  				cluster := rawCluster.DeepCopy()
   179  				cluster.Spec.Provider = settings.AliCloud
   180  				cluster.Spec.Image = settings.TestNydusImageName
   181  				cluster = apply.CreateAliCloudInfraAndSave(cluster, tempFile)
   182  				defer apply.CleanUpAliCloudInfra(cluster)
   183  				sshClient := testhelper.NewSSHClientByCluster(cluster)
   184  				testhelper.CheckFuncBeTrue(func() bool {
   185  					err := sshClient.SSH.Copy(sshClient.RemoteHostIP, settings.DefaultSealerBin, settings.DefaultSealerBin)
   186  					return err == nil
   187  				}, settings.MaxWaiteTime)
   188  
   189  				By("start to init cluster")
   190  				apply.GenerateClusterfile(tempFile)
   191  				apply.SendAndApplyCluster(sshClient, tempFile)
   192  				apply.CheckNodeNumWithSSH(sshClient, 2)
   193  
   194  				By("Wait for the cluster to be ready", func() {
   195  					apply.WaitAllNodeRunningBySSH(sshClient.SSH, sshClient.RemoteHostIP)
   196  				})
   197  
   198  				By("Use join command to add 3master and 3node for scale up cluster in baremetal mode", func() {
   199  					cluster.Spec.Nodes.Count = "3"
   200  					cluster.Spec.Masters.Count = "3"
   201  					cluster = apply.CreateAliCloudInfraAndSave(cluster, tempFile)
   202  					//waiting for service to start
   203  					time.Sleep(10 * time.Second)
   204  					joinMasters := strings.Join(cluster.Spec.Masters.IPList[1:], ",")
   205  					joinNodes := strings.Join(cluster.Spec.Nodes.IPList[1:], ",")
   206  					//sealer join master and node
   207  					apply.SendAndJoinCluster(sshClient, tempFile, joinMasters, joinNodes)
   208  					//add 3 masters and 3 nodes
   209  					apply.CheckNodeNumWithSSH(sshClient, 6)
   210  				})
   211  
   212  				By("start to scale down cluster")
   213  				cluster.Spec.Nodes.Count = "1"
   214  				cluster.Spec.Nodes.IPList = cluster.Spec.Nodes.IPList[:1]
   215  				cluster.Spec.Masters.Count = "3"
   216  				apply.WriteClusterFileToDisk(cluster, tempFile)
   217  				apply.SendAndApplyCluster(sshClient, tempFile)
   218  				apply.CheckNodeNumWithSSH(sshClient, 4)
   219  				By("start to delete cluster")
   220  				err := sshClient.SSH.CmdAsync(sshClient.RemoteHostIP, apply.SealerDeleteCmd(tempFile))
   221  				testhelper.CheckErr(err)
   222  			})
   223  
   224  		})
   225  	})
   226  })