github.com/alibaba/sealer@v0.8.6-0.20220430115802-37a2bdaa8173/pkg/config/config_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 config
    16  
    17  import (
    18  	"fmt"
    19  	"io/ioutil"
    20  	"testing"
    21  
    22  	v2 "github.com/alibaba/sealer/types/api/v2"
    23  
    24  	"github.com/alibaba/sealer/utils"
    25  
    26  	"github.com/alibaba/sealer/common"
    27  
    28  	v1 "github.com/alibaba/sealer/types/api/v1"
    29  )
    30  
    31  func TestDumper_Dump(t *testing.T) {
    32  	type fields struct {
    33  		configs     []v1.Config
    34  		clusterName string
    35  	}
    36  	type args struct {
    37  		clusterfile string
    38  	}
    39  	tests := []struct {
    40  		name    string
    41  		fields  fields
    42  		args    args
    43  		wantErr bool
    44  	}{
    45  		{
    46  			"test dump clusterfile configs",
    47  			fields{
    48  				configs:     nil,
    49  				clusterName: "my-cluster",
    50  			},
    51  			args{clusterfile: "test/test_clusterfile.yaml"},
    52  			false,
    53  		},
    54  	}
    55  	for _, tt := range tests {
    56  		t.Run(tt.name, func(t *testing.T) {
    57  			c := &Dumper{
    58  				Configs: tt.fields.configs,
    59  				Cluster: &v2.Cluster{},
    60  			}
    61  			c.Cluster.Name = tt.fields.clusterName
    62  			configs, err := utils.DecodeV1CRD(tt.args.clusterfile, common.Config)
    63  			if err != nil {
    64  				t.Error(err)
    65  				return
    66  			}
    67  			if err := c.Dump(configs.([]v1.Config)); (err != nil) != tt.wantErr {
    68  				t.Errorf("Dump() error = %v, wantErr %v", err, tt.wantErr)
    69  			}
    70  		})
    71  	}
    72  }
    73  
    74  func Test_getMergeConfig(t *testing.T) {
    75  	type args struct {
    76  		path string
    77  		data []byte
    78  	}
    79  	tests := []struct {
    80  		name    string
    81  		args    args
    82  		want    []byte
    83  		wantErr bool
    84  	}{
    85  		// TODO: Add test cases.
    86  		{
    87  			name: "test",
    88  			args: args{
    89  				data: []byte("spec:\n  image: kubernetes:v1.19.8"),
    90  				path: "test/test_clusterfile.yaml",
    91  			},
    92  		}, {
    93  			name: "test",
    94  			args: args{
    95  				data: []byte("spec:\n  template:\n    metadata:\n      labels:\n        name: tigera-operatorssssss"),
    96  				path: "test/tigera-operator.yaml",
    97  			},
    98  		},
    99  	}
   100  	for _, tt := range tests {
   101  		t.Run(tt.name, func(t *testing.T) {
   102  			got, err := getMergeConfigData(tt.args.path, tt.args.data)
   103  			if err != nil {
   104  				t.Error(err)
   105  				return
   106  			}
   107  			err = ioutil.WriteFile("test_"+tt.args.path, got, common.FileMode0644)
   108  			if err != nil {
   109  				t.Error(err)
   110  			}
   111  		})
   112  	}
   113  }
   114  
   115  func Test_convertSecretYaml(t *testing.T) {
   116  	testConfig := v1.Config{}
   117  	testConfig.Spec.Data = `
   118  global: e2FiYzogeHh4fQo=
   119  components: e215c3FsOntjcHU6e3JlcXVlc3Q6IDEwMDBtfX19Cg==`
   120  	testConfig.Spec.Process = "value|toJson|toBase64|toSecret"
   121  	type args struct {
   122  		config     v1.Config
   123  		configPath string
   124  	}
   125  	tests := []struct {
   126  		name string
   127  		args args
   128  	}{
   129  		{
   130  			"test secret convert to file (file exist)",
   131  			args{testConfig, "test/secret.yaml"},
   132  		},
   133  		{
   134  			"test secret convert to file (file not exist)",
   135  			args{testConfig, "test/secret1.yaml"},
   136  		},
   137  	}
   138  	for _, tt := range tests {
   139  		t.Run(tt.name, func(t *testing.T) {
   140  			got, err := convertSecretYaml(tt.args.config, tt.args.configPath)
   141  			if err != nil {
   142  				t.Errorf("convertSecretYaml() error = %v", err)
   143  				return
   144  			}
   145  			fmt.Println(string(got))
   146  		})
   147  	}
   148  }