-
Notifications
You must be signed in to change notification settings - Fork 300
fleetd: support dynamic metadata #1642
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,22 +19,29 @@ import ( | |
| "net/http/httptest" | ||
| "reflect" | ||
| "strconv" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/coreos/fleet/client" | ||
| "github.com/coreos/fleet/machine" | ||
| "github.com/coreos/fleet/registry" | ||
| ) | ||
|
|
||
| func TestMachinesList(t *testing.T) { | ||
| func fakeMachinesSetup() (*machinesResource, *httptest.ResponseRecorder) { | ||
| fr := registry.NewFakeRegistry() | ||
| fr.SetMachines([]machine.MachineState{ | ||
| {ID: "XXX", PublicIP: "", Metadata: nil}, | ||
| {ID: "XXX", PublicIP: "", Metadata: map[string]string{}}, | ||
| {ID: "YYY", PublicIP: "1.2.3.4", Metadata: map[string]string{"ping": "pong"}}, | ||
| }) | ||
| fAPI := &client.RegistryClient{Registry: fr} | ||
| resource := &machinesResource{cAPI: fAPI, tokenLimit: testTokenLimit} | ||
| rw := httptest.NewRecorder() | ||
|
|
||
| return resource, rw | ||
| } | ||
|
|
||
| func TestMachinesList(t *testing.T) { | ||
| resource, rw := fakeMachinesSetup() | ||
| req, err := http.NewRequest("GET", "http://example.com", nil) | ||
| if err != nil { | ||
| t.Fatalf("Failed creating http.Request: %v", err) | ||
|
|
@@ -136,3 +143,126 @@ func TestExtractMachinePage(t *testing.T) { | |
| } | ||
| } | ||
| } | ||
|
|
||
| func TestMachinesPatchAddModify(t *testing.T) { | ||
| reqBody := ` | ||
| [{"op": "add", "path": "/XXX/metadata/foo", "value": "bar"}, | ||
| {"op": "replace", "path": "/YYY/metadata/ping", "value": "splat"}] | ||
| ` | ||
|
|
||
| resource, rw := fakeMachinesSetup() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Build of unit test fails at this line as well as other 4 other call sites, because
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oops, will fix that.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've modified There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Great. Unit test builds now without error. Thanks! |
||
| req, err := http.NewRequest("PATCH", "http://example.com/machines", strings.NewReader(reqBody)) | ||
| if err != nil { | ||
| t.Fatalf("Failed creating http.Request: %v", err) | ||
| } | ||
|
|
||
| resource.ServeHTTP(rw, req) | ||
| if rw.Code != http.StatusNoContent { | ||
| t.Errorf("Expected 204, got %d", rw.Code) | ||
| } | ||
|
|
||
| // fetch machine to make sure data has been added | ||
| req, err = http.NewRequest("GET", "http://example.com/machines", nil) | ||
| if err != nil { | ||
| t.Fatalf("Failed creating http.Request: %v", err) | ||
| } | ||
| rw.Body.Reset() | ||
| resource.ServeHTTP(rw, req) | ||
|
|
||
| if rw.Body == nil { | ||
| t.Error("Received nil response body") | ||
| } else { | ||
| body := rw.Body.String() | ||
| expected := `{"machines":[{"id":"XXX","metadata":{"foo":"bar"}},{"id":"YYY","metadata":{"ping":"splat"},"primaryIP":"1.2.3.4"}]}` | ||
| if body != expected { | ||
| t.Errorf("Expected body:\n%s\n\nReceived body:\n%s\n", expected, body) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestMachinesPatchDelete(t *testing.T) { | ||
| reqBody := ` | ||
| [{"op": "remove", "path": "/XXX/metadata/foo"}, | ||
| {"op": "remove", "path": "/YYY/metadata/ping"}] | ||
| ` | ||
|
|
||
| resource, rw := fakeMachinesSetup() | ||
| req, err := http.NewRequest("PATCH", "http://example.com/machines", strings.NewReader(reqBody)) | ||
| if err != nil { | ||
| t.Fatalf("Failed creating http.Request: %v", err) | ||
| } | ||
|
|
||
| resource.ServeHTTP(rw, req) | ||
| if rw.Code != http.StatusNoContent { | ||
| t.Errorf("Expected 204, got %d", rw.Code) | ||
| } | ||
|
|
||
| // fetch machine to make sure data has been added | ||
| req, err = http.NewRequest("GET", "http://example.com/machines", nil) | ||
| if err != nil { | ||
| t.Fatalf("Failed creating http.Request: %v", err) | ||
| } | ||
| rw.Body.Reset() | ||
| resource.ServeHTTP(rw, req) | ||
|
|
||
| if rw.Body == nil { | ||
| t.Error("Received nil response body") | ||
| } else { | ||
| body := rw.Body.String() | ||
| expected := `{"machines":[{"id":"XXX"},{"id":"YYY","primaryIP":"1.2.3.4"}]}` | ||
| if body != expected { | ||
| t.Errorf("Expected body:\n%s\n\nReceived body:\n%s\n", expected, body) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestMachinesPatchBadOp(t *testing.T) { | ||
| reqBody := ` | ||
| [{"op": "noop", "path": "/XXX/metadata/foo", "value": "bar"}] | ||
| ` | ||
|
|
||
| resource, rw := fakeMachinesSetup() | ||
| req, err := http.NewRequest("PATCH", "http://example.com/machines", strings.NewReader(reqBody)) | ||
| if err != nil { | ||
| t.Fatalf("Failed creating http.Request: %v", err) | ||
| } | ||
|
|
||
| resource.ServeHTTP(rw, req) | ||
| if rw.Code != http.StatusBadRequest { | ||
| t.Errorf("Expected 400, got %d", rw.Code) | ||
| } | ||
| } | ||
|
|
||
| func TestMachinesPatchBadPath(t *testing.T) { | ||
| reqBody := ` | ||
| [{"op": "add", "path": "/XXX/foo", "value": "bar"}] | ||
| ` | ||
|
|
||
| resource, rw := fakeMachinesSetup() | ||
| req, err := http.NewRequest("PATCH", "http://example.com/machines", strings.NewReader(reqBody)) | ||
| if err != nil { | ||
| t.Fatalf("Failed creating http.Request: %v", err) | ||
| } | ||
|
|
||
| resource.ServeHTTP(rw, req) | ||
| if rw.Code != http.StatusBadRequest { | ||
| t.Errorf("Expected 400, got %d", rw.Code) | ||
| } | ||
| } | ||
|
|
||
| func TestMachinesPatchBadValue(t *testing.T) { | ||
| reqBody := ` | ||
| [{"op": "add", "path": "/XXX/foo"}] | ||
| ` | ||
|
|
||
| resource, rw := fakeMachinesSetup() | ||
| req, err := http.NewRequest("PATCH", "http://example.com/machines", strings.NewReader(reqBody)) | ||
| if err != nil { | ||
| t.Fatalf("Failed creating http.Request: %v", err) | ||
| } | ||
|
|
||
| resource.ServeHTTP(rw, req) | ||
| if rw.Code != http.StatusBadRequest { | ||
| t.Errorf("Expected 400, got %d", rw.Code) | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In relation with the question I asked about a possible TTL feature, I think this structure should be made more future-proof.
In practice,
Valueshould be an object instead of astring.That gives the room to later implement such improvement for example: https://github.com/dalbani/fleet/commit/c0a305037a214351e62442f7d234865699e9520f#diff-82e778d9c582a0fad1db38076b55deaaR47