github.com/abayer/test-infra@v0.0.5/prow/cmd/tot/main_test.go (about)

     1  /*
     2  Copyright 2016 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 main
    18  
    19  import (
    20  	"fmt"
    21  	"io/ioutil"
    22  	"net/http"
    23  	"net/http/httptest"
    24  	"os"
    25  	"reflect"
    26  	"strings"
    27  	"testing"
    28  
    29  	"k8s.io/test-infra/prow/config"
    30  )
    31  
    32  func expectEqual(t *testing.T, msg string, have interface{}, want interface{}) {
    33  	if !reflect.DeepEqual(have, want) {
    34  		t.Errorf("bad %s: got %v, wanted %v",
    35  			msg, have, want)
    36  	}
    37  }
    38  
    39  func makeStore(t *testing.T) *store {
    40  	tmp, err := ioutil.TempFile("", "tot_test_")
    41  	if err != nil {
    42  		t.Fatal(err)
    43  	}
    44  	os.Remove(tmp.Name()) // json decoding an empty file throws an error
    45  
    46  	store, err := newStore(tmp.Name())
    47  	if err != nil {
    48  		t.Fatal(err)
    49  	}
    50  
    51  	return store
    52  }
    53  
    54  func TestVend(t *testing.T) {
    55  	store := makeStore(t)
    56  	defer os.Remove(store.storagePath)
    57  
    58  	expectEqual(t, "empty vend", store.vend("a"), 1)
    59  	expectEqual(t, "second vend", store.vend("a"), 2)
    60  	expectEqual(t, "third vend", store.vend("a"), 3)
    61  	expectEqual(t, "second empty", store.vend("b"), 1)
    62  
    63  	store2, err := newStore(store.storagePath)
    64  	if err != nil {
    65  		t.Fatal(err)
    66  	}
    67  	expectEqual(t, "fourth vend, different instance", store2.vend("a"), 4)
    68  }
    69  
    70  func TestSet(t *testing.T) {
    71  	store := makeStore(t)
    72  	defer os.Remove(store.storagePath)
    73  
    74  	store.set("foo", 300)
    75  	expectEqual(t, "peek", store.peek("foo"), 300)
    76  	store.set("foo2", 300)
    77  	expectEqual(t, "vend", store.vend("foo2"), 301)
    78  	expectEqual(t, "vend", store.vend("foo2"), 302)
    79  }
    80  
    81  func expectResponse(t *testing.T, handler http.Handler, req *http.Request, msg, value string) {
    82  	rr := httptest.NewRecorder()
    83  
    84  	handler.ServeHTTP(rr, req)
    85  
    86  	expectEqual(t, "http status OK", rr.Code, 200)
    87  
    88  	expectEqual(t, msg, rr.Body.String(), value)
    89  }
    90  
    91  func TestHandler(t *testing.T) {
    92  	store := makeStore(t)
    93  	defer os.Remove(store.storagePath)
    94  
    95  	handler := http.HandlerFunc(store.handle)
    96  
    97  	req, err := http.NewRequest("GET", "/vend/foo", nil)
    98  	if err != nil {
    99  		t.Fatal(err)
   100  	}
   101  
   102  	expectResponse(t, handler, req, "http vend", "1")
   103  	expectResponse(t, handler, req, "http vend", "2")
   104  
   105  	req, err = http.NewRequest("HEAD", "/vend/foo", nil)
   106  	if err != nil {
   107  		t.Fatal(err)
   108  	}
   109  
   110  	expectResponse(t, handler, req, "http peek", "2")
   111  
   112  	req, err = http.NewRequest("POST", "/vend/bar", strings.NewReader("40"))
   113  	if err != nil {
   114  		t.Fatal(err)
   115  	}
   116  
   117  	expectResponse(t, handler, req, "http post", "")
   118  
   119  	req, err = http.NewRequest("HEAD", "/vend/bar", nil)
   120  	if err != nil {
   121  		t.Fatal(err)
   122  	}
   123  
   124  	expectResponse(t, handler, req, "http vend", "40")
   125  }
   126  
   127  type mapHandler map[string]string
   128  
   129  func (h mapHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
   130  	fmt.Fprintf(w, "%s", h[r.URL.Path])
   131  }
   132  
   133  func TestFallback(t *testing.T) {
   134  	serv := httptest.NewServer(mapHandler(map[string]string{
   135  		"/logs/foo/latest-build.txt": "200",
   136  		"/logs/bar/latest-build.txt": "\t300 \n",
   137  		"/logs/baz/latest-build.txt": "asdf",
   138  	}))
   139  	defer serv.Close()
   140  
   141  	store := makeStore(t)
   142  	defer os.Remove(store.storagePath)
   143  	store.fallbackFunc = fallbackHandler{template: serv.URL + "/logs/%s/latest-build.txt"}.get
   144  
   145  	expectEqual(t, "vend foo 1", store.vend("foo"), 201)
   146  	expectEqual(t, "vend foo 2", store.vend("foo"), 202)
   147  
   148  	expectEqual(t, "vend bar", store.vend("bar"), 301)
   149  	expectEqual(t, "vend baz", store.vend("baz"), 1)
   150  	expectEqual(t, "vend quux", store.vend("quux"), 1)
   151  }
   152  
   153  var c *config.Config
   154  
   155  func TestMain(m *testing.M) {
   156  	conf, err := config.Load("../../config.yaml", "../../../config/jobs")
   157  	if err != nil {
   158  		fmt.Printf("Could not load config: %v", err)
   159  		os.Exit(1)
   160  	}
   161  	c = conf
   162  	os.Exit(m.Run())
   163  }
   164  
   165  func TestGetURL(t *testing.T) {
   166  	tests := []struct {
   167  		name string
   168  
   169  		jobName  string
   170  		template string
   171  		c        *config.Config
   172  		bucket   string
   173  
   174  		expected string
   175  	}{
   176  		{
   177  			name: "fallback template",
   178  
   179  			jobName:  "pull-community-verify",
   180  			template: "https://storage.googleapis.com/kubernetes-jenkins/logs/%s/latest-build.txt",
   181  
   182  			expected: "https://storage.googleapis.com/kubernetes-jenkins/logs/pull-community-verify/latest-build.txt",
   183  		},
   184  		{
   185  			name: "fallback bucket - presubmit",
   186  
   187  			jobName: "pull-community-verify",
   188  			c:       c,
   189  			bucket:  "https://storage.googleapis.com/kubernetes-jenkins",
   190  
   191  			expected: "https://storage.googleapis.com/kubernetes-jenkins/pr-logs/directory/pull-community-verify/latest-build.txt",
   192  		},
   193  		{
   194  			name: "fallback bucket - postsubmit",
   195  
   196  			jobName: "ci-federation-release",
   197  			c:       c,
   198  			bucket:  "https://storage.googleapis.com/kubernetes-jenkins",
   199  
   200  			expected: "https://storage.googleapis.com/kubernetes-jenkins/logs/ci-federation-release/latest-build.txt",
   201  		},
   202  		{
   203  			name: "fallback bucket - periodic",
   204  
   205  			jobName: "ci-kubernetes-cross-build",
   206  			c:       c,
   207  			bucket:  "https://storage.googleapis.com/kubernetes-jenkins",
   208  
   209  			expected: "https://storage.googleapis.com/kubernetes-jenkins/logs/ci-kubernetes-cross-build/latest-build.txt",
   210  		},
   211  		{
   212  			name: "fallback bucket - unknown",
   213  
   214  			jobName: "a-name-that-is-what-it-is",
   215  			c:       c,
   216  			bucket:  "https://storage.googleapis.com/kubernetes-jenkins",
   217  
   218  			expected: "",
   219  		},
   220  		{
   221  			name: "fallback bucket with trailing slash",
   222  
   223  			jobName: "pull-community-verify",
   224  			c:       c,
   225  			bucket:  "https://storage.googleapis.com/kubernetes-jenkins/",
   226  
   227  			expected: "https://storage.googleapis.com/kubernetes-jenkins/pr-logs/directory/pull-community-verify/latest-build.txt",
   228  		},
   229  	}
   230  
   231  	for _, test := range tests {
   232  		t.Logf("running scenario %q", test.name)
   233  
   234  		var configAgent *config.Agent
   235  		if test.c != nil {
   236  			configAgent = new(config.Agent)
   237  			configAgent.Set(test.c)
   238  		}
   239  		f := fallbackHandler{
   240  			template:    test.template,
   241  			configAgent: configAgent,
   242  			bucket:      test.bucket,
   243  		}
   244  
   245  		if got := f.getURL(test.jobName); got != test.expected {
   246  			t.Errorf("unexpected URL:\n%s\nexpected:\n%s", got, test.expected)
   247  		}
   248  	}
   249  }