github.com/siglens/siglens@v0.0.0-20240328180423-f7ce9ae441ed/pkg/retention/retention_test.go (about) 1 /* 2 Copyright 2023. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package retention 18 19 import ( 20 "fmt" 21 "os" 22 "testing" 23 "time" 24 25 "github.com/siglens/siglens/pkg/config" 26 27 "github.com/stretchr/testify/assert" 28 ) 29 30 func Test_IsDirEmpty(t *testing.T) { 31 type args struct { 32 name string 33 dirName string 34 } 35 tests := []struct { 36 name string 37 args args 38 want bool 39 }{ 40 { 41 name: "Test Empty Directory", 42 args: args{ 43 name: "data", 44 dirName: "data", 45 }, 46 want: true, 47 }, 48 { 49 name: "Test Empty Directory", 50 args: args{ 51 name: "data", 52 dirName: "data/test", 53 }, 54 want: false, 55 }, 56 } 57 for _, tt := range tests { 58 t.Run(tt.name, func(t *testing.T) { 59 err := os.MkdirAll(tt.args.dirName, 0755) 60 if err != nil { 61 panic("error creating directory") 62 } 63 if got := IsDirEmpty(tt.args.name); got != tt.want { 64 t.Errorf("IsDirEmpty() = %v, want %v", got, tt.want) 65 } 66 os.RemoveAll("data") 67 }) 68 } 69 } 70 71 func Test_RecursivelyDeleteParentDirectories(t *testing.T) { 72 config.InitializeDefaultConfig() 73 type args struct { 74 filePath string 75 testFile1 string 76 testFile2 string 77 fileExists string 78 } 79 tests := []struct { 80 name string 81 args args 82 }{ 83 { 84 name: "Test positive scenario till final directory", 85 args: args{ 86 filePath: "data/test_host/final/2022/01/01", 87 testFile1: "data/test_host/final/2023/01/01", 88 testFile2: "data/test_host/final/2023/02/02", 89 fileExists: "data/test_host/final/2022", 90 }, 91 }, 92 { 93 name: "Test positive scenario month folder", 94 args: args{ 95 filePath: "data/test_host/final/2022/01/01", 96 testFile1: "data/test_host/final/2022/01/02", 97 testFile2: "data/test_host/final/2022/01/03", 98 fileExists: "data/test_host/final/2022/01/01", 99 }, 100 }, 101 } 102 for _, tt := range tests { 103 t.Run(tt.name, func(t *testing.T) { 104 if tt.args.filePath != "" { 105 err := os.MkdirAll(tt.args.filePath, 0755) 106 fmt.Println(err) 107 } 108 if tt.args.testFile1 != "" { 109 err := os.MkdirAll(tt.args.testFile1, 0755) 110 fmt.Println(err) 111 } 112 if tt.args.testFile2 != "" { 113 err := os.MkdirAll(tt.args.testFile2, 0755) 114 fmt.Println(err) 115 } 116 RecursivelyDeleteParentDirectories(tt.args.filePath + "/t.txt") 117 assert.NoDirExists(t, tt.args.fileExists, "Failed to backtrack cleanup") 118 os.RemoveAll("data") 119 }) 120 } 121 } 122 123 func Test_GetRetentionTimeMs(t *testing.T) { 124 currTime := time.Now() 125 oneHourAgo := time.Now().Add(-time.Duration(time.Hour)) 126 retentionInMs := GetRetentionTimeMs(1, currTime) 127 assert.Equal(t, uint64(oneHourAgo.UnixMilli()), retentionInMs) 128 }