github.com/tommi2day/tnscli@v0.0.0-20240401211958-338fc0647b73/cmd/list_test.go (about) 1 package cmd 2 3 import ( 4 "os" 5 "path" 6 "strings" 7 "testing" 8 9 "github.com/tommi2day/gomodules/common" 10 11 "github.com/tommi2day/tnscli/test" 12 13 "github.com/stretchr/testify/assert" 14 "github.com/stretchr/testify/require" 15 "github.com/tommi2day/gomodules/dblib" 16 ) 17 18 const tnsnamesora = ` 19 # Test ifile relative 20 ifile=ifile.ora 21 DB_T.local= 22 (DESCRIPTION= 23 (CONNECT_TIMEOUT=15) 24 (TRANSPORT_CONNECT_TIMEOUT=3) 25 (ADDRESS_LIST= 26 (FAILOVER=on) 27 (LOAD_BALANCE=on) 28 (ADDRESS= 29 (PROTOCOL=TCP) 30 (HOST=tdb1.ora.local) 31 (PORT=1562) 32 ) 33 (ADDRESS= 34 (PROTOCOL=TCP) 35 (HOST=tdb2.ora.local) 36 (PORT=1562) 37 ) 38 ) 39 (CONNECT_DATA= 40 (SERVER=dedicated) 41 (SERVICE_NAME=DB_T.local) 42 ) 43 ) 44 45 46 DB_V.local =(DESCRIPTION = 47 (CONNECT_TIMEOUT=15) 48 (RETRY_COUNT=20) 49 (RETRY_DELAY=3) 50 (TRANSPORT_CONNECT_TIMEOUT=3) 51 (ADDRESS_LIST = 52 (LOAD_BALANCE=ON) 53 (FAILOVER=ON) 54 (ADDRESS=(PROTOCOL=TCP)(HOST=vdb1.ora.local)(PORT=1672)) 55 (ADDRESS=(PROTOCOL=TCP)(HOST=vdb2.ora.local)(PORT=1672)) 56 ) 57 (CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME = DB_V.local)) 58 ) 59 ` 60 61 const ifileora = ` 62 XE =(DESCRIPTION = 63 (ADDRESS_LIST = (ADDRESS=(PROTOCOL=TCP)(HOST=127.0.0.1)(PORT=1521))) 64 (CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME = XE-ohne)) 65 ) 66 XE.local =(DESCRIPTION = 67 (ADDRESS_LIST = (ADDRESS=(PROTOCOL=TCP)(HOST=127.0.0.1)(PORT=1521))) 68 (CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME = XE)) 69 ) 70 XE1 =(DESCRIPTION = 71 (ADDRESS_LIST = (ADDRESS=(PROTOCOL=TCP)(HOST=127.0.0.1)(PORT=1521))) 72 (CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME = XE)) 73 ) 74 XEPDB1.local =(DESCRIPTION = 75 (ADDRESS_LIST = (ADDRESS=(PROTOCOL=TCP)(HOST=127.0.0.1)(PORT=1521))) 76 (CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME = XEPDB1)) 77 ) 78 SID.local =(DESCRIPTION = 79 (ADDRESS_LIST = (ADDRESS=(PROTOCOL=TCP)(HOST=127.0.0.1)(PORT=1521))) 80 (CONNECT_DATA=(SID = XESID)) 81 )` 82 const sqlnetora = ` 83 NAMES.DEFAULT_DOMAIN=local 84 NAMES.DIRECTORY_PATH=(TNSNAMES,EZCONNECT) 85 ` 86 const entryCount = 7 87 88 var tnsAdminDir = "testdata" 89 90 const tnsFile = "tnsnames.ora" 91 const sqlnetFile = "sqlnet.ora" 92 93 func TestParseTns(t *testing.T) { 94 var err error 95 test.Testinit(t) 96 //nolint gosec 97 err = os.Chdir(test.TestDir) 98 require.NoErrorf(t, err, "ChDir failed") 99 100 //nolint gosec 101 err = os.WriteFile(path.Join(tnsAdminDir, sqlnetFile), []byte(sqlnetora), 0644) 102 require.NoErrorf(t, err, "Create test sqlnet.ora failed") 103 //nolint gosec 104 err = os.WriteFile(path.Join(tnsAdminDir, tnsFile), []byte(tnsnamesora), 0644) 105 require.NoErrorf(t, err, "Create test tnsnames.ora failed") 106 //nolint gosec 107 err = os.WriteFile(path.Join(tnsAdminDir, "ifile.ora"), []byte(ifileora), 0644) 108 require.NoErrorf(t, err, "Create test ifile.ora failed") 109 110 filename = path.Join(tnsAdminDir, tnsFile) 111 t.Logf("load from %s", filename) 112 tnsEntries, domain, err := dblib.GetTnsnames(filename, true) 113 t.Logf("Default Domain: '%s'", domain) 114 t.Run("Parse TNSNames.ora", func(t *testing.T) { 115 require.NoErrorf(t, err, "Parsing %s failed: %s", filename, err) 116 }) 117 if err != nil { 118 t.Logf("load returned error: %s ", err) 119 return 120 } 121 t.Run("Count Entries", func(t *testing.T) { 122 countEntries := len(tnsEntries) 123 expected := entryCount 124 actual := countEntries 125 assert.Equal(t, expected, actual, "Count not expected") 126 }) 127 t.Run("Check entry", func(t *testing.T) { 128 type testTableType struct { 129 name string 130 alias string 131 success bool 132 service string 133 } 134 for _, testRun := range []testTableType{ 135 { 136 name: "XE-full", 137 alias: "XE.local", 138 success: true, 139 service: "XE", 140 }, 141 { 142 name: "XE-short", 143 alias: "XE", 144 success: true, 145 service: "XE", 146 }, 147 { 148 name: "XE-SID", 149 alias: "SID.local", 150 success: true, 151 service: "XESID", 152 }, 153 { 154 name: "XE1-short-invalid", 155 alias: "XE1", 156 success: false, 157 service: "", 158 }, 159 { 160 name: "XE+full-invalid", 161 alias: "XE1.local", 162 success: false, 163 service: "", 164 }, 165 { 166 name: "XE+invalid domain", 167 alias: "XE.xx.xx", 168 success: false, 169 service: "", 170 }, 171 { 172 name: "novalue", 173 alias: "", 174 success: false, 175 service: "", 176 }, 177 } { 178 t.Run(testRun.name, func(t *testing.T) { 179 e, ok := dblib.GetEntry(testRun.alias, tnsEntries, domain) 180 if testRun.success { 181 assert.True(t, ok, "Alias %s not found", testRun.alias) 182 name := strings.ToUpper(e.Name) 183 assert.True(t, strings.HasPrefix(name, strings.ToUpper(testRun.alias)), "entry not related to given alias %s", testRun.alias) 184 assert.Equalf(t, testRun.service, e.Service, "entry returned wrong service ('%s' <>'%s)", e.Service, testRun.service) 185 } else { 186 assert.False(t, ok, "Alias %s found, but shouldnt be", testRun.alias) 187 } 188 }) 189 } 190 }) 191 192 alias := "XE" 193 t.Run("Check entry value", func(t *testing.T) { 194 e, ok := dblib.GetEntry(alias, tnsEntries, domain) 195 assert.True(t, ok, "Alias %s not found", alias) 196 actualDesc := e.Desc 197 location := e.Location 198 expectedLocation := "ifile.ora Line: 6" 199 expectedDesc := `(DESCRIPTION = 200 (ADDRESS_LIST = (ADDRESS=(PROTOCOL=TCP)(HOST=127.0.0.1)(PORT=1521))) 201 (CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME = XE)) 202 )` 203 assert.Equal(t, strings.TrimSpace(expectedDesc), strings.TrimSpace(actualDesc), "Description not expected") 204 assert.Equal(t, expectedLocation, location, "Location not expected") 205 t.Logf("Location: %s", e.Location) 206 }) 207 t.Run("Check Server Entry", func(t *testing.T) { 208 e, found := tnsEntries[alias] 209 assert.True(t, found, "Alias not found") 210 actual := len(e.Servers) 211 expected := 1 212 assert.Equal(t, expected, actual, "Server Count not expected") 213 if actual > 0 { 214 server := e.Servers[0] 215 assert.NotEmpty(t, server.Host, "Host ist empty") 216 assert.NotEmpty(t, server.Port, "Port ist empty") 217 } 218 }) 219 var out = "" 220 t.Run("CMD List", func(t *testing.T) { 221 args := []string{ 222 "list", 223 "-A", tnsAdminDir, 224 "--filename", filename, 225 "--search", "XE1", 226 "--info", 227 "--unit-test", 228 } 229 out, err = common.CmdRun(RootCmd, args) 230 assert.NoErrorf(t, err, "List command should not return an error:%s", err) 231 assert.NotEmpty(t, out, "List should not empty") 232 assert.Contains(t, out, "found 1 ", "Output should state one entry") 233 t.Logf(out) 234 }) 235 }