fake_test.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. Copyright © 2025 ESO Maintainer Team
  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. "context"
  16. "reflect"
  17. "testing"
  18. apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
  19. "sigs.k8s.io/controller-runtime/pkg/client"
  20. )
  21. func TestGenerate(t *testing.T) {
  22. type args struct {
  23. ctx context.Context
  24. jsonSpec *apiextensions.JSON
  25. kube client.Client
  26. namespace string
  27. }
  28. tests := []struct {
  29. name string
  30. args args
  31. want map[string][]byte
  32. wantErr bool
  33. }{
  34. {
  35. name: "no spec",
  36. args: args{
  37. jsonSpec: nil,
  38. },
  39. wantErr: true,
  40. },
  41. {
  42. name: "invalid json",
  43. args: args{
  44. jsonSpec: &apiextensions.JSON{
  45. Raw: []byte(``),
  46. },
  47. },
  48. wantErr: true,
  49. },
  50. {
  51. name: "empty json produces empty map",
  52. args: args{
  53. jsonSpec: &apiextensions.JSON{
  54. Raw: []byte(`{}`),
  55. },
  56. },
  57. want: make(map[string][]byte),
  58. wantErr: false,
  59. },
  60. {
  61. name: "spec with values produces valus",
  62. args: args{
  63. jsonSpec: &apiextensions.JSON{
  64. Raw: []byte(`{"spec":{"data":{"foo":"bar","num":"42"}}}`),
  65. },
  66. },
  67. want: map[string][]byte{
  68. "foo": []byte(`bar`),
  69. "num": []byte(`42`),
  70. },
  71. wantErr: false,
  72. },
  73. }
  74. for _, tt := range tests {
  75. t.Run(tt.name, func(t *testing.T) {
  76. g := &Generator{}
  77. got, _, err := g.Generate(tt.args.ctx, tt.args.jsonSpec, tt.args.kube, tt.args.namespace)
  78. if (err != nil) != tt.wantErr {
  79. t.Errorf("Generator.Generate() error = %v, wantErr %v", err, tt.wantErr)
  80. return
  81. }
  82. if !reflect.DeepEqual(got, tt.want) {
  83. t.Errorf("Generator.Generate() = %v, want %v", got, tt.want)
  84. }
  85. })
  86. }
  87. }