Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TechDebt] Adding support to spin up multiple grafana versions #203

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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/[email protected]
- name: Test
if: "${{ matrix.grafana != '10.1.4' }}"
run: go test -v ./...
env:
GDG_GRAFANA_VERSION: "${{ matrix.grafana }}-ubuntu"

76 changes: 48 additions & 28 deletions test/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand All @@ -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("")
Expand All @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion test/dashboard_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading