Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor tests #119

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 8 additions & 14 deletions cmd/synthetic_checks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,7 @@ func TestRunSyntheticTests(t *testing.T) {
defer mockPromAggregationGateway.Close()

// Set the PROM_AGGREGATION_GATEWAY_URL to the mock server's URL
os.Setenv(c.EnvPromAggregationGatewayUrl, mockPromAggregationGateway.URL)
defer os.Unsetenv(c.EnvPromAggregationGatewayUrl)
t.Setenv(c.EnvPromAggregationGatewayUrl, mockPromAggregationGateway.URL)

// Convert MockApplications to real ones
var appData []*a.Application
Expand Down Expand Up @@ -201,12 +200,14 @@ func TestDoCheck(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {

// Set up environment variables and command line arguments
os.Setenv(c.EnvYamlPath, tt.envYamlPath)
os.Setenv(c.EnvSlackWebhookUrl, tt.envSlackUrl)
os.Setenv(c.EnvClusterInfo, tt.envClusterInfo)
os.Setenv(c.EnvPromAggregationGatewayUrl, mockPushgateway.URL)
t.Setenv(c.EnvYamlPath, tt.envYamlPath)
t.Setenv(c.EnvSlackWebhookUrl, tt.envSlackUrl)
t.Setenv(c.EnvClusterInfo, tt.envClusterInfo)
t.Setenv(c.EnvPromAggregationGatewayUrl, mockPushgateway.URL)
// Set environment variable to true for this test
os.Setenv(c.EnvSkipWhitelistCheck, "true")
t.Setenv(c.EnvSkipWhitelistCheck, "true")
originalArgs := os.Args
defer func() { os.Args = originalArgs }()
os.Args = tt.cmdArgs

// Call function under test
Expand All @@ -218,13 +219,6 @@ func TestDoCheck(t *testing.T) {
} else {
assert.NoError(t, err, "Expected no error but got one")
}

// Unset environment variables
os.Unsetenv(c.EnvYamlPath)
os.Unsetenv(c.EnvSlackWebhookUrl)
os.Unsetenv(c.EnvClusterInfo)
os.Unsetenv(c.EnvPromAggregationGatewayUrl)
os.Unsetenv(c.EnvSkipWhitelistCheck)
})
}
}
4 changes: 1 addition & 3 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
a "github.com/NYULibraries/aswa/pkg/application"
"github.com/stretchr/testify/assert"
"net/http"
"os"
"testing"
"time"
)
Expand Down Expand Up @@ -39,10 +38,9 @@ func TestNewConfig(t *testing.T) {
func testNewConfigFunc(path string, expectedErr string) func(*testing.T) {
return func(t *testing.T) {
// Set environment variable to true for this test
os.Setenv(EnvSkipWhitelistCheck, "true")
t.Setenv(EnvSkipWhitelistCheck, "true")
_, err := NewConfig(path)

os.Unsetenv(EnvSkipWhitelistCheck)
if expectedErr == "" {
assert.Nil(t, err)
} else {
Expand Down
18 changes: 7 additions & 11 deletions pkg/config/env_vars_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,12 @@ func TestGetYamlPath(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

os.Setenv(EnvYamlPath, tt.envYamlPath)
t.Setenv(EnvYamlPath, tt.envYamlPath)

got := GetYamlPath()

assert.Equal(t, tt.want, got, "getYamlPath() should return correct yaml path")

os.Unsetenv(EnvYamlPath)
})
}
}
Expand All @@ -61,6 +60,8 @@ func TestGetCmdArg(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

originalArgs := os.Args
defer func() { os.Args = originalArgs }()
os.Args = tt.osArgs

got := GetCmdArg()
Expand All @@ -83,13 +84,12 @@ func TestGetSlackWebhookUrl(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

os.Setenv(EnvSlackWebhookUrl, tt.envSlackWebhookUrl)
t.Setenv(EnvSlackWebhookUrl, tt.envSlackWebhookUrl)

gotLogMessage := CaptureOutput(GetSlackWebhookUrl)

assert.Equal(t, tt.wantLogmessage, gotLogMessage, "getSlackWebhookUrl() should return correct Slack webhook URL")

os.Unsetenv(EnvSlackWebhookUrl)
})
}
}
Expand All @@ -108,16 +108,14 @@ func TestGetClusterInfo(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {

// Set up environment variable
os.Setenv(EnvClusterInfo, tt.envClusterInfo)
t.Setenv(EnvClusterInfo, tt.envClusterInfo)

// Call function under test
gotLogMessage := CaptureOutput(GetClusterInfo)

// Assert that the function returns the expected result
assert.Equal(t, tt.wantLogMessage, gotLogMessage, "getClusterInfo() should return correct cluster info")

// Unset environment variable for next test
os.Unsetenv(EnvClusterInfo)
})
}
}
Expand All @@ -135,13 +133,12 @@ func TestGetPromAggregationGatewayUrl(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

os.Setenv(EnvPromAggregationGatewayUrl, tt.envPromAggregationGatewayUrl)
t.Setenv(EnvPromAggregationGatewayUrl, tt.envPromAggregationGatewayUrl)

got := GetPromAggregationgatewayUrl()

assert.Equal(t, tt.want, got, "getPushgatewayUrl() should return correct pushgateway URL")

os.Unsetenv(EnvPromAggregationGatewayUrl)
})
}

Expand All @@ -160,13 +157,12 @@ func TestGetEnvironmentName(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

os.Setenv(EnvName, tt.envName)
t.Setenv(EnvName, tt.envName)

got := GetEnvironmentName()

assert.Equal(t, tt.want, got, "GetEnvironmentName() should return correct environment name")

os.Unsetenv(EnvName)
})
}
}
4 changes: 1 addition & 3 deletions pkg/metrics/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
c "github.com/NYULibraries/aswa/pkg/config"
"net/http"
"net/http/httptest"
"os"
"testing"
)

Expand Down Expand Up @@ -37,8 +36,7 @@ func TestPushMetrics(t *testing.T) {
defer server.Close()

// Setup environment variable to mock the pushgateway URL
os.Setenv(c.EnvPromAggregationGatewayUrl, server.URL)
defer os.Unsetenv(c.EnvPromAggregationGatewayUrl)
t.Setenv(c.EnvPromAggregationGatewayUrl, server.URL)

// Increment a test counter to simulate metrics that would be pushed
IncrementFailedTestsCounter("testApp")
Expand Down