Skip to content

Commit

Permalink
Add tests for getPrefix
Browse files Browse the repository at this point in the history
Signed-off-by: Spencer Schrock <sschrock@google.com>
  • Loading branch information
spencerschrock committed Nov 9, 2022
1 parent 5a0505a commit 9e62f24
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 10 deletions.
17 changes: 8 additions & 9 deletions cron/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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")
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion cron/internal/controller/bucket.go
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
95 changes: 95 additions & 0 deletions cron/internal/controller/bucket_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}
1 change: 1 addition & 0 deletions cron/internal/controller/testdata/getPrefix/marker
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bar

0 comments on commit 9e62f24

Please sign in to comment.