github.com/munnerz/test-infra@v0.0.0-20190108210205-ce3d181dc989/experiment/tracer/trace_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 main 18 19 import ( 20 "encoding/json" 21 "net/http" 22 "testing" 23 ) 24 25 func TestLinesByTimestampValidJSON(t *testing.T) { 26 tests := []struct { 27 name string 28 log linesByTimestamp 29 }{ 30 { 31 name: "no line", 32 log: linesByTimestamp{}, 33 }, 34 { 35 name: "single line", 36 log: linesByTimestamp{ 37 { 38 actual: []byte("{\"component\":\"jenkins-operator\",\"event-GUID\":\"thisisunique\",\"from\":\"triggered\",\"job\":\"origin-ci-ut-origin\",\"level\":\"info\",\"msg\":\"Transitioning states.\",\"name\":\"50ea24ea-d9a9-11e7-8e52-0a58ac101211\",\"org\":\"openshift\",\"pr\":17586,\"repo\":\"origin\",\"time\":\"2017-12-05T10:45:14Z\",\"to\":\"pending\",\"type\":\"presubmit\"}\n"), 39 }, 40 }, 41 }, 42 { 43 name: "multiple lines", 44 log: linesByTimestamp{ 45 { 46 actual: []byte("{\"component\":\"jenkins-operator\",\"event-GUID\":\"thisisunique\",\"from\":\"triggered\",\"job\":\"origin-ci-ut-origin\",\"level\":\"info\",\"msg\":\"Transitioning states.\",\"name\":\"50ea24ea-d9a9-11e7-8e52-0a58ac101211\",\"org\":\"openshift\",\"pr\":17586,\"repo\":\"origin\",\"time\":\"2017-12-05T10:45:14Z\",\"to\":\"pending\",\"type\":\"presubmit\"}\n"), 47 }, 48 { 49 actual: []byte("{\"author\":\"bob\",\"event-GUID\":\"thisisunique\",\"event-type\":\"issue_comment\",\"job\":\"test_pull_request_origin_extended_conformance_install_update\",\"level\":\"info\",\"msg\":\"Creating a new prowjob.\",\"name\":\"50edc666-d9a9-11e7-8e52-0a58ac101211\",\"org\":\"openshift\",\"plugin\":\"trigger\",\"pr\":17586,\"repo\":\"origin\",\"time\":\"2017-12-05T10:44:48Z\",\"type\":\"presubmit\",\"url\":\"an_url\"}\n"), 50 }, 51 { 52 actual: []byte("{\"author\":\"bob\",\"event-GUID\":\"thisisunique\",\"event-type\":\"issue_comment\",\"level\":\"info\",\"msg\":\"Starting test_pull_request_origin_extended_networking_minimal build.\",\"org\":\"openshift\",\"plugin\":\"trigger\",\"pr\":17586,\"repo\":\"origin\",\"time\":\"2017-12-05T10:44:48Z\",\"url\":\"an_url\"}\n"), 53 }, 54 }, 55 }, 56 } 57 58 for _, test := range tests { 59 jsonLog := test.log.String() 60 if !json.Valid([]byte(jsonLog)) { 61 t.Errorf("%s: got invalid json:\n%v", test.name, jsonLog) 62 } 63 } 64 } 65 66 func TestValidTraceRequest(t *testing.T) { 67 tests := []struct { 68 name string 69 url string 70 expectedErr string 71 }{ 72 { 73 name: "valid request - eventGUID", 74 url: "https://deck/trace?event-GUID=503265b0-d9a9-11e7-9b32-1fd823242322", 75 expectedErr: "", 76 }, 77 { 78 name: "valid request - org/repo#pr", 79 url: "https://deck/trace?repo=origin&org=openshift&pr=17586", 80 expectedErr: "", 81 }, 82 { 83 name: "valid request - org/repo#pr#issuecomment", 84 url: "https://deck/trace?repo=origin&org=openshift&pr=17586&issuecomment=350075289", 85 expectedErr: "", 86 }, 87 { 88 name: "invalid request - pr is not a number", 89 url: "https://deck/trace?repo=origin&org=openshift&pr=175fd", 90 expectedErr: "invalid pr query \"175fd\": strconv.Atoi: parsing \"175fd\": invalid syntax", 91 }, 92 { 93 name: "invalid request - pr is not a positive number", 94 url: "https://deck/trace?repo=origin&org=openshift&pr=-17453", 95 expectedErr: "invalid pr query \"-17453\": needs to be a positive number", 96 }, 97 { 98 name: "invalid request - missing org parameter", 99 url: "https://deck/trace?repo=origin&pr=17453", 100 expectedErr: "need either \"pr\", \"repo\", and \"org\", or \"event-GUID\", or \"issuecomment\" to be specified", 101 }, 102 { 103 name: "invalid request - missing repo parameter", 104 url: "https://deck/trace?org=openshift&pr=17453", 105 expectedErr: "need either \"pr\", \"repo\", and \"org\", or \"event-GUID\", or \"issuecomment\" to be specified", 106 }, 107 { 108 name: "invalid request - missing pr parameter", 109 url: "https://deck/trace?org=openshift&repo=origin", 110 expectedErr: "need either \"pr\", \"repo\", and \"org\", or \"event-GUID\", or \"issuecomment\" to be specified", 111 }, 112 { 113 name: "invalid request - missing org and repo parameter", 114 url: "https://deck/trace?pr=17453", 115 expectedErr: "need either \"pr\", \"repo\", and \"org\", or \"event-GUID\", or \"issuecomment\" to be specified", 116 }, 117 { 118 name: "invalid request - missing org and pr parameter", 119 url: "https://deck/trace?repo=origin", 120 expectedErr: "need either \"pr\", \"repo\", and \"org\", or \"event-GUID\", or \"issuecomment\" to be specified", 121 }, 122 { 123 name: "invalid request - missing repo and pr parameter", 124 url: "https://deck/trace?org=openshift", 125 expectedErr: "need either \"pr\", \"repo\", and \"org\", or \"event-GUID\", or \"issuecomment\" to be specified", 126 }, 127 { 128 name: "invalid request - no parameters", 129 url: "https://deck/trace", 130 expectedErr: "need either \"pr\", \"repo\", and \"org\", or \"event-GUID\", or \"issuecomment\" to be specified", 131 }, 132 { 133 name: "invalid request - issuecomment and event-GUID are mutually exclusive", 134 url: "https://deck/trace?repo=origin&org=openshift&pr=175&issuecomment=350075289&event-GUID=503265b0", 135 expectedErr: "cannot specify both issuecomment (350075289) and event-GUID (503265b0)", 136 }, 137 } 138 139 for _, test := range tests { 140 req, err := http.NewRequest(http.MethodGet, test.url, nil) 141 if err != nil { 142 t.Errorf("%s: unexpected error: %v", test.name, err) 143 continue 144 } 145 gotErr := validateTraceRequest(req) 146 if gotErr != nil && gotErr.Error() != test.expectedErr { 147 t.Errorf("%s: unexpected error: %q, expected: %q", test.name, gotErr.Error(), test.expectedErr) 148 continue 149 } 150 if test.expectedErr != "" && gotErr == nil { 151 t.Errorf("%s: expected an error (%s) but got none", test.name, test.expectedErr) 152 } 153 } 154 }