fix(kyaml): add lock for schema related globals

This commit is contained in:
Patryk Małek
2023-01-10 12:40:15 +01:00
parent af3e8ee353
commit f7c3fce6a5
3 changed files with 105 additions and 11 deletions

View File

@@ -6,10 +6,12 @@ package openapi
import (
"fmt"
"os"
"sync"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"sigs.k8s.io/kustomize/kyaml/yaml"
)
@@ -120,7 +122,7 @@ openAPI:
if !assert.NoError(t, err) {
t.FailNow()
}
if !assert.NoError(t, os.WriteFile(f.Name(), []byte(inputyaml), 0600)) {
if !assert.NoError(t, os.WriteFile(f.Name(), []byte(inputyaml), 0o600)) {
t.FailNow()
}
@@ -172,7 +174,7 @@ openAPI:
if !assert.NoError(t, err) {
t.FailNow()
}
if !assert.NoError(t, os.WriteFile(f.Name(), []byte(inputyaml), 0600)) {
if !assert.NoError(t, os.WriteFile(f.Name(), []byte(inputyaml), 0o600)) {
t.FailNow()
}
@@ -207,7 +209,7 @@ kind: Example
if !assert.NoError(t, err) {
t.FailNow()
}
if !assert.NoError(t, os.WriteFile(f.Name(), []byte(inputyaml), 0600)) {
if !assert.NoError(t, os.WriteFile(f.Name(), []byte(inputyaml), 0o600)) {
t.FailNow()
}
@@ -325,3 +327,69 @@ func TestIsNamespaceScoped_custom(t *testing.T) {
assert.True(t, isFound)
assert.True(t, isNamespaceable)
}
func TestCanSetAndResetSchemaConcurrently(t *testing.T) {
t.Run("SetSchema doesn't cause a data race when called concurrently", func(t *testing.T) {
set := func(wg *sync.WaitGroup) {
defer wg.Done()
err := SetSchema(
map[string]string{
"/apis/custom.io/v1": "true",
},
[]byte(`
{
"definitions": {},
"paths": {
"/apis/custom.io/v1/namespaces/{namespace}/customs/{name}": {
"get": {
"x-kubernetes-action": "get",
"x-kubernetes-group-version-kind": {
"group": "custom.io",
"kind": "Custom",
"version": "v1"
}
}
},
"/apis/custom.io/v1/clustercustoms": {
"get": {
"x-kubernetes-action": "get",
"x-kubernetes-group-version-kind": {
"group": "custom.io",
"kind": "ClusterCustom",
"version": "v1"
}
}
}
}
}
`),
true,
)
require.NoError(t, err)
}
var wg sync.WaitGroup
require.NotPanics(t, func() {
for i := 0; i < 100; i++ {
wg.Add(1)
go set(&wg)
}
})
wg.Wait()
})
t.Run("ResetOpenAPI doesn't cause a data race when called concurrently", func(t *testing.T) {
reset := func(wg *sync.WaitGroup) {
defer wg.Done()
ResetOpenAPI()
}
var wg sync.WaitGroup
require.NotPanics(t, func() {
for i := 0; i < 100; i++ {
wg.Add(1)
go reset(&wg)
}
})
wg.Wait()
})
}