Skip to content

Commit

Permalink
Update test for /info endpoint and CLI command
Browse files Browse the repository at this point in the history
using redis service in GitHub actions, and it's only supported if I'm using Linux env.
So removing "macos-latest" from the GitHub Actions unit test yaml file.
  • Loading branch information
XD-DENG committed Aug 25, 2020
1 parent c8f75bf commit 7492e8b
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 3 deletions.
9 changes: 8 additions & 1 deletion .github/workflows/rediseen_unit_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,15 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, macOS-latest]
os: [ubuntu-latest]
goversion: [1.12, 1.13, 1.14, 1.15]

services:
redis:
image: redis:latest
ports:
- 6379/tcp

steps:
- name: Set up Go
uses: actions/setup-go@v1
Expand All @@ -38,6 +44,7 @@ jobs:
REDISEEN_REDIS_URI: redis://:@localhost:6400
REDISEEN_TEST_MODE: true
REDISEEN_DB_EXPOSED: 0-5
REAL_REDIS_URI: redis://:@localhost:${{ job.services.redis.ports['6379'] }}
run: go test -v -cover github.com/xd-deng/rediseen

- name: Build
Expand Down
5 changes: 4 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
language: go

services:
- redis-server

go:
- 1.12.x
- 1.13.x
- 1.14.x
- 1.15.x

env:
- GO111MODULE=on REDISEEN_REDIS_URI="redis://:@localhost:6400" REDISEEN_KEY_PATTERN_EXPOSED="^key:[.]*" REDISEEN_TEST_MODE=true REDISEEN_DB_EXPOSED=0-5
- GO111MODULE=on REDISEEN_REDIS_URI="redis://:@localhost:6400" REDISEEN_KEY_PATTERN_EXPOSED="^key:[.]*" REDISEEN_TEST_MODE=true REDISEEN_DB_EXPOSED=0-5 REAL_REDIS_URI="redis://:@localhost:6379"

before_install:
- go get -v -t -d ./...
Expand Down
2 changes: 1 addition & 1 deletion rediseen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func Test_Main(t *testing.T) {
main()

// commands "rediseen version", "rediseen help", "rediseen stop", "rediseen wrong_command"
for _, command := range []string{"version", "help", "stop", "wrong_command"} {
for _, command := range []string{"version", "help", "configdoc", "stop", "wrong_command"} {
os.Args = []string{"", command}
main()
}
Expand Down
100 changes: 100 additions & 0 deletions service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1541,3 +1541,103 @@ func Test_service_root(t *testing.T) {
t.Error("Expected partial content not found:\n", expectedPartialContent)
}
}

func Test_service_info(t *testing.T) {

originalRedisURI := os.Getenv("REDISEEN_REDIS_URI")
realRedis, realRedisFound := os.LookupEnv("REAL_REDIS_URI")
if !realRedisFound {
t.Skip("skipping test when there is no real Redis instance")
}
os.Setenv("REDISEEN_REDIS_URI", realRedis)
defer os.Setenv("REDISEEN_REDIS_URI", originalRedisURI)

var testService service
testService.loadConfigFromEnv()
s := httptest.NewServer(http.Handler(&testService))
defer s.Close()

res, _ := http.Get(s.URL + "/info")

expectedCode := 200
compareAndShout(t, expectedCode, res.StatusCode)
}

func Test_service_info_by_section(t *testing.T) {

originalRedisURI := os.Getenv("REDISEEN_REDIS_URI")
realRedis, realRedisFound := os.LookupEnv("REAL_REDIS_URI")
if !realRedisFound {
t.Skip("skipping test when there is no real Redis instance")
}
os.Setenv("REDISEEN_REDIS_URI", realRedis)
defer os.Setenv("REDISEEN_REDIS_URI", originalRedisURI)

var testService service
testService.loadConfigFromEnv()
s := httptest.NewServer(http.Handler(&testService))
defer s.Close()

res, _ := http.Get(s.URL + "/info/cpu")

expectedCode := 200
compareAndShout(t, expectedCode, res.StatusCode)
}

func Test_service_info_by_section_uppercase(t *testing.T) {

originalRedisURI := os.Getenv("REDISEEN_REDIS_URI")
realRedis, realRedisFound := os.LookupEnv("REAL_REDIS_URI")
if !realRedisFound {
t.Skip("skipping test when there is no real Redis instance")
}
os.Setenv("REDISEEN_REDIS_URI", realRedis)
defer os.Setenv("REDISEEN_REDIS_URI", originalRedisURI)

var testService service
testService.loadConfigFromEnv()
s := httptest.NewServer(http.Handler(&testService))
defer s.Close()

res, _ := http.Get(s.URL + "/info/CPU")

expectedCode := 200
compareAndShout(t, expectedCode, res.StatusCode)
}

func Test_service_info_invalid_section(t *testing.T) {

originalRedisURI := os.Getenv("REDISEEN_REDIS_URI")
realRedis, realRedisFound := os.LookupEnv("REAL_REDIS_URI")
if !realRedisFound {
t.Skip("skipping test when there is no real Redis instance")
}
os.Setenv("REDISEEN_REDIS_URI", realRedis)
defer os.Setenv("REDISEEN_REDIS_URI", originalRedisURI)

var testService service
testService.loadConfigFromEnv()
s := httptest.NewServer(http.Handler(&testService))
defer s.Close()

res, _ := http.Get(s.URL + "/info/abc")

expectedCode := 400
compareAndShout(t, expectedCode, res.StatusCode)

resultStr, _ := ioutil.ReadAll(res.Body)
res.Body.Close()

var result types.ErrorType
json.Unmarshal([]byte(resultStr), &result)

expectedError := "Exception while getting Redis Info"
if !strings.Contains(result.Error, expectedError) {
t.Error("Expecting to contain \n", expectedError, "\ngot\n", result.Error)
}

expectedError = "invalid section"
if !strings.Contains(result.Error, expectedError) {
t.Error("Expecting to contain \n", expectedError, "\ngot\n", result.Error)
}
}

0 comments on commit 7492e8b

Please sign in to comment.