uuid_test.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 uuid
  14. import (
  15. "testing"
  16. "github.com/stretchr/testify/assert"
  17. apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
  18. )
  19. func TestGenerate(t *testing.T) {
  20. type args struct {
  21. jsonSpec *apiextensions.JSON
  22. }
  23. tests := []struct {
  24. name string
  25. g *Generator
  26. args args
  27. wantErr bool
  28. }{
  29. {
  30. name: "generate UUID successfully",
  31. args: args{
  32. jsonSpec: &apiextensions.JSON{Raw: []byte(`{}`)},
  33. },
  34. wantErr: false,
  35. },
  36. {
  37. name: "no json spec should not result in error",
  38. args: args{
  39. jsonSpec: nil,
  40. },
  41. wantErr: false,
  42. },
  43. }
  44. for _, tt := range tests {
  45. t.Run(tt.name, func(t *testing.T) {
  46. g := &Generator{}
  47. got, _, err := g.generate(tt.args.jsonSpec, generateUUID)
  48. if (err != nil) != tt.wantErr {
  49. t.Errorf("Generator.Generate() error = %v, wantErr %v", err, tt.wantErr)
  50. return
  51. }
  52. if err == nil {
  53. // Basic validation that the generated string looks like a UUID
  54. assert.Regexp(t, `[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}`, string(got["uuid"]), "Generated string must be a valid UUID")
  55. }
  56. })
  57. }
  58. }