github.com/google/cadvisor@v0.49.1/container/podman/podman_test.go (about)

     1  // Copyright 2022 Google Inc. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package podman
    16  
    17  import (
    18  	"errors"
    19  	"net/http"
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/assert"
    23  )
    24  
    25  func TestValidateResponse(t *testing.T) {
    26  	for _, tc := range []struct {
    27  		response *http.Response
    28  		err      error
    29  		expected string
    30  	}{
    31  		{
    32  			response: nil,
    33  			err:      nil,
    34  			expected: "response not present",
    35  		},
    36  		{
    37  			response: &http.Response{
    38  				StatusCode: http.StatusNotFound,
    39  			},
    40  			err:      errors.New("some error"),
    41  			expected: "item not found: some error",
    42  		},
    43  		{
    44  			response: &http.Response{
    45  				StatusCode: http.StatusNotImplemented,
    46  			},
    47  			err:      errors.New("some error"),
    48  			expected: "query not implemented: some error",
    49  		},
    50  		{
    51  			response: &http.Response{
    52  				StatusCode: http.StatusOK,
    53  			},
    54  			err:      errors.New("some error"),
    55  			expected: "some error",
    56  		},
    57  		{
    58  			response: &http.Response{
    59  				StatusCode: http.StatusOK,
    60  			},
    61  			err:      nil,
    62  			expected: "",
    63  		},
    64  	} {
    65  		err := validateResponse(tc.err, tc.response)
    66  		if tc.expected != "" {
    67  			assert.EqualError(t, err, tc.expected)
    68  		} else {
    69  			assert.NoError(t, err)
    70  		}
    71  	}
    72  }