Last active
December 6, 2018 13:16
-
-
Save vharsh/d0545d66c932ae8b1a05009e1169f75f to your computer and use it in GitHub Desktop.
health test mock
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ( | |
"os" | |
"testing" | |
"time" | |
) | |
func TestGetPingPeriod(t *testing.T) { | |
beforeFunc := func(value string) { | |
if err := os.Setenv("OPENEBS_IO_ANALYTICS_PING_INTERVAL", value); err != nil { | |
t.Logf("Unable to set environment variable") | |
} | |
} | |
afterFunc := func() { | |
if err := os.Unsetenv("OPENEBS_IO_ANALYTICS_PING_INTERVAL"); err != nil { | |
t.Logf("Unable to unset environment variable") | |
} | |
} | |
testSuite := map[string]struct { | |
OpenEBSPingPeriodValue string | |
ExpectedPeriodValue time.Duration | |
}{ | |
"24 seconds": {"24s", 86400000000000}, | |
"24 minutes": {"24m", 86400000000000}, | |
"24 hours": {"24h", 86400000000000}, | |
"Negative 24 hours": {"-24h", 86400000000000}, | |
"Random string input": {"Apache", 86400000000000}, | |
"Two hours": {"2h", 7200000000000}, | |
"Three hundred hours": {"300h", 1080000000000000}, | |
"Fifty two seconds": {"52000000000ns", 86400000000000}, | |
"Empty env value": {"", 86400000000000}, | |
} | |
for testKey, testData := range testSuite { | |
beforeFunc(testData.OpenEBSPingPeriodValue) | |
evaluatedValue := getPingPeriod() | |
if evaluatedValue != testData.ExpectedPeriodValue { | |
t.Fatalf("Tests failed for %s, expected=%d, got=%d", testKey, testData.ExpectedPeriodValue, evaluatedValue) | |
} | |
afterFunc() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package usage | |
import ( | |
"testing" | |
"time" | |
) | |
// mockPingPeriod mocks getPingPeriod | |
func mockPingPeriod(envValue string) time.Duration { | |
duration, _ := time.ParseDuration(envValue) | |
if duration < minimumPingPeriod { | |
return time.Duration(defaultPingPeriod) | |
} else { | |
return time.Duration(duration) | |
} | |
} | |
func TestGetPingPeriod(t *testing.T) { | |
// {"24s", "-100h", "OpenEBS", "true", "100h", "24h", "24.3h", "300000h"} | |
testSuite := map[string]struct { | |
OpenEBSPingPeriodValue string | |
ExpectedPeriodValue time.Duration | |
}{ | |
"24 seconds": { | |
"24s", | |
86400000000000, | |
}, | |
"24 minutes": { | |
"24m", | |
86400000000000, | |
}, | |
"24 hours": { | |
"24h", | |
86400000000000, | |
}, | |
"Negative 24 hours": { | |
"-24h", | |
86400000000000, | |
}, | |
"Random string input": { | |
"Apache", | |
86400000000000, | |
}, | |
"Two hours": { | |
"2h", | |
7200000000000, | |
}, | |
"Three hundred hours": { | |
"300h", | |
1080000000000000, | |
}, | |
"Fifty two seconds": { | |
"52000000000ns", | |
86400000000000, | |
}, | |
} | |
for testKey, testData := range testSuite { | |
evaluatedValue := mockPingPeriod(testData.OpenEBSPingPeriodValue) | |
if evaluatedValue != testData.ExpectedPeriodValue { | |
t.Fatalf("Tests failed for %s, expected=%d, got=%d", testKey, testData.ExpectedPeriodValue, evaluatedValue) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment