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

Commit

Permalink
Merge pull request #562 from rancher-max/kubectl_integrations
Browse files Browse the repository at this point in the history
Add route test and kubectl validations
  • Loading branch information
rancher-max authored Oct 14, 2019
2 parents 9533119 + 02d8396 commit 1595df5
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 20 deletions.
1 change: 1 addition & 0 deletions tests/integration/domain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func domainTests(t *testing.T, when spec.G, it spec.S) {
randomDomain := testutil.GenerateRandomDomain()
domain.RegisterDomain(t, randomDomain, service.Name)
assert.Equal(t, randomDomain, domain.GetDomain())
assert.Equal(t, randomDomain, domain.GetKubeDomain())
})
}, 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 @@ -41,8 +41,8 @@ func riofileTests(t *testing.T, when spec.G, it spec.S) {
assert.Equal(t, "/bv0", routerBar.Router.Spec.Routes[0].Matches[0].Path.Exact, "should have correct route set")
routerFooV0 := testutil.GetRoute(t, "route-foo", "/v0")
routerFooV3 := testutil.GetRoute(t, "route-foo", "/v3")
assert.Equal(t, "Hello World", routerFooV0.GetEndpoint(), "should have working route paths for v1")
assert.Equal(t, "Hello World v3", routerFooV3.GetEndpoint(), "should have working route paths for v3")
assert.Equal(t, "Hello World", routerFooV0.GetEndpointResponse(), "should have working route paths for v1")
assert.Equal(t, "Hello World v3", routerFooV3.GetEndpointResponse(), "should have working route paths for v3")
})
}, spec.Parallel())
}
20 changes: 16 additions & 4 deletions tests/integration/route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,22 @@ func routeTests(t *testing.T, when spec.G, it spec.S) {

when("a running service has routes added to it", func() {
it("should be accessible on multiple revision by multiple routes", func() {
routeA.Add(t, "/to-svc-v0", "to", service)
routeB.Add(t, "/to-svc-v3", "to", stagedService)
assert.Equal(t, "Hello World", routeA.GetEndpoint())
assert.Equal(t, "Hello World v3", routeB.GetEndpoint())
routeA.Add(t, "", "/to-svc-v0", "to", service)
routeB.Add(t, "", "/to-svc-v3", "to", stagedService)
assert.Equal(t, "Hello World", routeA.GetEndpointResponse())
assert.Equal(t, "Hello World v3", routeB.GetEndpointResponse())
assert.Equal(t, "Hello World", routeA.GetKubeEndpointResponse())
assert.Equal(t, "Hello World v3", routeB.GetKubeEndpointResponse())
})
it("should be accessible from one domain", func() {
routeA.Add(t, "test-route-root", "/first", "to", service)
routeB.Add(t, "test-route-root", "/to-svc-v3", "to", stagedService)
assert.Equal(t, routeA.Router.Status.Endpoints, routeB.Router.Status.Endpoints)
assert.Equal(t, routeA.Name, routeB.Name)
assert.Equal(t, "Hello World", routeA.GetEndpointResponse())
assert.Equal(t, "Hello World v3", routeB.GetEndpointResponse())
assert.Equal(t, "Hello World", routeA.GetKubeEndpointResponse())
assert.Equal(t, "Hello World v3", routeB.GetKubeEndpointResponse())
})
}, spec.Parallel())
}
4 changes: 4 additions & 0 deletions tests/integration/weight_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,15 @@ func weightTests(t *testing.T, when spec.G, it spec.S) {
it("should keep 100% of weight on original revision", func() {
assert.Equal(t, 100, service.GetCurrentWeight())
assert.Equal(t, 0, stagedService.GetCurrentWeight())
assert.Equal(t, 100, service.GetKubeCurrentWeight())
assert.Equal(t, 0, stagedService.GetKubeCurrentWeight())
})
it("should be able to move 5% of weight to new revision", func() {
stagedService.Weight(5)
assert.Equal(t, 95, service.GetCurrentWeight())
assert.Equal(t, 5, stagedService.GetCurrentWeight())
assert.Equal(t, 95, service.GetKubeCurrentWeight())
assert.Equal(t, 5, stagedService.GetKubeCurrentWeight())
})
}, spec.Parallel())
}
31 changes: 27 additions & 4 deletions tests/testutil/publicdomain.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,30 @@ func (td *TestDomain) UnRegister() {
}
}

// GetDomainName returns standard format non-namespaced domain, ex: "foo.bar"
// GetDomain returns standard format non-namespaced domain, ex: "foo.bar"
func (td *TestDomain) GetDomain() string {
err := td.reload()
if err != nil {
td.T.Fatalf("failed to fetch domain: %v", err.Error())
}
if td.PublicDomain.Spec.DomainName == "" {
return ""
return getStandardFormatDomain(td.PublicDomain)
}

// GetKubeDomain receives the TestDomain object to retrieve the test PublicDomain data
// CLI Command Run: "kubectl get publicdomains my-domain -n testing-ns -o json"
func (td *TestDomain) GetKubeDomain() string {
td.reload()
args := []string{"get", "publicdomains", td.PublicDomain.GetName(), "-n", testingNamespace, "-o", "json"}
resultString, err := KubectlCmd(args)
if err != nil {
td.T.Fatalf("Failed to get admin.rio.cattle.io.publicdomains: %v", err.Error())
}
var results adminv1.PublicDomain
err = json.Unmarshal([]byte(resultString), &results)
if err != nil {
td.T.Fatalf("Failed to unmarshal PublicDomain result: %s with error: %v", resultString, err.Error())
}
return strings.Replace(td.PublicDomain.Spec.DomainName, "-", ".", 1)
return getStandardFormatDomain(results)
}

//////////////////
Expand Down Expand Up @@ -94,3 +108,12 @@ func (td *TestDomain) waitForDomain() error {
}
return nil
}

