Skip to content

Commit

Permalink
bugfix loading config & test code (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmnote authored Apr 5, 2023
1 parent 0d1474d commit 14cea77
Show file tree
Hide file tree
Showing 15 changed files with 117 additions and 102 deletions.
18 changes: 2 additions & 16 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ jobs:
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
args: --timeout 3m

args: --timeout 5m

go-test-failfast:
runs-on: ubuntu-latest
Expand All @@ -65,22 +64,9 @@ jobs:
- uses: actions/setup-go@v4
- uses: actions/checkout@v3
- name: go test with coverage
#run: go test -v -covermode=count -coverprofile=coverage.out
run: go test ./... -race -coverprofile=coverage.out -covermode=atomic
run: go test ./... -v -race -coverprofile=coverage.out
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
#- name: Convert coverage to lcov
# uses: jandelgado/gcov2lcov-action@v1
#- name: Coveralls
# uses: coverallsapp/github-action@master
# with:
# github-token: ${{ secrets.GITHUB_TOKEN }}
# path-to-lcov: coverage.lcov
#- name: Coverage Gate
# uses: VeryGoodOpenSource/very_good_coverage@v2
# with:
# path: coverage.lcov
# min_coverage: 55

go-licenses:
runs-on: ubuntu-latest
Expand Down
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ pre-checks:
go install honnef.co/go/tools/cmd/staticcheck@latest
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest

checks: fmt vet staticcheck golangci-lint test-cover
checks:
./scripts/checks.sh

fmt:
go fmt ./...
Expand All @@ -54,7 +55,7 @@ staticcheck:
staticcheck ./...

golangci-lint:
golangci-lint run
golangci-lint run --timeout 5m

test-cover:
./scripts/test-cover.sh
Expand Down
1 change: 0 additions & 1 deletion etc/.gitignore

This file was deleted.

4 changes: 0 additions & 4 deletions etc/datasources.example.yaml → etc/datasources.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
# queryTimeOut: 30
datasources:
- name: Prometheus
type: prometheus
url: http://prometheus:9090
# basicAuth: true
# basicAuthUser: Aladdin
# basicAuthPassword: openSesame
- name: Lethe
type: lethe
url: http://lethe:3100
30 changes: 3 additions & 27 deletions pkg/configuration/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

type Config struct {
Version string
UserConfig *UsersConfig
UserConfig UsersConfig
DatasourcesConfig *DatasourcesConfig
//Dashboards []Dashboard
//AlertRuleGroups []AlertRuleGroup
Expand Down Expand Up @@ -73,8 +73,8 @@ func Load(version string) (*Config, error) {
}
defer userConfigFile.Close()

var userConf *UsersConfig
err = loadConfig(userConfigFile, userConf)
var userConf UsersConfig
err = loadConfig(userConfigFile, &userConf)
if err != nil {
return nil, fmt.Errorf("error on loading User Config: %w", err)
}
Expand All @@ -91,30 +91,6 @@ func Load(version string) (*Config, error) {
return nil, fmt.Errorf("error on loading Datasources Config: %w", err)
}

/*
dashboardfilepaths := glob("etc/dashboards", func(path string) bool {
return !strings.Contains(path, "/..") && filepath.Ext(path) == ".yaml"
})
for _, path := range dashboardfilepaths {
f, err := os.Open(path)
if err != nil {
return nil, err
}
var dashBoard *Dashboard
err = loadConfig(f, dashBoard)
if err != nil {
return nil, err
}
}
*/

//loadDashboards()
//loadAlertRuleGroups()

//datasourceStore = pkg.NewDatasourceStore(Config.DatasourcesConfig)

return &Config{
Version: version,
UserConfig: userConf,
Expand Down
21 changes: 20 additions & 1 deletion pkg/configuration/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,30 @@ package configuration

import (
"bytes"
"github.com/stretchr/testify/assert"
"io"
"os"
"testing"

"github.com/stretchr/testify/assert"
)

func init() {
_ = os.Chdir("../..")
}

func TestLoad(t *testing.T) {
cfg, err := Load("Unknown")
assert.Nil(t, err)
assert.Equal(t, cfg.Version, "Unknown")
assert.Equal(t, cfg.UserConfig, UsersConfig{EtcUsers: []EtcUser{
{Username: "admin", Hash: "$2a$12$VcCDgh2NDk07JGN0rjGbM.Ad41qVR/YFJcgHp0UGns5JDymv..TOG", IsAdmin: true},
}})
assert.ElementsMatch(t, cfg.DatasourcesConfig.Datasources, []*Datasource{
{Type: DatasourceTypePrometheus, Name: "Prometheus", URL: "http://prometheus:9090", BasicAuth: false, BasicAuthUser: "", BasicAuthPassword: "", IsDefault: false, IsDiscovered: false},
{Type: DatasourceTypeLethe, Name: "Lethe", URL: "http://lethe:3100", BasicAuth: false, BasicAuthUser: "", BasicAuthPassword: "", IsDefault: false, IsDiscovered: false},
})
}

func TestLoadDatasourcesConfig(t *testing.T) {

tests := map[string]struct {
Expand Down
4 changes: 2 additions & 2 deletions pkg/handler/alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ func NewAlertHandler(r *store.AlertRuleStore) *alertHandler {
return &alertHandler{r}
}

func (ah *alertHandler) AlertRuleGroups(c *gin.Context) {
c.JSON(200, ah.AlertRuleStore.Groups())
func (ah *alertHandler) AlertRuleGroupsList(c *gin.Context) {
c.JSON(200, ah.AlertRuleStore.RuleGroupsList())
}
5 changes: 3 additions & 2 deletions pkg/handler/dashboard.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package handler

import (
"net/http"

"github.com/gin-gonic/gin"
"github.com/kuoss/venti/pkg/store"
"net/http"
)

// todo moved from config handler *should* modify web router path
Expand All @@ -18,7 +19,7 @@ func NewDashboardHandler(ds *store.DashboardStore) *dashboardHandler {
return &dashboardHandler{ds}
}

//GET /dashboards
// GET /dashboards
func (dh *dashboardHandler) Dashboards(c *gin.Context) {
c.JSON(http.StatusOK, dh.DashboardStore.Dashboards())
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package pkg

import (
"fmt"

"github.com/gin-gonic/gin"
"github.com/kuoss/venti/pkg/configuration"
"github.com/kuoss/venti/pkg/handler"
Expand Down Expand Up @@ -31,7 +32,7 @@ func LoadStores(config *configuration.Config) (*Stores, error) {
return nil, fmt.Errorf("load datasource configuration failed: %w", err)
}

userStore, err := store.NewUserService("./data/venti.sqlite3", config.UserConfig)
userStore, err := store.NewUserStore("./data/venti.sqlite3", config.UserConfig)
if err != nil {
return nil, fmt.Errorf("load user configuration failed: %w", err)
}
Expand Down Expand Up @@ -89,7 +90,7 @@ func LoadRouter(s *Stores, config *configuration.Config) *gin.Engine {

alertGroup := api.Group("/alerts")
{
alertGroup.GET("/", ah.AlertRuleGroups)
alertGroup.GET("/", ah.AlertRuleGroupsList)
}

letheGroup := api.Group("/lethe")
Expand Down
41 changes: 41 additions & 0 deletions pkg/store/alertRuleStore.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package store

import (
"fmt"
"log"
"os"
"path/filepath"

"github.com/prometheus/prometheus/model/rulefmt"
)

type AlertRuleStore struct {
ruleGroupsList []rulefmt.RuleGroups
}

func NewAlertRuleStore(pattern string) (*AlertRuleStore, error) {
log.Println("Loading alertRules...")
files, err := filepath.Glob("etc/alertrules/*.yaml")
if err != nil {
return nil, err
}
var ruleGroupsList []rulefmt.RuleGroups
for _, filename := range files {
log.Printf("alertRule file: %s\n", filename)
f, err := os.Open(filename)
if err != nil {
return nil, fmt.Errorf("error on Open: %w", err)
}
var ruleGroups *rulefmt.RuleGroups
err = loadYaml(f, &ruleGroups)
if err != nil {
return nil, fmt.Errorf("error on loadYaml: %w", err)
}
ruleGroupsList = append(ruleGroupsList, *ruleGroups)
}
return &AlertRuleStore{ruleGroupsList: ruleGroupsList}, nil
}

func (ars *AlertRuleStore) RuleGroupsList() []rulefmt.RuleGroups {
return ars.ruleGroupsList
}
26 changes: 26 additions & 0 deletions pkg/store/alertRuleStore_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package store

import (
"os"
"testing"

"github.com/prometheus/prometheus/model/rulefmt"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
)

func init() {
_ = os.Chdir("../..")
}

func TestNewAlertRuleStore(t *testing.T) {
ars, err := NewAlertRuleStore("")
assert.Nil(t, err)
assert.Equal(t, ars, &AlertRuleStore{ruleGroupsList: []rulefmt.RuleGroups{rulefmt.RuleGroups{Groups: []rulefmt.RuleGroup{rulefmt.RuleGroup{Name: "sample", Interval: 0, Limit: 0, Rules: []rulefmt.RuleNode{rulefmt.RuleNode{Record: yaml.Node{Kind: 0x0, Style: 0x0, Tag: "", Value: "", Anchor: "", Alias: (*yaml.Node)(nil), Content: []*yaml.Node(nil), HeadComment: "", LineComment: "", FootComment: "", Line: 0, Column: 0}, Alert: yaml.Node{Kind: 0x8, Style: 0x0, Tag: "!!str", Value: "S00-AlwaysOn", Anchor: "", Alias: (*yaml.Node)(nil), Content: []*yaml.Node(nil), HeadComment: "", LineComment: "", FootComment: "", Line: 7, Column: 12}, Expr: yaml.Node{Kind: 0x8, Style: 0x0, Tag: "!!str", Value: "vector(1234)", Anchor: "", Alias: (*yaml.Node)(nil), Content: []*yaml.Node(nil), HeadComment: "", LineComment: "", FootComment: "", Line: 8, Column: 11}, For: 0, Labels: map[string]string(nil), Annotations: map[string]string{"summary": "AlwaysOn value={{ $value }}"}}, rulefmt.RuleNode{Record: yaml.Node{Kind: 0x0, Style: 0x0, Tag: "", Value: "", Anchor: "", Alias: (*yaml.Node)(nil), Content: []*yaml.Node(nil), HeadComment: "", LineComment: "", FootComment: "", Line: 0, Column: 0}, Alert: yaml.Node{Kind: 0x8, Style: 0x0, Tag: "!!str", Value: "S01-Monday", Anchor: "", Alias: (*yaml.Node)(nil), Content: []*yaml.Node(nil), HeadComment: "", LineComment: "", FootComment: "", Line: 12, Column: 12}, Expr: yaml.Node{Kind: 0x8, Style: 0x0, Tag: "!!str", Value: "day_of_week() == 1 and hour() < 2", Anchor: "", Alias: (*yaml.Node)(nil), Content: []*yaml.Node(nil), HeadComment: "", LineComment: "", FootComment: "", Line: 13, Column: 11}, For: 0, Labels: map[string]string(nil), Annotations: map[string]string{"summary": "Monday"}}, rulefmt.RuleNode{Record: yaml.Node{Kind: 0x0, Style: 0x0, Tag: "", Value: "", Anchor: "", Alias: (*yaml.Node)(nil), Content: []*yaml.Node(nil), HeadComment: "", LineComment: "", FootComment: "", Line: 0, Column: 0}, Alert: yaml.Node{Kind: 0x8, Style: 0x0, Tag: "!!str", Value: "S02-NewNamespace", Anchor: "", Alias: (*yaml.Node)(nil), Content: []*yaml.Node(nil), HeadComment: "", LineComment: "", FootComment: "", Line: 17, Column: 12}, Expr: yaml.Node{Kind: 0x8, Style: 0x0, Tag: "!!str", Value: "time() - kube_namespace_created < 120", Anchor: "", Alias: (*yaml.Node)(nil), Content: []*yaml.Node(nil), HeadComment: "", LineComment: "", FootComment: "", Line: 18, Column: 11}, For: 0, Labels: map[string]string(nil), Annotations: map[string]string{"summary": "labels={{ $labels }} namespace={{ $labels.namespace }} value={{ $value }}"}}}}}}}})
}

func TestRuleGroupsList(t *testing.T) {
ars, err := NewAlertRuleStore("")
assert.Nil(t, err)
assert.Equal(t, ars.RuleGroupsList(), []rulefmt.RuleGroups{rulefmt.RuleGroups{Groups: []rulefmt.RuleGroup{rulefmt.RuleGroup{Name: "sample", Interval: 0, Limit: 0, Rules: []rulefmt.RuleNode{rulefmt.RuleNode{Record: yaml.Node{Kind: 0x0, Style: 0x0, Tag: "", Value: "", Anchor: "", Alias: (*yaml.Node)(nil), Content: []*yaml.Node(nil), HeadComment: "", LineComment: "", FootComment: "", Line: 0, Column: 0}, Alert: yaml.Node{Kind: 0x8, Style: 0x0, Tag: "!!str", Value: "S00-AlwaysOn", Anchor: "", Alias: (*yaml.Node)(nil), Content: []*yaml.Node(nil), HeadComment: "", LineComment: "", FootComment: "", Line: 7, Column: 12}, Expr: yaml.Node{Kind: 0x8, Style: 0x0, Tag: "!!str", Value: "vector(1234)", Anchor: "", Alias: (*yaml.Node)(nil), Content: []*yaml.Node(nil), HeadComment: "", LineComment: "", FootComment: "", Line: 8, Column: 11}, For: 0, Labels: map[string]string(nil), Annotations: map[string]string{"summary": "AlwaysOn value={{ $value }}"}}, rulefmt.RuleNode{Record: yaml.Node{Kind: 0x0, Style: 0x0, Tag: "", Value: "", Anchor: "", Alias: (*yaml.Node)(nil), Content: []*yaml.Node(nil), HeadComment: "", LineComment: "", FootComment: "", Line: 0, Column: 0}, Alert: yaml.Node{Kind: 0x8, Style: 0x0, Tag: "!!str", Value: "S01-Monday", Anchor: "", Alias: (*yaml.Node)(nil), Content: []*yaml.Node(nil), HeadComment: "", LineComment: "", FootComment: "", Line: 12, Column: 12}, Expr: yaml.Node{Kind: 0x8, Style: 0x0, Tag: "!!str", Value: "day_of_week() == 1 and hour() < 2", Anchor: "", Alias: (*yaml.Node)(nil), Content: []*yaml.Node(nil), HeadComment: "", LineComment: "", FootComment: "", Line: 13, Column: 11}, For: 0, Labels: map[string]string(nil), Annotations: map[string]string{"summary": "Monday"}}, rulefmt.RuleNode{Record: yaml.Node{Kind: 0x0, Style: 0x0, Tag: "", Value: "", Anchor: "", Alias: (*yaml.Node)(nil), Content: []*yaml.Node(nil), HeadComment: "", LineComment: "", FootComment: "", Line: 0, Column: 0}, Alert: yaml.Node{Kind: 0x8, Style: 0x0, Tag: "!!str", Value: "S02-NewNamespace", Anchor: "", Alias: (*yaml.Node)(nil), Content: []*yaml.Node(nil), HeadComment: "", LineComment: "", FootComment: "", Line: 17, Column: 12}, Expr: yaml.Node{Kind: 0x8, Style: 0x0, Tag: "!!str", Value: "time() - kube_namespace_created < 120", Anchor: "", Alias: (*yaml.Node)(nil), Content: []*yaml.Node(nil), HeadComment: "", LineComment: "", FootComment: "", Line: 18, Column: 11}, For: 0, Labels: map[string]string(nil), Annotations: map[string]string{"summary": "labels={{ $labels }} namespace={{ $labels.namespace }} value={{ $value }}"}}}}}}})
}
41 changes: 0 additions & 41 deletions pkg/store/alertrulestore.go

This file was deleted.

7 changes: 4 additions & 3 deletions pkg/store/userStore.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package store

import (
"fmt"
"log"

"github.com/kuoss/venti/pkg/auth"
"github.com/kuoss/venti/pkg/configuration"
"log"

"gorm.io/driver/sqlite"
"gorm.io/gorm"
Expand All @@ -17,7 +18,7 @@ type UserStore struct {
db *gorm.DB
}

func NewUserService(filepath string, config *configuration.UsersConfig) (*UserStore, error) {
func NewUserStore(filepath string, config configuration.UsersConfig) (*UserStore, error) {
log.Println("Initializing database...")

db, err := gorm.Open(sqlite.Open(filepath), &gorm.Config{})
Expand All @@ -33,7 +34,7 @@ func NewUserService(filepath string, config *configuration.UsersConfig) (*UserSt
return &UserStore{db}, nil
}

func setEtcUsers(db *gorm.DB, config *configuration.UsersConfig) {
func setEtcUsers(db *gorm.DB, config configuration.UsersConfig) {

for _, etcUser := range config.EtcUsers {
var user auth.User
Expand Down
9 changes: 9 additions & 0 deletions scripts/checks.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash
cd $(dirname $0)/..

set -xeuo pipefail
go fmt ./...
go vet ./...
staticcheck ./...
golangci-lint run --timeout 5m
./scripts/test-cover.sh
2 changes: 1 addition & 1 deletion scripts/test-cover.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
MIN_COVER=50.0

cd $(dirname $0)/..
go test ./... -failfast -coverprofile cover.out
go test ./... -failfast -race -coverprofile cover.out
if [[ $? != 0 ]]; then
echo "❌ FAIL ( test failed )"
exit 1
Expand Down

0 comments on commit 14cea77

Please sign in to comment.