github.com/alibaba/sealer@v0.8.6-0.20220430115802-37a2bdaa8173/test/suites/image/image.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 image
    16  
    17  import (
    18  	"fmt"
    19  	"io"
    20  	"path/filepath"
    21  
    22  	"github.com/alibaba/sealer/utils/platform"
    23  
    24  	"github.com/onsi/ginkgo"
    25  	"github.com/onsi/gomega"
    26  	"github.com/onsi/gomega/gbytes"
    27  	"github.com/onsi/gomega/gexec"
    28  
    29  	"github.com/alibaba/sealer/common"
    30  	"github.com/alibaba/sealer/pkg/image/store"
    31  	"github.com/alibaba/sealer/test/suites/build"
    32  	"github.com/alibaba/sealer/test/testhelper"
    33  	"github.com/alibaba/sealer/test/testhelper/settings"
    34  	"github.com/alibaba/sealer/utils"
    35  )
    36  
    37  func DoImageOps(action, imageName string) {
    38  	cmd := ""
    39  	switch action {
    40  	case settings.SubCmdPullOfSealer:
    41  		cmd = fmt.Sprintf("%s pull %s -d", settings.DefaultSealerBin, imageName)
    42  	case settings.SubCmdPushOfSealer:
    43  		cmd = fmt.Sprintf("%s push %s -d", settings.DefaultSealerBin, imageName)
    44  	case settings.SubCmdRmiOfSealer:
    45  		cmd = fmt.Sprintf("%s rmi %s -d", settings.DefaultSealerBin, imageName)
    46  	case settings.SubCmdRunOfSealer:
    47  		cmd = fmt.Sprintf("%s run %s -d", settings.DefaultSealerBin, imageName)
    48  	case settings.SubCmdListOfSealer:
    49  		cmd = fmt.Sprintf("%s images", settings.DefaultSealerBin)
    50  	}
    51  
    52  	testhelper.RunCmdAndCheckResult(cmd, 0)
    53  }
    54  func TagImages(oldName, newName string) {
    55  	cmd := fmt.Sprintf("%s %s %s %s", settings.DefaultSealerBin, settings.SubCmdTagOfSealer, oldName, newName)
    56  	testhelper.RunCmdAndCheckResult(cmd, 0)
    57  }
    58  
    59  func GetEnvDirMd5() string {
    60  	getEnvMd5Cmd := fmt.Sprintf("sudo -E find %s -type f -print0|xargs -0 sudo md5sum|cut -d\" \" -f1|md5sum|cut -d\" \" -f1\n", filepath.Dir(common.DefaultImageRootDir))
    61  	dirMd5, err := utils.RunSimpleCmd(getEnvMd5Cmd)
    62  	gomega.Expect(err).NotTo(gomega.HaveOccurred())
    63  	_, err = io.WriteString(ginkgo.GinkgoWriter, getEnvMd5Cmd+dirMd5+"\n")
    64  	gomega.Expect(err).NotTo(gomega.HaveOccurred())
    65  	return dirMd5
    66  }
    67  
    68  func GetImageID(imageName string) string {
    69  	is, err := store.NewDefaultImageStore()
    70  	gomega.Expect(err).NotTo(gomega.HaveOccurred())
    71  	image, err := is.GetByName(imageName, platform.GetDefaultPlatform())
    72  	gomega.Expect(err).NotTo(gomega.HaveOccurred())
    73  	return image.Spec.ID
    74  }
    75  
    76  func CheckLoginResult(registryURL, username, passwd string, result bool) {
    77  	usernameCmd, passwdCmd := "", ""
    78  	if username != "" {
    79  		usernameCmd = fmt.Sprintf("-u %s", username)
    80  	}
    81  	if passwd != "" {
    82  		passwdCmd = fmt.Sprintf("-p %s", passwd)
    83  	}
    84  	loginCmd := fmt.Sprintf("%s login %s %s %s", settings.DefaultSealerBin,
    85  		settings.RegistryURL,
    86  		usernameCmd,
    87  		passwdCmd)
    88  	if result {
    89  		sess, err := testhelper.Start(loginCmd)
    90  		gomega.Expect(err).NotTo(gomega.HaveOccurred())
    91  		gomega.Eventually(sess).Should(gbytes.Say(fmt.Sprintf("%s login %s success", username, registryURL)))
    92  		gomega.Eventually(sess).Should(gexec.Exit(0))
    93  		return
    94  	}
    95  	sess, err := testhelper.Start(loginCmd)
    96  	gomega.Expect(err).NotTo(gomega.HaveOccurred())
    97  	gomega.Eventually(sess).ShouldNot(gbytes.Say(fmt.Sprintf("%s login %s success", username, registryURL)))
    98  	gomega.Eventually(sess).ShouldNot(gexec.Exit(0))
    99  }
   100  
   101  func TagImageList(imageNameOrID string, tagImageNames []string) {
   102  	for _, tagImageName := range tagImageNames {
   103  		tagImageName := tagImageName
   104  		TagImages(imageNameOrID, tagImageName)
   105  		gomega.Expect(build.CheckIsImageExist(settings.TestImageName)).Should(gomega.BeTrue())
   106  	}
   107  }
   108  
   109  func RemoveImageList(imageNameList []string) {
   110  	for _, imageName := range imageNameList {
   111  		removeImage := imageName
   112  		DoImageOps(settings.SubCmdRmiOfSealer, removeImage)
   113  	}
   114  }