fake.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. */
  12. package fake
  13. import (
  14. "errors"
  15. "fmt"
  16. "math"
  17. "github.com/go-chef/chef"
  18. )
  19. const (
  20. CORRECTUSER = "correctUser"
  21. testitem = "item03"
  22. DatabagName = "databag01"
  23. )
  24. type ChefMockClient struct {
  25. getItem func(databagName string, databagItem string) (item chef.DataBagItem, err error)
  26. listItems func(name string) (data *chef.DataBagListResult, err error)
  27. getUser func(name string) (user chef.User, err error)
  28. }
  29. func (mc *ChefMockClient) GetItem(databagName, databagItem string) (item chef.DataBagItem, err error) {
  30. return mc.getItem(databagName, databagItem)
  31. }
  32. func (mc *ChefMockClient) ListItems(name string) (data *chef.DataBagListResult, err error) {
  33. return mc.listItems(name)
  34. }
  35. func (mc *ChefMockClient) Get(name string) (user chef.User, err error) {
  36. if name == CORRECTUSER {
  37. user = chef.User{
  38. UserName: name,
  39. }
  40. err = nil
  41. } else {
  42. user = chef.User{}
  43. err = errors.New("message")
  44. }
  45. return user, err
  46. }
  47. func (mc *ChefMockClient) WithItem(_, _ string, _ error) {
  48. if mc != nil {
  49. mc.getItem = func(dataBagName, databagItemName string) (item chef.DataBagItem, err error) {
  50. ret := make(map[string]any)
  51. switch {
  52. case dataBagName == DatabagName && databagItemName == "item01":
  53. jsonstring := `{"id":"` + dataBagName + `-` + databagItemName + `","some_key":"fe7f29ede349519a1","some_password":"dolphin_123zc","some_username":"testuser"}`
  54. ret[databagItemName] = jsonstring
  55. case dataBagName == "databag03" && databagItemName == testitem:
  56. jsonMap := make(map[string]string)
  57. jsonMap["id"] = testitem
  58. jsonMap["findProperty"] = "foundProperty"
  59. return jsonMap, nil
  60. case dataBagName == DatabagName && databagItemName == testitem:
  61. return math.Inf(1), nil
  62. default:
  63. str := "https://chef.com/organizations/dev/data/" + dataBagName + "/" + databagItemName + ": 404"
  64. return nil, errors.New(str)
  65. }
  66. return ret, nil
  67. }
  68. }
  69. }
  70. func (mc *ChefMockClient) WithListItems(_ string, _ error) {
  71. if mc != nil {
  72. mc.listItems = func(databagName string) (data *chef.DataBagListResult, err error) {
  73. ret := make(chef.DataBagListResult)
  74. if databagName == DatabagName {
  75. jsonstring := fmt.Sprintf("https://chef.com/organizations/dev/data/%s/item01", databagName)
  76. ret["item01"] = jsonstring
  77. } else {
  78. return nil, fmt.Errorf("data bag not found: %s", databagName)
  79. }
  80. return &ret, nil
  81. }
  82. }
  83. }
  84. func (mc *ChefMockClient) WithUser(_ string, _ error) {
  85. if mc != nil {
  86. mc.getUser = func(name string) (user chef.User, err error) {
  87. return chef.User{
  88. UserName: name,
  89. }, nil
  90. }
  91. }
  92. }