From 702c7cdf7a37f3a87fdd889e1adaee7c34704e02 Mon Sep 17 00:00:00 2001 From: Jason Kulatunga Date: Fri, 29 Apr 2022 16:39:14 -0700 Subject: [PATCH] if running test iin github actions, use influxdb service for testing. --- .github/workflows/build.yaml | 3 +- webapp/backend/pkg/web/server_test.go | 69 ++++++++++++++++++++++----- 2 files changed, 59 insertions(+), 13 deletions(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 064b177..916cb67 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -11,7 +11,7 @@ jobs: # Service containers to run with `build` (Required for end-to-end testing) services: influxdb: - image: influxdb:2.0 + image: influxdb:2.2 env: DOCKER_INFLUXDB_INIT_MODE: setup DOCKER_INFLUXDB_INIT_USERNAME: admin @@ -24,7 +24,6 @@ jobs: env: PROJECT_PATH: /go/src/github.com/analogj/scrutiny CGO_ENABLED: 1 - SCRUTINY_WEB_INFLUXDB_HOST: influxdb steps: - name: Git run: | diff --git a/webapp/backend/pkg/web/server_test.go b/webapp/backend/pkg/web/server_test.go index 0a247e7..b7816d8 100644 --- a/webapp/backend/pkg/web/server_test.go +++ b/webapp/backend/pkg/web/server_test.go @@ -70,13 +70,19 @@ func TestHealthRoute(t *testing.T) { fakeConfig := mock_config.NewMockInterface(mockCtrl) fakeConfig.EXPECT().GetString("web.database.location").Return(path.Join(parentPath, "scrutiny_test.db")).AnyTimes() fakeConfig.EXPECT().GetString("web.src.frontend.path").Return(parentPath).AnyTimes() - fakeConfig.EXPECT().GetString("web.influxdb.host").Return("localhost").AnyTimes() + fakeConfig.EXPECT().GetString("web.influxdb.port").Return("8086").AnyTimes() fakeConfig.EXPECT().IsSet("web.influxdb.token").Return(true).AnyTimes() fakeConfig.EXPECT().GetString("web.influxdb.token").Return("my-super-secret-auth-token").AnyTimes() fakeConfig.EXPECT().GetString("web.influxdb.org").Return("scrutiny").AnyTimes() fakeConfig.EXPECT().GetString("web.influxdb.bucket").Return("metrics").AnyTimes() fakeConfig.EXPECT().GetBool("web.influxdb.retention_policy").Return(false).AnyTimes() + if _, isGithubActions := os.LookupEnv("GITHUB_ACTIONS"); isGithubActions { + // when running test suite in github actions, we run an influxdb service as a sidecar. + fakeConfig.EXPECT().GetString("web.influxdb.host").Return("influxdb").AnyTimes() + } else { + fakeConfig.EXPECT().GetString("web.influxdb.host").Return("localhost").AnyTimes() + } ae := web.AppEngine{ Config: fakeConfig, @@ -103,13 +109,18 @@ func TestRegisterDevicesRoute(t *testing.T) { fakeConfig := mock_config.NewMockInterface(mockCtrl) fakeConfig.EXPECT().GetString("web.database.location").Return(path.Join(parentPath, "scrutiny_test.db")).AnyTimes() fakeConfig.EXPECT().GetString("web.src.frontend.path").Return(parentPath).AnyTimes() - fakeConfig.EXPECT().GetString("web.influxdb.host").Return("localhost").AnyTimes() fakeConfig.EXPECT().GetString("web.influxdb.port").Return("8086").AnyTimes() fakeConfig.EXPECT().IsSet("web.influxdb.token").Return(true).AnyTimes() fakeConfig.EXPECT().GetString("web.influxdb.token").Return("my-super-secret-auth-token").AnyTimes() fakeConfig.EXPECT().GetString("web.influxdb.org").Return("scrutiny").AnyTimes() fakeConfig.EXPECT().GetString("web.influxdb.bucket").Return("metrics").AnyTimes() fakeConfig.EXPECT().GetBool("web.influxdb.retention_policy").Return(false).AnyTimes() + if _, isGithubActions := os.LookupEnv("GITHUB_ACTIONS"); isGithubActions { + // when running test suite in github actions, we run an influxdb service as a sidecar. + fakeConfig.EXPECT().GetString("web.influxdb.host").Return("influxdb").AnyTimes() + } else { + fakeConfig.EXPECT().GetString("web.influxdb.host").Return("localhost").AnyTimes() + } ae := web.AppEngine{ Config: fakeConfig, @@ -136,13 +147,18 @@ func TestUploadDeviceMetricsRoute(t *testing.T) { fakeConfig := mock_config.NewMockInterface(mockCtrl) fakeConfig.EXPECT().GetString("web.database.location").AnyTimes().Return(path.Join(parentPath, "scrutiny_test.db")) fakeConfig.EXPECT().GetString("web.src.frontend.path").AnyTimes().Return(parentPath) - fakeConfig.EXPECT().GetString("web.influxdb.host").Return("localhost").AnyTimes() fakeConfig.EXPECT().GetString("web.influxdb.port").Return("8086").AnyTimes() fakeConfig.EXPECT().IsSet("web.influxdb.token").Return(true).AnyTimes() fakeConfig.EXPECT().GetString("web.influxdb.token").Return("my-super-secret-auth-token").AnyTimes() fakeConfig.EXPECT().GetString("web.influxdb.org").Return("scrutiny").AnyTimes() fakeConfig.EXPECT().GetString("web.influxdb.bucket").Return("metrics").AnyTimes() fakeConfig.EXPECT().GetBool("web.influxdb.retention_policy").Return(false).AnyTimes() + if _, isGithubActions := os.LookupEnv("GITHUB_ACTIONS"); isGithubActions { + // when running test suite in github actions, we run an influxdb service as a sidecar. + fakeConfig.EXPECT().GetString("web.influxdb.host").Return("influxdb").AnyTimes() + } else { + fakeConfig.EXPECT().GetString("web.influxdb.host").Return("localhost").AnyTimes() + } ae := web.AppEngine{ Config: fakeConfig, @@ -178,13 +194,18 @@ func TestPopulateMultiple(t *testing.T) { fakeConfig.EXPECT().GetStringSlice("notify.urls").Return([]string{}).AnyTimes() fakeConfig.EXPECT().GetString("web.database.location").AnyTimes().Return(path.Join(parentPath, "scrutiny_test.db")) fakeConfig.EXPECT().GetString("web.src.frontend.path").AnyTimes().Return(parentPath) - fakeConfig.EXPECT().GetString("web.influxdb.host").Return("localhost").AnyTimes() fakeConfig.EXPECT().GetString("web.influxdb.port").Return("8086").AnyTimes() fakeConfig.EXPECT().IsSet("web.influxdb.token").Return(true).AnyTimes() fakeConfig.EXPECT().GetString("web.influxdb.token").Return("my-super-secret-auth-token").AnyTimes() fakeConfig.EXPECT().GetString("web.influxdb.org").Return("scrutiny").AnyTimes() fakeConfig.EXPECT().GetString("web.influxdb.bucket").Return("metrics").AnyTimes() fakeConfig.EXPECT().GetBool("web.influxdb.retention_policy").Return(false).AnyTimes() + if _, isGithubActions := os.LookupEnv("GITHUB_ACTIONS"); isGithubActions { + // when running test suite in github actions, we run an influxdb service as a sidecar. + fakeConfig.EXPECT().GetString("web.influxdb.host").Return("influxdb").AnyTimes() + } else { + fakeConfig.EXPECT().GetString("web.influxdb.host").Return("localhost").AnyTimes() + } ae := web.AppEngine{ Config: fakeConfig, @@ -267,7 +288,6 @@ func TestSendTestNotificationRoute_WebhookFailure(t *testing.T) { fakeConfig := mock_config.NewMockInterface(mockCtrl) fakeConfig.EXPECT().GetString("web.database.location").AnyTimes().Return(path.Join(parentPath, "scrutiny_test.db")) fakeConfig.EXPECT().GetString("web.src.frontend.path").AnyTimes().Return(parentPath) - fakeConfig.EXPECT().GetString("web.influxdb.host").Return("localhost").AnyTimes() fakeConfig.EXPECT().GetString("web.influxdb.port").Return("8086").AnyTimes() fakeConfig.EXPECT().IsSet("web.influxdb.token").Return(true).AnyTimes() fakeConfig.EXPECT().GetString("web.influxdb.token").Return("my-super-secret-auth-token").AnyTimes() @@ -275,6 +295,13 @@ func TestSendTestNotificationRoute_WebhookFailure(t *testing.T) { fakeConfig.EXPECT().GetString("web.influxdb.bucket").Return("metrics").AnyTimes() fakeConfig.EXPECT().GetBool("web.influxdb.retention_policy").Return(false).AnyTimes() fakeConfig.EXPECT().GetStringSlice("notify.urls").AnyTimes().Return([]string{"https://unroutable.domain.example.asdfghj"}) + if _, isGithubActions := os.LookupEnv("GITHUB_ACTIONS"); isGithubActions { + // when running test suite in github actions, we run an influxdb service as a sidecar. + fakeConfig.EXPECT().GetString("web.influxdb.host").Return("influxdb").AnyTimes() + } else { + fakeConfig.EXPECT().GetString("web.influxdb.host").Return("localhost").AnyTimes() + } + ae := web.AppEngine{ Config: fakeConfig, } @@ -298,7 +325,6 @@ func TestSendTestNotificationRoute_ScriptFailure(t *testing.T) { fakeConfig := mock_config.NewMockInterface(mockCtrl) fakeConfig.EXPECT().GetString("web.database.location").AnyTimes().Return(path.Join(parentPath, "scrutiny_test.db")) fakeConfig.EXPECT().GetString("web.src.frontend.path").AnyTimes().Return(parentPath) - fakeConfig.EXPECT().GetString("web.influxdb.host").Return("localhost").AnyTimes() fakeConfig.EXPECT().GetString("web.influxdb.port").Return("8086").AnyTimes() fakeConfig.EXPECT().IsSet("web.influxdb.token").Return(true).AnyTimes() fakeConfig.EXPECT().GetString("web.influxdb.token").Return("my-super-secret-auth-token").AnyTimes() @@ -306,6 +332,13 @@ func TestSendTestNotificationRoute_ScriptFailure(t *testing.T) { fakeConfig.EXPECT().GetString("web.influxdb.bucket").Return("metrics").AnyTimes() fakeConfig.EXPECT().GetBool("web.influxdb.retention_policy").Return(false).AnyTimes() fakeConfig.EXPECT().GetStringSlice("notify.urls").AnyTimes().Return([]string{"script:///missing/path/on/disk"}) + if _, isGithubActions := os.LookupEnv("GITHUB_ACTIONS"); isGithubActions { + // when running test suite in github actions, we run an influxdb service as a sidecar. + fakeConfig.EXPECT().GetString("web.influxdb.host").Return("influxdb").AnyTimes() + } else { + fakeConfig.EXPECT().GetString("web.influxdb.host").Return("localhost").AnyTimes() + } + ae := web.AppEngine{ Config: fakeConfig, } @@ -329,15 +362,20 @@ func TestSendTestNotificationRoute_ScriptSuccess(t *testing.T) { fakeConfig := mock_config.NewMockInterface(mockCtrl) fakeConfig.EXPECT().GetString("web.database.location").AnyTimes().Return(path.Join(parentPath, "scrutiny_test.db")) fakeConfig.EXPECT().GetString("web.src.frontend.path").AnyTimes().Return(parentPath) - fakeConfig.EXPECT().GetString("web.influxdb.host").Return("localhost").AnyTimes() fakeConfig.EXPECT().GetString("web.influxdb.port").Return("8086").AnyTimes() fakeConfig.EXPECT().IsSet("web.influxdb.token").Return(true).AnyTimes() fakeConfig.EXPECT().GetString("web.influxdb.token").Return("my-super-secret-auth-token").AnyTimes() fakeConfig.EXPECT().GetString("web.influxdb.org").Return("scrutiny").AnyTimes() fakeConfig.EXPECT().GetString("web.influxdb.bucket").Return("metrics").AnyTimes() fakeConfig.EXPECT().GetBool("web.influxdb.retention_policy").Return(false).AnyTimes() - fakeConfig.EXPECT().GetStringSlice("notify.urls").AnyTimes().Return([]string{"script:///usr/bin/env"}) + if _, isGithubActions := os.LookupEnv("GITHUB_ACTIONS"); isGithubActions { + // when running test suite in github actions, we run an influxdb service as a sidecar. + fakeConfig.EXPECT().GetString("web.influxdb.host").Return("influxdb").AnyTimes() + } else { + fakeConfig.EXPECT().GetString("web.influxdb.host").Return("localhost").AnyTimes() + } + ae := web.AppEngine{ Config: fakeConfig, } @@ -361,15 +399,19 @@ func TestSendTestNotificationRoute_ShoutrrrFailure(t *testing.T) { fakeConfig := mock_config.NewMockInterface(mockCtrl) fakeConfig.EXPECT().GetString("web.database.location").AnyTimes().Return(path.Join(parentPath, "scrutiny_test.db")) fakeConfig.EXPECT().GetString("web.src.frontend.path").AnyTimes().Return(parentPath) - fakeConfig.EXPECT().GetString("web.influxdb.host").Return("localhost").AnyTimes() fakeConfig.EXPECT().GetString("web.influxdb.port").Return("8086").AnyTimes() fakeConfig.EXPECT().IsSet("web.influxdb.token").Return(true).AnyTimes() fakeConfig.EXPECT().GetString("web.influxdb.token").Return("my-super-secret-auth-token").AnyTimes() fakeConfig.EXPECT().GetString("web.influxdb.org").Return("scrutiny").AnyTimes() fakeConfig.EXPECT().GetString("web.influxdb.bucket").Return("metrics").AnyTimes() fakeConfig.EXPECT().GetBool("web.influxdb.retention_policy").Return(false).AnyTimes() - fakeConfig.EXPECT().GetStringSlice("notify.urls").AnyTimes().Return([]string{"discord://invalidtoken@channel"}) + if _, isGithubActions := os.LookupEnv("GITHUB_ACTIONS"); isGithubActions { + // when running test suite in github actions, we run an influxdb service as a sidecar. + fakeConfig.EXPECT().GetString("web.influxdb.host").Return("influxdb").AnyTimes() + } else { + fakeConfig.EXPECT().GetString("web.influxdb.host").Return("localhost").AnyTimes() + } ae := web.AppEngine{ Config: fakeConfig, } @@ -393,7 +435,6 @@ func TestGetDevicesSummaryRoute_Nvme(t *testing.T) { fakeConfig := mock_config.NewMockInterface(mockCtrl) fakeConfig.EXPECT().GetString("web.database.location").AnyTimes().Return(path.Join(parentPath, "scrutiny_test.db")) fakeConfig.EXPECT().GetString("web.src.frontend.path").AnyTimes().Return(parentPath) - fakeConfig.EXPECT().GetString("web.influxdb.host").Return("localhost").AnyTimes() fakeConfig.EXPECT().GetString("web.influxdb.port").Return("8086").AnyTimes() fakeConfig.EXPECT().IsSet("web.influxdb.token").Return(true).AnyTimes() fakeConfig.EXPECT().GetString("web.influxdb.token").Return("my-super-secret-auth-token").AnyTimes() @@ -401,6 +442,12 @@ func TestGetDevicesSummaryRoute_Nvme(t *testing.T) { fakeConfig.EXPECT().GetString("web.influxdb.bucket").Return("metrics").AnyTimes() fakeConfig.EXPECT().GetBool("web.influxdb.retention_policy").Return(false).AnyTimes() fakeConfig.EXPECT().GetStringSlice("notify.urls").AnyTimes().Return([]string{}) + if _, isGithubActions := os.LookupEnv("GITHUB_ACTIONS"); isGithubActions { + // when running test suite in github actions, we run an influxdb service as a sidecar. + fakeConfig.EXPECT().GetString("web.influxdb.host").Return("influxdb").AnyTimes() + } else { + fakeConfig.EXPECT().GetString("web.influxdb.host").Return("localhost").AnyTimes() + } ae := web.AppEngine{ Config: fakeConfig,