Skip to content

Commit

Permalink
Add tests for MONGO_SERVER_URL rotation
Browse files Browse the repository at this point in the history
  • Loading branch information
concaf committed May 6, 2024
1 parent b4230b7 commit e76e64a
Showing 1 changed file with 108 additions and 0 deletions.
108 changes: 108 additions & 0 deletions pkg/chains/storage/docdb/docdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ package docdb

import (
"encoding/json"
"os"
"path/filepath"
"testing"

"github.com/tektoncd/chains/pkg/chains/objects"
Expand Down Expand Up @@ -117,3 +119,109 @@ func TestBackend_StorePayload(t *testing.T) {
})
}
}

func TestPopulateMongoServerURL(t *testing.T) {
mongoDir := t.TempDir()
mongoEnvFromFile := "mongoEnvFromFile"
if err := os.WriteFile(filepath.Join(mongoDir, "MONGO_SERVER_URL"), []byte(mongoEnvFromFile), 0644); err != nil {
t.Fatal(err)
}

tests := []struct {
name string
cfg config.Config
setMongoEnv string
expectedMongoEnv string
wantErr bool
}{
{
name: "fail when MONGO_SERVER_URL is not set but storage.docdb.url is set",
cfg: config.Config{
Storage: config.StorageConfigs{
DocDB: config.DocDBStorageConfig{
URL: "mongo://chainsdb/chainscollection?id_field=name",
},
},
},
wantErr: true,
},
{
name: "pass when MONGO_SERVER_URL is set and storage.docdb.url is set",
cfg: config.Config{
Storage: config.StorageConfigs{
DocDB: config.DocDBStorageConfig{
URL: "mongo://chainsdb/chainscollection?id_field=name",
},
},
},
setMongoEnv: "testEnv",
expectedMongoEnv: "testEnv",
wantErr: false,
},
{
name: "storage.docdb.mongo-server-url has more precedence than MONGO_SERVER_URL",
cfg: config.Config{
Storage: config.StorageConfigs{
DocDB: config.DocDBStorageConfig{
URL: "mongo://chainsdb/chainscollection?id_field=name",
MongoServerURL: "envFromConfig",
},
},
},
setMongoEnv: "testEnv",
expectedMongoEnv: "envFromConfig",
wantErr: false,
},
{
name: "storage.docdb.mongo-server-url works solo",
cfg: config.Config{
Storage: config.StorageConfigs{
DocDB: config.DocDBStorageConfig{
URL: "mongo://chainsdb/chainscollection?id_field=name",
MongoServerURL: "envFromConfigSolo",
},
},
},
setMongoEnv: "",
expectedMongoEnv: "envFromConfigSolo",
wantErr: false,
},
{
name: "storage.docdb.mongo-server-url-dir has precedence over storage.docdb.mongo-server-url and MONGO_SERVER_URL",
cfg: config.Config{
Storage: config.StorageConfigs{
DocDB: config.DocDBStorageConfig{
URL: "mongo://chainsdb/chainscollection?id_field=name",
MongoServerURLDir: mongoDir,
MongoServerURL: "envFromConfig",
},
},
},
setMongoEnv: "mongoEnvVar",
expectedMongoEnv: mongoEnvFromFile,
wantErr: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
defer os.Unsetenv("MONGO_SERVER_URL")
ctx, _ := rtesting.SetupFakeContext(t)

if tt.setMongoEnv != "" {
if err := os.Setenv("MONGO_SERVER_URL", tt.setMongoEnv); err != nil {
t.Error(err)
}
}

if err := populateMongoServerURL(ctx, tt.cfg); (err != nil) != tt.wantErr {
t.Errorf("did not expect an error, but got: %v", err)
}

currentMongoEnv := os.Getenv("MONGO_SERVER_URL")
if os.Getenv("MONGO_SERVER_URL") != tt.expectedMongoEnv {
t.Errorf("expected MONGO_SERVER_URL to be: %s, but got: %s", tt.expectedMongoEnv, currentMongoEnv)
}
})
}
}

0 comments on commit e76e64a

Please sign in to comment.