-
Notifications
You must be signed in to change notification settings - Fork 23
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
9 changed files
with
224 additions
and
27 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
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
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 |
---|---|---|
@@ -1,19 +1,122 @@ | ||
package api | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
"time" | ||
|
||
"github.com/anthdm/raptor/internal/shared" | ||
"github.com/anthdm/raptor/internal/storage" | ||
"github.com/anthdm/raptor/internal/types" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestCreateEndpoint(t *testing.T) { | ||
s := createServer() | ||
|
||
name := "My endpoint" | ||
runtime := "go" | ||
environment := map[string]string{"FOO": "BAR"} | ||
|
||
params := CreateEndpointParams{ | ||
Name: name, | ||
Runtime: runtime, | ||
Environment: environment, | ||
} | ||
b, err := json.Marshal(params) | ||
require.Nil(t, err) | ||
|
||
req := httptest.NewRequest("POST", "/endpoint", bytes.NewReader(b)) | ||
resp := httptest.NewRecorder() | ||
s.router.ServeHTTP(resp, req) | ||
|
||
var endpoint types.Endpoint | ||
err = json.NewDecoder(resp.Body).Decode(&endpoint) | ||
require.Nil(t, err) | ||
|
||
require.Equal(t, http.StatusOK, resp.Result().StatusCode) | ||
require.Equal(t, name, endpoint.Name) | ||
require.Equal(t, runtime, endpoint.Runtime) | ||
require.Equal(t, environment, endpoint.Environment) | ||
require.True(t, shared.IsZeroUUID(endpoint.ActiveDeploymentID)) | ||
} | ||
|
||
func TestGetEndpoint(t *testing.T) { | ||
s := createServer() | ||
endpoint := seedEndpoint(t, s) | ||
|
||
req := httptest.NewRequest("GET", "/endpoint/"+endpoint.ID.String(), nil) | ||
resp := httptest.NewRecorder() | ||
s.router.ServeHTTP(resp, req) | ||
|
||
require.Equal(t, http.StatusOK, resp.Result().StatusCode) | ||
|
||
var other types.Endpoint | ||
err := json.NewDecoder(resp.Body).Decode(&other) | ||
require.Nil(t, err) | ||
endpoint.CreatedAT = time.Time{} | ||
other.CreatedAT = time.Time{} | ||
require.Equal(t, *endpoint, other) | ||
} | ||
|
||
func TestCreateDeploy(t *testing.T) { | ||
s := createServer() | ||
endpoint := seedEndpoint(t, s) | ||
|
||
req := httptest.NewRequest("POST", "/endpoint/"+endpoint.ID.String()+"/deployment", bytes.NewReader([]byte("a"))) | ||
req.Header.Set("content-type", "application/octet-stream") | ||
resp := httptest.NewRecorder() | ||
s.router.ServeHTTP(resp, req) | ||
|
||
require.Equal(t, http.StatusOK, resp.Result().StatusCode) | ||
|
||
var deploy types.Deployment | ||
require.Nil(t, json.NewDecoder(resp.Body).Decode(&deploy)) | ||
|
||
require.Equal(t, endpoint.ID, deploy.EndpointID) | ||
require.Equal(t, 32, len(deploy.Hash)) | ||
} | ||
|
||
func TestPublish(t *testing.T) { | ||
s := createServer() | ||
endpoint := seedEndpoint(t, s) | ||
deployment := types.NewDeployment(endpoint, []byte("somefakeblob")) | ||
|
||
require.Nil(t, s.store.CreateDeployment(deployment)) | ||
require.True(t, shared.IsZeroUUID(endpoint.ActiveDeploymentID)) | ||
|
||
params := PublishParams{ | ||
DeploymentID: deployment.ID, | ||
} | ||
b, err := json.Marshal(params) | ||
require.Nil(t, err) | ||
|
||
req := httptest.NewRequest("POST", "/publish", bytes.NewReader(b)) | ||
resp := httptest.NewRecorder() | ||
s.router.ServeHTTP(resp, req) | ||
|
||
var publishResp PublishResponse | ||
require.Nil(t, json.NewDecoder(resp.Body).Decode(&publishResp)) | ||
|
||
require.Equal(t, http.StatusOK, resp.Result().StatusCode) | ||
require.Equal(t, deployment.ID, endpoint.ActiveDeploymentID) | ||
require.Equal(t, deployment.ID, publishResp.DeploymentID) | ||
require.Equal(t, "http://0.0.0.0:80/live/"+endpoint.ID.String(), publishResp.URL) | ||
} | ||
|
||
func TestGetDeploy(t *testing.T) { | ||
func seedEndpoint(t *testing.T, s *Server) *types.Endpoint { | ||
e := types.NewEndpoint("My endpoint", "go", map[string]string{"FOO": "BAR"}) | ||
require.Nil(t, s.store.CreateEndpoint(e)) | ||
return e | ||
} | ||
|
||
func TestRollback(t *testing.T) {} | ||
func createServer() *Server { | ||
cache := storage.NewDefaultModCache() | ||
store := storage.NewMemoryStore() | ||
s := NewServer(store, store, cache) | ||
s.initRouter() | ||
return s | ||
} |
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,82 @@ | ||
package storage | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
|
||
"github.com/anthdm/raptor/internal/types" | ||
"github.com/google/uuid" | ||
) | ||
|
||
type MemoryStore struct { | ||
mu sync.RWMutex | ||
endpoints map[uuid.UUID]*types.Endpoint | ||
deploys map[uuid.UUID]*types.Deployment | ||
} | ||
|
||
func NewMemoryStore() *MemoryStore { | ||
return &MemoryStore{ | ||
endpoints: make(map[uuid.UUID]*types.Endpoint), | ||
deploys: make(map[uuid.UUID]*types.Deployment), | ||
} | ||
} | ||
|
||
func (s *MemoryStore) CreateEndpoint(e *types.Endpoint) error { | ||
s.mu.Lock() | ||
defer s.mu.Unlock() | ||
s.endpoints[e.ID] = e | ||
return nil | ||
} | ||
|
||
func (s *MemoryStore) GetEndpoint(id uuid.UUID) (*types.Endpoint, error) { | ||
s.mu.RLock() | ||
defer s.mu.RUnlock() | ||
e, ok := s.endpoints[id] | ||
if !ok { | ||
return nil, fmt.Errorf("could not find endpoint with id (%s)", id) | ||
} | ||
return e, nil | ||
} | ||
|
||
func (s *MemoryStore) UpdateEndpoint(id uuid.UUID, params UpdateEndpointParams) error { | ||
endpoint, err := s.GetEndpoint(id) | ||
if err != nil { | ||
return err | ||
} | ||
s.mu.Lock() | ||
defer s.mu.Unlock() | ||
if params.ActiveDeployID.String() != "00000000-0000-0000-0000-000000000000" { | ||
endpoint.ActiveDeploymentID = params.ActiveDeployID | ||
} | ||
if params.Environment != nil { | ||
for key, val := range params.Environment { | ||
endpoint.Environment[key] = val | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (s *MemoryStore) CreateDeployment(deploy *types.Deployment) error { | ||
s.mu.Lock() | ||
defer s.mu.Unlock() | ||
s.deploys[deploy.ID] = deploy | ||
return nil | ||
} | ||
|
||
func (s *MemoryStore) GetDeployment(id uuid.UUID) (*types.Deployment, error) { | ||
s.mu.RLock() | ||
defer s.mu.RUnlock() | ||
deploy, ok := s.deploys[id] | ||
if !ok { | ||
return nil, fmt.Errorf("could not find deployment with id (%s)", id) | ||
} | ||
return deploy, nil | ||
} | ||
|
||
func (s *MemoryStore) CreateRuntimeMetric(_ *types.RuntimeMetric) error { | ||
return nil | ||
} | ||
|
||
func (s *MemoryStore) GetRuntimeMetrics(_ uuid.UUID) ([]types.RuntimeMetric, error) { | ||
return nil, nil | ||
} |
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