From 9e62f24ea1582526fe21b4ab8815d0a52c00a0ad Mon Sep 17 00:00:00 2001 From: Spencer Schrock Date: Wed, 9 Nov 2022 09:52:53 -0800 Subject: [PATCH] Add tests for getPrefix Signed-off-by: Spencer Schrock --- cron/config/config.go | 17 ++-- cron/internal/controller/bucket.go | 2 +- cron/internal/controller/bucket_test.go | 95 +++++++++++++++++++ .../controller/testdata/getPrefix/marker | 1 + 4 files changed, 105 insertions(+), 10 deletions(-) create mode 100644 cron/internal/controller/bucket_test.go create mode 100644 cron/internal/controller/testdata/getPrefix/marker diff --git a/cron/config/config.go b/cron/config/config.go index 14177c194de..cbc47293313 100644 --- a/cron/config/config.go +++ b/cron/config/config.go @@ -37,11 +37,10 @@ const ( // TransferStatusFilename file identifies if shard transfer to BigQuery is completed. TransferStatusFilename string = ".transfer_complete" - configFlag string = "config" - configDefault string = "" - configUsage string = "Location of config file. Required" - - inputBucketName string = "input-bucket" + configFlag string = "config" + configDefault string = "" + configUsage string = "Location of config file. Required" + inputBucketParams string = "input-bucket" projectID string = "SCORECARD_PROJECT_ID" requestTopicURL string = "SCORECARD_REQUEST_TOPIC_URL" @@ -302,7 +301,7 @@ func GetAPIResultsBucketURL() (string, error) { // GetInputBucketURL() returns the bucket URL for input files. func GetInputBucketURL() (string, error) { - bucketParams, err := GetAdditionalParams(inputBucketName) + bucketParams, err := GetAdditionalParams(inputBucketParams) bURL, ok := bucketParams["url"] if err != nil || !ok { // TODO temporarily falling back to old variables until changes propagate to production @@ -313,7 +312,7 @@ func GetInputBucketURL() (string, error) { // GetInputBucketPrefix() returns the prefix used when fetching files from a bucket. func GetInputBucketPrefix() (string, error) { - bucketParams, err := GetAdditionalParams(inputBucketName) + bucketParams, err := GetAdditionalParams(inputBucketParams) if err != nil { // TODO temporarily falling back to old variables until changes propagate to production prefix, err := getStringConfigValue(inputBucketPrefix, configYAML, "InputBucketPrefix", "input-bucket-prefix") @@ -327,9 +326,9 @@ func GetInputBucketPrefix() (string, error) { // GetInputBucketPrefixFile() returns the file whose contents specify the prefix to use. func GetInputBucketPrefixFile() (string, error) { - bucketParams, err := GetAdditionalParams(inputBucketName) + bucketParams, err := GetAdditionalParams(inputBucketParams) if err != nil { - return "", fmt.Errorf("getting config for %s: %w", inputBucketName, err) + return "", fmt.Errorf("getting config for %s: %w", inputBucketParams, err) } return bucketParams["prefix-file"], nil } diff --git a/cron/internal/controller/bucket.go b/cron/internal/controller/bucket.go index 82dbda4d683..203f872112e 100644 --- a/cron/internal/controller/bucket.go +++ b/cron/internal/controller/bucket.go @@ -1,4 +1,4 @@ -// Copyright 2021 Security Scorecard Authors +// Copyright 2022 Security Scorecard Authors // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/cron/internal/controller/bucket_test.go b/cron/internal/controller/bucket_test.go new file mode 100644 index 00000000000..38e78b419e9 --- /dev/null +++ b/cron/internal/controller/bucket_test.go @@ -0,0 +1,95 @@ +// Copyright 2022 Security Scorecard 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 main + +import ( + "context" + "path/filepath" + "testing" +) + +//nolint:tparallel,paralleltest // since t.Setenv is used +func TestGetPrefix(t *testing.T) { + //nolint:govet + testcases := []struct { + name string + url string + prefix string + prefixFile string + want string + wantErr bool + }{ + { + name: "no prefix", + url: "testdata/getPrefix", + prefix: "", + prefixFile: "", + want: "", + wantErr: false, + }, + { + name: "prefix set", + url: "testdata/getPrefix", + prefix: "foo", + prefixFile: "", + want: "foo", + wantErr: false, + }, + { + name: "prefix file set", + url: "testdata/getPrefix", + prefix: "", + prefixFile: "marker", + want: "bar", + wantErr: false, + }, + { + name: "prefix and prefix file set", + url: "testdata/getPrefix", + prefix: "foo", + prefixFile: "marker", + want: "foo", + wantErr: false, + }, + { + name: "non existent prefix file", + url: "testdata/getPrefix", + prefix: "", + prefixFile: "baz", + want: "", + wantErr: true, + }, + } + + for _, testcase := range testcases { + testcase := testcase + t.Run(testcase.name, func(t *testing.T) { + t.Setenv("INPUT_BUCKET_URL", testcase.url) + t.Setenv("INPUT_BUCKET_PREFIX", testcase.prefix) + t.Setenv("INPUT_BUCKET_PREFIX_FILE", testcase.prefixFile) + testdataPath, err := filepath.Abs(testcase.url) + if err != nil { + t.Errorf("unexpected error: %v", err) + } + got, err := getPrefix(context.Background(), "file:///"+testdataPath) + if (err != nil) != testcase.wantErr { + t.Errorf("unexpected error produced: %v", err) + } + if got != testcase.want { + t.Errorf("test failed: expected - %s, got = %s", testcase.want, got) + } + }) + } +} diff --git a/cron/internal/controller/testdata/getPrefix/marker b/cron/internal/controller/testdata/getPrefix/marker new file mode 100644 index 00000000000..5716ca5987c --- /dev/null +++ b/cron/internal/controller/testdata/getPrefix/marker @@ -0,0 +1 @@ +bar