Files
matchbox/api/pixiecore_test.go
Dalton Hubble ccd3c7a839 api: Associate machines' attribute labels to specs by group matchers
* Use YAML group config to match machine labels to groups of zero or
more machines sharing a Specification
* Discontinue using machine.json resources representations which are
repetitive, don't support groups, and don't allow label matching
* Previous "default" special case now handled by a Group with an empty
matcher
2016-01-07 23:24:05 -08:00

46 lines
1.3 KiB
Go

package api
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
func TestPixiecoreHandler(t *testing.T) {
store := &fixedStore{
Groups: []Group{testGroupWithMAC},
Specs: map[string]*Spec{testGroupWithMAC.Spec: testSpec},
}
h := pixiecoreHandler(store)
req, _ := http.NewRequest("GET", "/"+validMACStr, nil)
w := httptest.NewRecorder()
h.ServeHTTP(w, req)
// assert that:
// - machine 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) {
store := &emptyStore{}
h := pixiecoreHandler(store)
req, _ := http.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
h.ServeHTTP(w, req)
assert.Equal(t, http.StatusBadRequest, w.Code)
assert.Equal(t, "invalid MAC address /\n", w.Body.String())
}
func TestPixiecoreHandler_NoMatchingSpec(t *testing.T) {
store := &emptyStore{}
h := pixiecoreHandler(store)
req, _ := http.NewRequest("GET", "/"+validMACStr, nil)
w := httptest.NewRecorder()
h.ServeHTTP(w, req)
assert.Equal(t, http.StatusNotFound, w.Code)
}