From 16f854dd683d32dd4bb601e151a4ca4df1259d84 Mon Sep 17 00:00:00 2001 From: Eric Zhang Date: Mon, 15 Apr 2024 10:22:21 -0400 Subject: [PATCH] chore: add tests --- .../sidecarlogartifacts.go | 4 +- .../sidecarlogartifacts_test.go | 85 +++++++++++++++++++ 2 files changed, 86 insertions(+), 3 deletions(-) create mode 100644 internal/sidecarlogartifacts/sidecarlogartifacts_test.go diff --git a/internal/sidecarlogartifacts/sidecarlogartifacts.go b/internal/sidecarlogartifacts/sidecarlogartifacts.go index 981caf1a59d..65825c8d21f 100644 --- a/internal/sidecarlogartifacts/sidecarlogartifacts.go +++ b/internal/sidecarlogartifacts/sidecarlogartifacts.go @@ -17,7 +17,6 @@ import ( "context" "encoding/json" "fmt" - "log" "os" "path/filepath" "time" @@ -117,7 +116,7 @@ func GetArtifactsFromSidecarLogs(ctx context.Context, clientset kubernetes.Inter func LookForArtifacts(names []string, runDir string) (SidecarArtifacts, error) { err := waitForStepsToFinish(runDir) if err != nil { - log.Fatal(err) + return nil, err } artifacts := SidecarArtifacts{} for _, name := range names { @@ -132,7 +131,6 @@ func LookForArtifacts(names []string, runDir string) (SidecarArtifacts, error) { return artifacts, err } artifacts[name] = v1.Artifacts{Inputs: subRes.Inputs, Outputs: subRes.Outputs} - } return artifacts, nil } diff --git a/internal/sidecarlogartifacts/sidecarlogartifacts_test.go b/internal/sidecarlogartifacts/sidecarlogartifacts_test.go new file mode 100644 index 00000000000..9cff4e249dc --- /dev/null +++ b/internal/sidecarlogartifacts/sidecarlogartifacts_test.go @@ -0,0 +1,85 @@ +/* +Copyright 2024 The Tekton Authors + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package sidecarlogartifacts + +import ( + "os" + "path/filepath" + "testing" +) + +func TestLookForArtifacts_WaitForFiles(t *testing.T) { + + tests := []struct { + desc string + wantErr bool + runDirMode os.FileMode + stepDir string + outFile string + }{ + { + desc: "fail to read runDir, err", + wantErr: true, + runDirMode: 0o000, + }, + { + desc: "out.err file exist, no err", + runDirMode: 0o755, + stepDir: "first", + outFile: "out.err", + }, + { + desc: "out file exist, no err", + runDirMode: 0o755, + stepDir: "first", + outFile: "out.err", + }, + } + + for _, tc := range tests { + t.Run(tc.desc, func(t *testing.T) { + dir := t.TempDir() + _ = os.Chmod(dir, tc.runDirMode) + if tc.stepDir != "" { + _ = os.MkdirAll(filepath.Join(dir, tc.stepDir), os.ModePerm) + + outFile := filepath.Join(dir, tc.stepDir, tc.outFile) + _, err := os.Create(outFile) + if err != nil { + t.Fatalf("failed to create file %v", err) + } + } + _, err := LookForArtifacts([]string{}, dir) + if (err != nil) != tc.wantErr { + t.Fatalf("error checking failed, err: %v", err) + } + }) + } +} + +func TestLookForArtifacts(t *testing.T) { + + tests := []struct { + desc string + wantErr bool + runDirMode os.FileMode + }{{}} + + for _, tc := range tests { + + } +} \ No newline at end of file