github.com/munnerz/test-infra@v0.0.0-20190108210205-ce3d181dc989/testgrid/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  	"net/url"
    21  	"testing"
    22  )
    23  
    24  func Test_SetURL(t *testing.T) {
    25  	cases := []struct {
    26  		name   string
    27  		url    string
    28  		err    bool
    29  		bucket string
    30  		object string
    31  	}{
    32  		{
    33  			name:   "only bucket",
    34  			url:    "gs://thisbucket",
    35  			bucket: "thisbucket",
    36  		},
    37  		{
    38  			name:   "bucket and object",
    39  			url:    "gs://first/second",
    40  			bucket: "first",
    41  			object: "second",
    42  		},
    43  		{
    44  			name: "reject files",
    45  			url:  "/path/to/my/bucket",
    46  			err:  true,
    47  		},
    48  		{
    49  			name: "reject websites",
    50  			url:  "http://example.com/object",
    51  			err:  true,
    52  		},
    53  		{
    54  			name: "reject ports",
    55  			url:  "gs://first:123/second",
    56  			err:  true,
    57  		},
    58  		{
    59  			name: "reject username",
    60  			url:  "gs://erick@first/second",
    61  			err:  true,
    62  		},
    63  		{
    64  			name: "reject queries",
    65  			url:  "gs://first/second?query=true",
    66  			err:  true,
    67  		},
    68  		{
    69  			name: "reject fragments",
    70  			url:  "gs://first/second#fragment",
    71  			err:  true,
    72  		},
    73  	}
    74  	for _, tc := range cases {
    75  		var p Path
    76  		err := p.Set(tc.url)
    77  		switch {
    78  		case err != nil && !tc.err:
    79  			t.Errorf("%s: unexpected error: %v", tc.name, err)
    80  		case err == nil && tc.err:
    81  			t.Errorf("%s: failed to raise an error", tc.name)
    82  		default:
    83  			if p.Bucket() != tc.bucket {
    84  				t.Errorf("%s: bad bucket %s != %s", tc.name, p.Bucket(), tc.bucket)
    85  			}
    86  			if p.Object() != tc.object {
    87  				t.Errorf("%s: bad object %s != %s", tc.name, p.Object(), tc.object)
    88  			}
    89  		}
    90  	}
    91  }
    92  
    93  func Test_ResolveReference(t *testing.T) {
    94  	var p Path
    95  	err := p.Set("gs://bucket/path/to/config")
    96  	if err != nil {
    97  		t.Fatalf("bad path: %v", err)
    98  	}
    99  	u, err := url.Parse("testgroup")
   100  	if err != nil {
   101  		t.Fatalf("bad url: %v", err)
   102  	}
   103  	q, err := p.ResolveReference(u)
   104  	if q.Object() != "path/to/testgroup" {
   105  		t.Errorf("bad object: %s", q)
   106  	}
   107  	if q.Bucket() != "bucket" {
   108  		t.Errorf("bad bucket: %s", q)
   109  	}
   110  }
   111  
   112  // Ensure that a == b => calcCRC(a) == calcCRC(b)
   113  func Test_calcCRC(t *testing.T) {
   114  	b1 := []byte("hello")
   115  	b2 := []byte("world")
   116  	b1a := []byte("h")
   117  	b1a = append(b1a, []byte("ello")...)
   118  	c1 := calcCRC(b1)
   119  	c2 := calcCRC(b2)
   120  	c1a := calcCRC(b1a)
   121  
   122  	switch {
   123  	case c1 == c2:
   124  		t.Errorf("g1 crc %d should not equal g2 crc %d", c1, c2)
   125  	case len(b1) == 0, len(b2) == 0:
   126  		t.Errorf("empty b1 b2 %s %s", b1, b2)
   127  	case len(b1) != len(b1a), c1 != c1a:
   128  		t.Errorf("different results: %s %d != %s %d", b1, c1, b1a, c1a)
   129  	}
   130  
   131  }