github.com/someshkoli/terratest@v0.41.1/modules/k8s/errors.go (about)

     1  package k8s
     2  
     3  import (
     4  	"fmt"
     5  
     6  	batchv1 "k8s.io/api/batch/v1"
     7  	corev1 "k8s.io/api/core/v1"
     8  	networkingv1 "k8s.io/api/networking/v1"
     9  	networkingv1beta1 "k8s.io/api/networking/v1beta1"
    10  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    11  )
    12  
    13  // IngressNotAvailable is returned when a Kubernetes service is not yet available to accept traffic.
    14  type IngressNotAvailable struct {
    15  	ingress *networkingv1.Ingress
    16  }
    17  
    18  // Error is a simple function to return a formatted error message as a string
    19  func (err IngressNotAvailable) Error() string {
    20  	return fmt.Sprintf("Ingress %s is not available", err.ingress.Name)
    21  }
    22  
    23  // IngressNotAvailableV1Beta1 is returned when a Kubernetes service is not yet available to accept traffic.
    24  type IngressNotAvailableV1Beta1 struct {
    25  	ingress *networkingv1beta1.Ingress
    26  }
    27  
    28  // Error is a simple function to return a formatted error message as a string
    29  func (err IngressNotAvailableV1Beta1) Error() string {
    30  	return fmt.Sprintf("Ingress %s is not available", err.ingress.Name)
    31  }
    32  
    33  // UnknownKubeResourceType is returned if the given resource type does not match the list of known resource types.
    34  type UnknownKubeResourceType struct {
    35  	ResourceType KubeResourceType
    36  }
    37  
    38  func (err UnknownKubeResourceType) Error() string {
    39  	return fmt.Sprintf("ResourceType ID %d is unknown", err.ResourceType)
    40  }
    41  
    42  // DesiredNumberOfPodsNotCreated is returned when the number of pods matching a filter condition does not match the
    43  // desired number of Pods.
    44  type DesiredNumberOfPodsNotCreated struct {
    45  	Filter       metav1.ListOptions
    46  	DesiredCount int
    47  }
    48  
    49  // Error is a simple function to return a formatted error message as a string
    50  func (err DesiredNumberOfPodsNotCreated) Error() string {
    51  	return fmt.Sprintf("Desired number of pods (%d) matching filter %v not yet created", err.DesiredCount, err.Filter)
    52  }
    53  
    54  // ServiceAccountTokenNotAvailable is returned when a Kubernetes ServiceAccount does not have a token provisioned yet.
    55  type ServiceAccountTokenNotAvailable struct {
    56  	Name string
    57  }
    58  
    59  // Error is a simple function to return a formatted error message as a string
    60  func (err ServiceAccountTokenNotAvailable) Error() string {
    61  	return fmt.Sprintf("ServiceAccount %s does not have a token yet.", err.Name)
    62  }
    63  
    64  // PodNotAvailable is returned when a Kubernetes service is not yet available to accept traffic.
    65  type PodNotAvailable struct {
    66  	pod *corev1.Pod
    67  }
    68  
    69  // Error is a simple function to return a formatted error message as a string
    70  func (err PodNotAvailable) Error() string {
    71  	return fmt.Sprintf("Pod %s is not available", err.pod.Name)
    72  }
    73  
    74  // NewPodNotAvailableError returnes a PodNotAvailable struct when Kubernetes deems a pod is not available
    75  func NewPodNotAvailableError(pod *corev1.Pod) PodNotAvailable {
    76  	return PodNotAvailable{pod}
    77  }
    78  
    79  // JobNotSucceeded is returned when a Kubernetes job is not Succeeded
    80  type JobNotSucceeded struct {
    81  	job *batchv1.Job
    82  }
    83  
    84  // Error is a simple function to return a formatted error message as a string
    85  func (err JobNotSucceeded) Error() string {
    86  	return fmt.Sprintf("Job %s is not Succeeded", err.job.Name)
    87  }
    88  
    89  // NewJobNotSucceeded returnes a JobNotSucceeded when the status of the job is not Succeeded
    90  func NewJobNotSucceeded(job *batchv1.Job) JobNotSucceeded {
    91  	return JobNotSucceeded{job}
    92  }
    93  
    94  // ServiceNotAvailable is returned when a Kubernetes service is not yet available to accept traffic.
    95  type ServiceNotAvailable struct {
    96  	service *corev1.Service
    97  }
    98  
    99  // Error is a simple function to return a formatted error message as a string
   100  func (err ServiceNotAvailable) Error() string {
   101  	return fmt.Sprintf("Service %s is not available", err.service.Name)
   102  }
   103  
   104  // NewServiceNotAvailableError returnes a ServiceNotAvailable struct when Kubernetes deems a service is not available
   105  func NewServiceNotAvailableError(service *corev1.Service) ServiceNotAvailable {
   106  	return ServiceNotAvailable{service}
   107  }
   108  
   109  // UnknownServiceType is returned when a Kubernetes service has a type that is not yet handled by the test functions.
   110  type UnknownServiceType struct {
   111  	service *corev1.Service
   112  }
   113  
   114  // Error is a simple function to return a formatted error message as a string
   115  func (err UnknownServiceType) Error() string {
   116  	return fmt.Sprintf("Service %s has an unknown service type", err.service.Name)
   117  }
   118  
   119  // NewUnknownServiceTypeError returns an UnknownServiceType struct when is it deemed that Kubernetes does not know the service type provided
   120  func NewUnknownServiceTypeError(service *corev1.Service) UnknownServiceType {
   121  	return UnknownServiceType{service}
   122  }
   123  
   124  // UnknownServicePort is returned when the given service port is not an exported port of the service.
   125  type UnknownServicePort struct {
   126  	service *corev1.Service
   127  	port    int32
   128  }
   129  
   130  // Error is a simple function to return a formatted error message as a string
   131  func (err UnknownServicePort) Error() string {
   132  	return fmt.Sprintf("Port %d is not a part of the service %s", err.port, err.service.Name)
   133  }
   134  
   135  // NewUnknownServicePortError returns an UnknownServicePort struct when it is deemed that Kuberenetes does not know of the provided Service Port
   136  func NewUnknownServicePortError(service *corev1.Service, port int32) UnknownServicePort {
   137  	return UnknownServicePort{service, port}
   138  }
   139  
   140  // NoNodesInKubernetes is returned when the Kubernetes cluster has no nodes registered.
   141  type NoNodesInKubernetes struct{}
   142  
   143  // Error is a simple function to return a formatted error message as a string
   144  func (err NoNodesInKubernetes) Error() string {
   145  	return "There are no nodes in the Kubernetes cluster"
   146  }
   147  
   148  // NewNoNodesInKubernetesError returns a NoNodesInKubernetes struct when it is deemed that there are no Kubernetes nodes registered
   149  func NewNoNodesInKubernetesError() NoNodesInKubernetes {
   150  	return NoNodesInKubernetes{}
   151  }
   152  
   153  // NodeHasNoHostname is returned when a Kubernetes node has no discernible hostname
   154  type NodeHasNoHostname struct {
   155  	node *corev1.Node
   156  }
   157  
   158  // Error is a simple function to return a formatted error message as a string
   159  func (err NodeHasNoHostname) Error() string {
   160  	return fmt.Sprintf("Node %s has no hostname", err.node.Name)
   161  }
   162  
   163  // NewNodeHasNoHostnameError returns a NodeHasNoHostname struct when it is deemed that the provided node has no hostname
   164  func NewNodeHasNoHostnameError(node *corev1.Node) NodeHasNoHostname {
   165  	return NodeHasNoHostname{node}
   166  }
   167  
   168  // MalformedNodeID is returned when a Kubernetes node has a malformed node id scheme
   169  type MalformedNodeID struct {
   170  	node *corev1.Node
   171  }
   172  
   173  // Error is a simple function to return a formatted error message as a string
   174  func (err MalformedNodeID) Error() string {
   175  	return fmt.Sprintf("Node %s has malformed ID %s", err.node.Name, err.node.Spec.ProviderID)
   176  }
   177  
   178  // NewMalformedNodeIDError returns a MalformedNodeID struct when Kubernetes deems that a NodeID is malformed
   179  func NewMalformedNodeIDError(node *corev1.Node) MalformedNodeID {
   180  	return MalformedNodeID{node}
   181  }
   182  
   183  // JSONPathMalformedJSONErr is returned when the jsonpath unmarshal routine fails to parse the given JSON blob.
   184  type JSONPathMalformedJSONErr struct {
   185  	underlyingErr error
   186  }
   187  
   188  func (err JSONPathMalformedJSONErr) Error() string {
   189  	return fmt.Sprintf("Error unmarshaling original json blob: %s", err.underlyingErr)
   190  }
   191  
   192  // JSONPathMalformedJSONPathErr is returned when the jsonpath unmarshal routine fails to parse the given JSON path
   193  // string.
   194  type JSONPathMalformedJSONPathErr struct {
   195  	underlyingErr error
   196  }
   197  
   198  func (err JSONPathMalformedJSONPathErr) Error() string {
   199  	return fmt.Sprintf("Error parsing json path: %s", err.underlyingErr)
   200  }
   201  
   202  // JSONPathExtractJSONPathErr is returned when the jsonpath unmarshal routine fails to extract the given JSON path from
   203  // the JSON blob.
   204  type JSONPathExtractJSONPathErr struct {
   205  	underlyingErr error
   206  }
   207  
   208  func (err JSONPathExtractJSONPathErr) Error() string {
   209  	return fmt.Sprintf("Error extracting json path from blob: %s", err.underlyingErr)
   210  }
   211  
   212  // JSONPathMalformedJSONPathResultErr is returned when the jsonpath unmarshal routine fails to unmarshal the resulting
   213  // data from extraction.
   214  type JSONPathMalformedJSONPathResultErr struct {
   215  	underlyingErr error
   216  }
   217  
   218  func (err JSONPathMalformedJSONPathResultErr) Error() string {
   219  	return fmt.Sprintf("Error unmarshaling json path output: %s", err.underlyingErr)
   220  }