mirror of
https://github.com/prometheus-operator/prometheus-operator.git
synced 2025-04-16 09:16:38 +00:00
139 lines
3.5 KiB
Go
139 lines
3.5 KiB
Go
// Copyright 2017 The prometheus-operator Authors
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package prometheus
|
|
|
|
import (
|
|
"reflect"
|
|
"testing"
|
|
|
|
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
|
|
"k8s.io/api/core/v1"
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"github.com/kylelemons/godebug/pretty"
|
|
)
|
|
|
|
func TestListOptions(t *testing.T) {
|
|
for i := 0; i < 1000; i++ {
|
|
o := ListOptions("test")
|
|
if o.LabelSelector != "app=prometheus,prometheus=test" && o.LabelSelector != "prometheus=test,app=prometheus" {
|
|
t.Fatalf("LabelSelector not computed correctly\n\nExpected: \"app=prometheus,prometheus=test\"\n\nGot: %#+v", o.LabelSelector)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestCreateStatefulSetInputHash(t *testing.T) {
|
|
p1 := monitoringv1.Prometheus{}
|
|
p1.Spec.Version = "v1.7.0"
|
|
p2 := monitoringv1.Prometheus{}
|
|
p2.Spec.Version = "v1.7.2"
|
|
c := Config{}
|
|
|
|
p1Hash, err := createSSetInputHash(p1, c, []string{}, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
p2Hash, err := createSSetInputHash(p2, c, []string{}, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if p1Hash == p2Hash {
|
|
t.Fatal("expected two different Prometheus CRDs to result in two different hash but got equal hash")
|
|
}
|
|
}
|
|
|
|
func TestGetNodeAddresses(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
nodes *v1.NodeList
|
|
expectedAddresses []string
|
|
expectedErrors int
|
|
}{
|
|
{
|
|
name: "simple",
|
|
nodes: &v1.NodeList{
|
|
Items: []v1.Node{
|
|
v1.Node{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Name: "node-0",
|
|
},
|
|
Status: v1.NodeStatus{
|
|
Addresses: []v1.NodeAddress{
|
|
v1.NodeAddress{
|
|
Address: "10.0.0.1",
|
|
Type: v1.NodeInternalIP,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
expectedAddresses: []string{"10.0.0.1"},
|
|
expectedErrors: 0,
|
|
},
|
|
{
|
|
// Replicates #1815
|
|
name: "missing ip on one node",
|
|
nodes: &v1.NodeList{
|
|
Items: []v1.Node{
|
|
v1.Node{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Name: "node-0",
|
|
},
|
|
Status: v1.NodeStatus{
|
|
Addresses: []v1.NodeAddress{
|
|
v1.NodeAddress{
|
|
Address: "node-0",
|
|
Type: v1.NodeHostName,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
v1.Node{
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Name: "node-1",
|
|
},
|
|
Status: v1.NodeStatus{
|
|
Addresses: []v1.NodeAddress{
|
|
v1.NodeAddress{
|
|
Address: "10.0.0.1",
|
|
Type: v1.NodeInternalIP,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
expectedAddresses: []string{"10.0.0.1"},
|
|
expectedErrors: 1,
|
|
},
|
|
}
|
|
for _, c := range cases {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
addrs, errs := getNodeAddresses(c.nodes)
|
|
if len(errs) != c.expectedErrors {
|
|
t.Errorf("Expected %d errors, got %d. Errors: %v", c.expectedErrors, len(errs), errs)
|
|
}
|
|
ips := make([]string, 0)
|
|
for _, addr := range addrs {
|
|
ips = append(ips, addr.IP)
|
|
}
|
|
if !reflect.DeepEqual(ips, c.expectedAddresses) {
|
|
t.Error(pretty.Compare(ips, c.expectedAddresses))
|
|
}
|
|
})
|
|
}
|
|
}
|