Skip to content

Commit

Permalink
feat: convert '+' sign in filenames (#40)
Browse files Browse the repository at this point in the history
replace with '-', as we already do with ':'

Signed-off-by: Xavier Coulon <[email protected]>
  • Loading branch information
xcoulon authored Jun 6, 2024
1 parent 6cabf40 commit 88dface
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/cmd/generate/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ func filePath(rootDir string, obj runtimeclient.Object, resource string) string
path := filepath.Join(dirPath, fmt.Sprintf("%s.yaml", obj.GetName()))
// make sure that `path` does not contain `:` characters
path = strings.ReplaceAll(path, ":", "-")
path = strings.ReplaceAll(path, "+", "-")
return path
}

Expand Down
64 changes: 64 additions & 0 deletions pkg/cmd/generate/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import (
"testing"

"github.com/kubesaw/ksctl/pkg/configuration"
userv1 "github.com/openshift/api/user/v1"
v1 "github.com/openshift/api/user/v1"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/scheme"
runtimeclient "sigs.k8s.io/controller-runtime/pkg/client"
)
Expand Down Expand Up @@ -301,3 +303,65 @@ func TestWriteManifest(t *testing.T) {
})
}
}

func TestFilePath(t *testing.T) {

rootDir := "/path/to"

t.Run("cluster-scoped", func(t *testing.T) {
t.Run("regular name", func(t *testing.T) {
// given
user := &userv1.User{
ObjectMeta: metav1.ObjectMeta{
Name: "my-user",
},
}
// when
f := filePath(rootDir, user, "users")
// then
assert.Equal(t, "/path/to/cluster-scoped/users/my-user.yaml", f)
})
t.Run("name with colon", func(t *testing.T) {
// given
identity := &userv1.Identity{
ObjectMeta: metav1.ObjectMeta{
Name: "sso:my-identity",
},
}
// when
f := filePath(rootDir, identity, "identities")
// then
assert.Equal(t, "/path/to/cluster-scoped/identities/sso-my-identity.yaml", f)
})
})

t.Run("namespace-scoped", func(t *testing.T) {
t.Run("regular name", func(t *testing.T) {
// given
sa := &corev1.ServiceAccount{
ObjectMeta: metav1.ObjectMeta{
Namespace: "my-ns",
Name: "my-sa",
},
}
// when
f := filePath(rootDir, sa, "serviceaccounts")
// then
assert.Equal(t, "/path/to/namespace-scoped/my-ns/serviceaccounts/my-sa.yaml", f)
})
t.Run("name with plus sign", func(t *testing.T) {
// given
sa := &corev1.ServiceAccount{
ObjectMeta: metav1.ObjectMeta{
Namespace: "my-ns",
Name: "my+sa",
},
}
// when
f := filePath(rootDir, sa, "serviceaccounts")
// then
assert.Equal(t, "/path/to/namespace-scoped/my-ns/serviceaccounts/my-sa.yaml", f)
})
})

}

0 comments on commit 88dface

Please sign in to comment.