// getStandardFormatDomain takes in a PublicDomain object
// Returns standard format non-namespaced public domain name, ex: "foo.bar"
func getStandardFormatDomain(publicDomain adminv1.PublicDomain) string {
if publicDomain.Spec.DomainName == "" {
return ""
}
return strings.Replace(publicDomain.Spec.DomainName, "-", ".", 1)
}
42 changes: 36 additions & 6 deletions tests/testutil/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@ type TestRoute struct {

// Executes "rio route add routename.testing-namespace/{routePath} to {service}"
// This does not take a domain param, that is setup by default.
// domain is optional, if empty it will generate a random domain.
// routePath is optional, if empty it will set only domain.
func (tr *TestRoute) Add(t *testing.T, routePath string, action string, target TestService) {
func (tr *TestRoute) Add(t *testing.T, domain string, routePath string, action string, target TestService) {
tr.T = t
fakeDomain := RandomString(5)
if domain == "" {
domain = RandomString(5)
}
tr.Path = routePath
tr.Name = fmt.Sprintf("%s/%s", testingNamespace, fakeDomain)
route := fmt.Sprintf("%s.%s%s", fakeDomain, testingNamespace, routePath)
tr.Name = fmt.Sprintf("%s/%s", testingNamespace, domain)
route := fmt.Sprintf("%s.%s%s", domain, testingNamespace, routePath)
_, err := RioCmd([]string{"route", "add", route, action, target.Name})
if err != nil {
tr.T.Fatalf("route add command failed: %v", err.Error())
Expand Down Expand Up @@ -63,9 +66,9 @@ func (tr *TestRoute) Remove() {
}
}

// GetEndpoint performs an http.get against the route's full domain and path and
// GetEndpointResponse performs an http.get against the route's full domain and path and
// returns response if status code is 200, otherwise it errors out
func (tr *TestRoute) GetEndpoint() string {
func (tr *TestRoute) GetEndpointResponse() string {
if len(tr.Router.Status.Endpoints) == 0 {
tr.T.Fatal("router has no endpoint")
}
Expand All @@ -76,6 +79,33 @@ func (tr *TestRoute) GetEndpoint() string {
return response
}

// GetKubeEndpointResponse performs an http.get against the route's full domain and all paths on it
// Returns responses if status code is 200 for all of them, otherwise it errors out
func (tr *TestRoute) GetKubeEndpointResponse() string {
// Get Router object using kubectl
args := []string{"get", "routers", tr.Router.GetName(), "-n", testingNamespace, "-o", "json"}
resultString, err := KubectlCmd(args)
if err != nil {
tr.T.Fatalf("Failed to get rio.cattle.io.routers: %v", err.Error())
}
var results riov1.Router
err = json.Unmarshal([]byte(resultString), &results)
if err != nil {
tr.T.Fatalf("Failed to unmarshal results: %v", err.Error())
}

// Validate there is a base endpoint
if len(results.Status.Endpoints) == 0 {
tr.T.Fatal("router has no endpoint")
}
response, err := WaitForURLResponse(fmt.Sprintf("%s%s", results.Status.Endpoints[0], tr.Path))
if err != nil {
tr.T.Fatal(err.Error())
}

return response
}

//////////////////
// Private methods
//////////////////
Expand Down
30 changes: 26 additions & 4 deletions tests/testutil/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,7 @@ func (ts *TestService) GetSpecWeight() int {
// Return service's actual current weight, not the spec (end-goal) weight
func (ts *TestService) GetCurrentWeight() int {
ts.reloadApp()
if val, ok := ts.App.Status.RevisionWeight[ts.Version]; ok {
return val.Weight
}
return 0
return getRevisionWeight(ts.App, ts.Version)
}

func (ts *TestService) GetImage() string {
Expand Down Expand Up @@ -288,6 +285,23 @@ func (ts *TestService) GetKubeAppEndpointURL() string {
return strings.Replace(url, "\"", "", -1) // remove double quotes from output
}

// GetKubeCurrentWeight takes in a revision value and retrieves the actual current weight, not the spec (end-goal) weight
func (ts *TestService) GetKubeCurrentWeight() int {
ts.reloadApp()
args := []string{"get", "apps", ts.App.GetName(), "-n", testingNamespace, "-o", "json"}
resultString, err := KubectlCmd(args)
if err != nil {
ts.T.Fatalf("Failed to get rio.cattle.io.apps: %v", err.Error())
}
var app riov1.App
err = json.Unmarshal([]byte(resultString), &app)
if err != nil {
ts.T.Fatalf("Failed to unmarshal App results: %s with error: %v", resultString, err.Error())
}

return getRevisionWeight(app, ts.Version)
}

//////////////////
// Private methods
//////////////////
Expand Down Expand Up @@ -343,6 +357,14 @@ func (ts *TestService) getScalingTimeout() time.Duration {
return time.Second * scalingTimeout
}

// Get the current weight of a revision. If it does not exist, then it is 0.
func getRevisionWeight(app riov1.App, version string) int {
if val, ok := app.Status.RevisionWeight[version]; ok {
return val.Weight
}
return 0
}

//////////////////
// Wait helpers
//////////////////
Expand Down

0 comments on commit 1595df5

Please sign in to comment.