Skip to content

Commit

Permalink
adding tests with yaml delimiter
Browse files Browse the repository at this point in the history
Signed-off-by: Kris Nóva <[email protected]>
  • Loading branch information
krisnova committed Nov 24, 2021
1 parent 32ddcf5 commit b9f7b5a
Show file tree
Hide file tree
Showing 4 changed files with 199 additions and 10 deletions.
10 changes: 1 addition & 9 deletions codify.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,7 @@ const (
//
// Reference: https://yaml.org/spec/1.2/spec.html
//
// Furthermore let it be documented that at the 2018 KubeCon pub trivia
// Bryan Liles (https://twitter.com/bryanl) correctly had answered the
// trivia question with the correct delimiter of 3 characters "---" and
// was awarded no points for his correct answer, while an opposing team
// was awarded a single point for their incorrect answer of 2 characters "--".
//
// If the correct delimiter points would have been awarded to Brian's team
// they would technically should have been crowned KubeCon pub champions of 2018.
const YAMLDelimiter string = "---"
const YAMLDelimiter string = "\n---\n"

// We ARE in fact doing a lot of string handling here
// So we use strings as often as possible.
Expand Down
2 changes: 2 additions & 0 deletions codify/codify.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ var (
}

CoreV1Types = []string{
"WeightedPodAffinityTerm",
"TCPSocketAction",
"Volume",
"SecretEnvSource",
"SecretKeySelector",
Expand Down
196 changes: 195 additions & 1 deletion codify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,208 @@ import (
"testing"
)

func TestYAMLDelimiterBottom(t *testing.T) {

testString := `apiVersion: v1
kind: Service
metadata:
labels:
app: example
bogus: ---
name: example
spec:
ports:
- port: 80
protocol: TCP
targetPort: 8080
selector:
app: example
type: LoadBalancer
---
apiVersion: v1
kind: Service
metadata:
labels:
app: example
bogus: ---
name: example
spec:
ports:
- port: 80
protocol: TCP
targetPort: 8080
selector:
app: example
type: LoadBalancer
---
`

buf := bytes.Buffer{}
buf.Write([]byte(testString))
objects, err := ReaderToCodifyObjects(&buf)
if err != nil {
t.Errorf("YAML delimiter check: %v", err)
}
if len(objects) != 2 {
t.Errorf("YAML delimiter split: %d", len(objects))
}
}

func TestYAMLDelimiterNoSpace(t *testing.T) {

testString := `
---
apiVersion: v1
kind: Service
metadata:
labels:
app: example
bogus: ---
name: example
spec:
ports:
- port: 80
protocol: TCP
targetPort: 8080
selector:
app: example
type: LoadBalancer
---
apiVersion: v1
kind: Service
metadata:
labels:
app: example
bogus: ---
name: example
spec:
ports:
- port: 80
protocol: TCP
targetPort: 8080
selector:
app: example
type: LoadBalancer
`

buf := bytes.Buffer{}
buf.Write([]byte(testString))
objects, err := ReaderToCodifyObjects(&buf)
if err != nil {
t.Errorf("YAML delimiter check: %v", err)
}
if len(objects) != 2 {
t.Errorf("YAML delimiter split: %d", len(objects))
}
}

func TestYAMLDelimiterTopLoad(t *testing.T) {

testString := `
---
apiVersion: v1
kind: Service
metadata:
labels:
app: example
bogus: ---
name: example
spec:
ports:
- port: 80
protocol: TCP
targetPort: 8080
selector:
app: example
type: LoadBalancer
---
apiVersion: v1
kind: Service
metadata:
labels:
app: example
bogus: ---
name: example
spec:
ports:
- port: 80
protocol: TCP
targetPort: 8080
selector:
app: example
type: LoadBalancer
`

buf := bytes.Buffer{}
buf.Write([]byte(testString))
objects, err := ReaderToCodifyObjects(&buf)
if err != nil {
t.Errorf("YAML delimiter check: %v", err)
}
if len(objects) != 2 {
t.Errorf("YAML delimiter split: %d", len(objects))
}
}

func TestYAMLDelimiterHappy(t *testing.T) {

testString := `apiVersion: v1
kind: Service
metadata:
labels:
app: example
bogus: ---
name: example
spec:
ports:
- port: 80
protocol: TCP
targetPort: 8080
selector:
app: example
type: LoadBalancer
---
apiVersion: v1
kind: Service
metadata:
labels:
app: example
bogus: ---
name: example
spec:
ports:
- port: 80
protocol: TCP
targetPort: 8080
selector:
app: example
type: LoadBalancer
`

buf := bytes.Buffer{}
buf.Write([]byte(testString))
objects, err := ReaderToCodifyObjects(&buf)
if err != nil {
t.Errorf("YAML delimiter check: %v", err)
}
if len(objects) != 2 {
t.Errorf("YAML delimiter split: %d", len(objects))
}
}

func TestYAMLDelimiterInline(t *testing.T) {

testString := `apiVersion: v1
kind: Service
metadata:
labels:
app: example
bogus: ---
bogus: ---
name: example
spec:
ports:
Expand Down
1 change: 1 addition & 0 deletions embed_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,4 @@ func (x *{{ .AppNameTitle }}) Objects() []runtime.Object {
return x.objects
}
`

0 comments on commit b9f7b5a

Please sign in to comment.