diff --git a/pkg/collector/corechecks/oracle/sql/test-env/setup_test_env.sql b/pkg/collector/corechecks/oracle/sql/test-env/setup_test_env.sql index 09dd742f2a24b..d21fb5100aa81 100644 --- a/pkg/collector/corechecks/oracle/sql/test-env/setup_test_env.sql +++ b/pkg/collector/corechecks/oracle/sql/test-env/setup_test_env.sql @@ -54,9 +54,16 @@ begin if l_create_dir is null then select SUBSTR(file_name,1,(INSTR(file_name,'/',-1,1)-1)) into dir from dba_data_files where rownum = 1; execute immediate 'create tablespace tbs_test datafile ''' || dir || '/tbs_test01.dbf'' size 100M'; + execute immediate 'create tablespace tbs_test_offline datafile ''' || dir || '/tbs_test01.dbf'' size 10M'; else execute immediate 'create tablespace tbs_test datafile size 100M'; + execute immediate 'create tablespace tbs_test_offline datafile size 10M'; end if; end; / +-- to avoid size getting to 0 with RDS or Oracle managed files +create table t_tbs_test(n number) tablespace tbs_test; +insert into t_tbs_test values(1); +commit; + diff --git a/pkg/collector/corechecks/oracle/tablespaces_test.go b/pkg/collector/corechecks/oracle/tablespaces_test.go index 6ee11232793df..8488eb09ddbf0 100644 --- a/pkg/collector/corechecks/oracle/tablespaces_test.go +++ b/pkg/collector/corechecks/oracle/tablespaces_test.go @@ -31,14 +31,7 @@ func TestTablespaces(t *testing.T) { err := c.Run() require.NoError(t, err) s.On("Gauge", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return() - var expectedPdb string - if c.hostingType == rds { - expectedPdb = "orcl" - } else if c.connectedToPdb { - expectedPdb = c.cdbName - } else { - expectedPdb = "cdb$root" - } + expectedPdb := getExpectedPdb(&c) tags := []string{fmt.Sprintf("pdb:%s", expectedPdb), "tablespace:TBS_TEST"} s.AssertMetricOnce(t, "Gauge", "oracle.tablespace.size", 104857600, c.dbHostname, tags) s.AssertMetricOnce(t, "Gauge", "oracle.tablespace.offline", 0, c.dbHostname, tags) @@ -47,19 +40,20 @@ func TestTablespaces(t *testing.T) { func TestTablespacesOffline(t *testing.T) { c, s := newDefaultCheck(t, "", "") defer c.Teardown() - connection := getConnectData(t, useSysUser) databaseUrl := go_ora.BuildUrl(connection.Server, connection.Port, connection.ServiceName, connection.Username, connection.Password, nil) conn, err2 := sql.Open("oracle", databaseUrl) require.NoError(t, err2) + const tablespaceName = "TBS_TEST_OFFLINE" + defer func() { - _, err := conn.Exec("ALTER TABLESPACE TBS_TEST ONLINE") + _, err := conn.Exec(fmt.Sprintf("ALTER TABLESPACE %s ONLINE", tablespaceName)) require.NoError(t, err) conn.Close() }() - _, err3 := conn.Exec("ALTER TABLESPACE TBS_TEST OFFLINE") + _, err3 := conn.Exec(fmt.Sprintf("ALTER TABLESPACE %s OFFLINE", tablespaceName)) require.NoError(t, err3) time.Sleep(10 * time.Second) @@ -67,6 +61,12 @@ func TestTablespacesOffline(t *testing.T) { err := c.Run() require.NoError(t, err) s.On("Gauge", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return() + expectedPdb := getExpectedPdb(&c) + tags := []string{fmt.Sprintf("pdb:%s", expectedPdb), fmt.Sprintf("tablespace:%s", tablespaceName)} + s.AssertMetricOnce(t, "Gauge", "oracle.tablespace.offline", 1, c.dbHostname, tags) +} + +func getExpectedPdb(c *Check) string { var expectedPdb string if c.hostingType == rds { expectedPdb = "orcl" @@ -75,6 +75,5 @@ func TestTablespacesOffline(t *testing.T) { } else { expectedPdb = "cdb$root" } - tags := []string{fmt.Sprintf("pdb:%s", expectedPdb), "tablespace:TBS_TEST"} - s.AssertMetricOnce(t, "Gauge", "oracle.tablespace.offline", 1, c.dbHostname, tags) + return expectedPdb }