| 1 |
// Copyright 2019 The Kubernetes Authors. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package main_test |
| 5 |
|
| 6 |
import ( |
| 7 |
"testing" |
| 8 |
|
| 9 |
kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest" |
| 10 |
"sigs.k8s.io/kustomize/api/types" |
| 11 |
) |
| 12 |
|
| 13 |
func TestReplicaCountTransformer(t *testing.T) { |
| 14 |
th := kusttest_test.MakeEnhancedHarness(t). |
| 15 |
PrepBuiltin("ReplicaCountTransformer") |
| 16 |
defer th.Reset() |
| 17 |
|
| 18 |
rm := th.LoadAndRunTransformer(` |
| 19 |
apiVersion: builtin |
| 20 |
kind: ReplicaCountTransformer |
| 21 |
metadata: |
| 22 |
name: notImportantHere |
| 23 |
|
| 24 |
replica: |
| 25 |
name: myapp |
| 26 |
count: 23 |
| 27 |
fieldSpecs: |
| 28 |
- path: spec/replicas |
| 29 |
create: true |
| 30 |
kind: Deployment |
| 31 |
|
| 32 |
- path: spec/replicas |
| 33 |
create: true |
| 34 |
kind: ReplicationController |
| 35 |
|
| 36 |
- path: spec/replicas |
| 37 |
create: true |
| 38 |
kind: ReplicaSet |
| 39 |
|
| 40 |
- path: spec/replicas |
| 41 |
create: true |
| 42 |
kind: StatefulSet |
| 43 |
`, ` |
| 44 |
apiVersion: apps/v1 |
| 45 |
kind: Service |
| 46 |
metadata: |
| 47 |
name: myapp |
| 48 |
spec: |
| 49 |
ports: |
| 50 |
- port: 1111 |
| 51 |
targetport: 1111 |
| 52 |
--- |
| 53 |
apiVersion: apps/v1 |
| 54 |
kind: Deployment |
| 55 |
metadata: |
| 56 |
name: otherapp |
| 57 |
spec: |
| 58 |
replicas: 5 |
| 59 |
--- |
| 60 |
apiVersion: apps/v1 |
| 61 |
kind: Deployment |
| 62 |
metadata: |
| 63 |
name: myapp |
| 64 |
spec: |
| 65 |
replicas: 5 |
| 66 |
--- |
| 67 |
apiVersion: apps/v1 |
| 68 |
kind: StatefulSet |
| 69 |
metadata: |
| 70 |
name: myapp |
| 71 |
spec: |
| 72 |
selector: |
| 73 |
matchLabels: |
| 74 |
app: app |
| 75 |
--- |
| 76 |
apiVersion: apps/v1 |
| 77 |
kind: ReplicaSet |
| 78 |
metadata: |
| 79 |
name: myapp |
| 80 |
spec: |
| 81 |
selector: |
| 82 |
matchLabels: |
| 83 |
app: app |
| 84 |
--- |
| 85 |
apiVersion: apps/v1 |
| 86 |
kind: ReplicationController |
| 87 |
metadata: |
| 88 |
name: myapp |
| 89 |
spec: |
| 90 |
selector: |
| 91 |
matchLabels: |
| 92 |
app: app |
| 93 |
`) |
| 94 |
|
| 95 |
th.AssertActualEqualsExpectedNoIdAnnotations(rm, ` |
| 96 |
apiVersion: apps/v1 |
| 97 |
kind: Service |
| 98 |
metadata: |
| 99 |
name: myapp |
| 100 |
spec: |
| 101 |
ports: |
| 102 |
- port: 1111 |
| 103 |
targetport: 1111 |
| 104 |
--- |
| 105 |
apiVersion: apps/v1 |
| 106 |
kind: Deployment |
| 107 |
metadata: |
| 108 |
name: otherapp |
| 109 |
spec: |
| 110 |
replicas: 5 |
| 111 |
--- |
| 112 |
apiVersion: apps/v1 |
| 113 |
kind: Deployment |
| 114 |
metadata: |
| 115 |
name: myapp |
| 116 |
spec: |
| 117 |
replicas: 23 |
| 118 |
--- |
| 119 |
apiVersion: apps/v1 |
| 120 |
kind: StatefulSet |
| 121 |
metadata: |
| 122 |
name: myapp |
| 123 |
spec: |
| 124 |
replicas: 23 |
| 125 |
selector: |
| 126 |
matchLabels: |
| 127 |
app: app |
| 128 |
--- |
| 129 |
apiVersion: apps/v1 |
| 130 |
kind: ReplicaSet |
| 131 |
metadata: |
| 132 |
name: myapp |
| 133 |
spec: |
| 134 |
replicas: 23 |
| 135 |
selector: |
| 136 |
matchLabels: |
| 137 |
app: app |
| 138 |
--- |
| 139 |
apiVersion: apps/v1 |
| 140 |
kind: ReplicationController |
| 141 |
metadata: |
| 142 |
name: myapp |
| 143 |
spec: |
| 144 |
replicas: 23 |
| 145 |
selector: |
| 146 |
matchLabels: |
| 147 |
app: app |
| 148 |
`) |
| 149 |
} |
| 150 |
|
| 151 |
func TestMatchesCurrentID(t *testing.T) { |
| 152 |
th := kusttest_test.MakeEnhancedHarness(t) |
| 153 |
th.GetPluginConfig().BpLoadingOptions = types.BploUseStaticallyLinked |
| 154 |
defer th.Reset() |
| 155 |
|
| 156 |
rm := th.LoadAndRunTransformer(` |
| 157 |
apiVersion: builtin |
| 158 |
kind: PrefixSuffixTransformer |
| 159 |
metadata: |
| 160 |
name: notImportantHere |
| 161 |
suffix: -test |
| 162 |
fieldSpecs: |
| 163 |
- path: metadata/name |
| 164 |
`, ` |
| 165 |
apiVersion: apps/v1 |
| 166 |
kind: Deployment |
| 167 |
metadata: |
| 168 |
name: deployment`) |
| 169 |
|
| 170 |
rm, _ = th.RunTransformerFromResMap(` |
| 171 |
apiVersion: builtin |
| 172 |
kind: ReplicaCountTransformer |
| 173 |
metadata: |
| 174 |
name: notImportantHere |
| 175 |
|
| 176 |
replica: |
| 177 |
name: deployment-test |
| 178 |
count: 23 |
| 179 |
fieldSpecs: |
| 180 |
- path: spec/replicas |
| 181 |
create: true |
| 182 |
kind: Deployment`, rm) |
| 183 |
th.AssertActualEqualsExpectedNoIdAnnotations(rm, ` |
| 184 |
apiVersion: apps/v1 |
| 185 |
kind: Deployment |
| 186 |
metadata: |
| 187 |
name: deployment-test |
| 188 |
spec: |
| 189 |
replicas: 23 |
| 190 |
`) |
| 191 |
} |
| 192 |
|
| 193 |
func TestNoMatch(t *testing.T) { |
| 194 |
th := kusttest_test.MakeEnhancedHarness(t). |
| 195 |
PrepBuiltin("ReplicaCountTransformer") |
| 196 |
defer th.Reset() |
| 197 |
|
| 198 |
err := th.ErrorFromLoadAndRunTransformer(` |
| 199 |
apiVersion: builtin |
| 200 |
kind: ReplicaCountTransformer |
| 201 |
metadata: |
| 202 |
name: notImportantHere |
| 203 |
replica: |
| 204 |
name: service |
| 205 |
count: 3 |
| 206 |
fieldSpecs: |
| 207 |
- path: spec/replicas |
| 208 |
create: true |
| 209 |
kind: Deployment |
| 210 |
`, ` |
| 211 |
kind: Service |
| 212 |
metadata: |
| 213 |
name: service |
| 214 |
spec: |
| 215 |
`) |
| 216 |
|
| 217 |
if err == nil { |
| 218 |
t.Fatalf("No match should return an error") |
| 219 |
} |
| 220 |
if err.Error() != |
| 221 |
"resource with name service does not match a config with the following GVK [Deployment.[noVer].[noGrp]]" { |
| 222 |
t.Fatalf("Unexpected error: %v", err) |
| 223 |
} |
| 224 |
} |