Skip to content

Commit

Permalink
Completed test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
gvicentin committed Apr 5, 2024
1 parent 1f0d2c6 commit 7e9db4e
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 0 deletions.
29 changes: 29 additions & 0 deletions cmd/plugin/rpaasv2/cmd/start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,32 @@
// license that can be found in the LICENSE file.

package cmd

import (
"bytes"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/tsuru/rpaas-operator/pkg/rpaas/client/fake"
)

func TestStart(t *testing.T) {
stdout := &bytes.Buffer{}
stderr := &bytes.Buffer{}

args := []string{"./rpaasv2", "start", "-s", "some-service", "-i", "my-instance"}

client := &fake.FakeClient{
FakeStart: func(instance string) error {
require.Equal(t, instance, "my-instance")
return nil
},
}

app := NewApp(stdout, stderr, client)
err := app.Run(args)
require.NoError(t, err)
assert.Equal(t, stdout.String(), "Started instance some-service/my-instance\n")
}
29 changes: 29 additions & 0 deletions cmd/plugin/rpaasv2/cmd/stop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,32 @@
// license that can be found in the LICENSE file.

package cmd

import (
"bytes"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/tsuru/rpaas-operator/pkg/rpaas/client/fake"
)

func TestStop(t *testing.T) {
stdout := &bytes.Buffer{}
stderr := &bytes.Buffer{}

args := []string{"./rpaasv2", "stop", "-s", "some-service", "-i", "my-instance"}

client := &fake.FakeClient{
FakeStop: func(instance string) error {
require.Equal(t, instance, "my-instance")
return nil
},
}

app := NewApp(stdout, stderr, client)
err := app.Run(args)
require.NoError(t, err)
assert.Equal(t, stdout.String(), "Shutting down instance some-service/my-instance\n")
}
53 changes: 53 additions & 0 deletions pkg/rpaas/client/start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,56 @@
// license that can be found in the LICENSE file.

package client

import (
"context"
"fmt"
"net/http"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestClientThroughTsuru_Start(t *testing.T) {
tests := []struct {
name string
instance string
expectedError string
handler http.HandlerFunc
}{
{
name: "when server returns an unexpected status code",
instance: "my-instance",
expectedError: "rpaasv2: unexpected status code: 404 Not Found, detail: instance not found",
handler: func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, "instance not found")
},
},
{
name: "when server returns the expected response",
instance: "my-instance",
handler: func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, r.Method, "POST")
assert.Equal(t, fmt.Sprintf("/services/%s/proxy/%s?callback=%s", FakeTsuruService, "my-instance", "/resources/my-instance/start"), r.URL.RequestURI())
assert.Equal(t, "Bearer f4k3t0k3n", r.Header.Get("Authorization"))
assert.Equal(t, "application/x-www-form-urlencoded", r.Header.Get("Content-Type"))
w.WriteHeader(http.StatusOK)
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
client, server := newClientThroughTsuru(t, tt.handler)
defer server.Close()
err := client.Start(context.TODO(), tt.instance)
if tt.expectedError == "" {
require.NoError(t, err)
return
}
assert.EqualError(t, err, tt.expectedError)
})
}
}
53 changes: 53 additions & 0 deletions pkg/rpaas/client/stop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,56 @@
// license that can be found in the LICENSE file.

package client

import (
"context"
"fmt"
"net/http"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestClientThroughTsuru_Stop(t *testing.T) {
tests := []struct {
name string
instance string
expectedError string
handler http.HandlerFunc
}{
{
name: "when server returns an unexpected status code",
instance: "my-instance",
expectedError: "rpaasv2: unexpected status code: 404 Not Found, detail: instance not found",
handler: func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, "instance not found")
},
},
{
name: "when server returns the expected response",
instance: "my-instance",
handler: func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, r.Method, "POST")
assert.Equal(t, fmt.Sprintf("/services/%s/proxy/%s?callback=%s", FakeTsuruService, "my-instance", "/resources/my-instance/stop"), r.URL.RequestURI())
assert.Equal(t, "Bearer f4k3t0k3n", r.Header.Get("Authorization"))
assert.Equal(t, "application/x-www-form-urlencoded", r.Header.Get("Content-Type"))
w.WriteHeader(http.StatusOK)
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
client, server := newClientThroughTsuru(t, tt.handler)
defer server.Close()
err := client.Stop(context.TODO(), tt.instance)
if tt.expectedError == "" {
require.NoError(t, err)
return
}
assert.EqualError(t, err, tt.expectedError)
})
}
}

0 comments on commit 7e9db4e

Please sign in to comment.