github.com/munnerz/test-infra@v0.0.0-20190108210205-ce3d181dc989/prow/spyglass/gcsartifact_fetcher_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 spyglass 18 19 import ( 20 "testing" 21 ) 22 23 func TestNewGCSJobSource(t *testing.T) { 24 testCases := []struct { 25 name string 26 src string 27 exJobPrefix string 28 exBucket string 29 exName string 30 exBuildID string 31 expectedErr error 32 }{ 33 { 34 name: "Test standard GCS link", 35 src: "test-bucket/logs/example-ci-run/403", 36 exBucket: "test-bucket", 37 exJobPrefix: "logs/example-ci-run/403/", 38 exName: "example-ci-run", 39 exBuildID: "403", 40 expectedErr: nil, 41 }, 42 { 43 name: "Test GCS link with trailing /", 44 src: "test-bucket/logs/example-ci-run/403/", 45 exBucket: "test-bucket", 46 exJobPrefix: "logs/example-ci-run/403/", 47 exName: "example-ci-run", 48 exBuildID: "403", 49 expectedErr: nil, 50 }, 51 { 52 name: "Test GCS link with org name", 53 src: "test-bucket/logs/sig-flexing/example-ci-run/403", 54 exBucket: "test-bucket", 55 exJobPrefix: "logs/sig-flexing/example-ci-run/403/", 56 exName: "example-ci-run", 57 exBuildID: "403", 58 expectedErr: nil, 59 }, 60 } 61 for _, tc := range testCases { 62 t.Run(tc.name, func(t *testing.T) { 63 jobSource, err := newGCSJobSource(tc.src) 64 if err != tc.expectedErr { 65 t.Errorf("Expected err: %v, got err: %v", tc.expectedErr, err) 66 } 67 if tc.exBucket != jobSource.bucket { 68 t.Errorf("Expected bucket %s, got %s", tc.exBucket, jobSource.bucket) 69 } 70 if tc.exName != jobSource.jobName { 71 t.Errorf("Expected name %s, got %s", tc.exName, jobSource.jobName) 72 } 73 if tc.exJobPrefix != jobSource.jobPrefix { 74 t.Errorf("Expected name %s, got %s", tc.exJobPrefix, jobSource.jobPrefix) 75 } 76 }) 77 } 78 } 79 80 // Tests listing objects associated with the current job in GCS 81 func TestArtifacts_ListGCS(t *testing.T) { 82 fakeGCSClient := fakeGCSServer.Client() 83 testAf := NewGCSArtifactFetcher(fakeGCSClient) 84 testCases := []struct { 85 name string 86 handle artifactHandle 87 source string 88 expectedArtifacts []string 89 }{ 90 { 91 name: "Test ArtifactFetcher simple list artifacts", 92 source: "test-bucket/logs/example-ci-run/403", 93 expectedArtifacts: []string{ 94 "build-log.txt", 95 "started.json", 96 "finished.json", 97 "junit_01.xml", 98 "long-log.txt", 99 }, 100 }, 101 { 102 name: "Test ArtifactFetcher list artifacts on source with no artifacts", 103 source: "test-bucket/logs/example-ci/404", 104 expectedArtifacts: []string{}, 105 }, 106 } 107 108 for _, tc := range testCases { 109 actualArtifacts, err := testAf.artifacts(tc.source) 110 if err != nil { 111 t.Errorf("Failed to get artifact names: %v", err) 112 } 113 for _, ea := range tc.expectedArtifacts { 114 found := false 115 for _, aa := range actualArtifacts { 116 if ea == aa { 117 found = true 118 break 119 } 120 } 121 if !found { 122 t.Errorf("Case %s failed to retrieve the following artifact: %s\nRetrieved: %s.", tc.name, ea, actualArtifacts) 123 } 124 125 } 126 if len(tc.expectedArtifacts) != len(actualArtifacts) { 127 t.Errorf("Case %s produced more artifacts than expected. Expected: %s\nActual: %s.", tc.name, tc.expectedArtifacts, actualArtifacts) 128 } 129 } 130 } 131 132 // Tests getting handles to objects associated with the current job in GCS 133 func TestFetchArtifacts_GCS(t *testing.T) { 134 fakeGCSClient := fakeGCSServer.Client() 135 testAf := NewGCSArtifactFetcher(fakeGCSClient) 136 maxSize := int64(500e6) 137 testCases := []struct { 138 name string 139 artifactName string 140 source string 141 expectedSize int64 142 expectErr bool 143 }{ 144 { 145 name: "Fetch build-log.txt from valid source", 146 artifactName: "build-log.txt", 147 source: "test-bucket/logs/example-ci-run/403", 148 expectedSize: 25, 149 }, 150 { 151 name: "Fetch build-log.txt from invalid source", 152 artifactName: "build-log.txt", 153 source: "test-bucket/logs/example-ci-run/404", 154 expectErr: true, 155 }, 156 } 157 158 for _, tc := range testCases { 159 artifact, err := testAf.artifact(tc.source, tc.artifactName, maxSize) 160 if err != nil { 161 t.Errorf("Failed to get artifacts: %v", err) 162 } 163 size, err := artifact.Size() 164 if err != nil && !tc.expectErr { 165 t.Fatalf("%s failed getting size for artifact %s, err: %v", tc.name, artifact.JobPath(), err) 166 } 167 if err == nil && tc.expectErr { 168 t.Errorf("%s expected error, got no error", tc.name) 169 } 170 171 if size != tc.expectedSize { 172 t.Errorf("%s expected artifact with size %d but got %d", tc.name, tc.expectedSize, size) 173 } 174 } 175 }