Add new constructor to create documents
The change would allow to build a document object from bytes Change-Id: I15c13a960b4e989cffa4525c85b8f32f2a51a0e1 Relates-To: #238 Relates-To: #279 Closes: #279
This commit is contained in:
parent
6a896704b5
commit
36facd30eb
@ -31,6 +31,9 @@ const (
|
||||
SecretKind = "Secret"
|
||||
BareMetalHostKind = "BareMetalHost"
|
||||
|
||||
ConfigMapKind = "ConfigMap"
|
||||
ConfigMapVersion = "v1"
|
||||
|
||||
ClusterctlMetadataKind = "Metadata"
|
||||
ClusterctlMetadataVersion = "v1alpha3"
|
||||
ClusterctlMetadataGroup = "clusterctl.cluster.x-k8s.io"
|
||||
|
@ -15,6 +15,7 @@
|
||||
package document
|
||||
|
||||
import (
|
||||
"sigs.k8s.io/kustomize/api/k8sdeps/kunstruct"
|
||||
"sigs.k8s.io/kustomize/api/resource"
|
||||
"sigs.k8s.io/yaml"
|
||||
)
|
||||
@ -198,3 +199,14 @@ func NewDocument(r *resource.Resource) (Document, error) {
|
||||
err := doc.SetKustomizeResource(r)
|
||||
return doc, err
|
||||
}
|
||||
|
||||
// NewDocumentFromBytes constructs document from bytes
|
||||
func NewDocumentFromBytes(b []byte) (Document, error) {
|
||||
res, err := resource.NewFactory(kunstruct.NewKunstructuredFactoryImpl()).FromBytes(b)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
doc := &Factory{}
|
||||
err = doc.SetKustomizeResource(res)
|
||||
return doc, err
|
||||
}
|
||||
|
@ -158,3 +158,46 @@ func TestDocument(t *testing.T) {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestNewDocumentFromBytes(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
stringData string
|
||||
expectErr bool
|
||||
expectedDocName string
|
||||
}{
|
||||
{
|
||||
name: "ConfigMap",
|
||||
stringData: `apiVersion: v1
|
||||
kind: Secret
|
||||
metadata:
|
||||
name: control-0-bmc
|
||||
namespace: metal3
|
||||
type: Opaque
|
||||
stringData:
|
||||
username: username
|
||||
password: password`,
|
||||
expectErr: false,
|
||||
expectedDocName: "control-0-bmc",
|
||||
},
|
||||
{
|
||||
name: "Manformed Bytes",
|
||||
stringData: `
|
||||
broken:fas -<
|
||||
fasd`,
|
||||
expectErr: true,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
doc, err := document.NewDocumentFromBytes([]byte(tt.stringData))
|
||||
if tt.expectErr {
|
||||
assert.Error(t, err)
|
||||
} else {
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, doc.GetName(), tt.expectedDocName)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user