github.com/sealerio/sealer@v0.11.1-0.20240507115618-f4f89c5853ae/pkg/infradriver/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 infradriver
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/stretchr/testify/assert"
    21  	k8sv1 "k8s.io/api/core/v1"
    22  )
    23  
    24  func TestFormatData(t *testing.T) {
    25  	type args struct {
    26  		data   string
    27  		wanted k8sv1.Taint
    28  	}
    29  
    30  	var tests = []struct {
    31  		name string
    32  		args args
    33  	}{
    34  		{
    35  			"test format date: key1=value1:NoSchedule",
    36  			args{
    37  				data: "key1=value1:NoSchedule",
    38  				wanted: k8sv1.Taint{
    39  					Key:    "key1",
    40  					Value:  "value1",
    41  					Effect: k8sv1.TaintEffect("NoSchedule"),
    42  				},
    43  			},
    44  		},
    45  		{
    46  			"test format date: key2:PreferNoSchedule",
    47  			args{
    48  				data: "key2=:PreferNoSchedule",
    49  				wanted: k8sv1.Taint{
    50  					Key:    "key2",
    51  					Value:  "",
    52  					Effect: k8sv1.TaintEffect("PreferNoSchedule"),
    53  				},
    54  			},
    55  		},
    56  		{
    57  			"test format date: key3=:NoExecute",
    58  			args{
    59  				data: "key3=:NoExecute",
    60  				wanted: k8sv1.Taint{
    61  					Key:    "key3",
    62  					Value:  "",
    63  					Effect: k8sv1.TaintEffect("NoExecute"),
    64  				},
    65  			},
    66  		},
    67  		{
    68  			"test format date: key4:NoExecute-",
    69  			args{
    70  				data: "key4:NoExecute-",
    71  				wanted: k8sv1.Taint{
    72  					Key:    "key4",
    73  					Value:  "",
    74  					Effect: k8sv1.TaintEffect("NoExecute-"),
    75  				},
    76  			},
    77  		},
    78  		{
    79  			"test format date: key7-",
    80  			args{
    81  				data: "key7-",
    82  				wanted: k8sv1.Taint{
    83  					Key:    "key7-",
    84  					Value:  "",
    85  					Effect: k8sv1.TaintEffect(""),
    86  				},
    87  			},
    88  		},
    89  	}
    90  
    91  	for _, tt := range tests {
    92  		t.Run(tt.name, func(t *testing.T) {
    93  			result, err := formatData(tt.args.data)
    94  			if err != nil {
    95  				t.Errorf("failed to format data, error:%v", err)
    96  			}
    97  			assert.Equal(t, tt.args.wanted, result)
    98  		})
    99  	}
   100  }