File: pod_test.go

package info (click to toggle)
golang-github-appc-spec 0.8.9%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,108 kB
  • ctags: 1,103
  • sloc: sh: 163; makefile: 17
file content (202 lines) | stat: -rw-r--r-- 6,322 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// Copyright 2015 The appc Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package lastditch

import (
	"fmt"
	"reflect"
	"strings"
	"testing"
)

func TestInvalidPodManifest(t *testing.T) {
	tests := []struct {
		desc     string
		json     string
		expected PodManifest
	}{
		{
			desc:     "Check an empty pod manifest",
			json:     podJ(appsJ(), ""),
			expected: podI(appsI()),
		},
		{
			desc:     "Check a pod manifest with an empty app",
			json:     podJ(appsJ(appJ("", rImgJ("i", "id", labsJ(), ""), "")), ""),
			expected: podI(appsI(appI("", rImgI("i", "id", labsI())))),
		},
		{
			desc:     "Check a pod manifest with an app based on an empty image",
			json:     podJ(appsJ(appJ("a", rImgJ("", "", labsJ(), ""), "")), ""),
			expected: podI(appsI(appI("a", rImgI("", "", labsI())))),
		},
		{
			desc:     "Check a pod manifest with an app based on an image containing an empty label",
			json:     podJ(appsJ(appJ("a", rImgJ("", "", labsJ(labJ("", "", "")), ""), "")), ""),
			expected: podI(appsI(appI("a", rImgI("", "", labsI(labI("", "")))))),
		},
		{
			desc:     "Check a pod manifest with an invalid app name",
			json:     podJ(appsJ(appJ("!", rImgJ("i", "id", labsJ(), ""), "")), ""),
			expected: podI(appsI(appI("!", rImgI("i", "id", labsI())))),
		},
		{
			desc:     "Check a pod manifest with duplicated app names",
			json:     podJ(appsJ(appJ("a", rImgJ("i", "id", labsJ(), ""), ""), appJ("a", rImgJ("", "", labsJ(), ""), "")), ""),
			expected: podI(appsI(appI("a", rImgI("i", "id", labsI())), appI("a", rImgI("", "", labsI())))),
		},
		{
			desc:     "Check a pod manifest with an invalid image name and ID",
			json:     podJ(appsJ(appJ("?", rImgJ("!!!", "&&&", labsJ(), ""), "")), ""),
			expected: podI(appsI(appI("?", rImgI("!!!", "&&&", labsI())))),
		},
		{
			desc:     "Check a pod manifest with an app based on an image containing labels with invalid names",
			json:     podJ(appsJ(appJ("a", rImgJ("i", "id", labsJ(labJ("!n1", "v1", ""), labJ("N2~", "v2", "")), ""), "")), ""),
			expected: podI(appsI(appI("a", rImgI("i", "id", labsI(labI("!n1", "v1"), labI("N2~", "v2")))))),
		},
		{
			desc:     "Check a pod manifest with an app based on an image containing repeated labels",
			json:     podJ(appsJ(appJ("a", rImgJ("i", "id", labsJ(labJ("n1", "v1", ""), labJ("n1", "v2", "")), ""), "")), ""),
			expected: podI(appsI(appI("a", rImgI("i", "id", labsI(labI("n1", "v1"), labI("n1", "v2")))))),
		},
		{
			desc:     "Check a pod manifest with some extra fields",
			json:     podJ(appsJ(), extJ("goblins")),
			expected: podI(appsI()),
		},
		{
			desc:     "Check a pod manifest with an app containing some extra fields",
			json:     podJ(appsJ(appJ("a", rImgJ("i", "id", labsJ(), ""), extJ("trolls"))), extJ("goblins")),
			expected: podI(appsI(appI("a", rImgI("i", "id", labsI())))),
		},
		{
			desc:     "Check a pod manifest with an app based on an image containing some extra fields",
			json:     podJ(appsJ(appJ("a", rImgJ("i", "id", labsJ(), extJ("stuff")), extJ("trolls"))), extJ("goblins")),
			expected: podI(appsI(appI("a", rImgI("i", "id", labsI())))),
		},
		{
			desc:     "Check a pod manifest with an app based on an image containing labels with some extra fields",
			json:     podJ(appsJ(appJ("a", rImgJ("i", "id", labsJ(labJ("n", "v", extJ("color"))), extJ("stuff")), extJ("trolls"))), extJ("goblins")),
			expected: podI(appsI(appI("a", rImgI("i", "id", labsI(labI("n", "v")))))),
		},
	}
	for _, tt := range tests {
		got := PodManifest{}
		if err := got.UnmarshalJSON([]byte(tt.json)); err != nil {
			t.Errorf("%s: unexpected error during unmarshalling pod manifest: %v", tt.desc, err)
		}
		if !reflect.DeepEqual(tt.expected, got) {
			t.Errorf("%s: did not get expected pod manifest, got:\n  %#v\nexpected:\n  %#v", tt.desc, got, tt.expected)
		}
	}
}

func TestBogusPodManifest(t *testing.T) {
	bogus := []string{
		`
			{
			    "acKind": "Bogus",
			    "acVersion": "0.8.9",
			}
			`,
		`
			<html>
			    <head>
				<title>Certainly not a JSON</title>
			    </head>
			</html>`,
	}

	for _, str := range bogus {
		pm := PodManifest{}
		if pm.UnmarshalJSON([]byte(str)) == nil {
			t.Errorf("bogus pod manifest unmarshalled successfully: %s", str)
		}
	}
}

// podJ returns a pod manifest JSON with given apps
func podJ(apps, extra string) string {
	return fmt.Sprintf(`
		{
		    %s
		    "acKind": "PodManifest",
		    "acVersion": "0.8.9",
		    "apps": %s
		}`, extra, apps)
}

// podI returns a pod manifest instance with given apps
func podI(apps AppList) PodManifest {
	return PodManifest{
		ACVersion: "0.8.9",
		ACKind:    "PodManifest",
		Apps:      apps,
	}
}

// appsJ returns an applist JSON snippet with given apps
func appsJ(apps ...string) string {
	return fmt.Sprintf("[%s]", strings.Join(apps, ","))
}

// appsI returns an applist instance with given apps
func appsI(apps ...RuntimeApp) AppList {
	if apps == nil {
		return AppList{}
	}
	return apps
}

// appJ returns an app JSON snippet with given name and image
func appJ(name, image, extra string) string {
	return fmt.Sprintf(`
		{
		    %s
		    "name": "%s",
		    "image": %s
		}`, extra, name, image)
}

// appI returns an app instance with given name and image
func appI(name string, image RuntimeImage) RuntimeApp {
	return RuntimeApp{
		Name:  name,
		Image: image,
	}
}

// rImgJ returns a runtime image JSON snippet with given name, id and
// labels
func rImgJ(name, id, labels, extra string) string {
	return fmt.Sprintf(`
		{
		    %s
		    "name": "%s",
		    "id": "%s",
		    "labels": %s
		}`, extra, name, id, labels)
}

// rImgI returns a runtime image instance with given name, id and
// labels
func rImgI(name, id string, labels Labels) RuntimeImage {
	return RuntimeImage{
		Name:   name,
		ID:     id,
		Labels: labels,
	}
}