github.com/GoogleCloudPlatform/testgrid@v0.0.174/util/gcs/gcs_test.go (about) 1 /* 2 Copyright 2018 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 gcs 18 19 import ( 20 "errors" 21 "fmt" 22 "net/http" 23 "net/url" 24 "reflect" 25 "testing" 26 27 statepb "github.com/GoogleCloudPlatform/testgrid/pb/state" 28 "github.com/golang/protobuf/proto" 29 "google.golang.org/api/googleapi" 30 ) 31 32 func TestIsPreconditionFailed(t *testing.T) { 33 cases := []struct { 34 name string 35 err error 36 want bool 37 }{ 38 { 39 name: "pass", 40 }, 41 { 42 name: "normal", 43 err: errors.New("normal"), 44 }, 45 { 46 name: "googleapi", 47 err: &googleapi.Error{ 48 Code: 404, 49 }, 50 }, 51 { 52 name: "precondition", 53 err: &googleapi.Error{ 54 Code: http.StatusPreconditionFailed, 55 }, 56 want: true, 57 }, 58 { 59 name: "wrapped precondition", 60 err: fmt.Errorf("wrap: %w", &googleapi.Error{ 61 Code: http.StatusPreconditionFailed, 62 }), 63 want: true, 64 }, 65 } 66 67 for _, tc := range cases { 68 t.Run(tc.name, func(t *testing.T) { 69 if got := IsPreconditionFailed(tc.err); got != tc.want { 70 t.Errorf("isPreconditionFailed(%v) got %t, want %t", tc.err, got, tc.want) 71 } 72 }) 73 } 74 } 75 76 func Test_SetURL(t *testing.T) { 77 cases := []struct { 78 name string 79 url string 80 err bool 81 bucket string 82 object string 83 }{ 84 { 85 name: "only bucket", 86 url: "gs://thisbucket", 87 bucket: "thisbucket", 88 }, 89 { 90 name: "bucket and object", 91 url: "gs://first/second", 92 bucket: "first", 93 object: "second", 94 }, 95 { 96 name: "allow files", 97 url: "/path/to/my/bucket/foo", 98 bucket: "", 99 object: "path/to/my/bucket/foo", 100 }, 101 { 102 name: "allow file urls", 103 url: "file://path/to/my/bucket/foo", 104 bucket: "path", 105 object: "to/my/bucket/foo", 106 }, 107 { 108 name: "reject unknown scheme", 109 url: "foo://some/path", 110 err: true, 111 }, 112 { 113 name: "reject websites", 114 url: "http://example.com/object", 115 err: true, 116 }, 117 { 118 name: "reject ports", 119 url: "gs://first:123/second", 120 err: true, 121 }, 122 { 123 name: "reject username", 124 url: "gs://erick@first/second", 125 err: true, 126 }, 127 { 128 name: "reject queries", 129 url: "gs://first/second?query=true", 130 err: true, 131 }, 132 { 133 name: "reject fragments", 134 url: "gs://first/second#fragment", 135 err: true, 136 }, 137 } 138 for _, tc := range cases { 139 var p Path 140 err := p.Set(tc.url) 141 switch { 142 case err != nil && !tc.err: 143 t.Errorf("%s: unexpected error: %v", tc.name, err) 144 case err == nil && tc.err: 145 t.Errorf("%s: failed to raise an error", tc.name) 146 default: 147 if p.Bucket() != tc.bucket { 148 t.Errorf("%s: bad bucket %s != %s", tc.name, p.Bucket(), tc.bucket) 149 } 150 if p.Object() != tc.object { 151 t.Errorf("%s: bad object %s != %s", tc.name, p.Object(), tc.object) 152 } 153 } 154 } 155 } 156 157 func Test_ResolveReference(t *testing.T) { 158 var p Path 159 err := p.Set("gs://bucket/path/to/config") 160 if err != nil { 161 t.Fatalf("bad path: %v", err) 162 } 163 u, err := url.Parse("testgroup") 164 if err != nil { 165 t.Fatalf("bad url: %v", err) 166 } 167 q, err := p.ResolveReference(u) 168 if q.Object() != "path/to/testgroup" { 169 t.Errorf("bad object: %s", q) 170 } 171 if q.Bucket() != "bucket" { 172 t.Errorf("bad bucket: %s", q) 173 } 174 } 175 176 // Ensure that a == b => calcCRC(a) == calcCRC(b) 177 func Test_calcCRC(t *testing.T) { 178 b1 := []byte("hello") 179 b2 := []byte("world") 180 b1a := []byte("h") 181 b1a = append(b1a, []byte("ello")...) 182 c1 := calcCRC(b1) 183 c2 := calcCRC(b2) 184 c1a := calcCRC(b1a) 185 186 switch { 187 case c1 == c2: 188 t.Errorf("g1 crc %d should not equal g2 crc %d", c1, c2) 189 case len(b1) == 0, len(b2) == 0: 190 t.Errorf("empty b1 b2 %s %s", b1, b2) 191 case len(b1) != len(b1a), c1 != c1a: 192 t.Errorf("different results: %s %d != %s %d", b1, c1, b1a, c1a) 193 } 194 195 } 196 197 func TestMarshalGrid(t *testing.T) { 198 g1 := statepb.Grid{ 199 Columns: []*statepb.Column{ 200 {Build: "alpha"}, 201 {Build: "second"}, 202 }, 203 } 204 g2 := statepb.Grid{ 205 Columns: []*statepb.Column{ 206 {Build: "first"}, 207 {Build: "second"}, 208 }, 209 } 210 211 b1, e1 := MarshalGrid(&g1) 212 b2, e2 := MarshalGrid(&g2) 213 uncompressed, e1a := proto.Marshal(&g1) 214 215 switch { 216 case e1 != nil, e2 != nil: 217 t.Errorf("unexpected error %v %v %v", e1, e2, e1a) 218 } 219 220 if reflect.DeepEqual(b1, b2) { 221 t.Errorf("unexpected equality %v == %v", b1, b2) 222 } 223 224 if reflect.DeepEqual(b1, uncompressed) { 225 t.Errorf("should be compressed but is not: %v", b1) 226 } 227 }