Skip to content

Commit

Permalink
Fix unit test panic
Browse files Browse the repository at this point in the history
  • Loading branch information
gnufied committed Apr 4, 2019
1 parent e0eb289 commit 0ec8bd6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func ObserveCloudProviderNames(genericListers configobserver.Listers, recorder e
cloudProviderConfPath := []string{"extendedArguments", "cloud-config"}
previouslyObservedConfig := map[string]interface{}{}

existinCloudConfig, _, err := unstructured.NestedStringSlice(existingConfig, cloudProviderConfPath...)
existingCloudConfig, _, err := unstructured.NestedStringSlice(existingConfig, cloudProviderConfPath...)
if err != nil {
return previouslyObservedConfig, append(errs, err)
}
Expand All @@ -40,8 +40,8 @@ func ObserveCloudProviderNames(genericListers configobserver.Listers, recorder e
}
}

if len(existinCloudConfig) > 0 {
if err := unstructured.SetNestedStringSlice(previouslyObservedConfig, existinCloudConfig, cloudProviderConfPath...); err != nil {
if len(existingCloudConfig) > 0 {
if err := unstructured.SetNestedStringSlice(previouslyObservedConfig, existingCloudConfig, cloudProviderConfPath...); err != nil {
errs = append(errs, err)
}
}
Expand All @@ -50,7 +50,7 @@ func ObserveCloudProviderNames(genericListers configobserver.Listers, recorder e

infrastructure, err := listers.InfrastructureLister.Get("cluster")
if errors.IsNotFound(err) {
recorder.Warningf("ObserverCloudProviderNames", "Required infrastructures.%s/cluster not found", configv1.GroupName)
recorder.Warningf("ObserveCloudProviderNames", "Required infrastructures.%s/cluster not found", configv1.GroupName)
return observedConfig, errs
}
if err != nil {
Expand All @@ -66,17 +66,22 @@ func ObserveCloudProviderNames(genericListers configobserver.Listers, recorder e

sourceCloudConfigMap := infrastructure.Spec.CloudConfig.Name
sourceCloudConfigNamespace := configNamespace
sourceLocation := resourcesynccontroller.ResourceLocation{
Namespace: sourceCloudConfigNamespace,
Name: sourceCloudConfigMap,
}

if len(sourceCloudConfigMap) == 0 {
sourceLocation = resourcesynccontroller.ResourceLocation{}
}

err = listers.ResourceSyncer().SyncConfigMap(
resourcesynccontroller.ResourceLocation{
Namespace: targetNamespaceName,
Name: "cloud-config",
},
resourcesynccontroller.ResourceLocation{
Namespace: sourceCloudConfigNamespace,
Name: sourceCloudConfigMap,
},
)
sourceLocation)

if err != nil {
errs = append(errs, err)
return observedConfig, errs
Expand All @@ -90,12 +95,12 @@ func ObserveCloudProviderNames(genericListers configobserver.Listers, recorder e
staticCloudConfFile := fmt.Sprintf(cloudProviderConfFilePath, infrastructure.Spec.CloudConfig.Key)

if err := unstructured.SetNestedStringSlice(observedConfig, []string{staticCloudConfFile}, cloudProviderConfPath...); err != nil {
recorder.Warningf("ObserverCloudProviderNames", "Failed setting cloud-config : %v", err)
recorder.Warningf("ObserveCloudProviderNames", "Failed setting cloud-config : %v", err)
errs = append(errs, err)
}

if !equality.Semantic.DeepEqual(existinCloudConfig, []string{staticCloudConfFile}) {
recorder.Eventf("ObserverCloudProviderNamesChanges", "CloudProvider config file changed to %s", staticCloudConfFile)
if !equality.Semantic.DeepEqual(existingCloudConfig, []string{staticCloudConfFile}) {
recorder.Eventf("ObserveCloudProviderNamesChanges", "CloudProvider config file changed to %s", staticCloudConfFile)
}

return observedConfig, errs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,21 @@ import (
configv1 "github.com/openshift/api/config/v1"
configlistersv1 "github.com/openshift/client-go/config/listers/config/v1"
"github.com/openshift/library-go/pkg/operator/events"
"github.com/openshift/library-go/pkg/operator/resourcesynccontroller"

"github.com/openshift/cluster-kube-controller-manager-operator/pkg/operator/configobservation"
)

type FakeResourceSyncer struct{}

func (fakeSyncer *FakeResourceSyncer) SyncConfigMap(destination, source resourcesynccontroller.ResourceLocation) error {
return nil
}

func (fakeSyncer *FakeResourceSyncer) SyncSecret(destination, source resourcesynccontroller.ResourceLocation) error {
return nil
}

func TestObserveCloudProviderNames(t *testing.T) {
cases := []struct {
platform configv1.PlatformType
Expand Down Expand Up @@ -52,6 +63,7 @@ func TestObserveCloudProviderNames(t *testing.T) {
}
listers := configobservation.Listers{
InfrastructureLister: configlistersv1.NewInfrastructureLister(indexer),
ResourceSync: &FakeResourceSyncer{},
}
result, errs := ObserveCloudProviderNames(listers, events.NewInMemoryRecorder("cloud"), map[string]interface{}{})
if len(errs) > 0 {
Expand Down

0 comments on commit 0ec8bd6

Please sign in to comment.