Files
matchbox/bootcfg/api/pixiecore_test.go
Dalton Hubble 0d148581b9 bootcfg/api: Switch from api types to storagepb types
* Remove api.Store and use storagepb.Store instead
* Remove api.Spec and use storagepb.Profile instead
* Switch from api.Group to storagepb.Group
* Move api.Group to config for YAML config decoding only
2016-03-10 18:32:57 -08:00

57 lines
1.9 KiB
Go

package api
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/coreos/coreos-baremetal/bootcfg/storage/storagepb"
"github.com/stretchr/testify/assert"
)
func TestPixiecoreHandler(t *testing.T) {
store := &fixedStore{
Groups: map[string]*storagepb.Group{testGroupWithMAC.Id: testGroupWithMAC},
Profiles: map[string]*storagepb.Profile{testGroupWithMAC.Profile: testProfile},
}
h := pixiecoreHandler(newGroupsResource(store), store)
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/"+validMACStr, nil)
h.ServeHTTP(w, req)
// assert that:
// - MAC address argument is used for Group matching
// - the Profile's NetBoot config is rendered as Pixiecore JSON
expectedJSON := `{"kernel":"/image/kernel","initrd":["/image/initrd_a","/image/initrd_b"],"cmdline":{"a":"b","c":""}}`
assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, jsonContentType, w.HeaderMap.Get(contentType))
assert.Equal(t, expectedJSON, w.Body.String())
}
func TestPixiecoreHandler_InvalidMACAddress(t *testing.T) {
h := pixiecoreHandler(&groupsResource{}, &emptyStore{})
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/", nil)
h.ServeHTTP(w, req)
assert.Equal(t, http.StatusBadRequest, w.Code)
assert.Equal(t, "invalid MAC address /\n", w.Body.String())
}
func TestPixiecoreHandler_NoMatchingGroup(t *testing.T) {
h := pixiecoreHandler(newGroupsResource(&emptyStore{}), &emptyStore{})
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/"+validMACStr, nil)
h.ServeHTTP(w, req)
assert.Equal(t, http.StatusNotFound, w.Code)
}
func TestPixiecoreHandler_NoMatchingProfile(t *testing.T) {
store := &fixedStore{
Groups: map[string]*storagepb.Group{testGroup.Id: testGroup},
}
h := pixiecoreHandler(newGroupsResource(store), &emptyStore{})
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/"+validMACStr, nil)
h.ServeHTTP(w, req)
assert.Equal(t, http.StatusNotFound, w.Code)
}