...

Source file src/github.com/chaos-mesh/chaos-mesh/controllers/utils/controller/finished_test.go

Documentation: github.com/chaos-mesh/chaos-mesh/controllers/utils/controller

     1  // Copyright 2021 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 controller
    17  
    18  import (
    19  	"fmt"
    20  	"testing"
    21  	"time"
    22  
    23  	. "github.com/onsi/gomega"
    24  	v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    25  	"k8s.io/utils/pointer"
    26  
    27  	"github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
    28  )
    29  
    30  func makeTestPodKill(creationTime time.Time, duration *string, desiredPhase v1alpha1.DesiredPhase, records []*v1alpha1.Record) v1alpha1.InnerObject {
    31  	return &v1alpha1.PodChaos{
    32  		ObjectMeta: v1.ObjectMeta{
    33  			CreationTimestamp: v1.Time{
    34  				Time: creationTime,
    35  			},
    36  		},
    37  		Spec: v1alpha1.PodChaosSpec{
    38  			Action:   v1alpha1.PodKillAction,
    39  			Duration: duration,
    40  		},
    41  		Status: v1alpha1.PodChaosStatus{
    42  			ChaosStatus: v1alpha1.ChaosStatus{
    43  				Experiment: v1alpha1.ExperimentStatus{
    44  					DesiredPhase: desiredPhase,
    45  					Records:      records,
    46  				},
    47  			},
    48  		},
    49  	}
    50  }
    51  func makeTestNetworkChaos(creationTime time.Time, duration *string, desiredPhase v1alpha1.DesiredPhase, records []*v1alpha1.Record) v1alpha1.InnerObject {
    52  	return &v1alpha1.NetworkChaos{
    53  		ObjectMeta: v1.ObjectMeta{
    54  			CreationTimestamp: v1.Time{
    55  				Time: creationTime,
    56  			},
    57  		},
    58  		Spec: v1alpha1.NetworkChaosSpec{
    59  			Duration: duration,
    60  		},
    61  		Status: v1alpha1.NetworkChaosStatus{
    62  			ChaosStatus: v1alpha1.ChaosStatus{
    63  				Experiment: v1alpha1.ExperimentStatus{
    64  					DesiredPhase: desiredPhase,
    65  					Records:      records,
    66  				},
    67  			},
    68  		},
    69  	}
    70  }
    71  
    72  func TestIsChaosFinished(t *testing.T) {
    73  	g := NewGomegaWithT(t)
    74  
    75  	type testCase struct {
    76  		chaos v1alpha1.InnerObject
    77  		now   time.Time
    78  
    79  		expected bool
    80  	}
    81  
    82  	beginTime := time.Now()
    83  	cases := []testCase{
    84  		{
    85  			chaos: makeTestNetworkChaos(beginTime, pointer.StringPtr("20s"), v1alpha1.RunningPhase, []*v1alpha1.Record{
    86  				{
    87  					Id:          "some",
    88  					SelectorKey: "some",
    89  					Phase:       v1alpha1.Injected,
    90  				},
    91  			}),
    92  			now: beginTime.Add(10 * time.Second),
    93  
    94  			expected: false,
    95  		},
    96  		{
    97  			chaos: makeTestNetworkChaos(beginTime, pointer.StringPtr("20s"), v1alpha1.RunningPhase, []*v1alpha1.Record{
    98  				{
    99  					Id:          "some",
   100  					SelectorKey: "some",
   101  					Phase:       v1alpha1.NotInjected,
   102  				},
   103  			}),
   104  			now: beginTime.Add(10 * time.Second),
   105  
   106  			expected: false,
   107  		},
   108  		{
   109  			chaos: makeTestNetworkChaos(beginTime, pointer.StringPtr("20s"), v1alpha1.RunningPhase, []*v1alpha1.Record{
   110  				{
   111  					Id:          "some",
   112  					SelectorKey: "some",
   113  					Phase:       v1alpha1.NotInjected,
   114  				},
   115  			}),
   116  			now: beginTime.Add(30 * time.Second),
   117  
   118  			expected: false,
   119  		},
   120  		{
   121  			chaos: makeTestNetworkChaos(beginTime, pointer.StringPtr("20s"), v1alpha1.StoppedPhase, []*v1alpha1.Record{
   122  				{
   123  					Id:          "some",
   124  					SelectorKey: "some",
   125  					Phase:       v1alpha1.NotInjected,
   126  				},
   127  			}),
   128  			now: beginTime.Add(30 * time.Second),
   129  
   130  			expected: true,
   131  		},
   132  		{
   133  			chaos: makeTestNetworkChaos(beginTime, nil, v1alpha1.RunningPhase, []*v1alpha1.Record{
   134  				{
   135  					Id:          "some",
   136  					SelectorKey: "some",
   137  					Phase:       v1alpha1.NotInjected,
   138  				},
   139  			}),
   140  			now: beginTime.Add(30 * time.Second),
   141  
   142  			expected: false,
   143  		},
   144  		{
   145  			chaos: makeTestNetworkChaos(beginTime, nil, v1alpha1.RunningPhase, []*v1alpha1.Record{
   146  				{
   147  					Id:          "some",
   148  					SelectorKey: "some",
   149  					Phase:       v1alpha1.Injected,
   150  				},
   151  			}),
   152  			now: beginTime.Add(30 * time.Second),
   153  
   154  			expected: false,
   155  		},
   156  		// The chaos is paused, but not recovered yet
   157  		{
   158  			chaos: makeTestNetworkChaos(beginTime, nil, v1alpha1.StoppedPhase, []*v1alpha1.Record{
   159  				{
   160  					Id:          "some",
   161  					SelectorKey: "some",
   162  					Phase:       v1alpha1.Injected,
   163  				},
   164  			}),
   165  			now: beginTime.Add(30 * time.Second),
   166  
   167  			expected: false,
   168  		},
   169  		{
   170  			chaos: makeTestPodKill(beginTime, pointer.StringPtr("20s"), v1alpha1.StoppedPhase, []*v1alpha1.Record{
   171  				{
   172  					Id:          "some",
   173  					SelectorKey: "some",
   174  					Phase:       v1alpha1.NotInjected,
   175  				},
   176  			}),
   177  			now: beginTime.Add(30 * time.Second),
   178  
   179  			expected: false,
   180  		},
   181  		{
   182  			chaos: makeTestPodKill(beginTime, pointer.StringPtr("20s"), v1alpha1.StoppedPhase, []*v1alpha1.Record{
   183  				{
   184  					Id:          "some",
   185  					SelectorKey: "some",
   186  					Phase:       v1alpha1.Injected,
   187  				},
   188  			}),
   189  			now: beginTime.Add(30 * time.Second),
   190  
   191  			expected: true,
   192  		},
   193  		{
   194  			chaos: makeTestPodKill(beginTime, nil, v1alpha1.StoppedPhase, []*v1alpha1.Record{
   195  				{
   196  					Id:          "some",
   197  					SelectorKey: "some",
   198  					Phase:       v1alpha1.Injected,
   199  				},
   200  			}),
   201  			now: beginTime.Add(30 * time.Second),
   202  
   203  			expected: true,
   204  		},
   205  	}
   206  
   207  	for index, c := range cases {
   208  		if index == 5 {
   209  			fmt.Println("some")
   210  		}
   211  		fmt.Println(index)
   212  		g.Expect(IsChaosFinished(c.chaos, c.now)).To(Equal(c.expected))
   213  	}
   214  }
   215