...

Source file src/github.com/chaos-mesh/chaos-mesh/controllers/statuscheck/manager_test.go

Documentation: github.com/chaos-mesh/chaos-mesh/controllers/statuscheck

     1  // Copyright Chaos Mesh Authors.
     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  
    16  package statuscheck
    17  
    18  import (
    19  	"reflect"
    20  	"testing"
    21  	"time"
    22  
    23  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    24  	"k8s.io/apimachinery/pkg/types"
    25  
    26  	"github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
    27  )
    28  
    29  var (
    30  	key = types.NamespacedName{
    31  		Namespace: "default",
    32  		Name:      "result-cache-test",
    33  	}
    34  	record1 = v1alpha1.StatusCheckRecord{
    35  		StartTime: &metav1.Time{Time: time.Now()},
    36  		Outcome:   v1alpha1.StatusCheckOutcomeFailure,
    37  	}
    38  	record2 = v1alpha1.StatusCheckRecord{
    39  		StartTime: &metav1.Time{Time: time.Now().Add(5 * time.Second)},
    40  		Outcome:   v1alpha1.StatusCheckOutcomeSuccess,
    41  	}
    42  )
    43  
    44  func Test_limitRecords(t *testing.T) {
    45  	type args struct {
    46  		records []v1alpha1.StatusCheckRecord
    47  		limit   uint
    48  	}
    49  	tests := []struct {
    50  		name string
    51  		args args
    52  		want []v1alpha1.StatusCheckRecord
    53  	}{
    54  		{
    55  			name: "not exceeded",
    56  			args: args{
    57  				records: []v1alpha1.StatusCheckRecord{record1},
    58  				limit:   2,
    59  			},
    60  			want: []v1alpha1.StatusCheckRecord{record1},
    61  		},
    62  		{
    63  			name: "exceeded",
    64  			args: args{
    65  				records: []v1alpha1.StatusCheckRecord{record1, record2},
    66  				limit:   1,
    67  			},
    68  			want: []v1alpha1.StatusCheckRecord{record2},
    69  		},
    70  		{
    71  			name: "empty",
    72  			args: args{
    73  				records: []v1alpha1.StatusCheckRecord{},
    74  				limit:   1,
    75  			},
    76  			want: []v1alpha1.StatusCheckRecord{},
    77  		},
    78  	}
    79  	for _, tt := range tests {
    80  		t.Run(tt.name, func(t *testing.T) {
    81  			if got := limitRecords(tt.args.records, tt.args.limit); !reflect.DeepEqual(got, tt.want) {
    82  				t.Errorf("limitRecords() = %v, want %v", got, tt.want)
    83  			}
    84  		})
    85  	}
    86  }
    87  
    88  func Test_resultCache_get(t *testing.T) {
    89  	type fields struct {
    90  		results map[types.NamespacedName]Result
    91  	}
    92  	type args struct {
    93  		key types.NamespacedName
    94  	}
    95  	tests := []struct {
    96  		name   string
    97  		fields fields
    98  		args   args
    99  		want   Result
   100  		want1  bool
   101  	}{
   102  		{
   103  			name: "found",
   104  			fields: fields{
   105  				results: map[types.NamespacedName]Result{
   106  					key: {
   107  						Records:             []v1alpha1.StatusCheckRecord{record1},
   108  						Count:               1,
   109  						recordsHistoryLimit: 1,
   110  					},
   111  				},
   112  			},
   113  			args: args{key: key},
   114  			want: Result{
   115  				Records:             []v1alpha1.StatusCheckRecord{record1},
   116  				Count:               1,
   117  				recordsHistoryLimit: 1,
   118  			},
   119  			want1: true,
   120  		},
   121  		{
   122  			name: "not found",
   123  			fields: fields{
   124  				results: map[types.NamespacedName]Result{
   125  					key: {
   126  						Records:             []v1alpha1.StatusCheckRecord{record1},
   127  						Count:               1,
   128  						recordsHistoryLimit: 1,
   129  					},
   130  				},
   131  			},
   132  			args: args{key: types.NamespacedName{
   133  				Namespace: "default",
   134  				Name:      "cache-test",
   135  			}},
   136  			want:  Result{},
   137  			want1: false,
   138  		},
   139  	}
   140  	for _, tt := range tests {
   141  		t.Run(tt.name, func(t *testing.T) {
   142  			c := &resultCache{
   143  				results: tt.fields.results,
   144  			}
   145  			got, got1 := c.get(tt.args.key)
   146  			if !reflect.DeepEqual(got, tt.want) {
   147  				t.Errorf("get() got = %v, want %v", got, tt.want)
   148  			}
   149  			if got1 != tt.want1 {
   150  				t.Errorf("get() got1 = %v, want %v", got1, tt.want1)
   151  			}
   152  		})
   153  	}
   154  }
   155  
   156  func Test_resultCache_append(t *testing.T) {
   157  	type fields struct {
   158  		results map[types.NamespacedName]Result
   159  	}
   160  	type args struct {
   161  		key types.NamespacedName
   162  		obj v1alpha1.StatusCheckRecord
   163  	}
   164  	tests := []struct {
   165  		name   string
   166  		fields fields
   167  		args   args
   168  		want   Result
   169  		want1  bool
   170  	}{
   171  		{
   172  			name: "append",
   173  			fields: fields{
   174  				results: map[types.NamespacedName]Result{
   175  					key: {
   176  						Records:             []v1alpha1.StatusCheckRecord{record1},
   177  						Count:               1,
   178  						recordsHistoryLimit: 2,
   179  					},
   180  				},
   181  			},
   182  			args: args{
   183  				key: key,
   184  				obj: record2,
   185  			},
   186  			want: Result{
   187  				Records:             []v1alpha1.StatusCheckRecord{record1, record2},
   188  				Count:               2,
   189  				recordsHistoryLimit: 2,
   190  			},
   191  			want1: true,
   192  		},
   193  	}
   194  	for _, tt := range tests {
   195  		t.Run(tt.name, func(t *testing.T) {
   196  			c := &resultCache{
   197  				results: tt.fields.results,
   198  			}
   199  			c.append(tt.args.key, tt.args.obj)
   200  			got, got1 := c.get(tt.args.key)
   201  			if !reflect.DeepEqual(got, tt.want) {
   202  				t.Errorf("append() got = %v, want %v", got, tt.want)
   203  			}
   204  			if got1 != tt.want1 {
   205  				t.Errorf("append() got1 = %v, want %v", got1, tt.want1)
   206  			}
   207  		})
   208  	}
   209  }
   210  
   211  func Test_resultCache_delete(t *testing.T) {
   212  	type fields struct {
   213  		results map[types.NamespacedName]Result
   214  	}
   215  	type args struct {
   216  		key types.NamespacedName
   217  	}
   218  	tests := []struct {
   219  		name   string
   220  		fields fields
   221  		args   args
   222  	}{
   223  		{
   224  			name: "delete",
   225  			fields: fields{
   226  				results: map[types.NamespacedName]Result{
   227  					key: {
   228  						Records:             []v1alpha1.StatusCheckRecord{record1},
   229  						Count:               1,
   230  						recordsHistoryLimit: 1,
   231  					},
   232  				},
   233  			},
   234  			args: args{key: key},
   235  		},
   236  	}
   237  	for _, tt := range tests {
   238  		t.Run(tt.name, func(t *testing.T) {
   239  			c := &resultCache{
   240  				results: tt.fields.results,
   241  			}
   242  			c.delete(tt.args.key)
   243  			_, ok := c.get(tt.args.key)
   244  			if ok {
   245  				t.Errorf("object exists after delete it")
   246  			}
   247  		})
   248  	}
   249  }
   250