store_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package store
  14. import (
  15. _ "crypto/sha256"
  16. "io/ioutil"
  17. "os"
  18. "testing"
  19. "gotest.tools/v3/assert"
  20. "gotest.tools/v3/assert/cmp"
  21. "github.com/docker/compose-cli/api/errdefs"
  22. )
  23. func testStore(t *testing.T) Store {
  24. d, err := ioutil.TempDir("", "store")
  25. assert.NilError(t, err)
  26. t.Cleanup(func() {
  27. _ = os.RemoveAll(d)
  28. })
  29. s, err := New(d)
  30. assert.NilError(t, err)
  31. return s
  32. }
  33. func TestCreate(t *testing.T) {
  34. s := testStore(t)
  35. err := s.Create("test", "test", "description", ContextMetadata{})
  36. assert.NilError(t, err)
  37. err = s.Create("test", "test", "descrsiption", ContextMetadata{})
  38. assert.Error(t, err, `context "test": already exists`)
  39. assert.Assert(t, errdefs.IsAlreadyExistsError(err))
  40. }
  41. func TestGetEndpoint(t *testing.T) {
  42. s := testStore(t)
  43. err := s.Create("aci", "aci", "description", AciContext{
  44. Location: "eu",
  45. })
  46. assert.NilError(t, err)
  47. var ctx AciContext
  48. err = s.GetEndpoint("aci", &ctx)
  49. assert.NilError(t, err)
  50. assert.Equal(t, ctx.Location, "eu")
  51. var localCtx LocalContext
  52. err = s.GetEndpoint("aci", &localCtx)
  53. assert.Error(t, err, "wrong context type")
  54. }
  55. func TestGetUnknown(t *testing.T) {
  56. s := testStore(t)
  57. meta, err := s.Get("unknown")
  58. assert.Assert(t, cmp.Nil(meta))
  59. assert.Error(t, err, `context "unknown": not found`)
  60. assert.Assert(t, errdefs.IsNotFoundError(err))
  61. }
  62. func TestGet(t *testing.T) {
  63. s := testStore(t)
  64. err := s.Create("test", "type", "description", ContextMetadata{})
  65. assert.NilError(t, err)
  66. meta, err := s.Get("test")
  67. assert.NilError(t, err)
  68. assert.Assert(t, meta != nil)
  69. var m DockerContext
  70. if meta != nil {
  71. m = *meta
  72. }
  73. assert.Equal(t, m.Name, "test")
  74. assert.Equal(t, m.Metadata.Description, "description")
  75. assert.Equal(t, m.Type(), "type")
  76. }
  77. func TestRemoveNotFound(t *testing.T) {
  78. s := testStore(t)
  79. err := s.Remove("notfound")
  80. assert.Error(t, err, `context "notfound": not found`)
  81. assert.Assert(t, errdefs.IsNotFoundError(err))
  82. }
  83. func TestRemove(t *testing.T) {
  84. s := testStore(t)
  85. err := s.Create("testremove", "type", "description", ContextMetadata{})
  86. assert.NilError(t, err)
  87. meta, err := s.Get("testremove")
  88. assert.NilError(t, err)
  89. assert.Assert(t, meta != nil)
  90. err = s.Remove("testremove")
  91. assert.NilError(t, err)
  92. meta, err = s.Get("testremove")
  93. assert.Error(t, err, `context "testremove": not found`)
  94. assert.Assert(t, cmp.Nil(meta))
  95. }