mirror of
https://github.com/prymitive/karma
synced 2026-05-07 03:26:52 +00:00
Generate mock data for all Alertmanager releases that we can support (0.4 - 0.6) and switch tests to use them instead of old files Fixes #67
50 lines
1.2 KiB
Go
50 lines
1.2 KiB
Go
package mock
|
|
|
|
import (
|
|
"fmt"
|
|
"io/ioutil"
|
|
"path/filepath"
|
|
"runtime"
|
|
|
|
httpmock "gopkg.in/jarcoal/httpmock.v1"
|
|
)
|
|
|
|
// GetAbsoluteMockPath returns absolute path for given mock file
|
|
func GetAbsoluteMockPath(filename string, version string) string {
|
|
_, f, _, _ := runtime.Caller(0)
|
|
cwd := filepath.Dir(f)
|
|
return fmt.Sprintf("%s/%s/api/v1/%s", cwd, version, filename)
|
|
}
|
|
|
|
// RegisterURL for given url and return 200 status register mock http responder
|
|
func RegisterURL(url string, version string, filename string) {
|
|
fullPath := GetAbsoluteMockPath(filename, version)
|
|
mockJSON, err := ioutil.ReadFile(fullPath)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
if len(mockJSON) == 0 {
|
|
panic(fmt.Errorf("Empty mock file '%s'", fullPath))
|
|
}
|
|
httpmock.RegisterResponder("GET", url, httpmock.NewBytesResponder(200, mockJSON))
|
|
}
|
|
|
|
// ListAllMocks will return a list of all mock versions we have files for
|
|
func ListAllMocks() []string {
|
|
_, f, _, _ := runtime.Caller(0)
|
|
cwd := filepath.Dir(f)
|
|
|
|
dirents, err := ioutil.ReadDir(cwd)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
dirs := []string{}
|
|
for _, dirent := range dirents {
|
|
if dirent.IsDir() {
|
|
dirs = append(dirs, dirent.Name())
|
|
}
|
|
}
|
|
return dirs
|
|
}
|