github.com/sealerio/sealer@v0.11.1-0.20240507115618-f4f89c5853ae/pkg/application/appdriver_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 application
    16  
    17  import (
    18  	"testing"
    19  
    20  	v1 "github.com/sealerio/sealer/pkg/define/application/v1"
    21  	"github.com/sealerio/sealer/pkg/define/application/version"
    22  	imagev1 "github.com/sealerio/sealer/pkg/define/image/v1"
    23  	"github.com/sealerio/sealer/types/api/constants"
    24  	v2 "github.com/sealerio/sealer/types/api/v2"
    25  	"github.com/stretchr/testify/assert"
    26  )
    27  
    28  func newTestApplication() (Interface, error) {
    29  	app := &v2.Application{
    30  		Spec: v2.ApplicationSpec{
    31  			LaunchApps: []string{"nginx1", "nginx2"},
    32  			Configs: []v2.ApplicationConfig{
    33  				{
    34  					Name: "nginx1",
    35  					Launch: &v2.Launch{
    36  						Cmds: []string{
    37  							"kubectl apply -f ns.yaml",
    38  							"kubectl apply -f nginx.yaml -n sealer-kube1",
    39  						},
    40  					},
    41  				},
    42  				{
    43  					Name: "nginx2",
    44  					Launch: &v2.Launch{
    45  						Cmds: []string{
    46  							"kubectl apply -f ns.yaml",
    47  							"kubectl apply -f nginx.yaml -n sealer-kube2",
    48  						},
    49  					},
    50  				},
    51  			},
    52  		},
    53  	}
    54  	app.Name = "my-app"
    55  	app.Kind = constants.ApplicationKind
    56  	app.APIVersion = v2.GroupVersion.String()
    57  
    58  	extension := imagev1.ImageExtension{
    59  		Launch: imagev1.Launch{
    60  			AppNames: []string{"nginx1", "nginx2"},
    61  		},
    62  		Applications: []version.VersionedApplication{
    63  			v1.NewV1Application(
    64  				"nginx1",
    65  				"kube",
    66  				[]string{"nginx1.yaml"},
    67  			),
    68  			v1.NewV1Application(
    69  				"nginx2",
    70  				"kube",
    71  				[]string{"nginx2.yaml"},
    72  			),
    73  			v1.NewV1Application(
    74  				"nginx3",
    75  				"kube",
    76  				[]string{"nginx3.yaml"},
    77  			),
    78  		},
    79  	}
    80  
    81  	driver, err := NewAppDriver(app, extension)
    82  	if err != nil {
    83  		return nil, err
    84  	}
    85  
    86  	return driver, nil
    87  }
    88  
    89  func TestV2Application_GetLaunchCmds(t *testing.T) {
    90  	driver, err := newTestApplication()
    91  	if err != nil {
    92  		assert.Error(t, err)
    93  	}
    94  
    95  	type args struct {
    96  		driver  Interface
    97  		appName string
    98  		wanted  []string
    99  	}
   100  	var tests = []struct {
   101  		name string
   102  		args args
   103  	}{
   104  		{
   105  			name: "get app launchCmds by name nginx1",
   106  			args: args{
   107  				driver:  driver,
   108  				appName: "nginx1",
   109  				wanted: []string{
   110  					"kubectl apply -f ns.yaml",
   111  					"kubectl apply -f nginx.yaml -n sealer-kube1",
   112  				},
   113  			},
   114  		},
   115  		{
   116  			name: "get app launchCmds by name nginx2",
   117  			args: args{
   118  				driver:  driver,
   119  				appName: "nginx2",
   120  				wanted: []string{
   121  					"kubectl apply -f ns.yaml",
   122  					"kubectl apply -f nginx.yaml -n sealer-kube2",
   123  				},
   124  			},
   125  		},
   126  	}
   127  
   128  	for _, tt := range tests {
   129  		t.Run(tt.name, func(t *testing.T) {
   130  			result := driver.GetAppLaunchCmds(tt.args.appName)
   131  			assert.Equal(t, tt.args.wanted, result)
   132  		})
   133  	}
   134  }
   135  
   136  func TestV2Application_GetAppNames(t *testing.T) {
   137  	driver, err := newTestApplication()
   138  	if err != nil {
   139  		assert.Error(t, err)
   140  	}
   141  
   142  	type args struct {
   143  		driver Interface
   144  		wanted []string
   145  	}
   146  	var tests = []struct {
   147  		name string
   148  		args args
   149  	}{{
   150  		name: "get app launch names",
   151  		args: args{
   152  			driver: driver,
   153  			wanted: []string{"nginx1", "nginx2"},
   154  		},
   155  	},
   156  	}
   157  
   158  	for _, tt := range tests {
   159  		t.Run(tt.name, func(t *testing.T) {
   160  			result := driver.GetAppNames()
   161  			assert.Equal(t, tt.args.wanted, result)
   162  		})
   163  	}
   164  }
   165  
   166  func TestV2Application_GetImageLaunchCmds(t *testing.T) {
   167  	driver, err := newTestApplication()
   168  	if err != nil {
   169  		assert.Error(t, err)
   170  	}
   171  
   172  	type args struct {
   173  		driver Interface
   174  		wanted []string
   175  	}
   176  	var tests = []struct {
   177  		name string
   178  		args args
   179  	}{{
   180  		name: "get image launch cmds",
   181  		args: args{
   182  			driver: driver,
   183  			wanted: []string{
   184  				"kubectl apply -f ns.yaml",
   185  				"kubectl apply -f nginx.yaml -n sealer-kube1",
   186  				"kubectl apply -f ns.yaml",
   187  				"kubectl apply -f nginx.yaml -n sealer-kube2",
   188  			},
   189  		},
   190  	},
   191  	}
   192  
   193  	for _, tt := range tests {
   194  		t.Run(tt.name, func(t *testing.T) {
   195  			result := driver.GetImageLaunchCmds()
   196  			assert.Equal(t, tt.args.wanted, result)
   197  		})
   198  	}
   199  }
   200  
   201  func TestFormatExtensionToApplication(t *testing.T) {
   202  	extension := imagev1.ImageExtension{
   203  		Env: map[string]string{"globalKey": "globalValue"},
   204  		Launch: imagev1.Launch{
   205  			Cmds:     []string{"kubectl apply -f nginx.yaml"},
   206  			AppNames: []string{"app1", "app2"},
   207  		},
   208  		Applications: []version.VersionedApplication{
   209  			&v1.Application{
   210  				NameVar:    "app1",
   211  				TypeVar:    "kube",
   212  				FilesVar:   []string{"/app1.yaml"},
   213  				VersionVar: "v1",
   214  				AppEnv:     map[string]string{"key1": "value1", "key2": "value2"},
   215  				AppCMDs:    []string{"kubectl apply -f app1.yaml -n nginx-namespace"},
   216  			},
   217  			&v1.Application{
   218  				NameVar:    "app2",
   219  				TypeVar:    "kube",
   220  				FilesVar:   []string{"/app2.yaml"},
   221  				VersionVar: "v1",
   222  				AppEnv:     map[string]string{"key3": "value3", "key4": "value4"},
   223  				AppCMDs:    []string{"kubectl apply -f app2.yaml -n nginx-namespace"},
   224  			},
   225  			&v1.Application{
   226  				NameVar:    "app3",
   227  				TypeVar:    "kube",
   228  				FilesVar:   []string{"/app3.yaml"},
   229  				VersionVar: "v1",
   230  				AppEnv:     map[string]string{"key5": "value5", "key6": "value6"},
   231  				AppCMDs:    []string{"kubectl apply -f app3.yaml -n nginx-namespace"},
   232  			},
   233  		},
   234  	}
   235  
   236  	appEnvMap := map[string]map[string]string{
   237  		"app1": {"key1": "value1", "key2": "value2", "globalKey": "globalValue"},
   238  		"app2": {"key3": "value3", "key4": "value4", "globalKey": "globalValue"},
   239  		"app3": {"key5": "value5", "key6": "value6", "globalKey": "globalValue"},
   240  	}
   241  
   242  	v2App := &applicationDriver{
   243  		app:            nil,
   244  		extension:      extension,
   245  		globalCmds:     []string{"kubectl apply -f nginx.yaml"},
   246  		globalEnv:      map[string]string{"globalKey": "globalValue"},
   247  		launchApps:     []string{"app1", "app2"},
   248  		registeredApps: []string{"app1", "app2", "app3"},
   249  		appLaunchCmdsMap: map[string][]string{
   250  			"app1": {"cd application/apps/app1/ && kubectl apply -f app1.yaml -n nginx-namespace"},
   251  			"app2": {"cd application/apps/app2/ && kubectl apply -f app2.yaml -n nginx-namespace"},
   252  			"app3": {"cd application/apps/app3/ && kubectl apply -f app3.yaml -n nginx-namespace"},
   253  		},
   254  		appRootMap: map[string]string{
   255  			"app1": "application/apps/app1/",
   256  			"app2": "application/apps/app2/",
   257  			"app3": "application/apps/app3/",
   258  		},
   259  		appEnvMap: appEnvMap,
   260  		appFileProcessorMap: map[string][]FileProcessor{
   261  			"app1": {envRender{envData: appEnvMap["app1"]}},
   262  			"app2": {envRender{envData: appEnvMap["app2"]}},
   263  			"app3": {envRender{envData: appEnvMap["app3"]}},
   264  		},
   265  	}
   266  
   267  	type args struct {
   268  		input  imagev1.ImageExtension
   269  		wanted *applicationDriver
   270  	}
   271  	var tests = []struct {
   272  		name string
   273  		args args
   274  	}{{
   275  		name: "format extension to application",
   276  		args: args{
   277  			input:  extension,
   278  			wanted: v2App,
   279  		},
   280  	},
   281  	}
   282  
   283  	for _, tt := range tests {
   284  		t.Run(tt.name, func(t *testing.T) {
   285  			result := formatImageExtension(tt.args.input)
   286  			assert.Equal(t, tt.args.wanted, result)
   287  		})
   288  	}
   289  }