github.com/yrj2011/jx-test-infra@v0.0.0-20190529031832-7a2065ee98eb/boskos/client/client_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 client
    18  
    19  import (
    20  	"errors"
    21  	"fmt"
    22  	"net/http"
    23  	"net/http/httptest"
    24  	"reflect"
    25  	"testing"
    26  	"time"
    27  
    28  	"k8s.io/test-infra/boskos/common"
    29  )
    30  
    31  const (
    32  	FakeRes    = "{\"name\": \"res\", \"type\": \"t\", \"state\": \"d\"}"
    33  	FakeMap    = "{\"res\":\"user\"}"
    34  	FakeMetric = "{\"type\":\"t\",\"current\":{\"s\":1},\"owner\":{\"merlin\":1}}"
    35  )
    36  
    37  func AreErrorsEqual(got error, expect error) bool {
    38  	if got == nil && expect == nil {
    39  		return true
    40  	}
    41  
    42  	if got == nil || expect == nil {
    43  		return false
    44  	}
    45  
    46  	return got.Error() == expect.Error()
    47  }
    48  
    49  func TestAcquire(t *testing.T) {
    50  	var testcases = []struct {
    51  		name      string
    52  		serverErr bool
    53  		expectErr error
    54  	}{
    55  		{
    56  			name:      "request error",
    57  			serverErr: true,
    58  			expectErr: fmt.Errorf("status %d %s, status code %d", http.StatusBadRequest, http.StatusText(http.StatusBadRequest), http.StatusBadRequest),
    59  		},
    60  		{
    61  			name:      "request successful",
    62  			expectErr: nil,
    63  		},
    64  	}
    65  
    66  	for _, tc := range testcases {
    67  		ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    68  			if tc.serverErr {
    69  				http.Error(w, "", http.StatusBadRequest)
    70  			} else {
    71  				fmt.Fprint(w, FakeRes)
    72  			}
    73  		}))
    74  		defer ts.Close()
    75  
    76  		c := NewClient("user", ts.URL)
    77  		res, err := c.Acquire("t", "s", "d")
    78  
    79  		if !AreErrorsEqual(err, tc.expectErr) {
    80  			t.Errorf("Test %v, got error %v, expect error %v", tc.name, err, tc.expectErr)
    81  		}
    82  		if err == nil {
    83  			if res.Name != "res" {
    84  				t.Errorf("Test %v, got resource name %v, expect res", tc.name, res.Name)
    85  			} else {
    86  				resources, _ := c.storage.List()
    87  				if len(resources) != 1 {
    88  					t.Errorf("Test %v, resource in client: %d, expect 1", tc.name, len(resources))
    89  				}
    90  
    91  			}
    92  		}
    93  	}
    94  }
    95  
    96  func TestRelease(t *testing.T) {
    97  	var testcases = []struct {
    98  		name      string
    99  		resources []string
   100  		res       string
   101  		expectErr error
   102  	}{
   103  		{
   104  			name:      "all - no res",
   105  			resources: []string{},
   106  			res:       "",
   107  			expectErr: errors.New("no holding resource"),
   108  		},
   109  		{
   110  			name:      "one - no res",
   111  			resources: []string{},
   112  			res:       "res",
   113  			expectErr: errors.New("no resource name res"),
   114  		},
   115  		{
   116  			name:      "one - no match",
   117  			resources: []string{"foo"},
   118  			res:       "res",
   119  			expectErr: errors.New("no resource name res"),
   120  		},
   121  		{
   122  			name:      "all - ok",
   123  			resources: []string{"foo"},
   124  			res:       "",
   125  			expectErr: nil,
   126  		},
   127  		{
   128  			name:      "one - ok",
   129  			resources: []string{"res"},
   130  			res:       "res",
   131  			expectErr: nil,
   132  		},
   133  	}
   134  
   135  	for _, tc := range testcases {
   136  		ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
   137  		defer ts.Close()
   138  
   139  		c := NewClient("user", ts.URL)
   140  		for _, r := range tc.resources {
   141  			c.storage.Add(common.Resource{Name: r})
   142  		}
   143  		var err error
   144  		if tc.res == "" {
   145  			err = c.ReleaseAll("d")
   146  		} else {
   147  			err = c.ReleaseOne(tc.res, "d")
   148  		}
   149  
   150  		if !AreErrorsEqual(err, tc.expectErr) {
   151  			t.Errorf("Test %v, got err %v, expect %v", tc.name, err, tc.expectErr)
   152  		}
   153  		resources, _ := c.storage.List()
   154  		if tc.expectErr == nil && len(resources) != 0 {
   155  			t.Errorf("Test %v, resource count %v, expect 0", tc.name, len(resources))
   156  		}
   157  	}
   158  }
   159  
   160  func TestUpdate(t *testing.T) {
   161  	var testcases = []struct {
   162  		name      string
   163  		resources []string
   164  		res       string
   165  		expectErr error
   166  	}{
   167  		{
   168  			name:      "all - no res",
   169  			resources: []string{},
   170  			res:       "",
   171  			expectErr: errors.New("no holding resource"),
   172  		},
   173  		{
   174  			name:      "one - no res",
   175  			resources: []string{},
   176  			res:       "res",
   177  			expectErr: errors.New("no resource name res"),
   178  		},
   179  		{
   180  			name:      "one - no match",
   181  			resources: []string{"foo"},
   182  			res:       "res",
   183  			expectErr: errors.New("no resource name res"),
   184  		},
   185  		{
   186  			name:      "all - ok",
   187  			resources: []string{"foo"},
   188  			res:       "",
   189  			expectErr: nil,
   190  		},
   191  		{
   192  			name:      "one - ok",
   193  			resources: []string{"res"},
   194  			res:       "res",
   195  			expectErr: nil,
   196  		},
   197  	}
   198  
   199  	for _, tc := range testcases {
   200  		ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
   201  		defer ts.Close()
   202  		c := NewClient("user", ts.URL)
   203  		for _, r := range tc.resources {
   204  			c.storage.Add(common.Resource{Name: r})
   205  		}
   206  
   207  		var err error
   208  		if tc.res == "" {
   209  			err = c.UpdateAll("s")
   210  		} else {
   211  			err = c.UpdateOne(tc.res, "s", nil)
   212  		}
   213  
   214  		if !AreErrorsEqual(err, tc.expectErr) {
   215  			t.Errorf("Test %v, got err %v, expect %v", tc.name, err, tc.expectErr)
   216  		}
   217  	}
   218  }
   219  
   220  func TestReset(t *testing.T) {
   221  	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   222  		fmt.Fprint(w, FakeMap)
   223  	}))
   224  	defer ts.Close()
   225  
   226  	c := NewClient("user", ts.URL)
   227  	rmap, err := c.Reset("t", "s", time.Minute, "d")
   228  	if err != nil {
   229  		t.Errorf("Error in reset : %v", err)
   230  	} else if len(rmap) != 1 {
   231  		t.Errorf("Resource in returned map: %d, expect 1", len(rmap))
   232  	} else if rmap["res"] != "user" {
   233  		t.Errorf("Owner of res: %s, expect user", rmap["res"])
   234  	}
   235  }
   236  
   237  func TestMetric(t *testing.T) {
   238  	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   239  		fmt.Fprint(w, FakeMetric)
   240  	}))
   241  	defer ts.Close()
   242  	expectMetric := common.Metric{
   243  		Type: "t",
   244  		Current: map[string]int{
   245  			"s": 1,
   246  		},
   247  		Owners: map[string]int{
   248  			"merlin": 1,
   249  		},
   250  	}
   251  
   252  	c := NewClient("user", ts.URL)
   253  	metric, err := c.Metric("t")
   254  	if err != nil {
   255  		t.Errorf("Error in reset : %v", err)
   256  	} else if !reflect.DeepEqual(metric, expectMetric) {
   257  		t.Errorf("wrong metric, got %v, want %v", metric, expectMetric)
   258  	}
   259  }