snapshot_create_repository_test.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Copyright 2012-present Oliver Eilhard. All rights reserved.
  2. // Use of this source code is governed by a MIT-license.
  3. // See http://olivere.mit-license.org/license.txt for details.
  4. package elastic
  5. import (
  6. "encoding/json"
  7. "testing"
  8. )
  9. func TestSnapshotPutRepositoryURL(t *testing.T) {
  10. client := setupTestClient(t)
  11. tests := []struct {
  12. Repository string
  13. Expected string
  14. }{
  15. {
  16. "repo",
  17. "/_snapshot/repo",
  18. },
  19. }
  20. for _, test := range tests {
  21. path, _, err := client.SnapshotCreateRepository(test.Repository).buildURL()
  22. if err != nil {
  23. t.Fatal(err)
  24. }
  25. if path != test.Expected {
  26. t.Errorf("expected %q; got: %q", test.Expected, path)
  27. }
  28. }
  29. }
  30. func TestSnapshotPutRepositoryBodyWithSettings(t *testing.T) {
  31. client := setupTestClient(t)
  32. service := client.SnapshotCreateRepository("my_backup")
  33. service = service.Type("fs").
  34. Settings(map[string]interface{}{
  35. "location": "my_backup_location",
  36. "compress": false,
  37. }).
  38. Setting("compress", true).
  39. Setting("chunk_size", 16*1024*1024)
  40. if err := service.Validate(); err != nil {
  41. t.Fatal(err)
  42. }
  43. src, err := service.buildBody()
  44. if err != nil {
  45. t.Fatal(err)
  46. }
  47. data, err := json.Marshal(src)
  48. if err != nil {
  49. t.Fatalf("marshaling to JSON failed: %v", err)
  50. }
  51. got := string(data)
  52. expected := `{"settings":{"chunk_size":16777216,"compress":true,"location":"my_backup_location"},"type":"fs"}`
  53. if got != expected {
  54. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  55. }
  56. }