...

Source file src/github.com/chaos-mesh/chaos-mesh/controllers/statuscheck/conditions_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  	"testing"
    20  
    21  	"github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
    22  )
    23  
    24  func Test_isThresholdExceed(t *testing.T) {
    25  	type args struct {
    26  		records   []v1alpha1.StatusCheckRecord
    27  		outcome   v1alpha1.StatusCheckOutcome
    28  		threshold int
    29  	}
    30  	tests := []struct {
    31  		name string
    32  		args args
    33  		want bool
    34  	}{
    35  		{
    36  			name: "not exceed",
    37  			args: args{
    38  				records: []v1alpha1.StatusCheckRecord{
    39  					{Outcome: v1alpha1.StatusCheckOutcomeFailure},
    40  					{Outcome: v1alpha1.StatusCheckOutcomeSuccess},
    41  					{Outcome: v1alpha1.StatusCheckOutcomeFailure},
    42  				},
    43  				outcome:   v1alpha1.StatusCheckOutcomeFailure,
    44  				threshold: 2,
    45  			},
    46  			want: false,
    47  		},
    48  		{
    49  			name: "exceed",
    50  			args: args{
    51  				records: []v1alpha1.StatusCheckRecord{
    52  					{Outcome: v1alpha1.StatusCheckOutcomeFailure},
    53  				},
    54  				outcome:   v1alpha1.StatusCheckOutcomeFailure,
    55  				threshold: 1,
    56  			},
    57  			want: true,
    58  		},
    59  		{
    60  			name: "exceed",
    61  			args: args{
    62  				records: []v1alpha1.StatusCheckRecord{
    63  					{Outcome: v1alpha1.StatusCheckOutcomeSuccess},
    64  					{Outcome: v1alpha1.StatusCheckOutcomeFailure},
    65  					{Outcome: v1alpha1.StatusCheckOutcomeFailure},
    66  				},
    67  				outcome:   v1alpha1.StatusCheckOutcomeFailure,
    68  				threshold: 2,
    69  			},
    70  			want: true,
    71  		},
    72  		{
    73  			name: "threshold is bigger than the length of record",
    74  			args: args{
    75  				records: []v1alpha1.StatusCheckRecord{
    76  					{Outcome: v1alpha1.StatusCheckOutcomeFailure},
    77  					{Outcome: v1alpha1.StatusCheckOutcomeFailure},
    78  				},
    79  				outcome:   v1alpha1.StatusCheckOutcomeFailure,
    80  				threshold: 3,
    81  			},
    82  			want: false,
    83  		},
    84  	}
    85  	for _, tt := range tests {
    86  		t.Run(tt.name, func(t *testing.T) {
    87  			if got := isThresholdExceed(tt.args.records, tt.args.outcome, tt.args.threshold); got != tt.want {
    88  				t.Errorf("isThresholdExceed() = %v, want %v", got, tt.want)
    89  			}
    90  		})
    91  	}
    92  }
    93