github.com/zntrio/harp/v2@v2.0.9/pkg/bundle/fs/root_test.go (about) 1 // Licensed to Elasticsearch B.V. under one or more contributor 2 // license agreements. See the NOTICE file distributed with 3 // this work for additional information regarding copyright 4 // ownership. Elasticsearch B.V. licenses this file to you under 5 // the Apache License, Version 2.0 (the "License"); you may 6 // not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, 12 // software distributed under the License is distributed on an 13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 // KIND, either express or implied. See the License for the 15 // specific language governing permissions and limitations 16 // under the License. 17 18 //go:build go1.16 19 // +build go1.16 20 21 package fs 22 23 import ( 24 "io/fs" 25 "testing" 26 27 fuzz "github.com/google/gofuzz" 28 29 bundlev1 "github.com/zntrio/harp/v2/api/gen/go/harp/bundle/v1" 30 ) 31 32 func TestFromBundle(t *testing.T) { 33 type args struct { 34 b *bundlev1.Bundle 35 } 36 tests := []struct { 37 name string 38 args args 39 want BundleFS 40 wantErr bool 41 }{ 42 { 43 name: "nil", 44 wantErr: true, 45 }, 46 { 47 name: "empty", 48 args: args{ 49 b: &bundlev1.Bundle{ 50 Packages: []*bundlev1.Package{}, 51 }, 52 }, 53 wantErr: false, 54 }, 55 { 56 name: "valid", 57 args: args{ 58 b: &bundlev1.Bundle{ 59 Packages: []*bundlev1.Package{ 60 { 61 Name: "application/test", 62 }, 63 { 64 Name: "application/production/test", 65 }, 66 { 67 Name: "application/staging/test", 68 }, 69 }, 70 }, 71 }, 72 wantErr: false, 73 }, 74 } 75 for _, tt := range tests { 76 t.Run(tt.name, func(t *testing.T) { 77 _, err := FromBundle(tt.args.b) 78 if (err != nil) != tt.wantErr { 79 t.Errorf("FromBundle() error = %v, wantErr %v", err, tt.wantErr) 80 return 81 } 82 }) 83 } 84 } 85 86 func TestFromBundle_Fuzz(t *testing.T) { 87 // Making sure the descrption never panics 88 for i := 0; i < 50; i++ { 89 f := fuzz.New() 90 91 var ( 92 src bundlev1.Bundle 93 ) 94 95 // Prepare arguments 96 f.Fuzz(&src) 97 98 // Execute 99 FromBundle(&src) 100 } 101 } 102 103 func mustFromBundle(b *bundlev1.Bundle) BundleFS { 104 fs, err := FromBundle(b) 105 if err != nil { 106 panic(err) 107 } 108 return fs 109 } 110 111 var testBundle = &bundlev1.Bundle{ 112 Packages: []*bundlev1.Package{ 113 { 114 Name: "application/test", 115 }, 116 { 117 Name: "application/production/test", 118 }, 119 { 120 Name: "application/staging/test", 121 }, 122 }, 123 } 124 125 func Test_bundleFs_Open(t *testing.T) { 126 type args struct { 127 name string 128 } 129 tests := []struct { 130 name string 131 fs BundleFS 132 args args 133 want fs.File 134 wantErr bool 135 }{ 136 { 137 name: "empty", 138 fs: mustFromBundle(testBundle), 139 args: args{ 140 name: "", 141 }, 142 }, 143 { 144 name: "directory", 145 fs: mustFromBundle(testBundle), 146 args: args{ 147 name: "application/production", 148 }, 149 }, 150 { 151 name: "directory not exists", 152 fs: mustFromBundle(testBundle), 153 args: args{ 154 name: "application/whatever", 155 }, 156 wantErr: true, 157 }, 158 { 159 name: "file", 160 fs: mustFromBundle(testBundle), 161 args: args{ 162 name: "application/production/test", 163 }, 164 }, 165 { 166 name: "file not exists", 167 fs: mustFromBundle(testBundle), 168 args: args{ 169 name: "application/production/whatever", 170 }, 171 wantErr: true, 172 }, 173 } 174 for _, tt := range tests { 175 t.Run(tt.name, func(t *testing.T) { 176 _, err := tt.fs.Open(tt.args.name) 177 if (err != nil) != tt.wantErr { 178 t.Errorf("bundleFs.Open() error = %v, wantErr %v", err, tt.wantErr) 179 return 180 } 181 }) 182 } 183 } 184 185 func Test_bundleFs_Open_Fuzz(t *testing.T) { 186 bfs, _ := FromBundle(testBundle) 187 188 // Making sure the descrption never panics 189 for i := 0; i < 50; i++ { 190 f := fuzz.New() 191 192 var ( 193 name string 194 ) 195 196 // Prepare arguments 197 f.Fuzz(&name) 198 199 // Execute 200 bfs.Open(name) 201 } 202 } 203 204 func Test_bundleFs_ReadDir(t *testing.T) { 205 type args struct { 206 name string 207 } 208 tests := []struct { 209 name string 210 fs BundleFS 211 args args 212 want []fs.DirEntry 213 wantErr bool 214 }{ 215 { 216 name: "empty", 217 fs: mustFromBundle(testBundle), 218 args: args{ 219 name: "", 220 }, 221 }, 222 { 223 name: "directory", 224 fs: mustFromBundle(testBundle), 225 args: args{ 226 name: "application/production", 227 }, 228 }, 229 { 230 name: "directory not exists", 231 fs: mustFromBundle(testBundle), 232 args: args{ 233 name: "application/whatever", 234 }, 235 wantErr: true, 236 }, 237 { 238 name: "file", 239 fs: mustFromBundle(testBundle), 240 args: args{ 241 name: "application/production/test", 242 }, 243 wantErr: true, 244 }, 245 { 246 name: "file not exists", 247 fs: mustFromBundle(testBundle), 248 args: args{ 249 name: "application/production/whatever", 250 }, 251 wantErr: true, 252 }, 253 } 254 for _, tt := range tests { 255 t.Run(tt.name, func(t *testing.T) { 256 _, err := tt.fs.ReadDir(tt.args.name) 257 if (err != nil) != tt.wantErr { 258 t.Errorf("bundleFs.ReadDir() error = %v, wantErr %v", err, tt.wantErr) 259 return 260 } 261 }) 262 } 263 } 264 265 func Test_bundleFs_ReadDir_Fuzz(t *testing.T) { 266 bfs, _ := FromBundle(testBundle) 267 268 // Making sure the descrption never panics 269 for i := 0; i < 50; i++ { 270 f := fuzz.New() 271 272 var ( 273 name string 274 ) 275 276 // Prepare arguments 277 f.Fuzz(&name) 278 279 // Execute 280 bfs.ReadDir(name) 281 } 282 } 283 284 func Test_bundleFs_ReadFile(t *testing.T) { 285 type args struct { 286 name string 287 } 288 tests := []struct { 289 name string 290 fs BundleFS 291 args args 292 want []fs.DirEntry 293 wantErr bool 294 }{ 295 { 296 name: "empty", 297 fs: mustFromBundle(testBundle), 298 args: args{ 299 name: "", 300 }, 301 wantErr: true, 302 }, 303 { 304 name: "directory", 305 fs: mustFromBundle(testBundle), 306 args: args{ 307 name: "application/production", 308 }, 309 wantErr: true, 310 }, 311 { 312 name: "directory not exists", 313 fs: mustFromBundle(testBundle), 314 args: args{ 315 name: "application/whatever", 316 }, 317 wantErr: true, 318 }, 319 { 320 name: "file", 321 fs: mustFromBundle(testBundle), 322 args: args{ 323 name: "application/production/test", 324 }, 325 }, 326 { 327 name: "file not exists", 328 fs: mustFromBundle(testBundle), 329 args: args{ 330 name: "application/production/whatever", 331 }, 332 wantErr: true, 333 }, 334 } 335 for _, tt := range tests { 336 t.Run(tt.name, func(t *testing.T) { 337 _, err := tt.fs.ReadFile(tt.args.name) 338 if (err != nil) != tt.wantErr { 339 t.Errorf("bundleFs.ReadFile() error = %v, wantErr %v", err, tt.wantErr) 340 return 341 } 342 }) 343 } 344 } 345 346 func Test_bundleFs_ReadFile_Fuzz(t *testing.T) { 347 bfs, _ := FromBundle(testBundle) 348 349 // Making sure the descrption never panics 350 for i := 0; i < 50; i++ { 351 f := fuzz.New() 352 353 var ( 354 name string 355 ) 356 357 // Prepare arguments 358 f.Fuzz(&name) 359 360 // Execute 361 bfs.ReadFile(name) 362 } 363 } 364 365 func Test_bundleFs_Stat(t *testing.T) { 366 type args struct { 367 name string 368 } 369 tests := []struct { 370 name string 371 fs BundleFS 372 args args 373 want []fs.DirEntry 374 wantErr bool 375 }{ 376 { 377 name: "empty", 378 fs: mustFromBundle(testBundle), 379 args: args{ 380 name: "", 381 }, 382 }, 383 { 384 name: "directory", 385 fs: mustFromBundle(testBundle), 386 args: args{ 387 name: "application/production", 388 }, 389 }, 390 { 391 name: "directory not exists", 392 fs: mustFromBundle(testBundle), 393 args: args{ 394 name: "application/whatever", 395 }, 396 wantErr: true, 397 }, 398 { 399 name: "file", 400 fs: mustFromBundle(testBundle), 401 args: args{ 402 name: "application/production/test", 403 }, 404 }, 405 { 406 name: "file not exists", 407 fs: mustFromBundle(testBundle), 408 args: args{ 409 name: "application/production/whatever", 410 }, 411 wantErr: true, 412 }, 413 } 414 for _, tt := range tests { 415 t.Run(tt.name, func(t *testing.T) { 416 _, err := tt.fs.Stat(tt.args.name) 417 if (err != nil) != tt.wantErr { 418 t.Errorf("bundleFs.Stat() error = %v, wantErr %v", err, tt.wantErr) 419 return 420 } 421 }) 422 } 423 } 424 425 func Test_bundleFs_Stat_Fuzz(t *testing.T) { 426 bfs, _ := FromBundle(testBundle) 427 428 // Making sure the descrption never panics 429 for i := 0; i < 50; i++ { 430 f := fuzz.New() 431 432 var ( 433 name string 434 ) 435 436 // Prepare arguments 437 f.Fuzz(&name) 438 439 // Execute 440 bfs.Stat(name) 441 } 442 }