fake_test.go 2.1 KB

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