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