From 7492e8b8e927ff15666867b7b8cd74d05860b2d6 Mon Sep 17 00:00:00 2001 From: XD-DENG Date: Tue, 25 Aug 2020 19:22:44 +0200 Subject: [PATCH] Update test for /info endpoint and CLI command 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. --- .github/workflows/rediseen_unit_test.yml | 9 +- .travis.yml | 5 +- rediseen_test.go | 2 +- service_test.go | 100 +++++++++++++++++++++++ 4 files changed, 113 insertions(+), 3 deletions(-) diff --git a/.github/workflows/rediseen_unit_test.yml b/.github/workflows/rediseen_unit_test.yml index b31d36a..fa7812b 100644 --- a/.github/workflows/rediseen_unit_test.yml +++ b/.github/workflows/rediseen_unit_test.yml @@ -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 @@ -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 diff --git a/.travis.yml b/.travis.yml index 021b237..fe7b871 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,8 @@ language: go +services: + - redis-server + go: - 1.12.x - 1.13.x @@ -7,7 +10,7 @@ go: - 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 ./... diff --git a/rediseen_test.go b/rediseen_test.go index 15fb4a0..73eb816 100644 --- a/rediseen_test.go +++ b/rediseen_test.go @@ -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() } diff --git a/service_test.go b/service_test.go index 0476f7c..c9d3893 100644 --- a/service_test.go +++ b/service_test.go @@ -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) + } +}