github.com/alibaba/sealer@v0.8.6-0.20220430115802-37a2bdaa8173/pkg/env/env_test.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 env
    16  
    17  import (
    18  	"reflect"
    19  	"testing"
    20  
    21  	v1 "github.com/alibaba/sealer/types/api/v1"
    22  	v2 "github.com/alibaba/sealer/types/api/v2"
    23  )
    24  
    25  func Test_convertEnv(t *testing.T) {
    26  	type args struct {
    27  		envList []string
    28  	}
    29  	tests := []struct {
    30  		name    string
    31  		args    args
    32  		wantEnv map[string]interface{}
    33  	}{
    34  		{
    35  			"test convert env",
    36  			args{envList: []string{"IP=127.0.0.1;127.0.0.2;127.0.0.3", "IP=192.168.0.2", "key=value"}},
    37  			map[string]interface{}{"IP": []string{"127.0.0.1", "127.0.0.2", "127.0.0.3", "192.168.0.2"}, "key": "value"},
    38  		},
    39  	}
    40  	for _, tt := range tests {
    41  		t.Run(tt.name, func(t *testing.T) {
    42  			if gotEnv := ConvertEnv(tt.args.envList); !reflect.DeepEqual(gotEnv, tt.wantEnv) {
    43  				t.Errorf("convertEnv() = %v, want %v", gotEnv, tt.wantEnv)
    44  			}
    45  		})
    46  	}
    47  }
    48  
    49  func getTestCluster() *v2.Cluster {
    50  	return &v2.Cluster{
    51  		Spec: v2.ClusterSpec{
    52  			Image: "",
    53  			Env:   []string{"IP=127.0.0.1", "key=value"},
    54  			Hosts: []v2.Host{
    55  				{
    56  					IPS:   []string{"192.168.0.2", "192.168.0.3", "192.168.0.4"},
    57  					Roles: []string{"master"},
    58  					Env:   []string{"key=bar", "key=foo", "foo=bar", "IP=127.0.0.2"},
    59  				},
    60  			},
    61  			SSH: v1.SSH{},
    62  		},
    63  	}
    64  }
    65  
    66  func Test_processor_WrapperShell(t *testing.T) {
    67  	type fields struct {
    68  		Cluster *v2.Cluster
    69  	}
    70  	type args struct {
    71  		host  string
    72  		shell string
    73  	}
    74  	tests := []struct {
    75  		name   string
    76  		fields fields
    77  		args   args
    78  		want   string
    79  	}{
    80  		{
    81  			"test command ENV",
    82  			fields{Cluster: getTestCluster()},
    83  			args{
    84  				host:  "192.168.0.2",
    85  				shell: "echo $foo ${IP[@]}",
    86  			},
    87  			"key=(bar foo) foo=bar IP=127.0.0.2 && echo $foo ${IP[@]}",
    88  		},
    89  	}
    90  	for _, tt := range tests {
    91  		t.Run(tt.name, func(t *testing.T) {
    92  			p := &processor{
    93  				Cluster: tt.fields.Cluster,
    94  			}
    95  			if got := p.WrapperShell(tt.args.host, tt.args.shell); got != tt.want {
    96  				t.Errorf("WrapperShell() = %v, want %v", got, tt.want)
    97  			}
    98  		})
    99  	}
   100  }
   101  
   102  func Test_processor_RenderAll(t *testing.T) {
   103  	type fields struct {
   104  		Cluster *v2.Cluster
   105  	}
   106  	type args struct {
   107  		host string
   108  		dir  string
   109  	}
   110  	tests := []struct {
   111  		name    string
   112  		fields  fields
   113  		args    args
   114  		wantErr bool
   115  	}{
   116  		{
   117  			"test render dir",
   118  			fields{getTestCluster()},
   119  			args{
   120  				host: "192.168.0.2",
   121  				dir:  "test/template",
   122  			},
   123  			false,
   124  		},
   125  	}
   126  
   127  	for _, tt := range tests {
   128  		t.Run(tt.name, func(t *testing.T) {
   129  			p := &processor{
   130  				Cluster: tt.fields.Cluster,
   131  			}
   132  			if err := p.RenderAll(tt.args.host, tt.args.dir); (err != nil) != tt.wantErr {
   133  				t.Errorf("RenderAll() error = %v, wantErr %v", err, tt.wantErr)
   134  			}
   135  		})
   136  	}
   137  }