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

test: remove global ValidatePortInUse that caused test to fail #1889

Merged
merged 1 commit into from
Apr 4, 2023
Merged
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
2 changes: 1 addition & 1 deletion cmd/harvest/harvest.go
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ func getPollerPrometheusPort(pollerName string, opts *options) int {
return opts.promPort
}

if promPort, err = conf.GetPrometheusExporterPorts(pollerName); err != nil {
if promPort, err = conf.GetPrometheusExporterPorts(pollerName, false); err != nil {
fmt.Println(err)
return 0
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/tools/generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,9 @@ func generateDocker(path string, kind int) {
if err != nil {
panic(err)
}
conf.ValidatePortInUse = true
var filesd []string
for _, v := range conf.Config.PollersOrdered {
port, _ := conf.GetPrometheusExporterPorts(v)
port, _ := conf.GetPrometheusExporterPorts(v, true)
pollerInfo := PollerInfo{
ServiceName: normalizeContainerNames(v),
PollerName: v,
Expand Down
16 changes: 8 additions & 8 deletions pkg/conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (

var Config = HarvestConfig{}
var configRead = false
var ValidatePortInUse = false

const (
DefaultAPIVersion = "1.3"
Expand All @@ -32,6 +31,7 @@ const (
// TestLoadHarvestConfig is used by testing code to reload a new config
func TestLoadHarvestConfig(configPath string) {
configRead = false
promPortRangeMapping = make(map[string]PortMap)
err := LoadHarvestConfig(configPath)
if err != nil {
log.Fatalf("Failed to load config at=[%s] err=%+v\n", configPath, err)
Expand Down Expand Up @@ -179,12 +179,12 @@ func GetHarvestLogPath() string {
}

// GetPrometheusExporterPorts returns the Prometheus port for the given poller
func GetPrometheusExporterPorts(pollerName string) (int, error) {
func GetPrometheusExporterPorts(pollerName string, validatePortInUse bool) (int, error) {
var port int
var isPrometheusExporterConfigured bool

if len(promPortRangeMapping) == 0 {
loadPrometheusExporterPortRangeMapping()
loadPrometheusExporterPortRangeMapping(validatePortInUse)
}
poller := Config.Pollers[pollerName]
if poller == nil {
Expand Down Expand Up @@ -229,31 +229,31 @@ type PortMap struct {
freePorts map[int]struct{}
}

func PortMapFromRange(address string, portRange *IntRange) PortMap {
func PortMapFromRange(address string, portRange *IntRange, validatePortInUse bool) PortMap {
portMap := PortMap{}
portMap.freePorts = make(map[int]struct{})
start := portRange.Min
end := portRange.Max
for i := start; i <= end; i++ {
portMap.portSet = append(portMap.portSet, i)
if ValidatePortInUse {
if validatePortInUse {
portMap.freePorts[i] = struct{}{}
}
}
if !ValidatePortInUse {
if !validatePortInUse {
portMap.freePorts = util.CheckFreePorts(address, portMap.portSet)
}
return portMap
}

var promPortRangeMapping = make(map[string]PortMap)

func loadPrometheusExporterPortRangeMapping() {
func loadPrometheusExporterPortRangeMapping(validatePortInUse bool) {
for k, v := range Config.Exporters {
if v.Type == "Prometheus" {
if v.PortRange != nil {
// we only care about free ports on the localhost
promPortRangeMapping[k] = PortMapFromRange("localhost", v.PortRange)
promPortRangeMapping[k] = PortMapFromRange("localhost", v.PortRange, validatePortInUse)
}
}
}
Expand Down
8 changes: 3 additions & 5 deletions pkg/conf/conf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ var testYml = "../../cmd/tools/doctor/testdata/testConfig.yml"

func TestGetPrometheusExporterPorts(t *testing.T) {
TestLoadHarvestConfig(testYml)
// Test without checking
ValidatePortInUse = true
type args struct {
pollerNames []string
}
Expand All @@ -29,7 +27,7 @@ func TestGetPrometheusExporterPorts(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
for i, v := range tt.args.pollerNames {
got, err := GetPrometheusExporterPorts(v)
got, err := GetPrometheusExporterPorts(v, true)
if (err != nil) != tt.wantErr[i] {
t.Errorf("GetPrometheusExporterPorts() error = %v, wantErr %v", err, tt.wantErr)
return
Expand All @@ -44,8 +42,8 @@ func TestGetPrometheusExporterPorts(t *testing.T) {

func TestGetPrometheusExporterPortsIssue284(t *testing.T) {
TestLoadHarvestConfig("../../cmd/tools/doctor/testdata/issue-284.yml")
loadPrometheusExporterPortRangeMapping()
got, _ := GetPrometheusExporterPorts("issue-284")
loadPrometheusExporterPortRangeMapping(false)
got, _ := GetPrometheusExporterPorts("issue-284", false)
if got != 0 {
t.Fatalf("expected port to be 0 but was %d", got)
}
Expand Down