From ef3158cac7341e7b846235d46f617942bb1dd7bc Mon Sep 17 00:00:00 2001 From: Mohammed Al Sahaf Date: Fri, 20 Mar 2026 20:53:10 +0300 Subject: [PATCH] storage: file_system Signed-off-by: Mohammed Al Sahaf --- modules/filestorage/filestorage_test.go | 102 ++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 modules/filestorage/filestorage_test.go diff --git a/modules/filestorage/filestorage_test.go b/modules/filestorage/filestorage_test.go new file mode 100644 index 000000000..daf3a9d24 --- /dev/null +++ b/modules/filestorage/filestorage_test.go @@ -0,0 +1,102 @@ +package filestorage + +import ( + "testing" + + "github.com/caddyserver/caddy/v2/caddyconfig/caddyfile" +) + +func TestFileStorageCaddyModule(t *testing.T) { + fs := FileStorage{} + info := fs.CaddyModule() + if info.ID != "caddy.storage.file_system" { + t.Errorf("CaddyModule().ID = %q, want 'caddy.storage.file_system'", info.ID) + } + mod := info.New() + if mod == nil { + t.Error("New() should not return nil") + } +} + +func TestFileStorageCertMagicStorage(t *testing.T) { + fs := FileStorage{Root: "/var/lib/caddy/certs"} + storage, err := fs.CertMagicStorage() + if err != nil { + t.Fatalf("CertMagicStorage() error = %v", err) + } + if storage == nil { + t.Fatal("CertMagicStorage() returned nil") + } +} + +func TestFileStorageCertMagicStorageEmptyRoot(t *testing.T) { + fs := FileStorage{Root: ""} + storage, err := fs.CertMagicStorage() + if err != nil { + t.Fatalf("CertMagicStorage() error = %v", err) + } + if storage == nil { + t.Fatal("CertMagicStorage() returned nil even with empty root") + } +} + +func TestFileStorageUnmarshalCaddyfile(t *testing.T) { + tests := []struct { + name string + input string + wantErr bool + wantVal string + }{ + { + name: "root as inline arg", + input: `file_system /var/lib/caddy`, + wantVal: "/var/lib/caddy", + }, + { + name: "root in block", + input: "file_system {\n\troot /var/lib/caddy\n}", + wantVal: "/var/lib/caddy", + }, + { + name: "missing root", + input: `file_system`, + wantErr: true, + }, + { + name: "too many inline args", + input: `file_system /path1 /path2`, + wantErr: true, + }, + { + name: "root already set inline then block", + input: "file_system /path1 {\n\troot /path2\n}", + wantErr: true, + }, + { + name: "unknown subdirective", + input: "file_system {\n\tunknown_option value\n}", + wantErr: true, + }, + { + name: "root in block without value", + input: "file_system {\n\troot\n}", + wantErr: true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + d := caddyfile.NewTestDispenser(tt.input) + fs := &FileStorage{} + err := fs.UnmarshalCaddyfile(d) + + if (err != nil) != tt.wantErr { + t.Errorf("UnmarshalCaddyfile() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !tt.wantErr && fs.Root != tt.wantVal { + t.Errorf("Root = %q, want %q", fs.Root, tt.wantVal) + } + }) + } +}