github.com/abayer/test-infra@v0.0.5/prow/pjutil/tot_test.go (about)

     1  /*
     2  Copyright 2017 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 pjutil
    18  
    19  import (
    20  	"fmt"
    21  	"net/http"
    22  	"net/http/httptest"
    23  	"testing"
    24  )
    25  
    26  type responseVendor struct {
    27  	codes []int
    28  	data  []string
    29  
    30  	position int
    31  }
    32  
    33  func (r *responseVendor) next() (int, string) {
    34  	code := r.codes[r.position]
    35  	datum := r.data[r.position]
    36  
    37  	r.position = r.position + 1
    38  	if r.position == len(r.codes) {
    39  		r.position = 0
    40  	}
    41  
    42  	return code, datum
    43  }
    44  
    45  func parrotServer(codes []int, data []string) *httptest.Server {
    46  	vendor := responseVendor{
    47  		codes: codes,
    48  		data:  data,
    49  	}
    50  
    51  	return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    52  		code, datum := vendor.next()
    53  		w.WriteHeader(code)
    54  		fmt.Fprint(w, datum)
    55  	}))
    56  }
    57  
    58  func TestGetBuildID(t *testing.T) {
    59  	var testCases = []struct {
    60  		name        string
    61  		codes       []int
    62  		data        []string
    63  		expected    string
    64  		expectedErr bool
    65  	}{
    66  		{
    67  			name:        "all good",
    68  			codes:       []int{200},
    69  			data:        []string{"yay"},
    70  			expected:    "yay",
    71  			expectedErr: false,
    72  		},
    73  		{
    74  			name:        "fail then success",
    75  			codes:       []int{500, 200},
    76  			data:        []string{"boo", "yay"},
    77  			expected:    "yay",
    78  			expectedErr: false,
    79  		},
    80  		{
    81  			name:        "fail",
    82  			codes:       []int{500},
    83  			data:        []string{"boo"},
    84  			expected:    "boo",
    85  			expectedErr: true,
    86  		},
    87  	}
    88  
    89  	for _, testCase := range testCases {
    90  		totServ := parrotServer(testCase.codes, testCase.data)
    91  
    92  		actual, actualErr := GetBuildID("dummy", totServ.URL)
    93  		if testCase.expectedErr && actualErr == nil {
    94  			t.Errorf("%s: expected an error but got none", testCase.name)
    95  		} else if !testCase.expectedErr && actualErr != nil {
    96  			t.Errorf("%s: expected no error but got one: %v", testCase.name, actualErr)
    97  		} else if !testCase.expectedErr && actual != testCase.expected {
    98  			t.Errorf("%s: expected response %v but got: %v", testCase.name, testCase.expected, actual)
    99  		}
   100  
   101  		totServ.Close()
   102  	}
   103  }