github.com/cloudberrydb/gpbackup@v1.0.3-0.20240118031043-5410fd45eed6/utils/util_test.go (about) 1 package utils_test 2 3 import ( 4 "github.com/cloudberrydb/gp-common-go-libs/testhelper" 5 "github.com/cloudberrydb/gpbackup/utils" 6 7 . "github.com/onsi/ginkgo/v2" 8 . "github.com/onsi/gomega" 9 ) 10 11 var _ = Describe("utils/util tests", func() { 12 Context("DollarQuoteString", func() { 13 It("uses $$ if the string contains no dollar signs", func() { 14 testStr := "message" 15 expected := "$$message$$" 16 actual := utils.DollarQuoteString(testStr) 17 Expect(actual).To(Equal(expected)) 18 }) 19 It("uses $_$ if the string contains $", func() { 20 testStr := "message$text" 21 expected := "$_$message$text$_$" 22 actual := utils.DollarQuoteString(testStr) 23 Expect(actual).To(Equal(expected)) 24 }) 25 It("uses $_X$ if the string contains $_", func() { 26 testStr := "message$_text" 27 expected := "$_X$message$_text$_X$" 28 actual := utils.DollarQuoteString(testStr) 29 Expect(actual).To(Equal(expected)) 30 }) 31 It("uses $_$ if the string contains non-adjacent $ and _", func() { 32 testStr := "message$text_" 33 expected := "$_$message$text_$_$" 34 actual := utils.DollarQuoteString(testStr) 35 Expect(actual).To(Equal(expected)) 36 }) 37 }) 38 Describe("ValidateFQNs", func() { 39 It("validates the following cases correctly", func() { 40 testStrings := []string{ 41 `schemaname.tablename`, // unquoted 42 `"schema,name".tablename`, // quoted schema 43 `schemaname."table,name"`, // quoted table 44 `schema name.tablename"`, // spaces 45 `schema name .tablename"`, //tabs 46 `schemaname.TABLENAME!@#$%^&*()_+={}|[]\';":/,?><"`, // special characters 47 } 48 utils.ValidateFQNs(testStrings) 49 }) 50 It("fails if given a string without a schema", func() { 51 testStrings := []string{`.tablename`} 52 err := utils.ValidateFQNs(testStrings) 53 Expect(err).To(HaveOccurred()) 54 }) 55 It("fails if given a string without a table", func() { 56 testStrings := []string{`schemaname.`} 57 err := utils.ValidateFQNs(testStrings) 58 Expect(err).To(HaveOccurred()) 59 }) 60 It("fails if the schema and table can't be determined", func() { 61 testStrings := []string{`schema.name.table.name`} 62 err := utils.ValidateFQNs(testStrings) 63 Expect(err).To(HaveOccurred()) 64 }) 65 }) 66 Context("ValidateFullPath", func() { 67 It("does not return error when the flag is not set", func() { 68 path := "" 69 Expect(utils.ValidateFullPath(path)).To(Succeed()) 70 }) 71 It("does not return error when given an absolute path", func() { 72 path := "/this/is/an/absolute/path" 73 Expect(utils.ValidateFullPath(path)).To(Succeed()) 74 }) 75 It("panics when given a relative path", func() { 76 path := "this/is/a/relative/path" 77 err := utils.ValidateFullPath(path) 78 Expect(err).To(MatchError("this/is/a/relative/path is not an absolute path.")) 79 80 }) 81 }) 82 Describe("ValidateGPDBVersionCompatibility", func() { 83 It("does not panic if GPDB version is at least 6.0.0", func() { 84 testhelper.SetDBVersion(connectionPool, "6.0.0") 85 utils.ValidateGPDBVersionCompatibility(connectionPool) 86 }) 87 }) 88 Describe("ValidateCompressionTypeAndLevel", func() { 89 It("validates a compression type 'gzip' and a level between 1 and 9", func() { 90 compressType := "gzip" 91 compressLevel := 5 92 err := utils.ValidateCompressionTypeAndLevel(compressType, compressLevel) 93 Expect(err).To(Not(HaveOccurred())) 94 }) 95 It("panics if given a compression type 'gzip' and a compression level < 1", func() { 96 compressType := "gzip" 97 compressLevel := 0 98 err := utils.ValidateCompressionTypeAndLevel(compressType, compressLevel) 99 Expect(err).To(MatchError("compression type 'gzip' only allows compression levels between 1 and 9, but the provided level is 0")) 100 }) 101 It("panics if given a compression type 'gzip' and a compression level > 9", func() { 102 compressType := "gzip" 103 compressLevel := 11 104 err := utils.ValidateCompressionTypeAndLevel(compressType, compressLevel) 105 Expect(err).To(MatchError("compression type 'gzip' only allows compression levels between 1 and 9, but the provided level is 11")) 106 }) 107 It("panics if given a compression type 'invalid' and a compression level > 0", func() { 108 compressType := "invalid" 109 compressLevel := 1 110 err := utils.ValidateCompressionTypeAndLevel(compressType, compressLevel) 111 Expect(err).To(MatchError("unknown compression type 'invalid'")) 112 }) 113 It("panics if given a compression type 'invalid' and a compression level < 0", func() { 114 compressType := "invalid" 115 compressLevel := -1 116 err := utils.ValidateCompressionTypeAndLevel(compressType, compressLevel) 117 Expect(err).To(MatchError("unknown compression type 'invalid'")) 118 }) 119 It("panics if given a compression type '' and a compression level > 0", func() { 120 compressType := "" 121 compressLevel := 1 122 err := utils.ValidateCompressionTypeAndLevel(compressType, compressLevel) 123 Expect(err).To(MatchError("unknown compression type ''")) 124 }) 125 It("validates a compression type 'zstd' and a level between 1 and 19", func() { 126 compressType := "zstd" 127 compressLevel := 11 128 err := utils.ValidateCompressionTypeAndLevel(compressType, compressLevel) 129 Expect(err).To(Not(HaveOccurred())) 130 }) 131 It("panics if given a compression type 'zstd' and a compression level < 1", func() { 132 compressType := "zstd" 133 compressLevel := 0 134 err := utils.ValidateCompressionTypeAndLevel(compressType, compressLevel) 135 Expect(err).To(MatchError("compression type 'zstd' only allows compression levels between 1 and 19, but the provided level is 0")) 136 }) 137 It("panics if given a compression type 'gzip' and a compression level > 19", func() { 138 compressType := "zstd" 139 compressLevel := 20 140 err := utils.ValidateCompressionTypeAndLevel(compressType, compressLevel) 141 Expect(err).To(MatchError("compression type 'zstd' only allows compression levels between 1 and 19, but the provided level is 20")) 142 }) 143 }) 144 Describe("UnquoteIdent", func() { 145 It("returns unchanged ident when passed a single char", func() { 146 dbname := `a` 147 resultString := utils.UnquoteIdent(dbname) 148 149 Expect(resultString).To(Equal(`a`)) 150 }) 151 It("returns unchanged ident when passed an unquoted ident", func() { 152 dbname := `test` 153 resultString := utils.UnquoteIdent(dbname) 154 155 Expect(resultString).To(Equal(`test`)) 156 }) 157 It("returns one double quote when passed a double quote", func() { 158 dbname := `"` 159 resultString := utils.UnquoteIdent(dbname) 160 161 Expect(resultString).To(Equal(`"`)) 162 }) 163 It("returns empty string when passed an empty string", func() { 164 dbname := "" 165 resultString := utils.UnquoteIdent(dbname) 166 167 Expect(resultString).To(Equal(``)) 168 }) 169 It("properly unquotes an identfier string and unescapes double quotes", func() { 170 dbname := `"""test"` 171 resultString := utils.UnquoteIdent(dbname) 172 173 Expect(resultString).To(Equal(`"test`)) 174 }) 175 }) 176 Describe("SliceToQuotedString", func() { 177 It("quotes and joins a slice of strings into a single string", func() { 178 inputStrings := []string{"string1", "string2", "string3"} 179 expectedString := "'string1','string2','string3'" 180 resultString := utils.SliceToQuotedString(inputStrings) 181 Expect(resultString).To(Equal(expectedString)) 182 }) 183 It("returns an empty string when given an empty slice", func() { 184 inputStrings := make([]string, 0) 185 resultString := utils.SliceToQuotedString(inputStrings) 186 Expect(resultString).To(Equal("")) 187 }) 188 }) 189 })