github.com/sealerio/sealer@v0.11.1-0.20240507115618-f4f89c5853ae/test/suites/build/build.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 build
    16  
    17  import (
    18  	"fmt"
    19  	"path/filepath"
    20  	"strings"
    21  
    22  	"github.com/sealerio/sealer/test/testhelper/settings"
    23  	"github.com/sealerio/sealer/utils/exec"
    24  )
    25  
    26  // GetBuildImageName return specific image name for sealer build test
    27  func GetBuildImageName() string {
    28  	buildImageName := "docker.io/sealerio/build-test:v1"
    29  	if settings.RegistryURL != "" && settings.RegistryUsername != "" && settings.RegistryPasswd != "" {
    30  		buildImageName = settings.RegistryURL + "/" + settings.RegistryUsername + "/" + "build-test:v1"
    31  	}
    32  	return buildImageName
    33  }
    34  
    35  func WithCmdsBuildDir() string {
    36  	return filepath.Join(settings.DefaultTestEnvDir, "suites", "build", "fixtures",
    37  		"build_with_cmds")
    38  }
    39  
    40  func WithImageListFlagBuildDir() string {
    41  	return filepath.Join(settings.DefaultTestEnvDir, "suites", "build", "fixtures",
    42  		"build_with_imagelist_flag")
    43  }
    44  
    45  func WithLaunchBuildDir() string {
    46  	return filepath.Join(settings.DefaultTestEnvDir, "suites", "build", "fixtures",
    47  		"build_with_launch")
    48  }
    49  
    50  func WithAPPCmdsBuildDir() string {
    51  	return filepath.Join(settings.DefaultTestEnvDir, "suites", "build", "fixtures",
    52  		"build_with_appcmds")
    53  }
    54  
    55  func WithMultiArchBuildDir() string {
    56  	return filepath.Join(settings.DefaultTestEnvDir, "suites", "build", "fixtures",
    57  		"build_with_multi_arch")
    58  }
    59  
    60  type ArgsOfBuild struct {
    61  	KubeFile, ImageName, Context string
    62  	Platform                     []string
    63  	ImageList                    string
    64  	ImageType                    string
    65  }
    66  
    67  func (a *ArgsOfBuild) SetKubeFile(kubeFile string) *ArgsOfBuild {
    68  	a.KubeFile = kubeFile
    69  	return a
    70  }
    71  
    72  func (a *ArgsOfBuild) SetImageName(imageName string) *ArgsOfBuild {
    73  	a.ImageName = imageName
    74  	return a
    75  }
    76  
    77  func (a *ArgsOfBuild) SetContext(context string) *ArgsOfBuild {
    78  	a.Context = context
    79  	return a
    80  }
    81  
    82  func (a *ArgsOfBuild) SetPlatforms(platforms []string) *ArgsOfBuild {
    83  	a.Platform = platforms
    84  	return a
    85  }
    86  
    87  func (a *ArgsOfBuild) SetImageList(imageList string) *ArgsOfBuild {
    88  	a.ImageList = imageList
    89  	return a
    90  }
    91  
    92  func (a *ArgsOfBuild) SetImageType(imageType string) *ArgsOfBuild {
    93  	a.ImageType = imageType
    94  	return a
    95  }
    96  
    97  func (a *ArgsOfBuild) String() string {
    98  	if settings.DefaultSealerBin == "" || a.KubeFile == "" || a.ImageName == "" {
    99  		return ""
   100  	}
   101  
   102  	var buildFlags []string
   103  	buildFlags = append(buildFlags, fmt.Sprintf("%s build", settings.DefaultSealerBin))
   104  
   105  	// add kubefile flag
   106  	if a.KubeFile != "" {
   107  		buildFlags = append(buildFlags, fmt.Sprintf("-f %s", a.KubeFile))
   108  	}
   109  
   110  	// add image tag flag
   111  	if a.ImageName != "" {
   112  		buildFlags = append(buildFlags, fmt.Sprintf("-t %s", a.ImageName))
   113  	}
   114  
   115  	// add image list flag
   116  	if a.ImageList != "" {
   117  		buildFlags = append(buildFlags, fmt.Sprintf("--image-list %s", a.ImageList))
   118  	}
   119  
   120  	// add platform flag
   121  	if len(a.Platform) != 0 {
   122  		buildFlags = append(buildFlags, fmt.Sprintf("--platform %s", strings.Join(a.Platform, ",")))
   123  	}
   124  
   125  	// add image type
   126  	if a.ImageType != "" {
   127  		buildFlags = append(buildFlags, fmt.Sprintf("--type %s", a.ImageType))
   128  	}
   129  
   130  	// add build context
   131  	if a.Context == "" {
   132  		a.Context = "."
   133  	}
   134  	buildFlags = append(buildFlags, a.Context)
   135  
   136  	return strings.Join(buildFlags, " ")
   137  }
   138  
   139  func NewArgsOfBuild() *ArgsOfBuild {
   140  	return &ArgsOfBuild{}
   141  }
   142  
   143  func CheckIsMultiArchImageExist(imageName string) bool {
   144  	cmd := fmt.Sprintf("%s alpha manifest inspect %s", settings.DefaultSealerBin, imageName)
   145  	_, err := exec.RunSimpleCmd(cmd)
   146  	return err == nil
   147  }
   148  
   149  func CheckIsImageExist(imageName string) bool {
   150  	cmd := fmt.Sprintf("%s inspect %s", settings.DefaultSealerBin, imageName)
   151  	_, err := exec.RunSimpleCmd(cmd)
   152  	return err == nil
   153  }