Files
odo/pkg/url/status_test.go
Philippe Martin 850c7cd723 Remove commands that wont be implemented for v3-alpha1 (#5433)
* Remove odo staorage commands

* Remove "odo service" + "odo catalog * service" commands

* Remove odo link/unlink commands

* Remove related integration tests

* Remove application concept

* fix rebase

* fix test

* Remove config command

* Remove env command

* Remove application package

* Remove config package

* Move odogenerator and unions packages into kclient

* Move notify package to cli/version

* Fix script mockgen

* Remove odo debug command oand debug package

* Remove odo component describe/exec/log/status/test

* Remove operator-hub tests from IBM tests

* Remove operator hub tests from CI

* Fix e2e tests
2022-02-06 23:42:15 -05:00

201 lines
5.8 KiB
Go

package url
import (
"reflect"
"testing"
"github.com/redhat-developer/odo/pkg/kclient/unions"
"github.com/golang/mock/gomock"
routev1 "github.com/openshift/api/route/v1"
"github.com/redhat-developer/odo/pkg/kclient"
"github.com/redhat-developer/odo/pkg/localConfigProvider"
"github.com/redhat-developer/odo/pkg/testingutil"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kclient_fake "github.com/redhat-developer/odo/pkg/kclient/fake"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/discovery/fake"
ktesting "k8s.io/client-go/testing"
)
type fakeDiscovery struct {
*fake.FakeDiscovery
}
var fakeDiscoveryWithProject = &fakeDiscovery{
FakeDiscovery: &fake.FakeDiscovery{
Fake: &ktesting.Fake{
Resources: []*metav1.APIResourceList{
{
GroupVersion: "route.openshift.io/v1",
APIResources: []metav1.APIResource{{
Name: "routes",
SingularName: "route",
Namespaced: false,
Kind: "Route",
ShortNames: []string{"route"},
}},
},
},
},
},
}
func TestGetURLsForKubernetes(t *testing.T) {
componentName := "my-component"
testURL1 := localConfigProvider.LocalURL{Name: "example-1", Port: 9090, Host: "com", Kind: "ingress", Secure: true}
testURL2 := localConfigProvider.LocalURL{Name: "example-2", Port: 9090, Host: "com", Kind: "ingress", Secure: false}
testURL3 := localConfigProvider.LocalURL{Name: "routeurl2", Port: 8080, Kind: "route"}
testURL4 := localConfigProvider.LocalURL{Name: "example", Port: 8080, Kind: "route"}
tests := []struct {
name string
envURLs []localConfigProvider.LocalURL
routeList *routev1.RouteList
ingressList *unions.KubernetesIngressList
expectedStatusURL statusURL
}{
{
name: "1) Cluster with https URL defined in env info",
envURLs: []localConfigProvider.LocalURL{testURL1},
ingressList: &unions.KubernetesIngressList{
Items: []*unions.KubernetesIngress{},
},
expectedStatusURL: statusURL{
name: testURL1.Name,
kind: "ingress",
port: testURL1.Port,
secure: testURL1.Secure,
url: "https://example-1.com",
},
routeList: &routev1.RouteList{
Items: []routev1.Route{},
},
},
{
name: "2) Cluster with https URL defined in env info",
envURLs: []localConfigProvider.LocalURL{testURL2},
ingressList: &unions.KubernetesIngressList{
Items: []*unions.KubernetesIngress{},
},
expectedStatusURL: statusURL{
name: testURL2.Name,
kind: "ingress",
port: testURL2.Port,
secure: testURL2.Secure,
url: "http://example-2.com",
},
routeList: &routev1.RouteList{
Items: []routev1.Route{},
},
},
{
name: "3) Cluster with route defined in env info",
envURLs: []localConfigProvider.LocalURL{testURL3},
ingressList: &unions.KubernetesIngressList{
Items: []*unions.KubernetesIngress{},
},
expectedStatusURL: statusURL{
name: testURL3.Name,
kind: "route",
port: testURL3.Port,
secure: false,
url: "http://",
},
routeList: &routev1.RouteList{
Items: []routev1.Route{},
},
},
{
name: "4) Cluster with route defined",
envURLs: []localConfigProvider.LocalURL{},
ingressList: &unions.KubernetesIngressList{
Items: []*unions.KubernetesIngress{},
},
expectedStatusURL: statusURL{
name: testURL4.Name,
kind: "route",
port: testURL4.Port,
secure: false,
url: "http://example.com",
},
routeList: &routev1.RouteList{
Items: []routev1.Route{
testingutil.GetSingleRoute(testURL4.Name, testURL4.Port, componentName, "app"),
},
},
},
{
name: "5) Cluster with ingress defined",
envURLs: []localConfigProvider.LocalURL{},
ingressList: &unions.KubernetesIngressList{
Items: []*unions.KubernetesIngress{
kclient_fake.GetKubernetesIngressListWithMultiple(componentName, "app", true, false).Items[0],
},
},
routeList: &routev1.RouteList{
Items: []routev1.Route{},
},
expectedStatusURL: statusURL{
name: "example-0",
kind: "ingress",
port: 8080,
secure: false,
url: "http://example-0.com",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockLocalConfig := localConfigProvider.NewMockLocalConfigProvider(ctrl)
mockLocalConfig.EXPECT().GetName().Return(componentName).AnyTimes()
mockLocalConfig.EXPECT().GetApplication().Return("app")
mockLocalConfig.EXPECT().Exists().Return(true)
mockLocalConfig.EXPECT().ListURLs().Return(tt.envURLs, nil)
// Initialising the fakeclient
fkclient, fkclientset := kclient.FakeNewWithIngressSupports(true, false)
fkclient.Namespace = "default"
fkclient.SetDiscoveryInterface(fakeDiscoveryWithProject)
// Return the test's ingress list when requested
fkclientset.Kubernetes.PrependReactor("list", "ingresses", func(action ktesting.Action) (bool, runtime.Object, error) {
if action.GetResource().GroupVersion().Group == "networking.k8s.io" {
return true, tt.ingressList.GetNetworkingV1IngressList(true), nil
}
return true, tt.ingressList.GetExtensionV1Beta1IngresList(true), nil
})
// Return the test's route list when requested
fkclientset.RouteClientset.PrependReactor("list", "routes", func(action ktesting.Action) (bool, runtime.Object, error) {
return true, tt.routeList, nil
})
statusUrls, err := getURLsForKubernetes(fkclient, mockLocalConfig, false)
if err != nil {
t.Fatalf("Error occurred: %v", err)
}
if len(statusUrls) == 0 {
t.Fatalf("statusURLs has unexpected size 0, must be 1")
}
if !reflect.DeepEqual(tt.expectedStatusURL, statusUrls[0]) {
t.Fatalf("Mismatching status URL - expected: %v, actual: %v", tt.expectedStatusURL, statusUrls[0])
}
})
}
}