Skip to content

Commit

Permalink
added test cases
Browse files Browse the repository at this point in the history
Signed-off-by: Disaiah Bennett <[email protected]>
  • Loading branch information
dislbenn committed Mar 19, 2024
1 parent d5cc4e3 commit 8e22aba
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 49 deletions.
3 changes: 2 additions & 1 deletion controllers/managedcluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ func unsetManagedStatus(dc *discovery.DiscoveredCluster) bool {
delete(dc.Labels, "isManagedCluster")
updated = true
}
if dc.Spec.IsManagedCluster == true {

if dc.Spec.IsManagedCluster {
dc.Spec.IsManagedCluster = false
updated = true
}
Expand Down
3 changes: 0 additions & 3 deletions controllers/managedcluster_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,6 @@ func Test_setManagedStatus(t *testing.T) {
}

func Test_unsetManagedStatus(t *testing.T) {
type args struct {
dc *discovery.DiscoveredCluster
}
tests := []struct {
name string
dc *discovery.DiscoveredCluster
Expand Down
2 changes: 0 additions & 2 deletions controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
. "github.com/onsi/gomega"
v1 "github.com/stolostron/discovery/api/v1"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/event"

clusterapiv1 "open-cluster-management.io/api/cluster/v1"
Expand All @@ -32,7 +31,6 @@ func TestControllers(t *testing.T) {
RunSpecs(t, "Controller Suite")
}

var cfg *rest.Config
var k8sClient client.Client
var testEnv *envtest.Environment
var signalHandlerContext context.Context
Expand Down
34 changes: 34 additions & 0 deletions pkg/ocm/cluster/cluster_types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright Contributors to the Open Cluster Management project

package cluster

import (
"testing"
)

func TestGetClusterURL(t *testing.T) {
tests := []struct {
name string
endpointURL string
want bool
}{
{
name: "Should get OCM API URL for cluster",
endpointURL: "%s/api/clusters_mgmt/v1/clusters",
want: true,
},
{
name: "Should get OCM API URL for cluster",
endpointURL: "path/to/api/clusters_mgmt/v1/clusters",
want: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.endpointURL == GetClusterURL(); got != tt.want {
t.Errorf("GetSubscriptionURL() = %v, got %v, want %v", GetClusterURL(), got, tt.want)
}
})
}
}
8 changes: 1 addition & 7 deletions pkg/ocm/ocm.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,8 @@ import (
"github.com/stolostron/discovery/pkg/ocm/common"
"github.com/stolostron/discovery/pkg/ocm/subscription"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/log"
)

var logr = log.Log.WithName("ocm_controller")

// DiscoverClusters returns a list of DiscoveredClusters found in both the accounts_mgmt and
// accounts_mgmt apis with the given filters
func DiscoverClusters(token string, baseURL string, baseAuthURL string, filters discovery.Filter) ([]discovery.DiscoveredCluster, error) {
Expand Down Expand Up @@ -102,10 +99,7 @@ func formatCluster(sub subscription.Subscription) (discovery.DiscoveredCluster,
// IsUnrecoverable returns true if the specified error is not temporary
// and will continue to occur with the current state.
func IsUnrecoverable(err error) bool {
if errors.Is(err, auth.ErrInvalidToken) {
return true
}
return false
return errors.Is(err, auth.ErrInvalidToken)
}

// computeDisplayName tries to provide a more user-friendly name if set
Expand Down
34 changes: 34 additions & 0 deletions pkg/ocm/subscription/subscription_types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright Contributors to the Open Cluster Management project

package subscription

import (
"testing"
)

func TestGetSubscriptionURL(t *testing.T) {
tests := []struct {
name string
endpointURL string
want bool
}{
{
name: "Should get OCM API URL for subscription",
endpointURL: "%s/api/accounts_mgmt/v1/subscriptions",
want: true,
},
{
name: "Should get OCM API URL for subscription",
endpointURL: "path/to/api/accounts_mgmt/v1/subscriptions",
want: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.endpointURL == GetSubscriptionURL(); got != tt.want {
t.Errorf("GetSubscriptionURL() = %v, got %v, want %v", GetSubscriptionURL(), got, tt.want)
}
})
}
}
37 changes: 1 addition & 36 deletions test/e2e/discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,10 +360,7 @@ var _ = Describe("[P1][Sev1][installer] Discoveryconfig controller", func() {
createdNS := &corev1.Namespace{}
Eventually(func() bool {
err := k8sClient.Get(ctx, types.NamespacedName{Name: secondNamespace}, createdNS)
if err != nil {
return false
}
return true
return err == nil
}, timeout, interval).Should(BeTrue())
})

Expand Down Expand Up @@ -542,18 +539,6 @@ func removeFinalizerFromRoleBinding(roleBinding rbacv1.RoleBinding) error {
return err
}

// annotate adds an annotation to modify the baseUrl used with the discoveryconfig
func annotate(dc *discovery.DiscoveryConfig) *discovery.DiscoveryConfig {
if baseURL != "" {
dc.SetAnnotations(map[string]string{"ocmBaseURL": baseURL, "authBaseURL": baseURL})
return dc
} else {
dc.SetAnnotations(map[string]string{"ocmBaseURL": defaultBaseUrl(), "authBaseURL": defaultBaseUrl()})

return dc
}
}

// annotateWithScenario adds an annotation to modify the baseUrl with a scenario path
func annotateWithScenario(dc *discovery.DiscoveryConfig, scenario string) *discovery.DiscoveryConfig {
if baseURL != "" {
Expand All @@ -578,26 +563,6 @@ func updateTestserverScenario(scenario string) {
Expect(k8sClient.Update(ctx, config)).Should(Succeed())
}

func listDiscoveredClusters() (*discovery.DiscoveredClusterList, error) {
discoveredClusters := &discovery.DiscoveredClusterList{}
err := k8sClient.List(ctx, discoveredClusters, client.InNamespace(discoveryNamespace))
return discoveredClusters, err
}

func getDiscoveredClusterByID(id string) (*discovery.DiscoveredCluster, error) {
discoveredClusters, err := listDiscoveredClusters()
if err != nil {
return nil, err
}
for _, dc := range discoveredClusters.Items {
dc := dc
if dc.Spec.Name == id {
return &dc, nil
}
}
return nil, fmt.Errorf("Cluster not found")
}

func countManagedDiscoveredClusters(namespace string) (int, error) {
discoveredClusters := &discovery.DiscoveredClusterList{}
err := k8sClient.List(ctx, discoveredClusters,
Expand Down

0 comments on commit 8e22aba

Please sign in to comment.