Skip to content

Commit

Permalink
configobservation/etcd: add basic test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
alaypatel07 authored and Alay Patel committed Nov 27, 2019
1 parent 136fa33 commit 5951c08
Show file tree
Hide file tree
Showing 223 changed files with 23,179 additions and 348 deletions.
4 changes: 0 additions & 4 deletions go.sum
Expand Up @@ -245,10 +245,6 @@ github.com/openshift/client-go v0.0.0-20191022152013-2823239d2298 h1:wg5VfVIEtKI
github.com/openshift/client-go v0.0.0-20191022152013-2823239d2298/go.mod h1:6rzn+JTr7+WYS2E1TExP4gByoABxMznR6y2SnUIkmxk=
github.com/openshift/kubernetes-kube-storage-version-migrator v0.0.3-0.20191031181212-bdca3bf7d454 h1:iLsYWhstnDtzE2Syn3QJQrx4F7mpcpwhAPNbk0Zkpvo=
github.com/openshift/kubernetes-kube-storage-version-migrator v0.0.3-0.20191031181212-bdca3bf7d454/go.mod h1:ArHs8HJeXUzb2/WdnsiGXCVCWQwX5X0a1GNS6hM9FN0=
github.com/openshift/library-go v0.0.0-20191114153449-9563a4e8e5fb h1:/z8bjN9uZb39JFiBAG3tVgxgwWH9oLIboIssLH1blik=
github.com/openshift/library-go v0.0.0-20191114153449-9563a4e8e5fb/go.mod h1:NBttNjZpWwup/nthuLbPAPSYC8Qyo+BBK5bCtFoyYjo=
github.com/openshift/library-go v0.0.0-20191118102510-4e2c7112d252 h1:GY3oBSyQIkjpn4UPzs2Fz78zgXlJT6tZUvcVpec+frg=
github.com/openshift/library-go v0.0.0-20191118102510-4e2c7112d252/go.mod h1:NBttNjZpWwup/nthuLbPAPSYC8Qyo+BBK5bCtFoyYjo=
github.com/openshift/library-go v0.0.0-20191121124438-7c776f7cc17a h1:AUH7F2j7/piVDQ6Y7jGmIxTVur0H5pz4oGc50zMHrRk=
github.com/openshift/library-go v0.0.0-20191121124438-7c776f7cc17a/go.mod h1:NBttNjZpWwup/nthuLbPAPSYC8Qyo+BBK5bCtFoyYjo=
github.com/pborman/uuid v1.2.0 h1:J7Q5mO4ysT1dv8hyrUGHb9+ooztCXu1D8MY8DZYsu3g=
Expand Down
119 changes: 119 additions & 0 deletions pkg/operator/configobservation/etcd/observe_etcd_test.go
@@ -0,0 +1,119 @@
package etcd

import (
"github.com/openshift/cluster-kube-apiserver-operator/pkg/operator/configobservation"
"github.com/openshift/library-go/pkg/operator/events"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/client-go/kubernetes/fake"
corev1listers "k8s.io/client-go/listers/core/v1"
"k8s.io/client-go/tools/cache"
"reflect"
"testing"
)

const clusterFQDN = "foo.bar"

func fakeObjectReference(ep *v1.Endpoints) *v1.ObjectReference {
return &v1.ObjectReference{
Kind: ep.Kind,
Namespace: ep.Namespace,
Name: ep.Name,
UID: ep.UID,
APIVersion: ep.APIVersion,
ResourceVersion: ep.ResourceVersion,
}
}

func getWantObserverConfig(etcdURLs []string) (map[string]interface{}, error) {
wantObserverConfig := map[string]interface{}{}
storageConfigURLsPath := []string{"storageConfig", "urls"}
if err := unstructured.SetNestedStringSlice(wantObserverConfig, etcdURLs, storageConfigURLsPath...); err != nil {
return nil, err
}
return wantObserverConfig, nil
}

func getEndpoint(hostname, ip string) *v1.Endpoints {
return &v1.Endpoints{
ObjectMeta: metav1.ObjectMeta{
Name: "host-etcd",
Namespace: "openshift-etcd",
Annotations: map[string]string{
"alpha.installer.openshift.io/dns-suffix": clusterFQDN,
},
},
Subsets: []v1.EndpointSubset{
{
Addresses: []v1.EndpointAddress{
{
IP: ip,
Hostname: hostname,
},
},
},
},
}
}

func TestObserveStorageURLs(t *testing.T) {
tests := []struct {
name string
indexer cache.Indexer
currentConfig map[string]interface{}
wantStorageURLs []string
wantErrs []error
endpoint *v1.Endpoints
}{
{
name: "test etcd-bootstrap with dummy IP",
indexer: cache.NewIndexer(cache.MetaNamespaceKeyFunc, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc}),
currentConfig: nil,
wantStorageURLs: []string{"https://etcd-bootstrap." + clusterFQDN + ":2379"},
wantErrs: nil,
endpoint: getEndpoint("etcd-bootstrap", "192.0.2.1"),
},
{
name: "test etcd-bootstrap with real IP",
indexer: cache.NewIndexer(cache.MetaNamespaceKeyFunc, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc}),
currentConfig: nil,
wantStorageURLs: []string{"https://10.0.0.1:2379"},
wantErrs: nil,
endpoint: getEndpoint("etcd-bootstrap", "10.0.0.1"),
},
{
name: "test etcd member",
indexer: cache.NewIndexer(cache.MetaNamespaceKeyFunc, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc}),
currentConfig: nil,
wantStorageURLs: []string{"https://etcd-0." + clusterFQDN + ":2379"},
wantErrs: nil,
endpoint: getEndpoint("etcd-0", "192.0.2.1"),
},
// TODO: Add more test cases
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
client := fake.NewSimpleClientset()
lister := configobservation.Listers{
OpenshiftEtcdEndpointsLister: corev1listers.NewEndpointsLister(tt.indexer),
}
r := events.NewRecorder(client.CoreV1().Events("openshift-etcd"), "test-operator",
fakeObjectReference(tt.endpoint))
if err := tt.indexer.Add(tt.endpoint); err != nil {
t.Errorf("error adding endpoint to store: %#v", err)
}
wantObserverConfig, err := getWantObserverConfig(tt.wantStorageURLs)
if err != nil {
t.Errorf("error getting wantObserverConfig: %#v", err)
}
gotObservedConfig, gotErrs := ObserveStorageURLs(lister, r, tt.currentConfig)
if !reflect.DeepEqual(gotObservedConfig, wantObserverConfig) {
t.Errorf("ObserveStorageURLs() gotObservedConfig = %v, want %v", gotObservedConfig, wantObserverConfig)
}
if !reflect.DeepEqual(gotErrs, tt.wantErrs) {
t.Errorf("ObserveStorageURLs() gotErrs = %v, want %v", gotErrs, tt.wantErrs)
}
})
}
}
72 changes: 72 additions & 0 deletions vendor/github.com/certifi/gocertifi/gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5951c08

Please sign in to comment.