fake.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. Copyright © The ESO 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. https://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 fake
  14. import (
  15. "errors"
  16. "fmt"
  17. "math"
  18. "github.com/go-chef/chef"
  19. )
  20. const (
  21. CORRECTUSER = "correctUser"
  22. testitem = "item03"
  23. DatabagName = "databag01"
  24. )
  25. type ChefMockClient struct {
  26. getItem func(databagName string, databagItem string) (item chef.DataBagItem, err error)
  27. listItems func(name string) (data *chef.DataBagListResult, err error)
  28. getUser func(name string) (user chef.User, err error)
  29. }
  30. func (mc *ChefMockClient) GetItem(databagName, databagItem string) (item chef.DataBagItem, err error) {
  31. return mc.getItem(databagName, databagItem)
  32. }
  33. func (mc *ChefMockClient) ListItems(name string) (data *chef.DataBagListResult, err error) {
  34. return mc.listItems(name)
  35. }
  36. func (mc *ChefMockClient) Get(name string) (user chef.User, err error) {
  37. if name == CORRECTUSER {
  38. user = chef.User{
  39. UserName: name,
  40. }
  41. err = nil
  42. } else {
  43. user = chef.User{}
  44. err = errors.New("message")
  45. }
  46. return user, err
  47. }
  48. func (mc *ChefMockClient) WithItem(_, _ string, _ error) {
  49. if mc != nil {
  50. mc.getItem = func(dataBagName, databagItemName string) (item chef.DataBagItem, err error) {
  51. ret := make(map[string]any)
  52. switch {
  53. case dataBagName == DatabagName && databagItemName == "item01":
  54. jsonstring := `{"id":"` + dataBagName + `-` + databagItemName + `","some_key":"fe7f29ede349519a1","some_password":"dolphin_123zc","some_username":"testuser"}`
  55. ret[databagItemName] = jsonstring
  56. case dataBagName == "databag03" && databagItemName == testitem:
  57. jsonMap := make(map[string]string)
  58. jsonMap["id"] = testitem
  59. jsonMap["findProperty"] = "foundProperty"
  60. return jsonMap, nil
  61. case dataBagName == DatabagName && databagItemName == testitem:
  62. return math.Inf(1), nil
  63. default:
  64. str := "https://chef.com/organizations/dev/data/" + dataBagName + "/" + databagItemName + ": 404"
  65. return nil, errors.New(str)
  66. }
  67. return ret, nil
  68. }
  69. }
  70. }
  71. func (mc *ChefMockClient) WithListItems(_ string, _ error) {
  72. if mc != nil {
  73. mc.listItems = func(databagName string) (data *chef.DataBagListResult, err error) {
  74. ret := make(chef.DataBagListResult)
  75. if databagName == DatabagName {
  76. jsonstring := fmt.Sprintf("https://chef.com/organizations/dev/data/%s/item01", databagName)
  77. ret["item01"] = jsonstring
  78. } else {
  79. return nil, fmt.Errorf("data bag not found: %s", databagName)
  80. }
  81. return &ret, nil
  82. }
  83. }
  84. }
  85. func (mc *ChefMockClient) WithUser(_ string, _ error) {
  86. if mc != nil {
  87. mc.getUser = func(name string) (user chef.User, err error) {
  88. return chef.User{
  89. UserName: name,
  90. }, nil
  91. }
  92. }
  93. }