-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
56 changed files
with
9,809 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ _output | |
_scratch | ||
.git | ||
_output | ||
Docker* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package rest | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
"net/http" | ||
"net/http/httptest" | ||
"strings" | ||
"testing" | ||
|
||
errors "github.com/go-openapi/errors" | ||
"github.com/stretchr/testify/assert" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
||
apipkg "github.com/sapcc/kubernikus/pkg/api" | ||
"github.com/sapcc/kubernikus/pkg/api/models" | ||
"github.com/sapcc/kubernikus/pkg/api/rest/operations" | ||
"github.com/sapcc/kubernikus/pkg/api/spec" | ||
"github.com/sapcc/kubernikus/pkg/generated/clientset/fake" | ||
) | ||
|
||
const ( | ||
NAMESPACE = "test" | ||
TOKEN = "abc123" | ||
ACCOUNT = "testaccount" | ||
) | ||
|
||
func mockAuth(token string) (*models.Principal, error) { | ||
if token != TOKEN { | ||
return nil, errors.New(401, "auth failed") | ||
} | ||
return &models.Principal{ | ||
AuthURL: "http://identity.test/v3", | ||
ID: "test", | ||
Name: "Test Mc Dougle", | ||
Domain: "TestDomain", | ||
Account: ACCOUNT, | ||
Roles: []string{"member", "os:kubernikus_admin"}, | ||
}, nil | ||
|
||
} | ||
|
||
func createTestHandler(t *testing.T) (http.Handler, *apipkg.Runtime) { | ||
swaggerSpec, err := spec.Spec() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
api := operations.NewKubernikusAPI(swaggerSpec) | ||
rt := &apipkg.Runtime{ | ||
Namespace: NAMESPACE, | ||
Kubernikus: fake.NewSimpleClientset(), | ||
} | ||
Configure(api, rt) | ||
api.KeystoneAuth = mockAuth | ||
return configureAPI(api), rt | ||
|
||
} | ||
|
||
func createRequest(method, path, body string) *http.Request { | ||
var buf io.Reader | ||
if body != "" { | ||
buf = strings.NewReader(body) | ||
} | ||
req := httptest.NewRequest(method, path, buf) | ||
req.Header.Set("Content-Type", "application/json") | ||
req.Header.Set("X-Auth-Token", TOKEN) | ||
return req | ||
} | ||
func result(handler http.Handler, req *http.Request) (int, http.Header, []byte) { | ||
rec := httptest.NewRecorder() | ||
handler.ServeHTTP(rec, req) | ||
response := rec.Result() | ||
body, _ := ioutil.ReadAll(response.Body) | ||
return response.StatusCode, response.Header, body | ||
|
||
} | ||
|
||
func TestCreateCluster(t *testing.T) { | ||
handler, rt := createTestHandler(t) | ||
req := createRequest("POST", "/api/v1/clusters", `{"name": "nase"}`) | ||
_, _, body := result(handler, req) | ||
|
||
_, err := rt.Kubernikus.KubernikusV1().Klusters(rt.Namespace).Get(fmt.Sprintf("%s-%s", "nase", ACCOUNT), metav1.GetOptions{}) | ||
assert.NoError(t, err, "resource not persisted") | ||
|
||
var kluster models.Kluster | ||
assert.NoError(t, kluster.UnmarshalBinary(body), "Failed to parse response") | ||
assert.Equal(t, "nase", kluster.Name) | ||
assert.Equal(t, "nase", kluster.Spec.Name) | ||
assert.Equal(t, models.KlusterPhasePending, kluster.Status.Phase) | ||
|
||
//ensure authentication | ||
req = createRequest("POST", "/api/v1/clusters", `{"name": "nase2"}`) | ||
req.Header.Del("X-Auth-Token") | ||
code, _, body := result(handler, req) | ||
if code != 401 { | ||
t.Errorf("Expected auth failure. Got status %d, body: %s", code, body) | ||
} | ||
|
||
} | ||
|
||
func TestClusterShow(t *testing.T) { | ||
//kluster := kubernikusv1.Kluster{ | ||
// ObjectMeta: metav1.ObjectMeta{ | ||
// Name: "nase-testaccount", | ||
// Namespace: rt.Namespace, | ||
// Labels: map[string]string{"account": "testaccount"}, | ||
// }, | ||
// Spec: models.KlusterSpec{Name: "nase"}, | ||
//} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package rest | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/go-openapi/errors" | ||
runtime "github.com/go-openapi/runtime" | ||
"github.com/golang/glog" | ||
|
||
apipkg "github.com/sapcc/kubernikus/pkg/api" | ||
"github.com/sapcc/kubernikus/pkg/api/handlers" | ||
"github.com/sapcc/kubernikus/pkg/api/rest/operations" | ||
) | ||
|
||
func Configure(api *operations.KubernikusAPI, rt *apipkg.Runtime) { | ||
// configure the api here | ||
api.ServeError = errors.ServeError | ||
|
||
// Set your custom logger if needed. Default one is log.Printf | ||
// Expected interface func(string, ...interface{}) | ||
// | ||
// Example: | ||
api.Logger = func(msg string, args ...interface{}) { | ||
glog.InfoDepth(2, fmt.Sprintf(msg, args...)) | ||
} | ||
|
||
api.JSONConsumer = runtime.JSONConsumer() | ||
api.JSONProducer = runtime.JSONProducer() | ||
|
||
// Applies when the "x-auth-token" header is set | ||
api.KeystoneAuth = keystoneAuth() | ||
|
||
// Set your custom authorizer if needed. Default one is security.Authorized() | ||
// api.APIAuthorizer = security.Authorized() | ||
|
||
api.InfoHandler = handlers.NewInfo(rt) | ||
api.ListAPIVersionsHandler = handlers.NewListAPIVersions(rt) | ||
api.ListClustersHandler = handlers.NewListClusters(rt) | ||
api.CreateClusterHandler = handlers.NewCreateCluster(rt) | ||
api.ShowClusterHandler = handlers.NewShowCluster(rt) | ||
api.TerminateClusterHandler = handlers.NewTerminateCluster(rt) | ||
api.UpdateClusterHandler = handlers.NewUpdateCluster(rt) | ||
api.GetClusterCredentialsHandler = handlers.NewGetClusterCredentials(rt) | ||
api.GetClusterInfoHandler = handlers.NewGetClusterInfo(rt) | ||
|
||
api.ServerShutdown = func() {} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// +k8s:deepcopy-gen=package,register | ||
//+groupName=kubernikus.sap.cc | ||
package v1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.