github.com/abayer/test-infra@v0.0.5/velodrome/transform/plugins/states_test.go (about)

     1  /*
     2  Copyright 2017 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package plugins
    18  
    19  import (
    20  	"reflect"
    21  	"testing"
    22  	"time"
    23  )
    24  
    25  func TestActiveStateActive(t *testing.T) {
    26  	state := ActiveState{
    27  		startTime: time.Unix(0, 0),
    28  		exit:      LabelEvent{Label: "test"},
    29  	}
    30  
    31  	got := state.Active()
    32  	want := true
    33  
    34  	if got != want {
    35  		t.Errorf("%#v.Active() = %t, want %t", state, got, want)
    36  	}
    37  }
    38  
    39  func TestActiveStateAge(t *testing.T) {
    40  	state := ActiveState{
    41  		startTime: time.Unix(0, 0),
    42  		exit:      LabelEvent{Label: "test"},
    43  	}
    44  
    45  	got := state.Age(time.Unix(0, 10))
    46  	want := time.Duration(10)
    47  	if got != want {
    48  		t.Errorf("%#v.Age(time.Unix(0, 10)) = %s, want %s", state, got, want)
    49  	}
    50  }
    51  
    52  func TestActiveStateReceiveMatchingEvent(t *testing.T) {
    53  	state := ActiveState{
    54  		startTime: time.Unix(0, 0),
    55  		exit:      LabelEvent{Label: "test"},
    56  	}
    57  
    58  	gotState, gotChanged := state.ReceiveEvent("labeled", "test", time.Unix(0, 10))
    59  	wantState := &InactiveState{UnlabelEvent{Label: "test"}}
    60  	wantChanged := true
    61  	if !reflect.DeepEqual(gotState, wantState) || gotChanged != wantChanged {
    62  		t.Errorf(`%#v.ReceiveEvent("labeled", "test", _) = (%#v, %t), want (%#v, %t)`,
    63  			state,
    64  			gotState, gotChanged,
    65  			wantState, wantChanged)
    66  	}
    67  }
    68  
    69  func TestActiveStateReceiveNonMatchingEvent(t *testing.T) {
    70  	state := ActiveState{
    71  		startTime: time.Unix(0, 0),
    72  		exit:      LabelEvent{Label: "test"},
    73  	}
    74  
    75  	gotState, gotChanged := state.ReceiveEvent("labeled", "non-matching", time.Unix(0, 10))
    76  	wantState := &state
    77  	wantChanged := false
    78  	if !reflect.DeepEqual(gotState, wantState) || gotChanged != wantChanged {
    79  		t.Errorf(`%#v.ReceiveEvent("labeled", "non-matching", _) = (%#v, %t), want (%#v, %t)`,
    80  			state,
    81  			gotState, gotChanged,
    82  			wantState, wantChanged)
    83  	}
    84  }
    85  
    86  func TestInactiveStateActive(t *testing.T) {
    87  	state := InactiveState{
    88  		entry: LabelEvent{Label: "test"},
    89  	}
    90  
    91  	got := state.Active()
    92  	want := false
    93  
    94  	if got != want {
    95  		t.Errorf("%#v.Active() = %t, want %t", state, got, want)
    96  	}
    97  }
    98  
    99  func TestInactiveStateReceiveMatchingEvent(t *testing.T) {
   100  	state := InactiveState{
   101  		entry: LabelEvent{Label: "test"},
   102  	}
   103  
   104  	gotState, gotChanged := state.ReceiveEvent("labeled", "test", time.Unix(0, 10))
   105  	wantState := &ActiveState{startTime: time.Unix(0, 10), exit: UnlabelEvent{Label: "test"}}
   106  	wantChanged := true
   107  	if !reflect.DeepEqual(gotState, wantState) || gotChanged != wantChanged {
   108  		t.Errorf(`%#v.ReceiveEvent("labeled", "test", _) = (%#v, %t), want (%#v, %t)`,
   109  			state,
   110  			gotState, gotChanged,
   111  			wantState, wantChanged)
   112  	}
   113  }
   114  
   115  func TestInactiveStateReceiveNonMatchingEvent(t *testing.T) {
   116  	state := InactiveState{
   117  		entry: LabelEvent{Label: "test"},
   118  	}
   119  
   120  	gotState, gotChanged := state.ReceiveEvent("labeled", "non-matching", time.Unix(0, 10))
   121  	wantState := &state
   122  	wantChanged := false
   123  	if !reflect.DeepEqual(gotState, wantState) || gotChanged != wantChanged {
   124  		t.Errorf(`%#v.ReceiveEvent("labeled", "non-matching", _) = (%#v, %t), want (%#v, %t)`,
   125  			state,
   126  			gotState, gotChanged,
   127  			wantState, wantChanged)
   128  	}
   129  }
   130  
   131  func TestMultiStateActive(t *testing.T) {
   132  	// All states are active
   133  	state := &MultiState{
   134  		[]State{
   135  			&ActiveState{exit: MergeEvent{}, startTime: time.Unix(0, 10)},
   136  			&ActiveState{exit: CloseEvent{}, startTime: time.Unix(0, 20)},
   137  		},
   138  	}
   139  
   140  	got := state.Active()
   141  	want := true
   142  	if got != want {
   143  		t.Errorf("%#v.Active() = %t, want %t", state, got, want)
   144  	}
   145  }
   146  
   147  func TestMultiStateActiveAge(t *testing.T) {
   148  	// All states are active, Age returns time since latest active
   149  	state := &MultiState{
   150  		[]State{
   151  			&ActiveState{exit: MergeEvent{}, startTime: time.Unix(0, 10)},
   152  			&ActiveState{exit: CloseEvent{}, startTime: time.Unix(0, 20)},
   153  		},
   154  	}
   155  
   156  	got := state.Age(time.Unix(0, 30))
   157  	want := time.Duration(10)
   158  	if got != want {
   159  		t.Errorf("%#v.Age(time.Unix(0, 30)) = %s, want %s", state, got, want)
   160  	}
   161  }
   162  
   163  func TestMultiStateInactive(t *testing.T) {
   164  	// One state is inactive
   165  	state := &MultiState{
   166  		[]State{
   167  			&ActiveState{exit: MergeEvent{}, startTime: time.Unix(0, 10)},
   168  			&InactiveState{entry: CloseEvent{}},
   169  		},
   170  	}
   171  
   172  	got := state.Active()
   173  	want := false
   174  	if got != want {
   175  		t.Errorf("%#v.Active() = %t, want %t", state, got, want)
   176  	}
   177  }
   178  
   179  func TestMultiStateReceiveEvent(t *testing.T) {
   180  	var want, got, state State
   181  	var wantChanged, gotChanged bool
   182  	// We are looking for "merged,!closed", i.e. "merged" but not "closed"
   183  	state = &MultiState{
   184  		[]State{
   185  			&InactiveState{entry: MergeEvent{}},
   186  			&ActiveState{exit: CloseEvent{}, startTime: time.Time{}},
   187  		},
   188  	}
   189  	got, gotChanged = state.ReceiveEvent("closed", "", time.Unix(0, 10))
   190  	want, wantChanged = &MultiState{
   191  		[]State{
   192  			&InactiveState{entry: MergeEvent{}},
   193  			&InactiveState{entry: ReopenEvent{}},
   194  		},
   195  	}, true
   196  	if !reflect.DeepEqual(got, want) || gotChanged != wantChanged {
   197  		t.Errorf(`%#v.ReceiveEvent("closed", "", _) = (%#v, %t), want (%#v, %t)`,
   198  			state, got, gotChanged, want, wantChanged)
   199  	}
   200  
   201  	state = got
   202  	got, gotChanged = state.ReceiveEvent("merged", "", time.Unix(0, 20))
   203  	want, wantChanged = &MultiState{
   204  		[]State{
   205  			&ActiveState{exit: FalseEvent{}, startTime: time.Unix(0, 20)},
   206  			&InactiveState{entry: ReopenEvent{}},
   207  		},
   208  	}, true
   209  	if !reflect.DeepEqual(got, want) || gotChanged != wantChanged {
   210  		t.Errorf(`%#v.ReceiveEvent("merged", "", time.Unix(0, 20)) = (%#v, %t), want (%#v, %t)`,
   211  			state, got, gotChanged, want, wantChanged)
   212  	}
   213  
   214  	state = got
   215  	got, gotChanged = state.ReceiveEvent("reopened", "", time.Unix(0, 30))
   216  	want, wantChanged = &MultiState{
   217  		[]State{
   218  			&ActiveState{exit: FalseEvent{}, startTime: time.Unix(0, 20)},
   219  			&ActiveState{exit: CloseEvent{}, startTime: time.Unix(0, 30)},
   220  		},
   221  	}, true
   222  	if !reflect.DeepEqual(got, want) || gotChanged != wantChanged {
   223  		t.Errorf(`%#v.ReceiveEvent("merged", "", time.Unix(0, 20)) = (%#v, %t), want (%#v, %t)`,
   224  			state, got, gotChanged, want, wantChanged)
   225  	}
   226  }
   227  
   228  func TestNewState(t *testing.T) {
   229  	tests := []struct {
   230  		description string
   231  		state       State
   232  	}{
   233  		// Empty description generates impossible state.
   234  		{
   235  			description: "",
   236  			state:       &InactiveState{entry: FalseEvent{}},
   237  		},
   238  		// Single event state.
   239  		{
   240  			description: "merged",
   241  			state: &MultiState{
   242  				[]State{
   243  					&InactiveState{entry: MergeEvent{}},
   244  				},
   245  			},
   246  		},
   247  		// Comma separated multi-event state with active sub-state.
   248  		{
   249  			description: "merged,!closed",
   250  			state: &MultiState{
   251  				[]State{
   252  					&InactiveState{entry: MergeEvent{}},
   253  					&ActiveState{exit: CloseEvent{}, startTime: time.Time{}},
   254  				},
   255  			},
   256  		},
   257  	}
   258  
   259  	for _, test := range tests {
   260  		got := NewState(test.description)
   261  		want := test.state
   262  		if !reflect.DeepEqual(got, want) {
   263  			t.Errorf("NewState(%v) = %#v, want %#v", test.description, got, want)
   264  		}
   265  	}
   266  }