github.com/openshift/installer@v1.4.17/pkg/agent/validations_test.go (about)

     1  package agent
     2  
     3  import (
     4  	"io"
     5  	"testing"
     6  
     7  	"github.com/sirupsen/logrus"
     8  	"github.com/sirupsen/logrus/hooks/test"
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  // ValidationHistory test helpers
    13  const (
    14  	testKey                string = "test-cluster"
    15  	testID                 string = "test"
    16  	testLogPrefix          string = "Test: "
    17  	validationsInfoSuccess string = "{\"test\":[{\"id\":\"test\",\"status\":\"success\",\"message\":\"The validation succeeded\"}]}"
    18  	validationsInfoFailure string = "{\"test\":[{\"id\":\"test\",\"status\":\"failure\",\"message\":\"The validation failed\"}]}"
    19  )
    20  
    21  func TestUpdateValidationHistory(t *testing.T) {
    22  	tests := []struct {
    23  		name            string
    24  		validationsInfo string
    25  		inputHistory    map[string]*validationResultHistory
    26  		expectedResult  map[string]*validationResultHistory
    27  	}{
    28  		{
    29  			name:            "success-first-seen",
    30  			validationsInfo: validationsInfoSuccess,
    31  			inputHistory: map[string]*validationResultHistory{
    32  				"test": {
    33  					numFailures:     0,
    34  					seen:            false,
    35  					currentStatus:   "",
    36  					currentMessage:  "",
    37  					previousStatus:  "",
    38  					previousMessage: "",
    39  				},
    40  			},
    41  			expectedResult: map[string]*validationResultHistory{
    42  				"test": {
    43  					numFailures:     0,
    44  					seen:            true,
    45  					currentStatus:   "success",
    46  					currentMessage:  "The validation succeeded",
    47  					previousStatus:  "",
    48  					previousMessage: "",
    49  				},
    50  			},
    51  		},
    52  		{
    53  			name:            "success-no-change",
    54  			validationsInfo: validationsInfoSuccess,
    55  			inputHistory: map[string]*validationResultHistory{
    56  				"test": {
    57  					numFailures:     0,
    58  					seen:            true,
    59  					currentStatus:   "success",
    60  					currentMessage:  "The validation succeeded",
    61  					previousStatus:  "",
    62  					previousMessage: "",
    63  				},
    64  			},
    65  			expectedResult: map[string]*validationResultHistory{
    66  				"test": {
    67  					numFailures:     0,
    68  					seen:            true,
    69  					currentStatus:   "success",
    70  					currentMessage:  "The validation succeeded",
    71  					previousStatus:  "success",
    72  					previousMessage: "The validation succeeded",
    73  				},
    74  			},
    75  		},
    76  		{
    77  			name:            "failure-first-seen",
    78  			validationsInfo: validationsInfoFailure,
    79  			inputHistory: map[string]*validationResultHistory{
    80  				"test": {
    81  					numFailures:     0,
    82  					seen:            false,
    83  					currentStatus:   "",
    84  					currentMessage:  "",
    85  					previousStatus:  "",
    86  					previousMessage: "",
    87  				},
    88  			},
    89  			expectedResult: map[string]*validationResultHistory{
    90  				"test": {
    91  					numFailures:     1,
    92  					seen:            true,
    93  					currentStatus:   "failure",
    94  					currentMessage:  "The validation failed",
    95  					previousStatus:  "",
    96  					previousMessage: "",
    97  				},
    98  			},
    99  		},
   100  		{
   101  			name:            "failure-no-change",
   102  			validationsInfo: validationsInfoFailure,
   103  			inputHistory: map[string]*validationResultHistory{
   104  				"test": {
   105  					numFailures:     0,
   106  					seen:            true,
   107  					currentStatus:   "failure",
   108  					currentMessage:  "The validation failed",
   109  					previousStatus:  "",
   110  					previousMessage: "",
   111  				},
   112  			},
   113  			expectedResult: map[string]*validationResultHistory{
   114  				"test": {
   115  					numFailures:     1,
   116  					seen:            true,
   117  					currentStatus:   "failure",
   118  					currentMessage:  "The validation failed",
   119  					previousStatus:  "failure",
   120  					previousMessage: "The validation failed",
   121  				},
   122  			},
   123  		},
   124  		{
   125  			name:            "success-after-failure",
   126  			validationsInfo: validationsInfoSuccess,
   127  			inputHistory: map[string]*validationResultHistory{
   128  				"test": {
   129  					numFailures:     1,
   130  					seen:            true,
   131  					currentStatus:   "failure",
   132  					currentMessage:  "The validation failed",
   133  					previousStatus:  "failure",
   134  					previousMessage: "The validation failed",
   135  				},
   136  			},
   137  			expectedResult: map[string]*validationResultHistory{
   138  				"test": {
   139  					numFailures:     1,
   140  					seen:            true,
   141  					currentStatus:   "success",
   142  					currentMessage:  "The validation succeeded",
   143  					previousStatus:  "failure",
   144  					previousMessage: "The validation failed",
   145  				},
   146  			},
   147  		},
   148  		{
   149  			name:            "failure-after-success",
   150  			validationsInfo: validationsInfoFailure,
   151  			inputHistory: map[string]*validationResultHistory{
   152  				"test": {
   153  					numFailures:     0,
   154  					seen:            true,
   155  					currentStatus:   "success",
   156  					currentMessage:  "The validation succeeded",
   157  					previousStatus:  "success",
   158  					previousMessage: "The validation succeeded",
   159  				},
   160  			},
   161  			expectedResult: map[string]*validationResultHistory{
   162  				"test": {
   163  					numFailures:     1,
   164  					seen:            true,
   165  					currentStatus:   "failure",
   166  					currentMessage:  "The validation failed",
   167  					previousStatus:  "success",
   168  					previousMessage: "The validation succeeded",
   169  				},
   170  			},
   171  		},
   172  	}
   173  	for _, tt := range tests {
   174  		t.Run(tt.name, func(t *testing.T) {
   175  			// Test logger
   176  			logger := &logrus.Logger{
   177  				Out:       io.Discard,
   178  				Formatter: new(logrus.TextFormatter),
   179  				Hooks:     make(logrus.LevelHooks),
   180  				Level:     logrus.DebugLevel,
   181  			}
   182  			actualResult, _ := updateValidationResultHistory(testLogPrefix, tt.validationsInfo, tt.inputHistory, logger)
   183  			assert.Equal(t, tt.expectedResult, actualResult)
   184  		})
   185  	}
   186  }
   187  
   188  func TestLogValidationHistory(t *testing.T) {
   189  	tests := []struct {
   190  		name           string
   191  		inputHistory   *validationResultHistory
   192  		expectedResult *logrus.Entry
   193  	}{
   194  		{
   195  			name: "default-case-not-seen",
   196  			inputHistory: &validationResultHistory{
   197  				numFailures:     0,
   198  				seen:            false,
   199  				currentStatus:   "test",
   200  				currentMessage:  "This is the default validation case",
   201  				previousStatus:  "",
   202  				previousMessage: "",
   203  			},
   204  			expectedResult: &logrus.Entry{Level: logrus.TraceLevel, Message: "Test: This is the default validation case"},
   205  		},
   206  		{
   207  			name: "default-case-seen",
   208  			inputHistory: &validationResultHistory{
   209  				numFailures:     0,
   210  				seen:            true,
   211  				currentStatus:   "test",
   212  				currentMessage:  "This is the default validation case",
   213  				previousStatus:  "",
   214  				previousMessage: "",
   215  			},
   216  			expectedResult: &logrus.Entry{Level: logrus.TraceLevel, Message: "Test: This is the default validation case"},
   217  		},
   218  		{
   219  			name: "success-first-seen",
   220  			inputHistory: &validationResultHistory{
   221  				numFailures:     0,
   222  				seen:            false,
   223  				currentStatus:   "success",
   224  				currentMessage:  "The validation succeeded",
   225  				previousStatus:  "",
   226  				previousMessage: "",
   227  			},
   228  			expectedResult: &logrus.Entry{Level: logrus.DebugLevel, Message: "Test: The validation succeeded"},
   229  		},
   230  		{
   231  			name: "success-no-change",
   232  			inputHistory: &validationResultHistory{
   233  				numFailures:     0,
   234  				seen:            true,
   235  				currentStatus:   "success",
   236  				currentMessage:  "The validation succeeded",
   237  				previousStatus:  "success",
   238  				previousMessage: "The validation succeeded",
   239  			},
   240  			expectedResult: nil,
   241  		},
   242  		{
   243  			name: "failure-first-seen",
   244  			inputHistory: &validationResultHistory{
   245  				numFailures:     0,
   246  				seen:            false,
   247  				currentStatus:   "failure",
   248  				currentMessage:  "The validation failed",
   249  				previousStatus:  "",
   250  				previousMessage: "",
   251  			},
   252  			expectedResult: &logrus.Entry{Level: logrus.WarnLevel, Message: "Test: The validation failed"},
   253  		},
   254  		{
   255  			name: "failure-no-change",
   256  			inputHistory: &validationResultHistory{
   257  				numFailures:     1,
   258  				seen:            true,
   259  				currentStatus:   "failure",
   260  				currentMessage:  "The validation failed",
   261  				previousStatus:  "failure",
   262  				previousMessage: "The validation failed",
   263  			},
   264  			expectedResult: nil,
   265  		},
   266  		{
   267  			name: "success-after-failure",
   268  			inputHistory: &validationResultHistory{
   269  				numFailures:     1,
   270  				seen:            true,
   271  				currentStatus:   "success",
   272  				currentMessage:  "The validation succeeded",
   273  				previousStatus:  "failure",
   274  				previousMessage: "The validation failed",
   275  			},
   276  			expectedResult: &logrus.Entry{Level: logrus.InfoLevel, Message: "Test: The validation succeeded"},
   277  		},
   278  		{
   279  			name: "failure-after-success",
   280  			inputHistory: &validationResultHistory{
   281  				numFailures:     1,
   282  				seen:            true,
   283  				currentStatus:   "failure",
   284  				currentMessage:  "The validation failed",
   285  				previousStatus:  "success",
   286  				previousMessage: "The validation succeeded",
   287  			},
   288  			expectedResult: &logrus.Entry{Level: logrus.WarnLevel, Message: "Test: The validation failed"},
   289  		},
   290  	}
   291  	for _, tt := range tests {
   292  		t.Run(tt.name, func(t *testing.T) {
   293  			// Test logger
   294  			logger := &logrus.Logger{
   295  				Out:       io.Discard,
   296  				Formatter: new(logrus.TextFormatter),
   297  				Hooks:     make(logrus.LevelHooks),
   298  				Level:     logrus.TraceLevel,
   299  			}
   300  			var hook = test.NewLocal(logger)
   301  
   302  			logValidationHistory(testLogPrefix, tt.inputHistory, logger)
   303  			actualResult := hook.LastEntry()
   304  
   305  			if actualResult == nil {
   306  				assert.Equal(t, tt.expectedResult, actualResult)
   307  			} else {
   308  				assert.Equal(t, tt.expectedResult.Level, actualResult.Level)
   309  				assert.Equal(t, tt.expectedResult.Message, actualResult.Message)
   310  			}
   311  		})
   312  	}
   313  }