github.com/treeverse/lakefs@v1.24.1-0.20240520134607-95648127bfb0/pkg/actions/testdata/lua/catalogexport_unity.lua (about) 1 local action = { 2 repository_id = "myRepo", 3 commit_id = "myCommit", 4 branch_id = "myBranch", 5 } 6 7 -- table names must be unique 8 local test_cases = { 9 { 10 name = "failed_not_delta_type", 11 tables = { 12 ["my_table_not_delta"] = { 13 td = { 14 type = "notDelta", 15 catalog = "ok", 16 name = "notDelta", 17 }, 18 }, 19 }, 20 error = "registration failed", 21 }, 22 { 23 name = "failed_no_catalog_name", 24 tables = { 25 ["my_table_no_catalog"] = { 26 td = { 27 type = "delta", 28 name = "noCatalog", 29 }, 30 }, 31 }, 32 error = "catalog name is required", 33 }, 34 { 35 name = "failed_no_name", 36 tables = { 37 ["my_table_no_name"] = { 38 td = { 39 type = "delta", 40 catalog = "ok", 41 }, 42 }, 43 }, 44 error = "table name is required", 45 }, 46 { 47 name = "failed_schema_creation", 48 tables = { 49 ["my_table_schema_failure"] = { 50 td = { 51 type = "delta", 52 catalog = "ok", 53 name = "schemaFailure", 54 }, 55 }, 56 }, 57 schema_failure = true, 58 error = "failed creating/getting catalog's schema", 59 }, 60 { 61 name = "success_all_tables", 62 tables = { 63 ["my_table_success"] = { 64 status = "SUCCEEDED", 65 }, 66 ["my_table2_success"] = { 67 status = "SUCCEEDED", 68 }, 69 ["my_table3_success"] = { 70 status = "SUCCEEDED", 71 }, 72 }, 73 }, 74 { 75 name = "mixed_statuses", 76 tables = { 77 ["my_table_failure"] = { 78 status = "FAILED", 79 }, 80 ["my_table2_success_2"] = { 81 status = "SUCCEEDED", 82 }, 83 ["my_table3_failure"] = { 84 status = "FAILED", 85 }, 86 }, 87 }, 88 } 89 90 -- Loads a mock table (descriptor) extractor 91 local function load_table_descriptor(tables) 92 package.loaded["lakefs/catalogexport/table_extractor"] = { 93 get_table_descriptor = function(_, _, _, table_src_path) 94 local examined_tables = {} 95 for name, t in pairs(tables) do 96 table.insert(examined_tables, name) 97 if string.find(table_src_path, name) then 98 if not t.td then 99 return { 100 type = "delta", 101 catalog = "ok", 102 name = name 103 } 104 end 105 return t.td 106 end 107 end 108 error("test was configured incorrectly. expected to find a table descriptor for table \"" .. table_src_path .. "\" but no such was found." ) 109 end 110 } 111 end 112 113 -- Generates a mock databricks client 114 local function db_client(schema_failure, tables) 115 return { 116 create_schema = function(branch_id, catalog, _) 117 if schema_failure then 118 return nil 119 end 120 return catalog .. "." .. branch_id 121 end, 122 register_external_table = function(table_name, _, _, _, _) 123 for name, t in pairs(tables) do 124 if name == table_name then 125 return t.status 126 end 127 end 128 end 129 } 130 end 131 132 --------------------------------- 133 ---------- Begin tests ---------- 134 --------------------------------- 135 for _, test in ipairs(test_cases) do 136 package.loaded["lakefs/catalogexport/unity_exporter"] = nil 137 load_table_descriptor(test.tables) 138 local unity_export = require("lakefs/catalogexport/unity_exporter") 139 local err = test.error 140 local schema_failure = test.schema_failure 141 local test_tables = test.tables 142 local table_paths = {} 143 for name, _ in pairs(test_tables) do 144 table_paths[name] = "s3://physical/" .. name 145 end 146 147 local db = db_client(schema_failure, test_tables) 148 -- Run test: 149 local s, resp = pcall(unity_export.register_tables, action, "_lakefs_tables", table_paths, db, "id") 150 if err ~= nil then 151 if s ~= false then -- the status is true which means no error was returned 152 local str_resp = "" 153 for k, v in pairs(resp) do 154 str_resp = str_resp .. k .. " = " .. v .. "\n" 155 end 156 error("test " .. test.name .. " expected an error:\n" .. err .. "\nbut returned status: \"" .. tostring(s) .. "\"\nresponse:\n" .. str_resp) 157 end 158 -- status is false as expected -> error returned 159 if string.find(resp, err) == nil then 160 error("test " .. test.name .. " returned incorrect error.\nexpected:\n" .. err .. "\nactual:\n" .. resp) 161 end 162 else 163 for table_name, status in pairs(resp) do 164 local expected_status = test.tables[table_name].status 165 if expected_status ~= status then 166 error("test " .. test.name .. " returned incorrect status for table \"" .. table_name .."\"\nexpected: \"" .. expected_status .. "\"\nactual:\n\"" .. status .. "\"") 167 end 168 end 169 end 170 end