Skip to content
This repository has been archived by the owner on Sep 5, 2024. It is now read-only.

Commit

Permalink
Adding extra validations using kubectl for service endpoints
Browse files Browse the repository at this point in the history
This validates the endpoint URLs generated by rio for the revisions and Global are present.
- Making improvements based on PR feedback
- Getting the url hostname only to compare the endpoint between kubectl and rio
  • Loading branch information
Izaac Zavaleta committed Oct 11, 2019
1 parent c9e648d commit 9f415d8
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 19 deletions.
28 changes: 25 additions & 3 deletions tests/integration/endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,39 @@ func endpointTests(t *testing.T, when spec.G, it spec.S) {
var service testutil.TestService
var stagedService testutil.TestService

it.Before(func() {
service.Create(t, "ibuildthecloud/demo:v1")
})
it.After(func() {
service.Remove()
stagedService.Remove()
})

when("a service is running and another is staged", func() {
it("should have endpoints that are available on both", func() {
service.Create(t, "ibuildthecloud/demo:v1")
stagedService = service.Stage("ibuildthecloud/demo:v3", "v3")
assert.Equal(t, "Hello World", service.GetEndpoint())
assert.Equal(t, "Hello World v3", stagedService.GetEndpoint())

// Check the hostnames returned by Rio and Kubectl are equal
assert.Equal(t,
testutil.GetHostname(service.GetKubeEndpointURL()),
testutil.GetHostname(service.GetEndpointURL()),
)
assert.Equal(t,
testutil.GetHostname(stagedService.GetKubeEndpointURL()),
testutil.GetHostname(stagedService.GetEndpointURL()),
)

assert.Equal(t, "Hello World", service.GetEndpointResponse())
assert.Equal(t, "Hello World v3", stagedService.GetEndpointResponse())
})
it("should have the service app endpoint properly created", func() {

// Check the hostnames returned by Rio and Kubectl are equal
assert.Equal(t,
testutil.GetHostname(service.GetKubeAppEndpointURL()),
testutil.GetHostname(service.GetAppEndpointURL()),
)
assert.Equal(t, "Hello World", service.GetAppEndpointResponse())
})
}, spec.Parallel())
}
4 changes: 2 additions & 2 deletions tests/integration/riofile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ func riofileTests(t *testing.T, when spec.G, it spec.S) {
// services and their endpoints
serviceV0 := testutil.GetService(t, "export-test-image", "v0")
serviceV3 := testutil.GetService(t, "export-test-image", "v3")
assert.Equal(t, serviceV0.GetEndpoint(), "Hello World", "should have service v0 with endpoint")
assert.Equal(t, serviceV3.GetEndpoint(), "Hello World v3", "should have service v3 with endpoint")
assert.Equal(t, serviceV0.GetEndpointResponse(), "Hello World", "should have service v0 with endpoint")
assert.Equal(t, serviceV3.GetEndpointResponse(), "Hello World v3", "should have service v3 with endpoint")
// routers and their endpoints
routerBar := testutil.GetRoute(t, "route-bar", "/bv0")
assert.Equal(t, "/bv0", routerBar.Router.Spec.Routes[0].Matches[0].Path.Exact, "should have correct route set")
Expand Down
115 changes: 101 additions & 14 deletions tests/testutil/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"math"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -133,20 +134,6 @@ func (ts *TestService) Stage(source, version string) TestService {
return stagedService
}

// GetEndpoint performs an http.get against the service endpoint and returns response if
// status code is 200, otherwise it errors out
func (ts *TestService) GetEndpoint() string {
endpoint, err := ts.waitForEndpointDNS()
if err != nil {
ts.T.Fatal(err.Error())
}
response, err := WaitForURLResponse(endpoint)
if err != nil {
ts.T.Fatal(err.Error())
}
return response
}

// Export calls "rio export {serviceName}" and returns that in a new TestService object
func (ts *TestService) Export() TestService {
args := []string{"export", "--type", "service", "--format", "json", ts.Name}
Expand All @@ -171,6 +158,24 @@ func (ts *TestService) ExportRaw() TestService {
// Getters
//////////

// GetEndpointResponse performs an http.get against the service endpoint and returns response if
// status code is 200, otherwise it errors out
func (ts *TestService) GetEndpointResponse() string {
response, err := WaitForURLResponse(ts.GetEndpointURL())
if err != nil {
ts.T.Fatal(err.Error())
}
return response
}

func (ts *TestService) GetAppEndpointResponse() string {
response, err := WaitForURLResponse(ts.GetAppEndpointURL())
if err != nil {
ts.T.Fatal(err.Error())
}
return response
}

// Returns count of ready and available pods
func (ts *TestService) GetAvailableReplicas() int {
if ts.Service.Status.DeploymentStatus != nil {
Expand Down Expand Up @@ -221,6 +226,68 @@ func (ts *TestService) GetRunningPods() string {
return out
}

// GetEndpointURL returns the URL for this service's app
func (ts *TestService) GetEndpointURL() string {
url, err := ts.waitForEndpointDNS()
if err != nil {
ts.T.Fatalf("Failed to get the endpoint url: %v", err.Error())
return ""
}
return url
}

// GetAppEndpointURL retrieves the service's app endpoint URL and returns it as string
func (ts *TestService) GetAppEndpointURL() string {
url, err := ts.waitForAppEndpointDNS()
if err != nil {
ts.T.Fatalf("Failed to get the endpoint url: %v", err.Error())
return ""
}
return url
}

// GetKubeEndpointURL returns the app revision endpoint URL
// and returns it as string
func (ts *TestService) GetKubeEndpointURL() string {
_, err := ts.waitForEndpointDNS()
if err != nil {
ts.T.Fatalf("Failed waiting for DNS: %v", err.Error())
return ""
}
args := []string{"get", "service.rio.cattle.io",
"-n", testingNamespace,
ts.Service.Name,
"-o", `jsonpath="{.status.endpoints[0]}"`}
url, err := KubectlCmd(args)
if err != nil {
ts.T.Fatalf("Failed to get endpoint url: %v", err.Error())
return ""
}
return strings.Replace(url, "\"", "", -1) // remove double quotes from output
}

// GetKubeAppEndpointURL returns the endpoint URL of the service's app
// by using kubectl and returns it as string
func (ts *TestService) GetKubeAppEndpointURL() string {
_, err := ts.waitForAppEndpointDNS()
if err != nil {
ts.T.Fatalf("Failed waiting for DNS: %v", err.Error())
return ""
}
appName := strings.Split(ts.AppName, "/")[1]
args := []string{"get", "apps",
"-n", testingNamespace,
appName,
"-o", `jsonpath="{.status.endpoints[0]}"`}
url, err := KubectlCmd(args)
if err != nil {
ts.T.Fatalf("Failed to get app endpoint url: %v", err.Error())
return ""
}

return strings.Replace(url, "\"", "", -1) // remove double quotes from output
}

//////////////////
// Private methods
//////////////////
Expand Down Expand Up @@ -372,3 +439,23 @@ func (ts *TestService) waitForEndpointDNS() (string, error) {
}
return ts.Service.Status.Endpoints[0], nil
}

func (ts *TestService) waitForAppEndpointDNS() (string, error) {
if len(ts.App.Status.Endpoints) > 0 {
return ts.App.Status.Endpoints[0], nil
}
f := wait.ConditionFunc(func() (bool, error) {
err := ts.reloadApp()
if err == nil {
if len(ts.App.Status.Endpoints) > 0 {
return true, nil
}
}
return false, nil
})
err := wait.Poll(2*time.Second, 60*time.Second, f)
if err != nil {
return "", errors.New("app endpoint never created")
}
return ts.App.Status.Endpoints[0], nil
}
9 changes: 9 additions & 0 deletions tests/testutil/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io/ioutil"
"math/rand"
"net/http"
"net/url"
"os"
"os/exec"
"strings"
Expand Down Expand Up @@ -102,3 +103,11 @@ func RandomString(length int) string {
func GenerateName() string {
return strings.Replace(namesgenerator.GetRandomName(2), "_", "-", -1)
}

func GetHostname(URL string) string {
u, err := url.Parse(URL)
if err != nil {
return ""
}
return u.Hostname()
}

0 comments on commit 9f415d8

Please sign in to comment.