|
| 1 | +package cache |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "os" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/adrg/xdg" |
| 9 | + "github.com/google/uuid" |
| 10 | +) |
| 11 | + |
| 12 | +func TestGetObject(t *testing.T) { |
| 13 | + tests := []struct { |
| 14 | + description string |
| 15 | + identifier string |
| 16 | + expectFile bool |
| 17 | + expectedErr error |
| 18 | + }{ |
| 19 | + { |
| 20 | + description: "identifier exists", |
| 21 | + identifier: "test-cache-get-exists", |
| 22 | + expectFile: true, |
| 23 | + expectedErr: nil, |
| 24 | + }, |
| 25 | + { |
| 26 | + description: "identifier does not exist", |
| 27 | + identifier: "test-cache-get-not-exists", |
| 28 | + expectFile: false, |
| 29 | + expectedErr: os.ErrNotExist, |
| 30 | + }, |
| 31 | + { |
| 32 | + description: "identifier is invalid", |
| 33 | + identifier: "in../../valid", |
| 34 | + expectFile: false, |
| 35 | + expectedErr: ErrorInvalidCacheIdentifier, |
| 36 | + }, |
| 37 | + } |
| 38 | + |
| 39 | + for _, tt := range tests { |
| 40 | + t.Run(tt.description, func(t *testing.T) { |
| 41 | + id := tt.identifier + "-" + uuid.NewString() |
| 42 | + |
| 43 | + // setup |
| 44 | + if tt.expectFile { |
| 45 | + err := createFolderIfNotExists(cacheFolderPath) |
| 46 | + if err != nil { |
| 47 | + t.Fatalf("create cache folder: %s", err.Error()) |
| 48 | + } |
| 49 | + path := cacheFolderPath + "/" + id |
| 50 | + if err := os.WriteFile(path, []byte("dummy"), 0o600); err != nil { |
| 51 | + t.Fatalf("setup: WriteFile (%s) failed", path) |
| 52 | + } |
| 53 | + } |
| 54 | + // test |
| 55 | + file, err := GetObject(id) |
| 56 | + |
| 57 | + if !errors.Is(err, tt.expectedErr) { |
| 58 | + t.Fatalf("returned error (%q) does not match %q", err.Error(), tt.expectedErr.Error()) |
| 59 | + } |
| 60 | + |
| 61 | + if tt.expectFile { |
| 62 | + if len(file) < 1 { |
| 63 | + t.Fatalf("expected a file but byte array is empty (len %d)", len(file)) |
| 64 | + } |
| 65 | + } else { |
| 66 | + if len(file) > 0 { |
| 67 | + t.Fatalf("didn't expect a file, but byte array is not empty (len %d)", len(file)) |
| 68 | + } |
| 69 | + } |
| 70 | + }) |
| 71 | + } |
| 72 | +} |
| 73 | +func TestPutObject(t *testing.T) { |
| 74 | + // Also test createFolderIfNotExists here |
| 75 | + tests := []struct { |
| 76 | + description string |
| 77 | + identifier string |
| 78 | + existingFile bool |
| 79 | + expectFile bool |
| 80 | + expectedErr error |
| 81 | + customPath string |
| 82 | + }{ |
| 83 | + { |
| 84 | + description: "identifier already exists", |
| 85 | + identifier: "test-cache-put-exists", |
| 86 | + existingFile: true, |
| 87 | + expectFile: true, |
| 88 | + expectedErr: nil, |
| 89 | + }, |
| 90 | + { |
| 91 | + description: "identifier does not exist", |
| 92 | + identifier: "test-cache-put-not-exists", |
| 93 | + expectFile: true, |
| 94 | + expectedErr: nil, |
| 95 | + }, |
| 96 | + { |
| 97 | + description: "identifier is invalid", |
| 98 | + identifier: "in../../valid", |
| 99 | + expectFile: false, |
| 100 | + expectedErr: ErrorInvalidCacheIdentifier, |
| 101 | + }, |
| 102 | + { |
| 103 | + description: "directory does not yet exist", |
| 104 | + identifier: "test-cache-put-folder-not-exists", |
| 105 | + expectFile: true, |
| 106 | + expectedErr: nil, |
| 107 | + customPath: "/tmp/stackit-cli-test", |
| 108 | + }, |
| 109 | + } |
| 110 | + |
| 111 | + for _, tt := range tests { |
| 112 | + t.Run(tt.description, func(t *testing.T) { |
| 113 | + id := tt.identifier + "-" + uuid.NewString() |
| 114 | + if tt.customPath != "" { |
| 115 | + cacheFolderPath = tt.customPath |
| 116 | + } else { |
| 117 | + cacheFolderPath = xdg.CacheHome |
| 118 | + } |
| 119 | + path := cacheFolderPath + "/" + id |
| 120 | + |
| 121 | + // setup |
| 122 | + if tt.existingFile { |
| 123 | + if err := os.WriteFile(path, []byte("dummy"), 0o600); err != nil { |
| 124 | + t.Fatalf("setup: WriteFile (%s) failed", path) |
| 125 | + } |
| 126 | + } |
| 127 | + // test |
| 128 | + err := PutObject(id, []byte("dummy")) |
| 129 | + |
| 130 | + if !errors.Is(err, tt.expectedErr) { |
| 131 | + t.Fatalf("returned error (%q) does not match %q", err.Error(), tt.expectedErr.Error()) |
| 132 | + } |
| 133 | + |
| 134 | + if tt.expectFile { |
| 135 | + if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) { |
| 136 | + t.Fatalf("expected file (%q) to exist", path) |
| 137 | + } |
| 138 | + } |
| 139 | + }) |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +func TestDeleteObject(t *testing.T) { |
| 144 | + tests := []struct { |
| 145 | + description string |
| 146 | + identifier string |
| 147 | + existingFile bool |
| 148 | + expectedErr error |
| 149 | + }{ |
| 150 | + { |
| 151 | + description: "identifier exists", |
| 152 | + identifier: "test-cache-delete-exists", |
| 153 | + existingFile: true, |
| 154 | + expectedErr: nil, |
| 155 | + }, |
| 156 | + { |
| 157 | + description: "identifier does not exist", |
| 158 | + identifier: "test-cache-delete-not-exists", |
| 159 | + existingFile: false, |
| 160 | + expectedErr: nil, |
| 161 | + }, |
| 162 | + { |
| 163 | + description: "identifier is invalid", |
| 164 | + identifier: "in../../valid", |
| 165 | + existingFile: false, |
| 166 | + expectedErr: ErrorInvalidCacheIdentifier, |
| 167 | + }, |
| 168 | + } |
| 169 | + |
| 170 | + for _, tt := range tests { |
| 171 | + t.Run(tt.description, func(t *testing.T) { |
| 172 | + id := tt.identifier + "-" + uuid.NewString() |
| 173 | + path := cacheFolderPath + "/" + id |
| 174 | + |
| 175 | + // setup |
| 176 | + if tt.existingFile { |
| 177 | + if err := os.WriteFile(path, []byte("dummy"), 0o600); err != nil { |
| 178 | + t.Fatalf("setup: WriteFile (%s) failed", path) |
| 179 | + } |
| 180 | + } |
| 181 | + // test |
| 182 | + err := DeleteObject(id) |
| 183 | + |
| 184 | + if !errors.Is(err, tt.expectedErr) { |
| 185 | + t.Fatalf("returned error (%q) does not match %q", err.Error(), tt.expectedErr.Error()) |
| 186 | + } |
| 187 | + |
| 188 | + if tt.existingFile { |
| 189 | + if _, err := os.Stat(path); !errors.Is(err, os.ErrNotExist) { |
| 190 | + t.Fatalf("expected file (%q) to not exist", path) |
| 191 | + } |
| 192 | + } |
| 193 | + }) |
| 194 | + } |
| 195 | +} |
0 commit comments