Skip to content

Commit

Permalink
e2e test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
jessepeterson committed Oct 14, 2024
1 parent 9be5af5 commit 06466a8
Show file tree
Hide file tree
Showing 32 changed files with 1,193 additions and 520 deletions.
12 changes: 9 additions & 3 deletions http/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,12 @@ func PushHandler(pusher push.Pusher, logger log.Logger) http.HandlerFunc {
// using. Also note we expose Go errors to the output as this is meant
// for "API" users.
func RawCommandEnqueueHandler(enqueuer storage.CommandEnqueuer, pusher push.Pusher, logger log.Logger) http.HandlerFunc {
if enqueuer == nil {
panic("nil enqueuer")
}
if logger == nil {
panic("nil logger")
}
return func(w http.ResponseWriter, r *http.Request) {
ids := strings.Split(r.URL.Path, ",")
ctx, logger := setupCtxLog(r.Context(), ids, logger)
Expand Down Expand Up @@ -201,14 +207,14 @@ func RawCommandEnqueueHandler(enqueuer storage.CommandEnqueuer, pusher push.Push
// optionally send pushes
pushResp := make(map[string]*push.Response)
var pushErr error
if !nopush {
if !nopush && pusher != nil {
pushResp, pushErr = pusher.Push(ctx, ids)
if err != nil {
logger.Info("msg", "push", "err", err)
output.PushError = err.Error()
}
} else {
pushErr = nil
} else if !nopush && pusher == nil {
pushErr = errors.New("nil pusher")
}
// loop through our push errors, if any, and add to output
var pushCt, pushErrCt int
Expand Down
6 changes: 6 additions & 0 deletions http/mdm/mdm.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ func mdmReqFromHTTPReq(r *http.Request) *mdm.Request {

// CheckinHandler decodes an MDM check-in request and adapts it to service.
func CheckinHandler(svc service.Checkin, logger log.Logger) http.HandlerFunc {
if svc == nil {
panic("nil service")
}
if logger == nil {
panic("nil logger")
}
return func(w http.ResponseWriter, r *http.Request) {
logger := ctxlog.Logger(r.Context(), logger)
bodyBytes, err := mdmhttp.ReadAllAndReplaceBody(r)
Expand Down
2 changes: 1 addition & 1 deletion mdm/checkin.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type Authenticate struct {

// Fields that may be present but are not strictly required for the
// operation of the MDM protocol. Nice-to-haves.
SerialNumber string
SerialNumber string `plist:",omitempty"`
}

type b64Data []byte
Expand Down
7 changes: 3 additions & 4 deletions mdm/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@ type ErrorChain struct {
// See https://developer.apple.com/documentation/devicemanagement/implementing_device_management/sending_mdm_commands_to_a_device
type CommandResults struct {
Enrollment
CommandUUID string
CommandUUID string `plist:",omitempty"`
Status string
ErrorChain []ErrorChain
RequestType string
Raw []byte `plist:"-"` // Original command result XML plist
ErrorChain []ErrorChain `plist:",omitempty"`
Raw []byte `plist:"-"` // Original command result XML plist
}

// DecodeCheckin unmarshals rawMessage into results
Expand Down
5 changes: 0 additions & 5 deletions mdm/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@ func TestCommandAndReportResults(t *testing.T) {
for _, test := range []struct {
filename string
UDID string
RequestType string
Status string
CommandUUID string
}{
{
"testdata/DeviceInformation.1.plist",
"66ADE930-5FDF-5EC4-8429-15640684C489",
"DeviceInformation",
"Acknowledged",
"76eda240-5488-4989-8339-f2ae160113c4",
},
Expand All @@ -36,9 +34,6 @@ func TestCommandAndReportResults(t *testing.T) {
if msg, have, want := "incorrect UDID", a.UDID, test.UDID; have != want {
t.Errorf("%s: %q, want: %q", msg, have, want)
}
if msg, have, want := "incorrect RequestType", a.RequestType, test.RequestType; have != want {
t.Errorf("%s: %q, want: %q", msg, have, want)
}
if msg, have, want := "incorrect Status", a.Status, test.Status; have != want {
t.Errorf("%s: %q, want: %q", msg, have, want)
}
Expand Down
13 changes: 7 additions & 6 deletions service/certauth/certauth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/micromdm/nanomdm/mdm"
"github.com/micromdm/nanomdm/storage/file"
"github.com/micromdm/nanomdm/test"
)

func loadAuthMsg() (*mdm.Authenticate, error) {
Expand Down Expand Up @@ -61,15 +62,15 @@ func TestNilCertAuth(t *testing.T) {
}

func TestCertAuth(t *testing.T) {
_, crt, err := SimpleSelfSignedRSAKeypair("TESTDEVICE", 1)
_, crt, err := test.SimpleSelfSignedRSAKeypair("TESTDEVICE", 1)
if err != nil {
t.Fatal(err)
}
storage, err := file.New("test-db")
if err != nil {
t.Fatal(err)
}
certAuth := New(&NopService{}, storage)
certAuth := New(&test.NopService{}, storage)
if certAuth == nil {
t.Fatal("New returned nil")
}
Expand Down Expand Up @@ -109,7 +110,7 @@ func TestCertAuth(t *testing.T) {
if err != nil {
t.Fatal(err)
}
_, crt2, err := SimpleSelfSignedRSAKeypair("TESTDEVICE", 2)
_, crt2, err := test.SimpleSelfSignedRSAKeypair("TESTDEVICE", 2)
if err != nil {
t.Fatal(err)
}
Expand All @@ -125,15 +126,15 @@ func TestCertAuth(t *testing.T) {
}

func TestCertAuthRetro(t *testing.T) {
_, crt, err := SimpleSelfSignedRSAKeypair("TESTDEVICE", 1)
_, crt, err := test.SimpleSelfSignedRSAKeypair("TESTDEVICE", 1)
if err != nil {
t.Fatal(err)
}
storage, err := file.New("test-db")
if err != nil {
t.Fatal(err)
}
certAuth := New(&NopService{}, storage, WithAllowRetroactive())
certAuth := New(&test.NopService{}, storage, WithAllowRetroactive())
if certAuth == nil {
t.Fatal("New returned nil")
}
Expand All @@ -153,7 +154,7 @@ func TestCertAuthRetro(t *testing.T) {
if err != nil {
t.Fatal(err)
}
_, crt2, err := SimpleSelfSignedRSAKeypair("TESTDEVICE", 2)
_, crt2, err := test.SimpleSelfSignedRSAKeypair("TESTDEVICE", 2)
if err != nil {
t.Fatal(err)
}
Expand Down
7 changes: 6 additions & 1 deletion storage/file/bstoken.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,15 @@ func (s *FileStorage) StoreBootstrapToken(r *mdm.Request, msg *mdm.SetBootstrapT
return nil
}

// RetrieveBootstrapToken reads the BootstrapToken from disk and returns it.
// If no token yet exists a nil token and no error are returned.
func (s *FileStorage) RetrieveBootstrapToken(r *mdm.Request, _ *mdm.GetBootstrapToken) (*mdm.BootstrapToken, error) {
e := s.newEnrollment(r.ID)
bsTokenRaw, err := e.readFile(BootstrapTokenFile)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
// mute the error if we haven't escrowed a token yet.
return nil, nil
} else if err != nil {
return nil, err
}
bsToken := &mdm.BootstrapToken{
Expand Down
5 changes: 2 additions & 3 deletions storage/file/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"testing"

"github.com/micromdm/nanomdm/storage/test"
"github.com/micromdm/nanomdm/test/e2e"
)

func TestFileStorage(t *testing.T) {
Expand All @@ -13,6 +13,5 @@ func TestFileStorage(t *testing.T) {
t.Fatal(err)
}

test.TestQueue(t, "EA4E19F1-7F8B-493D-BEAB-264B33BCF4E6", s)
test.TestRetrievePushInfo(t, context.Background(), s)
t.Run("e2e", func(t *testing.T) { e2e.TestE2E(t, context.Background(), s) })
}
59 changes: 0 additions & 59 deletions storage/mysql/bstoken_test.go

This file was deleted.

86 changes: 0 additions & 86 deletions storage/mysql/device_test.go

This file was deleted.

31 changes: 31 additions & 0 deletions storage/mysql/mysql_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package mysql

import (
"context"
"os"
"testing"

_ "github.com/go-sql-driver/mysql"
"github.com/micromdm/nanomdm/test/e2e"
)

func TestMySQL(t *testing.T) {
testDSN := os.Getenv("NANOMDM_MYSQL_STORAGE_TEST_DSN")
if testDSN == "" {
t.Skip("NANOMDM_MYSQL_STORAGE_TEST_DSN not set")
}

s, err := New(WithDSN(testDSN), WithDeleteCommands())
if err != nil {
t.Fatal(err)
}

t.Run("e2e-WithDeleteCommands()", func(t *testing.T) { e2e.TestE2E(t, context.Background(), s) })

s, err = New(WithDSN(testDSN))
if err != nil {
t.Fatal(err)
}

t.Run("e2e", func(t *testing.T) { e2e.TestE2E(t, context.Background(), s) })
}
23 changes: 0 additions & 23 deletions storage/mysql/push_test.go

This file was deleted.

Loading

0 comments on commit 06466a8

Please sign in to comment.