github.com/oam-dev/kubevela@v1.9.11/pkg/oam/util/test_utils.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  	"encoding/json"
    21  	"reflect"
    22  	"sort"
    23  
    24  	"github.com/onsi/gomega/format"
    25  	"github.com/onsi/gomega/types"
    26  	apierrors "k8s.io/apimachinery/pkg/api/errors"
    27  	"sigs.k8s.io/yaml"
    28  
    29  	"github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
    30  )
    31  
    32  // JSONMarshal returns the JSON encoding
    33  func JSONMarshal(o interface{}) []byte {
    34  	j, _ := json.Marshal(o)
    35  	return j
    36  }
    37  
    38  var _ types.GomegaMatcher = AlreadyExistMatcher{}
    39  
    40  // AlreadyExistMatcher matches the error to be already exist
    41  type AlreadyExistMatcher struct {
    42  }
    43  
    44  // Match matches error.
    45  func (matcher AlreadyExistMatcher) Match(actual interface{}) (success bool, err error) {
    46  	if actual == nil {
    47  		return false, nil
    48  	}
    49  	actualError := actual.(error)
    50  	return apierrors.IsAlreadyExists(actualError), nil
    51  }
    52  
    53  // FailureMessage builds an error message.
    54  func (matcher AlreadyExistMatcher) FailureMessage(actual interface{}) (message string) {
    55  	return format.Message(actual, "to be already exist")
    56  }
    57  
    58  // NegatedFailureMessage builds an error message.
    59  func (matcher AlreadyExistMatcher) NegatedFailureMessage(actual interface{}) (message string) {
    60  	return format.Message(actual, "not to be already exist")
    61  }
    62  
    63  var _ types.GomegaMatcher = NotFoundMatcher{}
    64  
    65  // NotFoundMatcher matches the error to be not found.
    66  type NotFoundMatcher struct {
    67  }
    68  
    69  // Match matches the api error.
    70  func (matcher NotFoundMatcher) Match(actual interface{}) (success bool, err error) {
    71  	if actual == nil {
    72  		return false, nil
    73  	}
    74  	actualError := actual.(error)
    75  	return apierrors.IsNotFound(actualError), nil
    76  }
    77  
    78  // FailureMessage builds an error message.
    79  func (matcher NotFoundMatcher) FailureMessage(actual interface{}) (message string) {
    80  	return format.Message(actual, "to be not found")
    81  }
    82  
    83  // NegatedFailureMessage builds an error message.
    84  func (matcher NotFoundMatcher) NegatedFailureMessage(actual interface{}) (message string) {
    85  	return format.Message(actual, "not to be not found")
    86  }
    87  
    88  // BeEquivalentToError matches the error to take care of nil.
    89  func BeEquivalentToError(expected error) types.GomegaMatcher {
    90  	return &ErrorMatcher{
    91  		ExpectedError: expected,
    92  	}
    93  }
    94  
    95  var _ types.GomegaMatcher = ErrorMatcher{}
    96  
    97  // ErrorMatcher matches errors.
    98  type ErrorMatcher struct {
    99  	ExpectedError error
   100  }
   101  
   102  // Match matches an error.
   103  func (matcher ErrorMatcher) Match(actual interface{}) (success bool, err error) {
   104  	if actual == nil {
   105  		return matcher.ExpectedError == nil, nil
   106  	}
   107  	actualError := actual.(error)
   108  	return actualError.Error() == matcher.ExpectedError.Error(), nil
   109  }
   110  
   111  // FailureMessage builds an error message.
   112  func (matcher ErrorMatcher) FailureMessage(actual interface{}) (message string) {
   113  	actualError, actualOK := actual.(error)
   114  	expectedError := matcher.ExpectedError
   115  	expectedOK := expectedError != nil
   116  
   117  	if actualOK && expectedOK {
   118  		return format.MessageWithDiff(actualError.Error(), "to equal", expectedError.Error())
   119  	}
   120  
   121  	if actualOK && !expectedOK {
   122  		return format.Message(actualError.Error(), "to equal", expectedError.Error())
   123  	}
   124  
   125  	if !actualOK && expectedOK {
   126  		return format.Message(actual, "to equal", expectedError.Error())
   127  	}
   128  
   129  	return format.Message(actual, "to equal", expectedError)
   130  }
   131  
   132  // NegatedFailureMessage builds an error message.
   133  func (matcher ErrorMatcher) NegatedFailureMessage(actual interface{}) (message string) {
   134  	actualError, actualOK := actual.(error)
   135  	expectedError := matcher.ExpectedError
   136  	expectedOK := expectedError != nil
   137  
   138  	if actualOK && expectedOK {
   139  		return format.MessageWithDiff(actualError.Error(), "not to equal", expectedError.Error())
   140  	}
   141  
   142  	if actualOK && !expectedOK {
   143  		return format.Message(actualError.Error(), "not to equal", expectedError.Error())
   144  	}
   145  
   146  	if !actualOK && expectedOK {
   147  		return format.Message(actual, "not to equal", expectedError.Error())
   148  	}
   149  
   150  	return format.Message(actual, "not to equal", expectedError)
   151  }
   152  
   153  // UnMarshalStringToComponentDefinition parse a string to a componentDefinition object
   154  func UnMarshalStringToComponentDefinition(s string) (*v1beta1.ComponentDefinition, error) {
   155  	obj := &v1beta1.ComponentDefinition{}
   156  	_body, err := yaml.YAMLToJSON([]byte(s))
   157  	if err != nil {
   158  		return nil, err
   159  	}
   160  	if err := json.Unmarshal(_body, obj); err != nil {
   161  		return nil, err
   162  	}
   163  	return obj, nil
   164  }
   165  
   166  // UnMarshalStringToTraitDefinition parse a string to a traitDefinition object
   167  func UnMarshalStringToTraitDefinition(s string) (*v1beta1.TraitDefinition, error) {
   168  	obj := &v1beta1.TraitDefinition{}
   169  	_body, err := yaml.YAMLToJSON([]byte(s))
   170  	if err != nil {
   171  		return nil, err
   172  	}
   173  	if err := json.Unmarshal(_body, obj); err != nil {
   174  		return nil, err
   175  	}
   176  	return obj, nil
   177  }
   178  
   179  // UnMarshalStringToPolicyDefinition parse a string to a policyDefinition object
   180  func UnMarshalStringToPolicyDefinition(s string) (*v1beta1.PolicyDefinition, error) {
   181  	obj := &v1beta1.PolicyDefinition{}
   182  	_body, err := yaml.YAMLToJSON([]byte(s))
   183  	if err != nil {
   184  		return nil, err
   185  	}
   186  	if err := json.Unmarshal(_body, obj); err != nil {
   187  		return nil, err
   188  	}
   189  	return obj, nil
   190  }
   191  
   192  // CheckAppRevision check if appRevision list is right
   193  func CheckAppRevision(revs []v1beta1.ApplicationRevision, collection []int) (bool, error) {
   194  	if len(revs) != len(collection) {
   195  		return false, nil
   196  	}
   197  	var revNums []int
   198  	for _, rev := range revs {
   199  		num, err := ExtractRevisionNum(rev.Name, "-")
   200  		if err != nil {
   201  			return false, err
   202  		}
   203  		revNums = append(revNums, num)
   204  	}
   205  	sort.Ints(revNums)
   206  	sort.Ints(collection)
   207  	if reflect.DeepEqual(revNums, collection) {
   208  		return true, nil
   209  	}
   210  	return false, nil
   211  }