github.com/nmstate/kubernetes-nmstate@v0.82.0/pkg/enactmentstatus/conditions/counter_test.go (about)

     1  /*
     2  Copyright The Kubernetes NMState Authors.
     3  
     4  
     5  Licensed under the Apache License, Version 2.0 (the "License");
     6  you may not use this file except in compliance with the License.
     7  You may obtain a copy of the License at
     8  
     9      http://www.apache.org/licenses/LICENSE-2.0
    10  
    11  Unless required by applicable law or agreed to in writing, software
    12  distributed under the License is distributed on an "AS IS" BASIS,
    13  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  See the License for the specific language governing permissions and
    15  limitations under the License.
    16  */
    17  
    18  package conditions
    19  
    20  import (
    21  	. "github.com/onsi/ginkgo/v2"
    22  	. "github.com/onsi/gomega"
    23  
    24  	corev1 "k8s.io/api/core/v1"
    25  
    26  	nmstate "github.com/nmstate/kubernetes-nmstate/api/shared"
    27  	nmstatev1beta1 "github.com/nmstate/kubernetes-nmstate/api/v1beta1"
    28  )
    29  
    30  const (
    31  	failing     = nmstate.NodeNetworkConfigurationEnactmentConditionFailing
    32  	available   = nmstate.NodeNetworkConfigurationEnactmentConditionAvailable
    33  	progressing = nmstate.NodeNetworkConfigurationEnactmentConditionProgressing
    34  	pending     = nmstate.NodeNetworkConfigurationEnactmentConditionPending
    35  	aborted     = nmstate.NodeNetworkConfigurationEnactmentConditionAborted
    36  	t           = corev1.ConditionTrue
    37  	f           = corev1.ConditionFalse
    38  	u           = corev1.ConditionUnknown
    39  )
    40  
    41  type setter = func(*nmstate.ConditionList, string)
    42  
    43  func enactments(enactments ...nmstatev1beta1.NodeNetworkConfigurationEnactment) nmstatev1beta1.NodeNetworkConfigurationEnactmentList {
    44  	return nmstatev1beta1.NodeNetworkConfigurationEnactmentList{
    45  		Items: append([]nmstatev1beta1.NodeNetworkConfigurationEnactment{}, enactments...),
    46  	}
    47  }
    48  
    49  func enactment(policyGeneration int64, setters ...setter) nmstatev1beta1.NodeNetworkConfigurationEnactment {
    50  	enactment := nmstatev1beta1.NodeNetworkConfigurationEnactment{
    51  		Status: nmstate.NodeNetworkConfigurationEnactmentStatus{
    52  			PolicyGeneration: policyGeneration,
    53  			Conditions:       nmstate.ConditionList{},
    54  		},
    55  	}
    56  	for _, setter := range setters {
    57  		setter(&enactment.Status.Conditions, "")
    58  	}
    59  	return enactment
    60  }
    61  
    62  var _ = Describe("Enactment condition counter", func() {
    63  	type EnactmentCounterCase struct {
    64  		enactmentsToCount nmstatev1beta1.NodeNetworkConfigurationEnactmentList
    65  		policyGeneration  int64
    66  		expectedCount     ConditionCount
    67  	}
    68  	DescribeTable("the enactments statuses", func(c EnactmentCounterCase) {
    69  		obtainedCount := Count(c.enactmentsToCount, c.policyGeneration)
    70  		Expect(obtainedCount).To(Equal(c.expectedCount))
    71  	},
    72  		Entry("e(), e()", EnactmentCounterCase{
    73  			policyGeneration: 1,
    74  			enactmentsToCount: enactments(
    75  				enactment(1),
    76  				enactment(1),
    77  			),
    78  			expectedCount: ConditionCount{
    79  				available:   CountByConditionStatus{t: 0, f: 0, u: 2},
    80  				failing:     CountByConditionStatus{t: 0, f: 0, u: 2},
    81  				progressing: CountByConditionStatus{t: 0, f: 0, u: 2},
    82  				pending:     CountByConditionStatus{t: 0, f: 0, u: 2},
    83  				aborted:     CountByConditionStatus{t: 0, f: 0, u: 2},
    84  			},
    85  		}),
    86  		Entry("e(Failed), e(Progressing)", EnactmentCounterCase{
    87  			policyGeneration: 1,
    88  			enactmentsToCount: enactments(
    89  				enactment(1, SetFailedToConfigure),
    90  				enactment(1, SetProgressing),
    91  			),
    92  			expectedCount: ConditionCount{
    93  				available:   CountByConditionStatus{t: 0, f: 1, u: 1},
    94  				failing:     CountByConditionStatus{t: 1, f: 0, u: 1},
    95  				progressing: CountByConditionStatus{t: 1, f: 1, u: 0},
    96  				pending:     CountByConditionStatus{t: 0, f: 2, u: 0},
    97  				aborted:     CountByConditionStatus{t: 0, f: 2, u: 0},
    98  			},
    99  		}),
   100  		Entry("e(Success), e(Progressing)", EnactmentCounterCase{
   101  			policyGeneration: 1,
   102  			enactmentsToCount: enactments(
   103  				enactment(1, SetSuccess),
   104  				enactment(1, SetProgressing),
   105  			),
   106  			expectedCount: ConditionCount{
   107  				available:   CountByConditionStatus{t: 1, f: 0, u: 1},
   108  				failing:     CountByConditionStatus{t: 0, f: 1, u: 1},
   109  				progressing: CountByConditionStatus{t: 1, f: 1, u: 0},
   110  				pending:     CountByConditionStatus{t: 0, f: 2, u: 0},
   111  				aborted:     CountByConditionStatus{t: 0, f: 2, u: 0},
   112  			},
   113  		}),
   114  		Entry("e(Progressing), e(Progressing)", EnactmentCounterCase{
   115  			policyGeneration: 1,
   116  			enactmentsToCount: enactments(
   117  				enactment(1, SetProgressing),
   118  				enactment(1, SetProgressing),
   119  			),
   120  			expectedCount: ConditionCount{
   121  				available:   CountByConditionStatus{t: 0, f: 0, u: 2},
   122  				failing:     CountByConditionStatus{t: 0, f: 0, u: 2},
   123  				progressing: CountByConditionStatus{t: 2, f: 0, u: 0},
   124  				pending:     CountByConditionStatus{t: 0, f: 2, u: 0},
   125  				aborted:     CountByConditionStatus{t: 0, f: 2, u: 0},
   126  			},
   127  		}),
   128  		Entry("e(Success), e(Success)", EnactmentCounterCase{
   129  			policyGeneration: 1,
   130  			enactmentsToCount: enactments(
   131  				enactment(1, SetSuccess),
   132  				enactment(1, SetSuccess),
   133  			),
   134  			expectedCount: ConditionCount{
   135  				available:   CountByConditionStatus{t: 2, f: 0, u: 0},
   136  				failing:     CountByConditionStatus{t: 0, f: 2, u: 0},
   137  				progressing: CountByConditionStatus{t: 0, f: 2, u: 0},
   138  				pending:     CountByConditionStatus{t: 0, f: 2, u: 0},
   139  				aborted:     CountByConditionStatus{t: 0, f: 2, u: 0},
   140  			},
   141  		}),
   142  		Entry("e(Failed), e(Failed)", EnactmentCounterCase{
   143  			policyGeneration: 1,
   144  			enactmentsToCount: enactments(
   145  				enactment(1, SetFailedToConfigure),
   146  				enactment(1, SetFailedToConfigure),
   147  			),
   148  			expectedCount: ConditionCount{
   149  				available:   CountByConditionStatus{t: 0, f: 2, u: 0},
   150  				failing:     CountByConditionStatus{t: 2, f: 0, u: 0},
   151  				progressing: CountByConditionStatus{t: 0, f: 2, u: 0},
   152  				pending:     CountByConditionStatus{t: 0, f: 2, u: 0},
   153  				aborted:     CountByConditionStatus{t: 0, f: 2, u: 0},
   154  			},
   155  		}),
   156  		Entry("e(Failed), e(Aborted)", EnactmentCounterCase{
   157  			policyGeneration: 1,
   158  			enactmentsToCount: enactments(
   159  				enactment(1, SetFailedToConfigure),
   160  				enactment(1, SetConfigurationAborted),
   161  			),
   162  			expectedCount: ConditionCount{
   163  				available:   CountByConditionStatus{t: 0, f: 2, u: 0},
   164  				failing:     CountByConditionStatus{t: 1, f: 1, u: 0},
   165  				progressing: CountByConditionStatus{t: 0, f: 2, u: 0},
   166  				pending:     CountByConditionStatus{t: 0, f: 2, u: 0},
   167  				aborted:     CountByConditionStatus{t: 1, f: 1, u: 0},
   168  			},
   169  		}),
   170  		Entry("e(Pending), e(Progressing)", EnactmentCounterCase{
   171  			policyGeneration: 1,
   172  			enactmentsToCount: enactments(
   173  				enactment(1, SetPending),
   174  				enactment(1, SetProgressing),
   175  			),
   176  			expectedCount: ConditionCount{
   177  				available:   CountByConditionStatus{t: 0, f: 1, u: 1},
   178  				failing:     CountByConditionStatus{t: 0, f: 1, u: 1},
   179  				progressing: CountByConditionStatus{t: 1, f: 1, u: 0},
   180  				pending:     CountByConditionStatus{t: 1, f: 1, u: 0},
   181  				aborted:     CountByConditionStatus{t: 0, f: 2, u: 0},
   182  			},
   183  		}),
   184  		Entry("e(Pending), e(Success)", EnactmentCounterCase{
   185  			policyGeneration: 1,
   186  			enactmentsToCount: enactments(
   187  				enactment(1, SetPending),
   188  				enactment(1, SetSuccess),
   189  			),
   190  			expectedCount: ConditionCount{
   191  				available:   CountByConditionStatus{t: 1, f: 1, u: 0},
   192  				failing:     CountByConditionStatus{t: 0, f: 2, u: 0},
   193  				progressing: CountByConditionStatus{t: 0, f: 2, u: 0},
   194  				pending:     CountByConditionStatus{t: 1, f: 1, u: 0},
   195  				aborted:     CountByConditionStatus{t: 0, f: 2, u: 0},
   196  			},
   197  		}),
   198  		Entry("e(Pending), e(Failed)", EnactmentCounterCase{
   199  			policyGeneration: 1,
   200  			enactmentsToCount: enactments(
   201  				enactment(1, SetPending),
   202  				enactment(1, SetFailedToConfigure),
   203  			),
   204  			expectedCount: ConditionCount{
   205  				available:   CountByConditionStatus{t: 0, f: 2, u: 0},
   206  				failing:     CountByConditionStatus{t: 1, f: 1, u: 0},
   207  				progressing: CountByConditionStatus{t: 0, f: 2, u: 0},
   208  				pending:     CountByConditionStatus{t: 1, f: 1, u: 0},
   209  				aborted:     CountByConditionStatus{t: 0, f: 2, u: 0},
   210  			},
   211  		}),
   212  		Entry("e(Pending), e(Aborted)", EnactmentCounterCase{
   213  			policyGeneration: 1,
   214  			enactmentsToCount: enactments(
   215  				enactment(1, SetPending),
   216  				enactment(1, SetConfigurationAborted),
   217  			),
   218  			expectedCount: ConditionCount{
   219  				available:   CountByConditionStatus{t: 0, f: 2, u: 0},
   220  				failing:     CountByConditionStatus{t: 0, f: 2, u: 0},
   221  				progressing: CountByConditionStatus{t: 0, f: 2, u: 0},
   222  				pending:     CountByConditionStatus{t: 1, f: 1, u: 0},
   223  				aborted:     CountByConditionStatus{t: 1, f: 1, u: 0},
   224  			},
   225  		}),
   226  		Entry("p(2), e(1,Progressing), e(2,Progressing)", EnactmentCounterCase{
   227  			policyGeneration: 2,
   228  			enactmentsToCount: enactments(
   229  				enactment(1, SetProgressing),
   230  				enactment(2, SetProgressing),
   231  			),
   232  			expectedCount: ConditionCount{
   233  				available:   CountByConditionStatus{t: 0, f: 0, u: 2},
   234  				failing:     CountByConditionStatus{t: 0, f: 0, u: 2},
   235  				progressing: CountByConditionStatus{t: 1, f: 0, u: 1},
   236  				pending:     CountByConditionStatus{t: 0, f: 1, u: 1},
   237  				aborted:     CountByConditionStatus{t: 0, f: 1, u: 1},
   238  			},
   239  		}),
   240  		Entry("p(2), e(1,Pending), e(2,Pending)", EnactmentCounterCase{
   241  			policyGeneration: 2,
   242  			enactmentsToCount: enactments(
   243  				enactment(1, SetPending),
   244  				enactment(2, SetPending),
   245  			),
   246  			expectedCount: ConditionCount{
   247  				available:   CountByConditionStatus{t: 0, f: 1, u: 1},
   248  				failing:     CountByConditionStatus{t: 0, f: 1, u: 1},
   249  				progressing: CountByConditionStatus{t: 0, f: 1, u: 1},
   250  				pending:     CountByConditionStatus{t: 1, f: 0, u: 1},
   251  				aborted:     CountByConditionStatus{t: 0, f: 1, u: 1},
   252  			},
   253  		}),
   254  		Entry("p(2), e(1,Success), e(2,Success)", EnactmentCounterCase{
   255  			policyGeneration: 2,
   256  			enactmentsToCount: enactments(
   257  				enactment(1, SetSuccess),
   258  				enactment(2, SetSuccess),
   259  			),
   260  			expectedCount: ConditionCount{
   261  				available:   CountByConditionStatus{t: 1, f: 0, u: 1},
   262  				failing:     CountByConditionStatus{t: 0, f: 1, u: 1},
   263  				progressing: CountByConditionStatus{t: 0, f: 1, u: 1},
   264  				pending:     CountByConditionStatus{t: 0, f: 1, u: 1},
   265  				aborted:     CountByConditionStatus{t: 0, f: 1, u: 1},
   266  			},
   267  		}),
   268  		Entry("p(2), e(1,Failed), e(2,Failed)", EnactmentCounterCase{
   269  			policyGeneration: 2,
   270  			enactmentsToCount: enactments(
   271  				enactment(1, SetFailedToConfigure),
   272  				enactment(2, SetFailedToConfigure),
   273  			),
   274  			expectedCount: ConditionCount{
   275  				available:   CountByConditionStatus{t: 0, f: 1, u: 1},
   276  				failing:     CountByConditionStatus{t: 1, f: 0, u: 1},
   277  				progressing: CountByConditionStatus{t: 0, f: 1, u: 1},
   278  				pending:     CountByConditionStatus{t: 0, f: 1, u: 1},
   279  				aborted:     CountByConditionStatus{t: 0, f: 1, u: 1},
   280  			},
   281  		}),
   282  		Entry("p(2), e(1,Failed), e(2,Aborted)", EnactmentCounterCase{
   283  			policyGeneration: 2,
   284  			enactmentsToCount: enactments(
   285  				enactment(1, SetFailedToConfigure),
   286  				enactment(2, SetConfigurationAborted),
   287  			),
   288  			expectedCount: ConditionCount{
   289  				available:   CountByConditionStatus{t: 0, f: 1, u: 1},
   290  				failing:     CountByConditionStatus{t: 0, f: 1, u: 1},
   291  				progressing: CountByConditionStatus{t: 0, f: 1, u: 1},
   292  				pending:     CountByConditionStatus{t: 0, f: 1, u: 1},
   293  				aborted:     CountByConditionStatus{t: 1, f: 0, u: 1},
   294  			},
   295  		}),
   296  	)
   297  })