github.com/alibaba/sealer@v0.8.6-0.20220430115802-37a2bdaa8173/pkg/plugin/taint_plugin_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 plugin
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/alibaba/sealer/logger"
    21  )
    22  
    23  func TestTaint_formatData(t *testing.T) {
    24  	type fields struct {
    25  		IPList    []string
    26  		TaintList TaintList
    27  	}
    28  	type args struct {
    29  		data string
    30  	}
    31  	tests := []struct {
    32  		name    string
    33  		fields  fields
    34  		args    args
    35  		wantErr bool
    36  	}{
    37  		{
    38  			"1",
    39  			fields{},
    40  			args{
    41  				data: "192.168.56.3 addKey1=addValue1:NoSchedule\n192.168.56.2 delKey1=delValue1:NoSchedule-\n192.168.56.3 addKey2=:NoSchedule\n192.168.56.1 delKey2=:NoSchedule-\n192.168.56.2 addKey3:NoSchedule\n192.168.56.4 delKey3:NoSchedule-\n",
    42  			},
    43  			false,
    44  		},
    45  		{
    46  			"invalid taint argument",
    47  			fields{},
    48  			args{
    49  				data: "192.168.56.3 addKey1==addValue1:NoSchedule\n",
    50  			},
    51  			true,
    52  		},
    53  		{
    54  			"invalid taint argument",
    55  			fields{},
    56  			args{
    57  				data: "192.168.56.3 addKey1=add:Value1:NoSchedule\n",
    58  			},
    59  			true,
    60  		},
    61  		{
    62  			"no key",
    63  			fields{},
    64  			args{
    65  				data: "192.168.56.3 =addValue1:NoSchedule\n",
    66  			},
    67  			true,
    68  		},
    69  		{
    70  			"no effect",
    71  			fields{},
    72  			args{
    73  				data: "192.168.56.3 addKey1=addValue1:\n",
    74  			},
    75  			true,
    76  		},
    77  	}
    78  	for _, tt := range tests {
    79  		t.Run(tt.name, func(t *testing.T) {
    80  			l := &Taint{
    81  				IPList:    tt.fields.IPList,
    82  				TaintList: map[string]*taintList{},
    83  			}
    84  			if err := l.formatData(tt.args.data); (err != nil) != tt.wantErr {
    85  				t.Errorf("formatData(%s) error = %v, wantErr %v", tt.args.data, err, tt.wantErr)
    86  			} else {
    87  				logger.Info("IPList:", l.IPList)
    88  				for k, v := range l.TaintList {
    89  					logger.Info("[%s] taints: %v", k, *v)
    90  				}
    91  			}
    92  		})
    93  	}
    94  }