Skip to content

Commit

Permalink
Add mysql storage backend queue test based on shared queue test code.
Browse files Browse the repository at this point in the history
Note that this requires the hidden build flag `integration` as well as specifying a DSN flag for the test.
As well the target DSN needs to exist and have the NanoMDM SQL schema present.
You'll also need to clear those commands before every test.
  • Loading branch information
jessepeterson committed Apr 20, 2022
1 parent ca03a50 commit cbda10f
Showing 1 changed file with 100 additions and 0 deletions.
100 changes: 100 additions & 0 deletions storage/mysql/queue_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
//go:build integration

package mysql

import (
"context"
"errors"
"flag"
"io/ioutil"
"testing"

"github.com/micromdm/nanomdm/mdm"
"github.com/micromdm/nanomdm/storage/internal/test"

_ "github.com/go-sql-driver/mysql"
)

var flDSN = flag.String("dsn", "", "DSN of test MySQL instance")

func loadAuthMsg() (*mdm.Authenticate, error) {
b, err := ioutil.ReadFile("../../mdm/testdata/Authenticate.2.plist")
if err != nil {
return nil, err
}
r, err := mdm.DecodeCheckin(b)
if err != nil {
return nil, err
}
a, ok := r.(*mdm.Authenticate)
if !ok {
return nil, errors.New("not an Authenticate message")
}
return a, nil
}

func loadTokenMsg() (*mdm.TokenUpdate, error) {
b, err := ioutil.ReadFile("../../mdm/testdata/TokenUpdate.2.plist")
if err != nil {
return nil, err
}
r, err := mdm.DecodeCheckin(b)
if err != nil {
return nil, err
}
a, ok := r.(*mdm.TokenUpdate)
if !ok {
return nil, errors.New("not a TokenUpdate message")
}
return a, nil
}

const deviceUDID = "66ADE930-5FDF-5EC4-8429-15640684C489"

func newMdmReq() *mdm.Request {
return &mdm.Request{
Context: context.Background(),
EnrollID: &mdm.EnrollID{
Type: mdm.Device,
ID: deviceUDID,
},
}
}

func enrollTestDevice(storage *MySQLStorage) error {
authMsg, err := loadAuthMsg()
if err != nil {
return err
}
err = storage.StoreAuthenticate(newMdmReq(), authMsg)
if err != nil {
return err
}
tokenMsg, err := loadTokenMsg()
if err != nil {
return err
}
err = storage.StoreTokenUpdate(newMdmReq(), tokenMsg)
if err != nil {
return err
}
return nil
}

func TestQueue(t *testing.T) {
if *flDSN == "" {
t.Fatal("MySQL DSN flag not provided to test")
}

storage, err := New(WithDSN(*flDSN))
if err != nil {
t.Fatal(err)
}

err = enrollTestDevice(storage)
if err != nil {
t.Fatal(err)
}

test.TestQueue(t, deviceUDID, storage)
}

0 comments on commit cbda10f

Please sign in to comment.