sigs.k8s.io/prow@v0.0.0-20240503223140-c5e374dc7eb1/pkg/spyglass/podlogartifact_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 "bytes" 21 "context" 22 "fmt" 23 "testing" 24 25 "sigs.k8s.io/prow/pkg/kube" 26 ) 27 28 // Tests getting handles to objects associated with the current Prow job 29 func TestFetchArtifacts_Prow(t *testing.T) { 30 goodFetcher := NewPodLogArtifactFetcher(&fakePodLogJAgent{}) 31 maxSize := int64(500e6) 32 testCases := []struct { 33 name string 34 key string 35 artifact string 36 expectedPath string 37 expectedLink string 38 expected []byte 39 expectErr bool 40 }{ 41 { 42 name: "Fetch build-log.txt from valid src", 43 key: "BFG/435", 44 artifact: singleLogName, 45 expectedLink: fmt.Sprintf("/log?container=%s&id=435&job=BFG", kube.TestContainerName), 46 expected: []byte("frobscottle"), 47 }, 48 { 49 name: "Fetch log from empty src", 50 key: "", 51 artifact: singleLogName, 52 expectErr: true, 53 }, 54 { 55 name: "Fetch log from incomplete src", 56 key: "BFG", 57 artifact: singleLogName, 58 expectErr: true, 59 }, 60 { 61 name: "Fetch log with no artifact name", 62 key: "BFG/435", 63 artifact: "", 64 expectErr: true, 65 }, 66 { 67 name: "Fetch log with custom artifact name", 68 key: "BFG/435", 69 artifact: fmt.Sprintf("%s-%s", customContainerName, singleLogName), 70 expectedLink: fmt.Sprintf("/log?container=%s&id=435&job=BFG", customContainerName), 71 expected: []byte("snozzcumber"), 72 }, 73 } 74 75 for _, tc := range testCases { 76 artifact, err := goodFetcher.Artifact(context.Background(), tc.key, tc.artifact, maxSize) 77 if err != nil && !tc.expectErr { 78 t.Errorf("%s: failed unexpectedly for artifact %s, err: %v", tc.name, artifact.JobPath(), err) 79 continue 80 } 81 if err == nil && tc.expectErr { 82 t.Errorf("%s: expected error, got no error", tc.name) 83 continue 84 } 85 86 if artifact != nil { 87 if artifact.JobPath() != tc.artifact { 88 t.Errorf("Unexpected job path, expected %s, got %q", artifact.JobPath(), tc.artifact) 89 } 90 link := artifact.CanonicalLink() 91 if link != tc.expectedLink { 92 t.Errorf("Unexpected link, expected %s, got %q", tc.expectedLink, link) 93 } 94 res, err := artifact.ReadAll() 95 if err != nil { 96 t.Fatalf("%s failed reading bytes of log. got err: %v", tc.name, err) 97 } 98 if !bytes.Equal(tc.expected, res) { 99 t.Errorf("Unexpected result of reading pod logs, expected %q, got %q", tc.expected, res) 100 } 101 } 102 103 } 104 }