github.com/howieyuen/kusion@v0.7.4-rc.2/pkg/projectstack/stack_test.go (about)

     1  //go:build !arm64
     2  // +build !arm64
     3  
     4  package projectstack
     5  
     6  import (
     7  	"os"
     8  	"path/filepath"
     9  	"reflect"
    10  	"testing"
    11  
    12  	"bou.ke/monkey"
    13  
    14  	"github.com/howieyuen/kusion/pkg/util/json"
    15  )
    16  
    17  func TestFindStackPath(t *testing.T) {
    18  	_ = os.Chdir(filepath.Join(TestStackPathAA, "ci-test"))
    19  	defer os.Chdir(TestCurrentDir)
    20  
    21  	tests := []struct {
    22  		name    string
    23  		want    string
    24  		wantErr bool
    25  	}{
    26  		{
    27  			name:    "success",
    28  			want:    filepath.Join(TestCurrentDir, TestStackPathAA),
    29  			wantErr: false,
    30  		},
    31  	}
    32  	for _, tt := range tests {
    33  		t.Run(tt.name, func(t *testing.T) {
    34  			got, err := FindStackPath()
    35  			if (err != nil) != tt.wantErr {
    36  				t.Errorf("FindStackPath() error = %v, wantErr %v", err, tt.wantErr)
    37  				return
    38  			}
    39  			if got != tt.want {
    40  				t.Errorf("FindStackPath() = %v, want %v", got, tt.want)
    41  			}
    42  		})
    43  	}
    44  }
    45  
    46  func TestFindStackPathFrom(t *testing.T) {
    47  	type args struct {
    48  		path string
    49  	}
    50  	tests := []struct {
    51  		name    string
    52  		args    args
    53  		want    string
    54  		wantErr bool
    55  	}{
    56  		{
    57  			name: "success",
    58  			args: args{
    59  				path: "./testdata/appops/http-echo/dev/ci-test",
    60  			},
    61  			want:    "testdata/appops/http-echo/dev",
    62  			wantErr: false,
    63  		},
    64  	}
    65  	for _, tt := range tests {
    66  		t.Run(tt.name, func(t *testing.T) {
    67  			got, err := FindStackPathFrom(tt.args.path)
    68  			if (err != nil) != tt.wantErr {
    69  				t.Errorf("FindStackPathFrom() error = %v, wantErr %v", err, tt.wantErr)
    70  				return
    71  			}
    72  			if got != tt.want {
    73  				t.Errorf("FindStackPathFrom() = %v, want %v", got, tt.want)
    74  			}
    75  		})
    76  	}
    77  }
    78  
    79  func TestIsStack(t *testing.T) {
    80  	type args struct {
    81  		path string
    82  	}
    83  	tests := []struct {
    84  		name string
    85  		args args
    86  		want bool
    87  	}{
    88  		{
    89  			name: "is-stack",
    90  			args: args{
    91  				path: "./testdata/appops/http-echo/dev/ci-test",
    92  			},
    93  			want: false,
    94  		},
    95  		{
    96  			name: "is-not-stack",
    97  			args: args{
    98  				path: "./testdata/appops/http-echo/dev",
    99  			},
   100  			want: true,
   101  		},
   102  	}
   103  	for _, tt := range tests {
   104  		t.Run(tt.name, func(t *testing.T) {
   105  			if got := IsStack(tt.args.path); got != tt.want {
   106  				t.Errorf("IsStack() = %v, want %v", got, tt.want)
   107  			}
   108  		})
   109  	}
   110  }
   111  
   112  func TestParseStackConfiguration(t *testing.T) {
   113  	type args struct {
   114  		path string
   115  	}
   116  	tests := []struct {
   117  		name    string
   118  		args    args
   119  		want    *StackConfiguration
   120  		wantErr bool
   121  	}{
   122  		{
   123  			name: "success",
   124  			args: args{
   125  				path: "./testdata/appops/http-echo/dev",
   126  			},
   127  			want: &StackConfiguration{
   128  				Name: TestStackA,
   129  			},
   130  			wantErr: false,
   131  		},
   132  		{
   133  			name: "fail",
   134  			args: args{
   135  				path: "./testdata/appops/http-echo",
   136  			},
   137  			want:    nil,
   138  			wantErr: true,
   139  		},
   140  	}
   141  	for _, tt := range tests {
   142  		t.Run(tt.name, func(t *testing.T) {
   143  			got, err := ParseStackConfiguration(tt.args.path)
   144  			if (err != nil) != tt.wantErr {
   145  				t.Errorf("ParseStackConfiguration() error = %v, wantErr %v", err, tt.wantErr)
   146  				return
   147  			}
   148  			if !reflect.DeepEqual(got, tt.want) {
   149  				t.Errorf("ParseStackConfiguration() = %v, want %v", got, tt.want)
   150  			}
   151  		})
   152  	}
   153  }
   154  
   155  func TestFindAllStacks(t *testing.T) {
   156  	_ = os.Chdir(TestProjectPathA)
   157  	defer os.Chdir(TestCurrentDir)
   158  
   159  	tests := []struct {
   160  		name    string
   161  		want    []*Stack
   162  		wantErr bool
   163  	}{
   164  		{
   165  			name: "given-project-path",
   166  			want: []*Stack{
   167  				{
   168  					StackConfiguration: StackConfiguration{
   169  						Name: TestStackA,
   170  					},
   171  					Path: filepath.Join(TestCurrentDir, TestStackPathAA),
   172  				},
   173  			},
   174  			wantErr: false,
   175  		},
   176  	}
   177  	for _, tt := range tests {
   178  		t.Run(tt.name, func(t *testing.T) {
   179  			got, err := FindAllStacks()
   180  			if (err != nil) != tt.wantErr {
   181  				t.Errorf("FindAllStacks() error = %v, wantErr %v", err, tt.wantErr)
   182  				return
   183  			}
   184  			if !reflect.DeepEqual(got, tt.want) {
   185  				t.Errorf("FindAllStacks() = %v, want %v", json.MustMarshal2PrettyString(got), json.MustMarshal2PrettyString(tt.want))
   186  			}
   187  		})
   188  	}
   189  }
   190  
   191  func TestFindAllStacksFrom(t *testing.T) {
   192  	type args struct {
   193  		path string
   194  	}
   195  	tests := []struct {
   196  		name    string
   197  		args    args
   198  		want    []*Stack
   199  		wantErr bool
   200  	}{
   201  		{
   202  			name: "given-project-path",
   203  			args: args{
   204  				path: "./testdata/appops/http-echo",
   205  			},
   206  			want: []*Stack{
   207  				{
   208  					StackConfiguration: StackConfiguration{
   209  						Name: TestStackA,
   210  					},
   211  					Path: filepath.Join(TestCurrentDir, TestStackPathAA),
   212  				},
   213  			},
   214  			wantErr: false,
   215  		},
   216  		{
   217  			name: "give-project-path-with-two-stacks",
   218  			args: args{
   219  				path: "./testdata/appops/nginx-example",
   220  			},
   221  			want: []*Stack{
   222  				{
   223  					StackConfiguration: StackConfiguration{
   224  						Name: TestStackA,
   225  					},
   226  					Path: filepath.Join(TestCurrentDir, TestStackPathBA),
   227  				},
   228  				{
   229  					StackConfiguration: StackConfiguration{
   230  						Name: TestStackB,
   231  					},
   232  					Path: filepath.Join(TestCurrentDir, TestStackPathBB),
   233  				},
   234  			},
   235  			wantErr: false,
   236  		},
   237  		{
   238  			name: "given-stack-path",
   239  			args: args{
   240  				path: "./testdata/appops/http-echo/dev/",
   241  			},
   242  			want: []*Stack{
   243  				{
   244  					StackConfiguration: StackConfiguration{
   245  						Name: TestStackA,
   246  					},
   247  					Path: filepath.Join(TestCurrentDir, TestStackPathAA),
   248  				},
   249  			},
   250  			wantErr: false,
   251  		},
   252  		{
   253  			name: "given-no-stack-path",
   254  			args: args{
   255  				path: "./testdata/appops/http-echo/gray/",
   256  			},
   257  			want:    []*Stack{},
   258  			wantErr: false,
   259  		},
   260  	}
   261  	for _, tt := range tests {
   262  		t.Run(tt.name, func(t *testing.T) {
   263  			got, err := FindAllStacksFrom(tt.args.path)
   264  			if (err != nil) != tt.wantErr {
   265  				t.Errorf("FindAllStacksFrom() error = %v, wantErr %v", err, tt.wantErr)
   266  				return
   267  			}
   268  			if !reflect.DeepEqual(got, tt.want) {
   269  				t.Errorf("FindAllStacksFrom() = %v, want %v", json.MustMarshal2PrettyString(got), json.MustMarshal2PrettyString(tt.want))
   270  			}
   271  		})
   272  	}
   273  }
   274  
   275  func TestGetStack(t *testing.T) {
   276  	_ = os.Chdir(TestStackPathAA)
   277  	defer os.Chdir(TestCurrentDir)
   278  
   279  	tests := []struct {
   280  		name    string
   281  		want    *Stack
   282  		wantErr bool
   283  		preRun  func()
   284  		postRun func()
   285  	}{
   286  		{
   287  			name: "success",
   288  			want: &Stack{
   289  				StackConfiguration: StackConfiguration{
   290  					Name: TestStackA,
   291  				},
   292  				Path: filepath.Join(TestCurrentDir, TestStackPathAA),
   293  			},
   294  			wantErr: false,
   295  			preRun:  func() {},
   296  			postRun: func() {},
   297  		},
   298  		{
   299  			name:    "fail-for-GetStackFrom",
   300  			want:    nil,
   301  			wantErr: true,
   302  			preRun: func() {
   303  				mockGetStackFrom(ErrFake)
   304  			},
   305  			postRun: func() {
   306  				defer monkey.UnpatchAll()
   307  			},
   308  		},
   309  	}
   310  	for _, tt := range tests {
   311  		t.Run(tt.name, func(t *testing.T) {
   312  			tt.preRun()
   313  			got, err := GetStack()
   314  			tt.postRun()
   315  			if (err != nil) != tt.wantErr {
   316  				t.Errorf("GetStack() error = %v, wantErr %v", err, tt.wantErr)
   317  				return
   318  			}
   319  			if !reflect.DeepEqual(got, tt.want) {
   320  				t.Errorf("GetStack() = %v, want %v", got, tt.want)
   321  			}
   322  		})
   323  	}
   324  }
   325  
   326  func TestGetStackFrom(t *testing.T) {
   327  	type args struct {
   328  		path string
   329  	}
   330  	tests := []struct {
   331  		name    string
   332  		args    args
   333  		want    *Stack
   334  		wantErr bool
   335  	}{
   336  		{
   337  			name: "success",
   338  			args: args{
   339  				path: "./testdata/appops/http-echo/dev/",
   340  			},
   341  			want: &Stack{
   342  				StackConfiguration: StackConfiguration{
   343  					Name: TestStackA,
   344  				},
   345  				Path: filepath.Join(TestCurrentDir, TestStackPathAA),
   346  			},
   347  			wantErr: false,
   348  		},
   349  		{
   350  			name: "failed-because-no-stack-path",
   351  			args: args{
   352  				path: TestProjectPathA,
   353  			},
   354  			want:    nil,
   355  			wantErr: true,
   356  		},
   357  		{
   358  			name: "failed-because-not-existed-path",
   359  			args: args{
   360  				path: "./testdata-not-exist",
   361  			},
   362  			want:    nil,
   363  			wantErr: true,
   364  		},
   365  	}
   366  	for _, tt := range tests {
   367  		t.Run(tt.name, func(t *testing.T) {
   368  			got, err := GetStackFrom(tt.args.path)
   369  			if (err != nil) != tt.wantErr {
   370  				t.Errorf("GetStackFrom() error = %v, wantErr %v", err, tt.wantErr)
   371  				return
   372  			}
   373  			if !reflect.DeepEqual(got, tt.want) {
   374  				t.Errorf("GetStackFrom() = %v, want %v", got, tt.want)
   375  			}
   376  		})
   377  	}
   378  }
   379  
   380  func TestIsStackFile(t *testing.T) {
   381  	type args struct {
   382  		path string
   383  	}
   384  	tests := []struct {
   385  		name string
   386  		args args
   387  		want bool
   388  	}{
   389  		{
   390  			name: "is-stack-file",
   391  			args: args{
   392  				path: filepath.Join(TestStackPathAA, StackFile),
   393  			},
   394  			want: true,
   395  		},
   396  		{
   397  			name: "is-not-stack-file",
   398  			args: args{
   399  				path: TestStackPathAA,
   400  			},
   401  			want: false,
   402  		},
   403  		{
   404  			name: "is-not-stack-file-2",
   405  			args: args{
   406  				path: filepath.Join(TestStackPathAA, "main.k"),
   407  			},
   408  			want: false,
   409  		},
   410  	}
   411  	for _, tt := range tests {
   412  		t.Run(tt.name, func(t *testing.T) {
   413  			if got := IsStackFile(tt.args.path); got != tt.want {
   414  				t.Errorf("IsStackFile() = %v, want %v", got, tt.want)
   415  			}
   416  		})
   417  	}
   418  }