sigs.k8s.io/prow@v0.0.0-20240503223140-c5e374dc7eb1/pkg/jira/jira_test.go (about)

     1  /*
     2  Copyright 2020 The Kubernetes 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 jira
    18  
    19  import (
    20  	"errors"
    21  	"fmt"
    22  	"testing"
    23  
    24  	"github.com/hashicorp/go-retryablehttp"
    25  )
    26  
    27  // our client implements Client
    28  var _ Client = &client{}
    29  
    30  // our logger implements the leveledLogger inteface
    31  var _ retryablehttp.LeveledLogger = &retryableHTTPLogrusWrapper{}
    32  
    33  func TestJiraErrorStatusCode(t *testing.T) {
    34  	err := &JiraError{
    35  		StatusCode:    400,
    36  		Body:          "something went wrong",
    37  		OriginalError: errors.New("error: check response body for details"),
    38  	}
    39  	if code := JiraErrorStatusCode(err); code != 400 {
    40  		t.Errorf("status code of unwrapped JiraError is %d; expected 400", code)
    41  	}
    42  	if code := JiraErrorStatusCode(fmt.Errorf("This is a wrapped error: %w", err)); code != 400 {
    43  		t.Errorf("status code of wrapped JiraError is %d; expected 400", code)
    44  	}
    45  	if code := JiraErrorStatusCode(errors.New("This is not a jira error")); code != -1 {
    46  		t.Errorf("status code of non-jira error is %d; expected -1", code)
    47  	}
    48  }
    49  
    50  func TestJiraErrorBody(t *testing.T) {
    51  	err := &JiraError{
    52  		StatusCode:    400,
    53  		Body:          "something went wrong",
    54  		OriginalError: errors.New("error: check response body for details"),
    55  	}
    56  	if body := JiraErrorBody(err); body != "something went wrong" {
    57  		t.Errorf("body of unwrapped JiraError is `%s`; expected `something went wrong`", body)
    58  	}
    59  	if body := JiraErrorBody(fmt.Errorf("This is a wrapped error: %w", err)); body != "something went wrong" {
    60  		t.Errorf("body of wrapped JiraError is `%s`; expected `something went wrong`", body)
    61  	}
    62  	if body := JiraErrorBody(errors.New("This is not a jira error")); body != "" {
    63  		t.Errorf("body of non-jira error is `%s`; expected empty string", body)
    64  	}
    65  }