From dc31f64752fa14a02f3ee6397e91734699cea5fc Mon Sep 17 00:00:00 2001 From: Samir Faci Date: Mon, 9 Oct 2023 09:22:48 -0400 Subject: [PATCH] [TechDebt] Adding support to spin up multiple grafana versions --- .github/workflows/go.yml | 9 +++- test/common_test.go | 76 +++++++++++++++++++----------- test/dashboard_integration_test.go | 2 +- 3 files changed, 57 insertions(+), 30 deletions(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 6dd57fa0..0a2a76a9 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -10,12 +10,15 @@ on: jobs: test: strategy: + max-parallel: 1 matrix: go: [ 1.21.0 ] - grafana: [ 8.5.22, 9.4.3, 10.1.4 ] +# grafana: [ 8.5.22, 9.4.3, 10.1.4 ] + grafana: [ 10.1.4 ] env: GRAFANA_INTEGRATION: 1 + GDG_GRAFANA_VERSION: ${{ matrix.grafana }} runs-on: ubuntu-latest steps: @@ -40,10 +43,14 @@ jobs: if: "${{ matrix.go == '1.21.0' && matrix.grafana == '10.1.4' }}" run: | go test -v -covermode=atomic -coverprofile=coverage.out ./... + env: + GDG_GRAFANA_VERSION: ${{ matrix.grafana }} - name: Convert coverage.out to coverage.lcov if: "${{ matrix.go == '1.21.0' && matrix.grafana == '10.1.4' }}" uses: jandelgado/gcov2lcov-action@v1.0.9 - name: Test if: "${{ matrix.grafana != '10.1.4' }}" run: go test -v ./... + env: + GDG_GRAFANA_VERSION: "${{ matrix.grafana }}-ubuntu" diff --git a/test/common_test.go b/test/common_test.go index e26cc974..78422be4 100644 --- a/test/common_test.go +++ b/test/common_test.go @@ -22,10 +22,21 @@ import ( var minioPortResource *dockertest.Resource var grafanaResource *dockertest.Resource +func getEnvVersion(envName, defaultValue string) string { + if val, ok := os.LookupEnv(envName); ok { + return val + } + + return defaultValue + +} + func setupMinioContainer(pool *dockertest.Pool, wg *sync.WaitGroup) { // pulls an image, creates a container based on it and runs it defer wg.Done() - resource, err := pool.Run("bitnami/minio", "latest", + version := getEnvVersion("GDG_MINIO_VERSION", "latest") + log.Infof("Starting minio container version: %s", version) + resource, err := pool.Run("bitnami/minio", version, []string{"MINIO_ROOT_USER=test", "MINIO_ROOT_PASSWORD=secretsss"}) if err != nil { log.Fatalf("Could not start resource: %s", err) @@ -37,6 +48,23 @@ func setupMinioContainer(pool *dockertest.Pool, wg *sync.WaitGroup) { } +func setupGrafanaContainer(pool *dockertest.Pool, wg *sync.WaitGroup) { + // pulls an image, creates a container based on it and runs it + defer wg.Done() + version := getEnvVersion("GDG_GRAFANA_VERSION", "10.0.0-ubuntu") + log.Infof("Starting grafana container version: %s", version) + resource, err := pool.Run("grafana/grafana", version, + []string{"GF_INSTALL_PLUGINS=grafana-googlesheets-datasource", "GF_AUTH_ANONYMOUS_ENABLED=true"}) + if err != nil { + log.Fatalf("Could not start resource: %s", err) + } + grafanaResource = resource + + validatePort(resource, 5*time.Second, []string{"3000"}, "Unable to connect to grafana container. Cannot run test") + + log.Info("Grafana container is up and running") +} + func validatePort(resource *dockertest.Resource, delay time.Duration, ports []string, errorMsg string) { time.Sleep(delay) for _, port := range ports { @@ -55,21 +83,6 @@ func validatePort(resource *dockertest.Resource, delay time.Duration, ports []st } -func setupGrafanaContainer(pool *dockertest.Pool, wg *sync.WaitGroup) { - // pulls an image, creates a container based on it and runs it - defer wg.Done() - resource, err := pool.Run("grafana/grafana", "10.0.0-ubuntu", - []string{"GF_INSTALL_PLUGINS=grafana-googlesheets-datasource", "GF_AUTH_ANONYMOUS_ENABLED=true"}) - if err != nil { - log.Fatalf("Could not start resource: %s", err) - } - grafanaResource = resource - - validatePort(resource, 5*time.Second, []string{"3000"}, "Unable to connect to grafana container. Cannot run test") - - log.Info("Grafana container is up and running") -} - func setupDockerTest() *dockertest.Pool { // uses a sensible default on windows (tcp/http) and linux/osx (socket) pool, err := dockertest.NewPool("") @@ -94,22 +107,29 @@ func TestMain(m *testing.M) { go setupMinioContainer(pool, wg) go setupGrafanaContainer(pool, wg) wg.Wait() + //Adding some more sleep to ensure everything is up and running + time.Sleep(time.Second * 5) log.Infof("Ending at: %s", time.Now().String()) - exitVal := m.Run() - - // You can't defer this because os.Exit doesn't care for defer - for _, resource := range []*dockertest.Resource{minioPortResource, grafanaResource} { - if resource == nil { - log.Warning("No resource set, skipping cleanup") - continue + defer func() { + if r := recover(); r != nil { + log.Info("Recovering from panic, cleaning up") } - if err := pool.Purge(resource); err != nil { - log.Fatalf("Could not purge resource: %s", err) - } else { - log.Info("Resource has been purged") + // You can't defer this because os.Exit doesn't care for defer + for _, resource := range []*dockertest.Resource{minioPortResource, grafanaResource} { + if resource == nil { + log.Warning("No resource set, skipping cleanup") + continue + } + if err := pool.Purge(resource); err != nil { + log.Fatalf("Could not purge resource: %s", err) + } else { + log.Info("Resource has been purged") + } } - } + }() + + exitVal := m.Run() os.Exit(exitVal) } diff --git a/test/dashboard_integration_test.go b/test/dashboard_integration_test.go index 83618d51..fefb45e3 100644 --- a/test/dashboard_integration_test.go +++ b/test/dashboard_integration_test.go @@ -33,7 +33,7 @@ func TestDashboardAPIKeyCRUD(t *testing.T) { apiClient.DeleteAllTokens() //Remove any old data newKey, err := apiClient.CreateAPIKey("testing", "admin", 0) - assert.Nil(t, err) + assert.Nil(t, err, err.Error()) wrapper := map[string]*config.GrafanaConfig{} _ = wrapper