github.com/sealerio/sealer@v0.11.1-0.20240507115618-f4f89c5853ae/build/kubefile/parser/utils_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  	"os"
    19  	"path/filepath"
    20  	"testing"
    21  
    22  	"github.com/sealerio/sealer/pkg/define/application"
    23  
    24  	"github.com/stretchr/testify/assert"
    25  )
    26  
    27  func TestIsHelm(t *testing.T) {
    28  	t.Run("charts under targets root", func(t *testing.T) {
    29  		dir, err := os.MkdirTemp("/tmp/", "sealer-test")
    30  		if err != nil {
    31  			t.Error(err)
    32  			return
    33  		}
    34  		defer func() {
    35  			_ = os.RemoveAll(dir)
    36  		}()
    37  
    38  		targets := []string{
    39  			filepath.Join(dir, "values.yaml"),
    40  			filepath.Join(dir, "Chart.yaml"),
    41  			filepath.Join(dir, "templates"),
    42  		}
    43  		for _, tar := range targets {
    44  			if _, err := os.Create(tar); err != nil {
    45  				t.Error(err)
    46  				return
    47  			}
    48  		}
    49  
    50  		isH, err := isHelm(targets...)
    51  		if err != nil {
    52  			t.Error(err)
    53  			return
    54  		}
    55  		assert.Equal(t, true, isH)
    56  	})
    57  
    58  	t.Run("charts under dir of target", func(t *testing.T) {
    59  		githubAppChartsPath := "./test/brigade-github-app"
    60  		isH, err := isHelm(githubAppChartsPath)
    61  		if err != nil {
    62  			t.Error(err)
    63  			return
    64  		}
    65  		assert.Equal(t, true, isH)
    66  	})
    67  
    68  	t.Run("no charts under the targets", func(t *testing.T) {
    69  		nginxPath := "./test/kube-nginx-deployment"
    70  		isH, err := isHelm(nginxPath)
    71  		if err != nil {
    72  			t.Error(err)
    73  			return
    74  		}
    75  		assert.Equal(t, false, isH)
    76  	})
    77  
    78  	// There is a problem
    79  	// if the charts does not exist in the first layer of targets
    80  	// could it be the charts?
    81  	t.Run("kube and charts exist same time", func(t *testing.T) {
    82  		targets := []string{
    83  			"./test/kube-nginx-deployment",
    84  			"./test/brigade-github-app",
    85  		}
    86  
    87  		isH, err := isHelm(targets...)
    88  		if err != nil {
    89  			t.Error(err)
    90  			return
    91  		}
    92  		assert.Equal(t, true, isH)
    93  	})
    94  
    95  	t.Run("charts under subdir of target dir", func(t *testing.T) {
    96  		targets := []string{
    97  			"./test/",
    98  		}
    99  
   100  		isH, err := isHelm(targets...)
   101  		if err != nil {
   102  			t.Error(err)
   103  			return
   104  		}
   105  		assert.Equal(t, false, isH)
   106  	})
   107  }
   108  
   109  func TestGetApplicationType(t *testing.T) {
   110  	type args struct {
   111  		preparedFunc func() (string, error)
   112  		wanted       string
   113  	}
   114  	var tests = []struct {
   115  		name string
   116  		args args
   117  	}{
   118  		{
   119  			name: "it is helm application",
   120  			args: args{
   121  				preparedFunc: func() (string, error) {
   122  					dir, err := os.MkdirTemp("/tmp/", "sealer-test")
   123  					if err != nil {
   124  						t.Error(err)
   125  					}
   126  					targets := []string{
   127  						filepath.Join(dir, "values.yaml"),
   128  						filepath.Join(dir, "Chart.yaml"),
   129  						filepath.Join(dir, "templates"),
   130  					}
   131  					for _, tar := range targets {
   132  						if _, err := os.Create(tar); err != nil {
   133  							t.Error(err)
   134  						}
   135  					}
   136  
   137  					return dir, nil
   138  				},
   139  				wanted: application.HelmApp,
   140  			},
   141  		},
   142  		{
   143  			name: "it is kube yaml application",
   144  			args: args{
   145  				preparedFunc: func() (string, error) {
   146  					dir, err := os.MkdirTemp("/tmp/", "sealer-test")
   147  					if err != nil {
   148  						t.Error(err)
   149  					}
   150  					targets := []string{
   151  						filepath.Join(dir, "a.yaml"),
   152  						filepath.Join(dir, "b.yaml"),
   153  						filepath.Join(dir, "c.yaml"),
   154  					}
   155  					for _, tar := range targets {
   156  						if _, err := os.Create(tar); err != nil {
   157  							t.Error(err)
   158  						}
   159  					}
   160  
   161  					return dir, nil
   162  				},
   163  				wanted: application.KubeApp,
   164  			},
   165  		},
   166  		{
   167  			name: "it is shell application",
   168  			args: args{
   169  				preparedFunc: func() (string, error) {
   170  					dir, err := os.MkdirTemp("/tmp/", "sealer-test")
   171  					if err != nil {
   172  						t.Error(err)
   173  					}
   174  					targets := []string{
   175  						filepath.Join(dir, "a.sh"),
   176  						filepath.Join(dir, "v.sh"),
   177  						filepath.Join(dir, "c.sh"),
   178  					}
   179  					for _, tar := range targets {
   180  						if _, err := os.Create(tar); err != nil {
   181  							t.Error(err)
   182  						}
   183  					}
   184  
   185  					return dir, nil
   186  				},
   187  				wanted: application.ShellApp,
   188  			},
   189  		},
   190  		{
   191  			name: "it is mixed application",
   192  			args: args{
   193  				preparedFunc: func() (string, error) {
   194  					dir, err := os.MkdirTemp("/tmp/", "sealer-test")
   195  					if err != nil {
   196  						t.Error(err)
   197  					}
   198  					targets := []string{
   199  						filepath.Join(dir, "a.sh"),
   200  						filepath.Join(dir, "t.yaml"),
   201  						filepath.Join(dir, "c.sh"),
   202  					}
   203  					for _, tar := range targets {
   204  						if _, err := os.Create(tar); err != nil {
   205  							t.Error(err)
   206  						}
   207  					}
   208  
   209  					return dir, nil
   210  				},
   211  				wanted: "",
   212  			},
   213  		},
   214  	}
   215  
   216  	for _, tt := range tests {
   217  		t.Run(tt.name, func(t *testing.T) {
   218  			dir, err := tt.args.preparedFunc()
   219  			if err != nil {
   220  				assert.Errorf(t, err, "failed to create test files for %s: %v", tt.name, err)
   221  			}
   222  			defer func() {
   223  				_ = os.RemoveAll(dir)
   224  			}()
   225  
   226  			appType, err := getApplicationType([]string{dir})
   227  			if err != nil {
   228  				t.Error(err)
   229  				return
   230  			}
   231  
   232  			assert.Equal(t, tt.args.wanted, appType)
   233  		})
   234  	}
   235  }