Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor refactoring (nicer lib usage) #1081

Merged
merged 6 commits into from
Feb 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions server/handlers/changefeed.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,23 @@ func Changefeed(ctx context.Context, w http.ResponseWriter, r *http.Request) err
vars = mux.Vars(r)
logger = ctxu.GetLogger(ctx)
qs = r.URL.Query()
imageName = vars["imageName"]
gun = vars["gun"]
changeID = qs.Get("change_id")
store, records, err = checkChangefeedInputs(logger, ctx.Value(notary.CtxKeyMetaStore), qs.Get("records"))
)
if err != nil {
// err already logged and in correct format.
return err
}
out, err := changefeed(logger, store, imageName, changeID, records)
out, err := changefeed(logger, store, gun, changeID, records)
if err == nil {
w.Write(out)
}
return err
}

func changefeed(logger ctxu.Logger, store storage.MetaStore, imageName, changeID string, records int64) ([]byte, error) {
changes, err := store.GetChanges(changeID, int(records), imageName)
func changefeed(logger ctxu.Logger, store storage.MetaStore, gun, changeID string, records int64) ([]byte, error) {
changes, err := store.GetChanges(changeID, int(records), gun)
if err != nil {
logger.Errorf("%d GET could not retrieve records: %s", http.StatusInternalServerError, err.Error())
return nil, errors.ErrUnknown.WithDetail(err)
Expand Down
32 changes: 16 additions & 16 deletions server/handlers/changefeed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import (
)

type changefeedArgs struct {
logger ctxu.Logger
store storage.MetaStore
imageName string
changeID string
pageSize int64
logger ctxu.Logger
store storage.MetaStore
gun string
changeID string
pageSize int64
}

type changefeedTest struct {
Expand All @@ -33,23 +33,23 @@ func Test_changefeed(t *testing.T) {
{
name: "Empty Store",
args: changefeedArgs{
logger: logrus.New(),
store: s,
imageName: "",
changeID: "0",
pageSize: notary.DefaultPageSize,
logger: logrus.New(),
store: s,
gun: "",
changeID: "0",
pageSize: notary.DefaultPageSize,
},
want: []byte("{\"count\":0,\"records\":null}"),
wantErr: false,
},
{
name: "Bad ChangeID",
args: changefeedArgs{
logger: logrus.New(),
store: s,
imageName: "",
changeID: "not_a_number",
pageSize: notary.DefaultPageSize,
logger: logrus.New(),
store: s,
gun: "",
changeID: "not_a_number",
pageSize: notary.DefaultPageSize,
},
want: nil,
wantErr: true,
Expand All @@ -61,7 +61,7 @@ func Test_changefeed(t *testing.T) {

func runChangefeedTests(t *testing.T, tests []changefeedTest) {
for _, tt := range tests {
got, err := changefeed(tt.args.logger, tt.args.store, tt.args.imageName, tt.args.changeID, tt.args.pageSize)
got, err := changefeed(tt.args.logger, tt.args.store, tt.args.gun, tt.args.changeID, tt.args.pageSize)
if tt.wantErr {
require.Error(t, err,
"%q. changefeed() error = %v, wantErr %v", tt.name, err, tt.wantErr)
Expand Down
8 changes: 4 additions & 4 deletions server/handlers/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func AtomicUpdateHandler(ctx context.Context, w http.ResponseWriter, r *http.Req
}

func atomicUpdateHandler(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
gun := data.GUN(vars["imageName"])
gun := data.GUN(vars["gun"])
s := ctx.Value(notary.CtxKeyMetaStore)
logger := ctxu.GetLoggerWithField(ctx, gun, "gun")
store, ok := s.(storage.MetaStore)
Expand Down Expand Up @@ -125,7 +125,7 @@ func GetHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) err
}

func getHandler(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
gun := data.GUN(vars["imageName"])
gun := data.GUN(vars["gun"])
checksum := vars["checksum"]
version := vars["version"]
tufRole := vars["tufRole"]
Expand Down Expand Up @@ -161,7 +161,7 @@ func getHandler(ctx context.Context, w http.ResponseWriter, r *http.Request, var
// DeleteHandler deletes all data for a GUN. A 200 responses indicates success.
func DeleteHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
vars := mux.Vars(r)
gun := data.GUN(vars["imageName"])
gun := data.GUN(vars["gun"])
logger := ctxu.GetLoggerWithField(ctx, gun, "gun")
s := ctx.Value(notary.CtxKeyMetaStore)
store, ok := s.(storage.MetaStore)
Expand Down Expand Up @@ -256,7 +256,7 @@ func rotateKeyHandler(ctx context.Context, w http.ResponseWriter, r *http.Reques

// To be called before getKeyHandler or rotateKeyHandler
func setupKeyHandler(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string, actionVerb string) (data.RoleName, data.GUN, string, storage.MetaStore, signed.CryptoService, error) {
gun := data.GUN(vars["imageName"])
gun := data.GUN(vars["gun"])
logger := ctxu.GetLoggerWithField(ctx, gun, "gun")
if gun == "" {
logger.Infof("400 %s no gun in request", actionVerb)
Expand Down
48 changes: 24 additions & 24 deletions server/handlers/default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ func TestKeyHandlersInvalidConfiguration(t *testing.T) {
}

vars := map[string]string{
"imageName": "gun",
"tufRole": data.CanonicalTimestampRole.String(),
"gun": "gun",
"tufRole": data.CanonicalTimestampRole.String(),
}
req := &http.Request{Body: ioutil.NopCloser(bytes.NewBuffer(nil))}
for _, keyHandler := range []simplerHandler{getKeyHandler, rotateKeyHandler} {
Expand All @@ -121,16 +121,16 @@ func TestKeyHandlersInvalidConfiguration(t *testing.T) {
}
}

// GetKeyHandler and RotateKeyHandler need to be set up such that an imageName and tufRole are both
// GetKeyHandler and RotateKeyHandler need to be set up such that an gun and tufRole are both
// provided and non-empty.
func TestKeyHandlersNoRoleOrRepo(t *testing.T) {
state := defaultState()
req := &http.Request{Body: ioutil.NopCloser(bytes.NewBuffer(nil))}
for _, keyHandler := range []simplerHandler{getKeyHandler, rotateKeyHandler} {
for _, key := range []string{"imageName", "tufRole"} {
for _, key := range []string{"gun", "tufRole"} {
vars := map[string]string{
"imageName": "gun",
"tufRole": data.CanonicalTimestampRole.String(),
"gun": "gun",
"tufRole": data.CanonicalTimestampRole.String(),
}

// not provided
Expand All @@ -154,8 +154,8 @@ func TestKeyHandlersInvalidRole(t *testing.T) {
for _, keyHandler := range []simplerHandler{getKeyHandler, rotateKeyHandler} {
for _, role := range []string{data.CanonicalRootRole.String(), data.CanonicalTargetsRole.String(), "targets/a", "invalidrole"} {
vars := map[string]string{
"imageName": "gun",
"tufRole": role,
"gun": "gun",
"tufRole": role,
}
req := &http.Request{Body: ioutil.NopCloser(bytes.NewBuffer(nil))}

Expand All @@ -173,7 +173,7 @@ func TestGetKeyHandlerCreatesOnce(t *testing.T) {
req := &http.Request{Body: ioutil.NopCloser(bytes.NewBuffer(nil))}

for _, role := range roles {
vars := map[string]string{"imageName": "gun", "tufRole": role}
vars := map[string]string{"gun": "gun", "tufRole": role}
recorder := httptest.NewRecorder()
err := getKeyHandler(getContext(state), recorder, req, vars)
require.NoError(t, err)
Expand All @@ -187,7 +187,7 @@ func TestKeyHandlersInvalidKeyAlgo(t *testing.T) {
req := &http.Request{Body: ioutil.NopCloser(bytes.NewBuffer(nil))}
for _, keyHandler := range []simplerHandler{getKeyHandler, rotateKeyHandler} {
for _, role := range roles {
vars := map[string]string{"imageName": "gun", "tufRole": role}
vars := map[string]string{"gun": "gun", "tufRole": role}
recorder := httptest.NewRecorder()
invalidKeyAlgoState := defaultState()
invalidKeyAlgoState.keyAlgo = "notactuallyakeyalgorithm"
Expand All @@ -204,7 +204,7 @@ func TestRotateKeyHandlerSuccessfulRotation(t *testing.T) {
req := &http.Request{Body: ioutil.NopCloser(bytes.NewBuffer(nil))}

for _, role := range roles {
vars := map[string]string{"imageName": "gun", "tufRole": role}
vars := map[string]string{"gun": "gun", "tufRole": role}
recorder := httptest.NewRecorder()
err := rotateKeyHandler(getContext(state), recorder, req, vars)
require.NoError(t, err)
Expand All @@ -231,8 +231,8 @@ func TestGetHandlerRoot(t *testing.T) {
}

vars := map[string]string{
"imageName": "gun",
"tufRole": "root",
"gun": "gun",
"tufRole": "root",
}

rw := httptest.NewRecorder()
Expand Down Expand Up @@ -275,8 +275,8 @@ func TestGetHandlerTimestamp(t *testing.T) {
}

vars := map[string]string{
"imageName": "gun",
"tufRole": "timestamp",
"gun": "gun",
"tufRole": "timestamp",
}

rw := httptest.NewRecorder()
Expand Down Expand Up @@ -312,8 +312,8 @@ func TestGetHandlerSnapshot(t *testing.T) {
}

vars := map[string]string{
"imageName": "gun",
"tufRole": "snapshot",
"gun": "gun",
"tufRole": "snapshot",
}

rw := httptest.NewRecorder()
Expand All @@ -333,8 +333,8 @@ func TestGetHandler404(t *testing.T) {
}

vars := map[string]string{
"imageName": "gun",
"tufRole": "root",
"gun": "gun",
"tufRole": "root",
}

rw := httptest.NewRecorder()
Expand All @@ -355,8 +355,8 @@ func TestGetHandlerNilData(t *testing.T) {
}

vars := map[string]string{
"imageName": "gun",
"tufRole": "root",
"gun": "gun",
"tufRole": "root",
}

rw := httptest.NewRecorder()
Expand All @@ -382,7 +382,7 @@ func TestGetHandlerNoStorage(t *testing.T) {
func TestAtomicUpdateValidationFailurePropagated(t *testing.T) {
metaStore := storage.NewMemStorage()
var gun data.GUN = "testGUN"
vars := map[string]string{"imageName": gun.String()}
vars := map[string]string{"gun": gun.String()}

repo, cs, err := testutils.EmptyRepo(gun)
require.NoError(t, err)
Expand Down Expand Up @@ -425,7 +425,7 @@ func (s *failStore) GetCurrent(_ data.GUN, _ data.RoleName) (*time.Time, []byte,
func TestAtomicUpdateNonValidationFailureNotPropagated(t *testing.T) {
metaStore := storage.NewMemStorage()
var gun data.GUN = "testGUN"
vars := map[string]string{"imageName": gun.String()}
vars := map[string]string{"gun": gun.String()}

repo, cs, err := testutils.EmptyRepo(gun)
require.NoError(t, err)
Expand Down Expand Up @@ -467,7 +467,7 @@ func (s *invalidVersionStore) UpdateMany(_ data.GUN, _ []storage.MetaUpdate) err
func TestAtomicUpdateVersionErrorPropagated(t *testing.T) {
metaStore := storage.NewMemStorage()
var gun data.GUN = "testGUN"
vars := map[string]string{"imageName": gun.String()}
vars := map[string]string{"gun": gun.String()}

repo, cs, err := testutils.EmptyRepo(gun)
require.NoError(t, err)
Expand Down
Loading