Skip to content

Commit fba6df7

Browse files
committed
commands/.../build{,_test}.go: add unit test for verifyDeploymentImage
1 parent 276a62f commit fba6df7

File tree

2 files changed

+125
-5
lines changed

2 files changed

+125
-5
lines changed

commands/operator-sdk/cmd/build.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package cmd
1616

1717
import (
1818
"bytes"
19+
"errors"
1920
"fmt"
2021
"io/ioutil"
2122
"log"
@@ -68,14 +69,14 @@ For example:
6869
* image. As it is possible for a namespaced yaml to have multiple deployments (such as the vault
6970
* operator, which depends on the etcd-operator), this is just a warning, not a fatal error.
7071
*/
71-
func verifyDeploymentImage(yamlFile []byte, imageName string) string {
72+
func verifyDeploymentImage(yamlFile []byte, imageName string) error {
7273
warningMessages := ""
7374
yamlSplit := bytes.Split(yamlFile, []byte("\n---\n"))
7475
for _, yamlSpec := range yamlSplit {
7576
yamlMap := make(map[string]interface{})
7677
err := yaml.Unmarshal(yamlSpec, &yamlMap)
7778
if err != nil {
78-
return fmt.Sprintf("WARNING: Could not unmarshal yaml namespaced spec")
79+
return fmt.Errorf("WARNING: Could not unmarshal yaml namespaced spec")
7980
}
8081
kind, ok := yamlMap["kind"].(string)
8182
if !ok {
@@ -110,10 +111,13 @@ func verifyDeploymentImage(yamlFile []byte, imageName string) string {
110111
}
111112
}
112113
}
113-
return warningMessages
114+
if warningMessages == "" {
115+
return nil
116+
}
117+
return errors.New(warningMessages)
114118
}
115119

116-
func renderTestManifest(image string) string {
120+
func renderTestManifest(image string) error {
117121
namespacedBytes, err := ioutil.ReadFile(namespacedManBuild)
118122
if err != nil {
119123
log.Fatalf("could not read rbac manifest: %v", err)
@@ -168,7 +172,7 @@ func buildFunc(cmd *cobra.Command, args []string) {
168172
fmt.Fprintln(os.Stdout, string(o))
169173
// create test-pod.yaml as well as check image name of deployments in namespaced manifest
170174
genWarning := renderTestManifest(image)
171-
if genWarning != "" {
175+
if genWarning != nil {
172176
fmt.Printf("%s\n", genWarning)
173177
}
174178
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
// Copyright 2018 The Operator-SDK Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package cmd
16+
17+
import "testing"
18+
19+
var memcachedNamespaceManExample = `apiVersion: v1
20+
kind: ServiceAccount
21+
metadata:
22+
name: memcached-operator
23+
24+
---
25+
26+
kind: Role
27+
apiVersion: rbac.authorization.k8s.io/v1beta1
28+
metadata:
29+
name: memcached-operator
30+
rules:
31+
- apiGroups:
32+
- cache.example.com
33+
resources:
34+
- "*"
35+
verbs:
36+
- "*"
37+
- apiGroups:
38+
- ""
39+
resources:
40+
- pods
41+
- services
42+
- endpoints
43+
- persistentvolumeclaims
44+
- events
45+
- configmaps
46+
- secrets
47+
verbs:
48+
- "*"
49+
- apiGroups:
50+
- apps
51+
resources:
52+
- deployments
53+
- daemonsets
54+
- replicasets
55+
- statefulsets
56+
verbs:
57+
- "*"
58+
59+
---
60+
61+
kind: RoleBinding
62+
apiVersion: rbac.authorization.k8s.io/v1beta1
63+
metadata:
64+
name: memcached-operator
65+
subjects:
66+
- kind: ServiceAccount
67+
name: memcached-operator
68+
roleRef:
69+
kind: Role
70+
name: memcached-operator
71+
apiGroup: rbac.authorization.k8s.io
72+
73+
---
74+
75+
apiVersion: apps/v1
76+
kind: Deployment
77+
metadata:
78+
name: memcached-operator
79+
spec:
80+
replicas: 1
81+
selector:
82+
matchLabels:
83+
name: memcached-operator
84+
template:
85+
metadata:
86+
labels:
87+
name: memcached-operator
88+
spec:
89+
serviceAccountName: memcached-operator
90+
containers:
91+
- name: memcached-operator
92+
image: quay.io/coreos/operator-sdk-dev:test-framework-operator
93+
ports:
94+
- containerPort: 60000
95+
name: metrics
96+
command:
97+
- memcached-operator
98+
imagePullPolicy: Always
99+
env:
100+
- name: WATCH_NAMESPACE
101+
valueFrom:
102+
fieldRef:
103+
fieldPath: metadata.namespace
104+
- name: OPERATOR_NAME
105+
value: "memcached-operator"
106+
107+
`
108+
109+
func TestVerifyDeploymentImage(t *testing.T) {
110+
if err := verifyDeploymentImage([]byte(memcachedNamespaceManExample), "quay.io/coreos/operator-sdk-dev:test-framework-operator"); err != nil {
111+
t.Fatalf("verifyDeploymentImage incorrectly reported an error: %v", err)
112+
}
113+
if err := verifyDeploymentImage([]byte(memcachedNamespaceManExample), "different-image-name"); err == nil {
114+
t.Fatal("verifyDeploymentImage did not report an error on an incorrect manifest")
115+
}
116+
}

0 commit comments

Comments
 (0)