mirror of
https://github.com/outbackdingo/matchbox.git
synced 2026-01-27 10:19:35 +00:00
* Match machines to their Ignition template file, render the template, parse as JSON or YAML, and serve as Ignition JSON * Existing JSON ignition configs should still work * Remove the Ignition YAML -> JSON transform script which used fuze
59 lines
1.5 KiB
Go
59 lines
1.5 KiB
Go
package api
|
|
|
|
import (
|
|
"bufio"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"golang.org/x/net/context"
|
|
)
|
|
|
|
func TestMetadataHandler(t *testing.T) {
|
|
h := metadataHandler()
|
|
ctx := withGroup(context.Background(), &testGroup)
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("GET", "/?uuid=a1b2c3d4&mac="+nonNormalizedMACStr, nil)
|
|
h.ServeHTTP(ctx, w, req)
|
|
// assert that:
|
|
// - the Group's custom metadata is
|
|
// - query argument attributes are added to the metadata
|
|
// - key names are upper case
|
|
expectedData := map[string]string{
|
|
"K8S_VERSION": "v1.1.2",
|
|
"POD_NETWORK": "10.2.0.0/16",
|
|
"SERVICE_NAME": "etcd2",
|
|
"UUID": "a1b2c3d4",
|
|
"MAC": validMACStr,
|
|
}
|
|
assert.Equal(t, http.StatusOK, w.Code)
|
|
assert.Equal(t, expectedData, metadataToMap(w.Body.String()))
|
|
assert.Equal(t, plainContentType, w.HeaderMap.Get(contentType))
|
|
}
|
|
|
|
func TestMetadataHandler_MissingCtxGroup(t *testing.T) {
|
|
h := metadataHandler()
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("GET", "/", nil)
|
|
h.ServeHTTP(context.Background(), w, req)
|
|
assert.Equal(t, http.StatusNotFound, w.Code)
|
|
}
|
|
|
|
// metadataToMap converts a KEY=val\nKEY=val ResponseWriter body to a map for
|
|
// testing purposes.
|
|
func metadataToMap(metadata string) map[string]string {
|
|
scanner := bufio.NewScanner(strings.NewReader(metadata))
|
|
data := make(map[string]string)
|
|
for scanner.Scan() {
|
|
token := scanner.Text()
|
|
pair := strings.SplitN(token, "=", 2)
|
|
if len(pair) != 2 {
|
|
continue
|
|
}
|
|
data[pair[0]] = pair[1]
|
|
}
|
|
return data
|
|
}
|