1
0
mirror of synced 2026-02-13 13:06:56 +00:00

check for correct kustomization in multi-doc yaml

Signed-off-by: Somtochi Onyekwere <somtochionyekwere@gmail.com>
This commit is contained in:
Somtochi Onyekwere
2022-06-01 11:26:01 +01:00
parent 56c5e784fb
commit 355ed94852
6 changed files with 122 additions and 4 deletions

View File

@@ -17,6 +17,7 @@ limitations under the License.
package build
import (
"strings"
"testing"
"github.com/google/go-cmp/cmp"
@@ -157,3 +158,60 @@ type: kubernetes.io/dockerconfigjson
})
}
}
func Test_unMarshallKustomization(t *testing.T) {
tests := []struct {
name string
localKsFile string
wantErr bool
errString string
}{
{
name: "valid kustomization",
localKsFile: "testdata/local-kustomization/valid.yaml",
},
{
name: "Multi-doc yaml containing kustomization and other resources",
localKsFile: "testdata/local-kustomization/multi-doc-valid.yaml",
},
{
name: "no namespace",
localKsFile: "testdata/local-kustomization/no-ns.yaml",
},
{
name: "kustomization with a different name",
localKsFile: "testdata/local-kustomization/different-name.yaml",
wantErr: true,
errString: "failed find kustomization with name",
},
}
b := &Builder{
name: "podinfo",
namespace: "flux-system",
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
b.kustomizationFile = tt.localKsFile
ks, err := b.unMarshallKustomization()
if !tt.wantErr {
if err != nil {
t.Fatalf("unexpected err '%s'", err)
}
if ks.Name != b.name && ks.Namespace != b.namespace {
t.Errorf("expected kustomization '%s/%s' to match '%s/%s'",
ks.Name, ks.Namespace, b.name, b.namespace)
}
} else {
if err == nil {
t.Fatal("expected error but got nil")
}
if !strings.Contains(err.Error(), tt.errString) {
t.Errorf("expected error '%s' to contain string '%s'", err.Error(), tt.errString)
}
}
})
}
}