github.com/sealerio/sealer@v0.11.1-0.20240507115618-f4f89c5853ae/build/kubefile/parser/parse_test.go (about)

     1  // Copyright © 2022 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 parser
    16  
    17  import (
    18  	"bytes"
    19  	"fmt"
    20  	"os"
    21  	"strings"
    22  	"testing"
    23  
    24  	platformParse "github.com/containers/buildah/pkg/parse"
    25  	"github.com/stretchr/testify/assert"
    26  
    27  	"github.com/sealerio/sealer/pkg/define/application"
    28  	v1 "github.com/sealerio/sealer/pkg/define/application/v1"
    29  	"github.com/sealerio/sealer/pkg/define/application/version"
    30  	"github.com/sealerio/sealer/pkg/define/options"
    31  )
    32  
    33  const (
    34  // nginxDemoURL = "https://raw.githubusercontent.com/kubernetes/website/main/content/en/examples/application/deployment.yaml"
    35  )
    36  
    37  var testParser *KubefileParser
    38  
    39  var testAppRootPath = "/test/apps"
    40  
    41  func TestParserKubeApp(t *testing.T) {
    42  	nginxDemoPath := "./test/kube-nginx-deployment/deployment.yaml"
    43  	imageEngine := testImageEngine{}
    44  	opts := options.BuildOptions{
    45  		PullPolicy: "missing",
    46  	}
    47  
    48  	buildCxt, err := setupTempContext()
    49  	assert.Equal(t, nil, err)
    50  	defer func() {
    51  		_ = os.RemoveAll(buildCxt)
    52  	}()
    53  
    54  	opts.ContextDir = buildCxt
    55  	testParser = NewParser(testAppRootPath, opts, imageEngine, platformParse.DefaultPlatform())
    56  
    57  	var (
    58  		app1Name = "nginx"
    59  		app1Path = testParser.appRootPathFunc(app1Name)
    60  		text     = fmt.Sprintf(`
    61  FROM scratch
    62  APP %s local://%s
    63  LAUNCH ["%s"]
    64  `, app1Name, nginxDemoPath, app1Name)
    65  	)
    66  
    67  	reader := bytes.NewReader([]byte(text))
    68  	result, err := testParser.ParseKubefile(reader)
    69  	if err != nil {
    70  		t.Fatalf("failed to parse kubefile: %s", err)
    71  	}
    72  	defer func() {
    73  		_ = result.CleanLegacyContext()
    74  	}()
    75  
    76  	var expectedText = fmt.Sprintf(`
    77  FROM scratch
    78  copy %s %s
    79  `,
    80  		strings.Join(result.legacyContext.apps2Files[app1Name], " "),
    81  		app1Path,
    82  	)
    83  
    84  	result.Dockerfile = strings.TrimSpace(result.Dockerfile)
    85  	expectedResult := &KubefileResult{
    86  		Dockerfile:       strings.TrimSpace(expectedText),
    87  		LaunchedAppNames: []string{app1Name},
    88  		Applications: map[string]version.VersionedApplication{
    89  			app1Name: v1.NewV1Application(
    90  				app1Name,
    91  				application.KubeApp,
    92  				[]string{nginxDemoPath},
    93  			),
    94  		},
    95  	}
    96  
    97  	assert.Equal(t, expectedResult.Dockerfile, result.Dockerfile)
    98  	assert.Equal(t, len(expectedResult.Applications), len(result.Applications))
    99  	assert.Equal(t, expectedResult.Applications[app1Name].Name(), result.Applications[app1Name].Name())
   100  	assert.Equal(t, expectedResult.Applications[app1Name].Type(), result.Applications[app1Name].Type())
   101  	assert.Equal(t, expectedResult.LaunchedAppNames, result.LaunchedAppNames)
   102  }
   103  
   104  func TestParserHelmApp(t *testing.T) {
   105  	githubAppPath := "./test/brigade-github-app"
   106  	imageEngine := testImageEngine{}
   107  	opts := options.BuildOptions{
   108  		PullPolicy: "missing",
   109  	}
   110  
   111  	buildCxt, err := setupTempContext()
   112  	assert.Equal(t, nil, err)
   113  	defer func() {
   114  		_ = os.RemoveAll(buildCxt)
   115  	}()
   116  
   117  	opts.ContextDir = buildCxt
   118  	testParser = NewParser(testAppRootPath, opts, imageEngine, platformParse.DefaultPlatform())
   119  
   120  	var (
   121  		app1Name = "github-app"
   122  		app1Path = testParser.appRootPathFunc(app1Name)
   123  		text     = fmt.Sprintf(`
   124  FROM scratch
   125  APP %s local://%s
   126  LAUNCH %s
   127  `, app1Name, githubAppPath, app1Name)
   128  	)
   129  
   130  	reader := bytes.NewReader([]byte(text))
   131  	result, err := testParser.ParseKubefile(reader)
   132  	if err != nil {
   133  		t.Fatalf("failed to parse kubefile: %s", err)
   134  	}
   135  	defer func() {
   136  		_ = result.CleanLegacyContext()
   137  	}()
   138  
   139  	var expectedText = fmt.Sprintf(`
   140  FROM scratch
   141  copy %s %s
   142  `,
   143  		strings.Join(result.legacyContext.apps2Files[app1Name], " "),
   144  		app1Path,
   145  	)
   146  
   147  	result.Dockerfile = strings.TrimSpace(result.Dockerfile)
   148  	expectedResult := &KubefileResult{
   149  		Dockerfile:       strings.TrimSpace(expectedText),
   150  		LaunchedAppNames: []string{app1Name},
   151  		Applications: map[string]version.VersionedApplication{
   152  			app1Name: v1.NewV1Application(
   153  				app1Name,
   154  				application.HelmApp,
   155  				[]string{githubAppPath},
   156  			),
   157  		},
   158  	}
   159  
   160  	assert.Equal(t, expectedResult.Dockerfile, result.Dockerfile)
   161  	assert.Equal(t, len(expectedResult.Applications), len(result.Applications))
   162  	assert.Equal(t, expectedResult.Applications[app1Name].Name(), result.Applications[app1Name].Name())
   163  	assert.Equal(t, expectedResult.Applications[app1Name].Type(), result.Applications[app1Name].Type())
   164  	assert.Equal(t, expectedResult.LaunchedAppNames, result.LaunchedAppNames)
   165  }
   166  
   167  func TestParserCMDS(t *testing.T) {
   168  	imageEngine := testImageEngine{}
   169  	opts := options.BuildOptions{
   170  		PullPolicy: "missing",
   171  	}
   172  
   173  	buildCxt, err := setupTempContext()
   174  	assert.Equal(t, nil, err)
   175  	defer func() {
   176  		_ = os.RemoveAll(buildCxt)
   177  	}()
   178  
   179  	opts.ContextDir = buildCxt
   180  	testParser = NewParser(testAppRootPath, opts, imageEngine, platformParse.DefaultPlatform())
   181  
   182  	var (
   183  		text = fmt.Sprintf(`
   184  FROM scratch
   185  CMDS ["%s", "%s"]
   186  `, "kubectl apply -f abc.yaml", "kubectl apply -f bcd.yaml")
   187  	)
   188  
   189  	reader := bytes.NewReader([]byte(text))
   190  	result, err := testParser.ParseKubefile(reader)
   191  	if err != nil {
   192  		t.Fatalf("failed to parse kubefile: %s", err)
   193  	}
   194  	defer func() {
   195  		_ = result.CleanLegacyContext()
   196  	}()
   197  
   198  	var expectedText = `
   199  FROM scratch
   200  `
   201  
   202  	result.Dockerfile = strings.TrimSpace(result.Dockerfile)
   203  	expectedResult := &KubefileResult{
   204  		Dockerfile: strings.TrimSpace(expectedText),
   205  		RawCmds: []string{
   206  			"kubectl apply -f abc.yaml",
   207  			"kubectl apply -f bcd.yaml",
   208  		},
   209  		Applications: map[string]version.VersionedApplication{},
   210  	}
   211  
   212  	assert.Equal(t, expectedResult.Dockerfile, result.Dockerfile)
   213  	assert.Equal(t, len(expectedResult.Applications), len(result.Applications))
   214  	assert.Equal(t, expectedResult.RawCmds, result.RawCmds)
   215  }
   216  
   217  func TestParserEnv(t *testing.T) {
   218  	appFilePath := "./test/kube-nginx-deployment/deployment.yaml"
   219  	imageEngine := testImageEngine{}
   220  	opts := options.BuildOptions{
   221  		PullPolicy: "missing",
   222  	}
   223  
   224  	buildCxt, err := setupTempContext()
   225  	assert.Equal(t, nil, err)
   226  	defer func() {
   227  		_ = os.RemoveAll(buildCxt)
   228  	}()
   229  
   230  	opts.ContextDir = buildCxt
   231  	testParser = NewParser(testAppRootPath, opts, imageEngine, platformParse.DefaultPlatform())
   232  
   233  	var (
   234  		text = fmt.Sprintf(`
   235  FROM scratch 
   236  APP app1 local://%s
   237  ENV globalKey=globalValue
   238  APPENV app1 key1=value1 key2=value2
   239  APPENV app1 key1=value3 key2=value3
   240  LAUNCH ["app1"]`, appFilePath)
   241  	)
   242  
   243  	reader := bytes.NewReader([]byte(text))
   244  	result, err := testParser.ParseKubefile(reader)
   245  	if err != nil {
   246  		t.Fatalf("failed to parse kubefile: %s", err)
   247  	}
   248  	defer func() {
   249  		_ = result.CleanLegacyContext()
   250  	}()
   251  
   252  	expectedResult := &KubefileResult{
   253  		GlobalEnv: map[string]string{
   254  			"globalKey": "globalValue",
   255  		},
   256  		AppEnvMap: map[string]map[string]string{
   257  			"app1": {"key1": "value3", "key2": "value3"},
   258  		},
   259  	}
   260  
   261  	assert.Equal(t, expectedResult.GlobalEnv, result.GlobalEnv)
   262  	assert.Equal(t, expectedResult.AppEnvMap, result.AppEnvMap)
   263  }
   264  
   265  func setupTempContext() (string, error) {
   266  	tmpDir, err := os.MkdirTemp("/tmp/", "sealer-test")
   267  	if err != nil {
   268  		return "", err
   269  	}
   270  
   271  	return tmpDir, nil
   272  }