github.com/oam-dev/kubevela@v1.9.11/pkg/oam/util/test_utils_test.go (about)

     1  /*
     2  Copyright 2021 The KubeVela 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 util
    18  
    19  import (
    20  	"fmt"
    21  	"testing"
    22  
    23  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    24  
    25  	"github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
    26  
    27  	"github.com/stretchr/testify/assert"
    28  
    29  	"k8s.io/apimachinery/pkg/api/errors"
    30  	"k8s.io/apimachinery/pkg/runtime/schema"
    31  )
    32  
    33  func TestAlreadyExistMatcher(t *testing.T) {
    34  	type want struct {
    35  		success bool
    36  		err     error
    37  	}
    38  
    39  	cases := map[string]struct {
    40  		input interface{}
    41  		want  want
    42  	}{
    43  		"Matches": {
    44  			input: errors.NewAlreadyExists(schema.GroupResource{
    45  				Group:    "g",
    46  				Resource: "r",
    47  			}, "name"),
    48  			want: want{
    49  				success: true,
    50  				err:     nil,
    51  			},
    52  		},
    53  		"Does not match": {
    54  			input: errors.NewNotFound(schema.GroupResource{
    55  				Group:    "g",
    56  				Resource: "r",
    57  			}, "name"),
    58  			want: want{
    59  				success: false,
    60  				err:     nil,
    61  			},
    62  		},
    63  		"Does not match nil": {
    64  			input: nil,
    65  			want: want{
    66  				success: false,
    67  				err:     nil,
    68  			},
    69  		},
    70  	}
    71  	matcher := AlreadyExistMatcher{}
    72  	for name, tc := range cases {
    73  		success, err := matcher.Match(tc.input)
    74  		t.Log(fmt.Sprint("Running test: ", name))
    75  		assert.Equal(t, tc.want.success, success)
    76  		if tc.want.err == nil {
    77  			assert.NoError(t, err)
    78  		} else {
    79  			assert.Error(t, tc.want.err, err)
    80  		}
    81  	}
    82  
    83  	// Error messages
    84  	assert.Equal(t, "Expected\n    <string>: myerror\nto be already exist", matcher.FailureMessage("myerror"))
    85  	assert.Equal(t, "Expected\n    <string>: myerror\nnot to be already exist", matcher.NegatedFailureMessage("myerror"))
    86  }
    87  
    88  func TestNotFoundMatcher(t *testing.T) {
    89  	type want struct {
    90  		success bool
    91  		err     error
    92  	}
    93  
    94  	cases := map[string]struct {
    95  		input interface{}
    96  		want  want
    97  	}{
    98  		"Does not matche": {
    99  			input: errors.NewAlreadyExists(schema.GroupResource{
   100  				Group:    "g",
   101  				Resource: "r",
   102  			}, "name"),
   103  			want: want{
   104  				success: false,
   105  				err:     nil,
   106  			},
   107  		},
   108  		"Matches": {
   109  			input: errors.NewNotFound(schema.GroupResource{
   110  				Group:    "g",
   111  				Resource: "r",
   112  			}, "name"),
   113  			want: want{
   114  				success: true,
   115  				err:     nil,
   116  			},
   117  		},
   118  		"Does not match nil": {
   119  			input: nil,
   120  			want: want{
   121  				success: false,
   122  				err:     nil,
   123  			},
   124  		},
   125  	}
   126  
   127  	matcher := NotFoundMatcher{}
   128  	for name, tc := range cases {
   129  		success, err := matcher.Match(tc.input)
   130  		t.Log(fmt.Sprint("Running test: ", name))
   131  		assert.Equal(t, tc.want.success, success)
   132  		if tc.want.err == nil {
   133  			assert.NoError(t, err)
   134  		} else {
   135  			assert.Equal(t, tc.want.err, err)
   136  		}
   137  	}
   138  
   139  	// Error messages
   140  	assert.Equal(t, "Expected\n    <string>: myerror\nto be not found", matcher.FailureMessage("myerror"))
   141  	assert.Equal(t, "Expected\n    <string>: myerror\nnot to be not found", matcher.NegatedFailureMessage("myerror"))
   142  }
   143  func TestErrorMatcher(t *testing.T) {
   144  	type input struct {
   145  		expected error
   146  		input    error
   147  	}
   148  
   149  	type want struct {
   150  		success               bool
   151  		err                   error
   152  		failureMessage        string
   153  		negatedFailureMessage string
   154  	}
   155  
   156  	cases := map[string]struct {
   157  		input input
   158  		want  want
   159  	}{
   160  		"Matches": {
   161  			input: input{
   162  				expected: fmt.Errorf("my error"),
   163  				input:    fmt.Errorf("my error"),
   164  			},
   165  			want: want{
   166  				success:               true,
   167  				err:                   nil,
   168  				failureMessage:        "Expected\n    <string>: my error\nto equal\n    <string>: my error",
   169  				negatedFailureMessage: "Expected\n    <string>: my error\nnot to equal\n    <string>: my error",
   170  			},
   171  		},
   172  		"Matches nil": {
   173  			input: input{
   174  				expected: nil,
   175  				input:    nil,
   176  			},
   177  			want: want{
   178  				success:               true,
   179  				err:                   nil,
   180  				failureMessage:        "Expected\n    <nil>: nil\nto equal\n    <nil>: nil",
   181  				negatedFailureMessage: "Expected\n    <nil>: nil\nnot to equal\n    <nil>: nil",
   182  			},
   183  		},
   184  		"Does not match": {
   185  			input: input{
   186  				expected: fmt.Errorf("my error"),
   187  				input:    fmt.Errorf("my other error"),
   188  			},
   189  			want: want{
   190  				success:               false,
   191  				err:                   nil,
   192  				failureMessage:        "Expected\n    <string>: my other error\nto equal\n    <string>: my error",
   193  				negatedFailureMessage: "Expected\n    <string>: my other error\nnot to equal\n    <string>: my error",
   194  			},
   195  		},
   196  		"Does not match nil": {
   197  			input: input{
   198  				expected: fmt.Errorf("my error"),
   199  				input:    nil,
   200  			},
   201  			want: want{
   202  				success:               false,
   203  				err:                   nil,
   204  				failureMessage:        "Expected\n    <nil>: nil\nto equal\n    <string>: my error",
   205  				negatedFailureMessage: "Expected\n    <nil>: nil\nnot to equal\n    <string>: my error",
   206  			},
   207  		},
   208  	}
   209  	for name, tc := range cases {
   210  		matcher := ErrorMatcher{
   211  			ExpectedError: tc.input.expected,
   212  		}
   213  		success, err := matcher.Match(tc.input.input)
   214  		t.Log(fmt.Sprint("Running test: ", name))
   215  		assert.Equal(t, tc.want.success, success)
   216  		if tc.want.err == nil {
   217  			assert.NoError(t, err)
   218  		} else {
   219  			assert.Equal(t, tc.want.err, err)
   220  		}
   221  
   222  		assert.Equal(t, tc.want.failureMessage, matcher.FailureMessage(tc.input.input))
   223  		assert.Equal(t, tc.want.negatedFailureMessage, matcher.NegatedFailureMessage(tc.input.input))
   224  	}
   225  }
   226  
   227  func TestCheckAppRevision(t *testing.T) {
   228  	testcases := map[string]struct {
   229  		revs       []v1beta1.ApplicationRevision
   230  		collection []int
   231  		want       bool
   232  		hasError   bool
   233  	}{
   234  		"match": {
   235  			revs: []v1beta1.ApplicationRevision{
   236  				{
   237  					ObjectMeta: metav1.ObjectMeta{
   238  						Name: "app-v1",
   239  					},
   240  				},
   241  				{
   242  					ObjectMeta: metav1.ObjectMeta{
   243  						Name: "app-v2",
   244  					},
   245  				},
   246  				{
   247  					ObjectMeta: metav1.ObjectMeta{
   248  						Name: "app-v3",
   249  					},
   250  				},
   251  			},
   252  			collection: []int{1, 2, 3},
   253  			want:       true,
   254  			hasError:   false,
   255  		},
   256  		"lengthNotMatch": {
   257  			revs: []v1beta1.ApplicationRevision{
   258  				{
   259  					ObjectMeta: metav1.ObjectMeta{
   260  						Name: "app-v1",
   261  					},
   262  				},
   263  				{
   264  					ObjectMeta: metav1.ObjectMeta{
   265  						Name: "app-v2",
   266  					},
   267  				},
   268  			},
   269  			collection: []int{1, 2, 3},
   270  			want:       false,
   271  			hasError:   false,
   272  		},
   273  		"notMatch": {
   274  			revs: []v1beta1.ApplicationRevision{
   275  				{
   276  					ObjectMeta: metav1.ObjectMeta{
   277  						Name: "app-v1",
   278  					},
   279  				},
   280  				{
   281  					ObjectMeta: metav1.ObjectMeta{
   282  						Name: "app-v2",
   283  					},
   284  				},
   285  				{
   286  					ObjectMeta: metav1.ObjectMeta{
   287  						Name: "app-v4",
   288  					},
   289  				},
   290  			},
   291  			collection: []int{1, 2, 3},
   292  			want:       false,
   293  			hasError:   false,
   294  		},
   295  		"testSort": {
   296  			revs: []v1beta1.ApplicationRevision{
   297  				{
   298  					ObjectMeta: metav1.ObjectMeta{
   299  						Name: "app-v1",
   300  					},
   301  				},
   302  				{
   303  					ObjectMeta: metav1.ObjectMeta{
   304  						Name: "app-v3",
   305  					},
   306  				},
   307  				{
   308  					ObjectMeta: metav1.ObjectMeta{
   309  						Name: "app-v2",
   310  					},
   311  				},
   312  			},
   313  			collection: []int{3, 2, 1},
   314  			want:       true,
   315  			hasError:   false,
   316  		},
   317  		"testErrorName": {
   318  			revs: []v1beta1.ApplicationRevision{
   319  				{
   320  					ObjectMeta: metav1.ObjectMeta{
   321  						Name: "app-v1",
   322  					},
   323  				},
   324  				{
   325  					ObjectMeta: metav1.ObjectMeta{
   326  						Name: "app-v3",
   327  					},
   328  				},
   329  				{
   330  					ObjectMeta: metav1.ObjectMeta{
   331  						Name: "app-vs",
   332  					},
   333  				},
   334  			},
   335  			collection: []int{3, 2, 1},
   336  			want:       false,
   337  			hasError:   true,
   338  		},
   339  	}
   340  	for name, testcase := range testcases {
   341  		t.Log(fmt.Sprint("Running test: ", name))
   342  		checkEqual, err := CheckAppRevision(testcase.revs, testcase.collection)
   343  		hasError := err != nil
   344  		assert.Equal(t, checkEqual, testcase.want)
   345  		assert.Equal(t, hasError, testcase.hasError)
   346  	}
   347  }