Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions pkg/chains/storage/docdb/docdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"net/url"
"os"
"path/filepath"
"slices"
"strings"

"github.com/fsnotify/fsnotify"
Expand Down Expand Up @@ -124,7 +123,15 @@ func WatchBackend(ctx context.Context, cfg config.Config, watcherStop chan bool)
continue
}

if !slices.Contains(pathsToWatch, event.Name) {
// Checking if the event.name matches ANY of the pathsToWatch
matched := false
for _, p := range pathsToWatch {
if strings.HasPrefix(event.Name, p) {
matched = true
break
}
}
if !matched {
continue
}

Expand All @@ -134,12 +141,14 @@ func WatchBackend(ctx context.Context, cfg config.Config, watcherStop chan bool)
if err != nil {
logger.Error(err)
backendChan <- nil
continue
}
} else if cfg.Storage.DocDB.MongoServerURLDir != "" {
updatedEnv, err = getMongoServerURLFromDir(cfg.Storage.DocDB.MongoServerURLDir)
if err != nil {
logger.Error(err)
backendChan <- nil
continue
}
}

Expand Down
72 changes: 58 additions & 14 deletions pkg/chains/storage/docdb/docdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ limitations under the License.
package docdb

import (
"context"
"encoding/json"
"os"
"path/filepath"
Expand All @@ -31,6 +32,33 @@ import (
rtesting "knative.dev/pkg/reconciler/testing"
)

func TestNewStorageBackend(t *testing.T) {
cfg := config.Config{
Storage: config.StorageConfigs{
DocDB: config.DocDBStorageConfig{
// "mem://collection_name/id_field"
// Here "collection" is the name of the collection, and "_id" is the primary key field.
URL: "mem://collection/_id",
},
},
}

ctx := context.Background()
backend, err := NewStorageBackend(ctx, cfg)

if err != nil {
t.Fatalf("NewStorageBackend() failed: %v", err)
}

if backend == nil {
t.Fatal("NewStorageBackend() returned nil backend")
}

if backend.coll == nil {
t.Fatal("NewStorageBackend() returned backend with nil collection")
}
}

func TestBackend_StorePayload(t *testing.T) {
ctx, _ := rtesting.SetupFakeContext(t)
type args struct {
Expand Down Expand Up @@ -202,6 +230,22 @@ func TestPopulateMongoServerURL(t *testing.T) {
expectedMongoEnv: mongoEnvFromFile,
wantErr: false,
},
{
name: "storage.docdb.mongo-server-url-path has precedence over EVERYTHING",
cfg: config.Config{
Storage: config.StorageConfigs{
DocDB: config.DocDBStorageConfig{
URL: "mongo://chainsdb/chainscollection?id_field=name",
MongoServerURLPath: filepath.Join(mongoDir, "MONGO_SERVER_URL"), // Point directly to the file
MongoServerURLDir: mongoDir, // Point to dir (lower priority)
MongoServerURL: "envFromConfig", // Point to string (lowest priority)
},
},
},
setMongoEnv: "mongoEnvVar",
expectedMongoEnv: mongoEnvFromFile,
wantErr: false,
},
}

for _, tt := range tests {
Expand Down Expand Up @@ -310,20 +354,20 @@ func TestWatchBackend(t *testing.T) {
expectedMongoEnv: testEnv,
wantErr: true,
},
// TODO: https://github.com/tektoncd/chains/issues/1178
// {
// name: "verify mongo-server-url-dir/MONGO_SERVER_URL is watched",
// cfg: config.Config{
// Storage: config.StorageConfigs{
// DocDB: config.DocDBStorageConfig{
// URL: "mongo://chainsdb/chainscollection?id_field=name",
// MongoServerURLDir: t.TempDir(),
// },
// },
// },
// expectedMongoEnv: "mongodb://updatedEnv",
// wantErr: false,
// },
// FIXED https://github.com/tektoncd/chains/issues/1178
{
name: "verify mongo-server-url-dir/MONGO_SERVER_URL is watched",
cfg: config.Config{
Storage: config.StorageConfigs{
DocDB: config.DocDBStorageConfig{
URL: "mongo://chainsdb/chainscollection?id_field=name",
MongoServerURLDir: t.TempDir(),
},
},
},
expectedMongoEnv: "mongodb://updatedEnv",
wantErr: false,
},
}

for _, tt := range tests {
Expand Down
Loading