github.com/alibaba/sealer@v0.8.6-0.20220430115802-37a2bdaa8173/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  	"bytes"
    19  	"fmt"
    20  	"io/ioutil"
    21  	"os"
    22  	"path/filepath"
    23  
    24  	"github.com/onsi/gomega"
    25  
    26  	"github.com/alibaba/sealer/test/testhelper/settings"
    27  	"github.com/alibaba/sealer/utils"
    28  )
    29  
    30  func GetFixtures() string {
    31  	return filepath.Join(settings.DefaultTestEnvDir, "suites", "build", "fixtures")
    32  }
    33  
    34  func GetLiteBuildDir() string {
    35  	return "lite_build"
    36  }
    37  
    38  func GetCloudBuildDir() string {
    39  	return "cloud_build"
    40  }
    41  
    42  func GetContainerBuildDir() string {
    43  	return "container_build"
    44  }
    45  
    46  // GetTestImageName return specific image name that will be push to registry
    47  func GetTestImageName() string {
    48  	return fmt.Sprintf("registry.cn-qingdao.aliyuncs.com/sealer-io/%s%d:%s", settings.ImageName, 719, "v1")
    49  }
    50  
    51  type ArgsOfBuild struct {
    52  	KubeFile, ImageName, Context, BuildType string
    53  }
    54  
    55  func (a *ArgsOfBuild) SetKubeFile(kubeFile string) *ArgsOfBuild {
    56  	a.KubeFile = kubeFile
    57  	return a
    58  }
    59  
    60  func (a *ArgsOfBuild) SetImageName(imageName string) *ArgsOfBuild {
    61  	a.ImageName = imageName
    62  	return a
    63  }
    64  
    65  func (a *ArgsOfBuild) SetContext(context string) *ArgsOfBuild {
    66  	a.Context = context
    67  	return a
    68  }
    69  
    70  func (a *ArgsOfBuild) SetBuildType(buildType string) *ArgsOfBuild {
    71  	a.BuildType = buildType
    72  	return a
    73  }
    74  
    75  func (a *ArgsOfBuild) Build() string {
    76  	if settings.DefaultSealerBin == "" || a.KubeFile == "" || a.ImageName == "" {
    77  		return ""
    78  	}
    79  
    80  	if a.Context == "" {
    81  		a.Context = "."
    82  	}
    83  
    84  	if a.BuildType == "" {
    85  		a.BuildType = settings.LiteBuild
    86  	}
    87  	return fmt.Sprintf("%s build -f %s -t %s -m %s %s -d", settings.DefaultSealerBin, a.KubeFile, a.ImageName, a.BuildType, a.Context)
    88  }
    89  
    90  func NewArgsOfBuild() *ArgsOfBuild {
    91  	return &ArgsOfBuild{}
    92  }
    93  
    94  func CheckIsImageExist(imageName string) bool {
    95  	cmd := fmt.Sprintf("%s inspect %s", settings.DefaultSealerBin, imageName)
    96  	_, err := utils.RunSimpleCmd(cmd)
    97  	return err == nil
    98  }
    99  
   100  func UpdateKubeFromImage(imageName string, KubefilePath string) {
   101  	Kube, err := ioutil.ReadFile(filepath.Clean(KubefilePath))
   102  	gomega.Expect(err).NotTo(gomega.HaveOccurred())
   103  	Kube = append([]byte(fmt.Sprintf("FROM %s", imageName)), Kube[bytes.IndexByte(Kube, '\n'):]...) // #nosec
   104  	err = ioutil.WriteFile(KubefilePath, Kube, os.ModePerm)
   105  	gomega.Expect(err).NotTo(gomega.HaveOccurred())
   106  }