istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/test/framework/errors/deprecations.go (about)

     1  // Copyright Istio Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package errors
    16  
    17  import (
    18  	"bufio"
    19  	"fmt"
    20  	"strings"
    21  
    22  	"github.com/hashicorp/go-multierror"
    23  )
    24  
    25  type DeprecatedError struct {
    26  	msg string
    27  }
    28  
    29  func NewDeprecatedError(format string, args ...any) error {
    30  	return &DeprecatedError{fmt.Sprintf(format, args...)}
    31  }
    32  
    33  func IsDeprecatedError(err error) bool {
    34  	_, ok := err.(*DeprecatedError)
    35  	return ok
    36  }
    37  
    38  func IsOrContainsDeprecatedError(err error) bool {
    39  	if IsDeprecatedError(err) {
    40  		return true
    41  	}
    42  
    43  	if m, ok := err.(*multierror.Error); ok {
    44  		for _, e := range m.Errors {
    45  			if IsDeprecatedError(e) {
    46  				return true
    47  			}
    48  		}
    49  	}
    50  
    51  	return false
    52  }
    53  
    54  func (de *DeprecatedError) Error() string {
    55  	return de.msg
    56  }
    57  
    58  // FindDeprecatedMessagesInEnvoyLog looks for deprecated messages in the `logs` parameter. If found, it will return
    59  // a DeprecatedError. Use `extraInfo` to pass additional info, like pod namespace/name, etc.
    60  func FindDeprecatedMessagesInEnvoyLog(logs, extraInfo string) error {
    61  	scanner := bufio.NewScanner(strings.NewReader(logs))
    62  	for scanner.Scan() {
    63  		line := scanner.Text()
    64  		if strings.Contains(strings.ToLower(line), "deprecated") {
    65  			if len(extraInfo) > 0 {
    66  				extraInfo = fmt.Sprintf(" (%s)", extraInfo)
    67  			}
    68  			return NewDeprecatedError("usage of deprecated stuff in Envoy%s: %s", extraInfo, line)
    69  		}
    70  	}
    71  
    72  	return nil
    73  }