github.com/nmstate/kubernetes-nmstate@v0.82.0/test/e2e/deployment/matchers.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 deployment
    19  
    20  import (
    21  	"fmt"
    22  
    23  	"github.com/onsi/gomega/format"
    24  	"github.com/onsi/gomega/types"
    25  
    26  	appsv1 "k8s.io/api/apps/v1"
    27  	corev1 "k8s.io/api/core/v1"
    28  	deputils "k8s.io/kubectl/pkg/util/deployment"
    29  )
    30  
    31  func BeReady() types.GomegaMatcher {
    32  	return &BeReadyMatcher{}
    33  }
    34  
    35  type BeReadyMatcher struct {
    36  	obtainedDeployment *appsv1.Deployment
    37  }
    38  
    39  func (matcher *BeReadyMatcher) Match(obtained interface{}) (success bool, err error) {
    40  	obtainedDeployment, ok := obtained.(appsv1.Deployment)
    41  
    42  	if !ok {
    43  		return false, fmt.Errorf("deployment.IsReady matcher expects a v1.Deployment")
    44  	}
    45  
    46  	matcher.obtainedDeployment = &obtainedDeployment
    47  
    48  	cond := deputils.GetDeploymentCondition(matcher.obtainedDeployment.Status, appsv1.DeploymentAvailable)
    49  	if cond == nil {
    50  		return false, fmt.Errorf("deployment.Status does not contain the DeploymentAvailable condition")
    51  	}
    52  
    53  	return cond.Status == corev1.ConditionTrue, nil
    54  }
    55  
    56  func (matcher *BeReadyMatcher) FailureMessage(actual interface{}) (message string) {
    57  	return matcher.message("to equal")
    58  }
    59  
    60  func (matcher *BeReadyMatcher) NegatedFailureMessage(actual interface{}) (message string) {
    61  	return matcher.message("to not equal")
    62  }
    63  
    64  func (matcher *BeReadyMatcher) message(message string) string {
    65  	cond := deputils.GetDeploymentCondition(matcher.obtainedDeployment.Status, appsv1.DeploymentAvailable)
    66  	return format.Message(cond, fmt.Sprintf("deployment.Status.Condition %v corev1.ConditionTrue", message), corev1.ConditionTrue)
    67  }